]> Git Repo - VerusCoin.git/blame - src/hash.h
Adjust consensus rules to require v4 transactions from Sapling activation
[VerusCoin.git] / src / hash.h
CommitLineData
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
0fb9073e 4// file COPYING or http://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"
29a8ade7 11#include "prevector.h"
0fb9073e 12#include "serialize.h"
51ed9ec9
BD
13#include "uint256.h"
14#include "version.h"
0fb9073e 15
132dc81f
A
16#include "sodium.h"
17
7ab026f4 18#include <vector>
0fb9073e 19
a5748996
CF
20typedef uint256 ChainCode;
21
7b4737c8
PW
22/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
23class CHash256 {
24private:
25 CSHA256 sha;
26public:
a0495bb6
PW
27 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
28
29 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
30 unsigned char buf[sha.OUTPUT_SIZE];
7b4737c8 31 sha.Finalize(buf);
a0495bb6 32 sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
7b4737c8
PW
33 }
34
35 CHash256& Write(const unsigned char *data, size_t len) {
36 sha.Write(data, len);
37 return *this;
38 }
39
40 CHash256& Reset() {
41 sha.Reset();
42 return *this;
43 }
44};
45
46/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
47class CHash160 {
48private:
49 CSHA256 sha;
50public:
a0495bb6
PW
51 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
52
53 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
54 unsigned char buf[sha.OUTPUT_SIZE];
7b4737c8 55 sha.Finalize(buf);
a0495bb6 56 CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
7b4737c8
PW
57 }
58
59 CHash160& Write(const unsigned char *data, size_t len) {
60 sha.Write(data, len);
61 return *this;
62 }
63
64 CHash160& Reset() {
65 sha.Reset();
66 return *this;
67 }
68};
69
70/** Compute the 256-bit hash of an object. */
0fb9073e
PW
71template<typename T1>
72inline uint256 Hash(const T1 pbegin, const T1 pend)
73{
7b4737c8
PW
74 static const unsigned char pblank[1] = {};
75 uint256 result;
76 CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
77 .Finalize((unsigned char*)&result);
78 return result;
79}
80
81/** Compute the 256-bit hash of the concatenation of two objects. */
82template<typename T1, typename T2>
83inline uint256 Hash(const T1 p1begin, const T1 p1end,
84 const T2 p2begin, const T2 p2end) {
85 static const unsigned char pblank[1] = {};
86 uint256 result;
87 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
88 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
89 .Finalize((unsigned char*)&result);
90 return result;
91}
92
93/** Compute the 256-bit hash of the concatenation of three objects. */
94template<typename T1, typename T2, typename T3>
95inline uint256 Hash(const T1 p1begin, const T1 p1end,
96 const T2 p2begin, const T2 p2end,
97 const T3 p3begin, const T3 p3end) {
98 static const unsigned char pblank[1] = {};
99 uint256 result;
100 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
101 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
102 .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
103 .Finalize((unsigned char*)&result);
104 return result;
105}
106
107/** Compute the 160-bit hash an object. */
108template<typename T1>
109inline uint160 Hash160(const T1 pbegin, const T1 pend)
110{
111 static unsigned char pblank[1] = {};
112 uint160 result;
113 CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
114 .Finalize((unsigned char*)&result);
115 return result;
116}
117
118/** Compute the 160-bit hash of a vector. */
119inline uint160 Hash160(const std::vector<unsigned char>& vch)
120{
121 return Hash160(vch.begin(), vch.end());
0fb9073e
PW
122}
123
29a8ade7
PW
124/** Compute the 160-bit hash of a vector. */
125template<unsigned int N>
126inline uint160 Hash160(const prevector<N, unsigned char>& vch)
127{
128 return Hash160(vch.begin(), vch.end());
129}
130
7b4737c8 131/** A writer stream (for serialization) that computes a 256-bit hash. */
0fb9073e
PW
132class CHashWriter
133{
134private:
7b4737c8 135 CHash256 ctx;
0fb9073e 136
7f4acac4
PW
137 const int nType;
138 const int nVersion;
0fb9073e 139public:
0fb9073e 140
7b4737c8 141 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
0fb9073e 142
242f1421
PW
143 int GetType() const { return nType; }
144 int GetVersion() const { return nVersion; }
145
1315591c 146 void write(const char *pch, size_t size) {
7b4737c8 147 ctx.Write((const unsigned char*)pch, size);
0fb9073e
PW
148 }
149
150 // invalidates the object
151 uint256 GetHash() {
7b4737c8
PW
152 uint256 result;
153 ctx.Finalize((unsigned char*)&result);
154 return result;
0fb9073e
PW
155 }
156
157 template<typename T>
158 CHashWriter& operator<<(const T& obj) {
159 // Serialize to this stream
242f1421 160 ::Serialize(*this, obj);
0fb9073e
PW
161 return (*this);
162 }
163};
164
132dc81f
A
165
166/** A writer stream (for serialization) that computes a 256-bit BLAKE2b hash. */
167class CBLAKE2bWriter
168{
169private:
170 crypto_generichash_blake2b_state state;
171
172public:
173 int nType;
174 int nVersion;
175
176 CBLAKE2bWriter(int nTypeIn, int nVersionIn, const unsigned char* personal) : nType(nTypeIn), nVersion(nVersionIn) {
177 assert(crypto_generichash_blake2b_init_salt_personal(
178 &state,
179 NULL, 0, // No key.
180 32,
181 NULL, // No salt.
182 personal) == 0);
183 }
184
185 CBLAKE2bWriter& write(const char *pch, size_t size) {
186 crypto_generichash_blake2b_update(&state, (const unsigned char*)pch, size);
187 return (*this);
188 }
189
190 // invalidates the object
191 uint256 GetHash() {
192 uint256 result;
193 crypto_generichash_blake2b_final(&state, (unsigned char*)&result, 32);
194 return result;
195 }
196
197 template<typename T>
198 CBLAKE2bWriter& operator<<(const T& obj) {
199 // Serialize to this stream
68a1a592 200 ::Serialize(*this, obj);
132dc81f
A
201 return (*this);
202 }
203};
204
205
7b4737c8 206/** Compute the 256-bit hash of an object's serialization. */
0fb9073e
PW
207template<typename T>
208uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
209{
210 CHashWriter ss(nType, nVersion);
211 ss << obj;
212 return ss.GetHash();
213}
214
7ab026f4
MC
215unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
216
a5748996 217void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
b4347f60 218
093303a8 219#endif // BITCOIN_HASH_H
This page took 0.164363 seconds and 4 git commands to generate.