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