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