PIVX Core  5.6.99
P2P Digital Currency
hash.h
Go to the documentation of this file.
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 
35 
37 class CHash256
38 {
39 private:
41 
42 public:
43  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
44 
45  void Finalize(unsigned char hash[OUTPUT_SIZE])
46  {
47  unsigned char buf[CSHA256::OUTPUT_SIZE];
48  sha.Finalize(buf);
50  }
51 
52  CHash256& Write(const unsigned char* data, size_t len)
53  {
54  sha.Write(data, len);
55  return *this;
56  }
57 
59  {
60  sha.Reset();
61  return *this;
62  }
63 };
64 
65 class CHash512
66 {
67 private:
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);
78  }
79 
80  CHash512& Write(const unsigned char* data, size_t len)
81  {
82  sha.Write(data, len);
83  return *this;
84  }
85 
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;
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 ------------------------------------------------- */
125 class CHash160
126 {
127 private:
129 
130 public:
131  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
132 
133  void Finalize(unsigned char hash[OUTPUT_SIZE])
134  {
135  unsigned char buf[CSHA256::OUTPUT_SIZE];
136  sha.Finalize(buf);
138  }
139 
140  CHash160& Write(const unsigned char* data, size_t len)
141  {
142  sha.Write(data, len);
143  return *this;
144  }
145 
147  {
148  sha.Reset();
149  return *this;
150  }
151 };
152 
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 
172 template <typename T1>
173 inline uint256 Hash(const T1 pbegin, const T1 pend)
174 {
175  static const unsigned char pblank[1] = {};
176  uint256 result;
177  CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])).Finalize((unsigned char*)&result);
178  return result;
179 }
180 
182 template <typename T1, typename T2>
183 inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin, const T2 p2end)
184 {
185  static const unsigned char pblank[1] = {};
186  uint256 result;
187  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  return result;
189 }
190 
192 template <typename T1>
193 inline uint160 Hash160(const T1 pbegin, const T1 pend)
194 {
195  static unsigned char pblank[1] = {};
196  uint160 result;
197  CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])).Finalize((unsigned char*)&result);
198  return result;
199 }
200 
202 inline uint160 Hash160(const std::vector<unsigned char>& vch)
203 {
204  return Hash160(vch.begin(), vch.end());
205 }
206 
208 template<unsigned int N>
210 {
211  return Hash160(vch.begin(), vch.end());
212 }
213 
216 {
217 private:
219 
220  const int nType;
221  const int nVersion;
222 
223 public:
224 
225  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
226 
227  int GetType() const { return nType; }
228  int GetVersion() const { return nVersion; }
229 
230  void write(const char* pch, size_t size)
231  {
232  ctx.Write((const unsigned char*)pch, size);
233  }
234 
235  // invalidates the object
237  {
238  uint256 result;
239  ctx.Finalize((unsigned char*)&result);
240  return result;
241  }
242 
243  template <typename T>
244  CHashWriter& operator<<(const T& obj)
245  {
246  // Serialize to this stream
247  ::Serialize(*this, obj);
248  return (*this);
249  }
250 };
251 
253 template<typename Source>
255 {
256 private:
257  Source* source;
258 
259 public:
260  explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
261 
262  void read(char* pch, size_t nSize)
263  {
264  source->read(pch, nSize);
265  this->write(pch, nSize);
266  }
267 
268  void ignore(size_t nSize)
269  {
270  char data[1024];
271  while (nSize > 0) {
272  size_t now = std::min<size_t>(nSize, 1024);
273  read(data, now);
274  nSize -= now;
275  }
276  }
277 
278  template<typename T>
280  {
281  // Unserialize from this stream
282  ::Unserialize(*this, obj);
283  return (*this);
284  }
285 };
286 
288 template <typename T>
289 uint256 SerializeHash(const T& obj, int nType = SER_GETHASH, int nVersion = PROTOCOL_VERSION)
290 {
291  CHashWriter ss(nType, nVersion);
292  ss << obj;
293  return ss.GetHash();
294 }
295 
298 {
299 private:
300  crypto_generichash_blake2b_state state;
301 
302 public:
303  int nType;
304  int nVersion;
305 
306  CBLAKE2bWriter(int nTypeIn, int nVersionIn, const unsigned char* personal) : nType(nTypeIn), nVersion(nVersionIn) {
307  assert(crypto_generichash_blake2b_init_salt_personal(
308  &state,
309  nullptr, 0, // No key.
310  32,
311  nullptr, // No salt.
312  personal) == 0);
313  }
314 
315  CBLAKE2bWriter& write(const char *pch, size_t size) {
316  crypto_generichash_blake2b_update(&state, (const unsigned char*)pch, size);
317  return (*this);
318  }
319 
320  // invalidates the object
322  uint256 result;
323  crypto_generichash_blake2b_final(&state, (unsigned char*)&result, 32);
324  return result;
325  }
326 
327  template<typename T>
328  CBLAKE2bWriter& operator<<(const T& obj) {
329  // Serialize to this stream
330  ::Serialize(*this, obj);
331  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 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  arith_uint512 mask(8);
357  arith_uint512 zero(0);
358 
359  arith_uint512 hash[9];
360 
361  sph_blake512_init(&ctx_blake);
362  // ZBLAKE;
363  sph_blake512(&ctx_blake, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
364  sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[0]));
365 
366  sph_bmw512_init(&ctx_bmw);
367  // ZBMW;
368  sph_bmw512(&ctx_bmw, static_cast<const void*>(&hash[0]), 64);
369  sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[1]));
370 
371  if ((hash[1] & mask) != zero) {
372  sph_groestl512_init(&ctx_groestl);
373  // ZGROESTL;
374  sph_groestl512(&ctx_groestl, static_cast<const void*>(&hash[1]), 64);
375  sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[2]));
376  } else {
377  sph_skein512_init(&ctx_skein);
378  // ZSKEIN;
379  sph_skein512(&ctx_skein, static_cast<const void*>(&hash[1]), 64);
380  sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[2]));
381  }
382 
383  sph_groestl512_init(&ctx_groestl);
384  // ZGROESTL;
385  sph_groestl512(&ctx_groestl, static_cast<const void*>(&hash[2]), 64);
386  sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[3]));
387 
388  sph_jh512_init(&ctx_jh);
389  // ZJH;
390  sph_jh512(&ctx_jh, static_cast<const void*>(&hash[3]), 64);
391  sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[4]));
392 
393  if ((hash[4] & mask) != zero) {
394  sph_blake512_init(&ctx_blake);
395  // ZBLAKE;
396  sph_blake512(&ctx_blake, static_cast<const void*>(&hash[4]), 64);
397  sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[5]));
398  } else {
399  sph_bmw512_init(&ctx_bmw);
400  // ZBMW;
401  sph_bmw512(&ctx_bmw, static_cast<const void*>(&hash[4]), 64);
402  sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[5]));
403  }
404 
405  sph_keccak512_init(&ctx_keccak);
406  // ZKECCAK;
407  sph_keccak512(&ctx_keccak, static_cast<const void*>(&hash[5]), 64);
408  sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[6]));
409 
410  sph_skein512_init(&ctx_skein);
411  // SKEIN;
412  sph_skein512(&ctx_skein, static_cast<const void*>(&hash[6]), 64);
413  sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[7]));
414 
415  if ((hash[7] & mask) != zero) {
416  sph_keccak512_init(&ctx_keccak);
417  // ZKECCAK;
418  sph_keccak512(&ctx_keccak, static_cast<const void*>(&hash[7]), 64);
419  sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[8]));
420  } else {
421  sph_jh512_init(&ctx_jh);
422  // ZJH;
423  sph_jh512(&ctx_jh, static_cast<const void*>(&hash[7]), 64);
424  sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[8]));
425  }
426  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 
A writer stream (for serialization) that computes a 256-bit BLAKE2b hash.
Definition: hash.h:298
int nType
Definition: hash.h:303
CBLAKE2bWriter & write(const char *pch, size_t size)
Definition: hash.h:315
int nVersion
Definition: hash.h:304
CBLAKE2bWriter & operator<<(const T &obj)
Definition: hash.h:328
CBLAKE2bWriter(int nTypeIn, int nVersionIn, const unsigned char *personal)
Definition: hash.h:306
crypto_generichash_blake2b_state state
Definition: hash.h:300
uint256 GetHash()
Definition: hash.h:321
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:126
static const size_t OUTPUT_SIZE
Definition: hash.h:131
CSHA256 sha
Definition: hash.h:128
CHash160 & Reset()
Definition: hash.h:146
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:133
CHash160 & Write(const unsigned char *data, size_t len)
Definition: hash.h:140
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:38
static const size_t OUTPUT_SIZE
Definition: hash.h:43
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:52
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:45
CHash256 & Reset()
Definition: hash.h:58
CSHA256 sha
Definition: hash.h:40
Definition: hash.h:66
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:73
CHash512 & Write(const unsigned char *data, size_t len)
Definition: hash.h:80
CSHA512 sha
Definition: hash.h:68
CHash512 & Reset()
Definition: hash.h:86
static const size_t OUTPUT_SIZE
Definition: hash.h:71
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:255
Source * source
Definition: hash.h:257
CHashVerifier(Source *source_)
Definition: hash.h:260
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:279
void ignore(size_t nSize)
Definition: hash.h:268
void read(char *pch, size_t nSize)
Definition: hash.h:262
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:216
int GetType() const
Definition: hash.h:227
const int nType
Definition: hash.h:220
CHashWriter & operator<<(const T &obj)
Definition: hash.h:244
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:225
int GetVersion() const
Definition: hash.h:228
CHash256 ctx
Definition: hash.h:218
const int nVersion
Definition: hash.h:221
void write(const char *pch, size_t size)
Definition: hash.h:230
uint256 GetHash()
Definition: hash.h:236
A hasher class for RIPEMD-160.
Definition: ripemd160.h:13
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
A hasher class for SHA-256.
Definition: sha256.h:13
CSHA256 & Reset()
Definition: sha256.cpp:194
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:168
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:142
A hasher class for SHA-512.
Definition: sha512.h:13
static constexpr size_t OUTPUT_SIZE
Definition: sha512.h:20
CSHA512 & Reset()
Definition: sha512.cpp:202
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha512.cpp:185
CSHA512 & Write(const unsigned char *data, size_t len)
Definition: sha512.cpp:159
512-bit unsigned big integer.
uint256 trim256() const
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
iterator begin()
Definition: prevector.h:285
iterator end()
Definition: prevector.h:287
160-bit opaque blob.
Definition: uint256.h:127
256-bit opaque blob.
Definition: uint256.h:138
512-bit opaque blob.
Definition: uint256.h:184
void sph_groestl512_close(void *cc, void *dst)
Terminate the current Groestl-512 computation and output the result into the provided buffer.
Definition: groestl.c:3110
void sph_groestl512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: groestl.c:3103
void sph_groestl512_init(void *cc)
Initialize a Groestl-512 context.
Definition: groestl.c:3096
uint512 Hash512(const T1 pbegin, const T1 pend)
Compute the 512-bit hash of an object.
Definition: hash.h:155
GLOBAL sph_skein512_context z_skein
Definition: hash.h:104
void BIP32Hash(const ChainCode chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:72
GLOBAL sph_bmw512_context z_bmw
Definition: hash.h:100
GLOBAL sph_jh512_context z_jh
Definition: hash.h:102
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
Definition: hash.cpp:16
#define GLOBAL
Definition: hash.h:96
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:193
uint256 HashQuark(const T1 pbegin, const T1 pend)
Definition: hash.h:345
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:289
GLOBAL sph_blake512_context z_blake
Definition: hash.h:99
GLOBAL sph_keccak512_context z_keccak
Definition: hash.h:103
uint256 ChainCode
Definition: hash.h:34
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:173
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)
Definition: hash.cpp:82
GLOBAL sph_groestl512_context z_groestl
Definition: hash.h:101
#define T(expected, seed, data)
void sph_jh512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: jh.c:1095
void sph_jh512_init(void *cc)
Initialize a JH-512 context.
Definition: jh.c:1088
void sph_jh512_close(void *cc, void *dst)
Terminate the current JH-512 computation and output the result into the provided buffer.
Definition: jh.c:1102
void sph_keccak512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: keccak.c:1802
void sph_keccak512_init(void *cc)
Initialize a Keccak-512 context.
Definition: keccak.c:1795
void sph_keccak512_close(void *cc, void *dst)
Terminate the current Keccak-512 computation and output the result into the provided buffer.
Definition: keccak.c:1809
@ SER_GETHASH
Definition: serialize.h:176
void Serialize(Stream &s, char a)
Definition: serialize.h:237
void Unserialize(Stream &s, char &a)
Definition: serialize.h:253
BLAKE interface.
BMW interface.
Groestl interface.
JH interface.
Keccak interface.
Skein interface.
This structure is a context for Groestl-384 and Groestl-512 computations: it contains the intermediat...
Definition: sph_groestl.h:115
This structure is a context for JH computations: it contains the intermediate values and some data fr...
Definition: sph_jh.h:76
This structure is a context for Keccak computations: it contains the intermediate values and some dat...
Definition: sph_keccak.h:76