Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
script_sigcache.cpp
Go to the documentation of this file.
1
// Copyright (c) 2020-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <
consensus/amount.h
>
6
#include <
primitives/transaction.h
>
7
#include <
pubkey.h
>
8
#include <
script/interpreter.h
>
9
#include <
script/sigcache.h
>
10
#include <
span.h
>
11
#include <
test/fuzz/FuzzedDataProvider.h
>
12
#include <
test/fuzz/fuzz.h
>
13
#include <
test/fuzz/util.h
>
14
#include <
test/util/setup_common.h
>
15
#include <
uint256.h
>
16
17
#include <cstddef>
18
#include <optional>
19
#include <vector>
20
21
void
initialize_script_sigcache
()
22
{
23
static
const
auto
testing_setup =
MakeNoLogFileContext<>
();
24
}
25
26
FUZZ_TARGET
(script_sigcache, .
init
=
initialize_script_sigcache
)
27
{
28
SeedRandomStateForTest
(
SeedRand::ZEROS
);
29
FuzzedDataProvider
fuzzed_data_provider(buffer.data(), buffer.size());
30
31
const
auto
max_sigcache_bytes{fuzzed_data_provider.
ConsumeIntegralInRange
<
size_t
>(0,
DEFAULT_SIGNATURE_CACHE_BYTES
)};
32
SignatureCache
signature_cache{max_sigcache_bytes};
33
34
const
std::optional<CMutableTransaction> mutable_transaction =
ConsumeDeserializable<CMutableTransaction>
(fuzzed_data_provider,
TX_WITH_WITNESS
);
35
const
CTransaction
tx{mutable_transaction ? *mutable_transaction :
CMutableTransaction
{}};
36
const
unsigned
int
n_in = fuzzed_data_provider.
ConsumeIntegral
<
unsigned
int
>();
37
const
CAmount
amount =
ConsumeMoney
(fuzzed_data_provider);
38
const
bool
store = fuzzed_data_provider.
ConsumeBool
();
39
PrecomputedTransactionData
tx_data;
40
CachingTransactionSignatureChecker
caching_transaction_signature_checker{mutable_transaction ? &tx :
nullptr
, n_in, amount, store, signature_cache, tx_data};
41
if
(fuzzed_data_provider.
ConsumeBool
()) {
42
const
auto
random_bytes = fuzzed_data_provider.
ConsumeBytes
<
unsigned
char
>(64);
43
const
XOnlyPubKey
pub_key(
ConsumeUInt256
(fuzzed_data_provider));
44
if
(random_bytes.size() == 64) {
45
(void)caching_transaction_signature_checker.
VerifySchnorrSignature
(random_bytes, pub_key,
ConsumeUInt256
(fuzzed_data_provider));
46
}
47
}
else
{
48
const
auto
random_bytes =
ConsumeRandomLengthByteVector
(fuzzed_data_provider);
49
const
auto
pub_key =
ConsumeDeserializable<CPubKey>
(fuzzed_data_provider);
50
if
(pub_key) {
51
if
(!random_bytes.empty()) {
52
(void)caching_transaction_signature_checker.
VerifyECDSASignature
(random_bytes, *pub_key,
ConsumeUInt256
(fuzzed_data_provider));
53
}
54
}
55
}
56
}
FuzzedDataProvider.h
amount.h
CAmount
int64_t CAmount
Amount in satoshis (Can be negative).
Definition
amount.h:12
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition
transaction.h:281
CachingTransactionSignatureChecker
Definition
sigcache.h:64
CachingTransactionSignatureChecker::VerifySchnorrSignature
bool VerifySchnorrSignature(std::span< const unsigned char > sig, const XOnlyPubKey &pubkey, const uint256 &sighash) const override
Definition
sigcache.cpp:76
CachingTransactionSignatureChecker::VerifyECDSASignature
bool VerifyECDSASignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const override
Definition
sigcache.cpp:63
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeBytes
std::vector< T > ConsumeBytes(size_t num_bytes)
Definition
FuzzedDataProvider.h:109
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition
FuzzedDataProvider.h:289
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition
FuzzedDataProvider.h:205
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition
FuzzedDataProvider.h:195
SignatureCache
Valid signature cache, to avoid doing expensive ECDSA signature checking twice for every transaction ...
Definition
sigcache.h:39
XOnlyPubKey
Definition
pubkey.h:227
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
interpreter.h
init
Definition
bitcoin-gui.cpp:17
transaction.h
TX_WITH_WITNESS
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition
transaction.h:180
pubkey.h
initialize_script_sigcache
void initialize_script_sigcache()
Definition
script_sigcache.cpp:21
setup_common.h
MakeNoLogFileContext
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
Definition
setup_common.h:250
sigcache.h
DEFAULT_SIGNATURE_CACHE_BYTES
static constexpr size_t DEFAULT_SIGNATURE_CACHE_BYTES
Definition
sigcache.h:29
span.h
CMutableTransaction
A mutable version of CTransaction.
Definition
transaction.h:358
PrecomputedTransactionData
Definition
interpreter.h:164
ConsumeMoney
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition
util.cpp:29
util.h
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition
util.h:167
ConsumeRandomLengthByteVector
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition
util.h:57
ConsumeDeserializable
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P ¶ms, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition
util.h:100
SeedRandomStateForTest
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition
random.cpp:19
SeedRand::ZEROS
@ ZEROS
Seed with a compile time constant of zeros.
Definition
random.h:19
uint256.h
Generated on
for Bitcoin Core by
1.17.0