LCOV - code coverage report
Current view: top level - src - hash.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 78 90 86.7 %
Date: 2021-06-29 14:35:33 Functions: 78 85 91.8 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 4 0.0 %

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

Generated by: LCOV version 1.14