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 private:
37  const uint64_t k0, k1;
38 
39 public:
41 
52  size_t operator()(const COutPoint &outpoint) const noexcept {
53  return SipHashUint256Extra(k0, k1, outpoint.GetTxId(), outpoint.GetN());
54  }
55 };
56 
58  size_t operator()(const uint256 &hash) const {
59  return ReadLE64(hash.begin());
60  }
61 };
62 
72 public:
73  template <uint8_t hash_select>
74  uint32_t operator()(const uint256 &key) const {
75  static_assert(hash_select < 8,
76  "SignatureCacheHasher only has 8 hashes available.");
77  uint32_t u;
78  std::memcpy(&u, key.begin() + 4 * hash_select, 4);
79  return u;
80  }
81 };
82 
86 struct BlockHasher {
87  // this used to call `GetCheapHash()` in uint256, which was later moved; the
88  // cheap hash function simply calls ReadLE64() however, so the end result is
89  // identical
90  size_t operator()(const BlockHash &hash) const {
91  return ReadLE64(hash.begin());
92  }
93 };
94 
96 private:
98  const uint64_t m_k0, m_k1;
99 
100 public:
101  SaltedSipHasher();
102 
103  size_t operator()(const Span<const uint8_t> &script) const;
104 };
105 
107  size_t operator()(const CTxDestination &dest) const {
109  }
110 };
111 
112 #endif // BITCOIN_UTIL_HASHER_H
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
const uint64_t k0
Salt.
Definition: hasher.h:37
const uint64_t k1
Definition: hasher.h:37
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:52
size_t operator()(const Span< const uint8_t > &script) const
Definition: hasher.cpp:22
const uint64_t m_k0
Salt.
Definition: hasher.h:98
const uint64_t m_k1
Definition: hasher.h:98
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:71
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:74
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
uint8_t * begin()
Definition: uint256.h:83
256-bit opaque blob.
Definition: uint256.h:127
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:243
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:93
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:86
size_t operator()(const BlockHash &hash) const
Definition: hasher.h:90
size_t operator()(const uint256 &hash) const
Definition: hasher.h:58
size_t operator()(const CTxDestination &dest) const
Definition: hasher.h:107
A TxId is the identifier of a transaction.
Definition: txid.h:14