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