Bitcoin ABC  0.26.3
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>
9 #include <primitives/blockhash.h>
10 #include <primitives/transaction.h>
11 #include <primitives/txid.h>
12 #include <script/standard.h>
13 #include <uint256.h>
14 
16 private:
18  const uint64_t k0, k1;
19 
20 public:
22 
23  size_t hash(const uint256 &h) const { return SipHashUint256(k0, k1, h); }
24  size_t operator()(const uint256 &h) const { return hash(h); }
25 };
26 
28 public:
30 
31  size_t operator()(const TxId &txid) const { return hash(txid); }
32 };
33 
35 public:
37 
38  size_t operator()(const BlockHash &block_hash) const {
39  return hash(block_hash);
40  }
41 };
42 
44 private:
46  const uint64_t k0, k1;
47 
48 public:
50 
61  size_t operator()(const COutPoint &outpoint) const noexcept {
62  return SipHashUint256Extra(k0, k1, outpoint.GetTxId(), outpoint.GetN());
63  }
64 };
65 
67  size_t operator()(const uint256 &hash) const {
68  return ReadLE64(hash.begin());
69  }
70 };
71 
81 public:
82  template <uint8_t hash_select>
83  uint32_t operator()(const uint256 &key) const {
84  static_assert(hash_select < 8,
85  "SignatureCacheHasher only has 8 hashes available.");
86  uint32_t u;
87  std::memcpy(&u, key.begin() + 4 * hash_select, 4);
88  return u;
89  }
90 };
91 
95 struct BlockHasher {
96  // this used to call `GetCheapHash()` in uint256, which was later moved; the
97  // cheap hash function simply calls ReadLE64() however, so the end result is
98  // identical
99  size_t operator()(const BlockHash &hash) const {
100  return ReadLE64(hash.begin());
101  }
102 };
103 
105 private:
107  const uint64_t m_k0, m_k1;
108 
109 public:
110  SaltedSipHasher();
111 
112  size_t operator()(const Span<const uint8_t> &script) const;
113 };
114 
116  size_t operator()(const CTxDestination &dest) const {
118  }
119 };
120 
121 #endif // BITCOIN_UTIL_HASHER_H
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
size_t operator()(const BlockHash &block_hash) const
Definition: hasher.h:38
const uint64_t k0
Salt.
Definition: hasher.h:46
const uint64_t k1
Definition: hasher.h:46
size_t operator()(const COutPoint &outpoint) const noexcept
Having the hash noexcept allows libstdc++'s unordered_map to recalculate the hash during rehash,...
Definition: hasher.h:61
size_t operator()(const Span< const uint8_t > &script) const
Definition: hasher.cpp:19
const uint64_t m_k0
Salt.
Definition: hasher.h:107
const uint64_t m_k1
Definition: hasher.h:107
SaltedTxIdHasher()
Definition: hasher.h:29
size_t operator()(const TxId &txid) const
Definition: hasher.h:31
size_t hash(const uint256 &h) const
Definition: hasher.h:23
size_t operator()(const uint256 &h) const
Definition: hasher.h:24
const uint64_t k0
Salt.
Definition: hasher.h:18
const uint64_t k1
Definition: hasher.h:18
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:80
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:83
uint8_t * begin()
Definition: uint256.h:85
256-bit opaque blob.
Definition: uint256.h:129
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:138
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:99
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:240
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Maintain a map of CBlockIndex for all known headers.
Definition: hasher.h:95
size_t operator()(const BlockHash &hash) const
Definition: hasher.h:99
size_t operator()(const uint256 &hash) const
Definition: hasher.h:67
size_t operator()(const CTxDestination &dest) const
Definition: hasher.h:116
A TxId is the identifier of a transaction.
Definition: txid.h:14