Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present 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. |
5 | | |
6 | | #ifndef BITCOIN_HASH_H |
7 | | #define BITCOIN_HASH_H |
8 | | |
9 | | #include <attributes.h> |
10 | | #include <crypto/common.h> |
11 | | #include <crypto/ripemd160.h> |
12 | | #include <crypto/sha256.h> |
13 | | #include <prevector.h> |
14 | | #include <serialize.h> |
15 | | #include <span.h> |
16 | | #include <support/cleanse.h> |
17 | | #include <uint256.h> |
18 | | |
19 | | #include <string> |
20 | | #include <vector> |
21 | | |
22 | | /** A BIP32 chain code. Cleansed on destruction. */ |
23 | | class ChainCode : public base_blob<256> { |
24 | | public: |
25 | 1.17M | constexpr ChainCode() = default; |
26 | 0 | constexpr explicit ChainCode(std::span<const unsigned char> vch) : base_blob<256>(vch) {} |
27 | 0 | constexpr explicit ChainCode(const base_blob<256>& b) : base_blob<256>(b) {} |
28 | 18.8M | ~ChainCode() { memory_cleanse(data(), size()); } |
29 | | }; |
30 | | |
31 | | /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */ |
32 | | class CHash256 { |
33 | | private: |
34 | | CSHA256 sha; |
35 | | public: |
36 | | static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; |
37 | | |
38 | 1.71M | void Finalize(std::span<unsigned char> output) { |
39 | 1.71M | assert(output.size() == OUTPUT_SIZE); |
40 | 1.71M | unsigned char buf[CSHA256::OUTPUT_SIZE]; |
41 | 1.71M | sha.Finalize(buf); |
42 | 1.71M | sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); |
43 | 1.71M | } |
44 | | |
45 | 2.93M | CHash256& Write(std::span<const unsigned char> input) { |
46 | 2.93M | sha.Write(input.data(), input.size()); |
47 | 2.93M | return *this; |
48 | 2.93M | } |
49 | | |
50 | 154k | CHash256& Reset() { |
51 | 154k | sha.Reset(); |
52 | 154k | return *this; |
53 | 154k | } |
54 | | }; |
55 | | |
56 | | /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */ |
57 | | class CHash160 { |
58 | | private: |
59 | | CSHA256 sha; |
60 | | public: |
61 | | static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; |
62 | | |
63 | 6.30M | void Finalize(std::span<unsigned char> output) { |
64 | 6.30M | assert(output.size() == OUTPUT_SIZE); |
65 | 6.30M | unsigned char buf[CSHA256::OUTPUT_SIZE]; |
66 | 6.30M | sha.Finalize(buf); |
67 | 6.30M | CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); |
68 | 6.30M | } |
69 | | |
70 | 6.30M | CHash160& Write(std::span<const unsigned char> input) { |
71 | 6.30M | sha.Write(input.data(), input.size()); |
72 | 6.30M | return *this; |
73 | 6.30M | } |
74 | | |
75 | 0 | CHash160& Reset() { |
76 | 0 | sha.Reset(); |
77 | 0 | return *this; |
78 | 0 | } |
79 | | }; |
80 | | |
81 | | /** Compute the 256-bit hash of an object. */ |
82 | | template<typename T> |
83 | | inline uint256 Hash(const T& in1) |
84 | 399k | { |
85 | 399k | uint256 result; |
86 | 399k | CHash256().Write(MakeUCharSpan(in1)).Finalize(result); |
87 | 399k | return result; |
88 | 399k | } uint256 Hash<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&) Line | Count | Source | 84 | 3.28k | { | 85 | 3.28k | uint256 result; | 86 | 3.28k | CHash256().Write(MakeUCharSpan(in1)).Finalize(result); | 87 | 3.28k | return result; | 88 | 3.28k | } |
uint256 Hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) Line | Count | Source | 84 | 276 | { | 85 | 276 | uint256 result; | 86 | 276 | CHash256().Write(MakeUCharSpan(in1)).Finalize(result); | 87 | 276 | return result; | 88 | 276 | } |
uint256 Hash<DataStream>(DataStream const&) Line | Count | Source | 84 | 1 | { | 85 | 1 | uint256 result; | 86 | 1 | CHash256().Write(MakeUCharSpan(in1)).Finalize(result); | 87 | 1 | return result; | 88 | 1 | } |
uint256 Hash<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&) Line | Count | Source | 84 | 368k | { | 85 | 368k | uint256 result; | 86 | 368k | CHash256().Write(MakeUCharSpan(in1)).Finalize(result); | 87 | 368k | return result; | 88 | 368k | } |
uint256 Hash<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&) Line | Count | Source | 84 | 27.0k | { | 85 | 27.0k | uint256 result; | 86 | 27.0k | CHash256().Write(MakeUCharSpan(in1)).Finalize(result); | 87 | 27.0k | return result; | 88 | 27.0k | } |
|
89 | | |
90 | | /** Compute the 256-bit hash of the concatenation of two objects. */ |
91 | | template<typename T1, typename T2> |
92 | 1.00M | inline uint256 Hash(const T1& in1, const T2& in2) { |
93 | 1.00M | uint256 result; |
94 | 1.00M | CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result); |
95 | 1.00M | return result; |
96 | 1.00M | } uint256 Hash<uint256, uint256>(uint256 const&, uint256 const&) Line | Count | Source | 92 | 997k | inline uint256 Hash(const T1& in1, const T2& in2) { | 93 | 997k | uint256 result; | 94 | 997k | CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result); | 95 | 997k | return result; | 96 | 997k | } |
uint256 Hash<CPubKey, std::vector<unsigned char, secure_allocator<unsigned char>>>(CPubKey const&, std::vector<unsigned char, secure_allocator<unsigned char>> const&) Line | Count | Source | 92 | 6.71k | inline uint256 Hash(const T1& in1, const T2& in2) { | 93 | 6.71k | uint256 result; | 94 | 6.71k | CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result); | 95 | 6.71k | return result; | 96 | 6.71k | } |
uint256 Hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char [8]>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned char const (&) [8]) Line | Count | Source | 92 | 4.59k | inline uint256 Hash(const T1& in1, const T2& in2) { | 93 | 4.59k | uint256 result; | 94 | 4.59k | CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result); | 95 | 4.59k | return result; | 96 | 4.59k | } |
|
97 | | |
98 | | /** Compute the 160-bit hash an object. */ |
99 | | template<typename T1> |
100 | | inline uint160 Hash160(const T1& in1) |
101 | 6.15M | { |
102 | 6.15M | uint160 result; |
103 | 6.15M | CHash160().Write(MakeUCharSpan(in1)).Finalize(result); |
104 | 6.15M | return result; |
105 | 6.15M | } uint160 Hash160<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&) Line | Count | Source | 101 | 5.83M | { | 102 | 5.83M | uint160 result; | 103 | 5.83M | CHash160().Write(MakeUCharSpan(in1)).Finalize(result); | 104 | 5.83M | return result; | 105 | 5.83M | } |
uint160 Hash160<XOnlyPubKey>(XOnlyPubKey const&) Line | Count | Source | 101 | 19.5k | { | 102 | 19.5k | uint160 result; | 103 | 19.5k | CHash160().Write(MakeUCharSpan(in1)).Finalize(result); | 104 | 19.5k | return result; | 105 | 19.5k | } |
uint160 Hash160<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&) Line | Count | Source | 101 | 2 | { | 102 | 2 | uint160 result; | 103 | 2 | CHash160().Write(MakeUCharSpan(in1)).Finalize(result); | 104 | 2 | return result; | 105 | 2 | } |
uint160 Hash160<CPubKey>(CPubKey const&) Line | Count | Source | 101 | 2 | { | 102 | 2 | uint160 result; | 103 | 2 | CHash160().Write(MakeUCharSpan(in1)).Finalize(result); | 104 | 2 | return result; | 105 | 2 | } |
uint160 Hash160<CScript>(CScript const&) Line | Count | Source | 101 | 291k | { | 102 | 291k | uint160 result; | 103 | 291k | CHash160().Write(MakeUCharSpan(in1)).Finalize(result); | 104 | 291k | return result; | 105 | 291k | } |
|
106 | | |
107 | | /** A writer stream (for serialization) that computes a 256-bit hash. */ |
108 | | class HashWriter |
109 | | { |
110 | | private: |
111 | | CSHA256 ctx; |
112 | | |
113 | | public: |
114 | | void write(std::span<const std::byte> src) |
115 | 423M | { |
116 | 423M | ctx.Write(UCharCast(src.data()), src.size()); |
117 | 423M | } |
118 | | |
119 | | /** Compute the double-SHA256 hash of all data written to this object. |
120 | | * |
121 | | * Invalidates this object. |
122 | | */ |
123 | 51.0M | uint256 GetHash() { |
124 | 51.0M | uint256 result; |
125 | 51.0M | ctx.Finalize(result.begin()); |
126 | 51.0M | ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin()); |
127 | 51.0M | return result; |
128 | 51.0M | } |
129 | | |
130 | | /** Compute the SHA256 hash of all data written to this object. |
131 | | * |
132 | | * Invalidates this object. |
133 | | */ |
134 | 1.35M | uint256 GetSHA256() { |
135 | 1.35M | uint256 result; |
136 | 1.35M | ctx.Finalize(result.begin()); |
137 | 1.35M | return result; |
138 | 1.35M | } |
139 | | |
140 | | /** |
141 | | * Returns the first 64 bits from the resulting hash. |
142 | | */ |
143 | 225k | inline uint64_t GetCheapHash() { |
144 | 225k | uint256 result = GetHash(); |
145 | 225k | return ReadLE64(result.begin()); |
146 | 225k | } |
147 | | |
148 | | template <typename T> |
149 | | HashWriter& operator<<(const T& obj) |
150 | 220M | { |
151 | 220M | ::Serialize(*this, obj); |
152 | 220M | return *this; |
153 | 220M | } HashWriter& HashWriter::operator<<<int>(int const&) Line | Count | Source | 150 | 435k | { | 151 | 435k | ::Serialize(*this, obj); | 152 | 435k | return *this; | 153 | 435k | } |
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CMutableTransaction>>(ParamsWrapper<TransactionSerParams, CMutableTransaction> const&) Line | Count | Source | 150 | 50.0k | { | 151 | 50.0k | ::Serialize(*this, obj); | 152 | 50.0k | return *this; | 153 | 50.0k | } |
HashWriter& HashWriter::operator<<<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&) Line | Count | Source | 150 | 970k | { | 151 | 970k | ::Serialize(*this, obj); | 152 | 970k | return *this; | 153 | 970k | } |
HashWriter& HashWriter::operator<<<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&) Line | Count | Source | 150 | 270k | { | 151 | 270k | ::Serialize(*this, obj); | 152 | 270k | return *this; | 153 | 270k | } |
HashWriter& HashWriter::operator<<<std::vector<bool, std::allocator<bool>>>(std::vector<bool, std::allocator<bool>> const&) Line | Count | Source | 150 | 1 | { | 151 | 1 | ::Serialize(*this, obj); | 152 | 1 | return *this; | 153 | 1 | } |
HashWriter& HashWriter::operator<<<transaction_identifier<true>>(transaction_identifier<true> const&) Line | Count | Source | 150 | 888 | { | 151 | 888 | ::Serialize(*this, obj); | 152 | 888 | return *this; | 153 | 888 | } |
HashWriter& HashWriter::operator<<<std::span<unsigned char const, 32ul>>(std::span<unsigned char const, 32ul> const&) Line | Count | Source | 150 | 120M | { | 151 | 120M | ::Serialize(*this, obj); | 152 | 120M | return *this; | 153 | 120M | } |
HashWriter& HashWriter::operator<<<transaction_identifier<false>>(transaction_identifier<false> const&) Line | Count | Source | 150 | 3 | { | 151 | 3 | ::Serialize(*this, obj); | 152 | 3 | return *this; | 153 | 3 | } |
HashWriter& HashWriter::operator<<<uint256>(uint256 const&) Line | Count | Source | 150 | 2.31M | { | 151 | 2.31M | ::Serialize(*this, obj); | 152 | 2.31M | return *this; | 153 | 2.31M | } |
HashWriter& HashWriter::operator<<<unsigned long>(unsigned long const&) Line | Count | Source | 150 | 47.0k | { | 151 | 47.0k | ::Serialize(*this, obj); | 152 | 47.0k | return *this; | 153 | 47.0k | } |
HashWriter& HashWriter::operator<<<unsigned char>(unsigned char const&) Line | Count | Source | 150 | 1.33M | { | 151 | 1.33M | ::Serialize(*this, obj); | 152 | 1.33M | return *this; | 153 | 1.33M | } |
HashWriter& HashWriter::operator<<<COutPoint>(COutPoint const&) Line | Count | Source | 150 | 20.5M | { | 151 | 20.5M | ::Serialize(*this, obj); | 152 | 20.5M | return *this; | 153 | 20.5M | } |
HashWriter& HashWriter::operator<<<unsigned int>(unsigned int const&) Line | Count | Source | 150 | 8.21M | { | 151 | 8.21M | ::Serialize(*this, obj); | 152 | 8.21M | return *this; | 153 | 8.21M | } |
HashWriter& HashWriter::operator<<<CTxOut>(CTxOut const&) Line | Count | Source | 150 | 13.9M | { | 151 | 13.9M | ::Serialize(*this, obj); | 152 | 13.9M | return *this; | 153 | 13.9M | } |
HashWriter& HashWriter::operator<<<CBlockUndo>(CBlockUndo const&) Line | Count | Source | 150 | 102k | { | 151 | 102k | ::Serialize(*this, obj); | 152 | 102k | return *this; | 153 | 102k | } |
HashWriter& HashWriter::operator<<<Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&> const&) Line | Count | Source | 150 | 67.7k | { | 151 | 67.7k | ::Serialize(*this, obj); | 152 | 67.7k | return *this; | 153 | 67.7k | } |
HashWriter& HashWriter::operator<<<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&) Line | Count | Source | 150 | 8.87k | { | 151 | 8.87k | ::Serialize(*this, obj); | 152 | 8.87k | return *this; | 153 | 8.87k | } |
HashWriter& HashWriter::operator<<<Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&> const&) Line | Count | Source | 150 | 58.9k | { | 151 | 58.9k | ::Serialize(*this, obj); | 152 | 58.9k | return *this; | 153 | 58.9k | } |
HashWriter& HashWriter::operator<<<CBlockHeader>(CBlockHeader const&) Line | Count | Source | 150 | 48.4M | { | 151 | 48.4M | ::Serialize(*this, obj); | 152 | 48.4M | return *this; | 153 | 48.4M | } |
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CMutableTransaction const>>(ParamsWrapper<TransactionSerParams, CMutableTransaction const> const&) Line | Count | Source | 150 | 685k | { | 151 | 685k | ::Serialize(*this, obj); | 152 | 685k | return *this; | 153 | 685k | } |
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CTransaction const>>(ParamsWrapper<TransactionSerParams, CTransaction const> const&) Line | Count | Source | 150 | 1.47M | { | 151 | 1.47M | ::Serialize(*this, obj); | 152 | 1.47M | return *this; | 153 | 1.47M | } |
HashWriter& HashWriter::operator<<<long>(long const&) Line | Count | Source | 150 | 174k | { | 151 | 174k | ::Serialize(*this, obj); | 152 | 174k | return *this; | 153 | 174k | } |
HashWriter& HashWriter::operator<<<CScript>(CScript const&) Line | Count | Source | 150 | 174k | { | 151 | 174k | ::Serialize(*this, obj); | 152 | 174k | return *this; | 153 | 174k | } |
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CTransaction> const&) Line | Count | Source | 150 | 40.1k | { | 151 | 40.1k | ::Serialize(*this, obj); | 152 | 40.1k | return *this; | 153 | 40.1k | } |
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction> const&) Line | Count | Source | 150 | 82.6k | { | 151 | 82.6k | ::Serialize(*this, obj); | 152 | 82.6k | return *this; | 153 | 82.6k | } |
HashWriter& HashWriter::operator<<<CompactSizeWriter>(CompactSizeWriter const&) Line | Count | Source | 150 | 136k | { | 151 | 136k | ::Serialize(*this, obj); | 152 | 136k | return *this; | 153 | 136k | } |
HashWriter& HashWriter::operator<<<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) Line | Count | Source | 150 | 52 | { | 151 | 52 | ::Serialize(*this, obj); | 152 | 52 | return *this; | 153 | 52 | } |
HashWriter& HashWriter::operator<<<CPubKey>(CPubKey const&) Line | Count | Source | 150 | 798 | { | 151 | 798 | ::Serialize(*this, obj); | 152 | 798 | return *this; | 153 | 798 | } |
HashWriter& HashWriter::operator<<<std::span<std::byte const, 18446744073709551615ul>>(std::span<std::byte const, 18446744073709551615ul> const&) Line | Count | Source | 150 | 26 | { | 151 | 26 | ::Serialize(*this, obj); | 152 | 26 | return *this; | 153 | 26 | } |
HashWriter& HashWriter::operator<<<unsigned char [384]>(unsigned char const (&) [384]) Line | Count | Source | 150 | 4.09k | { | 151 | 4.09k | ::Serialize(*this, obj); | 152 | 4.09k | return *this; | 153 | 4.09k | } |
|
154 | | }; |
155 | | |
156 | | /** Reads data from an underlying stream, while hashing the read data. */ |
157 | | template <typename Source> |
158 | | class HashVerifier : public HashWriter |
159 | | { |
160 | | private: |
161 | | Source& m_source; |
162 | | |
163 | | public: |
164 | 37.3k | explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}HashVerifier<DataStream>::HashVerifier(DataStream&) Line | Count | Source | 164 | 3 | explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {} |
HashVerifier<AutoFile>::HashVerifier(AutoFile&) Line | Count | Source | 164 | 601 | explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {} |
HashVerifier<BufferedReader<AutoFile>>::HashVerifier(BufferedReader<AutoFile>&) Line | Count | Source | 164 | 36.7k | explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {} |
|
165 | | |
166 | | void read(std::span<std::byte> dst) |
167 | 1.11M | { |
168 | 1.11M | m_source.read(dst); |
169 | 1.11M | this->write(dst); |
170 | 1.11M | } HashVerifier<DataStream>::read(std::span<std::byte, 18446744073709551615ul>) Line | Count | Source | 167 | 1.08k | { | 168 | 1.08k | m_source.read(dst); | 169 | 1.08k | this->write(dst); | 170 | 1.08k | } |
HashVerifier<AutoFile>::read(std::span<std::byte, 18446744073709551615ul>) Line | Count | Source | 167 | 851k | { | 168 | 851k | m_source.read(dst); | 169 | 851k | this->write(dst); | 170 | 851k | } |
HashVerifier<BufferedReader<AutoFile>>::read(std::span<std::byte, 18446744073709551615ul>) Line | Count | Source | 167 | 263k | { | 168 | 263k | m_source.read(dst); | 169 | 263k | this->write(dst); | 170 | 263k | } |
|
171 | | |
172 | | void ignore(size_t num_bytes) |
173 | 0 | { |
174 | 0 | std::byte data[1024]; |
175 | 0 | while (num_bytes > 0) { |
176 | 0 | size_t now = std::min<size_t>(num_bytes, 1024); |
177 | 0 | read({data, now}); |
178 | 0 | num_bytes -= now; |
179 | 0 | } |
180 | 0 | } Unexecuted instantiation: HashVerifier<AutoFile>::ignore(unsigned long) Unexecuted instantiation: HashVerifier<DataStream>::ignore(unsigned long) Unexecuted instantiation: HashVerifier<BufferedReader<AutoFile>>::ignore(unsigned long) |
181 | | |
182 | | template <typename T> |
183 | | HashVerifier<Source>& operator>>(T&& obj) |
184 | 120k | { |
185 | 120k | ::Unserialize(*this, obj); |
186 | 120k | return *this; |
187 | 120k | } HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&) Line | Count | Source | 184 | 1 | { | 185 | 1 | ::Unserialize(*this, obj); | 186 | 1 | return *this; | 187 | 1 | } |
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><std::array<unsigned char, 4ul>&>(std::array<unsigned char, 4ul>&) Line | Count | Source | 184 | 2 | { | 185 | 2 | ::Unserialize(*this, obj); | 186 | 2 | return *this; | 187 | 2 | } |
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><AddrMan&>(AddrMan&) Line | Count | Source | 184 | 2 | { | 185 | 2 | ::Unserialize(*this, obj); | 186 | 2 | return *this; | 187 | 2 | } |
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><std::array<unsigned char, 4ul>&>(std::array<unsigned char, 4ul>&) Line | Count | Source | 184 | 601 | { | 185 | 601 | ::Unserialize(*this, obj); | 186 | 601 | return *this; | 187 | 601 | } |
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><AddrMan&>(AddrMan&) Line | Count | Source | 184 | 573 | { | 185 | 573 | ::Unserialize(*this, obj); | 186 | 573 | return *this; | 187 | 573 | } |
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>&>(ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>&) Line | Count | Source | 184 | 27 | { | 185 | 27 | ::Unserialize(*this, obj); | 186 | 27 | return *this; | 187 | 27 | } |
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>>(Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>&&) Line | Count | Source | 184 | 573 | { | 185 | 573 | ::Unserialize(*this, obj); | 186 | 573 | return *this; | 187 | 573 | } |
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>>(Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>&&) Line | Count | Source | 184 | 2 | { | 185 | 2 | ::Unserialize(*this, obj); | 186 | 2 | return *this; | 187 | 2 | } |
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><CBlockUndo&>(CBlockUndo&) Line | Count | Source | 184 | 36.7k | { | 185 | 36.7k | ::Unserialize(*this, obj); | 186 | 36.7k | return *this; | 187 | 36.7k | } |
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>&&) Line | Count | Source | 184 | 27.4k | { | 185 | 27.4k | ::Unserialize(*this, obj); | 186 | 27.4k | return *this; | 187 | 27.4k | } |
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>&&) Line | Count | Source | 184 | 27.4k | { | 185 | 27.4k | ::Unserialize(*this, obj); | 186 | 27.4k | return *this; | 187 | 27.4k | } |
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul>&&) Line | Count | Source | 184 | 27.4k | { | 185 | 27.4k | ::Unserialize(*this, obj); | 186 | 27.4k | return *this; | 187 | 27.4k | } |
|
188 | | }; |
189 | | |
190 | | /** Writes data to an underlying source stream, while hashing the written data. */ |
191 | | template <typename Source> |
192 | | class HashedSourceWriter : public HashWriter |
193 | | { |
194 | | private: |
195 | | Source& m_source; |
196 | | |
197 | | public: |
198 | 1.54k | explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}HashedSourceWriter<DataStream>::HashedSourceWriter(DataStream&) Line | Count | Source | 198 | 1 | explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {} |
HashedSourceWriter<AutoFile>::HashedSourceWriter(AutoFile&) Line | Count | Source | 198 | 1.54k | explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {} |
|
199 | | |
200 | | void write(std::span<const std::byte> src) |
201 | 2.20M | { |
202 | 2.20M | m_source.write(src); |
203 | 2.20M | HashWriter::write(src); |
204 | 2.20M | } HashedSourceWriter<DataStream>::write(std::span<std::byte const, 18446744073709551615ul>) Line | Count | Source | 201 | 2 | { | 202 | 2 | m_source.write(src); | 203 | 2 | HashWriter::write(src); | 204 | 2 | } |
HashedSourceWriter<AutoFile>::write(std::span<std::byte const, 18446744073709551615ul>) Line | Count | Source | 201 | 2.20M | { | 202 | 2.20M | m_source.write(src); | 203 | 2.20M | HashWriter::write(src); | 204 | 2.20M | } |
|
205 | | |
206 | | template <typename T> |
207 | | HashedSourceWriter& operator<<(const T& obj) |
208 | 3.08k | { |
209 | 3.08k | ::Serialize(*this, obj); |
210 | 3.08k | return *this; |
211 | 3.08k | } HashedSourceWriter<DataStream>& HashedSourceWriter<DataStream>::operator<<<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) Line | Count | Source | 208 | 1 | { | 209 | 1 | ::Serialize(*this, obj); | 210 | 1 | return *this; | 211 | 1 | } |
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<std::array<unsigned char, 4ul>>(std::array<unsigned char, 4ul> const&) Line | Count | Source | 208 | 1.54k | { | 209 | 1.54k | ::Serialize(*this, obj); | 210 | 1.54k | return *this; | 211 | 1.54k | } |
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<AddrMan>(AddrMan const&) Line | Count | Source | 208 | 1.51k | { | 209 | 1.51k | ::Serialize(*this, obj); | 210 | 1.51k | return *this; | 211 | 1.51k | } |
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>> const>>(ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>> const> const&) Line | Count | Source | 208 | 33 | { | 209 | 33 | ::Serialize(*this, obj); | 210 | 33 | return *this; | 211 | 33 | } |
|
212 | | }; |
213 | | |
214 | | /** Single-SHA256 a 32-byte input (represented as uint256). */ |
215 | | [[nodiscard]] uint256 SHA256Uint256(const uint256& input); |
216 | | |
217 | | unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash); |
218 | | |
219 | | void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]); |
220 | | |
221 | | /** Return a HashWriter primed for tagged hashes (as specified in BIP 340). |
222 | | * |
223 | | * The returned object will have SHA256(tag) written to it twice (= 64 bytes). |
224 | | * A tagged hash can be computed by feeding the message into this object, and |
225 | | * then calling HashWriter::GetSHA256(). |
226 | | */ |
227 | | HashWriter TaggedHash(const std::string& tag); |
228 | | |
229 | | /** Compute the 160-bit RIPEMD-160 hash of an array. */ |
230 | | inline uint160 RIPEMD160(std::span<const unsigned char> data) |
231 | 2.14k | { |
232 | 2.14k | uint160 result; |
233 | 2.14k | CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin()); |
234 | 2.14k | return result; |
235 | 2.14k | } |
236 | | |
237 | | #endif // BITCOIN_HASH_H |