Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2014 The Bitcoin developers
3 : // Copyright (c) 2014-2015 The Dash developers
4 : // Copyright (c) 2015-2021 The PIVX Core developers
5 : // Distributed under the MIT software license, see the accompanying
6 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 :
8 : #ifndef PIVX_HASH_H
9 : #define PIVX_HASH_H
10 :
11 : #include "arith_uint256.h"
12 : #include "crypto/ripemd160.h"
13 : #include "crypto/sha256.h"
14 : #include "prevector.h"
15 : #include "serialize.h"
16 : #include "uint256.h"
17 : #include "version.h"
18 :
19 : #include "crypto/sph_blake.h"
20 : #include "crypto/sph_bmw.h"
21 : #include "crypto/sph_groestl.h"
22 : #include "crypto/sph_jh.h"
23 : #include "crypto/sph_keccak.h"
24 : #include "crypto/sph_skein.h"
25 : #include "crypto/sha512.h"
26 :
27 : #include <iomanip>
28 : #include <sstream>
29 : #include <vector>
30 :
31 : #include <sodium.h>
32 :
33 :
34 : typedef uint256 ChainCode;
35 :
36 : /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
37 9961776 : class CHash256
38 : {
39 : private:
40 : CSHA256 sha;
41 :
42 : public:
43 : static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
44 :
45 12458356 : void Finalize(unsigned char hash[OUTPUT_SIZE])
46 : {
47 12458356 : unsigned char buf[CSHA256::OUTPUT_SIZE];
48 12458356 : sha.Finalize(buf);
49 12458356 : sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
50 12458356 : }
51 :
52 321930306 : CHash256& Write(const unsigned char* data, size_t len)
53 : {
54 305327781 : sha.Write(data, len);
55 284898668 : return *this;
56 : }
57 :
58 : CHash256& Reset()
59 : {
60 : sha.Reset();
61 : return *this;
62 : }
63 : };
64 :
65 : class CHash512
66 : {
67 : private:
68 : CSHA512 sha;
69 :
70 : public:
71 : static const size_t OUTPUT_SIZE = CSHA512::OUTPUT_SIZE;
72 :
73 : void Finalize(unsigned char hash[OUTPUT_SIZE])
74 : {
75 : unsigned char buf[CSHA512::OUTPUT_SIZE];
76 : sha.Finalize(buf);
77 : sha.Reset().Write(buf, CSHA512::OUTPUT_SIZE).Finalize(hash);
78 : }
79 :
80 : CHash512& Write(const unsigned char* data, size_t len)
81 : {
82 : sha.Write(data, len);
83 : return *this;
84 : }
85 :
86 : CHash512& Reset()
87 : {
88 : sha.Reset();
89 : return *this;
90 : }
91 : };
92 :
93 : #ifdef GLOBALDEFINED
94 : #define GLOBAL
95 : #else
96 : #define GLOBAL extern
97 : #endif
98 :
99 : GLOBAL sph_blake512_context z_blake;
100 : GLOBAL sph_bmw512_context z_bmw;
101 : GLOBAL sph_groestl512_context z_groestl;
102 : GLOBAL sph_jh512_context z_jh;
103 : GLOBAL sph_keccak512_context z_keccak;
104 : GLOBAL sph_skein512_context z_skein;
105 :
106 : #define fillz() \
107 : do { \
108 : sph_blake512_init(&z_blake); \
109 : sph_bmw512_init(&z_bmw); \
110 : sph_groestl512_init(&z_groestl); \
111 : sph_jh512_init(&z_jh); \
112 : sph_keccak512_init(&z_keccak); \
113 : sph_skein512_init(&z_skein); \
114 : } while (0)
115 :
116 : #define ZBLAKE (memcpy(&ctx_blake, &z_blake, sizeof(z_blake)))
117 : #define ZBMW (memcpy(&ctx_bmw, &z_bmw, sizeof(z_bmw)))
118 : #define ZGROESTL (memcpy(&ctx_groestl, &z_groestl, sizeof(z_groestl)))
119 : #define ZJH (memcpy(&ctx_jh, &z_jh, sizeof(z_jh)))
120 : #define ZKECCAK (memcpy(&ctx_keccak, &z_keccak, sizeof(z_keccak)))
121 : #define ZSKEIN (memcpy(&ctx_skein, &z_skein, sizeof(z_skein)))
122 :
123 : /* ----------- Bitcoin Hash ------------------------------------------------- */
124 : /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
125 8764361 : class CHash160
126 : {
127 : private:
128 : CSHA256 sha;
129 :
130 : public:
131 : static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
132 :
133 8764361 : void Finalize(unsigned char hash[OUTPUT_SIZE])
134 : {
135 8764361 : unsigned char buf[CSHA256::OUTPUT_SIZE];
136 8764361 : sha.Finalize(buf);
137 8764361 : CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
138 8764361 : }
139 :
140 8764361 : CHash160& Write(const unsigned char* data, size_t len)
141 : {
142 8764361 : sha.Write(data, len);
143 8764361 : return *this;
144 : }
145 :
146 : CHash160& Reset()
147 : {
148 : sha.Reset();
149 : return *this;
150 : }
151 : };
152 :
153 : /** Compute the 512-bit hash of an object. */
154 : template <typename T1>
155 : inline uint512 Hash512(const T1 pbegin, const T1 pend)
156 : {
157 : static const unsigned char pblank[1] = {};
158 : uint512 result;
159 : CHash512().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])).Finalize((unsigned char*)&result);
160 : return result;
161 : }
162 : template <typename T1, typename T2>
163 : inline uint512 Hash512(const T1 p1begin, const T1 p1end, const T2 p2begin, const T2 p2end)
164 : {
165 : static const unsigned char pblank[1] = {};
166 : uint512 result;
167 : CHash512().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])).Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])).Finalize((unsigned char*)&result);
168 : return result;
169 : }
170 :
171 : /** Compute the 256-bit hash of an object. */
172 : template <typename T1>
173 3074046 : inline uint256 Hash(const T1 pbegin, const T1 pend)
174 : {
175 : static const unsigned char pblank[1] = {};
176 3074046 : uint256 result;
177 6148092 : CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])).Finalize((unsigned char*)&result);
178 3074046 : return result;
179 : }
180 :
181 : /** Compute the 256-bit hash of the concatenation of two objects. */
182 : template <typename T1, typename T2>
183 368854 : inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin, const T2 p2end)
184 : {
185 : static const unsigned char pblank[1] = {};
186 368854 : uint256 result;
187 1106562 : CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])).Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])).Finalize((unsigned char*)&result);
188 368854 : return result;
189 : }
190 :
191 : /** Compute the 160-bit hash an object. */
192 : template <typename T1>
193 300116 : inline uint160 Hash160(const T1 pbegin, const T1 pend)
194 : {
195 : static unsigned char pblank[1] = {};
196 300116 : uint160 result;
197 600232 : CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])).Finalize((unsigned char*)&result);
198 300116 : return result;
199 : }
200 :
201 : /** Compute the 160-bit hash of a vector. */
202 : inline uint160 Hash160(const std::vector<unsigned char>& vch)
203 : {
204 : return Hash160(vch.begin(), vch.end());
205 : }
206 :
207 : /** Compute the 160-bit hash of a vector. */
208 : template<unsigned int N>
209 15 : inline uint160 Hash160(const prevector<N, unsigned char>& vch)
210 : {
211 30 : return Hash160(vch.begin(), vch.end());
212 : }
213 :
214 : /** A writer stream (for serialization) that computes a 256-bit hash. */
215 : class CHashWriter
216 : {
217 : private:
218 : CHash256 ctx;
219 :
220 : const int nType;
221 : const int nVersion;
222 :
223 : public:
224 :
225 11858894 : CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
226 :
227 1426 : int GetType() const { return nType; }
228 97510 : int GetVersion() const { return nVersion; }
229 :
230 10903614 : void write(const char* pch, size_t size)
231 : {
232 307383256 : ctx.Write((const unsigned char*)pch, size);
233 49695366 : }
234 :
235 : // invalidates the object
236 7418372 : uint256 GetHash()
237 : {
238 11858891 : uint256 result;
239 7418372 : ctx.Finalize((unsigned char*)&result);
240 6896324 : return result;
241 : }
242 :
243 : template <typename T>
244 15411118 : CHashWriter& operator<<(const T& obj)
245 : {
246 : // Serialize to this stream
247 13459797 : ::Serialize(*this, obj);
248 59750 : return (*this);
249 : }
250 : };
251 :
252 : /** Reads data from an underlying stream, while hashing the read data. */
253 : template<typename Source>
254 : class CHashVerifier : public CHashWriter
255 : {
256 : private:
257 : Source* source;
258 :
259 : public:
260 2759 : explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
261 :
262 2741700 : void read(char* pch, size_t nSize)
263 : {
264 2741700 : source->read(pch, nSize);
265 2741699 : this->write(pch, nSize);
266 2741699 : }
267 :
268 0 : void ignore(size_t nSize)
269 : {
270 : char data[1024];
271 0 : while (nSize > 0) {
272 0 : size_t now = std::min<size_t>(nSize, 1024);
273 0 : read(data, now);
274 0 : nSize -= now;
275 : }
276 0 : }
277 :
278 : template<typename T>
279 781802 : CHashVerifier<Source>& operator>>(T&& obj)
280 : {
281 : // Unserialize from this stream
282 781886 : ::Unserialize(*this, obj);
283 : return (*this);
284 : }
285 : };
286 :
287 : /** Compute the 256-bit hash of an object's serialization. */
288 : template <typename T>
289 4451088 : uint256 SerializeHash(const T& obj, int nType = SER_GETHASH, int nVersion = PROTOCOL_VERSION)
290 : {
291 4451088 : CHashWriter ss(nType, nVersion);
292 4451088 : ss << obj;
293 4451088 : return ss.GetHash();
294 : }
295 :
296 : /** A writer stream (for serialization) that computes a 256-bit BLAKE2b hash. */
297 : class CBLAKE2bWriter
298 : {
299 : private:
300 : crypto_generichash_blake2b_state state;
301 :
302 : public:
303 : int nType;
304 : int nVersion;
305 :
306 18952753 : CBLAKE2bWriter(int nTypeIn, int nVersionIn, const unsigned char* personal) : nType(nTypeIn), nVersion(nVersionIn) {
307 18952753 : assert(crypto_generichash_blake2b_init_salt_personal(
308 : &state,
309 : nullptr, 0, // No key.
310 : 32,
311 : nullptr, // No salt.
312 : personal) == 0);
313 18952753 : }
314 :
315 582109648 : CBLAKE2bWriter& write(const char *pch, size_t size) {
316 560436648 : crypto_generichash_blake2b_update(&state, (const unsigned char*)pch, size);
317 555668232 : return (*this);
318 : }
319 :
320 : // invalidates the object
321 18952753 : uint256 GetHash() {
322 18952753 : uint256 result;
323 18952753 : crypto_generichash_blake2b_final(&state, (unsigned char*)&result, 32);
324 7583349 : return result;
325 : }
326 :
327 : template<typename T>
328 47741416 : CBLAKE2bWriter& operator<<(const T& obj) {
329 : // Serialize to this stream
330 60077016 : ::Serialize(*this, obj);
331 112123 : return (*this);
332 : }
333 : };
334 :
335 : unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
336 :
337 : void BIP32Hash(const ChainCode chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
338 :
339 : //int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len);
340 : //int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len);
341 : //int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx);
342 :
343 : /* ----------- Quark Hash ------------------------------------------------ */
344 : template <typename T1>
345 6668 : inline uint256 HashQuark(const T1 pbegin, const T1 pend)
346 :
347 : {
348 : sph_blake512_context ctx_blake;
349 : sph_bmw512_context ctx_bmw;
350 : sph_groestl512_context ctx_groestl;
351 : sph_jh512_context ctx_jh;
352 : sph_keccak512_context ctx_keccak;
353 : sph_skein512_context ctx_skein;
354 : static unsigned char pblank[1];
355 :
356 6668 : arith_uint512 mask(8);
357 66680 : arith_uint512 zero(0);
358 :
359 66680 : arith_uint512 hash[9];
360 :
361 6668 : sph_blake512_init(&ctx_blake);
362 : // ZBLAKE;
363 6668 : sph_blake512(&ctx_blake, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
364 6668 : sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[0]));
365 :
366 6668 : sph_bmw512_init(&ctx_bmw);
367 : // ZBMW;
368 6668 : sph_bmw512(&ctx_bmw, static_cast<const void*>(&hash[0]), 64);
369 6668 : sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[1]));
370 :
371 6668 : if ((hash[1] & mask) != zero) {
372 249 : sph_groestl512_init(&ctx_groestl);
373 : // ZGROESTL;
374 249 : sph_groestl512(&ctx_groestl, static_cast<const void*>(&hash[1]), 64);
375 249 : sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[2]));
376 : } else {
377 6419 : sph_skein512_init(&ctx_skein);
378 : // ZSKEIN;
379 6419 : sph_skein512(&ctx_skein, static_cast<const void*>(&hash[1]), 64);
380 6419 : sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[2]));
381 : }
382 :
383 6668 : sph_groestl512_init(&ctx_groestl);
384 : // ZGROESTL;
385 6668 : sph_groestl512(&ctx_groestl, static_cast<const void*>(&hash[2]), 64);
386 6668 : sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[3]));
387 :
388 6668 : sph_jh512_init(&ctx_jh);
389 : // ZJH;
390 6668 : sph_jh512(&ctx_jh, static_cast<const void*>(&hash[3]), 64);
391 6668 : sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[4]));
392 :
393 6668 : if ((hash[4] & mask) != zero) {
394 3314 : sph_blake512_init(&ctx_blake);
395 : // ZBLAKE;
396 3314 : sph_blake512(&ctx_blake, static_cast<const void*>(&hash[4]), 64);
397 3314 : sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[5]));
398 : } else {
399 3354 : sph_bmw512_init(&ctx_bmw);
400 : // ZBMW;
401 3354 : sph_bmw512(&ctx_bmw, static_cast<const void*>(&hash[4]), 64);
402 3354 : sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[5]));
403 : }
404 :
405 6668 : sph_keccak512_init(&ctx_keccak);
406 : // ZKECCAK;
407 6668 : sph_keccak512(&ctx_keccak, static_cast<const void*>(&hash[5]), 64);
408 6668 : sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[6]));
409 :
410 6668 : sph_skein512_init(&ctx_skein);
411 : // SKEIN;
412 6668 : sph_skein512(&ctx_skein, static_cast<const void*>(&hash[6]), 64);
413 6668 : sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[7]));
414 :
415 6668 : if ((hash[7] & mask) != zero) {
416 250 : sph_keccak512_init(&ctx_keccak);
417 : // ZKECCAK;
418 250 : sph_keccak512(&ctx_keccak, static_cast<const void*>(&hash[7]), 64);
419 250 : sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[8]));
420 : } else {
421 6418 : sph_jh512_init(&ctx_jh);
422 : // ZJH;
423 6418 : sph_jh512(&ctx_jh, static_cast<const void*>(&hash[7]), 64);
424 6418 : sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[8]));
425 : }
426 6668 : return hash[8].trim256();
427 : }
428 :
429 : void scrypt_hash(const char* pass, unsigned int pLen, const char* salt, unsigned int sLen, char* output, unsigned int N, unsigned int r, unsigned int p, unsigned int dkLen);
430 :
431 : #endif // PIVX_HASH_H
432 :
|