Bitcoin Core  26.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 #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)
64  DataStream stream;
65  stream << TX_WITH_WITNESS(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 
int ret
int flags
Definition: bitcoin-tx.cpp:528
ECC_Start()
Definition: key.cpp:429
ECC_Stop()
Definition: key.cpp:446
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)
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
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
size_type size() const
Definition: streams.h:181
value_type * data()
Definition: streams.h:188
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)
bool error(const char *fmt, const Args &... args)
Definition: logging.h:262
@ HIGH
Definition: bench.h:47
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
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)