LCOV - code coverage report
Current view: top level - src - hash.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 48 86 55.8 %
Date: 2023-11-10 23:46:46 Functions: 38 83 45.8 %
Branches: 2 8 25.0 %

           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                 :            : #include <version.h>
      18                 :            : 
      19                 :            : #include <string>
      20                 :            : #include <vector>
      21                 :            : 
      22                 :            : typedef uint256 ChainCode;
      23                 :            : 
      24                 :            : /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
      25                 :          0 : class CHash256 {
      26                 :            : private:
      27                 :            :     CSHA256 sha;
      28                 :            : public:
      29                 :            :     static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
      30                 :            : 
      31                 :      34012 :     void Finalize(Span<unsigned char> output) {
      32         [ +  - ]:      34012 :         assert(output.size() == OUTPUT_SIZE);
      33                 :            :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      34                 :      34012 :         sha.Finalize(buf);
      35                 :      34012 :         sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      36                 :      34012 :     }
      37                 :            : 
      38                 :      34613 :     CHash256& Write(Span<const unsigned char> input) {
      39                 :      34613 :         sha.Write(input.data(), input.size());
      40                 :      34613 :         return *this;
      41                 :            :     }
      42                 :            : 
      43                 :          0 :     CHash256& Reset() {
      44                 :          0 :         sha.Reset();
      45                 :          0 :         return *this;
      46                 :            :     }
      47                 :            : };
      48                 :            : 
      49                 :            : /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
      50                 :          0 : class CHash160 {
      51                 :            : private:
      52                 :            :     CSHA256 sha;
      53                 :            : public:
      54                 :            :     static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
      55                 :            : 
      56                 :     569765 :     void Finalize(Span<unsigned char> output) {
      57         [ +  - ]:     569765 :         assert(output.size() == OUTPUT_SIZE);
      58                 :            :         unsigned char buf[CSHA256::OUTPUT_SIZE];
      59                 :     569765 :         sha.Finalize(buf);
      60                 :     569765 :         CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
      61                 :     569765 :     }
      62                 :            : 
      63                 :     569765 :     CHash160& Write(Span<const unsigned char> input) {
      64                 :     569765 :         sha.Write(input.data(), input.size());
      65                 :     569765 :         return *this;
      66                 :            :     }
      67                 :            : 
      68                 :          0 :     CHash160& Reset() {
      69                 :          0 :         sha.Reset();
      70                 :          0 :         return *this;
      71                 :            :     }
      72                 :            : };
      73                 :            : 
      74                 :            : /** Compute the 256-bit hash of an object. */
      75                 :            : template<typename T>
      76                 :      33411 : inline uint256 Hash(const T& in1)
      77                 :            : {
      78                 :      33411 :     uint256 result;
      79                 :      33411 :     CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
      80                 :      33411 :     return result;
      81                 :            : }
      82                 :            : 
      83                 :            : /** Compute the 256-bit hash of the concatenation of two objects. */
      84                 :            : template<typename T1, typename T2>
      85                 :          1 : inline uint256 Hash(const T1& in1, const T2& in2) {
      86                 :          1 :     uint256 result;
      87                 :          1 :     CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
      88                 :          1 :     return result;
      89                 :            : }
      90                 :            : 
      91                 :            : /** Compute the 160-bit hash an object. */
      92                 :            : template<typename T1>
      93                 :     569765 : inline uint160 Hash160(const T1& in1)
      94                 :            : {
      95                 :     569765 :     uint160 result;
      96                 :     569765 :     CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
      97                 :     569765 :     return result;
      98                 :            : }
      99                 :            : 
     100                 :            : /** A writer stream (for serialization) that computes a 256-bit hash. */
     101                 :          0 : class HashWriter
     102                 :            : {
     103                 :            : private:
     104                 :            :     CSHA256 ctx;
     105                 :            : 
     106                 :            : public:
     107                 :    3798137 :     void write(Span<const std::byte> src)
     108                 :            :     {
     109                 :    3798137 :         ctx.Write(UCharCast(src.data()), src.size());
     110                 :    3798137 :     }
     111                 :            : 
     112                 :            :     /** Compute the double-SHA256 hash of all data written to this object.
     113                 :            :      *
     114                 :            :      * Invalidates this object.
     115                 :            :      */
     116                 :      34651 :     uint256 GetHash() {
     117                 :      34651 :         uint256 result;
     118                 :      34651 :         ctx.Finalize(result.begin());
     119                 :      34651 :         ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
     120                 :      34651 :         return result;
     121                 :            :     }
     122                 :            : 
     123                 :            :     /** Compute the SHA256 hash of all data written to this object.
     124                 :            :      *
     125                 :            :      * Invalidates this object.
     126                 :            :      */
     127                 :      29805 :     uint256 GetSHA256() {
     128                 :      29805 :         uint256 result;
     129                 :      29805 :         ctx.Finalize(result.begin());
     130                 :      29805 :         return result;
     131                 :            :     }
     132                 :            : 
     133                 :            :     /**
     134                 :            :      * Returns the first 64 bits from the resulting hash.
     135                 :            :      */
     136                 :          0 :     inline uint64_t GetCheapHash() {
     137                 :          0 :         uint256 result = GetHash();
     138                 :          0 :         return ReadLE64(result.begin());
     139                 :            :     }
     140                 :            : 
     141                 :            :     template <typename T>
     142                 :     957380 :     HashWriter& operator<<(const T& obj)
     143                 :            :     {
     144                 :     957380 :         ::Serialize(*this, obj);
     145                 :     957380 :         return *this;
     146                 :            :     }
     147                 :            : };
     148                 :            : 
     149                 :            : class CHashWriter : public HashWriter
     150                 :            : {
     151                 :            : private:
     152                 :            :     const int nVersion;
     153                 :            : 
     154                 :            : public:
     155                 :      16416 :     CHashWriter(int nVersionIn) : nVersion{nVersionIn} {}
     156                 :            : 
     157                 :      14616 :     int GetVersion() const { return nVersion; }
     158                 :            : 
     159                 :            :     template<typename T>
     160                 :     235824 :     CHashWriter& operator<<(const T& obj) {
     161                 :     235824 :         ::Serialize(*this, obj);
     162                 :     235824 :         return (*this);
     163                 :            :     }
     164                 :            : };
     165                 :            : 
     166                 :            : /** Reads data from an underlying stream, while hashing the read data. */
     167                 :            : template <typename Source>
     168                 :            : class HashVerifier : public HashWriter
     169                 :            : {
     170                 :            : private:
     171                 :            :     Source& m_source;
     172                 :            : 
     173                 :            : public:
     174                 :          0 :     explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
     175                 :            : 
     176                 :          0 :     void read(Span<std::byte> dst)
     177                 :            :     {
     178                 :          0 :         m_source.read(dst);
     179                 :          0 :         this->write(dst);
     180                 :          0 :     }
     181                 :            : 
     182                 :          0 :     void ignore(size_t num_bytes)
     183                 :            :     {
     184                 :            :         std::byte data[1024];
     185 [ #  # ][ #  # ]:          0 :         while (num_bytes > 0) {
     186                 :          0 :             size_t now = std::min<size_t>(num_bytes, 1024);
     187                 :          0 :             read({data, now});
     188                 :          0 :             num_bytes -= now;
     189                 :            :         }
     190                 :          0 :     }
     191                 :            : 
     192                 :            :     template <typename T>
     193                 :          0 :     HashVerifier<Source>& operator>>(T&& obj)
     194                 :            :     {
     195                 :          0 :         ::Unserialize(*this, obj);
     196                 :          0 :         return *this;
     197                 :            :     }
     198                 :            : };
     199                 :            : 
     200                 :            : /** Writes data to an underlying source stream, while hashing the written data. */
     201                 :            : template <typename Source>
     202                 :            : class HashedSourceWriter : public HashWriter
     203                 :            : {
     204                 :            : private:
     205                 :            :     Source& m_source;
     206                 :            : 
     207                 :            : public:
     208                 :          0 :     explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
     209                 :            : 
     210                 :          0 :     void write(Span<const std::byte> src)
     211                 :            :     {
     212                 :          0 :         m_source.write(src);
     213                 :          0 :         HashWriter::write(src);
     214                 :          0 :     }
     215                 :            : 
     216                 :            :     template <typename T>
     217                 :          0 :     HashedSourceWriter& operator<<(const T& obj)
     218                 :            :     {
     219                 :          0 :         ::Serialize(*this, obj);
     220                 :          0 :         return *this;
     221                 :            :     }
     222                 :            : };
     223                 :            : 
     224                 :            : /** Single-SHA256 a 32-byte input (represented as uint256). */
     225                 :            : [[nodiscard]] uint256 SHA256Uint256(const uint256& input);
     226                 :            : 
     227                 :            : unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
     228                 :            : 
     229                 :            : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
     230                 :            : 
     231                 :            : /** Return a HashWriter primed for tagged hashes (as specified in BIP 340).
     232                 :            :  *
     233                 :            :  * The returned object will have SHA256(tag) written to it twice (= 64 bytes).
     234                 :            :  * A tagged hash can be computed by feeding the message into this object, and
     235                 :            :  * then calling HashWriter::GetSHA256().
     236                 :            :  */
     237                 :            : HashWriter TaggedHash(const std::string& tag);
     238                 :            : 
     239                 :            : /** Compute the 160-bit RIPEMD-160 hash of an array. */
     240                 :          0 : inline uint160 RIPEMD160(Span<const unsigned char> data)
     241                 :            : {
     242                 :          0 :     uint160 result;
     243                 :          0 :     CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
     244                 :          0 :     return result;
     245                 :            : }
     246                 :            : 
     247                 :            : #endif // BITCOIN_HASH_H

Generated by: LCOV version 1.14