Bitcoin Core 29.99.0
P2P Digital Currency
script_interpreter.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
8#include <test/fuzz/fuzz.h>
9#include <test/fuzz/util.h>
10#include <util/check.h>
11
12#include <cstdint>
13#include <optional>
14#include <string>
15#include <vector>
16
17bool CastToBool(const std::vector<unsigned char>& vch);
18
19FUZZ_TARGET(script_interpreter)
20{
21 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
22 {
23 const CScript script_code = ConsumeScript(fuzzed_data_provider);
24 const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
25 if (mtx) {
26 const CTransaction tx_to{*mtx};
27 const unsigned int in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
28 if (in < tx_to.vin.size()) {
29 auto n_hash_type = fuzzed_data_provider.ConsumeIntegral<int>();
30 auto amount = ConsumeMoney(fuzzed_data_provider);
31 auto sigversion = fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0});
32 (void)SignatureHash(script_code, tx_to, in, n_hash_type, amount, sigversion, nullptr);
33 const std::optional<CMutableTransaction> mtx_precomputed = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
34 if (mtx_precomputed) {
35 const CTransaction tx_precomputed{*mtx_precomputed};
36 const PrecomputedTransactionData precomputed_transaction_data{tx_precomputed};
37 n_hash_type = fuzzed_data_provider.ConsumeIntegral<int>();
38 amount = ConsumeMoney(fuzzed_data_provider);
39 sigversion = fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0});
40 (void)SignatureHash(script_code, tx_to, in, n_hash_type, amount, sigversion, &precomputed_transaction_data);
41 }
42 }
43 }
44 }
45 {
46 (void)CastToBool(ConsumeRandomLengthByteVector(fuzzed_data_provider));
47 }
48}
49
51FUZZ_TARGET(sighash_cache)
52{
53 FuzzedDataProvider provider(buffer.data(), buffer.size());
54
55 // Get inputs to the sighash function that won't change across types.
56 const auto scriptcode{ConsumeScript(provider)};
57 const auto tx{ConsumeTransaction(provider, std::nullopt)};
58 if (tx.vin.empty()) return;
59 const auto in_index{provider.ConsumeIntegralInRange<uint32_t>(0, tx.vin.size() - 1)};
60 const auto amount{ConsumeMoney(provider)};
61 const auto sigversion{(SigVersion)provider.ConsumeIntegralInRange(0, 1)};
62
63 // Check the sighash function will give the same result for 100 fuzzer-generated hash types whether or not a cache is
64 // provided. The cache is conserved across types to exercise cache hits.
65 SigHashCache sighash_cache{};
66 for (int i{0}; i < 100; ++i) {
67 const auto hash_type{((i & 2) == 0) ? provider.ConsumeIntegral<int8_t>() : provider.ConsumeIntegral<int32_t>()};
68 const auto nocache_res{SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion)};
69 const auto cache_res{SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &sighash_cache)};
70 Assert(nocache_res == cache_res);
71 }
72}
#define Assert(val)
Identity function.
Definition: check.h:106
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
Data structure to cache SHA256 midstates for the ECDSA sighash calculations (bare,...
Definition: interpreter.h:245
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int32_t nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache, SigHashCache *sighash_cache)
SigVersion
Definition: interpreter.h:191
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
FUZZ_TARGET(script_interpreter)
bool CastToBool(const std::vector< unsigned char > &vch)
Definition: interpreter.cpp:35
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition: util.cpp:93
CMutableTransaction ConsumeTransaction(FuzzedDataProvider &fuzzed_data_provider, const std::optional< std::vector< Txid > > &prevout_txids, const int max_num_in, const int max_num_out) noexcept
Definition: util.cpp:42
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