Skip to content

bip352: match P2SH-P2WPKH instead of evaluating it - #36776

Open
Eunovo wants to merge 3 commits into
bitcoin:masterfrom
Eunovo:bip352-skip-empty-witness-stack-psh-pwpkh
Open

Eunovo wants to merge 3 commits into
bitcoin:masterfrom
Eunovo:bip352-skip-empty-witness-stack-psh-pwpkh

Conversation

@Eunovo

@Eunovo Eunovo commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

For P2SH inputs, GetPubKeyFromInput evaluated the full scriptSig with EvalScript and then ran Solver on 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 to MAX_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 exactly 0x16 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:

ns/input input/s err% total benchmark
688.30 1,452,858.07 0.1% 2.20 BIP352_GetPubKey_P2SH_Multisig
7,158.19 139,700.11 0.1% 2.20 BIP352_GetPubKey_P2SH_P2WPKH

After:

ns/input input/s err% total benchmark
56.87 17,584,611.40 0.0% 2.20 BIP352_GetPubKey_P2SH_Multisig
6,949.45 143,896.18 0.0% 2.20 BIP352_GetPubKey_P2SH_P2WPKH

P2SH multisig is about 12x faster to rule out. P2SH-P2WPKH saves about 200 ns per input; its time is still dominated by CPubKey::IsFullyValid().

@DrahtBot

DrahtBot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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
Stale ACK Yudis-bit, theStack

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:

  • #36360 (test: add tests in script test suites covering script.cpp live mutants by Yudis-bit)

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.

@Yudis-bit Yudis-bit left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ACK 9590813

Benchmarked GetPubKeyFromInput on 2-of-3 multisig P2SH: drops from 1191 to 94 cycles on Zen 2 (-O2). All bip352_tests pass.

Since interpreter.cpp:2092 requires scriptSig == 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 skip EvalScript and Solver entirely:

-        std::vector<std::vector<unsigned char>> stack;
-        if (!EvalScript(stack, txin.scriptSig, SCRIPT_VERIFY_NONE, DUMMY_CHECKER, SigVersion::BASE)) {
-            return std::nullopt;
-        }
-        if (stack.empty()) return std::nullopt;
-        CScript redeem{stack.back().begin(), stack.back().end()};
-        if (Solver(redeem, solutions) != TxoutType::WITNESS_V0_KEYHASH) return std::nullopt;
+        if (txin.scriptSig.size() != 23 ||
+            txin.scriptSig[0] != 22 ||
+            txin.scriptSig[1] != OP_0 ||
+            txin.scriptSig[2] != WITNESS_V0_KEYHASH_SIZE) {
+            return std::nullopt;
+        }

That also avoids EvalScript when the witness is non-empty (P2SH-P2WSH drops from 357 to 131 cycles), and removes the interpreter dependency on CScriptWitness ahead of #36521. Fine either way.

@sedited
sedited requested a review from theStack November 6, 2026 20:20
@Eunovo
Eunovo force-pushed the bip352-skip-empty-witness-stack-psh-pwpkh branch from 9590813 to f9390ac Compare November 9, 2026 13:17
@Eunovo Eunovo changed the title bip352: skip scriptSig eval for non-segwit P2SH inputs bip352: match P2SH-P2WPKH instead of evaluating it Nov 9, 2026
@Eunovo

Eunovo commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Benchmarked GetPubKeyFromInput on 2-of-3 multisig P2SH: drops from 1191 to 94 cycles on Zen 2 (-O2). All bip352_tests pass.

Since interpreter.cpp:2092 requires scriptSig == 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 skip EvalScript and Solver entirely:

Updated the PR to skip EvalScript entirely. I also added a benchmark for P2SH-MULTISIG, see results in #36776 (comment)

@Yudis-bit Yudis-bit left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ACK f9390ac

Ran bip352_tests and script_segwit_tests cleanly on Linux x86_64. BIP352_GetPubKey_P2SH_Multisig drops to 127 cycles with the single-push check avoiding EvalScript.

@theStack theStack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f9390ac
(modulo a comment suggestion nit)

Seems reasonable to not involve the script interpreter for pubkey extraction at all.

Comment thread src/common/bip352.cpp Outdated
Comment on lines +210 to +212
// P2SH-P2WPKH only: eval scriptSig, verify redeem script is P2WPKH. Unlike P2PKH above,
// evaluating is safe here, as consensus (BIP-141) requires the scriptSig to be a single
// push of the redeem script.
std::vector<std::vector<unsigned char>> stack;
if (!EvalScript(stack, txin.scriptSig, SCRIPT_VERIFY_NONE, DUMMY_CHECKER, SigVersion::BASE)) {
return std::nullopt;
}
if (stack.empty()) return std::nullopt;
CScript redeem{stack.back().begin(), stack.back().end()};
if (Solver(redeem, solutions) != TxoutType::WITNESS_V0_KEYHASH) return std::nullopt;
// P2SH-P2WPKH only.
const CScript& script_sig{txin.scriptSig};
// A single direct push: the first byte is the length of the rest of the scriptSig.
if (script_sig.empty() || script_sig[0] != script_sig.size() - 1) return std::nullopt;
if (!CScript{script_sig.begin() + 1, script_sig.end()}.IsPayToWitnessPubKeyHash()) return std::nullopt;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 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:

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 exactly 0x16 OP_0 0x14 <20-byte keyhash>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do if I retouch.

Comment thread src/common/bip352.cpp Outdated
#include <script/script.h>
#include <script/sign.h>
#include <script/solver.h>
#include <script/verify_flags.h>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for other reviewers: I assumed the script/interpreter.h include could be removed now that we don't call EvalScript anymore, but it's still needed for the taproot control block size constants in the NUMS_H skipping logic

Consensus requires the scriptSig of a P2SH-wrapped witness program to
be exactly a single push of the redeem script, so a P2SH-P2WPKH
scriptSig is always <0x16 OP_0 0x14 <20-byte hash>>. Match that
template directly. This avoids evaluating scriptSigs of up to
MAX_SCRIPT_SIZE for non-segwit P2SH spends, and drops the
EvalScript dependency.
@Eunovo
Eunovo force-pushed the bip352-skip-empty-witness-stack-psh-pwpkh branch from f9390ac to 1f4c03f Compare November 27, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants