Bitcoin Core  31.0.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>
10 #include <primitives/transaction.h>
11 #include <span.h>
12 #include <uint256.h>
13 
14 #include <concepts>
15 #include <cstdint>
16 #include <cstring>
17 
19 {
21 
22 public:
24 
25  size_t operator()(const uint256& hash) const
26  {
27  return m_hasher(hash);
28  }
29 };
30 
32 {
34 
35 public:
37 
38  size_t operator()(const Txid& txid) const
39  {
40  return m_hasher(txid.ToUint256());
41  }
42 };
43 
45 {
47 
48 public:
50 
51  size_t operator()(const Wtxid& wtxid) const
52  {
53  return m_hasher(wtxid.ToUint256());
54  }
55 };
56 
58 {
60 
61 public:
62  SaltedOutpointHasher(bool deterministic = false);
63 
73  size_t operator()(const COutPoint& id) const noexcept
74  {
75  return m_hasher(id.hash.ToUint256(), id.n);
76  }
77 };
78 
88 {
89 public:
90  template <uint8_t hash_select>
91  uint32_t operator()(const uint256& key) const
92  {
93  static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
94  uint32_t u;
95  std::memcpy(&u, key.begin()+4*hash_select, 4);
96  return u;
97  }
98 };
99 
101 {
102  // this used to call `GetCheapHash()` in uint256, which was later moved; the
103  // cheap hash function simply calls ReadLE64() however, so the end result is
104  // identical
105  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
106 };
107 
109 {
110 private:
112  const uint64_t m_k0, m_k1;
113 
114 public:
115  SaltedSipHasher();
116 
117  size_t operator()(const std::span<const unsigned char>& script) const;
118 };
119 
120 #endif // BITCOIN_UTIL_HASHER_H
const PresaltedSipHasher m_hasher
Definition: hasher.h:20
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.h:55
size_t operator()(const Wtxid &wtxid) const
Definition: hasher.h:51
memcpy(result.begin(), stream.data(), stream.size())
const PresaltedSipHasher m_hasher
Definition: hasher.h:33
We&#39;re hashing a nonce into the entries themselves, so we don&#39;t need extra blinding in the set hash co...
Definition: hasher.h:87
constexpr unsigned char * begin()
Definition: uint256.h:100
size_t operator()(const uint256 &hash) const
Definition: hasher.h:25
size_t operator()(const Txid &txid) const
Definition: hasher.h:38
const PresaltedSipHasher m_hasher
Definition: hasher.h:46
const uint64_t m_k0
Salt.
Definition: hasher.h:112
uint64_t ReadLE64(const B *ptr)
Definition: common.h:35
size_t operator()(const COutPoint &id) const noexcept
Having the hash noexcept allows libstdc++&#39;s unordered_map to recalculate the hash during rehash...
Definition: hasher.h:73
size_t operator()(const std::span< const unsigned char > &script) const
Definition: hasher.cpp:35
const uint64_t m_k1
Definition: hasher.h:112
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
size_t operator()(const uint256 &hash) const
Definition: hasher.h:105
256-bit opaque blob.
Definition: uint256.h:195
const uint256 & ToUint256() const LIFETIMEBOUND
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:91
SaltedOutpointHasher(bool deterministic=false)
Definition: hasher.cpp:25
const PresaltedSipHasher m_hasher
Definition: hasher.h:59