]>
Commit | Line | Data |
---|---|---|
0fb9073e | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2013 The Bitcoin Core developers |
b4347f60 | 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 | |
0fb9073e PW |
6 | #ifndef BITCOIN_HASH_H |
7 | #define BITCOIN_HASH_H | |
8 | ||
a5bc9c09 | 9 | #include "crypto/ripemd160.h" |
36fa4a78 | 10 | #include "crypto/sha256.h" |
42181656 | 11 | #include "crypto/verus_hash.h" |
29a8ade7 | 12 | #include "prevector.h" |
0fb9073e | 13 | #include "serialize.h" |
51ed9ec9 BD |
14 | #include "uint256.h" |
15 | #include "version.h" | |
0fb9073e | 16 | |
132dc81f A |
17 | #include "sodium.h" |
18 | ||
7ab026f4 | 19 | #include <vector> |
0fb9073e | 20 | |
a5748996 CF |
21 | typedef uint256 ChainCode; |
22 | ||
7b4737c8 PW |
23 | /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */ |
24 | class CHash256 { | |
25 | private: | |
26 | CSHA256 sha; | |
27 | public: | |
a0495bb6 PW |
28 | static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; |
29 | ||
30 | void Finalize(unsigned char hash[OUTPUT_SIZE]) { | |
31 | unsigned char buf[sha.OUTPUT_SIZE]; | |
7b4737c8 | 32 | sha.Finalize(buf); |
a0495bb6 | 33 | sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash); |
7b4737c8 PW |
34 | } |
35 | ||
36 | CHash256& Write(const unsigned char *data, size_t len) { | |
37 | sha.Write(data, len); | |
38 | return *this; | |
39 | } | |
40 | ||
41 | CHash256& Reset() { | |
42 | sha.Reset(); | |
43 | return *this; | |
44 | } | |
45 | }; | |
46 | ||
47 | /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */ | |
48 | class CHash160 { | |
49 | private: | |
50 | CSHA256 sha; | |
51 | public: | |
a0495bb6 PW |
52 | static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; |
53 | ||
54 | void Finalize(unsigned char hash[OUTPUT_SIZE]) { | |
55 | unsigned char buf[sha.OUTPUT_SIZE]; | |
7b4737c8 | 56 | sha.Finalize(buf); |
a0495bb6 | 57 | CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash); |
7b4737c8 PW |
58 | } |
59 | ||
60 | CHash160& Write(const unsigned char *data, size_t len) { | |
61 | sha.Write(data, len); | |
62 | return *this; | |
63 | } | |
64 | ||
65 | CHash160& Reset() { | |
66 | sha.Reset(); | |
67 | return *this; | |
68 | } | |
69 | }; | |
70 | ||
71 | /** Compute the 256-bit hash of an object. */ | |
0fb9073e PW |
72 | template<typename T1> |
73 | inline uint256 Hash(const T1 pbegin, const T1 pend) | |
74 | { | |
7b4737c8 PW |
75 | static const unsigned char pblank[1] = {}; |
76 | uint256 result; | |
56fe75cb | 77 | CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&(*pbegin), (pend - pbegin) * sizeof(*pbegin)) |
7b4737c8 PW |
78 | .Finalize((unsigned char*)&result); |
79 | return result; | |
80 | } | |
81 | ||
82 | /** Compute the 256-bit hash of the concatenation of two objects. */ | |
83 | template<typename T1, typename T2> | |
84 | inline uint256 Hash(const T1 p1begin, const T1 p1end, | |
85 | const T2 p2begin, const T2 p2end) { | |
86 | static const unsigned char pblank[1] = {}; | |
87 | uint256 result; | |
88 | CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])) | |
89 | .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])) | |
90 | .Finalize((unsigned char*)&result); | |
91 | return result; | |
92 | } | |
93 | ||
94 | /** Compute the 256-bit hash of the concatenation of three objects. */ | |
95 | template<typename T1, typename T2, typename T3> | |
96 | inline uint256 Hash(const T1 p1begin, const T1 p1end, | |
97 | const T2 p2begin, const T2 p2end, | |
98 | const T3 p3begin, const T3 p3end) { | |
99 | static const unsigned char pblank[1] = {}; | |
100 | uint256 result; | |
101 | CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])) | |
102 | .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])) | |
103 | .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0])) | |
104 | .Finalize((unsigned char*)&result); | |
105 | return result; | |
106 | } | |
107 | ||
108 | /** Compute the 160-bit hash an object. */ | |
109 | template<typename T1> | |
110 | inline uint160 Hash160(const T1 pbegin, const T1 pend) | |
111 | { | |
112 | static unsigned char pblank[1] = {}; | |
113 | uint160 result; | |
114 | CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])) | |
115 | .Finalize((unsigned char*)&result); | |
116 | return result; | |
117 | } | |
118 | ||
119 | /** Compute the 160-bit hash of a vector. */ | |
120 | inline uint160 Hash160(const std::vector<unsigned char>& vch) | |
121 | { | |
122 | return Hash160(vch.begin(), vch.end()); | |
0fb9073e PW |
123 | } |
124 | ||
29a8ade7 PW |
125 | /** Compute the 160-bit hash of a vector. */ |
126 | template<unsigned int N> | |
127 | inline uint160 Hash160(const prevector<N, unsigned char>& vch) | |
128 | { | |
129 | return Hash160(vch.begin(), vch.end()); | |
130 | } | |
131 | ||
7b4737c8 | 132 | /** A writer stream (for serialization) that computes a 256-bit hash. */ |
0fb9073e PW |
133 | class CHashWriter |
134 | { | |
135 | private: | |
7b4737c8 | 136 | CHash256 ctx; |
0fb9073e | 137 | |
7f4acac4 PW |
138 | const int nType; |
139 | const int nVersion; | |
0fb9073e | 140 | public: |
0fb9073e | 141 | |
7b4737c8 | 142 | CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {} |
0fb9073e | 143 | |
242f1421 PW |
144 | int GetType() const { return nType; } |
145 | int GetVersion() const { return nVersion; } | |
146 | ||
1315591c | 147 | void write(const char *pch, size_t size) { |
7b4737c8 | 148 | ctx.Write((const unsigned char*)pch, size); |
0fb9073e PW |
149 | } |
150 | ||
151 | // invalidates the object | |
152 | uint256 GetHash() { | |
7b4737c8 PW |
153 | uint256 result; |
154 | ctx.Finalize((unsigned char*)&result); | |
155 | return result; | |
0fb9073e PW |
156 | } |
157 | ||
02b4ca7e | 158 | CHashWriter &Reset() |
159 | { | |
160 | ctx.Reset(); | |
161 | return *this; | |
162 | } | |
163 | ||
0fb9073e PW |
164 | template<typename T> |
165 | CHashWriter& operator<<(const T& obj) { | |
166 | // Serialize to this stream | |
242f1421 | 167 | ::Serialize(*this, obj); |
0fb9073e PW |
168 | return (*this); |
169 | } | |
170 | }; | |
171 | ||
56fe75cb | 172 | template <typename SERIALIZABLE> |
173 | uint256 GetHash(const SERIALIZABLE &obj) | |
174 | { | |
175 | CHashWriter hw(SER_GETHASH, PROTOCOL_VERSION); | |
176 | hw << obj; | |
177 | return hw.GetHash(); | |
178 | } | |
132dc81f | 179 | |
02b4ca7e | 180 | /** A writer stream (for serialization) that computes a 256-bit hash. */ |
181 | class CHashWriterSHA256 | |
182 | { | |
183 | private: | |
184 | CSHA256 ctx; | |
185 | ||
186 | const int nType; | |
187 | const int nVersion; | |
188 | public: | |
189 | ||
190 | CHashWriterSHA256(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {} | |
191 | ||
192 | int GetType() const { return nType; } | |
193 | int GetVersion() const { return nVersion; } | |
194 | ||
195 | void write(const char *pch, size_t size) { | |
196 | ctx.Write((const unsigned char*)pch, size); | |
197 | } | |
198 | ||
199 | // invalidates the object | |
200 | uint256 GetHash() { | |
201 | uint256 result; | |
202 | ctx.Finalize((unsigned char*)&result); | |
203 | return result; | |
204 | } | |
205 | ||
206 | CHashWriterSHA256 &Reset() | |
207 | { | |
208 | ctx.Reset(); | |
209 | return *this; | |
210 | } | |
211 | ||
212 | template<typename T> | |
213 | CHashWriterSHA256& operator<<(const T& obj) { | |
214 | // Serialize to this stream | |
215 | ::Serialize(*this, obj); | |
216 | return (*this); | |
217 | } | |
218 | }; | |
219 | ||
56fe75cb | 220 | const unsigned char BLAKE2Bpersonal[crypto_generichash_blake2b_PERSONALBYTES]={'V','e','r','u','s','D','e','f','a','u','l','t','H','a','s','h'}; |
02b4ca7e | 221 | |
132dc81f A |
222 | /** A writer stream (for serialization) that computes a 256-bit BLAKE2b hash. */ |
223 | class CBLAKE2bWriter | |
224 | { | |
225 | private: | |
226 | crypto_generichash_blake2b_state state; | |
227 | ||
228 | public: | |
229 | int nType; | |
230 | int nVersion; | |
231 | ||
56fe75cb | 232 | CBLAKE2bWriter(int nTypeIn, |
233 | int nVersionIn, | |
234 | const unsigned char *personal=BLAKE2Bpersonal) : | |
235 | nType(nTypeIn), nVersion(nVersionIn) | |
236 | { | |
132dc81f A |
237 | assert(crypto_generichash_blake2b_init_salt_personal( |
238 | &state, | |
239 | NULL, 0, // No key. | |
240 | 32, | |
241 | NULL, // No salt. | |
242 | personal) == 0); | |
243 | } | |
244 | ||
b7e75b17 JG |
245 | int GetType() const { return nType; } |
246 | int GetVersion() const { return nVersion; } | |
247 | ||
132dc81f A |
248 | CBLAKE2bWriter& write(const char *pch, size_t size) { |
249 | crypto_generichash_blake2b_update(&state, (const unsigned char*)pch, size); | |
250 | return (*this); | |
251 | } | |
252 | ||
253 | // invalidates the object | |
254 | uint256 GetHash() { | |
255 | uint256 result; | |
256 | crypto_generichash_blake2b_final(&state, (unsigned char*)&result, 32); | |
257 | return result; | |
258 | } | |
259 | ||
260 | template<typename T> | |
261 | CBLAKE2bWriter& operator<<(const T& obj) { | |
262 | // Serialize to this stream | |
68a1a592 | 263 | ::Serialize(*this, obj); |
132dc81f A |
264 | return (*this); |
265 | } | |
266 | }; | |
267 | ||
42181656 | 268 | /** A writer stream (for serialization) that computes a 256-bit Verus hash. */ |
269 | class CVerusHashWriter | |
270 | { | |
271 | private: | |
272 | CVerusHash state; | |
273 | ||
274 | public: | |
275 | int nType; | |
276 | int nVersion; | |
277 | ||
17d0160a | 278 | CVerusHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() { } |
3f8720fa | 279 | void Reset() { state.Reset(); } |
42181656 | 280 | |
281 | CVerusHashWriter& write(const char *pch, size_t size) { | |
282 | state.Write((const unsigned char*)pch, size); | |
283 | return (*this); | |
284 | } | |
285 | ||
286 | // invalidates the object for further writing | |
287 | uint256 GetHash() { | |
288 | uint256 result; | |
289 | state.Finalize((unsigned char*)&result); | |
290 | return result; | |
291 | } | |
292 | ||
4dcb64c0 MT |
293 | int64_t *xI64p() { return state.ExtraI64Ptr(); } |
294 | CVerusHash &GetState() { return state; } | |
295 | ||
42181656 | 296 | template<typename T> |
297 | CVerusHashWriter& operator<<(const T& obj) { | |
298 | // Serialize to this stream | |
9feb4b9e | 299 | ::Serialize(*this, obj); |
42181656 | 300 | return (*this); |
301 | } | |
302 | }; | |
132dc81f | 303 | |
4dcb64c0 MT |
304 | /** A writer stream (for serialization) that computes a 256-bit Verus hash with key initialized to Haraka standard. */ |
305 | class CVerusHashV2Writer | |
5296a850 MT |
306 | { |
307 | private: | |
4dcb64c0 | 308 | CVerusHashV2 state; |
5296a850 MT |
309 | |
310 | public: | |
311 | int nType; | |
312 | int nVersion; | |
313 | ||
4dcb64c0 | 314 | CVerusHashV2Writer(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() {} |
3f8720fa | 315 | void Reset() { state.Reset(); } |
5296a850 | 316 | |
4dcb64c0 | 317 | CVerusHashV2Writer& write(const char *pch, size_t size) { |
5296a850 MT |
318 | state.Write((const unsigned char*)pch, size); |
319 | return (*this); | |
320 | } | |
321 | ||
322 | // invalidates the object for further writing | |
323 | uint256 GetHash() { | |
324 | uint256 result; | |
325 | state.Finalize((unsigned char*)&result); | |
326 | return result; | |
327 | } | |
328 | ||
4dcb64c0 MT |
329 | int64_t *xI64p() { return state.ExtraI64Ptr(); } |
330 | CVerusHashV2 &GetState() { return state; } | |
20ab1c91 MT |
331 | |
332 | template<typename T> | |
4dcb64c0 | 333 | CVerusHashV2Writer& operator<<(const T& obj) { |
20ab1c91 | 334 | // Serialize to this stream |
9feb4b9e | 335 | ::Serialize(*this, obj); |
20ab1c91 MT |
336 | return (*this); |
337 | } | |
338 | }; | |
339 | ||
12217420 | 340 | /** A writer stream (for serialization) that computes a 256-bit VerusHash 2.0 hash */ |
341 | class CVerusHashV2bWriter | |
342 | { | |
343 | private: | |
344 | CVerusHashV2 state; | |
345 | ||
346 | public: | |
347 | int nType; | |
348 | int nVersion; | |
349 | ||
cb410830 | 350 | CVerusHashV2bWriter(int nTypeIn, int nVersionIn, int solutionVersion=SOLUTION_VERUSHHASH_V2, uint64_t keysize=VERUSKEYSIZE) : |
c17dbdee | 351 | nType(nTypeIn), nVersion(nVersionIn), state(solutionVersion) {} |
12217420 | 352 | |
353 | void Reset() { state.Reset(); } | |
354 | ||
355 | CVerusHashV2bWriter& write(const char *pch, size_t size) { | |
356 | state.Write((const unsigned char*)pch, size); | |
357 | return (*this); | |
358 | } | |
359 | ||
360 | // invalidates the object for further writing | |
361 | uint256 GetHash() { | |
362 | uint256 result; | |
363 | state.Finalize2b((unsigned char*)&result); | |
364 | return result; | |
365 | } | |
366 | ||
d5040cb1 | 367 | inline int64_t *xI64p() { return state.ExtraI64Ptr(); } |
12217420 | 368 | CVerusHashV2 &GetState() { return state; } |
369 | ||
370 | template<typename T> | |
371 | CVerusHashV2bWriter& operator<<(const T& obj) { | |
372 | // Serialize to this stream | |
373 | ::Serialize(*this, obj); | |
374 | return (*this); | |
375 | } | |
376 | }; | |
377 | ||
7b4737c8 | 378 | /** Compute the 256-bit hash of an object's serialization. */ |
0fb9073e PW |
379 | template<typename T> |
380 | uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) | |
381 | { | |
382 | CHashWriter ss(nType, nVersion); | |
383 | ss << obj; | |
384 | return ss.GetHash(); | |
385 | } | |
386 | ||
42181656 | 387 | /** Compute the 256-bit Verus hash of an object's serialization. */ |
388 | template<typename T> | |
389 | uint256 SerializeVerusHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) | |
390 | { | |
391 | CVerusHashWriter ss(nType, nVersion); | |
392 | ss << obj; | |
393 | return ss.GetHash(); | |
394 | } | |
395 | ||
5296a850 MT |
396 | /** Compute the 256-bit Verus hash of an object's serialization. */ |
397 | template<typename T> | |
4dcb64c0 | 398 | uint256 SerializeVerusHashV2(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) |
20ab1c91 | 399 | { |
4dcb64c0 | 400 | CVerusHashV2Writer ss(nType, nVersion); |
20ab1c91 MT |
401 | ss << obj; |
402 | return ss.GetHash(); | |
403 | } | |
404 | ||
12217420 | 405 | /** Compute the 256-bit Verus hash of an object's serialization with the final step including |
406 | * a carryless multiply-based hash as fill for the unused space. | |
407 | */ | |
408 | template<typename T> | |
cb410830 | 409 | uint256 SerializeVerusHashV2b(const T& obj, int solutionVersion=SOLUTION_VERUSHHASH_V2, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) |
12217420 | 410 | { |
c17dbdee | 411 | CVerusHashV2bWriter ss(nType, nVersion, solutionVersion); |
12217420 | 412 | ss << obj; |
413 | return ss.GetHash(); | |
414 | } | |
415 | ||
7ab026f4 MC |
416 | unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash); |
417 | ||
a5748996 | 418 | void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]); |
b4347f60 | 419 | |
093303a8 | 420 | #endif // BITCOIN_HASH_H |