forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_bitcoin_consensus.cpp
50 lines (44 loc) · 2.47 KB
/
script_bitcoin_consensus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) 2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <script/bitcoinconsensus.h>
#include <script/interpreter.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <cstdint>
#include <string>
#include <vector>
FUZZ_TARGET(script_bitcoin_consensus)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const std::vector<uint8_t> random_bytes_1 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
const std::vector<uint8_t> random_bytes_2 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
const CAmount money = ConsumeMoney(fuzzed_data_provider);
bitcoinconsensus_error err;
bitcoinconsensus_error* err_p = fuzzed_data_provider.ConsumeBool() ? &err : nullptr;
const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
const unsigned int flags = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
assert(bitcoinconsensus_version() == BITCOINCONSENSUS_API_VER);
if ((flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
return;
}
(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);
(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);
std::vector<UTXO> spent_outputs;
std::vector<std::vector<unsigned char>> spent_spks;
if (n_in <= 24386) {
spent_outputs.reserve(n_in);
spent_spks.reserve(n_in);
for (size_t i = 0; i < n_in; ++i) {
spent_spks.push_back(ConsumeRandomLengthByteVector(fuzzed_data_provider));
const CAmount value{ConsumeMoney(fuzzed_data_provider)};
const auto spk_size{static_cast<unsigned>(spent_spks.back().size())};
spent_outputs.push_back({.scriptPubKey = spent_spks.back().data(), .scriptPubKeySize = spk_size, .value = value});
}
}
const auto spent_outs_size{static_cast<unsigned>(spent_outputs.size())};
(void)bitcoinconsensus_verify_script_with_spent_outputs(
random_bytes_1.data(), random_bytes_1.size(), money, random_bytes_2.data(), random_bytes_2.size(),
spent_outputs.data(), spent_outs_size, n_in, flags, err_p);
}