Bitcoin Core  22.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 }
assert
assert(!tx.IsCoinBase())
flags
int flags
Definition: bitcoin-tx.cpp:513
BITCOINCONSENSUS_API_VER
#define BITCOINCONSENSUS_API_VER
Definition: bitcoinconsensus.h:34
util.h
interpreter.h
bitcoinconsensus.h
FUZZ_TARGET
FUZZ_TARGET(script_bitcoin_consensus)
Definition: script_bitcoin_consensus.cpp:15
SCRIPT_VERIFY_WITNESS
@ SCRIPT_VERIFY_WITNESS
Definition: interpreter.h:105
FuzzedDataProvider.h
bitcoinconsensus_version
unsigned int bitcoinconsensus_version()
Definition: bitcoinconsensus.cpp:122
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
bitcoinconsensus_verify_script
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...
Definition: bitcoinconsensus.cpp:110
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
SCRIPT_VERIFY_P2SH
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:46
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
bitcoinconsensus_error
enum bitcoinconsensus_error_t bitcoinconsensus_error
ConsumeMoney
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:221
bitcoinconsensus_verify_script_with_amount
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)
Definition: bitcoinconsensus.cpp:101