/root/bitcoin/src/util/hasher.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2019-2022 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 <cstdint> |
15 | | #include <cstring> |
16 | | |
17 | | class SaltedTxidHasher |
18 | | { |
19 | | private: |
20 | | /** Salt */ |
21 | | const uint64_t k0, k1; |
22 | | |
23 | | public: |
24 | | SaltedTxidHasher(); |
25 | | |
26 | 0 | size_t operator()(const uint256& txid) const { |
27 | 0 | return SipHashUint256(k0, k1, txid); |
28 | 0 | } |
29 | | }; |
30 | | |
31 | | class SaltedOutpointHasher |
32 | | { |
33 | | private: |
34 | | /** Salt */ |
35 | | const uint64_t k0, k1; |
36 | | |
37 | | public: |
38 | | SaltedOutpointHasher(bool deterministic = false); |
39 | | |
40 | | /** |
41 | | * Having the hash noexcept allows libstdc++'s unordered_map to recalculate |
42 | | * the hash during rehash, so it does not have to cache the value. This |
43 | | * reduces node's memory by sizeof(size_t). The required recalculation has |
44 | | * a slight performance penalty (around 1.6%), but this is compensated by |
45 | | * memory savings of about 9% which allow for a larger dbcache setting. |
46 | | * |
47 | | * @see https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/manual/manual/unordered_associative.html |
48 | | */ |
49 | 0 | size_t operator()(const COutPoint& id) const noexcept { |
50 | 0 | return SipHashUint256Extra(k0, k1, id.hash, id.n); |
51 | 0 | } |
52 | | }; |
53 | | |
54 | | struct FilterHeaderHasher |
55 | | { |
56 | 0 | size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); } |
57 | | }; |
58 | | |
59 | | /** |
60 | | * We're hashing a nonce into the entries themselves, so we don't need extra |
61 | | * blinding in the set hash computation. |
62 | | * |
63 | | * This may exhibit platform endian dependent behavior but because these are |
64 | | * nonced hashes (random) and this state is only ever used locally it is safe. |
65 | | * All that matters is local consistency. |
66 | | */ |
67 | | class SignatureCacheHasher |
68 | | { |
69 | | public: |
70 | | template <uint8_t hash_select> |
71 | | uint32_t operator()(const uint256& key) const |
72 | 0 | { |
73 | 0 | static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available."); |
74 | 0 | uint32_t u; |
75 | 0 | std::memcpy(&u, key.begin()+4*hash_select, 4); |
76 | 0 | return u; |
77 | 0 | } Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh0EEEjRK7uint256 Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh1EEEjRK7uint256 Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh2EEEjRK7uint256 Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh3EEEjRK7uint256 Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh4EEEjRK7uint256 Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh5EEEjRK7uint256 Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh6EEEjRK7uint256 Unexecuted instantiation: _ZNK20SignatureCacheHasherclILh7EEEjRK7uint256 |
78 | | }; |
79 | | |
80 | | struct BlockHasher |
81 | | { |
82 | | // this used to call `GetCheapHash()` in uint256, which was later moved; the |
83 | | // cheap hash function simply calls ReadLE64() however, so the end result is |
84 | | // identical |
85 | 0 | size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); } |
86 | | }; |
87 | | |
88 | | class SaltedSipHasher |
89 | | { |
90 | | private: |
91 | | /** Salt */ |
92 | | const uint64_t m_k0, m_k1; |
93 | | |
94 | | public: |
95 | | SaltedSipHasher(); |
96 | | |
97 | | size_t operator()(const Span<const unsigned char>& script) const; |
98 | | }; |
99 | | |
100 | | #endif // BITCOIN_UTIL_HASHER_H |