Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
hash.cpp
Go to the documentation of this file.
1
// Copyright (c) 2013-present 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 <bit>
11
#include <string>
12
13
unsigned
int
MurmurHash3
(
unsigned
int
nHashSeed, std::span<const unsigned char> vDataToHash)
14
{
15
// The following is MurmurHash3 (x86_32), see https://github.com/aappleby/smhasher/blob/master/src/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 = std::rotl(k1, 15);
31
k1 *= c2;
32
33
h1 ^= k1;
34
h1 = std::rotl(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 = std::rotl(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,
unsigned
int
nChild,
unsigned
char
header,
const
unsigned
char
data[32],
unsigned
char
output[64])
72
{
73
unsigned
char
num[4];
74
WriteBE32
(num, nChild);
75
CHMAC_SHA512
(chainCode.
begin
(), chainCode.
size
()).
Write
(&header, 1).
Write
(data, 32).
Write
(num, 4).
Finalize
(output);
76
}
77
78
uint256
SHA256Uint256
(
const
uint256
& input)
79
{
80
uint256
result;
81
CSHA256
().
Write
(input.
begin
(), 32).
Finalize
(result.
begin
());
82
return
result;
83
}
84
85
HashWriter
TaggedHash
(
const
std::string& tag)
86
{
87
HashWriter
writer{};
88
uint256
taghash;
89
CSHA256
().
Write
((
const
unsigned
char
*)tag.data(), tag.size()).
Finalize
(taghash.
begin
());
90
writer << taghash << taghash;
91
return
writer;
92
}
CHMAC_SHA512
A hasher class for HMAC-SHA-512.
Definition
hmac_sha512.h:14
CHMAC_SHA512::Write
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition
hmac_sha512.h:23
CHMAC_SHA512::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition
hmac_sha512.cpp:31
CSHA256
A hasher class for SHA-256.
Definition
sha256.h:14
CSHA256::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition
sha256.cpp:725
CSHA256::Write
CSHA256 & Write(const unsigned char *data, size_t len)
Definition
sha256.cpp:699
HashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition
hash.h:101
base_blob::size
static constexpr unsigned int size()
Definition
uint256.h:106
base_blob::begin
constexpr unsigned char * begin()
Definition
uint256.h:100
uint256
256-bit opaque blob.
Definition
uint256.h:195
common.h
ReadLE32
uint32_t ReadLE32(const B *ptr)
Definition
common.h:27
WriteBE32
void WriteBE32(B *ptr, uint32_t x)
Definition
common.h:95
BIP32Hash
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition
hash.cpp:71
TaggedHash
HashWriter TaggedHash(const std::string &tag)
Return a HashWriter primed for tagged hashes (as specified in BIP 340).
Definition
hash.cpp:85
SHA256Uint256
uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition
hash.cpp:78
MurmurHash3
unsigned int MurmurHash3(unsigned int nHashSeed, std::span< const unsigned char > vDataToHash)
Definition
hash.cpp:13
hash.h
ChainCode
uint256 ChainCode
Definition
hash.h:21
hmac_sha512.h
span.h
Generated on
for Bitcoin Core by
1.17.0