Skip to content

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

Open
Yudis-bit wants to merge 2 commits into
bitcoin:masterfrom
Yudis-bit:tr-miniscript-xonly-dupkeys
Open

Yudis-bit wants to merge 2 commits into
bitcoin:masterfrom
Yudis-bit:tr-miniscript-xonly-dupkeys

Conversation

@Yudis-bit

@Yudis-bit Yudis-bit commented Oct 2, 2026 •

Copy link
Copy Markdown

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.

Copilot AI balanced review requested due to automatic review settings October 2, 2026 15:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@DrahtBot

DrahtBot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

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

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested at cb85c2c. 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.

Comment thread src/test/descriptor_tests.cpp Outdated
Comment on lines +1135 to +1137
CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,multi_a(2,6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4,036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4))",
"tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,multi_a(2,6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4,036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4))",
"multi_a(2,6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4,036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4) is not sane: contains duplicate public keys");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This descriptor parses, so this check fails:

Suggested change
CheckUnparsable("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,multi_a(2,6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4,036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4))",
"tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,multi_a(2,6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4,036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4))",
"multi_a(2,6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4,036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4) is not sane: contains duplicate public keys");

"or_i(pk(6116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys");
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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
"or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)) is not sane: contains duplicate public keys");
"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");

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 0956ace, thanks.

@Yudis-bit
Yudis-bit force-pushed the tr-miniscript-xonly-dupkeys branch from cb85c2c to a1c65a7 Compare October 3, 2026 06:25
@Yudis-bit

Yudis-bit commented Oct 3, 2026 •

Copy link
Copy Markdown
Author

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

@fametrano

Copy link
Copy Markdown

ACK a1c65a7

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

The optional WIF case in #36420 (comment) still adds coverage: without the fix its private form is accepted, and this PR's cases all use public keys.

@Yudis-bit
Yudis-bit force-pushed the tr-miniscript-xonly-dupkeys branch from a1c65a7 to 0956ace Compare October 3, 2026 12:32
@Yudis-bit

Copy link
Copy Markdown
Author

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;

@fametrano

Copy link
Copy Markdown

reACK 0956ace

Only the WIF test case was added since a1c65a7. 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
Yudis-bit force-pushed the tr-miniscript-xonly-dupkeys branch from 0956ace to 2812159 Compare November 1, 2026 00:54
@Yudis-bit

Copy link
Copy Markdown
Author

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

1:  0956ace45 = 1:  28121591b descriptor: compare x-only public keys in Tapscript Miniscript duplicate check

Comment thread src/test/descriptor_tests.cpp Outdated
{
FlatSigningProvider keys;
std::string error;
auto parsed = Parse("wsh(or_i(pk(026116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4),pk(036116733562ba4df653e4ec6b98c904c8dd3c677428264d33f4ae0089b72abaa4)))", keys, error);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapped for Check( in 53439006c.

…ate check

In Tapscript Miniscript descriptors (inside tr()), public keys serialize
as 32-byte x-only pubkeys (BIP-340 / BIP-342 / BIP-379), discarding parity.
However, KeyParser::KeyCompare compared keys as full 33-byte CPubKeys,
causing keys that differ only in parity (such as pk(X) and pk(03X), or
pk(02X) and pk(03X)) to be treated as distinct.

As a result, DuplicateKeyCheck() passed and marked the script sane, even
though both branches serialize to the same script on-chain (<X> OP_CHECKSIG).
This violates BIP-379 malleability requirements and enables third parties
to malleate transactions by flipping branch selectors with a single signature.

Fix this by comparing keys as XOnlyPubKey when miniscript::IsTapscript(m_script_ctx)
in KeyParser::KeyCompare and KeyConverter::KeyCompare. In Tapscript, keys with
matching x-coordinates evaluate as equivalent, correctly failing duplicate-key
checks, while P2WSH (wsh()) continues to distinguish 33-byte compressed keys.

Fixes bitcoin#36414.
@Yudis-bit
Yudis-bit force-pushed the tr-miniscript-xonly-dupkeys branch from 2812159 to 5343900 Compare November 5, 2026 00:16
@Yudis-bit

Copy link
Copy Markdown
Author

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

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

Copy link
Copy Markdown

tACK 5343900
On master:

image

On PR:

image

@DrahtBot
DrahtBot requested a review from fametrano November 12, 2026 10:33

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 5343900

This blocks for me: an existing wallet that holds one of these descriptors no longer loads. On regtest, the base build (6c01bd0) 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

Copy link
Copy Markdown
Author

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.

…iptors

Existing wallets created before enforcing Tapscript x-only duplicate-key checks may store descriptors such as tr(K,or_i(pk(02X),pk(03X))) that now fail IsSane(), causing loadwallet to fail with error -4.

Add a require_sane = true parameter to Parse() and ParseScript() so callers deserializing or exporting already-persisted wallet descriptors (WalletDescriptor::FromStream and ExportWatchOnlyWallet) can opt out of Miniscript sanity checks while keeping IsValidTopLevel() enforced and leaving require_sane = true for imports and descriptor RPCs.
@Yudis-bit

Copy link
Copy Markdown
Author

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

descriptor: tr() duplicate-key check misses keys that differ only in parity

7 participants