19 #include <shared_mutex> 33 CSHA256 m_salted_hasher_schnorr;
36 std::shared_mutex cs_sigcache;
46 static constexpr
unsigned char PADDING_ECDSA[32] = {
'E'};
47 static constexpr
unsigned char PADDING_SCHNORR[32] = {
'S'};
48 m_salted_hasher_ecdsa.
Write(
nonce.begin(), 32);
49 m_salted_hasher_ecdsa.
Write(PADDING_ECDSA, 32);
50 m_salted_hasher_schnorr.
Write(
nonce.begin(), 32);
51 m_salted_hasher_schnorr.
Write(PADDING_SCHNORR, 32);
55 ComputeEntryECDSA(
uint256& entry,
const uint256 &hash,
const std::vector<unsigned char>& vchSig,
const CPubKey& pubkey)
const 57 CSHA256 hasher = m_salted_hasher_ecdsa;
58 hasher.
Write(hash.
begin(), 32).Write(pubkey.
data(), pubkey.
size()).Write(vchSig.data(), vchSig.size()).Finalize(entry.
begin());
64 CSHA256 hasher = m_salted_hasher_schnorr;
69 Get(
const uint256& entry,
const bool erase)
71 std::shared_lock<std::shared_mutex> lock(cs_sigcache);
72 return setValid.contains(entry, erase);
77 std::unique_lock<std::shared_mutex> lock(cs_sigcache);
78 setValid.insert(entry);
80 std::optional<std::pair<uint32_t, size_t>> setup_bytes(
size_t n)
82 return setValid.setup_bytes(n);
92 static CSignatureCache signatureCache;
99 auto setup_results = signatureCache.setup_bytes(max_size_bytes);
100 if (!setup_results)
return false;
102 const auto [num_elems, approx_size_bytes] = *setup_results;
103 LogPrintf(
"Using %zu MiB out of %zu MiB requested for signature cache, able to store %zu elements\n",
104 approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
111 signatureCache.ComputeEntryECDSA(entry,
sighash, vchSig, pubkey);
112 if (signatureCache.Get(entry, !
store))
117 signatureCache.Set(entry);
124 signatureCache.ComputeEntrySchnorr(entry,
sighash, sig, pubkey);
125 if (signatureCache.Get(entry, !
store))
return true;
127 if (
store) signatureCache.Set(entry);
static const std::string sighash
CSHA256 & Write(const unsigned char *data, size_t len)
static constexpr size_t size()
bool VerifySchnorrSignature(Span< const unsigned char > sig, const XOnlyPubKey &pubkey, const uint256 &sighash) const override
virtual bool VerifySchnorrSignature(Span< const unsigned char > sig, const XOnlyPubKey &pubkey, const uint256 &sighash) const
uint256 GetRandHash() noexcept
constexpr std::size_t size() const noexcept
cache implements a cache with properties similar to a cuckoo-set.
constexpr unsigned char * begin()
bool InitSignatureCache(size_t max_size_bytes)
virtual bool VerifyECDSASignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
An encapsulated public key.
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
constexpr C * data() const noexcept
bool VerifyECDSASignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
const unsigned char * data() const
A hasher class for SHA-256.
const unsigned char * data() const