Bitcoin Core 31.99.0
P2P Digital Currency
verify_script.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-present 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
5#include <addresstype.h>
6#include <bench/bench.h>
7#include <key.h>
8#include <policy/policy.h>
10#include <pubkey.h>
11#include <script/interpreter.h>
12#include <script/script.h>
13#include <span.h>
15#include <uint256.h>
16#include <util/translation.h>
17
18#include <array>
19#include <cassert>
20#include <cstdint>
21#include <vector>
22
23enum class ScriptType {
24 P2WPKH, // segwitv0, witness-pubkey-hash (ECDSA signature)
25 P2TR, // segwitv1, taproot key-path spend (Schnorr signature)
26};
27
28// Microbenchmark for verification of standard scripts.
29static void VerifyScriptBench(benchmark::Bench& bench, ScriptType script_type)
30{
32
33 // Create deterministic key material needed for output script creation / signing
34 CKey privkey;
35 privkey.Set(uint256::ONE.begin(), uint256::ONE.end(), /*fCompressedIn=*/true);
36 CPubKey pubkey = privkey.GetPubKey();
37 CKeyID key_id = pubkey.GetID();
38
39 FlatSigningProvider keystore;
40 keystore.keys.emplace(key_id, privkey);
41 keystore.pubkeys.emplace(key_id, pubkey);
42
43 // Create crediting and spending transactions with provided input type
44 const auto dest{[&]() -> CTxDestination {
45 switch (script_type) {
46 case ScriptType::P2WPKH: return WitnessV0KeyHash(pubkey);
47 case ScriptType::P2TR: return WitnessV1Taproot(XOnlyPubKey{pubkey});
48 } // no default case, so the compiler can warn about missing cases
49 assert(false);
50 }()};
52 CMutableTransaction txSpend = BuildSpendingTransaction(/*scriptSig=*/{}, /*scriptWitness=*/{}, CTransaction(txCredit));
53
54 // Sign spending transaction, precompute transaction data
56 {
57 std::map<COutPoint, Coin> coins;
58 coins[txSpend.vin[0].prevout] = Coin(txCredit.vout[0], /*nHeightIn=*/100, /*fCoinBaseIn=*/false);
59 std::map<int, bilingual_str> input_errors;
60 bool complete = SignTransaction(txSpend, &keystore, coins, SIGHASH_ALL, input_errors);
61 assert(complete);
62 txdata.Init(txSpend, /*spent_outputs=*/{txCredit.vout[0]});
63 }
64
65 // Benchmark.
66 bench.run([&] {
67 ScriptError err;
68 bool success = VerifyScript(
69 txSpend.vin[0].scriptSig,
70 txCredit.vout[0].scriptPubKey,
71 &txSpend.vin[0].scriptWitness,
73 MutableTransactionSignatureChecker(&txSpend, 0, txCredit.vout[0].nValue, txdata, MissingDataBehavior::ASSERT_FAIL),
74 &err);
75 assert(err == SCRIPT_ERR_OK);
76 assert(success);
77 });
78}
79
82
84{
85 std::vector<std::vector<unsigned char>> stack;
87 for (int i = 0; i < 100; ++i) {
88 script << OP_1 << OP_IF;
89 }
90 for (int i = 0; i < 1000; ++i) {
91 script << OP_1;
92 }
93 for (int i = 0; i < 100; ++i) {
95 }
96 bench.run([&] {
97 auto stack_copy = stack;
98 ScriptError error;
99 bool ret = EvalScript(stack_copy, script, 0, BaseSignatureChecker(), SigVersion::BASE, &error);
100 assert(ret);
101 });
102}
103
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
int ret
ECC_Context ecc_context
An encapsulated private key.
Definition: key.h:36
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:183
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:104
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
An encapsulated public key.
Definition: pubkey.h:34
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
A UTXO entry.
Definition: coins.h:35
RAII class initializing and deinitializing global state for elliptic curve support.
Definition: key.h:326
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
static const uint256 ONE
Definition: uint256.h:204
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, script_verify_flags flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ SIGHASH_ALL
Definition: interpreter.h:31
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
static constexpr script_verify_flags STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
Definition: policy.h:119
void SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, const UniValue &hashType, UniValue &result)
Sign a transaction with the given keystore and previous transactions.
@ OP_IF
Definition: script.h:104
@ OP_ENDIF
Definition: script.h:109
@ OP_1
Definition: script.h:83
enum ScriptError_t ScriptError
@ SCRIPT_ERR_OK
Definition: script_error.h:13
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxOut > vout
Definition: transaction.h:360
std::vector< CTxIn > vin
Definition: transaction.h:359
std::map< CKeyID, CPubKey > pubkeys
std::map< CKeyID, CKey > keys
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, int nValue)
assert(!tx.IsCoinBase())
ScriptType
static void VerifyScriptP2TR(benchmark::Bench &bench)
static void VerifyNestedIfScript(benchmark::Bench &bench)
static void VerifyScriptP2WPKH(benchmark::Bench &bench)
static void VerifyScriptBench(benchmark::Bench &bench, ScriptType script_type)
BENCHMARK(VerifyScriptP2WPKH)