Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
Yudis-bit
left a comment
There was a problem hiding this comment.
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.
9590813 to
f9390ac
Compare
Updated the PR to skip |
| // 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; |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
Will do if I retouch.
| #include <script/script.h> | ||
| #include <script/sign.h> | ||
| #include <script/solver.h> | ||
| #include <script/verify_flags.h> |
There was a problem hiding this comment.
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.
f9390ac to
1f4c03f
Compare

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().