Bitcoin Core  28.1.0
P2P Digital Currency
sign_transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2023 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 <addresstype.h>
7 #include <coins.h>
8 #include <key.h>
10 #include <pubkey.h>
11 #include <script/interpreter.h>
12 #include <script/script.h>
13 #include <script/sign.h>
14 #include <uint256.h>
15 #include <test/util/random.h>
16 #include <util/translation.h>
17 
18 enum class InputType {
19  P2WPKH, // segwitv0, witness-pubkey-hash (ECDSA signature)
20  P2TR, // segwitv1, taproot key-path spend (Schnorr signature)
21 };
22 
23 static void SignTransactionSingleInput(benchmark::Bench& bench, InputType input_type)
24 {
26 
27  FlatSigningProvider keystore;
28  std::vector<CScript> prev_spks;
29 
30  // Create a bunch of keys / UTXOs to avoid signing with the same key repeatedly
31  for (int i = 0; i < 32; i++) {
32  CKey privkey = GenerateRandomKey();
33  CPubKey pubkey = privkey.GetPubKey();
34  CKeyID key_id = pubkey.GetID();
35  keystore.keys.emplace(key_id, privkey);
36  keystore.pubkeys.emplace(key_id, pubkey);
37 
38  // Create specified locking script type
39  CScript prev_spk;
40  switch (input_type) {
41  case InputType::P2WPKH: prev_spk = GetScriptForDestination(WitnessV0KeyHash(pubkey)); break;
42  case InputType::P2TR: prev_spk = GetScriptForDestination(WitnessV1Taproot(XOnlyPubKey{pubkey})); break;
43  default: assert(false);
44  }
45  prev_spks.push_back(prev_spk);
46  }
47 
48  // Simple 1-input tx with artificial outpoint
49  // (note that for the purpose of signing with SIGHASH_ALL we don't need any outputs)
50  COutPoint prevout{/*hashIn=*/Txid::FromUint256(uint256::ONE), /*nIn=*/1337};
51  CMutableTransaction unsigned_tx;
52  unsigned_tx.vin.emplace_back(prevout);
53 
54  // Benchmark.
55  int iter = 0;
56  bench.minEpochIterations(100).run([&] {
57  CMutableTransaction tx{unsigned_tx};
58  std::map<COutPoint, Coin> coins;
59  CScript prev_spk = prev_spks[(iter++) % prev_spks.size()];
60  coins[prevout] = Coin(CTxOut(10000, prev_spk), /*nHeightIn=*/100, /*fCoinBaseIn=*/false);
61  std::map<int, bilingual_str> input_errors;
62  bool complete = SignTransaction(tx, &keystore, coins, SIGHASH_ALL, input_errors);
63  assert(complete);
64  });
65 }
66 
69 
70 static void SignSchnorrTapTweakBenchmark(benchmark::Bench& bench, bool use_null_merkle_root)
71 {
73 
74  auto key = GenerateRandomKey();
75  auto msg = InsecureRand256();
76  auto merkle_root = use_null_merkle_root ? uint256() : InsecureRand256();
77  auto aux = InsecureRand256();
78  std::vector<unsigned char> sig(64);
79 
80  bench.minEpochIterations(100).run([&] {
81  bool success = key.SignSchnorr(msg, sig, &merkle_root, aux);
82  assert(success);
83  });
84 }
85 
87 {
88  SignSchnorrTapTweakBenchmark(bench, /*use_null_merkle_root=*/false);
89 }
90 
92 {
93  SignSchnorrTapTweakBenchmark(bench, /*use_null_merkle_root=*/true);
94 }
95 
static const uint256 ONE
Definition: uint256.h:186
BENCHMARK(SignTransactionECDSA, benchmark::PriorityLevel::HIGH)
void SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, const UniValue &hashType, UniValue &result)
Sign a transaction with the given keystore and previous transactions.
static void SignSchnorrWithMerkleRoot(benchmark::Bench &bench)
assert(!tx.IsCoinBase())
RAII class initializing and deinitializing global state for elliptic curve support.
Definition: key.h:321
A UTXO entry.
Definition: coins.h:32
ECC_Context ecc_context
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
std::vector< CTxIn > vin
Definition: transaction.h:379
std::map< CKeyID, CKey > keys
static void SignSchnorrTapTweakBenchmark(benchmark::Bench &bench, bool use_null_merkle_root)
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
ANKERL_NANOBENCH(NODISCARD) std Bench & minEpochIterations(uint64_t numIters) noexcept
Sets the minimum number of iterations each epoch should take.
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
An encapsulated public key.
Definition: pubkey.h:33
std::map< CKeyID, CPubKey > pubkeys
An output of a transaction.
Definition: transaction.h:149
static uint256 InsecureRand256()
Definition: random.h:35
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static void SignTransactionECDSA(benchmark::Bench &bench)
static void SignTransactionSingleInput(benchmark::Bench &bench, InputType input_type)
256-bit opaque blob.
Definition: uint256.h:178
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
static transaction_identifier FromUint256(const uint256 &id)
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
A mutable version of CTransaction.
Definition: transaction.h:377
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
An encapsulated private key.
Definition: key.h:34
static void SignSchnorrWithNullMerkleRoot(benchmark::Bench &bench)
static void SignTransactionSchnorr(benchmark::Bench &bench)