LCOV - code coverage report
Current view: top level - src - hash.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 75 81 92.6 %
Date: 2026-06-25 07:23:43 Functions: 197 206 95.6 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2020 The Bitcoin Core developers
       3             : // Copyright (c) 2014-2024 The Dash Core developers
       4             : // Distributed under the MIT software license, see the accompanying
       5             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       6             : 
       7             : #ifndef BITCOIN_HASH_H
       8             : #define BITCOIN_HASH_H
       9             : 
      10             : #include <attributes.h>
      11             : #include <crypto/common.h>
      12             : #include <crypto/ripemd160.h>
      13             : #include <crypto/sha256.h>
      14             : #include <prevector.h>
      15             : #include <serialize.h>
      16             : #include <uint256.h>
      17             : #include <version.h>
      18             : 
      19             : #include <string>
      20             : #include <vector>
      21             : 
      22             : typedef uint256 ChainCode;
      23             : 
      24             : /* ----------- Bitcoin Hash ------------------------------------------------- */
      25             : /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
      26             : class CHash256 {
      27             : private:
      28             :     CSHA256 sha;
      29             : public:
      30             :     static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
      31             : 
      32     4573153 :     void Finalize(Span<unsigned char> output) {
      33     4573153 :         assert(output.size() == OUTPUT_SIZE);
      34             :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      35     4573153 :         sha.Finalize(buf);
      36     4573153 :         sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      37     4573153 :     }
      38             : 
      39     5705495 :     CHash256& Write(Span<const unsigned char> input) {
      40     5705495 :         sha.Write(input.data(), input.size());
      41     5705495 :         return *this;
      42             :     }
      43             : 
      44      818031 :     CHash256& Reset() {
      45      818031 :         sha.Reset();
      46      818031 :         return *this;
      47             :     }
      48             : };
      49             : 
      50             : /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
      51             : class CHash160 {
      52             : private:
      53             :     CSHA256 sha;
      54             : public:
      55             :     static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
      56             : 
      57     5809951 :     void Finalize(Span<unsigned char> output) {
      58     5809951 :         assert(output.size() == OUTPUT_SIZE);
      59             :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      60     5809951 :         sha.Finalize(buf);
      61     5809951 :         CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      62     5809951 :     }
      63             : 
      64     5810027 :     CHash160& Write(Span<const unsigned char> input) {
      65     5810027 :         sha.Write(input.data(), input.size());
      66     5810027 :         return *this;
      67             :     }
      68             : 
      69             :     CHash160& Reset() {
      70             :         sha.Reset();
      71             :         return *this;
      72             :     }
      73             : };
      74             : 
      75             : /** Compute the 256-bit hash of an object. */
      76             : template<typename T>
      77     2689960 : inline uint256 Hash(const T& in1)
      78             : {
      79     2689960 :     uint256 result;
      80     2689960 :     CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
      81     2689960 :     return result;
      82             : }
      83             : 
      84             : /** Compute the 256-bit hash of the concatenation of two objects. */
      85             : template<typename T1, typename T2>
      86     1074367 : inline uint256 Hash(const T1& in1, const T2& in2) {
      87     1074367 :     uint256 result;
      88     1074367 :     CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
      89     1074367 :     return result;
      90             : }
      91             : 
      92             : /** Compute the 160-bit hash an object. */
      93             : template<typename T1>
      94     4793054 : inline uint160 Hash160(const T1& in1)
      95             : {
      96     4793054 :     uint160 result;
      97     4793054 :     CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
      98     4793054 :     return result;
      99             : }
     100             : 
     101             : /** A writer stream (for serialization) that computes a 256-bit hash. */
     102             : class HashWriter
     103             : {
     104             : private:
     105             :     CSHA256 ctx;
     106             : 
     107             : public:
     108   141152089 :     void write(Span<const std::byte> src)
     109             :     {
     110   141152089 :         ctx.Write(UCharCast(src.data()), src.size());
     111   141152089 :     }
     112             : 
     113             :     /** Compute the double-SHA256 hash of all data written to this object.
     114             :      *
     115             :      * Invalidates this object.
     116             :      */
     117     7586122 :     uint256 GetHash() {
     118     7586122 :         uint256 result;
     119     7586122 :         ctx.Finalize(result.begin());
     120     7586122 :         ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
     121     7586122 :         return result;
     122             :     }
     123             : 
     124             :     /** Compute the SHA256 hash of all data written to this object.
     125             :      *
     126             :      * Invalidates this object.
     127             :      */
     128        4523 :     uint256 GetSHA256() {
     129        4523 :         uint256 result;
     130        4523 :         ctx.Finalize(result.begin());
     131        4523 :         return result;
     132             :     }
     133             : 
     134             :     /**
     135             :      * Returns the first 64 bits from the resulting hash.
     136             :      */
     137      342429 :     inline uint64_t GetCheapHash() {
     138      342429 :         uint256 result = GetHash();
     139      342429 :         return ReadLE64(result.begin());
     140             :     }
     141             : 
     142             :     template <typename T>
     143     3230706 :     HashWriter& operator<<(const T& obj)
     144             :     {
     145     3230706 :         ::Serialize(*this, obj);
     146     3230706 :         return *this;
     147             :     }
     148             : };
     149             : 
     150             : class CHashWriter : public HashWriter
     151             : {
     152             : private:
     153             :     const int nType;
     154             :     const int nVersion;
     155             : 
     156             : public:
     157    12719025 :     CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
     158             : 
     159       98382 :     int GetType() const { return nType; }
     160      157308 :     int GetVersion() const { return nVersion; }
     161             : 
     162             :     template<typename T>
     163    24084319 :     CHashWriter& operator<<(const T& obj) {
     164             :         // Serialize to this stream
     165    24084319 :         ::Serialize(*this, obj);
     166    24084319 :         return (*this);
     167             :     }
     168             : };
     169             : 
     170             : /** Reads data from an underlying stream, while hashing the read data. */
     171             : template<typename Source>
     172             : class CHashVerifier : public CHashWriter
     173             : {
     174             : private:
     175             :     Source* source;
     176             : 
     177             : public:
     178      382388 :     explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
     179             : 
     180     3193534 :     void read(Span<std::byte> dst)
     181             :     {
     182     3193534 :         source->read(dst);
     183     3193534 :         this->write(dst);
     184     3193534 :     }
     185             : 
     186           0 :     void ignore(size_t nSize)
     187             :     {
     188             :         std::byte data[1024];
     189           0 :         while (nSize > 0) {
     190           0 :             size_t now = std::min<size_t>(nSize, 1024);
     191           0 :             read({data, now});
     192           0 :             nSize -= now;
     193             :         }
     194           0 :     }
     195             : 
     196             :     template<typename T>
     197      499671 :     CHashVerifier<Source>& operator>>(T&& obj)
     198             :     {
     199             :         // Unserialize from this stream
     200      499671 :         ::Unserialize(*this, obj);
     201      499671 :         return (*this);
     202             :     }
     203             : };
     204             : 
     205             : /** Writes data to an underlying source stream, while hashing the written data. */
     206             : template <typename Source>
     207             : class HashedSourceWriter : public CHashWriter
     208             : {
     209             : private:
     210             :     Source& m_source;
     211             : 
     212             : public:
     213       12924 :     explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : CHashWriter{source.GetType(), source.GetVersion()}, m_source{source} {}
     214             : 
     215     7253165 :     void write(Span<const std::byte> src)
     216             :     {
     217     7253165 :         m_source.write(src);
     218     7253165 :         CHashWriter::write(src);
     219     7253165 :     }
     220             : 
     221             :     template <typename T>
     222       12929 :     HashedSourceWriter& operator<<(const T& obj)
     223             :     {
     224       12929 :         ::Serialize(*this, obj);
     225       12929 :         return *this;
     226             :     }
     227             : };
     228             : 
     229             : /** Compute the 256-bit hash of an object's serialization. */
     230             : template<typename T>
     231     5625876 : uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
     232             : {
     233     5625876 :     CHashWriter ss(nType, nVersion);
     234     5625876 :     ss << obj;
     235     5625876 :     return ss.GetHash();
     236             : }
     237             : 
     238             : /** Single-SHA256 a 32-byte input (represented as uint256). */
     239             : [[nodiscard]] uint256 SHA256Uint256(const uint256& input);
     240             : 
     241             : unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
     242             : 
     243             : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
     244             : 
     245             : /** Return a HashWriter primed for tagged hashes (as specified in BIP 340).
     246             :  *
     247             :  * The returned object will have SHA256(tag) written to it twice (= 64 bytes).
     248             :  * A tagged hash can be computed by feeding the message into this object, and
     249             :  * then calling HashWriter::GetSHA256().
     250             :  */
     251             : HashWriter TaggedHash(const std::string& tag);
     252             : 
     253             : #endif // BITCOIN_HASH_H

Generated by: LCOV version 1.16