Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2020 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 <uint256.h>
# 16 : : #include <version.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 : 2108877 : void Finalize(Span<unsigned char> output) {
# 31 : 2108877 : assert(output.size() == OUTPUT_SIZE);
# 32 : 0 : unsigned char buf[CSHA256::OUTPUT_SIZE];
# 33 : 2108877 : sha.Finalize(buf);
# 34 : 2108877 : sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
# 35 : 2108877 : }
# 36 : :
# 37 : 3769226 : CHash256& Write(Span<const unsigned char> input) {
# 38 : 3769226 : sha.Write(input.data(), input.size());
# 39 : 3769226 : return *this;
# 40 : 3769226 : }
# 41 : :
# 42 : 112871 : CHash256& Reset() {
# 43 : 112871 : sha.Reset();
# 44 : 112871 : return *this;
# 45 : 112871 : }
# 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 : 8752001 : void Finalize(Span<unsigned char> output) {
# 56 : 8752001 : assert(output.size() == OUTPUT_SIZE);
# 57 : 0 : unsigned char buf[CSHA256::OUTPUT_SIZE];
# 58 : 8752001 : sha.Finalize(buf);
# 59 : 8752001 : CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
# 60 : 8752001 : }
# 61 : :
# 62 : 8752005 : CHash160& Write(Span<const unsigned char> input) {
# 63 : 8752005 : sha.Write(input.data(), input.size());
# 64 : 8752005 : return *this;
# 65 : 8752005 : }
# 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 : 397741 : {
# 77 : 397741 : uint256 result;
# 78 : 397741 : CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
# 79 : 397741 : return result;
# 80 : 397741 : }
# 81 : :
# 82 : : /** Compute the 256-bit hash of the concatenation of two objects. */
# 83 : : template<typename T1, typename T2>
# 84 : 683909 : inline uint256 Hash(const T1& in1, const T2& in2) {
# 85 : 683909 : uint256 result;
# 86 : 683909 : CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
# 87 : 683909 : return result;
# 88 : 683909 : }
# 89 : :
# 90 : : /** Compute the 160-bit hash an object. */
# 91 : : template<typename T1>
# 92 : : inline uint160 Hash160(const T1& in1)
# 93 : 7416888 : {
# 94 : 7416888 : uint160 result;
# 95 : 7416888 : CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
# 96 : 7416888 : return result;
# 97 : 7416888 : }
# 98 : :
# 99 : : /** A writer stream (for serialization) that computes a 256-bit hash. */
# 100 : : class CHashWriter
# 101 : : {
# 102 : : private:
# 103 : : CSHA256 ctx;
# 104 : :
# 105 : : const int nType;
# 106 : : const int nVersion;
# 107 : : public:
# 108 : :
# 109 : 16042589 : CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
# 110 : :
# 111 : 1502 : int GetType() const { return nType; }
# 112 : 1922582 : int GetVersion() const { return nVersion; }
# 113 : :
# 114 : : void write(Span<const std::byte> src)
# 115 : 321970552 : {
# 116 : 321970552 : ctx.Write(UCharCast(src.data()), src.size());
# 117 : 321970552 : }
# 118 : :
# 119 : : /** Compute the double-SHA256 hash of all data written to this object.
# 120 : : *
# 121 : : * Invalidates this object.
# 122 : : */
# 123 : 15816858 : uint256 GetHash() {
# 124 : 15816858 : uint256 result;
# 125 : 15816858 : ctx.Finalize(result.begin());
# 126 : 15816858 : ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
# 127 : 15816858 : return result;
# 128 : 15816858 : }
# 129 : :
# 130 : : /** Compute the SHA256 hash of all data written to this object.
# 131 : : *
# 132 : : * Invalidates this object.
# 133 : : */
# 134 : 565416 : uint256 GetSHA256() {
# 135 : 565416 : uint256 result;
# 136 : 565416 : ctx.Finalize(result.begin());
# 137 : 565416 : return result;
# 138 : 565416 : }
# 139 : :
# 140 : : /**
# 141 : : * Returns the first 64 bits from the resulting hash.
# 142 : : */
# 143 : 208398 : inline uint64_t GetCheapHash() {
# 144 : 208398 : uint256 result = GetHash();
# 145 : 208398 : return ReadLE64(result.begin());
# 146 : 208398 : }
# 147 : :
# 148 : : template<typename T>
# 149 : 110709190 : CHashWriter& operator<<(const T& obj) {
# 150 : : // Serialize to this stream
# 151 : 110709190 : ::Serialize(*this, obj);
# 152 : 110709190 : return (*this);
# 153 : 110709190 : }
# 154 : : };
# 155 : :
# 156 : : /** Reads data from an underlying stream, while hashing the read data. */
# 157 : : template<typename Source>
# 158 : : class CHashVerifier : public CHashWriter
# 159 : : {
# 160 : : private:
# 161 : : Source* source;
# 162 : :
# 163 : : public:
# 164 : 18812 : explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
# 165 : :
# 166 : : void read(Span<std::byte> dst)
# 167 : 555494 : {
# 168 : 555494 : source->read(dst);
# 169 : 555494 : this->write(dst);
# 170 : 555494 : }
# 171 : :
# 172 : : void ignore(size_t nSize)
# 173 : 0 : {
# 174 : 0 : std::byte data[1024];
# 175 [ # # ][ # # ]: 0 : while (nSize > 0) {
# 176 : 0 : size_t now = std::min<size_t>(nSize, 1024);
# 177 : 0 : read({data, now});
# 178 : 0 : nSize -= now;
# 179 : 0 : }
# 180 : 0 : }
# 181 : :
# 182 : : template<typename T>
# 183 : : CHashVerifier<Source>& operator>>(T&& obj)
# 184 : 78038 : {
# 185 : : // Unserialize from this stream
# 186 : 78038 : ::Unserialize(*this, obj);
# 187 : 78038 : return (*this);
# 188 : 78038 : }
# 189 : : };
# 190 : :
# 191 : : /** Compute the 256-bit hash of an object's serialization. */
# 192 : : template<typename T>
# 193 : : uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
# 194 : 14919742 : {
# 195 : 14919742 : CHashWriter ss(nType, nVersion);
# 196 : 14919742 : ss << obj;
# 197 : 14919742 : return ss.GetHash();
# 198 : 14919742 : }
# 199 : :
# 200 : : /** Single-SHA256 a 32-byte input (represented as uint256). */
# 201 : : [[nodiscard]] uint256 SHA256Uint256(const uint256& input);
# 202 : :
# 203 : : unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
# 204 : :
# 205 : : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
# 206 : :
# 207 : : /** Return a CHashWriter primed for tagged hashes (as specified in BIP 340).
# 208 : : *
# 209 : : * The returned object will have SHA256(tag) written to it twice (= 64 bytes).
# 210 : : * A tagged hash can be computed by feeding the message into this object, and
# 211 : : * then calling CHashWriter::GetSHA256().
# 212 : : */
# 213 : : CHashWriter TaggedHash(const std::string& tag);
# 214 : :
# 215 : : #endif // BITCOIN_HASH_H
|