Bitcoin Core  27.99.0
P2P Digital Currency
hasher.h
Go to the documentation of this file.
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 <uint256.h>
12 
13 #include <cstdint>
14 #include <cstring>
15 
16 template <typename C> class Span;
17 
19 {
20 private:
22  const uint64_t k0, k1;
23 
24 public:
26 
27  size_t operator()(const uint256& txid) const {
28  return SipHashUint256(k0, k1, txid);
29  }
30 };
31 
33 {
34 private:
36  const uint64_t k0, k1;
37 
38 public:
39  SaltedOutpointHasher(bool deterministic = false);
40 
50  size_t operator()(const COutPoint& id) const noexcept {
51  return SipHashUint256Extra(k0, k1, id.hash, id.n);
52  }
53 };
54 
56 {
57  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
58 };
59 
69 {
70 public:
71  template <uint8_t hash_select>
72  uint32_t operator()(const uint256& key) const
73  {
74  static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
75  uint32_t u;
76  std::memcpy(&u, key.begin()+4*hash_select, 4);
77  return u;
78  }
79 };
80 
82 {
83  // this used to call `GetCheapHash()` in uint256, which was later moved; the
84  // cheap hash function simply calls ReadLE64() however, so the end result is
85  // identical
86  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
87 };
88 
90 {
91 private:
93  const uint64_t m_k0, m_k1;
94 
95 public:
97 
98  size_t operator()(const Span<const unsigned char>& script) const;
99 };
100 
101 #endif // BITCOIN_UTIL_HASHER_H
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
const uint64_t k0
Salt.
Definition: hasher.h:36
const uint64_t k1
Definition: hasher.h:36
size_t operator()(const COutPoint &id) const noexcept
Having the hash noexcept allows libstdc++'s unordered_map to recalculate the hash during rehash,...
Definition: hasher.h:50
SaltedOutpointHasher(bool deterministic=false)
Definition: hasher.cpp:12
size_t operator()(const Span< const unsigned char > &script) const
Definition: hasher.cpp:19
const uint64_t m_k0
Salt.
Definition: hasher.h:93
const uint64_t m_k1
Definition: hasher.h:93
const uint64_t k1
Definition: hasher.h:22
size_t operator()(const uint256 &txid) const
Definition: hasher.h:27
const uint64_t k0
Salt.
Definition: hasher.h:22
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:69
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:72
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
constexpr unsigned char * begin()
Definition: uint256.h:68
256-bit opaque blob.
Definition: uint256.h:106
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:27
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:135
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:95
size_t operator()(const uint256 &hash) const
Definition: hasher.h:86
size_t operator()(const uint256 &hash) const
Definition: hasher.h:57