LCOV - code coverage report
Current view: top level - src - hash.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 51 51
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 4 4
Branches: 100.0 % 6 6

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2013-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <hash.h>
       6                 :             : #include <span.h>
       7                 :             : #include <crypto/common.h>
       8                 :             : #include <crypto/hmac_sha512.h>
       9                 :             : 
      10                 :             : #include <bit>
      11                 :             : #include <string>
      12                 :             : 
      13                 :       55864 : unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash)
      14                 :             : {
      15                 :             :     // The following is MurmurHash3 (x86_32), see https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
      16                 :       55864 :     uint32_t h1 = nHashSeed;
      17                 :       55864 :     const uint32_t c1 = 0xcc9e2d51;
      18                 :       55864 :     const uint32_t c2 = 0x1b873593;
      19                 :             : 
      20                 :       55864 :     const int nblocks = vDataToHash.size() / 4;
      21                 :             : 
      22                 :             :     //----------
      23                 :             :     // body
      24                 :       55864 :     const uint8_t* blocks = vDataToHash.data();
      25                 :             : 
      26         [ +  + ]:      504826 :     for (int i = 0; i < nblocks; ++i) {
      27                 :      448962 :         uint32_t k1 = ReadLE32(blocks + i*4);
      28                 :             : 
      29                 :      448962 :         k1 *= c1;
      30                 :      448962 :         k1 = std::rotl(k1, 15);
      31                 :      448962 :         k1 *= c2;
      32                 :             : 
      33                 :      448962 :         h1 ^= k1;
      34                 :      448962 :         h1 = std::rotl(h1, 13);
      35                 :      448962 :         h1 = h1 * 5 + 0xe6546b64;
      36                 :             :     }
      37                 :             : 
      38                 :             :     //----------
      39                 :             :     // tail
      40                 :       55864 :     const uint8_t* tail = vDataToHash.data() + nblocks * 4;
      41                 :             : 
      42                 :       55864 :     uint32_t k1 = 0;
      43                 :             : 
      44   [ +  +  +  + ]:       55864 :     switch (vDataToHash.size() & 3) {
      45                 :          16 :         case 3:
      46                 :          16 :             k1 ^= tail[2] << 16;
      47                 :          30 :             [[fallthrough]];
      48                 :          30 :         case 2:
      49                 :          30 :             k1 ^= tail[1] << 8;
      50                 :         385 :             [[fallthrough]];
      51                 :         385 :         case 1:
      52                 :         385 :             k1 ^= tail[0];
      53                 :         385 :             k1 *= c1;
      54                 :         385 :             k1 = std::rotl(k1, 15);
      55                 :         385 :             k1 *= c2;
      56                 :         385 :             h1 ^= k1;
      57                 :             :     }
      58                 :             : 
      59                 :             :     //----------
      60                 :             :     // finalization
      61                 :       55864 :     h1 ^= vDataToHash.size();
      62                 :       55864 :     h1 ^= h1 >> 16;
      63                 :       55864 :     h1 *= 0x85ebca6b;
      64                 :       55864 :     h1 ^= h1 >> 13;
      65                 :       55864 :     h1 *= 0xc2b2ae35;
      66                 :       55864 :     h1 ^= h1 >> 16;
      67                 :             : 
      68                 :       55864 :     return h1;
      69                 :             : }
      70                 :             : 
      71                 :      317970 : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
      72                 :             : {
      73                 :      317970 :     unsigned char num[4];
      74                 :      317970 :     WriteBE32(num, nChild);
      75                 :      317970 :     CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
      76                 :      317970 : }
      77                 :             : 
      78                 :       12720 : uint256 SHA256Uint256(const uint256& input)
      79                 :             : {
      80                 :       12720 :     uint256 result;
      81                 :       12720 :     CSHA256().Write(input.begin(), 32).Finalize(result.begin());
      82                 :       12720 :     return result;
      83                 :             : }
      84                 :             : 
      85                 :        1032 : HashWriter TaggedHash(const std::string& tag)
      86                 :             : {
      87                 :        1032 :     HashWriter writer{};
      88                 :        1032 :     uint256 taghash;
      89                 :        1032 :     CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());
      90                 :        1032 :     writer << taghash << taghash;
      91                 :        1032 :     return writer;
      92                 :             : }
        

Generated by: LCOV version 2.0-1