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 explicit uint160(const std::vector
<unsigned char>& vch
) : base_blob
<160>(vch
) {}
117 /** 256-bit opaque blob.
118 * @note This type is called uint256 for historical reasons only. It is an
119 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
120 * those are required.
122 class uint256
: public base_blob
<256> {
125 explicit uint256(const std::vector
<unsigned char>& vch
) : base_blob
<256>(vch
) {}
127 /** A cheap hash function that just returns 64 bits from the result, it can be
128 * used when the contents are considered uniformly random. It is not appropriate
129 * when the value can easily be influenced from outside as e.g. a network adversary could
130 * provide values to trigger worst-case behavior.
132 uint64_t GetCheapHash() const
134 return ReadLE64(data
);
138 /* uint256 from const char *.
139 * This is a separate function because the constructor uint256(const char*) can result
140 * in dangerously catching uint256(0).
142 inline uint256
uint256S(const char *str
)
148 /* uint256 from std::string.
149 * This is a separate function because the constructor uint256(const std::string &str) can result
150 * in dangerously catching uint256(0) via std::string(const char*).
152 inline uint256
uint256S(const std::string
& str
)
159 #endif // BITCOIN_UINT256_H