Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_HASHER_H6#define BITCOIN_UTIL_HASHER_H8#include <crypto/common.h>9#include <crypto/siphash.h>10#include <primitives/transaction.h>11#include <uint256.h>13#include <cstdint>14#include <cstring>15#include <span>17class SaltedUint256Hasher18{19const PresaltedSipHasher m_hasher;21public:22SaltedUint256Hasher();24size_t operator()(const uint256& hash) const25{26return m_hasher(hash);27}28};30class SaltedTxidHasher31{32const PresaltedSipHasher m_hasher;34public:35SaltedTxidHasher();37size_t operator()(const Txid& txid) const38{39return m_hasher(txid.ToUint256());40}41};43class SaltedWtxidHasher44{45const PresaltedSipHasher m_hasher;47public:48SaltedWtxidHasher();50size_t operator()(const Wtxid& wtxid) const51{52return m_hasher(wtxid.ToUint256());53}54};56class SaltedOutpointHasher57{58const PresaltedSipHasher m_hasher;60public:61SaltedOutpointHasher();63/**64* Having the hash noexcept allows libstdc++'s unordered_map to recalculate65* the hash during rehash, so it does not have to cache the value. This66* reduces node's memory by sizeof(size_t). The required recalculation has67* a slight performance penalty (around 1.6%), but this is compensated by68* memory savings of about 9% which allow for a larger dbcache setting.69*70* @see https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/manual/manual/unordered_associative.html71*/72size_t operator()(const COutPoint& id) const noexcept73{74return m_hasher(id.hash.ToUint256(), id.n);75}76};78/**79* We're hashing a nonce into the entries themselves, so we don't need extra80* blinding in the set hash computation.81*82* This may exhibit platform endian dependent behavior but because these are83* nonced hashes (random) and this state is only ever used locally it is safe.84* All that matters is local consistency.85*/86class SignatureCacheHasher87{88public:89template <uint8_t hash_select>90uint32_t operator()(const uint256& key) const91{92static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");93uint32_t u;94std::memcpy(&u, key.begin()+4*hash_select, 4);95return u;96}97};99struct BlockHasher100{101// this used to call `GetCheapHash()` in uint256, which was later moved; the102// cheap hash function simply calls ReadLE64() however, so the end result is103// identical104size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }105};107class SaltedSipHasher108{109private:110/** Salt */111const uint64_t m_k0, m_k1;113public:114SaltedSipHasher();116size_t operator()(const std::span<const unsigned char>& script) const;117};119#endif // BITCOIN_UTIL_HASHER_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.