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

Open
Yudis-bit wants to merge 2 commits from tr-miniscript-xonly-dupkeys into master AGit
Yudis-bit commented 2026-10-02 15:45:05 +00:00 (Migrated from github.com)

In Tapscript Miniscript descriptors (tr()), public keys serialize as 32-byte x-only pubkeys per BIP-340, BIP-342, and BIP-379, discarding parity. However, KeyParser::KeyCompare in src/script/descriptor.cpp compared keys as full 33-byte CPubKeys, retaining the parity byte.

When a descriptor contains public keys that differ only in parity prefix (such as pk(X) alongside pk(03X) where X defaults to 02, or pk(02X) alongside pk(03X)), KeyParser::KeyCompare treated them as distinct keys. Miniscript duplicate key checking passed, declaring the descriptor sane even though both branches serialize on-chain to <X> OP_CHECKSIG. Because a single signature satisfies both branches, third parties could malleate transactions by altering branch selectors without holding private keys, violating BIP-379 non-malleability invariants.

This patch updates KeyParser::KeyCompare in src/script/descriptor.cpp and KeyConverter::KeyCompare in src/test/miniscript_tests.cpp to compare keys as XOnlyPubKey whenever miniscript::IsTapscript(m_script_ctx) evaluates true. Under Tapscript context, keys sharing identical x-coordinates compare equivalent (!comp(a, b) && !comp(b, a)), ensuring DuplicateKeyCheck() detects the collision and rejects the descriptor. In src/test/descriptor_tests.cpp, unit tests verify that tr() descriptors containing parity-differing duplicate keys (including public hex and private WIF formats) are rejected as not sane, while wsh() descriptors continue to treat 33-byte compressed keys with differing parities as distinct.

Fixes #36414.

In Tapscript Miniscript descriptors (`tr()`), public keys serialize as 32-byte x-only pubkeys per BIP-340, BIP-342, and BIP-379, discarding parity. However, `KeyParser::KeyCompare` in `src/script/descriptor.cpp` compared keys as full 33-byte `CPubKey`s, retaining the parity byte. When a descriptor contains public keys that differ only in parity prefix (such as `pk(X)` alongside `pk(03X)` where `X` defaults to `02`, or `pk(02X)` alongside `pk(03X)`), `KeyParser::KeyCompare` treated them as distinct keys. Miniscript duplicate key checking passed, declaring the descriptor sane even though both branches serialize on-chain to `<X> OP_CHECKSIG`. Because a single signature satisfies both branches, third parties could malleate transactions by altering branch selectors without holding private keys, violating BIP-379 non-malleability invariants. This patch updates `KeyParser::KeyCompare` in `src/script/descriptor.cpp` and `KeyConverter::KeyCompare` in `src/test/miniscript_tests.cpp` to compare keys as `XOnlyPubKey` whenever `miniscript::IsTapscript(m_script_ctx)` evaluates true. Under Tapscript context, keys sharing identical x-coordinates compare equivalent (`!comp(a, b) && !comp(b, a)`), ensuring `DuplicateKeyCheck()` detects the collision and rejects the descriptor. In `src/test/descriptor_tests.cpp`, unit tests verify that `tr()` descriptors containing parity-differing duplicate keys (including public hex and private WIF formats) are rejected as not sane, while `wsh()` descriptors continue to treat 33-byte compressed keys with differing parities as distinct. Fixes #36414.
Copilot (Migrated from github.com) reviewed 2026-10-02 15:45:12 +00:00
Copilot (Migrated from github.com) left a comment

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
DrahtBot commented 2026-10-02 15:45:14 +00:00 (Migrated from github.com)

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

External sites

Reviews

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

Type Reviewers
Concept ACK fametrano
Stale ACK Zeegaths

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36005 (rpc, descriptor: warn when multipath expansion clones single-path key expressions by pythcoiner)

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.

LLM Linter (✨ experimental)

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/wallet/test/walletload_tests.cpp: WalletDescriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0)

2026-11-25 15:40:57

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36420 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36420.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36420#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [fametrano](https://github.com/bitcoin/bitcoin/pull/36420#pullrequestreview-5884151928) | | Stale ACK | [Zeegaths](https://github.com/bitcoin/bitcoin/pull/36420#issuecomment-6821213872) | 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: * [#36005](https://github.com/bitcoin/bitcoin/pull/36005) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36005.svg"></sub> (rpc, descriptor: warn when multipath expansion clones single-path key expressions by pythcoiner) 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--> ### LLM Linter (✨ experimental) Possible places where named args for integral literals may be used (e.g. `func(x, /*named_arg=*/0)` in C++, and `func(x, named_arg=0)` in Python): - `src/wallet/test/walletload_tests.cpp`: `WalletDescriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0)` <sup>2026-11-25 15:40:57</sup>
First-time contributor

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

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

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

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

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

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

