Bitcoin ABC  0.24.10
P2P Digital Currency
hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-2016 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 
7 #include <crypto/hmac_sha512.h>
8 
9 inline uint32_t ROTL32(uint32_t x, int8_t r) {
10  return (x << r) | (x >> (32 - r));
11 }
12 
13 uint32_t MurmurHash3(uint32_t nHashSeed, Span<const uint8_t> vDataToHash) {
14  // The following is MurmurHash3 (x86_32), see
15  // http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
16  uint32_t h1 = nHashSeed;
17  const uint32_t c1 = 0xcc9e2d51;
18  const uint32_t c2 = 0x1b873593;
19 
20  const int nblocks = vDataToHash.size() / 4;
21 
22  //----------
23  // body
24  const uint8_t *blocks = vDataToHash.data();
25 
26  for (int i = 0; i < nblocks; ++i) {
27  uint32_t k1 = ReadLE32(blocks + i * 4);
28 
29  k1 *= c1;
30  k1 = ROTL32(k1, 15);
31  k1 *= c2;
32 
33  h1 ^= k1;
34  h1 = ROTL32(h1, 13);
35  h1 = h1 * 5 + 0xe6546b64;
36  }
37 
38  //----------
39  // tail
40  const uint8_t *tail = vDataToHash.data() + nblocks * 4;
41 
42  uint32_t k1 = 0;
43 
44  switch (vDataToHash.size() & 3) {
45  case 3:
46  k1 ^= tail[2] << 16;
47  // FALLTHROUGH
48  case 2:
49  k1 ^= tail[1] << 8;
50  // FALLTHROUGH
51  case 1:
52  k1 ^= tail[0];
53  k1 *= c1;
54  k1 = ROTL32(k1, 15);
55  k1 *= c2;
56  h1 ^= k1;
57  }
58 
59  //----------
60  // finalization
61  h1 ^= vDataToHash.size();
62  h1 ^= h1 >> 16;
63  h1 *= 0x85ebca6b;
64  h1 ^= h1 >> 13;
65  h1 *= 0xc2b2ae35;
66  h1 ^= h1 >> 16;
67 
68  return h1;
69 }
70 
71 void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
72  const uint8_t data[32], uint8_t output[64]) {
73  uint8_t num[4];
74  num[0] = (nChild >> 24) & 0xFF;
75  num[1] = (nChild >> 16) & 0xFF;
76  num[2] = (nChild >> 8) & 0xFF;
77  num[3] = (nChild >> 0) & 0xFF;
78  CHMAC_SHA512(chainCode.begin(), chainCode.size())
79  .Write(&header, 1)
80  .Write(data, 32)
81  .Write(num, 4)
82  .Finalize(output);
83 }
k1
static const uint8_t k1[32]
Definition: chacha_poly_aead.cpp:20
hmac_sha512.h
ReadLE32
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:23
MurmurHash3
uint32_t MurmurHash3(uint32_t nHashSeed, Span< const uint8_t > vDataToHash)
Definition: hash.cpp:13
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:209
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
CHMAC_SHA512::Write
CHMAC_SHA512 & Write(const uint8_t *data, size_t len)
Definition: hmac_sha512.h:23
ROTL32
uint32_t ROTL32(uint32_t x, int8_t r)
Definition: hash.cpp:9
CHMAC_SHA512::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: hmac_sha512.cpp:30
uint256
256-bit opaque blob.
Definition: uint256.h:127
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
Span::data
constexpr C * data() const noexcept
Definition: span.h:198
hash.h
base_blob::size
unsigned int size() const
Definition: uint256.h:91
CHMAC_SHA512
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:14
BIP32Hash
void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, const uint8_t data[32], uint8_t output[64])
Definition: hash.cpp:71