Merge bitcoin/bitcoin#36338: BIP-352: fix P2PKH pubkey extraction from malleated scriptSig
What changed, and why it matters
This update fixes a bug in how Bitcoin Core extracts public keys from certain old-style (P2PKH) transaction inputs when calculating Silent Payments. A troublemaker could tweak the input's script so that, during normal Bitcoin validation, the real public key is used, but the previous extraction method would pick a fake public key instead. That could cause a Silent Payments receiver to compute the wrong payment address and miss a payment. The fix manually scans the input for the public key whose fingerprint matches the output, instead of running the script with a dummy signature checker.
Apply the patch. Ensure downstream Silent Payments implementations that extract P2PKH pubkeys do not rely on EvalScript with a dummy checker; they should parse scriptSig directly and match the pubkey hash as specified by BIP-352.
Security signals we found
Silent Payments public-key extraction bug
P2PKH scriptSig third-party malleability
Dummy signature checker bypass
Wrong pubkey could be extracted from malleated input
Hash160-based pubkey matching fix
Evidence from the diff
In src/common/bip352.cpp, GetPubKeyFromInput previously used EvalScript with DUMMY_CHECKER to obtain the top stack item for P2PKH inputs. Because DUMMY_CHECKER accepts any non-empty signature as valid, a third-party-malleated scriptSig containing an OP_CHECKSIG that fails under real validation could leave an attacker-chosen wrong_pubkey on top of the stack. The patch replaces that approach with a manual GetOp iteration over scriptSig data pushes, returning the first 33-byte compressed key whose Hash160 matches the pubkey hash in the output script. A regression test is added in src/test/bip352_tests.cpp that builds a consensus-valid malleated scriptSig and verifies the correct pubkey is extracted.
Changed components
src/common/bip352.cppsrc/test/bip352_tests.cppBIP-352 Silent Payments receiver logicP2PKH input public key extractionInspect captured patch +66 / −8
### src/common/bip352.cpp
@@ -8,6 +8,7 @@
#include <bech32.h>
#include <chainparams.h>
#include <coins.h>
+#include <hash.h>
#include <key.h>
#include <primitives/transaction.h>
#include <pubkey.h>
@@ -179,18 +180,36 @@ std::optional<PubKey> GetPubKeyFromInput(const CTxIn& txin, const CScript& spk)
}
if (type == TxoutType::PUBKEYHASH) {
- std::vector<std::vector<unsigned char>> stack;
- if (!EvalScript(stack, txin.scriptSig, SCRIPT_VERIFY_NONE, DUMMY_CHECKER, SigVersion::BASE)) {
- return std::nullopt;
+ // For P2PKH public key extraction, BIP-352 states: "The receiver MUST parse the scriptSig
+ // for the public key, even if the scriptSig does not match the template specified. This is
+ // to address the third-party malleability of P2PKH scriptSigs."
+ //
+ // Find the preimage of the pubkey hash by iterating through the scriptSig data pushes and
+ // comparing the Hash160 of each 33-byte (compressed) push against the output script. In
+ // standard spends the pubkey is the last push, but due to malleability we can't rely on
+ // that (e.g. `<dummy> OP_DROP` inserted anywhere). Evaluating the scriptSig with a dummy
+ // signature checker isn't safe either, as a malleated scriptSig could then leave a
+ // different pubkey on top of the stack than real validation would.
+ const uint160 pubkey_hash{solutions[0]};
+ auto pc = txin.scriptSig.begin();
+ while (pc < txin.scriptSig.end()) {
+ opcodetype opcode;
+ std::vector<unsigned char> pubkey_candidate;
+ if (!txin.scriptSig.GetOp(pc, opcode, pubkey_candidate)) {
+ return std::nullopt;
+ }
+ if (pubkey_candidate.size() == 33 && Hash160(pubkey_candidate) == pubkey_hash) {
+ CPubKey key{pubkey_candidate};
+ if (key.IsFullyValid()) return PubKey{key};
+ }
}
- if (stack.empty()) return std::nullopt;
- CPubKey key{stack.back()};
- if (!key.IsCompressed() || !key.IsFullyValid()) return std::nullopt;
- return PubKey{key};
+ return std::nullopt;
}
if (type == TxoutType::SCRIPTHASH) {
- // P2SH-P2WPKH only: eval scriptSig, verify redeem script is P2WPKH.
+ // 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;
### src/test/bip352_tests.cpp
@@ -7,6 +7,10 @@
#include <coins.h>
#include <span.h>
#include <addresstype.h>
+#include <policy/policy.h>
+#include <script/interpreter.h>
+#include <script/script.h>
+#include <script/script_error.h>
#include <script/solver.h>
#include <test/data/bip352_send_and_receive_vectors.json.h>
@@ -273,6 +277,41 @@ BOOST_AUTO_TEST_CASE(bip352_scan_skips_invalid_taproot_outputs)
BOOST_CHECK(found_outputs->front().output == expected_output);
}
+BOOST_AUTO_TEST_CASE(bip352_p2pkh_pubkey_extraction_with_checksig_in_scriptsig)
+{
+ // P2PKH scriptSigs are third-party malleable. Construct one that contains an OP_CHECKSIG with a
+ // bogus (but strictly DER-encoded) signature, followed by a conditional push of a wrong pubkey:
+ //
+ // <sig> <pubkey> <bogus_sig> OP_OVER OP_CHECKSIG OP_IF <wrong_pubkey> OP_ENDIF
+ //
+ // In real validation the OP_CHECKSIG fails, the OP_IF branch is skipped and the final stack is
+ // <sig> <pubkey>, i.e. the spend is still valid and the pubkey must be extracted from it.
+ const CKey key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000001");
+ const CPubKey pubkey = key.GetPubKey();
+ const CPubKey wrong_pubkey = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000002").GetPubKey();
+ const CScript spk{GetScriptForDestination(PKHash{pubkey})};
+
+ CMutableTransaction tx;
+ tx.vin.emplace_back(COutPoint{Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000001").value(), 0});
+ std::vector<unsigned char> sig;
+ BOOST_REQUIRE(key.Sign(SignatureHash(spk, tx, 0, SIGHASH_ALL, /*amount=*/0, SigVersion::BASE), sig));
+ sig.push_back(SIGHASH_ALL);
+ const std::vector<unsigned char> bogus_sig{ParseHex("300602010102010101")}; // r=1, s=1, SIGHASH_ALL
+ tx.vin[0].scriptSig = CScript() << sig << ToByteVector(pubkey) << bogus_sig << OP_OVER << OP_CHECKSIG
+ << OP_IF << ToByteVector(wrong_pubkey) << OP_ENDIF;
+ const CTxIn& txin{tx.vin[0]};
+
+ // Verify that the malleated spend is consensus-valid
+ ScriptError serror = SCRIPT_ERR_OK;
+ BOOST_CHECK_MESSAGE(VerifyScript(txin.scriptSig, spk, nullptr, MANDATORY_SCRIPT_VERIFY_FLAGS,
+ MutableTransactionSignatureChecker(&tx, 0, /*amountIn=*/0, MissingDataBehavior::ASSERT_FAIL), &serror),
+ ScriptErrorString(serror));
+
+ const auto extracted_pubkey = GetPubKeyFromInput(txin, spk);
+ BOOST_REQUIRE(extracted_pubkey.has_value());
+ BOOST_CHECK(std::get<CPubKey>(*extracted_pubkey) == pubkey);
+}
+
BOOST_AUTO_TEST_CASE(bip352_label_serialize_roundtrip)
{
CKey scan_key = ParseHexToCKey("0000000000000000000000000000000000000000000000000000000000000002");Why this scored 62/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.