Add missing cs_main locks when calling blockToJSON/blockheaderToJSON
[bitcoinplatinum.git] / src / hash.h
blob474b13d65b50b444d89a5b9beafb71f39870ac6d
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_HASH_H
7 #define BITCOIN_HASH_H
9 #include "crypto/ripemd160.h"
10 #include "crypto/sha256.h"
11 #include "prevector.h"
12 #include "serialize.h"
13 #include "uint256.h"
14 #include "version.h"
16 #include <vector>
18 typedef uint256 ChainCode;
20 /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
21 class CHash256 {
22 private:
23 CSHA256 sha;
24 public:
25 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
27 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
28 unsigned char buf[CSHA256::OUTPUT_SIZE];
29 sha.Finalize(buf);
30 sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
33 CHash256& Write(const unsigned char *data, size_t len) {
34 sha.Write(data, len);
35 return *this;
38 CHash256& Reset() {
39 sha.Reset();
40 return *this;
44 /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
45 class CHash160 {
46 private:
47 CSHA256 sha;
48 public:
49 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
51 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
52 unsigned char buf[CSHA256::OUTPUT_SIZE];
53 sha.Finalize(buf);
54 CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
57 CHash160& Write(const unsigned char *data, size_t len) {
58 sha.Write(data, len);
59 return *this;
62 CHash160& Reset() {
63 sha.Reset();
64 return *this;
68 /** Compute the 256-bit hash of an object. */
69 template<typename T1>
70 inline uint256 Hash(const T1 pbegin, const T1 pend)
72 static const unsigned char pblank[1] = {};
73 uint256 result;
74 CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
75 .Finalize((unsigned char*)&result);
76 return result;
79 /** Compute the 256-bit hash of the concatenation of two objects. */
80 template<typename T1, typename T2>
81 inline uint256 Hash(const T1 p1begin, const T1 p1end,
82 const T2 p2begin, const T2 p2end) {
83 static const unsigned char pblank[1] = {};
84 uint256 result;
85 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
86 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
87 .Finalize((unsigned char*)&result);
88 return result;
91 /** Compute the 160-bit hash an object. */
92 template<typename T1>
93 inline uint160 Hash160(const T1 pbegin, const T1 pend)
95 static unsigned char pblank[1] = {};
96 uint160 result;
97 CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
98 .Finalize((unsigned char*)&result);
99 return result;
102 /** Compute the 160-bit hash of a vector. */
103 inline uint160 Hash160(const std::vector<unsigned char>& vch)
105 return Hash160(vch.begin(), vch.end());
108 /** Compute the 160-bit hash of a vector. */
109 template<unsigned int N>
110 inline uint160 Hash160(const prevector<N, unsigned char>& vch)
112 return Hash160(vch.begin(), vch.end());
115 /** A writer stream (for serialization) that computes a 256-bit hash. */
116 class CHashWriter
118 private:
119 CHash256 ctx;
121 const int nType;
122 const int nVersion;
123 public:
125 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
127 int GetType() const { return nType; }
128 int GetVersion() const { return nVersion; }
130 void write(const char *pch, size_t size) {
131 ctx.Write((const unsigned char*)pch, size);
134 // invalidates the object
135 uint256 GetHash() {
136 uint256 result;
137 ctx.Finalize((unsigned char*)&result);
138 return result;
141 template<typename T>
142 CHashWriter& operator<<(const T& obj) {
143 // Serialize to this stream
144 ::Serialize(*this, obj);
145 return (*this);
149 /** Reads data from an underlying stream, while hashing the read data. */
150 template<typename Source>
151 class CHashVerifier : public CHashWriter
153 private:
154 Source* source;
156 public:
157 explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
159 void read(char* pch, size_t nSize)
161 source->read(pch, nSize);
162 this->write(pch, nSize);
165 void ignore(size_t nSize)
167 char data[1024];
168 while (nSize > 0) {
169 size_t now = std::min<size_t>(nSize, 1024);
170 read(data, now);
171 nSize -= now;
175 template<typename T>
176 CHashVerifier<Source>& operator>>(T& obj)
178 // Unserialize from this stream
179 ::Unserialize(*this, obj);
180 return (*this);
184 /** Compute the 256-bit hash of an object's serialization. */
185 template<typename T>
186 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
188 CHashWriter ss(nType, nVersion);
189 ss << obj;
190 return ss.GetHash();
193 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
195 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
197 /** SipHash-2-4 */
198 class CSipHasher
200 private:
201 uint64_t v[4];
202 uint64_t tmp;
203 int count;
205 public:
206 /** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */
207 CSipHasher(uint64_t k0, uint64_t k1);
208 /** Hash a 64-bit integer worth of data
209 * It is treated as if this was the little-endian interpretation of 8 bytes.
210 * This function can only be used when a multiple of 8 bytes have been written so far.
212 CSipHasher& Write(uint64_t data);
213 /** Hash arbitrary bytes. */
214 CSipHasher& Write(const unsigned char* data, size_t size);
215 /** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */
216 uint64_t Finalize() const;
219 /** Optimized SipHash-2-4 implementation for uint256.
221 * It is identical to:
222 * SipHasher(k0, k1)
223 * .Write(val.GetUint64(0))
224 * .Write(val.GetUint64(1))
225 * .Write(val.GetUint64(2))
226 * .Write(val.GetUint64(3))
227 * .Finalize()
229 uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val);
230 uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra);
232 #endif // BITCOIN_HASH_H