/Users/eugenesiegel/btc/bitcoin/src/hash.h
| Line | Count | Source (jump to first uncovered line) | 
| 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 <uint256.h> | 
| 17 |  |  | 
| 18 |  | #include <string> | 
| 19 |  | #include <vector> | 
| 20 |  |  | 
| 21 |  | typedef uint256 ChainCode; | 
| 22 |  |  | 
| 23 |  | /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */ | 
| 24 |  | class CHash256 { | 
| 25 |  | private: | 
| 26 |  |     CSHA256 sha; | 
| 27 |  | public: | 
| 28 |  |     static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; | 
| 29 |  |  | 
| 30 | 16.9M |     void Finalize(std::span<unsigned char> output) { | 
| 31 | 16.9M |         assert(output.size() == OUTPUT_SIZE); | 
| 32 | 16.9M |         unsigned char buf[CSHA256::OUTPUT_SIZE]; | 
| 33 | 16.9M |         sha.Finalize(buf); | 
| 34 | 16.9M |         sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); | 
| 35 | 16.9M |     } | 
| 36 |  |  | 
| 37 | 18.8M |     CHash256& Write(std::span<const unsigned char> input) { | 
| 38 | 18.8M |         sha.Write(input.data(), input.size()); | 
| 39 | 18.8M |         return *this; | 
| 40 | 18.8M |     } | 
| 41 |  |  | 
| 42 | 7.03M |     CHash256& Reset() { | 
| 43 | 7.03M |         sha.Reset(); | 
| 44 | 7.03M |         return *this; | 
| 45 | 7.03M |     } | 
| 46 |  | }; | 
| 47 |  |  | 
| 48 |  | /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */ | 
| 49 |  | class CHash160 { | 
| 50 |  | private: | 
| 51 |  |     CSHA256 sha; | 
| 52 |  | public: | 
| 53 |  |     static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; | 
| 54 |  |  | 
| 55 | 0 |     void Finalize(std::span<unsigned char> output) { | 
| 56 | 0 |         assert(output.size() == OUTPUT_SIZE); | 
| 57 | 0 |         unsigned char buf[CSHA256::OUTPUT_SIZE]; | 
| 58 | 0 |         sha.Finalize(buf); | 
| 59 | 0 |         CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); | 
| 60 | 0 |     } | 
| 61 |  |  | 
| 62 | 0 |     CHash160& Write(std::span<const unsigned char> input) { | 
| 63 | 0 |         sha.Write(input.data(), input.size()); | 
| 64 | 0 |         return *this; | 
| 65 | 0 |     } | 
| 66 |  |  | 
| 67 | 0 |     CHash160& Reset() { | 
| 68 | 0 |         sha.Reset(); | 
| 69 | 0 |         return *this; | 
| 70 | 0 |     } | 
| 71 |  | }; | 
| 72 |  |  | 
| 73 |  | /** Compute the 256-bit hash of an object. */ | 
| 74 |  | template<typename T> | 
| 75 |  | inline uint256 Hash(const T& in1) | 
| 76 | 8.05M | { | 
| 77 | 8.05M |     uint256 result; | 
| 78 | 8.05M |     CHash256().Write(MakeUCharSpan(in1)).Finalize(result); | 
| 79 | 8.05M |     return result; | 
| 80 | 8.05M | } Unexecuted instantiation: _Z4HashINSt3__14spanIKhLm18446744073709551615EEEE7uint256RKT__Z4HashINSt3__16vectorIhNS0_9allocatorIhEEEEE7uint256RKT_| Line | Count | Source |  | 76 | 8.05M | { |  | 77 | 8.05M |     uint256 result; |  | 78 | 8.05M |     CHash256().Write(MakeUCharSpan(in1)).Finalize(result); |  | 79 | 8.05M |     return result; |  | 80 | 8.05M | } | 
Unexecuted instantiation: _Z4HashINSt3__14spanIhLm18446744073709551615EEEE7uint256RKT_ | 
| 81 |  |  | 
| 82 |  | /** Compute the 256-bit hash of the concatenation of two objects. */ | 
| 83 |  | template<typename T1, typename T2> | 
| 84 | 0 | inline uint256 Hash(const T1& in1, const T2& in2) { | 
| 85 | 0 |     uint256 result; | 
| 86 | 0 |     CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result); | 
| 87 | 0 |     return result; | 
| 88 | 0 | } Unexecuted instantiation: _Z4HashI7uint256S0_ES0_RKT_RKT0_Unexecuted instantiation: _Z4HashINSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEA8_hE7uint256RKT_RKT0_ | 
| 89 |  |  | 
| 90 |  | /** Compute the 160-bit hash an object. */ | 
| 91 |  | template<typename T1> | 
| 92 |  | inline uint160 Hash160(const T1& in1) | 
| 93 | 0 | { | 
| 94 | 0 |     uint160 result; | 
| 95 | 0 |     CHash160().Write(MakeUCharSpan(in1)).Finalize(result); | 
| 96 | 0 |     return result; | 
| 97 | 0 | } Unexecuted instantiation: _Z7Hash160INSt3__14spanIKhLm18446744073709551615EEEE7uint160RKT_Unexecuted instantiation: _Z7Hash160INSt3__16vectorIhNS0_9allocatorIhEEEEE7uint160RKT_Unexecuted instantiation: _Z7Hash160I11XOnlyPubKeyE7uint160RKT_Unexecuted instantiation: _Z7Hash160I7CPubKeyE7uint160RKT_Unexecuted instantiation: _Z7Hash160I7CScriptE7uint160RKT_ | 
| 98 |  |  | 
| 99 |  | /** A writer stream (for serialization) that computes a 256-bit hash. */ | 
| 100 |  | class HashWriter | 
| 101 |  | { | 
| 102 |  | private: | 
| 103 |  |     CSHA256 ctx; | 
| 104 |  |  | 
| 105 |  | public: | 
| 106 |  |     void write(std::span<const std::byte> src) | 
| 107 | 598M |     { | 
| 108 | 598M |         ctx.Write(UCharCast(src.data()), src.size()); | 
| 109 | 598M |     } | 
| 110 |  |  | 
| 111 |  |     /** Compute the double-SHA256 hash of all data written to this object. | 
| 112 |  |      * | 
| 113 |  |      * Invalidates this object. | 
| 114 |  |      */ | 
| 115 | 56.1M |     uint256 GetHash() { | 
| 116 | 56.1M |         uint256 result; | 
| 117 | 56.1M |         ctx.Finalize(result.begin()); | 
| 118 | 56.1M |         ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin()); | 
| 119 | 56.1M |         return result; | 
| 120 | 56.1M |     } | 
| 121 |  |  | 
| 122 |  |     /** Compute the SHA256 hash of all data written to this object. | 
| 123 |  |      * | 
| 124 |  |      * Invalidates this object. | 
| 125 |  |      */ | 
| 126 | 1.51M |     uint256 GetSHA256() { | 
| 127 | 1.51M |         uint256 result; | 
| 128 | 1.51M |         ctx.Finalize(result.begin()); | 
| 129 | 1.51M |         return result; | 
| 130 | 1.51M |     } | 
| 131 |  |  | 
| 132 |  |     /** | 
| 133 |  |      * Returns the first 64 bits from the resulting hash. | 
| 134 |  |      */ | 
| 135 | 0 |     inline uint64_t GetCheapHash() { | 
| 136 | 0 |         uint256 result = GetHash(); | 
| 137 | 0 |         return ReadLE64(result.begin()); | 
| 138 | 0 |     } | 
| 139 |  |  | 
| 140 |  |     template <typename T> | 
| 141 |  |     HashWriter& operator<<(const T& obj) | 
| 142 | 108M |     { | 
| 143 | 108M |         ::Serialize(*this, obj); | 
| 144 | 108M |         return *this; | 
| 145 | 108M |     } _ZN10HashWriterlsIjEERS_RKT_| Line | Count | Source |  | 142 | 504k |     { |  | 143 | 504k |         ::Serialize(*this, obj); |  | 144 | 504k |         return *this; |  | 145 | 504k |     } | 
Unexecuted instantiation: _ZN10HashWriterlsINSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEERS_RKT__ZN10HashWriterlsINSt3__16vectorIhNS1_9allocatorIhEEEEEERS_RKT_| Line | Count | Source |  | 142 | 51.2k |     { |  | 143 | 51.2k |         ::Serialize(*this, obj); |  | 144 | 51.2k |         return *this; |  | 145 | 51.2k |     } | 
Unexecuted instantiation: _ZN10HashWriterlsINSt3__14spanIcLm18446744073709551615EEEEERS_RKT__ZN10HashWriterlsI7uint256EERS_RKT_| Line | Count | Source |  | 142 | 637k |     { |  | 143 | 637k |         ::Serialize(*this, obj); |  | 144 | 637k |         return *this; |  | 145 | 637k |     } | 
_ZN10HashWriterlsINSt3__14spanIKhLm32EEEEERS_RKT_| Line | Count | Source |  | 142 | 50.3M |     { |  | 143 | 50.3M |         ::Serialize(*this, obj); |  | 144 | 50.3M |         return *this; |  | 145 | 50.3M |     } | 
Unexecuted instantiation: _ZN10HashWriterlsIyEERS_RKT_Unexecuted instantiation: _ZN10HashWriterlsIhEERS_RKT_Unexecuted instantiation: _ZN10HashWriterlsIiEERS_RKT_Unexecuted instantiation: _ZN10HashWriterlsINSt3__16vectorIbNS1_9allocatorIbEEEEEERS_RKT__ZN10HashWriterlsI9COutPointEERS_RKT_| Line | Count | Source |  | 142 | 504k |     { |  | 143 | 504k |         ::Serialize(*this, obj); |  | 144 | 504k |         return *this; |  | 145 | 504k |     } | 
_ZN10HashWriterlsI6CTxOutEERS_RKT_| Line | Count | Source |  | 142 | 504k |     { |  | 143 | 504k |         ::Serialize(*this, obj); |  | 144 | 504k |         return *this; |  | 145 | 504k |     } | 
_ZN10HashWriterlsI10CBlockUndoEERS_RKT_| Line | Count | Source |  | 142 | 21.7k |     { |  | 143 | 21.7k |         ::Serialize(*this, obj); |  | 144 | 21.7k |         return *this; |  | 145 | 21.7k |     } | 
_ZN10HashWriterlsI7WrapperI15VarIntFormatterIL10VarIntMode0EERyEEERS_RKT_| Line | Count | Source |  | 142 | 104k |     { |  | 143 | 104k |         ::Serialize(*this, obj); |  | 144 | 104k |         return *this; |  | 145 | 104k |     } | 
Unexecuted instantiation: _ZN10HashWriterlsINSt3__14spanIhLm18446744073709551615EEEEERS_RKT__ZN10HashWriterlsI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS_RKT_| Line | Count | Source |  | 142 | 104k |     { |  | 143 | 104k |         ::Serialize(*this, obj); |  | 144 | 104k |         return *this; |  | 145 | 104k |     } | 
_ZN10HashWriterlsINSt3__14spanIKhLm18446744073709551615EEEEERS_RKT_| Line | Count | Source |  | 142 | 104k |     { |  | 143 | 104k |         ::Serialize(*this, obj); |  | 144 | 104k |         return *this; |  | 145 | 104k |     } | 
Unexecuted instantiation: _ZN10HashWriterlsI22transaction_identifierILb1EEEERS_RKT__ZN10HashWriterlsI12CBlockHeaderEERS_RKT_| Line | Count | Source |  | 142 | 24.5M |     { |  | 143 | 24.5M |         ::Serialize(*this, obj); |  | 144 | 24.5M |         return *this; |  | 145 | 24.5M |     } | 
Unexecuted instantiation: _ZN10HashWriterlsI13ParamsWrapperI20TransactionSerParamsK19CMutableTransactionEEERS_RKT__ZN10HashWriterlsI13ParamsWrapperI20TransactionSerParamsK12CTransactionEEERS_RKT_| Line | Count | Source |  | 142 | 30.8M |     { |  | 143 | 30.8M |         ::Serialize(*this, obj); |  | 144 | 30.8M |         return *this; |  | 145 | 30.8M |     } | 
Unexecuted instantiation: _ZN10HashWriterlsIxEERS_RKT_Unexecuted instantiation: _ZN10HashWriterlsI7CScriptEERS_RKT_Unexecuted instantiation: interpreter.cpp:_ZN10HashWriterlsIN12_GLOBAL__N_131CTransactionSignatureSerializerI12CTransactionEEEERS_RKT_Unexecuted instantiation: interpreter.cpp:_ZN10HashWriterlsIN12_GLOBAL__N_131CTransactionSignatureSerializerI19CMutableTransactionEEEERS_RKT_Unexecuted instantiation: _ZN10HashWriterlsI17CompactSizeWriterEERS_RKT_Unexecuted instantiation: _ZN10HashWriterlsIA384_hEERS_RKT_ | 
| 146 |  | }; | 
| 147 |  |  | 
| 148 |  | /** Reads data from an underlying stream, while hashing the read data. */ | 
| 149 |  | template <typename Source> | 
| 150 |  | class HashVerifier : public HashWriter | 
| 151 |  | { | 
| 152 |  | private: | 
| 153 |  |     Source& m_source; | 
| 154 |  |  | 
| 155 |  | public: | 
| 156 | 615k |     explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}Unexecuted instantiation: _ZN12HashVerifierI10DataStreamEC2ERS0_Unexecuted instantiation: _ZN12HashVerifierI8AutoFileEC2ERS0__ZN12HashVerifierI14BufferedReaderI8AutoFileEEC2ERS2_| Line | Count | Source |  | 156 | 615k |     explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {} | 
 | 
