Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

hash.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2014-2019 The Dash Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_HASH_H
8 #define BITCOIN_HASH_H
9 
10 #include <crypto/ripemd160.h>
11 #include <crypto/sha256.h>
12 #include <prevector.h>
13 #include <serialize.h>
14 #include <uint256.h>
15 #include <version.h>
16 
17 #include <crypto/sph_blake.h>
18 #include <crypto/sph_bmw.h>
19 #include <crypto/sph_groestl.h>
20 #include <crypto/sph_jh.h>
21 #include <crypto/sph_keccak.h>
22 #include <crypto/sph_skein.h>
23 #include <crypto/sph_luffa.h>
24 #include <crypto/sph_cubehash.h>
25 #include <crypto/sph_shavite.h>
26 #include <crypto/sph_simd.h>
27 #include <crypto/sph_echo.h>
28 
29 #include <vector>
30 
32 
33 /* ----------- Bitcoin Hash ------------------------------------------------- */
35 class CHash256 {
36 private:
38 public:
39  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
40 
41  void Finalize(unsigned char hash[OUTPUT_SIZE]) {
42  unsigned char buf[CSHA256::OUTPUT_SIZE];
43  sha.Finalize(buf);
45  }
46 
47  CHash256& Write(const unsigned char *data, size_t len) {
48  sha.Write(data, len);
49  return *this;
50  }
51 
53  sha.Reset();
54  return *this;
55  }
56 };
57 
59 class CHash160 {
60 private:
62 public:
63  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
64 
65  void Finalize(unsigned char hash[OUTPUT_SIZE]) {
66  unsigned char buf[CSHA256::OUTPUT_SIZE];
67  sha.Finalize(buf);
69  }
70 
71  CHash160& Write(const unsigned char *data, size_t len) {
72  sha.Write(data, len);
73  return *this;
74  }
75 
77  sha.Reset();
78  return *this;
79  }
80 };
81 
83 template<typename T1>
84 inline uint256 Hash(const T1 pbegin, const T1 pend)
85 {
86  static const unsigned char pblank[1] = {};
87  uint256 result;
88  CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
89  .Finalize((unsigned char*)&result);
90  return result;
91 }
92 
94 template<typename T1, typename T2>
95 inline uint256 Hash(const T1 p1begin, const T1 p1end,
96  const T2 p2begin, const T2 p2end) {
97  static const unsigned char pblank[1] = {};
98  uint256 result;
99  CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
100  .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
101  .Finalize((unsigned char*)&result);
102  return result;
103 }
104 
106 template<typename T1, typename T2, typename T3, typename T4>
107 inline uint256 Hash(const T1 p1begin, const T1 p1end,
108  const T2 p2begin, const T2 p2end,
109  const T3 p3begin, const T3 p3end,
110  const T4 p4begin, const T4 p4end) {
111  static const unsigned char pblank[1] = {};
112  uint256 result;
113  CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
114  .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
115  .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
116  .Write(p4begin == p4end ? pblank : (const unsigned char*)&p4begin[0], (p4end - p4begin) * sizeof(p4begin[0]))
117  .Finalize((unsigned char*)&result);
118  return result;
119 }
120 
122 template<typename T1, typename T2, typename T3, typename T4, typename T5>
123 inline uint256 Hash(const T1 p1begin, const T1 p1end,
124  const T2 p2begin, const T2 p2end,
125  const T3 p3begin, const T3 p3end,
126  const T4 p4begin, const T4 p4end,
127  const T5 p5begin, const T5 p5end) {
128  static const unsigned char pblank[1] = {};
129  uint256 result;
130  CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
131  .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
132  .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
133  .Write(p4begin == p4end ? pblank : (const unsigned char*)&p4begin[0], (p4end - p4begin) * sizeof(p4begin[0]))
134  .Write(p5begin == p5end ? pblank : (const unsigned char*)&p5begin[0], (p5end - p5begin) * sizeof(p5begin[0]))
135  .Finalize((unsigned char*)&result);
136  return result;
137 }
138 
140 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
141 inline uint256 Hash(const T1 p1begin, const T1 p1end,
142  const T2 p2begin, const T2 p2end,
143  const T3 p3begin, const T3 p3end,
144  const T4 p4begin, const T4 p4end,
145  const T5 p5begin, const T5 p5end,
146  const T6 p6begin, const T6 p6end) {
147  static const unsigned char pblank[1] = {};
148  uint256 result;
149  CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
150  .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
151  .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
152  .Write(p4begin == p4end ? pblank : (const unsigned char*)&p4begin[0], (p4end - p4begin) * sizeof(p4begin[0]))
153  .Write(p5begin == p5end ? pblank : (const unsigned char*)&p5begin[0], (p5end - p5begin) * sizeof(p5begin[0]))
154  .Write(p6begin == p6end ? pblank : (const unsigned char*)&p6begin[0], (p6end - p6begin) * sizeof(p6begin[0]))
155  .Finalize((unsigned char*)&result);
156  return result;
157 }
158 
160 template<typename T1>
161 inline uint160 Hash160(const T1 pbegin, const T1 pend)
162 {
163  static unsigned char pblank[1] = {};
164  uint160 result;
165  CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
166  .Finalize((unsigned char*)&result);
167  return result;
168 }
169 
171 inline uint160 Hash160(const std::vector<unsigned char>& vch)
172 {
173  return Hash160(vch.begin(), vch.end());
174 }
175 
177 template<unsigned int N>
179 {
180  return Hash160(vch.begin(), vch.end());
181 }
182 
185 {
186 private:
188 
189  const int nType;
190  const int nVersion;
191 public:
192 
193  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
194 
195  int GetType() const { return nType; }
196  int GetVersion() const { return nVersion; }
197 
198  void write(const char *pch, size_t size) {
199  ctx.Write((const unsigned char*)pch, size);
200  }
201 
202  // invalidates the object
204  uint256 result;
205  ctx.Finalize((unsigned char*)&result);
206  return result;
207  }
208 
209  template<typename T>
210  CHashWriter& operator<<(const T& obj) {
211  // Serialize to this stream
212  ::Serialize(*this, obj);
213  return (*this);
214  }
215 };
216 
218 template<typename Source>
220 {
221 private:
222  Source* source;
223 
224 public:
225  explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
226 
227  void read(char* pch, size_t nSize)
228  {
229  source->read(pch, nSize);
230  this->write(pch, nSize);
231  }
232 
233  void ignore(size_t nSize)
234  {
235  char data[1024];
236  while (nSize > 0) {
237  size_t now = std::min<size_t>(nSize, 1024);
238  read(data, now);
239  nSize -= now;
240  }
241  }
242 
243  template<typename T>
245  {
246  // Unserialize from this stream
247  ::Unserialize(*this, obj);
248  return (*this);
249  }
250 };
251 
253 template<typename T>
254 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
255 {
256  CHashWriter ss(nType, nVersion);
257  ss << obj;
258  return ss.GetHash();
259 }
260 
261 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
262 
263 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
264 
267 {
268 private:
269  uint64_t v[4];
270  uint64_t tmp;
271  int count;
272 
273 public:
275  CSipHasher(uint64_t k0, uint64_t k1);
280  CSipHasher& Write(uint64_t data);
282  CSipHasher& Write(const unsigned char* data, size_t size);
284  uint64_t Finalize() const;
285 };
286 
297 uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val);
298 uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra);
299 
300 /* ----------- Dash Hash ------------------------------------------------ */
301 template<typename T1>
302 inline uint256 HashX11(const T1 pbegin, const T1 pend)
303 
304 {
305  sph_blake512_context ctx_blake;
306  sph_bmw512_context ctx_bmw;
307  sph_groestl512_context ctx_groestl;
308  sph_jh512_context ctx_jh;
309  sph_keccak512_context ctx_keccak;
310  sph_skein512_context ctx_skein;
311  sph_luffa512_context ctx_luffa;
312  sph_cubehash512_context ctx_cubehash;
313  sph_shavite512_context ctx_shavite;
314  sph_simd512_context ctx_simd;
315  sph_echo512_context ctx_echo;
316  static unsigned char pblank[1];
317 
318  uint512 hash[11];
319 
320  sph_blake512_init(&ctx_blake);
321  sph_blake512 (&ctx_blake, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
322  sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[0]));
323 
324  sph_bmw512_init(&ctx_bmw);
325  sph_bmw512 (&ctx_bmw, static_cast<const void*>(&hash[0]), 64);
326  sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[1]));
327 
328  sph_groestl512_init(&ctx_groestl);
329  sph_groestl512 (&ctx_groestl, static_cast<const void*>(&hash[1]), 64);
330  sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[2]));
331 
332  sph_skein512_init(&ctx_skein);
333  sph_skein512 (&ctx_skein, static_cast<const void*>(&hash[2]), 64);
334  sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[3]));
335 
336  sph_jh512_init(&ctx_jh);
337  sph_jh512 (&ctx_jh, static_cast<const void*>(&hash[3]), 64);
338  sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[4]));
339 
340  sph_keccak512_init(&ctx_keccak);
341  sph_keccak512 (&ctx_keccak, static_cast<const void*>(&hash[4]), 64);
342  sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[5]));
343 
344  sph_luffa512_init(&ctx_luffa);
345  sph_luffa512 (&ctx_luffa, static_cast<void*>(&hash[5]), 64);
346  sph_luffa512_close(&ctx_luffa, static_cast<void*>(&hash[6]));
347 
348  sph_cubehash512_init(&ctx_cubehash);
349  sph_cubehash512 (&ctx_cubehash, static_cast<const void*>(&hash[6]), 64);
350  sph_cubehash512_close(&ctx_cubehash, static_cast<void*>(&hash[7]));
351 
352  sph_shavite512_init(&ctx_shavite);
353  sph_shavite512(&ctx_shavite, static_cast<const void*>(&hash[7]), 64);
354  sph_shavite512_close(&ctx_shavite, static_cast<void*>(&hash[8]));
355 
356  sph_simd512_init(&ctx_simd);
357  sph_simd512 (&ctx_simd, static_cast<const void*>(&hash[8]), 64);
358  sph_simd512_close(&ctx_simd, static_cast<void*>(&hash[9]));
359 
360  sph_echo512_init(&ctx_echo);
361  sph_echo512 (&ctx_echo, static_cast<const void*>(&hash[9]), 64);
362  sph_echo512_close(&ctx_echo, static_cast<void*>(&hash[10]));
363 
364  return hash[10].trim256();
365 }
366 
367 #endif // BITCOIN_HASH_H
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:648
uint256 trim256() const
Definition: uint256.h:167
void write(const char *pch, size_t size)
Definition: hash.h:198
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:71
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
Definition: hash.cpp:15
This structure is a context for ECHO computations: it contains the intermediate values and some data ...
Definition: sph_echo.h:102
const int nVersion
Definition: hash.h:190
JH interface.
uint64_t v[4]
Definition: hash.h:269
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
Definition: hash.cpp:102
BLAKE interface.
Skein interface.
void sph_echo512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: echo.c:1011
void sph_simd512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: simd.c:1780
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:47
uint256 ChainCode
Definition: hash.h:31
void sph_simd512_close(void *cc, void *dst)
Terminate the current SIMD-512 computation and output the result into the provided buffer...
Definition: simd.c:1786
This structure is a context for SHAvite-384 and SHAvite-512 computations: it contains the intermediat...
Definition: sph_shavite.h:109
CHash160 & Reset()
Definition: hash.h:76
CSHA256 sha
Definition: hash.h:37
void sph_echo512_init(void *cc)
Initialize an ECHO-512 context.
Definition: echo.c:1004
void sph_jh512_init(void *cc)
Initialize a JH-512 context.
Definition: jh.c:1088
CSipHasher(uint64_t k0, uint64_t k1)
Construct a SipHash calculator initialized with 128-bit key (k0, k1)
Definition: hash.cpp:92
A hasher class for Bitcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:35
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:161
static const size_t OUTPUT_SIZE
Definition: hash.h:63
void ignore(size_t nSize)
Definition: hash.h:233
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:219
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:168
void sph_luffa512_close(void *cc, void *dst)
Terminate the current Luffa-512 computation and output the result into the provided buffer...
Definition: luffa.c:1411
SIMD interface.
void sph_luffa512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: luffa.c:1404
void Serialize(Stream &s, char a)
Definition: serialize.h:184
void sph_shavite512_init(void *cc)
Initialize a SHAvite-512 context.
Definition: shavite.c:1734
void sph_cubehash512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: cubehash.c:702
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:41
512-bit unsigned big integer.
Definition: uint256.h:161
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object&#39;s serialization.
Definition: hash.h:254
iterator end()
Definition: prevector.h:320
ECHO interface.
SHAvite-3 interface.
This structure is a context for SIMD computations: it contains the intermediate values and some data ...
Definition: sph_simd.h:97
CSHA256 & Reset()
Definition: sha256.cpp:691
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
int count
Definition: hash.h:271
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
void sph_cubehash512_close(void *cc, void *dst)
Terminate the current CubeHash-512 computation and output the result into the provided buffer...
Definition: cubehash.c:709
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:674
CHash160 & Write(const unsigned char *data, size_t len)
Definition: hash.h:71
static const unsigned char k1[32]
CHashVerifier< Source > & operator>>(T &obj)
Definition: hash.h:244
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:84
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: hash.cpp:150
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:193
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:39
uint64_t tmp
Definition: hash.h:270
Luffa interface.
CubeHash interface.
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
void sph_shavite512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: shavite.c:1741
CHash256 ctx
Definition: hash.h:187
uint256 GetHash()
Definition: hash.h:203
BMW interface.
256-bit opaque blob.
Definition: uint256.h:123
void sph_echo512_close(void *cc, void *dst)
Terminate the current ECHO-512 computation and output the result into the provided buffer...
Definition: echo.c:1018
Source * source
Definition: hash.h:222
void sph_keccak512_init(void *cc)
Initialize a Keccak-512 context.
Definition: keccak.c:1795
This structure is a context for Luffa-512 computations.
Definition: sph_luffa.h:104
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:14
const int nType
Definition: hash.h:189
void sph_keccak512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: keccak.c:1802
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:65
CHashVerifier(Source *source_)
Definition: hash.h:225
void sph_shavite512_close(void *cc, void *dst)
Terminate the current SHAvite-512 computation and output the result into the provided buffer...
Definition: shavite.c:1748
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: hash.cpp:208
160-bit opaque blob.
Definition: uint256.h:112
SipHash-2-4.
Definition: hash.h:266
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
int GetVersion() const
Definition: hash.h:196
static const size_t OUTPUT_SIZE
Definition: hash.h:39
CHashWriter & operator<<(const T &obj)
Definition: hash.h:210
void Unserialize(Stream &s, char &a)
Definition: serialize.h:196
iterator begin()
Definition: prevector.h:318
void read(char *pch, size_t nSize)
Definition: hash.h:227
void sph_groestl512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: groestl.c:3102
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:184
This structure is a context for JH computations: it contains the intermediate values and some data fr...
Definition: sph_jh.h:76
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
int GetType() const
Definition: hash.h:195
A hasher class for Bitcoin&#39;s 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:59
Groestl interface.
void sph_groestl512_init(void *cc)
Initialize a Groestl-512 context.
Definition: groestl.c:3095
CSHA256 sha
Definition: hash.h:61
void sph_simd512_init(void *cc)
Initialize an SIMD-512 context.
Definition: simd.c:1774
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
This structure is a context for Keccak computations: it contains the intermediate values and some dat...
Definition: sph_keccak.h:76
void sph_jh512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: jh.c:1095
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:3109
A hasher class for SHA-256.
Definition: sha256.h:13
CHash256 & Reset()
Definition: hash.h:52
void sph_cubehash512_init(void *cc)
Initialize a CubeHash-512 context.
Definition: cubehash.c:695
uint256 HashX11(const T1 pbegin, const T1 pend)
Definition: hash.h:302
Keccak interface.
This structure is a context for CubeHash computations: it contains the intermediate values and some d...
Definition: sph_cubehash.h:77
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
This structure is a context for Groestl-384 and Groestl-512 computations: it contains the intermediat...
Definition: sph_groestl.h:115
void sph_luffa512_init(void *cc)
Initialize a Luffa-512 context.
Definition: luffa.c:1393
Released under the MIT license