mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 11:53:39 +00:00
bip352: match P2SH-P2WPKH instead of evaluating it #36776
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36776
Loading…
Reference in a new issue
No description provided.
Delete branch "bip352-skip-empty-witness-stack-psh-pwpkh"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
For P2SH inputs,
GetPubKeyFromInputevaluated the full scriptSig withEvalScriptand then ranSolveron the redeem script, even though only P2SH-P2WPKH is eligible for BIP-352. That costs time on every non-segwit P2SH spend (e.g. multisig), whose scriptSigs can be up toMAX_SCRIPT_SIZE.BIP-141 requires the scriptSig of a P2SH-wrapped witness program to be exactly a single push of the redeem script (
SCRIPT_ERR_WITNESS_MALLEATED_P2SH). So any valid P2SH-P2WPKH scriptSig is exactly0x16 OP_0 0x14 <20-byte keyhash>, and matching that template gives the same result without evaluating anything.Benchmarks
bench_bitcoin -filter='BIP352_GetPubKey_P2SH_.*' -min-time=2000, Release build.Before:
BIP352_GetPubKey_P2SH_MultisigBIP352_GetPubKey_P2SH_P2WPKHAfter:
BIP352_GetPubKey_P2SH_MultisigBIP352_GetPubKey_P2SH_P2WPKHP2SH multisig is about 12x faster to rule out. P2SH-P2WPKH saves about 200 ns per input; its time is still dominated by
CPubKey::IsFullyValid().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.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
🔵 Suggestion · The P2SH template check loses its consensus rationale
The replacement comment describes the byte layout but omits the consensus requirement that makes exact matching safe here, unlike the adjacent malleable P2PKH case.
Location:
src/common/bip352.cpp:211(head).🔵 Suggestion · P2SH extraction tests miss the newly rejected
scriptSigformsThe tests do not cover the newly rejected non-canonical P2SH-P2WPKH
scriptSigforms or verify that consensus rejects them, leaving the exact-match optimization's safety boundary untested.Location:
src/common/bip352.cpp:212(head).Review coverage was incomplete.
Reviewed:
1f4c03f444e2· Review report · Previous reportspermalink
Tested ACK
959081325dBenchmarked GetPubKeyFromInput on 2-of-3 multisig P2SH: drops from 1191 to 94 cycles on Zen 2 (-O2). All bip352_tests pass.
Since
interpreter.cpp:2092requiresscriptSig == CScript() << redeem, any valid P2SH-P2WPKH scriptSig is always a single push of the 22-byte program ([22, OP_0, 20, <hash>], 23 bytes total). Could skipEvalScriptandSolverentirely:That also avoids
EvalScriptwhen the witness is non-empty (P2SH-P2WSH drops from 357 to 131 cycles), and removes the interpreter dependency onCScriptWitnessahead of #36521. Fine either way.Updated the PR to skip
EvalScriptentirely. I also added a benchmark forP2SH-MULTISIG, see results in https://github.com/bitcoin/bitcoin/pull/36776#issue-6224626766Tested ACK
f9390ac89aRan
bip352_testsandscript_segwit_testscleanly on Linux x86_64.BIP352_GetPubKey_P2SH_Multisigdrops to 127 cycles with the single-push check avoidingEvalScript.ACK
f9390ac89a(modulo a comment suggestion nit)
Seems reasonable to not involve the script interpreter for pubkey extraction at all.
note for other reviewers: I assumed the
script/interpreter.hinclude could be removed now that we don't callEvalScriptanymore, but it's still needed for the taproot control block size constants in the NUMS_H skipping logicnit: I'd keep the consensus rule for P2SH-P2WPKH (BIP 141) mentioned in the comments for clarity that only one (minimal push) encoding is possible, e.g. something like what you have in the PR description:
Will do if I retouch.
🐙 This pull request conflicts with the target branch and needs rebase.