| 157 |  |  | 
| 158 |  |     void read(std::span<std::byte> dst) | 
| 159 | 615k |     { | 
| 160 | 615k |         m_source.read(dst); | 
| 161 | 615k |         this->write(dst); | 
| 162 | 615k |     } Unexecuted instantiation: _ZN12HashVerifierI10DataStreamE4readENSt3__14spanISt4byteLm18446744073709551615EEEUnexecuted instantiation: _ZN12HashVerifierI8AutoFileE4readENSt3__14spanISt4byteLm18446744073709551615EEE_ZN12HashVerifierI14BufferedReaderI8AutoFileEE4readENSt3__14spanISt4byteLm18446744073709551615EEE| Line | Count | Source |  | 159 | 615k |     { |  | 160 | 615k |         m_source.read(dst); |  | 161 | 615k |         this->write(dst); |  | 162 | 615k |     } | 
 | 
| 163 |  |  | 
| 164 |  |     void ignore(size_t num_bytes) | 
| 165 | 0 |     { | 
| 166 | 0 |         std::byte data[1024]; | 
| 167 | 0 |         while (num_bytes > 0) { | 
| 168 | 0 |             size_t now = std::min<size_t>(num_bytes, 1024); | 
| 169 | 0 |             read({data, now}); | 
| 170 | 0 |             num_bytes -= now; | 
| 171 | 0 |         } | 
| 172 | 0 |     } Unexecuted instantiation: _ZN12HashVerifierI8AutoFileE6ignoreEmUnexecuted instantiation: _ZN12HashVerifierI10DataStreamE6ignoreEmUnexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEE6ignoreEm | 
| 173 |  |  | 
| 174 |  |     template <typename T> | 
| 175 |  |     HashVerifier<Source>& operator>>(T&& obj) | 
| 176 | 615k |     { | 
| 177 | 615k |         ::Unserialize(*this, obj); | 
| 178 | 615k |         return *this; | 
| 179 | 615k |     } Unexecuted instantiation: _ZN12HashVerifierI10DataStreamErsIRNSt3__15arrayIhLm4EEEEERS1_OT_Unexecuted instantiation: _ZN12HashVerifierI10DataStreamErsIR7AddrManEERS1_OT_Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsIRNSt3__15arrayIhLm4EEEEERS1_OT_Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsIR7AddrManEERS1_OT_Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsIR13ParamsWrapperIN8CAddress9SerParamsENSt3__16vectorIS4_NS6_9allocatorIS4_EEEEEEERS1_OT_Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsI7WrapperI19CustomUintFormatterILi1ELb0EERN11AddrManImpl6FormatEEEERS1_OT_Unexecuted instantiation: _ZN12HashVerifierI10DataStreamErsI7WrapperI19CustomUintFormatterILi1ELb0EERN11AddrManImpl6FormatEEEERS1_OT__ZN12HashVerifierI14BufferedReaderI8AutoFileEErsIR10CBlockUndoEERS3_OT_| Line | Count | Source |  | 176 | 615k |     { |  | 177 | 615k |         ::Unserialize(*this, obj); |  | 178 | 615k |         return *this; |  | 179 | 615k |     } | 
Unexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEErsI7WrapperI15VarIntFormatterIL10VarIntMode0EERyEEERS3_OT_Unexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEErsI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS3_OT_Unexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEErsINSt3__14spanIhLm18446744073709551615EEEEERS3_OT_ | 
| 180 |  | }; | 
| 181 |  |  | 
| 182 |  | /** Writes data to an underlying source stream, while hashing the written data. */ | 
| 183 |  | template <typename Source> | 
| 184 |  | class HashedSourceWriter : public HashWriter | 
| 185 |  | { | 
| 186 |  | private: | 
| 187 |  |     Source& m_source; | 
| 188 |  |  | 
| 189 |  | public: | 
| 190 | 0 |     explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {} | 
| 191 |  |  | 
| 192 |  |     void write(std::span<const std::byte> src) | 
| 193 | 0 |     { | 
| 194 | 0 |         m_source.write(src); | 
| 195 | 0 |         HashWriter::write(src); | 
| 196 | 0 |     } | 
| 197 |  |  | 
| 198 |  |     template <typename T> | 
| 199 |  |     HashedSourceWriter& operator<<(const T& obj) | 
| 200 | 0 |     { | 
| 201 | 0 |         ::Serialize(*this, obj); | 
| 202 | 0 |         return *this; | 
| 203 | 0 |     } Unexecuted instantiation: _ZN18HashedSourceWriterI8AutoFileElsINSt3__15arrayIhLm4EEEEERS1_RKT_Unexecuted instantiation: _ZN18HashedSourceWriterI8AutoFileElsI7AddrManEERS1_RKT_Unexecuted instantiation: _ZN18HashedSourceWriterI8AutoFileElsI13ParamsWrapperIN8CAddress9SerParamsEKNSt3__16vectorIS4_NS6_9allocatorIS4_EEEEEEERS1_RKT_ | 
| 204 |  | }; | 
| 205 |  |  | 
| 206 |  | /** Single-SHA256 a 32-byte input (represented as uint256). */ | 
| 207 |  | [[nodiscard]] uint256 SHA256Uint256(const uint256& input); | 
| 208 |  |  | 
| 209 |  | unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash); | 
| 210 |  |  | 
| 211 |  | void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]); | 
| 212 |  |  | 
| 213 |  | /** Return a HashWriter primed for tagged hashes (as specified in BIP 340). | 
| 214 |  |  * | 
| 215 |  |  * The returned object will have SHA256(tag) written to it twice (= 64 bytes). | 
| 216 |  |  * A tagged hash can be computed by feeding the message into this object, and | 
| 217 |  |  * then calling HashWriter::GetSHA256(). | 
| 218 |  |  */ | 
| 219 |  | HashWriter TaggedHash(const std::string& tag); | 
| 220 |  |  | 
| 221 |  | /** Compute the 160-bit RIPEMD-160 hash of an array. */ | 
| 222 |  | inline uint160 RIPEMD160(std::span<const unsigned char> data) | 
| 223 | 0 | { | 
| 224 | 0 |     uint160 result; | 
| 225 | 0 |     CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin()); | 
| 226 | 0 |     return result; | 
| 227 | 0 | } | 
| 228 |  |  | 
| 229 |  | #endif // BITCOIN_HASH_H |