bench: add BIP-352 pubkey extraction benchmarks (standard + P2PKH worst-case) #36475

Manually merged
theStack merged 2 commits from bip352-bench-pubkey_extraction into master 2026-11-06 12:03:43 +00:00 AGit
theStack commented 2026-10-08 14:30:11 +00:00 (Migrated from github.com)

As there are ongoing changes to the Silent Payments public key extraction function GetPubKeyFromInput (see recently merged #36338), it seems reasonable to add benchmarks for it. The first commit covers the standard shape of each eligible input type (P2PKH, P2SH-P2WPKH, P2WPKH and P2TR script-path spend; see BIP-352). The second adds a worst-case P2PKH input with a maximally malleated scriptSig, where every push has to be hashed during the scan.

Results on my arm64 machine (as per $ ./build/bin/bench_bitcoin -filter='BIP352_.*'):

ns/input input/s err% total benchmark
3,034.12 329,584.35 0.2% 0.01 BIP352_GetPubKey_P2PKH
69,868.20 14,312.66 0.1% 0.01 BIP352_GetPubKey_P2PKH_WorstCase
2,841.35 351,945.44 0.1% 0.01 BIP352_GetPubKey_P2SH_P2WPKH
2,768.00 361,272.01 0.1% 0.01 BIP352_GetPubKey_P2TR
2,757.57 362,637.66 0.0% 0.01 BIP352_GetPubKey_P2WPKH
As there are ongoing changes to the Silent Payments public key extraction function `GetPubKeyFromInput` (see recently merged #36338), it seems reasonable to add benchmarks for it. The first commit covers the standard shape of each eligible input type (P2PKH, P2SH-P2WPKH, P2WPKH and P2TR script-path spend; see [BIP-352](https://github.com/bitcoin/bips/blob/master/bip-0352.mediawiki#user-content-Inputs_For_Shared_Secret_Derivation)). The second adds a worst-case P2PKH input with a maximally malleated scriptSig, where every push has to be hashed during the scan. Results on my arm64 machine (as per `$ ./build/bin/bench_bitcoin -filter='BIP352_.*'`): | ns/input | input/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 3,034.12 | 329,584.35 | 0.2% | 0.01 | `BIP352_GetPubKey_P2PKH` | 69,868.20 | 14,312.66 | 0.1% | 0.01 | `BIP352_GetPubKey_P2PKH_WorstCase` | 2,841.35 | 351,945.44 | 0.1% | 0.01 | `BIP352_GetPubKey_P2SH_P2WPKH` | 2,768.00 | 361,272.01 | 0.1% | 0.01 | `BIP352_GetPubKey_P2TR` | 2,757.57 | 362,637.66 | 0.0% | 0.01 | `BIP352_GetPubKey_P2WPKH`
DrahtBot commented 2026-10-08 14:30:22 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK Yudis-bit, fametrano, Eunovo
Concept ACK w0xlt

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36475 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36475.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36475#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [Yudis-bit](https://github.com/bitcoin/bitcoin/pull/36475#issuecomment-6233551410), [fametrano](https://github.com/bitcoin/bitcoin/pull/36475#issuecomment-6310687100), [Eunovo](https://github.com/bitcoin/bitcoin/pull/36475#pullrequestreview-5785489409) | | Concept ACK | [w0xlt](https://github.com/bitcoin/bitcoin/pull/36475#issuecomment-6065831573) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: 228877d7836a55c1d6fde90b0b7ba2d4c90248ab
Head: 56c0f168d1a81b19e4cd71ef81e7ce2d9c1624e3

Findings

Suggestions

Add or label the P2TR key-path benchmark (src/bench/bip352.cpp:68, head)

This witness includes a leaf script and control block, so it exercises the script-path branch; a key-path witness skips those checks. Because this is the only P2TR benchmark and the PR description identifies the standard case as key-path, its timing is not a key-path baseline. Add a separate key-path case, or scope the benchmark and description explicitly to script-path so readers do not mistake its result for standard key-path timing.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `228877d7836a55c1d6fde90b0b7ba2d4c90248ab` Head: `56c0f168d1a81b19e4cd71ef81e7ce2d9c1624e3` ##### Findings **Suggestions** **Add or label the P2TR key-path benchmark** (src/bench/bip352.cpp:68, head) This witness includes a leaf script and control block, so it exercises the script-path branch; a key-path witness skips those checks. Because this is the only P2TR benchmark and the PR description identifies the standard case as key-path, its timing is not a key-path baseline. Add a separate key-path case, or scope the benchmark and description explicitly to script-path so readers do not mistake its result for standard key-path timing. [Full review report](<https://review.fish.foo/traces/36475-56c0f168d1a81b19e4cd71ef81e7ce2d9c1624e3-672-5.html>)
w0xlt commented 2026-10-08 17:52:20 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
Yudis-bit commented 2026-10-09 02:53:47 +00:00 (Migrated from github.com)

Concept and approach ACK.

Numbers make sense given that IsFullyValid() point validation dominates the standard paths. In P2PKH_WorstCase, hoisting pubkey_candidate outside the while loop in GetPubKeyFromInput would save ~290 heap reallocations across the dummy pushes.

It might also be worth adding P2TR_ScriptPath and P2TR_ScriptPath_NUMS to benchmark the control-block validation and the early rejection path.

Concept and approach ACK. Numbers make sense given that `IsFullyValid()` point validation dominates the standard paths. In `P2PKH_WorstCase`, hoisting `pubkey_candidate` outside the while loop in `GetPubKeyFromInput` would save ~290 heap reallocations across the dummy pushes. It might also be worth adding `P2TR_ScriptPath` and `P2TR_ScriptPath_NUMS` to benchmark the control-block validation and the early rejection path.
theStack commented 2026-10-10 17:07:26 +00:00 (Migrated from github.com)

It might also be worth adding P2TR_ScriptPath and P2TR_ScriptPath_NUMS to benchmark the control-block validation and the early rejection path.

Due to the strong similarity I think it's fine to have only a single representative P2TR benchmark, but I switched the one from key-path to script-path spending as the pubkey extraction contains (slightly) more logic. Also fixed the IWYU CI error. Thanks for the review!

> It might also be worth adding `P2TR_ScriptPath` and `P2TR_ScriptPath_NUMS` to benchmark the control-block validation and the early rejection path. Due to the strong similarity I think it's fine to have only a single representative P2TR benchmark, but I switched the one from key-path to script-path spending as the pubkey extraction contains (slightly) more logic. Also fixed the IWYU CI error. Thanks for the review!
Yudis-bit commented 2026-10-17 11:51:23 +00:00 (Migrated from github.com)

re-tACK 56c0f168d1

Makes sense to stick to one P2TR case. Windows numbers:

ns/input input/s err% total benchmark
4,660.44 214,571.81 0.2% 0.01 BIP352_GetPubKey_P2PKH
81,266.67 12,305.17 0.2% 0.01 BIP352_GetPubKey_P2PKH_WorstCase
4,624.56 216,236.72 0.4% 0.01 BIP352_GetPubKey_P2SH_P2WPKH
4,330.00 230,946.88 0.6% 0.01 BIP352_GetPubKey_P2TR
4,335.53 230,652.50 0.2% 0.01 BIP352_GetPubKey_P2WPKH
re-tACK 56c0f168d1 Makes sense to stick to one P2TR case. Windows numbers: | ns/input | input/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 4,660.44 | 214,571.81 | 0.2% | 0.01 | `BIP352_GetPubKey_P2PKH` | 81,266.67 | 12,305.17 | 0.2% | 0.01 | `BIP352_GetPubKey_P2PKH_WorstCase` | 4,624.56 | 216,236.72 | 0.4% | 0.01 | `BIP352_GetPubKey_P2SH_P2WPKH` | 4,330.00 | 230,946.88 | 0.6% | 0.01 | `BIP352_GetPubKey_P2TR` | 4,335.53 | 230,652.50 | 0.2% | 0.01 | `BIP352_GetPubKey_P2WPKH`
fametrano commented 2026-10-21 07:37:23 +00:00 (Migrated from github.com)

tACK 56c0f168d1

The five benchmarks build and run on an Apple M5 (Release build, bench_bitcoin -filter='BIP352_.*' -min-time=2000, three runs). The four standard inputs take 2.2-2.6 µs each. The worst case, a 10,000-byte P2PKH scriptSig with 291 dummy pushes, takes 39-43 µs. I also rebuilt the five input shapes independently. A separate BIP352 implementation (btclib-wallet) extracts the expected key from each.

tACK 56c0f168d1 The five benchmarks build and run on an Apple M5 (Release build, `bench_bitcoin -filter='BIP352_.*' -min-time=2000`, three runs). The four standard inputs take 2.2-2.6 µs each. The worst case, a 10,000-byte P2PKH scriptSig with 291 dummy pushes, takes 39-43 µs. I also rebuilt the five input shapes independently. A separate BIP352 implementation (btclib-wallet) extracts the expected key from each.
Eunovo (Migrated from github.com) approved these changes 2026-11-05 13:14:48 +00:00
Eunovo (Migrated from github.com) left a comment
ACK https://github.com/bitcoin/bitcoin/pull/36475/commits/56c0f168d1a81b19e4cd71ef81e7ce2d9c1624e3
fanquake commented 2026-11-24 15:01:20 +00:00 (Migrated from github.com)

This introduced an ODR violation. Followed up here: #36966.

This introduced an ODR violation. Followed up here: #36966.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36475
No description provided.