Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
script
sigcache.h
Go to the documentation of this file.
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
#include <
util/byte_units.h
>
15
#include <
util/hasher.h
>
16
17
#include <cstddef>
18
#include <shared_mutex>
19
#include <span>
20
#include <vector>
21
22
class
CPubKey
;
23
class
CTransaction
;
24
class
XOnlyPubKey
;
25
26
// DoS prevention: limit cache size to 32MiB (over 1000000 entries on 64-bit
27
// systems). Due to how we count cache size, actual memory usage is slightly
28
// more (~32.25 MiB)
29
inline
constexpr
size_t
DEFAULT_VALIDATION_CACHE_BYTES
{32_MiB};
30
inline
constexpr
size_t
DEFAULT_SIGNATURE_CACHE_BYTES
{
DEFAULT_VALIDATION_CACHE_BYTES
/ 2};
31
inline
constexpr
size_t
DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES
{
DEFAULT_VALIDATION_CACHE_BYTES
/ 2};
32
static_assert
(
DEFAULT_VALIDATION_CACHE_BYTES
==
DEFAULT_SIGNATURE_CACHE_BYTES
+
DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES
);
33
39
class
SignatureCache
40
{
41
private
:
43
CSHA256
m_salted_hasher_ecdsa
;
44
CSHA256
m_salted_hasher_schnorr
;
45
typedef
CuckooCache::cache<uint256, SignatureCacheHasher>
map_type
;
46
map_type
setValid
;
47
std::shared_mutex
cs_sigcache
;
48
49
public
:
50
SignatureCache
(
size_t
max_size_bytes);
51
52
SignatureCache
(
const
SignatureCache
&) =
delete
;
53
SignatureCache
&
operator=
(
const
SignatureCache
&) =
delete
;
54
55
void
ComputeEntryECDSA
(
uint256
& entry,
const
uint256
&hash,
const
std::vector<unsigned char>& vchSig,
const
CPubKey
& pubkey)
const
;
56
57
void
ComputeEntrySchnorr
(
uint256
& entry,
const
uint256
&hash, std::span<const unsigned char> sig,
const
XOnlyPubKey
& pubkey)
const
;
58
59
bool
Get
(
const
uint256
& entry,
bool
erase);
60
61
void
Set
(
const
uint256
& entry);
62
};
63
64
class
CachingTransactionSignatureChecker
:
public
TransactionSignatureChecker
65
{
66
private
:
67
bool
store
;
68
SignatureCache
&
m_signature_cache
;
69
70
public
:
71
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) {}
72
73
bool
VerifyECDSASignature
(
const
std::vector<unsigned char>& vchSig,
const
CPubKey
& vchPubKey,
const
uint256
& sighash)
const override
;
74
bool
VerifySchnorrSignature
(std::span<const unsigned char> sig,
const
XOnlyPubKey
& pubkey,
const
uint256
& sighash)
const override
;
75
};
76
77
#endif
// BITCOIN_SCRIPT_SIGCACHE_H
amount.h
CAmount
int64_t CAmount
Amount in satoshis (Can be negative).
Definition
amount.h:12
byte_units.h
CPubKey
An encapsulated public key.
Definition
pubkey.h:40
CSHA256
A hasher class for SHA-256.
Definition
sha256.h:14
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition
transaction.h:287
CachingTransactionSignatureChecker::store
bool store
Definition
sigcache.h:67
CachingTransactionSignatureChecker::CachingTransactionSignatureChecker
CachingTransactionSignatureChecker(const CTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, bool storeIn, SignatureCache &signature_cache, PrecomputedTransactionData &txdataIn)
Definition
sigcache.h:71
CachingTransactionSignatureChecker::VerifySchnorrSignature
bool VerifySchnorrSignature(std::span< const unsigned char > sig, const XOnlyPubKey &pubkey, const uint256 &sighash) const override
Definition
sigcache.cpp:76
CachingTransactionSignatureChecker::m_signature_cache
SignatureCache & m_signature_cache
Definition
sigcache.h:68
CachingTransactionSignatureChecker::VerifyECDSASignature
bool VerifyECDSASignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition
sigcache.cpp:63
CuckooCache::cache
cache implements a cache with properties similar to a cuckoo-set.
Definition
cuckoocache.h:163
SignatureCache
Valid signature cache, to avoid doing expensive ECDSA signature checking twice for every transaction ...
Definition
sigcache.h:40
SignatureCache::SignatureCache
SignatureCache(size_t max_size_bytes)
Definition
sigcache.cpp:20
SignatureCache::ComputeEntrySchnorr
void ComputeEntrySchnorr(uint256 &entry, const uint256 &hash, std::span< const unsigned char > sig, const XOnlyPubKey &pubkey) const
Definition
sigcache.cpp:45
SignatureCache::m_salted_hasher_schnorr
CSHA256 m_salted_hasher_schnorr
Definition
sigcache.h:44
SignatureCache::ComputeEntryECDSA
void ComputeEntryECDSA(uint256 &entry, const uint256 &hash, const std::vector< unsigned char > &vchSig, const CPubKey &pubkey) const
Definition
sigcache.cpp:39
SignatureCache::setValid
map_type setValid
Definition
sigcache.h:46
SignatureCache::cs_sigcache
std::shared_mutex cs_sigcache
Definition
sigcache.h:47
SignatureCache::Get
bool Get(const uint256 &entry, bool erase)
Definition
sigcache.cpp:51
SignatureCache::operator=
SignatureCache & operator=(const SignatureCache &)=delete
SignatureCache::SignatureCache
SignatureCache(const SignatureCache &)=delete
SignatureCache::m_salted_hasher_ecdsa
CSHA256 m_salted_hasher_ecdsa
Entries are SHA256(nonce || 'E' or 'S' || 31 zero bytes || signature hash || public key || signature)...
Definition
sigcache.h:43
SignatureCache::map_type
CuckooCache::cache< uint256, SignatureCacheHasher > map_type
Definition
sigcache.h:45
SignatureCache::Set
void Set(const uint256 &entry)
Definition
sigcache.cpp:57
XOnlyPubKey
Definition
pubkey.h:233
uint256
256-bit opaque blob.
Definition
uint256.h:196
cuckoocache.h
hasher.h
interpreter.h
TransactionSignatureChecker
GenericTransactionSignatureChecker< CTransaction > TransactionSignatureChecker
Definition
interpreter.h:337
MissingDataBehavior
MissingDataBehavior
Enum to specify what *TransactionSignatureChecker's behavior should be when dealing with missing tran...
Definition
interpreter.h:305
MissingDataBehavior::ASSERT_FAIL
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code).
Definition
interpreter.h:306
sha256.h
DEFAULT_SIGNATURE_CACHE_BYTES
constexpr size_t DEFAULT_SIGNATURE_CACHE_BYTES
Definition
sigcache.h:30
DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES
constexpr size_t DEFAULT_SCRIPT_EXECUTION_CACHE_BYTES
Definition
sigcache.h:31
DEFAULT_VALIDATION_CACHE_BYTES
constexpr size_t DEFAULT_VALIDATION_CACHE_BYTES
Definition
sigcache.h:29
PrecomputedTransactionData
Definition
interpreter.h:165
uint256.h
Generated on
for Bitcoin Core by
1.18.0