Bitcoin Core  22.99.0
P2P Digital Currency
script_sigcache.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <chainparams.h>
6 #include <chainparamsbase.h>
7 #include <key.h>
8 #include <pubkey.h>
9 #include <script/sigcache.h>
11 #include <test/fuzz/fuzz.h>
12 #include <test/fuzz/util.h>
13 
14 #include <cstdint>
15 #include <optional>
16 #include <string>
17 #include <vector>
18 
20 {
21  static const ECCVerifyHandle ecc_verify_handle;
22  ECC_Start();
25 }
26 
28 {
29  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
30 
31  const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
32  const CTransaction tx{mutable_transaction ? *mutable_transaction : CMutableTransaction{}};
33  const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
34  const CAmount amount = ConsumeMoney(fuzzed_data_provider);
35  const bool store = fuzzed_data_provider.ConsumeBool();
37  CachingTransactionSignatureChecker caching_transaction_signature_checker{mutable_transaction ? &tx : nullptr, n_in, amount, store, tx_data};
38  if (fuzzed_data_provider.ConsumeBool()) {
39  const auto random_bytes = fuzzed_data_provider.ConsumeBytes<unsigned char>(64);
40  const XOnlyPubKey pub_key(ConsumeUInt256(fuzzed_data_provider));
41  if (random_bytes.size() == 64) {
42  (void)caching_transaction_signature_checker.VerifySchnorrSignature(random_bytes, pub_key, ConsumeUInt256(fuzzed_data_provider));
43  }
44  } else {
45  const auto random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
46  const auto pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
47  if (pub_key) {
48  if (!random_bytes.empty()) {
49  (void)caching_transaction_signature_checker.VerifyECDSASignature(random_bytes, *pub_key, ConsumeUInt256(fuzzed_data_provider));
50  }
51  }
52  }
53 }
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
initialize_script_sigcache
void initialize_script_sigcache()
Definition: script_sigcache.cpp:19
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(script_sigcache, initialize_script_sigcache)
Definition: script_sigcache.cpp:27
chainparamsbase.h
util.h
XOnlyPubKey
Definition: pubkey.h:220
pubkey.h
chainparams.h
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
CachingTransactionSignatureChecker
Definition: sigcache.h:24
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
FuzzedDataProvider.h
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:555
PrecomputedTransactionData
Definition: interpreter.h:150
InitSignatureCache
void InitSignatureCache()
Definition: sigcache.cpp:95
ECC_Start
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:370
key.h
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:315
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
sigcache.h
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
ConsumeMoney
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:221
FuzzedDataProvider::ConsumeBytes
std::vector< T > ConsumeBytes(size_t num_bytes)
Definition: FuzzedDataProvider.h:108