]>
Commit | Line | Data |
---|---|---|
8bd66202 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
fa94b9d5 | 3 | // Distributed under the MIT software license, see the accompanying |
bc909a7a | 4 | // file COPYING or https://www.opensource.org/licenses/mit-license.php . |
51ed9ec9 | 5 | |
223b6f1b WL |
6 | #ifndef BITCOIN_UINT256_H |
7 | #define BITCOIN_UINT256_H | |
8 | ||
4d480c8a | 9 | #include <assert.h> |
611116d4 | 10 | #include <cstring> |
4d480c8a | 11 | #include <stdexcept> |
51ed9ec9 | 12 | #include <stdint.h> |
8bd66202 | 13 | #include <string> |
223b6f1b WL |
14 | #include <vector> |
15 | ||
bfc60703 | 16 | /** Template base class for fixed-sized opaque blobs. */ |
8bd66202 | 17 | template<unsigned int BITS> |
bfc60703 | 18 | class base_blob |
8bd66202 | 19 | { |
bc42503f | 20 | protected: |
bfc60703 | 21 | enum { WIDTH=BITS/8 }; |
c4cbee43 | 22 | alignas(uint32_t) uint8_t data[WIDTH]; |
8bd66202 | 23 | public: |
bfc60703 | 24 | base_blob() |
eb2cbd75 | 25 | { |
bfc60703 | 26 | memset(data, 0, sizeof(data)); |
eb2cbd75 PW |
27 | } |
28 | ||
bfc60703 | 29 | explicit base_blob(const std::vector<unsigned char>& vch); |
eb2cbd75 | 30 | |
bfc60703 | 31 | bool IsNull() const |
8bd66202 GA |
32 | { |
33 | for (int i = 0; i < WIDTH; i++) | |
bfc60703 | 34 | if (data[i] != 0) |
8bd66202 GA |
35 | return false; |
36 | return true; | |
37 | } | |
38 | ||
bfc60703 | 39 | void SetNull() |
8bd66202 | 40 | { |
bfc60703 | 41 | memset(data, 0, sizeof(data)); |
8bd66202 GA |
42 | } |
43 | ||
bfc60703 WL |
44 | friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; } |
45 | friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; } | |
46 | friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; } | |
8bd66202 | 47 | |
de79aaa7 PW |
48 | std::string GetHex() const; |
49 | void SetHex(const char* psz); | |
50 | void SetHex(const std::string& str); | |
51 | std::string ToString() const; | |
8bd66202 GA |
52 | |
53 | unsigned char* begin() | |
54 | { | |
bfc60703 | 55 | return &data[0]; |
8bd66202 GA |
56 | } |
57 | ||
58 | unsigned char* end() | |
59 | { | |
bfc60703 | 60 | return &data[WIDTH]; |
8bd66202 GA |
61 | } |
62 | ||
68feac96 MC |
63 | const unsigned char* begin() const |
64 | { | |
bfc60703 | 65 | return &data[0]; |
68feac96 MC |
66 | } |
67 | ||
68 | const unsigned char* end() const | |
69 | { | |
bfc60703 | 70 | return &data[WIDTH]; |
68feac96 MC |
71 | } |
72 | ||
73 | unsigned int size() const | |
8bd66202 | 74 | { |
bfc60703 | 75 | return sizeof(data); |
5fee401f | 76 | } |
8bd66202 | 77 | |
8bd66202 | 78 | template<typename Stream> |
242f1421 | 79 | void Serialize(Stream& s) const |
8bd66202 | 80 | { |
bfc60703 | 81 | s.write((char*)data, sizeof(data)); |
8bd66202 GA |
82 | } |
83 | ||
84 | template<typename Stream> | |
242f1421 | 85 | void Unserialize(Stream& s) |
8bd66202 | 86 | { |
bfc60703 | 87 | s.read((char*)data, sizeof(data)); |
5d3064bc | 88 | } |
8bd66202 GA |
89 | }; |
90 | ||
7614198f JG |
91 | /** 88-bit opaque blob. |
92 | */ | |
93 | class blob88 : public base_blob<88> { | |
94 | public: | |
95 | blob88() {} | |
96 | blob88(const base_blob<88>& b) : base_blob<88>(b) {} | |
97 | explicit blob88(const std::vector<unsigned char>& vch) : base_blob<88>(vch) {} | |
98 | }; | |
99 | ||
bfc60703 WL |
100 | /** 160-bit opaque blob. |
101 | * @note This type is called uint160 for historical reasons only. It is an opaque | |
102 | * blob of 160 bits and has no integer operations. | |
103 | */ | |
104 | class uint160 : public base_blob<160> { | |
8bd66202 | 105 | public: |
eb2cbd75 | 106 | uint160() {} |
bfc60703 WL |
107 | uint160(const base_blob<160>& b) : base_blob<160>(b) {} |
108 | explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {} | |
8bd66202 GA |
109 | }; |
110 | ||
bfc60703 WL |
111 | /** 256-bit opaque blob. |
112 | * @note This type is called uint256 for historical reasons only. It is an | |
113 | * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if | |
114 | * those are required. | |
115 | */ | |
116 | class uint256 : public base_blob<256> { | |
8bd66202 | 117 | public: |
eb2cbd75 | 118 | uint256() {} |
bfc60703 WL |
119 | uint256(const base_blob<256>& b) : base_blob<256>(b) {} |
120 | explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {} | |
121 | ||
122 | /** A cheap hash function that just returns 64 bits from the result, it can be | |
123 | * used when the contents are considered uniformly random. It is not appropriate | |
124 | * when the value can easily be influenced from outside as e.g. a network adversary could | |
125 | * provide values to trigger worst-case behavior. | |
126 | * @note The result of this function is not stable between little and big endian. | |
fa94b9d5 | 127 | */ |
bfc60703 WL |
128 | uint64_t GetCheapHash() const |
129 | { | |
130 | uint64_t result; | |
131 | memcpy((void*)&result, (void*)data, 8); | |
132 | return result; | |
133 | } | |
bc42503f | 134 | |
bfc60703 WL |
135 | /** A more secure, salted hash function. |
136 | * @note This hash is not stable between little and big endian. | |
137 | */ | |
bc42503f | 138 | uint64_t GetHash(const uint256& salt) const; |
8bd66202 GA |
139 | }; |
140 | ||
bfc60703 WL |
141 | /* uint256 from const char *. |
142 | * This is a separate function because the constructor uint256(const char*) can result | |
143 | * in dangerously catching uint256(0). | |
144 | */ | |
145 | inline uint256 uint256S(const char *str) | |
146 | { | |
147 | uint256 rv; | |
148 | rv.SetHex(str); | |
149 | return rv; | |
150 | } | |
151 | /* uint256 from std::string. | |
152 | * This is a separate function because the constructor uint256(const std::string &str) can result | |
153 | * in dangerously catching uint256(0) via std::string(const char*). | |
154 | */ | |
155 | inline uint256 uint256S(const std::string& str) | |
156 | { | |
157 | uint256 rv; | |
158 | rv.SetHex(str); | |
159 | return rv; | |
160 | } | |
5d3064bc | 161 | |
093303a8 | 162 | #endif // BITCOIN_UINT256_H |