Bitcoin Core  26.1.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 #if defined(HAVE_CONSENSUS_LIB)
9 #endif
10 #include <script/script.h>
11 #include <script/interpreter.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  ECC_Start();
22 
24  const int witnessversion = 0;
25 
26  // Key pair.
27  CKey key;
28  static const std::array<unsigned char, 32> vchKey = {
29  {
30  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
31  }
32  };
33  key.Set(vchKey.begin(), vchKey.end(), false);
34  CPubKey pubkey = key.GetPubKey();
35  uint160 pubkeyHash;
36  CHash160().Write(pubkey).Finalize(pubkeyHash);
37 
38  // Script.
39  CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
40  CScript scriptSig;
41  CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG;
42  const CMutableTransaction& txCredit = BuildCreditingTransaction(scriptPubKey, 1);
44  CScriptWitness& witness = txSpend.vin[0].scriptWitness;
45  witness.stack.emplace_back();
46  key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SigVersion::WITNESS_V0), witness.stack.back());
47  witness.stack.back().push_back(static_cast<unsigned char>(SIGHASH_ALL));
48  witness.stack.push_back(ToByteVector(pubkey));
49 
50  // Benchmark.
51  bench.run([&] {
52  ScriptError err;
53  bool success = VerifyScript(
54  txSpend.vin[0].scriptSig,
55  txCredit.vout[0].scriptPubKey,
56  &txSpend.vin[0].scriptWitness,
57  flags,
59  &err);
60  assert(err == SCRIPT_ERR_OK);
61  assert(success);
62 
63 #if defined(HAVE_CONSENSUS_LIB)
65  stream << txSpend;
67  txCredit.vout[0].scriptPubKey.data(),
68  txCredit.vout[0].scriptPubKey.size(),
69  txCredit.vout[0].nValue,
70  (const unsigned char*)stream.data(), stream.size(), 0, flags, nullptr);
71  assert(csuccess == 1);
72 #endif
73  });
74  ECC_Stop();
75 }
76 
78 {
79  std::vector<std::vector<unsigned char>> stack;
80  CScript script;
81  for (int i = 0; i < 100; ++i) {
82  script << OP_1 << OP_IF;
83  }
84  for (int i = 0; i < 1000; ++i) {
85  script << OP_1;
86  }
87  for (int i = 0; i < 100; ++i) {
88  script << OP_ENDIF;
89  }
90  bench.run([&] {
91  auto stack_copy = stack;
93  bool ret = EvalScript(stack_copy, script, 0, BaseSignatureChecker(), SigVersion::BASE, &error);
94  assert(ret);
95  });
96 }
97 
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, int nValue)
Witness v0 (P2WPKH and P2WSH); see BIP 141.
int ret
assert(!tx.IsCoinBase())
enum ScriptError_t ScriptError
Definition: script.h:123
void Finalize(Span< unsigned char > output)
Definition: hash.h:56
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:188
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
std::vector< CTxIn > vin
Definition: transaction.h:381
std::vector< std::vector< unsigned char > > stack
Definition: script.h:568
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
static void VerifyNestedIfScript(benchmark::Bench &bench)
value_type * data()
Definition: streams.h:234
Definition: script.h:81
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
Definition: script.h:102
static void VerifyScriptBench(benchmark::Bench &bench)
Abort execution through assertion failure (for consensus code)
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1230
ECC_Start()
Definition: key.cpp:429
An encapsulated public key.
Definition: pubkey.h:33
std::vector< CTxOut > vout
Definition: transaction.h:382
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:99
BENCHMARK(VerifyScriptBench, benchmark::PriorityLevel::HIGH)
ECC_Stop()
Definition: key.cpp:446
size_type size() const
Definition: streams.h:227
int flags
Definition: bitcoin-tx.cpp:528
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:65
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)
bool error(const char *fmt, const Args &... args)
Definition: logging.h:262
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:412
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
160-bit opaque blob.
Definition: uint256.h:95
A mutable version of CTransaction.
Definition: transaction.h:379
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:623
An encapsulated private key.
Definition: key.h:32
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:294
A hasher class for Bitcoin&#39;s 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:50
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:63