LCOV - code coverage report
Current view: top level - src - hash.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 42 42 100.0 %
Date: 2020-09-26 01:30:44 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2013-2018 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 <crypto/common.h>
       7             : #include <crypto/hmac_sha512.h>
       8             : 
       9             : 
      10    69282242 : inline uint32_t ROTL32(uint32_t x, int8_t r)
      11             : {
      12    69282242 :     return (x << r) | (x >> (32 - r));
      13             : }
      14             : 
      15     4414281 : unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash)
      16             : {
      17             :     // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
      18             :     uint32_t h1 = nHashSeed;
      19             :     const uint32_t c1 = 0xcc9e2d51;
      20             :     const uint32_t c2 = 0x1b873593;
      21             : 
      22     4414281 :     const int nblocks = vDataToHash.size() / 4;
      23             : 
      24             :     //----------
      25             :     // body
      26     4414281 :     const uint8_t* blocks = vDataToHash.data();
      27             : 
      28    38975084 :     for (int i = 0; i < nblocks; ++i) {
      29    34560806 :         uint32_t k1 = ReadLE32(blocks + i*4);
      30             : 
      31    34560806 :         k1 *= c1;
      32    34560806 :         k1 = ROTL32(k1, 15);
      33    34560806 :         k1 *= c2;
      34             : 
      35    34560806 :         h1 ^= k1;
      36    34560806 :         h1 = ROTL32(h1, 13);
      37    34560806 :         h1 = h1 * 5 + 0xe6546b64;
      38             :     }
      39             : 
      40             :     //----------
      41             :     // tail
      42     4414243 :     const uint8_t* tail = vDataToHash.data() + nblocks * 4;
      43             : 
      44             :     uint32_t k1 = 0;
      45             : 
      46     4414243 :     switch (vDataToHash.size() & 3) {
      47             :         case 3:
      48          18 :             k1 ^= tail[2] << 16;
      49             :         case 2:
      50      160301 :             k1 ^= tail[1] << 8;
      51             :         case 1:
      52      160661 :             k1 ^= tail[0];
      53      160661 :             k1 *= c1;
      54      160661 :             k1 = ROTL32(k1, 15);
      55      160661 :             k1 *= c2;
      56      160661 :             h1 ^= k1;
      57      160661 :     }
      58             : 
      59             :     //----------
      60             :     // finalization
      61     4414249 :     h1 ^= vDataToHash.size();
      62     4414249 :     h1 ^= h1 >> 16;
      63     4414249 :     h1 *= 0x85ebca6b;
      64     4414249 :     h1 ^= h1 >> 13;
      65     4414249 :     h1 *= 0xc2b2ae35;
      66     4414249 :     h1 ^= h1 >> 16;
      67             : 
      68     4414249 :     return h1;
      69             : }
      70             : 
      71      141499 : void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
      72             : {
      73      141499 :     unsigned char num[4];
      74      141499 :     num[0] = (nChild >> 24) & 0xFF;
      75      141499 :     num[1] = (nChild >> 16) & 0xFF;
      76      141499 :     num[2] = (nChild >>  8) & 0xFF;
      77      141499 :     num[3] = (nChild >>  0) & 0xFF;
      78      141499 :     CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
      79      141499 : }
      80             : 
      81      141351 : uint256 SHA256Uint256(const uint256& input)
      82             : {
      83      141351 :     uint256 result;
      84      141351 :     CSHA256().Write(input.begin(), 32).Finalize(result.begin());
      85      141351 :     return result;
      86             : }

Generated by: LCOV version 1.15