Bitcoin Core  29.1.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 <span.h>
12 #include <uint256.h>
13 
14 #include <cstdint>
15 #include <cstring>
16 
18 {
19 private:
21  const uint64_t k0, k1;
22 
23 public:
25 
26  size_t operator()(const uint256& txid) const {
27  return SipHashUint256(k0, k1, txid);
28  }
29 };
30 
32 {
33 private:
35  const uint64_t k0, k1;
36 
37 public:
38  SaltedOutpointHasher(bool deterministic = false);
39 
49  size_t operator()(const COutPoint& id) const noexcept {
50  return SipHashUint256Extra(k0, k1, id.hash, id.n);
51  }
52 };
53 
55 {
56  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
57 };
58 
68 {
69 public:
70  template <uint8_t hash_select>
71  uint32_t operator()(const uint256& key) const
72  {
73  static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
74  uint32_t u;
75  std::memcpy(&u, key.begin()+4*hash_select, 4);
76  return u;
77  }
78 };
79 
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  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
86 };
87 
89 {
90 private:
92  const uint64_t m_k0, m_k1;
93 
94 public:
96 
97  size_t operator()(const Span<const unsigned char>& script) const;
98 };
99 
100 #endif // BITCOIN_UTIL_HASHER_H
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 Span< const unsigned char > &script) const
Definition: hasher.cpp:23
const uint64_t k1
Definition: hasher.h:21
memcpy(result.begin(), stream.data(), stream.size())
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:67
constexpr unsigned char * begin()
Definition: uint256.h:115
const uint64_t m_k0
Salt.
Definition: hasher.h:92
uint64_t ReadLE64(const B *ptr)
Definition: common.h:35
const uint64_t k0
Salt.
Definition: hasher.h:21
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:49
const uint64_t k1
Definition: hasher.h:35
const uint64_t m_k1
Definition: hasher.h:92
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:135
size_t operator()(const uint256 &hash) const
Definition: hasher.h:85
256-bit opaque blob.
Definition: uint256.h:201
size_t operator()(const uint256 &hash) const
Definition: hasher.h:56
size_t operator()(const uint256 &txid) const
Definition: hasher.h:26
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:71
SaltedOutpointHasher(bool deterministic=false)
Definition: hasher.cpp:14
const uint64_t k0
Salt.
Definition: hasher.h:35