17 #include <shared_mutex> 31 CSHA256 m_salted_hasher_schnorr;
34 std::shared_mutex cs_sigcache;
44 static constexpr
unsigned char PADDING_ECDSA[32] = {
'E'};
45 static constexpr
unsigned char PADDING_SCHNORR[32] = {
'S'};
46 m_salted_hasher_ecdsa.
Write(nonce.
begin(), 32);
47 m_salted_hasher_ecdsa.
Write(PADDING_ECDSA, 32);
48 m_salted_hasher_schnorr.
Write(nonce.
begin(), 32);
49 m_salted_hasher_schnorr.
Write(PADDING_SCHNORR, 32);
53 ComputeEntryECDSA(
uint256& entry,
const uint256 &hash,
const std::vector<unsigned char>& vchSig,
const CPubKey& pubkey)
const 55 CSHA256 hasher = m_salted_hasher_ecdsa;
56 hasher.
Write(hash.
begin(), 32).Write(pubkey.
data(), pubkey.
size()).Write(vchSig.data(), vchSig.size()).Finalize(entry.
begin());
62 CSHA256 hasher = m_salted_hasher_schnorr;
67 Get(
const uint256& entry,
const bool erase)
69 std::shared_lock<std::shared_mutex> lock(cs_sigcache);
70 return setValid.contains(entry, erase);
75 std::unique_lock<std::shared_mutex> lock(cs_sigcache);
76 setValid.insert(entry);
78 uint32_t setup_bytes(
size_t n)
80 return setValid.setup_bytes(n);
90 static CSignatureCache signatureCache;
100 size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
101 LogPrintf(
"Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
102 (nElems*
sizeof(
uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
108 signatureCache.ComputeEntryECDSA(entry, sighash, vchSig, pubkey);
109 if (signatureCache.Get(entry, !
store))
114 signatureCache.Set(entry);
121 signatureCache.ComputeEntrySchnorr(entry, sighash, sig, pubkey);
122 if (signatureCache.Get(entry, !
store))
return true;
124 if (
store) signatureCache.Set(entry);
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
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE
uint256 GetRandHash() noexcept
constexpr std::size_t size() const noexcept
cache implements a cache with properties similar to a cuckoo-set.
virtual bool VerifyECDSASignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
An encapsulated public key.
void InitSignatureCache()
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
static const int64_t MAX_MAX_SIG_CACHE_SIZE
constexpr C * data() const noexcept
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool VerifyECDSASignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
const unsigned char * data() const
A Span is an object that can refer to a contiguous sequence of objects.
A hasher class for SHA-256.
const unsigned char * data() const