Branch data Line data Source code
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 : : private:
21 : : /** Salt */
22 : : const uint64_t k0, k1;
23 : :
24 : : public:
25 : : SaltedUint256Hasher();
26 : :
27 : 586 : size_t operator()(const uint256& hash) const {
28 : 586 : return SipHashUint256(k0, k1, hash);
29 : : }
30 : : };
31 : :
32 : : class SaltedTxidHasher
33 : : {
34 : : private:
35 : : /** Salt */
36 : : const uint64_t k0, k1;
37 : :
38 : : public:
39 : : SaltedTxidHasher();
40 : :
41 : 126073942 : size_t operator()(const Txid& txid) const {
42 [ + - ]: 126073942 : return SipHashUint256(k0, k1, txid.ToUint256());
43 : : }
44 : : };
45 : :
46 : : class SaltedWtxidHasher
47 : : {
48 : : private:
49 : : /** Salt */
50 : : const uint64_t k0, k1;
51 : :
52 : : public:
53 : : SaltedWtxidHasher();
54 : :
55 : 35980355 : size_t operator()(const Wtxid& wtxid) const {
56 [ + - ]: 35980355 : return SipHashUint256(k0, k1, wtxid.ToUint256());
57 : : }
58 : : };
59 : :
60 : :
61 : : class SaltedOutpointHasher
62 : : {
63 : : private:
64 : : /** Salt */
65 : : const uint64_t k0, k1;
66 : :
67 : : public:
68 : : SaltedOutpointHasher(bool deterministic = false);
69 : :
70 : : /**
71 : : * Having the hash noexcept allows libstdc++'s unordered_map to recalculate
72 : : * the hash during rehash, so it does not have to cache the value. This
73 : : * reduces node's memory by sizeof(size_t). The required recalculation has
74 : : * a slight performance penalty (around 1.6%), but this is compensated by
75 : : * memory savings of about 9% which allow for a larger dbcache setting.
76 : : *
77 : : * @see https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/manual/manual/unordered_associative.html
78 : : */
79 : 525371956 : size_t operator()(const COutPoint& id) const noexcept {
80 : 525371956 : return SipHashUint256Extra(k0, k1, id.hash.ToUint256(), id.n);
81 : : }
82 : : };
83 : :
84 : : struct FilterHeaderHasher
85 : : {
86 [ # # ]: 0 : size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
87 : : };
88 : :
89 : : /**
90 : : * We're hashing a nonce into the entries themselves, so we don't need extra
91 : : * blinding in the set hash computation.
92 : : *
93 : : * This may exhibit platform endian dependent behavior but because these are
94 : : * nonced hashes (random) and this state is only ever used locally it is safe.
95 : : * All that matters is local consistency.
96 : : */
97 : : class SignatureCacheHasher
98 : : {
99 : : public:
100 : : template <uint8_t hash_select>
101 : 531002 : 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 : 531002 : std::memcpy(&u, key.begin()+4*hash_select, 4);
106 : : return u;
107 : : }
108 : : };
109 : :
110 : : struct BlockHasher
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 [ + + ]: 3403223 : size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
116 : : };
117 : :
118 : : class SaltedSipHasher
119 : : {
120 : : private:
121 : : /** Salt */
122 : : const uint64_t m_k0, m_k1;
123 : :
124 : : public:
125 : : SaltedSipHasher();
126 : :
127 : : size_t operator()(const std::span<const unsigned char>& script) const;
128 : : };
129 : :
130 : : #endif // BITCOIN_UTIL_HASHER_H
|