Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
util
hasher.h
Go to the documentation of this file.
1
// Copyright (c) 2019-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
#ifndef BITCOIN_UTIL_HASHER_H
6
#define BITCOIN_UTIL_HASHER_H
7
8
#include <
crypto/common.h
>
9
#include <
crypto/siphash.h
>
10
#include <
primitives/transaction.h
>
11
#include <
span.h
>
12
#include <
uint256.h
>
13
14
#include <concepts>
15
#include <cstdint>
16
#include <cstring>
17
18
class
SaltedUint256Hasher
19
{
20
const
PresaltedSipHasher
m_hasher
;
21
22
public
:
23
SaltedUint256Hasher
();
24
25
size_t
operator()
(
const
uint256
& hash)
const
26
{
27
return
m_hasher
(hash);
28
}
29
};
30
31
class
SaltedTxidHasher
32
{
33
const
PresaltedSipHasher
m_hasher
;
34
35
public
:
36
SaltedTxidHasher
();
37
38
size_t
operator()
(
const
Txid
& txid)
const
39
{
40
return
m_hasher
(txid.
ToUint256
());
41
}
42
};
43
44
class
SaltedWtxidHasher
45
{
46
const
PresaltedSipHasher
m_hasher
;
47
48
public
:
49
SaltedWtxidHasher
();
50
51
size_t
operator()
(
const
Wtxid
& wtxid)
const
52
{
53
return
m_hasher
(wtxid.
ToUint256
());
54
}
55
};
56
57
class
SaltedOutpointHasher
58
{
59
const
PresaltedSipHasher
m_hasher
;
60
61
public
:
62
SaltedOutpointHasher
(
bool
deterministic =
false
);
63
73
size_t
operator()
(
const
COutPoint
&
id
)
const
noexcept
74
{
75
return
m_hasher
(
id
.hash.ToUint256(),
id
.n);
76
}
77
};
78
87
class
SignatureCacheHasher
88
{
89
public
:
90
template
<u
int
8_t hash_select>
91
uint32_t
operator()
(
const
uint256
& key)
const
92
{
93
static_assert
(hash_select <8,
"SignatureCacheHasher only has 8 hashes available."
);
94
uint32_t u;
95
std::memcpy(&u, key.
begin
()+4*hash_select, 4);
96
return
u;
97
}
98
};
99
100
struct
BlockHasher
101
{
102
// this used to call `GetCheapHash()` in uint256, which was later moved; the
103
// cheap hash function simply calls ReadLE64() however, so the end result is
104
// identical
105
size_t
operator()
(
const
uint256
& hash)
const
{
return
ReadLE64
(hash.
begin
()); }
106
};
107
108
class
SaltedSipHasher
109
{
110
private
:
112
const
uint64_t
m_k0
,
m_k1
;
113
114
public
:
115
SaltedSipHasher
();
116
117
size_t
operator()
(
const
std::span<const unsigned char>&
script
)
const
;
118
};
119
120
#endif
// BITCOIN_UTIL_HASHER_H
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition
transaction.h:29
PresaltedSipHasher
Optimized SipHash-2-4 implementation for uint256.
Definition
siphash.h:56
SaltedOutpointHasher::m_hasher
const PresaltedSipHasher m_hasher
Definition
hasher.h:59
SaltedOutpointHasher::operator()
size_t operator()(const COutPoint &id) const noexcept
Having the hash noexcept allows libstdc++'s unordered_map to recalculate the hash during rehash,...
Definition
hasher.h:73
SaltedOutpointHasher::SaltedOutpointHasher
SaltedOutpointHasher(bool deterministic=false)
Definition
hasher.cpp:25
SaltedSipHasher::SaltedSipHasher
SaltedSipHasher()
Definition
hasher.cpp:30
SaltedSipHasher::operator()
size_t operator()(const std::span< const unsigned char > &script) const
Definition
hasher.cpp:35
SaltedSipHasher::m_k0
const uint64_t m_k0
Salt.
Definition
hasher.h:112
SaltedSipHasher::m_k1
const uint64_t m_k1
Definition
hasher.h:112
SaltedTxidHasher::m_hasher
const PresaltedSipHasher m_hasher
Definition
hasher.h:33
SaltedTxidHasher::SaltedTxidHasher
SaltedTxidHasher()
Definition
hasher.cpp:15
SaltedTxidHasher::operator()
size_t operator()(const Txid &txid) const
Definition
hasher.h:38
SaltedUint256Hasher::m_hasher
const PresaltedSipHasher m_hasher
Definition
hasher.h:20
SaltedUint256Hasher::operator()
size_t operator()(const uint256 &hash) const
Definition
hasher.h:25
SaltedUint256Hasher::SaltedUint256Hasher
SaltedUint256Hasher()
Definition
hasher.cpp:10
SaltedWtxidHasher::SaltedWtxidHasher
SaltedWtxidHasher()
Definition
hasher.cpp:20
SaltedWtxidHasher::operator()
size_t operator()(const Wtxid &wtxid) const
Definition
hasher.h:51
SaltedWtxidHasher::m_hasher
const PresaltedSipHasher m_hasher
Definition
hasher.h:46
SignatureCacheHasher
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition
hasher.h:88
SignatureCacheHasher::operator()
uint32_t operator()(const uint256 &key) const
Definition
hasher.h:91
base_blob::begin
constexpr unsigned char * begin()
Definition
uint256.h:100
transaction_identifier::ToUint256
const uint256 & ToUint256() const LIFETIMEBOUND
Definition
transaction_identifier.h:48
uint256
256-bit opaque blob.
Definition
uint256.h:195
common.h
ReadLE64
uint64_t ReadLE64(const B *ptr)
Definition
common.h:35
script
Definition
parsing.cpp:13
transaction.h
siphash.h
span.h
BlockHasher
Definition
hasher.h:101
BlockHasher::operator()
size_t operator()(const uint256 &hash) const
Definition
hasher.h:105
Wtxid
transaction_identifier< true > Wtxid
Wtxid commits to all transaction fields including the witness.
Definition
transaction_identifier.h:73
Txid
transaction_identifier< false > Txid
Txid commits to all transaction fields except the witness.
Definition
transaction_identifier.h:71
uint256.h
Generated on
for Bitcoin Core by
1.17.0