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) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_SCRIPT_SIGCACHE_H7#define BITCOIN_SCRIPT_SIGCACHE_H9#include <consensus/amount.h>10#include <crypto/sha256.h>11#include <cuckoocache.h>12#include <script/interpreter.h>13#include <uint256.h>14#include <util/byte_units.h>15#include <util/hasher.h>17#include <cstddef>18#include <shared_mutex>19#include <span>20#include <vector>22class CPubKey;23class CTransaction;24class XOnlyPubKey;26// DoS prevention: limit cache size to 32MiB (over 1000000 entries on 64-bit27// systems). Due to how we count cache size, actual memory usage is slightly28// more (~32.25 MiB)29inline constexpr size_t DEFAULT_VALIDATION_CACHE_BYTES{32_MiB};30inline constexpr size_t DEFAULT_SIGNATURE_CACHE_BYTES{DEFAULT_VALIDATION_CACHE_BYTES / 2};31inline constexpr size_t DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES{DEFAULT_VALIDATION_CACHE_BYTES / 2};32static_assert(DEFAULT_VALIDATION_CACHE_BYTES == DEFAULT_SIGNATURE_CACHE_BYTES + DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES);34/**35* Valid signature cache, to avoid doing expensive ECDSA signature checking36* twice for every transaction (once when accepted into memory pool, and37* again when accepted into the block chain)38*/39class SignatureCache40{41private:42//! Entries are SHA256(nonce || 'E' or 'S' || 31 zero bytes || signature hash || public key || signature):43CSHA256 m_salted_hasher_ecdsa;44CSHA256 m_salted_hasher_schnorr;45typedef CuckooCache::cache<uint256, SignatureCacheHasher> map_type;46map_type setValid;47std::shared_mutex cs_sigcache;49public:50SignatureCache(size_t max_size_bytes);52SignatureCache(const SignatureCache&) = delete;53SignatureCache& operator=(const SignatureCache&) = delete;55void ComputeEntryECDSA(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey) const;57void ComputeEntrySchnorr(uint256& entry, const uint256 &hash, std::span<const unsigned char> sig, const XOnlyPubKey& pubkey) const;59bool Get(const uint256& entry, bool erase);61void Set(const uint256& entry);62};64class CachingTransactionSignatureChecker : public TransactionSignatureChecker65{66private:67bool store;68SignatureCache& m_signature_cache;70public:71CachingTransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, bool storeIn, SignatureCache& signature_cache, PrecomputedTransactionData& txdataIn) : TransactionSignatureChecker(txToIn, nInIn, amountIn, txdataIn, MissingDataBehavior::ASSERT_FAIL), store(storeIn), m_signature_cache(signature_cache) {}73bool VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const override;74bool VerifySchnorrSignature(std::span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override;75};77#endif // BITCOIN_SCRIPT_SIGCACHE_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.