2222// modified to measure performance of other types of scripts.
2323static void VerifyScriptBench (benchmark::Bench& bench)
2424{
25- ECC_Context ecc_context{};
25+ ECC_Context ecc_context{/* use_new_sign= */ false };
2626
2727 const script_verify_flags flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
2828 const int witnessversion = 0 ;
@@ -66,6 +66,55 @@ static void VerifyScriptBench(benchmark::Bench& bench)
6666 });
6767}
6868
69+ // Microbenchmark for verification of a basic P2WPKH script. Can be easily
70+ // modified to measure performance of other types of scripts.
71+ static void VerifyScriptBenchNew (benchmark::Bench& bench)
72+ {
73+ SHA256AutoDetect (sha256_implementation::USE_SSE4_AND_SHANI);
74+ ECC_Context ecc_context{/* use_new_sign=*/ true };
75+
76+ const script_verify_flags flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
77+ const int witnessversion = 0 ;
78+
79+ // Key pair.
80+ CKey key;
81+ static const std::array<unsigned char , 32 > vchKey = {
82+ {
83+ 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
84+ }
85+ };
86+ key.Set (vchKey.begin (), vchKey.end (), false );
87+ CPubKey pubkey = key.GetPubKey ();
88+ uint160 pubkeyHash;
89+ CHash160 ().Write (pubkey).Finalize (pubkeyHash);
90+
91+ // Script.
92+ CScript scriptPubKey = CScript () << witnessversion << ToByteVector (pubkeyHash);
93+ CScript scriptSig;
94+ CScript witScriptPubkey = CScript () << OP_DUP << OP_HASH160 << ToByteVector (pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG;
95+ const CMutableTransaction& txCredit = BuildCreditingTransaction (scriptPubKey, 1 );
96+ CMutableTransaction txSpend = BuildSpendingTransaction (scriptSig, CScriptWitness (), CTransaction (txCredit));
97+ CScriptWitness& witness = txSpend.vin [0 ].scriptWitness ;
98+ witness.stack .emplace_back ();
99+ key.Sign (SignatureHash (witScriptPubkey, txSpend, 0 , SIGHASH_ALL, txCredit.vout [0 ].nValue , SigVersion::WITNESS_V0), witness.stack .back ());
100+ witness.stack .back ().push_back (static_cast <unsigned char >(SIGHASH_ALL));
101+ witness.stack .push_back (ToByteVector (pubkey));
102+
103+ // Benchmark.
104+ bench.run ([&] {
105+ ScriptError err;
106+ bool success = VerifyScript (
107+ txSpend.vin [0 ].scriptSig ,
108+ txCredit.vout [0 ].scriptPubKey ,
109+ &txSpend.vin [0 ].scriptWitness ,
110+ flags,
111+ MutableTransactionSignatureChecker (&txSpend, 0 , txCredit.vout [0 ].nValue , MissingDataBehavior::ASSERT_FAIL),
112+ &err);
113+ assert (err == SCRIPT_ERR_OK);
114+ assert (success);
115+ });
116+ }
117+
69118static void VerifyNestedIfScript (benchmark::Bench& bench)
70119{
71120 std::vector<std::vector<unsigned char >> stack;
@@ -87,5 +136,6 @@ static void VerifyNestedIfScript(benchmark::Bench& bench)
87136 });
88137}
89138
90- BENCHMARK (VerifyScriptBench, benchmark::PriorityLevel::HIGH);
91- BENCHMARK (VerifyNestedIfScript, benchmark::PriorityLevel::HIGH);
139+ BENCHMARK (VerifyScriptBench, benchmark::PriorityLevel::SPECIAL);
140+ BENCHMARK (VerifyScriptBenchNew, benchmark::PriorityLevel::SPECIAL);
141+ // BENCHMARK(VerifyNestedIfScript, benchmark::PriorityLevel::HIGH);
0 commit comments