sigcache.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-present The Bitcoin Core developers
   3  // Distributed under the MIT software license, see the accompanying
   4  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  #ifndef BITCOIN_SCRIPT_SIGCACHE_H
   7  #define BITCOIN_SCRIPT_SIGCACHE_H
   8  
   9  #include <consensus/amount.h>
  10  #include <crypto/sha256.h>
  11  #include <cuckoocache.h>
  12  #include <script/interpreter.h>
  13  #include <uint256.h>
  14  // IWYU incorrectly suggests removing this header.
  15  // See https://github.com/include-what-you-use/include-what-you-use/issues/2014.
  16  #include <util/byte_units.h> // IWYU pragma: keep
  17  #include <util/hasher.h>
  18  
  19  #include <cstddef>
  20  #include <shared_mutex>
  21  #include <span>
  22  #include <vector>
  23  
  24  class CPubKey;
  25  class CTransaction;
  26  class XOnlyPubKey;
  27  
  28  // DoS prevention: limit cache size to 32MiB (over 1000000 entries on 64-bit
  29  // systems). Due to how we count cache size, actual memory usage is slightly
  30  // more (~32.25 MiB)
  31  static constexpr size_t DEFAULT_VALIDATION_CACHE_BYTES{32_MiB};
  32  static constexpr size_t DEFAULT_SIGNATURE_CACHE_BYTES{DEFAULT_VALIDATION_CACHE_BYTES / 2};
  33  static constexpr size_t DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES{DEFAULT_VALIDATION_CACHE_BYTES / 2};
  34  static_assert(DEFAULT_VALIDATION_CACHE_BYTES == DEFAULT_SIGNATURE_CACHE_BYTES + DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES);
  35  
  36  /**
  37   * Valid signature cache, to avoid doing expensive ECDSA signature checking
  38   * twice for every transaction (once when accepted into memory pool, and
  39   * again when accepted into the block chain)
  40   */
  41  class SignatureCache
  42  {
  43  private:
  44      //! Entries are SHA256(nonce || 'E' or 'S' || 31 zero bytes || signature hash || public key || signature):
  45      CSHA256 m_salted_hasher_ecdsa;
  46      CSHA256 m_salted_hasher_schnorr;
  47      typedef CuckooCache::cache<uint256, SignatureCacheHasher> map_type;
  48      map_type setValid;
  49      std::shared_mutex cs_sigcache;
  50  
  51  public:
  52      SignatureCache(size_t max_size_bytes);
  53  
  54      SignatureCache(const SignatureCache&) = delete;
  55      SignatureCache& operator=(const SignatureCache&) = delete;
  56  
  57      void ComputeEntryECDSA(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey) const;
  58  
  59      void ComputeEntrySchnorr(uint256& entry, const uint256 &hash, std::span<const unsigned char> sig, const XOnlyPubKey& pubkey) const;
  60  
  61      bool Get(const uint256& entry, bool erase);
  62  
  63      void Set(const uint256& entry);
  64  };
  65  
  66  class CachingTransactionSignatureChecker : public TransactionSignatureChecker
  67  {
  68  private:
  69      bool store;
  70      SignatureCache& m_signature_cache;
  71  
  72  public:
  73      CachingTransactionSignatureChecker(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)  {}
  74  
  75      bool VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const override;
  76      bool VerifySchnorrSignature(std::span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override;
  77  };
  78  
  79  #endif // BITCOIN_SCRIPT_SIGCACHE_H
  80