Review coverage was incomplete.


Reviewed: 4e549915cc7a · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:806938d40e279b6d701b2e78128bb1a09b55193a0b697245a1dce6241d3081c1:3607c760b63c01fb7f1f349adfc3fd0614b148a7df02e42f2f0edc2d448925a1 --> <!-- ralph-base:5ba05739881855cba25c3c05d6f82a61b8a0665f --> <!-- ralph-head:4e549915cc7ae52d075273558ecc1829076c50f1 --> <!-- ralph-case:42b93c120d5a757391825b64967df0d53b2b8557d0b7e8bf91276e7ec091dd93 --> Earlier concerns neither republished nor resolved: 1 major finding · 1 suggestion. See the Run archive linked from the Review report for their assessments. **🟠 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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4e549915cc7ae52d075273558ecc1829076c50f1/src/script/descriptor.cpp#L2750>) (head). **🔵 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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4e549915cc7ae52d075273558ecc1829076c50f1/src/wallet/export.cpp#L95>) (head). **🔵 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 (commit). **🔵 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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4e549915cc7ae52d075273558ecc1829076c50f1/src/wallet/walletutil.h#L143>) (head). **🔵 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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4e549915cc7ae52d075273558ecc1829076c50f1/src/test/miniscript_tests.cpp#L139>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`4e549915cc7a`](<https://git.fish.foo/bitcoin/bitcoin/commit/4e549915cc7ae52d075273558ecc1829076c50f1>) · [Review report](<https://review.fish.foo/report/36420-4e549915cc7ae52d075273558ecc1829076c50f1-3134-6.html>) · [Previous reports](<https://review.fish.foo/traces/36420/>) [permalink](<https://review.fish.foo/report/36420-4e549915cc7ae52d075273558ecc1829076c50f1-3134-6.html>)
fametrano (Migrated from github.com) reviewed 2026-10-02 16:36:23 +00:00
fametrano (Migrated from github.com) left a comment

Tested at cb85c2cdde. descriptor_tests fails on the multi_a check because that descriptor parses: a multi_a directly in tr() is not Miniscript and has no duplicate-key check, so even multi_a(2,X,X) is accepted. wallet_taproot.py repeats a key inside one multi_a, so refusing duplicates there would be a separate change. I suggest dropping the check here and in the description. The two or_i cases are refused with this change and accepted without it.

Tested at cb85c2cdde. `descriptor_tests` fails on the `multi_a` check because that descriptor parses: a `multi_a` directly in `tr()` is not Miniscript and has no duplicate-key check, so even `multi_a(2,X,X)` is accepted. `wallet_taproot.py` repeats a key inside one `multi_a`, so refusing duplicates there would be a separate change. I suggest dropping the check here and in the description. The two `or_i` cases are refused with this change and accepted without it.
fametrano (Migrated from github.com) commented 2026-10-02 16:36:24 +00:00

Optional: the same case with the second key as a WIF whose public key is 03X. Without the descriptor.cpp change this private descriptor is accepted, while its public form (two identical x-only keys) is already refused:

                    "or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys");
    CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(KztWktoNG1biXs3GSD6M7YDEynGaQPgjLguiWhMXiJM7sDA5vZFt)))",
                    "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))",
                    "or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys");
Optional: the same case with the second key as a WIF whose public key is `03X`. Without the `descriptor.cpp` change this private descriptor is accepted, while its public form (two identical x-only keys) is already refused: ```suggestion "or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys"); CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(KztWktoNG1biXs3GSD6M7YDEynGaQPgjLguiWhMXiJM7sDA5vZFt)))", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", "or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys"); ```
fametrano (Migrated from github.com) commented 2026-10-02 16:36:23 +00:00

This descriptor parses, so this check fails:

This descriptor parses, so this check fails: ```suggestion ```
Yudis-bit commented 2026-10-03 06:27:38 +00:00 (Migrated from github.com)

Good catch on multi_a parsing directly as MultiADescriptor rather than Miniscript. Dropped that test and updated the PR description. Thanks for testing!

