Bitcoin Core 22.99.0
P2P Digital Currency
verify_script.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-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
5#include <bench/bench.h>
6#include <key.h>
7#if defined(HAVE_CONSENSUS_LIB)
9#endif
10#include <script/script.h>
11#include <script/standard.h>
12#include <streams.h>
14
15#include <array>
16
17// Microbenchmark for verification of a basic P2WPKH script. Can be easily
18// modified to measure performance of other types of scripts.
20{
21 const ECCVerifyHandle verify_handle;
22 ECC_Start();
23
25 const int witnessversion = 0;
26
27 // Key pair.
28 CKey key;
29 static const std::array<unsigned char, 32> vchKey = {
30 {
31 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
32 }
33 };
34 key.Set(vchKey.begin(), vchKey.end(), false);
35 CPubKey pubkey = key.GetPubKey();
36 uint160 pubkeyHash;
37 CHash160().Write(pubkey).Finalize(pubkeyHash);
38
39 // Script.
40 CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
41 CScript scriptSig;
42 CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG;
43 const CMutableTransaction& txCredit = BuildCreditingTransaction(scriptPubKey, 1);
45 CScriptWitness& witness = txSpend.vin[0].scriptWitness;
46 witness.stack.emplace_back();
47 key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SigVersion::WITNESS_V0), witness.stack.back());
48 witness.stack.back().push_back(static_cast<unsigned char>(SIGHASH_ALL));
49 witness.stack.push_back(ToByteVector(pubkey));
50
51 // Benchmark.
52 bench.run([&] {
53 ScriptError err;
54 bool success = VerifyScript(
55 txSpend.vin[0].scriptSig,
56 txCredit.vout[0].scriptPubKey,
57 &txSpend.vin[0].scriptWitness,
58 flags,
60 &err);
61 assert(err == SCRIPT_ERR_OK);
62 assert(success);
63
64#if defined(HAVE_CONSENSUS_LIB)
66 stream << txSpend;
68 txCredit.vout[0].scriptPubKey.data(),
69 txCredit.vout[0].scriptPubKey.size(),
70 txCredit.vout[0].nValue,
71 (const unsigned char*)stream.data(), stream.size(), 0, flags, nullptr);
72 assert(csuccess == 1);
73#endif
74 });
75 ECC_Stop();
76}
77
79{
80 std::vector<std::vector<unsigned char>> stack;
81 CScript script;
82 for (int i = 0; i < 100; ++i) {
83 script << OP_1 << OP_IF;
84 }
85 for (int i = 0; i < 1000; ++i) {
86 script << OP_1;
87 }
88 for (int i = 0; i < 100; ++i) {
89 script << OP_ENDIF;
90 }
91 bench.run([&] {
92 auto stack_copy = stack;
94 bool ret = EvalScript(stack_copy, script, 0, BaseSignatureChecker(), SigVersion::BASE, &error);
95 assert(ret);
96 });
97}
98
int flags
Definition: bitcoin-tx.cpp:525
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
value_type * data()
Definition: streams.h:264
size_type size() const
Definition: streams.h:255
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:49
void Finalize(Span< unsigned char > output)
Definition: hash.h:55
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:62
An encapsulated private key.
Definition: key.h:27
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:213
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
An encapsulated public key.
Definition: pubkey.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:316
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
160-bit opaque blob.
Definition: uint256.h:113
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:46
@ SCRIPT_VERIFY_WITNESS
Definition: interpreter.h:105
@ SIGHASH_ALL
Definition: interpreter.h:27
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:370
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:387
@ OP_IF
Definition: script.h:97
@ OP_CHECKSIG
Definition: script.h:183
@ OP_ENDIF
Definition: script.h:102
@ OP_DUP
Definition: script.h:118
@ OP_HASH160
Definition: script.h:180
@ OP_1
Definition: script.h:76
@ OP_EQUALVERIFY
Definition: script.h:140
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
enum ScriptError_t ScriptError
@ SCRIPT_ERR_OK
Definition: script_error.h:13
@ SER_NETWORK
Definition: serialize.h:138
A mutable version of CTransaction.
Definition: transaction.h:345
std::vector< CTxOut > vout
Definition: transaction.h:347
std::vector< CTxIn > vin
Definition: transaction.h:346
std::vector< std::vector< unsigned char > > stack
Definition: script.h:561
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, int nValue)
assert(!tx.IsCoinBase())
BENCHMARK(VerifyScriptBench)
static void VerifyScriptBench(benchmark::Bench &bench)
static void VerifyNestedIfScript(benchmark::Bench &bench)
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12