Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <addresstype.h>6#include <bench/bench.h>7#include <coins.h>8#include <key.h>9#include <primitives/transaction.h>10#include <pubkey.h>11#include <random.h>12#include <script/interpreter.h>13#include <script/script.h>14#include <script/sign.h>15#include <script/signingprovider.h>16#include <uint256.h>17#include <util/check.h>18#include <util/translation.h>20#include <map>21#include <span>22#include <vector>24enum class InputType {25P2WPKH, // segwitv0, witness-pubkey-hash (ECDSA signature)26P2TR, // segwitv1, taproot key-path spend (Schnorr signature)27};29static void SignTransactionSingleInput(benchmark::Bench& bench, InputType input_type)30{31ECC_Context ecc_context{};33FlatSigningProvider keystore;34std::vector<CScript> prev_spks;36// Create a bunch of keys / UTXOs to avoid signing with the same key repeatedly37for (int i = 0; i < 32; i++) {38CKey privkey = GenerateRandomKey();39CPubKey pubkey = privkey.GetPubKey();40CKeyID key_id = pubkey.GetID();41keystore.keys.emplace(key_id, privkey);42keystore.pubkeys.emplace(key_id, pubkey);44// Create specified locking script type45CScript prev_spk = [&]() {46switch (input_type) {47case InputType::P2WPKH:48return GetScriptForDestination(WitnessV0KeyHash(pubkey));49case InputType::P2TR:50return GetScriptForDestination(WitnessV1Taproot(XOnlyPubKey{pubkey}));51} // no default case, so the compiler can warn about missing cases52assert(false);53}();54prev_spks.push_back(prev_spk);55}57// Simple 1-input tx with artificial outpoint58// (note that for the purpose of signing with SIGHASH_ALL we don't need any outputs)59COutPoint prevout{/*hashIn=*/Txid::FromUint256(uint256::ONE), /*nIn=*/1337};60CMutableTransaction unsigned_tx;61unsigned_tx.vin.emplace_back(prevout);63// Benchmark.64int iter = 0;65bench.minEpochIterations(100).run([&] {66CMutableTransaction tx{unsigned_tx};67std::map<COutPoint, Coin> coins;68const CScript& prev_spk = prev_spks[(iter++) % prev_spks.size()];69coins[prevout] = Coin(CTxOut(10000, prev_spk), /*nHeightIn=*/100, /*fCoinBaseIn=*/false);70std::map<int, bilingual_str> input_errors;71bool complete = SignTransaction(tx, &keystore, coins, {.sighash_type = SIGHASH_ALL}, input_errors);72assert(complete);73});74}76static void SignTransactionECDSA(benchmark::Bench& bench) { SignTransactionSingleInput(bench, InputType::P2WPKH); }77static void SignTransactionSchnorr(benchmark::Bench& bench) { SignTransactionSingleInput(bench, InputType::P2TR); }79static void SignSchnorrTapTweakBenchmark(benchmark::Bench& bench, bool use_null_merkle_root)80{81FastRandomContext rng;82ECC_Context ecc_context{};84auto key = GenerateRandomKey();85auto msg = rng.rand256();86auto merkle_root = use_null_merkle_root ? uint256() : rng.rand256();87auto aux = rng.rand256();88std::vector<unsigned char> sig(64);90bench.minEpochIterations(100).run([&] {91bool success = key.SignSchnorr(msg, sig, &merkle_root, aux);92assert(success);93});94}96static void SignSchnorrWithMerkleRoot(benchmark::Bench& bench)97{98SignSchnorrTapTweakBenchmark(bench, /*use_null_merkle_root=*/false);99}101static void SignSchnorrWithNullMerkleRoot(benchmark::Bench& bench)102{103SignSchnorrTapTweakBenchmark(bench, /*use_null_merkle_root=*/true);104}106BENCHMARK(SignTransactionECDSA);107BENCHMARK(SignTransactionSchnorr);108BENCHMARK(SignSchnorrWithMerkleRoot);109BENCHMARK(SignSchnorrWithNullMerkleRoot);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.