Bitcoin Core  24.99.0
P2P Digital Currency
script_sigcache.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 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 #include <test/util/setup_common.h>
14 
15 #include <cstdint>
16 #include <optional>
17 #include <string>
18 #include <vector>
19 
20 namespace {
21 const BasicTestingSetup* g_setup;
22 } // namespace
23 
25 {
26  static const auto testing_setup = MakeNoLogFileContext<>();
27  g_setup = testing_setup.get();
28 }
29 
31 {
32  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33 
34  const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
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();
40  CachingTransactionSignatureChecker caching_transaction_signature_checker{mutable_transaction ? &tx : nullptr, n_in, amount, store, 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 }
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
std::vector< T > ConsumeBytes(size_t num_bytes)
FUZZ_TARGET_INIT(script_sigcache, initialize_script_sigcache)
void initialize_script_sigcache()
Basic testing setup.
Definition: setup_common.h:79
A mutable version of CTransaction.
Definition: transaction.h:380
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:17
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:149