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