LCOV - code coverage report
Current view: top level - src - hash.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 90.2 % 82 74
Test Date: 2024-08-28 04:44:32 Functions: 80.0 % 25 20
Branches: 35.6 % 292 104

             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         [ +  - ]:      882631 : class CHash256 {
      25                 :             : private:
      26                 :             :     CSHA256 sha;
      27                 :             : public:
      28                 :             :     static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
      29                 :             : 
      30                 :      882518 :     void Finalize(Span<unsigned char> output) {
      31         [ -  + ]:      882518 :         assert(output.size() == OUTPUT_SIZE);
      32                 :      882518 :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      33                 :      882518 :         sha.Finalize(buf);
      34                 :      882518 :         sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      35                 :      882518 :     }
      36                 :             : 
      37                 :     1776812 :     CHash256& Write(Span<const unsigned char> input) {
      38   [ +  -  +  - ]:     1755438 :         sha.Write(input.data(), input.size());
                 [ +  - ]
      39   [ +  +  +  + ]:     1755474 :         return *this;
                 [ +  + ]
      40                 :             :     }
      41                 :             : 
      42                 :         117 :     CHash256& Reset() {
      43                 :         117 :         sha.Reset();
      44                 :         117 :         return *this;
      45                 :             :     }
      46                 :             : };
      47                 :             : 
      48                 :             : /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
      49         [ +  - ]:     1540285 : class CHash160 {
      50                 :             : private:
      51                 :             :     CSHA256 sha;
      52                 :             : public:
      53                 :             :     static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
      54                 :             : 
      55                 :     1540881 :     void Finalize(Span<unsigned char> output) {
      56         [ -  + ]:     1540881 :         assert(output.size() == OUTPUT_SIZE);
      57                 :     1540881 :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      58                 :     1540881 :         sha.Finalize(buf);
      59                 :     1540881 :         CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      60                 :     1540881 :     }
      61                 :             : 
      62                 :     1540881 :     CHash160& Write(Span<const unsigned char> input) {
      63         [ +  - ]:     1540285 :         sha.Write(input.data(), input.size());
      64         [ +  - ]:     1540285 :         return *this;
      65                 :             :     }
      66                 :             : 
      67                 :             :     CHash160& Reset() {
      68                 :           0 :         sha.Reset();
      69                 :           0 :         return *this;
      70                 :             :     }
      71                 :             : };
      72                 :             : 
      73                 :             : /** Compute the 256-bit hash of an object. */
      74                 :             : template<typename T>
      75                 :       37626 : inline uint256 Hash(const T& in1)
      76                 :             : {
      77                 :       37626 :     uint256 result;
      78                 :       37626 :     CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
      79                 :       37626 :     return result;
      80                 :             : }
      81                 :             : 
      82                 :             : /** Compute the 256-bit hash of the concatenation of two objects. */
      83                 :             : template<typename T1, typename T2>
      84                 :      822617 : inline uint256 Hash(const T1& in1, const T2& in2) {
      85                 :      822617 :     uint256 result;
      86                 :      822617 :     CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
      87                 :      822617 :     return result;
      88                 :             : }
      89                 :             : 
      90                 :             : /** Compute the 160-bit hash an object. */
      91                 :             : template<typename T1>
      92                 :     1482222 : inline uint160 Hash160(const T1& in1)
      93                 :             : {
      94                 :     1482222 :     uint160 result;
      95                 :     1482222 :     CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
      96                 :     1482222 :     return result;
      97                 :             : }
      98                 :             : 
      99                 :             : /** A writer stream (for serialization) that computes a 256-bit hash. */
     100   [ +  -  +  - ]:     1343816 : class HashWriter
           [ +  -  #  # ]
           [ +  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  -  
             +  -  +  - ]
     101                 :             : {
     102                 :             : private:
     103                 :             :     CSHA256 ctx;
     104                 :             : 
     105                 :             : public:
     106                 :   101617250 :     void write(Span<const std::byte> src)
     107                 :             :     {
     108   [ +  -  #  #  :   101617248 :         ctx.Write(UCharCast(src.data()), src.size());
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     109                 :    13863681 :     }
     110                 :             : 
     111                 :             :     /** Compute the double-SHA256 hash of all data written to this object.
     112                 :             :      *
     113                 :             :      * Invalidates this object.
     114                 :             :      */
     115                 :     1329299 :     uint256 GetHash() {
     116                 :     1329299 :         uint256 result;
     117                 :     1329299 :         ctx.Finalize(result.begin());
     118                 :     1329299 :         ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
     119                 :     1329299 :         return result;
     120                 :             :     }
     121                 :             : 
     122                 :             :     /** Compute the SHA256 hash of all data written to this object.
     123                 :             :      *
     124                 :             :      * Invalidates this object.
     125                 :             :      */
     126                 :       92627 :     uint256 GetSHA256() {
     127                 :       92627 :         uint256 result;
     128   [ +  -  #  #  :       92627 :         ctx.Finalize(result.begin());
             #  #  #  # ]
           [ +  -  +  -  
             +  -  +  - ]
     129         [ +  - ]:       75362 :         return result;
           [ -  +  -  - ]
     130                 :             :     }
     131                 :             : 
     132                 :             :     /**
     133                 :             :      * Returns the first 64 bits from the resulting hash.
     134                 :             :      */
     135                 :       87189 :     inline uint64_t GetCheapHash() {
     136   [ +  -  +  -  :       87189 :         uint256 result = GetHash();
          +  -  +  -  +  
                      - ]
     137                 :       87189 :         return ReadLE64(result.begin());
     138                 :             :     }
     139                 :             : 
     140                 :             :     template <typename T>
     141                 :    21902391 :     HashWriter& operator<<(const T& obj)
     142                 :             :     {
     143   [ +  -  +  -  :    63324290 :         ::Serialize(*this, obj);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  -  
          -  -  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
           +  - ][ +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  +  +  +  
          +  +  -  +  -  
          -  -  -  -  -  
             -  -  -  - ]
     144                 :       66369 :         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         [ +  - ]:         903 :     explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
           [ +  -  +  - ]
     157                 :             : 
     158                 :        1989 :     void read(Span<std::byte> dst)
     159                 :             :     {
     160                 :        1989 :         m_source.read(dst);
     161                 :        1988 :         this->write(dst);
     162                 :        1988 :     }
     163                 :             : 
     164                 :           0 :     void ignore(size_t num_bytes)
     165                 :             :     {
     166                 :             :         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                 :             :         }
     172                 :           0 :     }
     173                 :             : 
     174                 :             :     template <typename T>
     175                 :         907 :     HashVerifier<Source>& operator>>(T&& obj)
     176                 :             :     {
     177   [ +  -  +  - ]:         907 :         ::Unserialize(*this, obj);
                 [ +  - ]
     178                 :         903 :         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                 :           1 :     explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
     191                 :             : 
     192                 :           2 :     void write(Span<const std::byte> src)
     193                 :             :     {
     194                 :           2 :         m_source.write(src);
     195                 :           2 :         HashWriter::write(src);
     196                 :           2 :     }
     197                 :             : 
     198                 :             :     template <typename T>
     199                 :           1 :     HashedSourceWriter& operator<<(const T& obj)
     200                 :             :     {
     201   [ #  #  #  #  :           1 :         ::Serialize(*this, obj);
             #  #  #  # ]
                 [ +  - ]
     202                 :           1 :         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                 :         433 : inline uint160 RIPEMD160(Span<const unsigned char> data)
     223                 :             : {
     224                 :         433 :     uint160 result;
     225                 :         433 :     CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
     226                 :         433 :     return result;
     227                 :             : }
     228                 :             : 
     229                 :             : #endif // BITCOIN_HASH_H
        

Generated by: LCOV version 2.0-1