descriptor: compare x-only public keys in Tapscript Miniscript duplicate check

bitcoin/bitcoin: PR #36420

1 minor finding · 4 suggestions

Earlier concerns neither republished nor resolved: 1 major finding · 1 suggestion. See the Run archive for their assessments.

Findings

Link

[minor] · Persistence compatibility bypasses unrelated Miniscript checks

The persistence bypass now admits unrelated failures such as keyless wsh(1), although no normal-workflow producer of these newly accepted stored descriptors was found.

Location: src/script/descriptor.cpp:2750 (head).

Details and suggested fix

WalletDescriptor::FromStream and ExportWatchOnlyWallet previously enforced the full Miniscript acceptance check. Only duplicate-key detection changed, but require_sane=false also disables resource-limit, signature-requirement, non-malleability and timelock checks, plus IsNotSatisfiable(), for both P2WSH and Tapscript. A wallet record written outside the strict importdescriptors path can therefore bypass conditions unrelated to this compatibility requirement. The wsh(1) acceptance test illustrates this broader contract and requires the new empty-key construction path.

Use the old full-CPubKey ordering in persistence compatibility mode while retaining the other acceptance checks and the unconditional non-empty-key invariant. That preserves descriptors accepted under the former equivalence rule without accepting unrelated sanity failures. Update the compatibility tests to accept legacy parity-duplicate tr() descriptors but reject cases such as wsh(1).


Optional improvements

Link

[suggestion] · Legacy-descriptor regression test does not reach watch-only export

The new wallet test stops at LOAD_OK, so it cannot catch removal of the separate compatibility parse in ExportWatchOnlyWallet.

Location: src/wallet/export.cpp:95 (head).

Details and suggested fix

ExportWatchOnlyWallet reparses each public descriptor and requires descs.size() == 1. A stored parity-duplicate tr() descriptor would reach CHECK_NONFATAL if only this call reverted to strict parsing, but Test 3 exercises only PopulateWalletFromDB. The functional export tests create ordinary wallets or use strict importdescriptors, so they do not cover this compatibility case. Add a persisted-descriptor export-and-reload test that checks preservation of the descriptor and expanded scriptPubKey, plus disabled private keys. Keep a strict-parse rejection assertion for the same descriptor.


Link

[suggestion] · The duplicate-key fix precedes its wallet-compatibility prerequisite

The intermediate tree at 53439006 rejects previously loadable parity-duplicate tr() wallet records; 4e549915 restores loading, so the final tree is unaffected.

Location: src/script/descriptor.cpp:2315 at 53439006c631 (commit) (commit).

Details and suggested fix

53439006c63179ca639f97bbb0291ff7944654c5 tightens KeyParser::KeyCompare, but its WalletDescriptor::FromStream still enforces sanity through the old Parse() call. Raw 33-byte keys retain their parity prefixes when serialized, so previously accepted parity-duplicate wallet records are rejected at this intermediate revision. Only 4e549915cc7ae52d075273558ecc1829076c50f1 adds the parsing option and switches wallet loading and watch-only export to compatibility mode.

Move that API/caller preparation before the comparator change, keeping its permissive-parser tests with it and the strict rejection tests with the comparator. Include Test 3 with the comparator change; its record already loads under the old comparison. Alternatively, squash the two commits so the compatibility handling cannot be omitted from the backportable fix.


Link

[suggestion] · Legacy parity-duplicate descriptors load without a warning

Wallets silently retain previously accepted parity-duplicate tr() descriptors, although importdescriptors now rejects the same strings.

Location: src/wallet/walletutil.h:143 (head).

Details and suggested fix

FromStream uses compatibility parsing and returns the descriptor without recording that strict parsing would reject it. Neither DescriptorScriptPubKeyMan::Load() nor PopulateWalletFromDB() surfaces that diagnostic, so exporting the string and re-importing it can be the first time the user discovers the changed acceptance rule. Record when a stored descriptor needs compatibility mode and add a load warning identifying the affected descriptor and duplicate-key issue, without disabling it. Document the stricter import behavior in a release note.


Link

[suggestion] · Parity-duplicate keys are absent from miniscript_tests

Current miniscript_tests assertions do not distinguish the new Tapscript x-only comparison from full-CPubKey comparison.

Location: src/test/miniscript_tests.cpp:139 (head).

Details and suggested fix

The explicit ms_dup1 through ms_dup4 cases use wsh_converter, and the generated keys use scalars 1 through 255 without their negations. Thus reverting only KeyConverter::KeyCompare would leave the existing outcomes unchanged. Parse or_i(pk(02X),pk(03X)) for one valid x-coordinate with both converters. Assert top-level validity in both contexts, failed CheckDuplicateKey() and IsSane() for Tapscript, and success for P2WSH. This would exercise the test helper's serialization-equivalence invariant.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Miniscript descriptor support originally distinguished validity from sanity deliberately: well-typed expressions were not sufficient. Sanity additionally covered policy/script correspondence, consensus and standardness constraints, non-malleable satisfaction, compatible timelock units, and absence of duplicate keys. These requirements were already discussed for the initial watch-only implementation.

The checks being bypassed are a collection of independent safety guarantees, not merely a duplicate-key filter. Their historical purpose was broader than ensuring descriptors could be parsed or scripts reconstructed, including in watch-only use.

Source 1

Non-malleability remained an explicit goal after Segwit because an adversary without private keys could still alter witnesses, potentially increasing transaction weight and lowering effective feerate or harming propagation. Satisfaction selection also accounted for adversaries being able to remove signatures but not forge new ones.

Duplicate-key sanity belongs to a security model concerned with third-party transformations of an existing satisfaction, rather than just script execution success or transaction-ID stability. Preserving an existing descriptor's readability is therefore distinct from retaining the original satisfaction guarantees.

Source 1

Ralph's inferences

Before this change, persisted-descriptor loading and watch-only export shared the normal descriptor acceptance boundary rather than having a separate permissive decoding mode. Loading rejected descriptors that Parse could not reconstruct; export re-parsed public descriptors and asserted that each produced exactly one descriptor without private keys.

The base behavior treated stored and exported descriptors as still acceptable under the current parser. At the PR head, loading explicitly opts out of sanity and unsatisfiability rejection. The compatibility adjustment therefore changes a pre-existing boundary, rather than merely retaining an established unchecked persistence path.

The satisfaction API's guarantees are conditional and stronger than top-level type validity. ValidSatisfactions checks operation and stack limits; IsSane also requires non-malleability, compatible timelocks, no duplicate keys, and a signature requirement. The API promises successful satisfaction when conditions are met if IsSane holds, and its default mode refuses satisfactions marked malleable or lacking a signature.

Keeping IsValidTopLevel alone preserves type and script-size validity, but does not preserve the documented satisfaction contract. The explicit IsNotSatisfiable rejection is also a separate acceptance condition in the affected parser; it should not be conflated with either type validity or possession of signing material.

The inspected evidence does not establish that compatibility requires bypassing every sanity and unsatisfiability check. Accommodating descriptors accepted under the former public-key equivalence rule is a narrower compatibility requirement than accepting every well-typed expression that fails any of those checks.

At the pinned head, require_sane=false disables both IsSane and IsNotSatisfiable rejection, including unrelated checks for resource limits, signature requirements, malleability, and mixed timelocks. Historical rationale explains why those guarantees existed, but supplies no evidence that previously persisted x-only duplicate-key descriptors require relinquishing all of them. This is a distinction in compatibility scope, not a judgment on the implementation.

Source 1