Good catch on `multi_a` parsing directly as `MultiADescriptor` rather than Miniscript. Dropped that test and updated the PR description. Thanks for testing!
fametrano commented 2026-10-03 11:35:39 +00:00 (Migrated from github.com)

ACK a1c65a776c

descriptor_tests and miniscript_tests pass, and both new tr() checks fail without the descriptor.cpp change.

The optional WIF case in https://github.com/bitcoin/bitcoin/pull/36420#discussion_r4167758806 still adds coverage: without the fix its private form is accepted, and this PR's cases all use public keys.

ACK a1c65a776c252ce253972d1712936f315cbc5abc descriptor_tests and miniscript_tests pass, and both new tr() checks fail without the descriptor.cpp change. The optional WIF case in https://github.com/bitcoin/bitcoin/pull/36420#discussion_r4167758806 still adds coverage: without the fix its private form is accepted, and this PR's cases all use public keys.
Yudis-bit commented 2026-10-03 12:32:18 +00:00 (Migrated from github.com)

Re-pushed a1c65a7 -> 0956ace45d:

Range-diff
1:  a1c65a776c ! 1:  0956ace45d descriptor: compare x-only public keys in Tapscript Miniscript duplicate check
    @@ src/test/descriptor_tests.cpp: BOOST_AUTO_TEST_CASE(descriptor_test)
     +    CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))",
     +                    "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))",
     +                    "or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys");
    ++    CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(KztWktoNG1biXs3GSD6M7YDEynGaQPgjLguiWhMXiJM7sDA5vZFt)))",
    ++                    "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))",
    ++                    "or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys");
     +    // But in wsh(), keys differing only in parity are distinct 33-byte compressed pubkeys and not duplicate.
     +    {
     +        FlatSigningProvider keys;
Re-pushed `a1c65a7` -> `0956ace45d`: - Added the suggested WIF test case from https://github.com/bitcoin/bitcoin/pull/36420#discussion_r4167758806 to cover the private descriptor path in `KeyConverter`. <details><summary>Range-diff</summary> ```diff 1: a1c65a776c ! 1: 0956ace45d descriptor: compare x-only public keys in Tapscript Miniscript duplicate check @@ src/test/descriptor_tests.cpp: BOOST_AUTO_TEST_CASE(descriptor_test) + CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", + "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", + "or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys"); ++ CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(KztWktoNG1biXs3GSD6M7YDEynGaQPgjLguiWhMXiJM7sDA5vZFt)))", ++ "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", ++ "or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys"); + // But in wsh(), keys differing only in parity are distinct 33-byte compressed pubkeys and not duplicate. + { + FlatSigningProvider keys; ``` </details>
Yudis-bit (Migrated from github.com) reviewed 2026-10-03 12:32:37 +00:00
Yudis-bit (Migrated from github.com) commented 2026-10-03 12:32:37 +00:00

Applied in 0956ace45d, thanks.

Applied in 0956ace45d, thanks.
fametrano commented 2026-10-03 18:32:37 +00:00 (Migrated from github.com)

reACK 0956ace45d

Only the WIF test case was added since a1c65a77. descriptor_tests and miniscript_tests pass; without the descriptor.cpp change, the two tr() cases and the WIF case fail, the WIF one because its private form parses.

reACK 0956ace45dac99bc11256f4db1ca59f53016af6b Only the WIF test case was added since a1c65a77. descriptor_tests and miniscript_tests pass; without the descriptor.cpp change, the two tr() cases and the WIF case fail, the WIF one because its private form parses.
Yudis-bit commented 2026-11-01 00:57:22 +00:00 (Migrated from github.com)

Rebased on master (6c01bd0b2). Range-diff is clean:

1:  0956ace45 = 1:  28121591b descriptor: compare x-only public keys in Tapscript Miniscript duplicate check
Rebased on master (6c01bd0b2). Range-diff is clean: ``` 1: 0956ace45 = 1: 28121591b descriptor: compare x-only public keys in Tapscript Miniscript duplicate check ```
achow101 (Migrated from github.com) reviewed 2026-11-04 23:34:44 +00:00
achow101 (Migrated from github.com) commented 2026-11-04 23:34:44 +00:00

In 28121591bb "descriptor: compare x-only public keys in Tapscript Miniscript duplicate check"

Use Check( for test cases that are supposed to succeed.

In 28121591bb60e62d46f566c4775776d5cc37d9ec "descriptor: compare x-only public keys in Tapscript Miniscript duplicate check" Use `Check(` for test cases that are supposed to succeed.
Yudis-bit (Migrated from github.com) reviewed 2026-11-05 00:17:45 +00:00
Yudis-bit (Migrated from github.com) commented 2026-11-05 00:17:45 +00:00

Swapped for Check( in 53439006c.

Swapped for `Check(` in `53439006c`.
Yudis-bit commented 2026-11-05 00:18:02 +00:00 (Migrated from github.com)

Rebased and switched the wsh test case to Check(. Range-diff against 28121591b:

1:  28121591b ! 1:  53439006c descriptor: compare x-only public keys in Tapscript Miniscript duplicate check
    @@ src/test/descriptor_tests.cpp: BOOST_AUTO_TEST_CASE(descriptor_test)
     +                    "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))",
     +                    "or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys");
     +    // But in wsh(), keys differing only in parity are distinct 33-byte compressed pubkeys and not duplicate.
    -+    {
    -+        FlatSigningProvider keys;
    -+        std::string error;
    -+        auto parsed = Parse("wsh(or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", keys, error);
    -+        BOOST_CHECK_MESSAGE(!parsed.empty(), error);
    -+    }
    ++    Check("wsh(or_i(pk(L28YBaopjVrcu8N9hGMAVC6aye5oGETqqx5daUGQxqCjRzw7MNko),pk(KztWktoNG1biXs3GSD6M7YDEynGaQPgjLguiWhMXiJM7sDA5vZFt)))",
    ++          "wsh(or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))",
    ++          "wsh(or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))",
    ++          SIGNABLE, {{"00206d30950aa674b28b0fbf4cc43f70244c6bdbac4d8cd4ace43dc6fdf6d303d20e"}}, OutputType::BECH32);
          // Valid with extended keys.
          Check("wsh(and_v(v:ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e),multi(1,xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc,xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L/0)))", "wsh(and_v(v:ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e),multi(1,xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)))", "wsh(and_v(v:ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e),multi(1,xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)))", DEFAULT, {{"0020acf425291b98a1d7e0d4690139442abc289175be32ef1f75945e339924246d73"}}, OutputType::BECH32, /*op_compat_desc_hash=*/uint256{"0634b326edc66f9e2660562564d7a8fcca55f91dc4555ce0a51883cc72e0fa41"}, {{},{0}});
          // Valid under sh(wsh()) and with a mix of xpubs and raw keys.
Rebased and switched the wsh test case to `Check(`. Range-diff against 28121591b: ```diff 1: 28121591b ! 1: 53439006c descriptor: compare x-only public keys in Tapscript Miniscript duplicate check @@ src/test/descriptor_tests.cpp: BOOST_AUTO_TEST_CASE(descriptor_test) + "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", + "or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys"); + // But in wsh(), keys differing only in parity are distinct 33-byte compressed pubkeys and not duplicate. -+ { -+ FlatSigningProvider keys; -+ std::string error; -+ auto parsed = Parse("wsh(or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", keys, error); -+ BOOST_CHECK_MESSAGE(!parsed.empty(), error); -+ } ++ Check("wsh(or_i(pk(L28YBaopjVrcu8N9hGMAVC6aye5oGETqqx5daUGQxqCjRzw7MNko),pk(KztWktoNG1biXs3GSD6M7YDEynGaQPgjLguiWhMXiJM7sDA5vZFt)))", ++ "wsh(or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", ++ "wsh(or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", ++ SIGNABLE, {{"00206d30950aa674b28b0fbf4cc43f70244c6bdbac4d8cd4ace43dc6fdf6d303d20e"}}, OutputType::BECH32); // Valid with extended keys. Check("wsh(and_v(v:ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e),multi(1,xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc,xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L/0)))", "wsh(and_v(v:ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e),multi(1,xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)))", "wsh(and_v(v:ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e),multi(1,xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)))", DEFAULT, {{"0020acf425291b98a1d7e0d4690139442abc289175be32ef1f75945e339924246d73"}}, OutputType::BECH32, /*op_compat_desc_hash=*/uint256{"0634b326edc66f9e2660562564d7a8fcca55f91dc4555ce0a51883cc72e0fa41"}, {{},{0}}); // Valid under sh(wsh()) and with a mix of xpubs and raw keys. ```
Zeegaths commented 2026-11-12 10:33:07 +00:00 (Migrated from github.com)

tACK 5343900
On master:

image

On PR:

image
tACK 5343900 On master: <img width="819" height="383" alt="image" src="https://git.fish.foo/attachments/d953b0a9-6ee8-4d9a-918d-79383a718a74" /> On PR: <img width="813" height="476" alt="image" src="https://git.fish.foo/attachments/515107ea-c3a8-4bbb-8ec5-a6f9b3b59ed0" />
fametrano (Migrated from github.com) reviewed 2026-11-13 00:17:52 +00:00
fametrano (Migrated from github.com) left a comment

Concept ACK 53439006c6

This blocks for me: an existing wallet that holds one of these descriptors no longer loads. On regtest, the base build (6c01bd0b) imported tr(K,or_i(pk(02X),pk(03X))), with X one x-only key, into a watch-only wallet. With this head, loadwallet on that wallet fails with error -4, and the base build loads it again afterwards. The script and its output are in https://gist.github.com/fametrano/a763979702d860ff2824272ca87d465b/36113358b41d8c240ac95bf4846dd67eee313474.

Concept ACK 53439006c63179ca639f97bbb0291ff7944654c5 This blocks for me: an existing wallet that holds one of these descriptors no longer loads. On regtest, the base build (6c01bd0b) imported `tr(K,or_i(pk(02X),pk(03X)))`, with X one x-only key, into a watch-only wallet. With this head, `loadwallet` on that wallet fails with error -4, and the base build loads it again afterwards. The script and its output are in https://gist.github.com/fametrano/a763979702d860ff2824272ca87d465b/36113358b41d8c240ac95bf4846dd67eee313474.
Yudis-bit commented 2026-11-13 03:59:59 +00:00 (Migrated from github.com)

Should WalletDescriptor::FromStream decouple stream deserialization from IsSane()?

In walletutil.h, WalletDescriptor::FromStream calls Parse(..., /*require_checksum=*/true). Because ParseScript unconditionally rejects !node->IsSane(), tightening duplicate-key detection for Tapscript x-only keys causes existing wallet records containing 02X/03X twins to throw on load, even though node->IsValid() holds and the script remains spendable.

Allowing Parse to accept bool require_sane = true and passing false in FromStream would preserve wallet loading while keeping IsSane() enforced across importdescriptors, getdescriptorinfo, and deriveaddresses.

Should `WalletDescriptor::FromStream` decouple stream deserialization from `IsSane()`? In `walletutil.h`, `WalletDescriptor::FromStream` calls `Parse(..., /*require_checksum=*/true)`. Because `ParseScript` unconditionally rejects `!node->IsSane()`, tightening duplicate-key detection for Tapscript x-only keys causes existing wallet records containing `02X`/`03X` twins to throw on load, even though `node->IsValid()` holds and the script remains spendable. Allowing `Parse` to accept `bool require_sane = true` and passing `false` in `FromStream` would preserve wallet loading while keeping `IsSane()` enforced across `importdescriptors`, `getdescriptorinfo`, and `deriveaddresses`.
Yudis-bit commented 2026-11-25 15:40:42 +00:00 (Migrated from github.com)

Reproduced the loadwallet -4 failure under 53439006c6 and pushed 4e549915cc on top to add a require_sane = true parameter to Parse and ParseScript. WalletDescriptor::FromStream and ExportWatchOnlyWallet pass require_sane = false while keeping IsValidTopLevel() enforced, and imports and descriptor RPCs keep rejecting duplicate x-only Tapscript keys.

Tested with descriptor_tests, miniscript_tests, walletload_tests, wallet_tests, wallet_descriptor.py, wallet_exported_watchonly.py, wallet_taproot.py, and the regtest loadwallet and exportwatchonlywallet repro.

Reproduced the `loadwallet` `-4` failure under `53439006c6` and pushed `4e549915cc` on top to add a `require_sane = true` parameter to `Parse` and `ParseScript`. `WalletDescriptor::FromStream` and `ExportWatchOnlyWallet` pass `require_sane = false` while keeping `IsValidTopLevel()` enforced, and imports and descriptor RPCs keep rejecting duplicate x-only Tapscript keys. Tested with `descriptor_tests`, `miniscript_tests`, `walletload_tests`, `wallet_tests`, `wallet_descriptor.py`, `wallet_exported_watchonly.py`, `wallet_taproot.py`, and the regtest `loadwallet` and `exportwatchonlywallet` repro.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36420
No description provided.