Bitcoin Core  22.99.0
P2P Digital Currency
hash.cpp
Go to the documentation of this file.
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 {
13  return (x << r) | (x >> (32 - r));
14 }
15 
16 unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash)
17 {
18  // The following is MurmurHash3 (x86_32), see https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
19  uint32_t h1 = nHashSeed;
20  const uint32_t c1 = 0xcc9e2d51;
21  const uint32_t c2 = 0x1b873593;
22 
23  const int nblocks = vDataToHash.size() / 4;
24 
25  //----------
26  // body
27  const uint8_t* blocks = vDataToHash.data();
28 
29  for (int i = 0; i < nblocks; ++i) {
30  uint32_t k1 = ReadLE32(blocks + i*4);
31 
32  k1 *= c1;
33  k1 = ROTL32(k1, 15);
34  k1 *= c2;
35 
36  h1 ^= k1;
37  h1 = ROTL32(h1, 13);
38  h1 = h1 * 5 + 0xe6546b64;
39  }
40 
41  //----------
42  // tail
43  const uint8_t* tail = vDataToHash.data() + nblocks * 4;
44 
45  uint32_t k1 = 0;
46 
47  switch (vDataToHash.size() & 3) {
48  case 3:
49  k1 ^= tail[2] << 16;
50  [[fallthrough]];
51  case 2:
52  k1 ^= tail[1] << 8;
53  [[fallthrough]];
54  case 1:
55  k1 ^= tail[0];
56  k1 *= c1;
57  k1 = ROTL32(k1, 15);
58  k1 *= c2;
59  h1 ^= k1;
60  }
61 
62  //----------
63  // finalization
64  h1 ^= vDataToHash.size();
65  h1 ^= h1 >> 16;
66  h1 *= 0x85ebca6b;
67  h1 ^= h1 >> 13;
68  h1 *= 0xc2b2ae35;
69  h1 ^= h1 >> 16;
70 
71  return h1;
72 }
73 
74 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
75 {
76  unsigned char num[4];
77  num[0] = (nChild >> 24) & 0xFF;
78  num[1] = (nChild >> 16) & 0xFF;
79  num[2] = (nChild >> 8) & 0xFF;
80  num[3] = (nChild >> 0) & 0xFF;
81  CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
82 }
83 
85 {
86  uint256 result;
87  CSHA256().Write(input.begin(), 32).Finalize(result.begin());
88  return result;
89 }
90 
91 CHashWriter TaggedHash(const std::string& tag)
92 {
93  CHashWriter writer(SER_GETHASH, 0);
94  uint256 taghash;
95  CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());
96  writer << taghash << taghash;
97  return writer;
98 }
ReadLE32
static uint32_t ReadLE32(const unsigned char *ptr)
Definition: common.h:24
CSHA256::Write
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
base_blob::size
static constexpr unsigned int size()
Definition: uint256.h:78
hmac_sha512.h
common.h
CHMAC_SHA512::Write
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha512.h:24
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:182
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
SHA256Uint256
uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition: hash.cpp:84
ROTL32
uint32_t ROTL32(uint32_t x, int8_t r)
Definition: hash.cpp:11
CHMAC_SHA512::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hmac_sha512.cpp:29
uint256
256-bit opaque blob.
Definition: uint256.h:124
CSHA256::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:663
BIP32Hash
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:74
Span::data
constexpr C * data() const noexcept
Definition: span.h:169
MurmurHash3
unsigned int MurmurHash3(unsigned int nHashSeed, Span< const unsigned char > vDataToHash)
Definition: hash.cpp:16
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
hash.h
TaggedHash
CHashWriter TaggedHash(const std::string &tag)
Return a CHashWriter primed for tagged hashes (as specified in BIP 340).
Definition: hash.cpp:91
k1
static const unsigned char k1[32]
Definition: chacha_poly_aead.cpp:19
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:140
CHMAC_SHA512
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:14
base_blob::begin
unsigned char * begin()
Definition: uint256.h:58