Bitcoin Core  26.99.0
P2P Digital Currency
script_bitcoin_consensus.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 
6 #include <script/interpreter.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 
11 #include <cstdint>
12 #include <string>
13 #include <vector>
14 
15 FUZZ_TARGET(script_bitcoin_consensus)
16 {
17  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
18  const std::vector<uint8_t> random_bytes_1 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
19  const std::vector<uint8_t> random_bytes_2 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
20  const CAmount money = ConsumeMoney(fuzzed_data_provider);
22  bitcoinconsensus_error* err_p = fuzzed_data_provider.ConsumeBool() ? &err : nullptr;
23  const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
24  const unsigned int flags = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
26  if ((flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
27  return;
28  }
29  (void)bitcoinconsensus_verify_script(random_bytes_1.data(), random_bytes_1.size(), random_bytes_2.data(), random_bytes_2.size(), n_in, flags, err_p);
30  (void)bitcoinconsensus_verify_script_with_amount(random_bytes_1.data(), random_bytes_1.size(), money, random_bytes_2.data(), random_bytes_2.size(), n_in, flags, err_p);
31 
32  std::vector<UTXO> spent_outputs;
33  std::vector<std::vector<unsigned char>> spent_spks;
34  if (n_in <= 24386) {
35  spent_outputs.reserve(n_in);
36  spent_spks.reserve(n_in);
37  for (size_t i = 0; i < n_in; ++i) {
38  spent_spks.push_back(ConsumeRandomLengthByteVector(fuzzed_data_provider));
39  const CAmount value{ConsumeMoney(fuzzed_data_provider)};
40  const auto spk_size{static_cast<unsigned>(spent_spks.back().size())};
41  spent_outputs.push_back({.scriptPubKey = spent_spks.back().data(), .scriptPubKeySize = spk_size, .value = value});
42  }
43  }
44 
45  const auto spent_outs_size{static_cast<unsigned>(spent_outputs.size())};
46 
48  random_bytes_1.data(), random_bytes_1.size(), money, random_bytes_2.data(), random_bytes_2.size(),
49  spent_outputs.data(), spent_outs_size, n_in, flags, err_p);
50 }
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int flags
Definition: bitcoin-tx.cpp:530
unsigned int bitcoinconsensus_version()
int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
Returns 1 if the input nIn of the serialized transaction pointed to by txTo correctly spends the scri...
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
int bitcoinconsensus_verify_script_with_spent_outputs(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const unsigned char *txTo, unsigned int txToLen, const UTXO *spentOutputs, unsigned int spentOutputsLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
enum bitcoinconsensus_error_t bitcoinconsensus_error
#define BITCOINCONSENSUS_API_VER
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:49
@ SCRIPT_VERIFY_WITNESS
Definition: interpreter.h:108
FUZZ_TARGET(script_bitcoin_consensus)
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:29
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
assert(!tx.IsCoinBase())