Bitcoin Core  22.99.0
P2P Digital Currency
hasher.h
Go to the documentation of this file.
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>
10 #include <uint256.h>
11 
13 {
14 private:
16  const uint64_t k0, k1;
17 
18 public:
20 
21  size_t operator()(const uint256& txid) const {
22  return SipHashUint256(k0, k1, txid);
23  }
24 };
25 
27 {
28 private:
30  const uint64_t k0, k1;
31 
32 public:
34 
44  size_t operator()(const COutPoint& id) const noexcept {
45  return SipHashUint256Extra(k0, k1, id.hash, id.n);
46  }
47 };
48 
50 {
51  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
52 };
53 
63 {
64 public:
65  template <uint8_t hash_select>
66  uint32_t operator()(const uint256& key) const
67  {
68  static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
69  uint32_t u;
70  std::memcpy(&u, key.begin()+4*hash_select, 4);
71  return u;
72  }
73 };
74 
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  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
81 };
82 
84 {
85 private:
87  const uint64_t m_k0, m_k1;
88 
89 public:
91 
92  size_t operator()(const Span<const unsigned char>& script) const;
93 };
94 
95 #endif // BITCOIN_UTIL_HASHER_H
ReadLE64
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
SaltedSipHasher::operator()
size_t operator()(const Span< const unsigned char > &script) const
Definition: hasher.cpp:16
SignatureCacheHasher
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:62
FilterHeaderHasher::operator()
size_t operator()(const uint256 &hash) const
Definition: hasher.h:51
SaltedOutpointHasher::k0
const uint64_t k0
Salt.
Definition: hasher.h:30
transaction.h
uint256.h
SaltedTxidHasher::k1
const uint64_t k1
Definition: hasher.h:16
SaltedOutpointHasher::SaltedOutpointHasher
SaltedOutpointHasher()
Definition: hasher.cpp:12
BlockHasher
Definition: hasher.h:75
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
SaltedOutpointHasher::operator()
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:44
siphash.h
SipHashUint256Extra
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:134
SipHashUint256
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:94
SaltedTxidHasher
Definition: hasher.h:12
SaltedOutpointHasher::k1
const uint64_t k1
Definition: hasher.h:30
BlockHasher::operator()
size_t operator()(const uint256 &hash) const
Definition: hasher.h:80
uint256
256-bit opaque blob.
Definition: uint256.h:124
SaltedSipHasher::SaltedSipHasher
SaltedSipHasher()
Definition: hasher.cpp:14
SaltedSipHasher
Definition: hasher.h:83
SaltedOutpointHasher
Definition: hasher.h:26
SignatureCacheHasher::operator()
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:66
SaltedTxidHasher::k0
const uint64_t k0
Salt.
Definition: hasher.h:16
SaltedTxidHasher::SaltedTxidHasher
SaltedTxidHasher()
Definition: hasher.cpp:10
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
SaltedSipHasher::m_k0
const uint64_t m_k0
Salt.
Definition: hasher.h:87
SaltedTxidHasher::operator()
size_t operator()(const uint256 &txid) const
Definition: hasher.h:21
base_blob::begin
unsigned char * begin()
Definition: uint256.h:58
FilterHeaderHasher
Definition: hasher.h:49
SaltedSipHasher::m_k1
const uint64_t m_k1
Definition: hasher.h:87