Bitcoin Core 29.99.0
P2P Digital Currency
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>
11#include <span.h>
12#include <uint256.h>
13
14#include <concepts>
15#include <cstdint>
16#include <cstring>
17
19{
20private:
22 const uint64_t k0, k1;
23
24public:
26
27 size_t operator()(const uint256& hash) const {
28 return SipHashUint256(k0, k1, hash);
29 }
30};
31
33{
34private:
36 const uint64_t k0, k1;
37
38public:
40
41 size_t operator()(const Txid& txid) const {
42 return SipHashUint256(k0, k1, txid.ToUint256());
43 }
44};
45
47{
48private:
50 const uint64_t k0, k1;
51
52public:
54
55 size_t operator()(const Wtxid& wtxid) const {
56 return SipHashUint256(k0, k1, wtxid.ToUint256());
57 }
58};
59
60
62{
63private:
65 const uint64_t k0, k1;
66
67public:
68 SaltedOutpointHasher(bool deterministic = false);
69
79 size_t operator()(const COutPoint& id) const noexcept {
80 return SipHashUint256Extra(k0, k1, id.hash.ToUint256(), id.n);
81 }
82};
83
85{
86 size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
87};
88
98{
99public:
100 template <uint8_t hash_select>
101 uint32_t operator()(const uint256& key) const
102 {
103 static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
104 uint32_t u;
105 std::memcpy(&u, key.begin()+4*hash_select, 4);
106 return u;
107 }
108};
109
111{
112 // this used to call `GetCheapHash()` in uint256, which was later moved; the
113 // cheap hash function simply calls ReadLE64() however, so the end result is
114 // identical
115 size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
116};
117
119{
120private:
122 const uint64_t m_k0, m_k1;
123
124public:
126
127 size_t operator()(const std::span<const unsigned char>& script) const;
128};
129
130#endif // BITCOIN_UTIL_HASHER_H
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
const uint64_t k0
Salt.
Definition: hasher.h:65
const uint64_t k1
Definition: hasher.h:65
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:79
SaltedOutpointHasher(bool deterministic=false)
Definition: hasher.cpp:22
size_t operator()(const std::span< const unsigned char > &script) const
Definition: hasher.cpp:31
const uint64_t m_k0
Salt.
Definition: hasher.h:122
const uint64_t m_k1
Definition: hasher.h:122
const uint64_t k1
Definition: hasher.h:36
size_t operator()(const Txid &txid) const
Definition: hasher.h:41
const uint64_t k0
Salt.
Definition: hasher.h:36
size_t operator()(const uint256 &hash) const
Definition: hasher.h:27
const uint64_t k0
Salt.
Definition: hasher.h:22
const uint64_t k1
Definition: hasher.h:22
const uint64_t k1
Definition: hasher.h:50
size_t operator()(const Wtxid &wtxid) const
Definition: hasher.h:55
const uint64_t k0
Salt.
Definition: hasher.h:50
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:98
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:101
constexpr unsigned char * begin()
Definition: uint256.h:101
const uint256 & ToUint256() const LIFETIMEBOUND
256-bit opaque blob.
Definition: uint256.h:196
uint64_t ReadLE64(const B *ptr)
Definition: common.h:35
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:135
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:95
size_t operator()(const uint256 &hash) const
Definition: hasher.h:115
size_t operator()(const uint256 &hash) const
Definition: hasher.h:86