LCOV - code coverage report
Current view: top level - src/script - sigcache.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 93.2 % 44 41
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 7 7
Branches: 52.8 % 36 19

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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                 :             : #include <script/sigcache.h>
       7                 :             : 
       8                 :             : #include <crypto/sha256.h>
       9                 :             : #include <logging.h>
      10                 :             : #include <pubkey.h>
      11                 :             : #include <random.h>
      12                 :             : #include <script/interpreter.h>
      13                 :             : #include <span.h>
      14                 :             : #include <uint256.h>
      15                 :             : 
      16                 :             : #include <mutex>
      17                 :             : #include <shared_mutex>
      18                 :             : #include <vector>
      19                 :             : 
      20                 :        2961 : SignatureCache::SignatureCache(const size_t max_size_bytes)
      21                 :             : {
      22                 :        2961 :     uint256 nonce = GetRandHash();
      23                 :             :     // We want the nonce to be 64 bytes long to force the hasher to process
      24                 :             :     // this chunk, which makes later hash computations more efficient. We
      25                 :             :     // just write our 32-byte entropy, and then pad with 'E' for ECDSA and
      26                 :             :     // 'S' for Schnorr (followed by 0 bytes).
      27                 :             :     static constexpr unsigned char PADDING_ECDSA[32] = {'E'};
      28                 :             :     static constexpr unsigned char PADDING_SCHNORR[32] = {'S'};
      29   [ +  -  +  - ]:        2961 :     m_salted_hasher_ecdsa.Write(nonce.begin(), 32);
      30         [ +  - ]:        2961 :     m_salted_hasher_ecdsa.Write(PADDING_ECDSA, 32);
      31   [ +  -  +  - ]:        2961 :     m_salted_hasher_schnorr.Write(nonce.begin(), 32);
      32         [ +  - ]:        2961 :     m_salted_hasher_schnorr.Write(PADDING_SCHNORR, 32);
      33                 :             : 
      34         [ +  - ]:        2961 :     const auto [num_elems, approx_size_bytes] = setValid.setup_bytes(max_size_bytes);
      35   [ +  -  +  -  :        8883 :     LogPrintf("Using %zu MiB out of %zu MiB requested for signature cache, able to store %zu elements\n",
                   +  - ]
      36                 :             :               approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
      37                 :        2961 : }
      38                 :             : 
      39                 :         147 : void SignatureCache::ComputeEntryECDSA(uint256& entry, const uint256& hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey) const
      40                 :             : {
      41                 :         147 :     CSHA256 hasher = m_salted_hasher_ecdsa;
      42                 :         147 :     hasher.Write(hash.begin(), 32).Write(pubkey.data(), pubkey.size()).Write(vchSig.data(), vchSig.size()).Finalize(entry.begin());
      43                 :         147 : }
      44                 :             : 
      45                 :          79 : void SignatureCache::ComputeEntrySchnorr(uint256& entry, const uint256& hash, Span<const unsigned char> sig, const XOnlyPubKey& pubkey) const
      46                 :             : {
      47                 :          79 :     CSHA256 hasher = m_salted_hasher_schnorr;
      48                 :          79 :     hasher.Write(hash.begin(), 32).Write(pubkey.data(), pubkey.size()).Write(sig.data(), sig.size()).Finalize(entry.begin());
      49                 :          79 : }
      50                 :             : 
      51                 :         226 : bool SignatureCache::Get(const uint256& entry, const bool erase)
      52                 :             : {
      53                 :         226 :     std::shared_lock<std::shared_mutex> lock(cs_sigcache);
      54         [ +  - ]:         226 :     return setValid.contains(entry, erase);
      55                 :         226 : }
      56                 :             : 
      57                 :           1 : void SignatureCache::Set(const uint256& entry)
      58                 :             : {
      59                 :           1 :     std::unique_lock<std::shared_mutex> lock(cs_sigcache);
      60         [ +  - ]:           1 :     setValid.insert(entry);
      61                 :           1 : }
      62                 :             : 
      63                 :         147 : bool CachingTransactionSignatureChecker::VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
      64                 :             : {
      65                 :         147 :     uint256 entry;
      66                 :         147 :     m_signature_cache.ComputeEntryECDSA(entry, sighash, vchSig, pubkey);
      67         [ -  + ]:         147 :     if (m_signature_cache.Get(entry, !store))
      68                 :           0 :         return true;
      69         [ +  + ]:         147 :     if (!TransactionSignatureChecker::VerifyECDSASignature(vchSig, pubkey, sighash))
      70                 :         140 :         return false;
      71         [ +  + ]:           7 :     if (store)
      72                 :           1 :         m_signature_cache.Set(entry);
      73                 :           7 :     return true;
      74                 :         147 : }
      75                 :             : 
      76                 :          79 : bool CachingTransactionSignatureChecker::VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const
      77                 :             : {
      78                 :          79 :     uint256 entry;
      79                 :          79 :     m_signature_cache.ComputeEntrySchnorr(entry, sighash, sig, pubkey);
      80         [ -  + ]:          79 :     if (m_signature_cache.Get(entry, !store)) return true;
      81         [ -  + ]:          79 :     if (!TransactionSignatureChecker::VerifySchnorrSignature(sig, pubkey, sighash)) return false;
      82         [ #  # ]:           0 :     if (store) m_signature_cache.Set(entry);
      83                 :           0 :     return true;
      84                 :          79 : }
        

Generated by: LCOV version 2.0-1