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. |
74fea92 to
8400fdb
Compare
8400fdb to
d403c68
Compare
d403c68 to
d48b8af
Compare
|
Concept ACK |
|
Tested ACK d48b8af
Every input this PR newly rejects already fails consensus: a P2SH scriptSig that is not push-only, a P2SH-P2WPKH scriptSig that is not a single push, a P2WPKH witness without exactly two items, or a redeem script or pubkey that does not match its hash. The multisig spend in the description is invalid, and the BIP-352 test vectors still pass. So this changes nothing for valid transactions: it is a robustness fix, not a fix for an exploitable bug. The new |
… GetPubKeyFromInput In GetPubKeyFromInput, evaluating TxoutType::SCRIPTHASH (P2SH-P2WPKH) inputs runs EvalScript under SCRIPT_VERIFY_NONE and passes the resulting redeem script to Solver(redeem, solutions). However, Solver immediately clears the solutions vector, discarding the original 20-byte script hash from the UTXO's scriptPubKey without checking that Hash160(redeem) matches it. As a result, any non-P2WPKH P2SH UTXO (such as multisig or timelock) spent by an input whose scriptSig pushes a valid P2WPKH redeem script is mistakenly treated as an eligible BIP-352 input, causing an unintended pubkey to be extracted and summed into A_sum, which silently corrupts the derived input_hash and shared secret. Additionally, GetPubKeyFromInput did not enforce the BIP-16 push-only constraint (!txin.scriptSig.IsPushOnly()), the BIP-141 single-element redeem script constraint (stack.size() == 1), standard two-element P2WPKH witness stack depths (txin.scriptWitness.stack.size() == 2), or that the extracted compressed public key hashes to the witness program (Hash160(key) == solutions[0]). Preserve the expected script hash before Solver runs, enforce BIP-16 push-only and single-element constraints, verify both redeem script and witness pubkey preimage hashes, and validate exact witness stack depths for both native P2WPKH and P2SH-P2WPKH inputs. Targeted unit tests in src/test/bip352_tests.cpp cover redeem script mismatches, non-push opcodes, multi-push scriptSigs, pubkey hash mismatches, uncompressed keys, and invalid stack sizes.
d48b8af to
c751d92
Compare
|
Good catch. Updated both the P2SH-P2WPKH and native P2WPKH test suites so the 3-item and 1-item witness stacks end with the valid matching compressed pubkey. On master, both cases now fail as expected since master extracts |
fametrano
left a comment
There was a problem hiding this comment.
re-ACK c751d92
I checked that the new witness tests cover the stack.size() != 2 checks, which my last review said were untested.
The range-diff from d48b8af changes only bip352_tests.cpp. The 3-item and 1-item witnesses now end with the matching pubkey.
On this head, bip352_tests, script_tests and transaction_tests pass. With only the two size() != 2 checks put back to empty(), exactly those four new assertions fail. With master's bip352.cpp, nine assertions fail.
rustaceanrob
left a comment
There was a problem hiding this comment.
Concept NACK adding this. GetPubKeyFromInput assumes inputs have been validated,. This adds pointless hashing to an operation that will be used extensively in scanning.
|
Fair point on confirmed blocks—for transactions already connected to the chain, The motivation was whether On performance, #36475 showed Happy to close this if that's the preferred contract, or scope the check strictly to P2SH (where |
Not relevant here
Scripts are validated before I don't think this is in scope for the module |
|
Makes sense. Closing. |

In
GetPubKeyFromInput(), evaluatingTxoutType::SCRIPTHASH(P2SH-P2WPKH) inputs runsEvalScriptunderSCRIPT_VERIFY_NONEand passes the resulting redeem script toSolver(redeem, solutions). BecauseSolverclears the passedsolutionsvector, the 20-byte script hash originally extracted from the UTXO'sscriptPubKeyis erased without checking thatHash160(redeem)matches it. Consequently, spending an arbitrary P2SH UTXO (such as a multisig or timelock) with ascriptSigthat pushes a valid P2WPKH redeem script causes an unintended public key to be extracted and summed intoA_sum, silently corrupting the Silent Payments input hash and derived shared secret.Additionally,
GetPubKeyFromInput()previously accepted malleated or non-standard witness stacks and scriptSigs by grabbingstack.back()without enforcing the BIP-16 push-only requirement (!txin.scriptSig.IsPushOnly()), the BIP-141 single-element redeem script constraint (stack.size() == 1), the standard two-element P2WPKH witness stack depth (txin.scriptWitness.stack.size() == 2), or verifying that the extracted compressed public key actually hashes to the target witness program (Hash160(key) == solutions[0]).This change preserves the expected 20-byte script hash prior to solver invocation, enforces BIP-16 push-only and single-element constraints, verifies both redeem script and witness pubkey preimage hashes, and validates exact witness stack depths for both native P2WPKH and P2SH-P2WPKH inputs. Targeted unit test coverage in
src/test/bip352_tests.cppverifies rejection of redeem script mismatches, non-push opcodes, multi-push scriptSigs, pubkey hash mismatches, uncompressed keys, and invalid stack sizes.