0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-22 12:23:34 -05:00
bitcoin-bitcoin-core/src/bench/sign_transaction.cpp
merge-script d184fc3ba4
Merge bitcoin/bitcoin#30571: test: [refactor] Use m_rng directly
948238a683 test: Remove FastRandomContext global (Ryan Ofsky)
fa0fe08eca scripted-diff: [test] Use g_rng/m_rng directly (MarcoFalke)
fa54cab473 test: refactor: Accept any RandomNumberGenerator in RandMoney (MarcoFalke)
68f77dd21e test: refactor: Pass rng parameters to test functions (Ryan Ofsky)
fa19af555d test: refactor: Move g_insecure_rand_ctx.Reseed out of the helper that calls MakeRandDeterministicDANGEROUS (MarcoFalke)
3dc527f460 test: refactor: Give unit test functions access to test state (Ryan Ofsky)
fab023e177 test: refactor: Make unsigned promotion explicit (MarcoFalke)
fa2cb654ec test: Add m_rng alias for the global random context (MarcoFalke)
fae7e3791c test: Correct the random seed log on a prevector test failure (MarcoFalke)

Pull request description:

  This is mostly a style-cleanup for the tests' random generation:

  1) `g_insecure_rand_ctx` in the tests is problematic, because the name is a leftover when the generator was indeed insecure. However, now the generator is *deterministic*, because the seed is either passed in or printed (c.f. RANDOM_CTX_SEED). Stating that deterministic randomness is insecure in the tests seems redundant at best. Fix it by just using `m_rng` for the name.

  2) The global random context has many one-line aliases, such as `InsecureRand32`. This is problematic, because the same line of code may use the context directly and through a wrapper at the same time. For example in net_tests (see below). This inconsistency is harmless, but confusing. Fix it by just removing the one-line aliases.

  ```
  src/test/net_tests.cpp:        auto msg_data_1 = g_insecure_rand_ctx.randbytes<uint8_t>(InsecureRandRange(100000));
  ````

  3) The wrapper for randmoney has the same problem that the same unit test uses the context directly and through a wrapper at the same time. Also, it has a single type of Rng hardcoded. Fix it by accepting any type.

ACKs for top commit:
  hodlinator:
    ACK 948238a683
  ryanofsky:
    Code review ACK 948238a683. Only changes since last review were changing a comments a little bit.
  marcofleon:
    Code review ACK 948238a683. Only changes since my last review are the improvements in `prevector_tests`.

Tree-SHA512: 69c6b46a42cb743138ee8c87ff26a588dbe083e3efb3dca49b8a133ba5d3b09e8bf01c590ec7e121a7d77cb1fd7dcacd927a9ca139ac65e1f7c6d1ec46f93b57
2024-08-28 16:56:32 +01:00

106 lines
3.7 KiB
C++

// Copyright (c) 2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <addresstype.h>
#include <bench/bench.h>
#include <coins.h>
#include <key.h>
#include <primitives/transaction.h>
#include <pubkey.h>
#include <script/interpreter.h>
#include <script/script.h>
#include <script/sign.h>
#include <script/signingprovider.h>
#include <span.h>
#include <test/util/random.h>
#include <uint256.h>
#include <util/translation.h>
#include <cassert>
#include <map>
#include <vector>
enum class InputType {
P2WPKH, // segwitv0, witness-pubkey-hash (ECDSA signature)
P2TR, // segwitv1, taproot key-path spend (Schnorr signature)
};
static void SignTransactionSingleInput(benchmark::Bench& bench, InputType input_type)
{
ECC_Context ecc_context{};
FlatSigningProvider keystore;
std::vector<CScript> prev_spks;
// Create a bunch of keys / UTXOs to avoid signing with the same key repeatedly
for (int i = 0; i < 32; i++) {
CKey privkey = GenerateRandomKey();
CPubKey pubkey = privkey.GetPubKey();
CKeyID key_id = pubkey.GetID();
keystore.keys.emplace(key_id, privkey);
keystore.pubkeys.emplace(key_id, pubkey);
// Create specified locking script type
CScript prev_spk;
switch (input_type) {
case InputType::P2WPKH: prev_spk = GetScriptForDestination(WitnessV0KeyHash(pubkey)); break;
case InputType::P2TR: prev_spk = GetScriptForDestination(WitnessV1Taproot(XOnlyPubKey{pubkey})); break;
default: assert(false);
}
prev_spks.push_back(prev_spk);
}
// Simple 1-input tx with artificial outpoint
// (note that for the purpose of signing with SIGHASH_ALL we don't need any outputs)
COutPoint prevout{/*hashIn=*/Txid::FromUint256(uint256::ONE), /*nIn=*/1337};
CMutableTransaction unsigned_tx;
unsigned_tx.vin.emplace_back(prevout);
// Benchmark.
int iter = 0;
bench.minEpochIterations(100).run([&] {
CMutableTransaction tx{unsigned_tx};
std::map<COutPoint, Coin> coins;
CScript prev_spk = prev_spks[(iter++) % prev_spks.size()];
coins[prevout] = Coin(CTxOut(10000, prev_spk), /*nHeightIn=*/100, /*fCoinBaseIn=*/false);
std::map<int, bilingual_str> input_errors;
bool complete = SignTransaction(tx, &keystore, coins, SIGHASH_ALL, input_errors);
assert(complete);
});
}
static void SignTransactionECDSA(benchmark::Bench& bench) { SignTransactionSingleInput(bench, InputType::P2WPKH); }
static void SignTransactionSchnorr(benchmark::Bench& bench) { SignTransactionSingleInput(bench, InputType::P2TR); }
static void SignSchnorrTapTweakBenchmark(benchmark::Bench& bench, bool use_null_merkle_root)
{
FastRandomContext rng;
ECC_Context ecc_context{};
auto key = GenerateRandomKey();
auto msg = rng.rand256();
auto merkle_root = use_null_merkle_root ? uint256() : rng.rand256();
auto aux = rng.rand256();
std::vector<unsigned char> sig(64);
bench.minEpochIterations(100).run([&] {
bool success = key.SignSchnorr(msg, sig, &merkle_root, aux);
assert(success);
});
}
static void SignSchnorrWithMerkleRoot(benchmark::Bench& bench)
{
SignSchnorrTapTweakBenchmark(bench, /*use_null_merkle_root=*/false);
}
static void SignSchnorrWithNullMerkleRoot(benchmark::Bench& bench)
{
SignSchnorrTapTweakBenchmark(bench, /*use_null_merkle_root=*/true);
}
BENCHMARK(SignTransactionECDSA, benchmark::PriorityLevel::HIGH);
BENCHMARK(SignTransactionSchnorr, benchmark::PriorityLevel::HIGH);
BENCHMARK(SignSchnorrWithMerkleRoot, benchmark::PriorityLevel::HIGH);
BENCHMARK(SignSchnorrWithNullMerkleRoot, benchmark::PriorityLevel::HIGH);