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

pull Eunovo wants to merge 3 commits into bitcoin:master from Eunovo:bip352-skip-empty-witness-stack-psh-pwpkh changing 5 files +71 −13
  1. Eunovo commented at 1:58 PM on November 5, 2026: 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().

  2. DrahtBot commented at 1:58 PM on November 5, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    Type Reviewers
    ACK Yudis-bit, theStack

    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.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36721 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36721.svg"></sub> (script: store interpreter stack elements as reference-counted objects by conduition)
    • #36521 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36521.svg"></sub> (Keep witness stacks in serialized form in CScriptWitness by sipa)
    • #36360 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36360.svg"></sub> (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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. Yudis-bit commented at 2:17 PM on November 6, 2026: none

    Tested ACK 959081325d

    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.

  4. sedited requested review from theStack on Nov 6, 2026
  5. bench: add BIP-352 P2SH multisig pubkey extraction benchmark 53bb2b901f
  6. script: add CScript::IsPayToWitnessPubKeyHash() 8325f12fa8
  7. bip352: match P2SH-P2WPKH scriptSig instead of evaluating it
    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.
    f9390ac89a
  8. Eunovo force-pushed on Nov 9, 2026
  9. Eunovo renamed this:
    bip352: skip scriptSig eval for non-segwit P2SH inputs
    bip352: match P2SH-P2WPKH instead of evaluating it
    on Nov 9, 2026
  10. Eunovo commented at 1:23 PM on November 9, 2026: contributor

    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#issue-6224626766

  11. Yudis-bit commented at 2:13 PM on November 9, 2026: none

    Tested ACK f9390ac89a

    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.

  12. in src/common/bip352.cpp:212 in f9390ac89a
     217 | -        if (Solver(redeem, solutions) != TxoutType::WITNESS_V0_KEYHASH) return std::nullopt;
     218 | +        // P2SH-P2WPKH only.
     219 | +        const CScript& script_sig{txin.scriptSig};
     220 | +        // A single direct push: the first byte is the length of the rest of the scriptSig.
     221 | +        if (script_sig.empty() || script_sig[0] != script_sig.size() - 1) return std::nullopt;
     222 | +        if (!CScript{script_sig.begin() + 1, script_sig.end()}.IsPayToWitnessPubKeyHash()) return std::nullopt;
    


    theStack commented at 2:18 PM on November 9, 2026:

    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>


    Eunovo commented at 10:20 AM on November 10, 2026:

    Will do if I retouch.

  13. in src/common/bip352.cpp:19 in f9390ac89a
      13 | @@ -14,9 +14,7 @@
      14 |  #include <pubkey.h>
      15 |  #include <script/interpreter.h>
      16 |  #include <script/script.h>
      17 | -#include <script/sign.h>
      18 |  #include <script/solver.h>
      19 | -#include <script/verify_flags.h>
    


    theStack commented at 2:36 PM on November 9, 2026:

    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

  14. theStack approved
  15. theStack commented at 2:42 PM on November 9, 2026: contributor

    ACK f9390ac89a9c2241636a082322c3c9c0ec202531 (modulo a comment suggestion nit)

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


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-16 01:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me