Bitcoin Core
31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
bench
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 <
bench/bench.h
>
6
#include <hash.h>
7
#include <
key.h
>
8
#include <
primitives/transaction.h
>
9
#include <
pubkey.h
>
10
#include <
script/interpreter.h
>
11
#include <
script/script.h
>
12
#include <
span.h
>
13
#include <
test/util/transaction_utils.h
>
14
#include <
uint256.h
>
15
16
#include <array>
17
#include <cassert>
18
#include <cstdint>
19
#include <vector>
20
21
// Microbenchmark for verification of a basic P2WPKH script. Can be easily
22
// modified to measure performance of other types of scripts.
23
static
void
VerifyScriptBench
(
benchmark::Bench
&
bench
)
24
{
25
ECC_Context
ecc_context
{};
26
27
const
script_verify_flags
flags
{
SCRIPT_VERIFY_WITNESS
|
SCRIPT_VERIFY_P2SH
};
28
const
int
witnessversion
= 0;
29
30
// Key pair.
31
CKey
key;
32
static
const
std::array<unsigned char, 32> vchKey = {
33
{
34
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
35
}
36
};
37
key.
Set
(vchKey.begin(), vchKey.end(),
false
);
38
CPubKey
pubkey = key.
GetPubKey
();
39
uint160
pubkeyHash
;
40
CHash160
().
Write
(pubkey).
Finalize
(
pubkeyHash
);
41
42
// Script.
43
CScript
scriptPubKey =
CScript
() <<
witnessversion
<<
ToByteVector
(
pubkeyHash
);
44
CScript
scriptSig;
45
CScript
witScriptPubkey
=
CScript
() <<
OP_DUP
<<
OP_HASH160
<<
ToByteVector
(
pubkeyHash
) <<
OP_EQUALVERIFY
<<
OP_CHECKSIG
;
46
const
CMutableTransaction
&
txCredit
=
BuildCreditingTransaction
(scriptPubKey, 1);
47
CMutableTransaction
txSpend
=
BuildSpendingTransaction
(scriptSig,
CScriptWitness
(),
CTransaction
(
txCredit
));
48
CScriptWitness
& witness =
txSpend
.vin[0].scriptWitness;
49
witness.
stack
.emplace_back();
50
key.
Sign
(
SignatureHash
(
witScriptPubkey
,
txSpend
, 0,
SIGHASH_ALL
,
txCredit
.vout[0].nValue,
SigVersion::WITNESS_V0
), witness.
stack
.back());
51
witness.
stack
.back().push_back(
static_cast<
unsigned
char
>
(
SIGHASH_ALL
));
52
witness.
stack
.push_back(
ToByteVector
(pubkey));
53
54
// Benchmark.
55
bench
.run([&] {
56
ScriptError
err;
57
bool
success =
VerifyScript
(
58
txSpend
.vin[0].scriptSig,
59
txCredit
.vout[0].scriptPubKey,
60
&
txSpend
.vin[0].scriptWitness,
61
flags
,
62
MutableTransactionSignatureChecker
(&
txSpend
, 0,
txCredit
.vout[0].nValue,
MissingDataBehavior::ASSERT_FAIL
),
63
&err);
64
assert
(err ==
SCRIPT_ERR_OK
);
65
assert
(success);
66
});
67
}
68
69
static
void
VerifyNestedIfScript
(
benchmark::Bench
&
bench
)
70
{
71
std::vector<std::vector<unsigned char>> stack;
72
CScript
script
;
73
for
(
int
i = 0; i < 100; ++i) {
74
script
<<
OP_1
<<
OP_IF
;
75
}
76
for
(
int
i = 0; i < 1000; ++i) {
77
script
<<
OP_1
;
78
}
79
for
(
int
i = 0; i < 100; ++i) {
80
script
<<
OP_ENDIF
;
81
}
82
bench
.run([&] {
83
auto
stack_copy
= stack;
84
ScriptError
error;
85
bool
ret
=
EvalScript
(
stack_copy
,
script
, 0,
BaseSignatureChecker
(),
SigVersion::BASE
, &error);
86
assert
(
ret
);
87
});
88
}
89
90
BENCHMARK
(
VerifyScriptBench
);
91
BENCHMARK
(
VerifyNestedIfScript
);
bench.h
BENCHMARK
#define BENCHMARK(n)
Definition
bench.h:68
ret
int ret
Definition
bitcoin-cli.cpp:1350
flags
int flags
Definition
bitcoin-tx.cpp:529
ecc_context
ECC_Context ecc_context
Definition
bitcoin-wallet.cpp:126
BaseSignatureChecker
Definition
interpreter.h:275
CHash160
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition
hash.h:49
CHash160::Write
CHash160 & Write(std::span< const unsigned char > input)
Definition
hash.h:62
CHash160::Finalize
void Finalize(std::span< unsigned char > output)
Definition
hash.h:55
CKey
An encapsulated private key.
Definition
key.h:36
CKey::Sign
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition
key.cpp:209
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition
key.cpp:183
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition
key.h:104
CPubKey
An encapsulated public key.
Definition
pubkey.h:34
CScript
Serialized script, used inside transaction inputs and outputs.
Definition
script.h:405
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition
transaction.h:281
ECC_Context
RAII class initializing and deinitializing global state for elliptic curve support.
Definition
key.h:326
GenericTransactionSignatureChecker
Definition
interpreter.h:314
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition
nanobench.h:627
script_verify_flags
Definition
verify_flags.h:15
uint160
160-bit opaque blob.
Definition
uint256.h:183
EvalScript
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)
Definition
interpreter.cpp:407
SignatureHash
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)
Definition
interpreter.cpp:1600
VerifyScript
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
Definition
interpreter.cpp:2002
interpreter.h
SigVersion::BASE
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
SigVersion::WITNESS_V0
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
SIGHASH_ALL
@ SIGHASH_ALL
Definition
interpreter.h:31
MissingDataBehavior::ASSERT_FAIL
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
script_verify_flag_name::SCRIPT_VERIFY_WITNESS
@ SCRIPT_VERIFY_WITNESS
script_verify_flag_name::SCRIPT_VERIFY_P2SH
@ SCRIPT_VERIFY_P2SH
key.h
script
Definition
parsing.cpp:13
transaction.h
pubkey.h
script.h
OP_IF
@ OP_IF
Definition
script.h:104
OP_CHECKSIG
@ OP_CHECKSIG
Definition
script.h:190
OP_ENDIF
@ OP_ENDIF
Definition
script.h:109
OP_DUP
@ OP_DUP
Definition
script.h:125
OP_HASH160
@ OP_HASH160
Definition
script.h:187
OP_1
@ OP_1
Definition
script.h:83
OP_EQUALVERIFY
@ OP_EQUALVERIFY
Definition
script.h:147
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition
script.h:67
ScriptError
enum ScriptError_t ScriptError
SCRIPT_ERR_OK
@ SCRIPT_ERR_OK
Definition
script_error.h:13
span.h
CMutableTransaction
A mutable version of CTransaction.
Definition
transaction.h:358
CScriptWitness
Definition
script.h:577
CScriptWitness::stack
std::vector< std::vector< unsigned char > > stack
Definition
script.h:580
BuildSpendingTransaction
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
Definition
transaction_utils.cpp:26
BuildCreditingTransaction
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, int nValue)
Definition
transaction_utils.cpp:10
transaction_utils.h
uint256.h
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:73
assert
assert(!tx.IsCoinBase())
VerifyScriptBench
static void VerifyScriptBench(benchmark::Bench &bench)
Definition
verify_script.cpp:23
VerifyNestedIfScript
static void VerifyNestedIfScript(benchmark::Bench &bench)
Definition
verify_script.cpp:69
Generated on Thu Apr 16 2026 09:42:38 for Bitcoin Core by
1.10.0