LCOV - code coverage report
Current view: top level - src - hash.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 10 49 20.4 %
Date: 2023-09-26 12:08:55 Functions: 2 5 40.0 %

          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 <string>
      11             : 
      12           0 : inline uint32_t ROTL32(uint32_t x, int8_t r)
      13             : {
      14           0 :     return (x << r) | (x >> (32 - r));
      15             : }
      16             : 
      17           0 : unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash)
      18             : {
      19             :     // The following is MurmurHash3 (x86_32), see https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
      20           0 :     uint32_t h1 = nHashSeed;
      21           0 :     const uint32_t c1 = 0xcc9e2d51;
      22           0 :     const uint32_t c2 = 0x1b873593;
      23             : 
      24           0 :     const int nblocks = vDataToHash.size() / 4;
      25             : 
      26             :     //----------
      27             :     // body
      28           0 :     const uint8_t* blocks = vDataToHash.data();
      29             : 
      30           0 :     for (int i = 0; i < nblocks; ++i) {
      31           0 :         uint32_t k1 = ReadLE32(blocks + i*4);
      32             : 
      33           0 :         k1 *= c1;
      34           0 :         k1 = ROTL32(k1, 15);
      35           0 :         k1 *= c2;
      36             : 
      37           0 :         h1 ^= k1;
      38           0 :         h1 = ROTL32(h1, 13);
      39           0 :         h1 = h1 * 5 + 0xe6546b64;
      40           0 :     }
      41             : 
      42             :     //----------
      43             :     // tail
      44           0 :     const uint8_t* tail = vDataToHash.data() + nblocks * 4;
      45             : 
      46           0 :     uint32_t k1 = 0;
      47             : 
      48           0 :     switch (vDataToHash.size() & 3) {
      49             :         case 3:
      50           0 :             k1 ^= tail[2] << 16;
      51             :             [[fallthrough]];
      52             :         case 2:
      53           0 :             k1 ^= tail[1] << 8;
      54             :             [[fallthrough]];
      55             :         case 1:
      56           0 :             k1 ^= tail[0];
      57           0 :             k1 *= c1;
      58           0 :             k1 = ROTL32(k1, 15);
      59           0 :             k1 *= c2;
      60           0 :             h1 ^= k1;
      61           0 :     }
      62             : 
      63             :     //----------
      64             :     // finalization
      65           0 :     h1 ^= vDataToHash.size();
      66           0 :     h1 ^= h1 >> 16;
      67           0 :     h1 *= 0x85ebca6b;
      68           0 :     h1 ^= h1 >> 13;
      69           0 :     h1 *= 0xc2b2ae35;
      70           0 :     h1 ^= h1 >> 16;
      71             : 
      72           0 :     return h1;
      73             : }
      74             : 
      75           0 : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
      76             : {
      77             :     unsigned char num[4];
      78           0 :     WriteBE32(num, nChild);
      79           0 :     CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
      80           0 : }
      81             : 
      82       10602 : uint256 SHA256Uint256(const uint256& input)
      83             : {
      84       10602 :     uint256 result;
      85       10602 :     CSHA256().Write(input.begin(), 32).Finalize(result.begin());
      86       10602 :     return result;
      87             : }
      88             : 
      89          10 : HashWriter TaggedHash(const std::string& tag)
      90             : {
      91          10 :     HashWriter writer{};
      92          10 :     uint256 taghash;
      93          10 :     CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());
      94          10 :     writer << taghash << taghash;
      95          10 :     return writer;
      96             : }

Generated by: LCOV version 1.14