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{
14private:
16 const uint64_t k0, k1;
17
18public:
20
21 size_t operator()(const uint256& txid) const {
22 return SipHashUint256(k0, k1, txid);
23 }
24};
25
27{
28private:
30 const uint64_t k0, k1;
31
32public:
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{
64public:
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{
85private:
87 const uint64_t m_k0, m_k1;
88
89public:
91
92 size_t operator()(const Span<const unsigned char>& script) const;
93};
94
95#endif // BITCOIN_UTIL_HASHER_H
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:27
const uint64_t k0
Salt.
Definition: hasher.h:30
const uint64_t k1
Definition: hasher.h:30
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
size_t operator()(const Span< const unsigned char > &script) const
Definition: hasher.cpp:16
const uint64_t m_k0
Salt.
Definition: hasher.h:87
const uint64_t m_k1
Definition: hasher.h:87
const uint64_t k1
Definition: hasher.h:16
size_t operator()(const uint256 &txid) const
Definition: hasher.h:21
const uint64_t k0
Salt.
Definition: hasher.h:16
We're hashing a nonce into the entries themselves, so we don't need extra blinding in the set hash co...
Definition: hasher.h:63
uint32_t operator()(const uint256 &key) const
Definition: hasher.h:66
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
unsigned char * begin()
Definition: uint256.h:58
256-bit opaque blob.
Definition: uint256.h:124
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:134
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:94
size_t operator()(const uint256 &hash) const
Definition: hasher.h:80
size_t operator()(const uint256 &hash) const
Definition: hasher.h:51