LCOV - code coverage report
Current view: top level - src - hash.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 90.4 % 83 75
Test Date: 2024-09-01 05:20:30 Functions: 75.3 % 73 55
Branches: 33.3 % 12 4

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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                 :     5296097 :     void Finalize(Span<unsigned char> output) {
      31         [ +  - ]:     5296097 :         assert(output.size() == OUTPUT_SIZE);
      32                 :     5296097 :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      33                 :     5296097 :         sha.Finalize(buf);
      34                 :     5296097 :         sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      35                 :     5296097 :     }
      36                 :             : 
      37                 :     6159218 :     CHash256& Write(Span<const unsigned char> input) {
      38                 :     6159218 :         sha.Write(input.data(), input.size());
      39                 :     6159218 :         return *this;
      40                 :             :     }
      41                 :             : 
      42                 :      355643 :     CHash256& Reset() {
      43                 :      355643 :         sha.Reset();
      44                 :      355643 :         return *this;
      45                 :             :     }
      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                 :    21271102 :     void Finalize(Span<unsigned char> output) {
      56         [ +  - ]:    21271102 :         assert(output.size() == OUTPUT_SIZE);
      57                 :    21271102 :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      58                 :    21271102 :         sha.Finalize(buf);
      59                 :    21271102 :         CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      60                 :    21271102 :     }
      61                 :             : 
      62                 :    21273956 :     CHash160& Write(Span<const unsigned char> input) {
      63                 :    21273956 :         sha.Write(input.data(), input.size());
      64                 :    21273956 :         return *this;
      65                 :             :     }
      66                 :             : 
      67                 :         727 :     CHash160& Reset() {
      68                 :         727 :         sha.Reset();
      69                 :         727 :         return *this;
      70                 :             :     }
      71                 :             : };
      72                 :             : 
      73                 :             : /** Compute the 256-bit hash of an object. */
      74                 :             : template<typename T>
      75                 :     3992557 : inline uint256 Hash(const T& in1)
      76                 :             : {
      77                 :     3992557 :     uint256 result;
      78                 :     3992557 :     CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
      79                 :     3992557 :     return result;
      80                 :             : }
      81                 :             : 
      82                 :             : /** Compute the 256-bit hash of the concatenation of two objects. */
      83                 :             : template<typename T1, typename T2>
      84                 :      394222 : inline uint256 Hash(const T1& in1, const T2& in2) {
      85                 :      394222 :     uint256 result;
      86                 :      394222 :     CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
      87                 :      394222 :     return result;
      88                 :             : }
      89                 :             : 
      90                 :             : /** Compute the 160-bit hash an object. */
      91                 :             : template<typename T1>
      92                 :    21104104 : inline uint160 Hash160(const T1& in1)
      93                 :             : {
      94                 :    21104104 :     uint160 result;
      95                 :    21104104 :     CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
      96                 :    21104104 :     return result;
      97                 :             : }
      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                 :  1241528269 :     void write(Span<const std::byte> src)
     107                 :             :     {
     108                 :  1241528269 :         ctx.Write(UCharCast(src.data()), src.size());
     109                 :  1241528269 :     }
     110                 :             : 
     111                 :             :     /** Compute the double-SHA256 hash of all data written to this object.
     112                 :             :      *
     113                 :             :      * Invalidates this object.
     114                 :             :      */
     115                 :   168894232 :     uint256 GetHash() {
     116                 :   168894232 :         uint256 result;
     117                 :   168894232 :         ctx.Finalize(result.begin());
     118                 :   168894232 :         ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
     119                 :   168894232 :         return result;
     120                 :             :     }
     121                 :             : 
     122                 :             :     /** Compute the SHA256 hash of all data written to this object.
     123                 :             :      *
     124                 :             :      * Invalidates this object.
     125                 :             :      */
     126                 :     1737890 :     uint256 GetSHA256() {
     127                 :     1737890 :         uint256 result;
     128                 :     1737890 :         ctx.Finalize(result.begin());
     129                 :     1737890 :         return result;
     130                 :             :     }
     131                 :             : 
     132                 :             :     /**
     133                 :             :      * Returns the first 64 bits from the resulting hash.
     134                 :             :      */
     135                 :   153236843 :     inline uint64_t GetCheapHash() {
     136                 :   153236843 :         uint256 result = GetHash();
     137                 :   306473686 :         return ReadLE64(result.begin());
     138                 :   153236843 :     }
     139                 :             : 
     140                 :             :     template <typename T>
     141                 :   696825597 :     HashWriter& operator<<(const T& obj)
     142                 :             :     {
     143                 :   696825597 :         ::Serialize(*this, obj);
     144                 :   696825597 :         return *this;
     145                 :             :     }
     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                 :        2139 :     explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
     157                 :             : 
     158                 :     3283862 :     void read(Span<std::byte> dst)
     159                 :             :     {
     160                 :     3283862 :         m_source.read(dst);
     161                 :     3283862 :         this->write(dst);
     162                 :     3283862 :     }
     163                 :             : 
     164                 :      120542 :     void ignore(size_t num_bytes)
     165                 :             :     {
     166                 :      120542 :         std::byte data[1024];
     167   [ #  #  #  # ]:      127974 :         while (num_bytes > 0) {
           [ #  #  +  + ]
     168                 :        7432 :             size_t now = std::min<size_t>(num_bytes, 1024);
     169                 :        7432 :             read({data, now});
     170                 :        7432 :             num_bytes -= now;
     171                 :        7432 :         }
     172                 :      120542 :     }
     173                 :             : 
     174                 :             :     template <typename T>
     175                 :        6203 :     HashVerifier<Source>& operator>>(T&& obj)
     176                 :             :     {
     177                 :        6203 :         ::Unserialize(*this, obj);
     178                 :        6203 :         return *this;
     179                 :             :     }
     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                 :           0 :     void write(Span<const std::byte> src)
     193                 :             :     {
     194                 :           0 :         m_source.write(src);
     195                 :           0 :         HashWriter::write(src);
     196                 :           0 :     }
     197                 :             : 
     198                 :             :     template <typename T>
     199                 :           0 :     HashedSourceWriter& operator<<(const T& obj)
     200                 :             :     {
     201                 :           0 :         ::Serialize(*this, obj);
     202                 :           0 :         return *this;
     203                 :             :     }
     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, 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                 :       66939 : inline uint160 RIPEMD160(Span<const unsigned char> data)
     223                 :             : {
     224                 :       66939 :     uint160 result;
     225                 :       66939 :     CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
     226                 :       66939 :     return result;
     227                 :             : }
     228                 :             : 
     229                 :             : #endif // BITCOIN_HASH_H
        

Generated by: LCOV version 2.0-1