1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_UINT256_H
7 #define BITCOIN_UINT256_H
15 #include "crypto/common.h"
17 /** Template base class for fixed-sized opaque blobs. */
18 template<unsigned int BITS
>
22 enum { WIDTH
=BITS
/8 };
27 memset(data
, 0, sizeof(data
));
30 explicit base_blob(const std::vector
<unsigned char>& vch
);
34 for (int i
= 0; i
< WIDTH
; i
++)
42 memset(data
, 0, sizeof(data
));
45 inline int Compare(const base_blob
& other
) const { return memcmp(data
, other
.data
, sizeof(data
)); }
47 friend inline bool operator==(const base_blob
& a
, const base_blob
& b
) { return a
.Compare(b
) == 0; }
48 friend inline bool operator!=(const base_blob
& a
, const base_blob
& b
) { return a
.Compare(b
) != 0; }
49 friend inline bool operator<(const base_blob
& a
, const base_blob
& b
) { return a
.Compare(b
) < 0; }
51 std::string
GetHex() const;
52 void SetHex(const char* psz
);
53 void SetHex(const std::string
& str
);
54 std::string
ToString() const;
56 unsigned char* begin()
66 const unsigned char* begin() const
71 const unsigned char* end() const
76 unsigned int size() const
81 uint64_t GetUint64(int pos
) const
83 const uint8_t* ptr
= data
+ pos
* 8;
84 return ((uint64_t)ptr
[0]) | \
85 ((uint64_t)ptr
[1]) << 8 | \
86 ((uint64_t)ptr
[2]) << 16 | \
87 ((uint64_t)ptr
[3]) << 24 | \
88 ((uint64_t)ptr
[4]) << 32 | \
89 ((uint64_t)ptr
[5]) << 40 | \
90 ((uint64_t)ptr
[6]) << 48 | \
91 ((uint64_t)ptr
[7]) << 56;
94 template<typename Stream
>
95 void Serialize(Stream
& s
) const
97 s
.write((char*)data
, sizeof(data
));
100 template<typename Stream
>
101 void Unserialize(Stream
& s
)
103 s
.read((char*)data
, sizeof(data
));
107 /** 160-bit opaque blob.
108 * @note This type is called uint160 for historical reasons only. It is an opaque
109 * blob of 160 bits and has no integer operations.
111 class uint160
: public base_blob
<160> {
114 uint160(const base_blob
<160>& b
) : base_blob
<160>(b
) {}
115 explicit uint160(const std::vector
<unsigned char>& vch
) : base_blob
<160>(vch
) {}
118 /** 256-bit opaque blob.
119 * @note This type is called uint256 for historical reasons only. It is an
120 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
121 * those are required.
123 class uint256
: public base_blob
<256> {
126 uint256(const base_blob
<256>& b
) : base_blob
<256>(b
) {}
127 explicit uint256(const std::vector
<unsigned char>& vch
) : base_blob
<256>(vch
) {}
129 /** A cheap hash function that just returns 64 bits from the result, it can be
130 * used when the contents are considered uniformly random. It is not appropriate
131 * when the value can easily be influenced from outside as e.g. a network adversary could
132 * provide values to trigger worst-case behavior.
134 uint64_t GetCheapHash() const
136 return ReadLE64(data
);
140 /* uint256 from const char *.
141 * This is a separate function because the constructor uint256(const char*) can result
142 * in dangerously catching uint256(0).
144 inline uint256
uint256S(const char *str
)
150 /* uint256 from std::string.
151 * This is a separate function because the constructor uint256(const std::string &str) can result
152 * in dangerously catching uint256(0) via std::string(const char*).
154 inline uint256
uint256S(const std::string
& str
)
161 #endif // BITCOIN_UINT256_H