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