Bitcoin Core  27.99.0
P2P Digital Currency
verify_script.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2022 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 #include <script/script.h>
8 #include <script/interpreter.h>
9 #include <streams.h>
11 
12 #include <array>
13 
14 // Microbenchmark for verification of a basic P2WPKH script. Can be easily
15 // modified to measure performance of other types of scripts.
17 {
18  ECC_Start();
19 
21  const int witnessversion = 0;
22 
23  // Key pair.
24  CKey key;
25  static const std::array<unsigned char, 32> vchKey = {
26  {
27  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
28  }
29  };
30  key.Set(vchKey.begin(), vchKey.end(), false);
31  CPubKey pubkey = key.GetPubKey();
32  uint160 pubkeyHash;
33  CHash160().Write(pubkey).Finalize(pubkeyHash);
34 
35  // Script.
36  CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
37  CScript scriptSig;
38  CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG;
39  const CMutableTransaction& txCredit = BuildCreditingTransaction(scriptPubKey, 1);
41  CScriptWitness& witness = txSpend.vin[0].scriptWitness;
42  witness.stack.emplace_back();
43  key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SigVersion::WITNESS_V0), witness.stack.back());
44  witness.stack.back().push_back(static_cast<unsigned char>(SIGHASH_ALL));
45  witness.stack.push_back(ToByteVector(pubkey));
46 
47  // Benchmark.
48  bench.run([&] {
49  ScriptError err;
50  bool success = VerifyScript(
51  txSpend.vin[0].scriptSig,
52  txCredit.vout[0].scriptPubKey,
53  &txSpend.vin[0].scriptWitness,
54  flags,
56  &err);
57  assert(err == SCRIPT_ERR_OK);
58  assert(success);
59  });
60  ECC_Stop();
61 }
62 
64 {
65  std::vector<std::vector<unsigned char>> stack;
66  CScript script;
67  for (int i = 0; i < 100; ++i) {
68  script << OP_1 << OP_IF;
69  }
70  for (int i = 0; i < 1000; ++i) {
71  script << OP_1;
72  }
73  for (int i = 0; i < 100; ++i) {
74  script << OP_ENDIF;
75  }
76  bench.run([&] {
77  auto stack_copy = stack;
78  ScriptError error;
79  bool ret = EvalScript(stack_copy, script, 0, BaseSignatureChecker(), SigVersion::BASE, &error);
80  assert(ret);
81  });
82 }
83 
int ret
int flags
Definition: bitcoin-tx.cpp:530
ECC_Start()
Definition: key.cpp:435
ECC_Stop()
Definition: key.cpp:452
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:33
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:214
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:188
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:99
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
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
160-bit opaque blob.
Definition: uint256.h:95
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:49
@ SCRIPT_VERIFY_WITNESS
Definition: interpreter.h:108
@ SIGHASH_ALL
Definition: interpreter.h:30
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
@ HIGH
Definition: bench.h:47
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:66
@ OP_IF
Definition: script.h:103
@ OP_CHECKSIG
Definition: script.h:189
@ OP_ENDIF
Definition: script.h:108
@ OP_DUP
Definition: script.h:124
@ OP_HASH160
Definition: script.h:186
@ OP_1
Definition: script.h:82
@ OP_EQUALVERIFY
Definition: script.h:146
enum ScriptError_t ScriptError
@ SCRIPT_ERR_OK
Definition: script_error.h:13
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
std::vector< std::vector< unsigned char > > stack
Definition: script.h:569
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, int nValue)
assert(!tx.IsCoinBase())
BENCHMARK(VerifyScriptBench, benchmark::PriorityLevel::HIGH)
static void VerifyScriptBench(benchmark::Bench &bench)
static void VerifyNestedIfScript(benchmark::Bench &bench)