Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <primitives/transaction.h>6#include <script/interpreter.h>7#include <test/fuzz/FuzzedDataProvider.h>8#include <test/fuzz/fuzz.h>9#include <test/fuzz/util.h>10#include <util/check.h>12#include <cstdint>13#include <optional>14#include <string>15#include <vector>17bool CastToBool(const std::vector<unsigned char>& vch);19FUZZ_TARGET(script_interpreter)20{21FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());22{23const CScript script_code = ConsumeScript(fuzzed_data_provider);24const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);25if (mtx) {26const CTransaction tx_to{*mtx};27const unsigned int in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();28if (in < tx_to.vin.size()) {29auto n_hash_type = fuzzed_data_provider.ConsumeIntegral<int>();30auto amount = ConsumeMoney(fuzzed_data_provider);31auto sigversion = fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0});32(void)SignatureHash(script_code, tx_to, in, n_hash_type, amount, sigversion, nullptr);33const std::optional<CMutableTransaction> mtx_precomputed = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);34if (mtx_precomputed) {35const CTransaction tx_precomputed{*mtx_precomputed};36const PrecomputedTransactionData precomputed_transaction_data{tx_precomputed};37n_hash_type = fuzzed_data_provider.ConsumeIntegral<int>();38amount = ConsumeMoney(fuzzed_data_provider);39sigversion = 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}50/** Differential fuzzing for SignatureHash with and without cache. */51FUZZ_TARGET(sighash_cache)52{53FuzzedDataProvider provider(buffer.data(), buffer.size());55// Get inputs to the sighash function that won't change across types.56const auto scriptcode{ConsumeScript(provider)};57const auto tx{ConsumeTransaction(provider, std::nullopt)};58if (tx.vin.empty()) return;59const auto in_index{provider.ConsumeIntegralInRange<uint32_t>(0, tx.vin.size() - 1)};60const auto amount{ConsumeMoney(provider)};61const auto sigversion{(SigVersion)provider.ConsumeIntegralInRange(0, 1)};63// Check the sighash function will give the same result for 100 fuzzer-generated hash types whether or not a cache is64// provided. The cache is conserved across types to exercise cache hits.65SigHashCache sighash_cache{};66for (int i{0}; i < 100; ++i) {67const auto hash_type{((i & 2) == 0) ? provider.ConsumeIntegral<int8_t>() : provider.ConsumeIntegral<int32_t>()};68const auto nocache_res{SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion)};69const auto cache_res{SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &sighash_cache)};70Assert(nocache_res == cache_res);71}72}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.