Skip to content

descriptors: check duplicate keys in all multipath Miniscript branches - #35742

Closed
yashbhutwala wants to merge 1 commit into
bitcoin:masterfrom
yashbhutwala:fix/multipath-miniscript-sanity
Closed

yashbhutwala wants to merge 1 commit into
bitcoin:masterfrom
yashbhutwala:fix/multipath-miniscript-sanity

Conversation

@yashbhutwala

Copy link
Copy Markdown
Contributor

Fixes #35629.

Multipath Miniscript descriptors performed duplicate-key sanity checking while parsing the template, when key comparison used the first multipath branch. As a result, duplicate keys present only in a later branch were accepted.

Select each expanded multipath branch and rerun the existing duplicate-key check before constructing the final descriptors. If a branch contains a duplicate, report the offending concrete Miniscript expression.

The regression test covers:

  • a valid two-branch wsh control;
  • a duplicate in the second wsh branch;
  • a duplicate in the third of three tr branches.

Tested with:

build/bin/test_bitcoin --run_test=descriptor_tests/multipath_miniscript_duplicate_keys
build/bin/test_bitcoin --run_test=descriptor_tests
build/bin/test_bitcoin

@DrahtBot

DrahtBot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35742.

Reviews

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

Type Reviewers
Concept ACK l0rinc, vicjuma

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:

  • #36122 (BIP460: CISA for Taproot key path spends by fjahr)
  • #35445 (wallet, descriptor: Revert StringType::COMPAT for Miniscript expressions and drop the concept of a Descriptor ID that can be validated by achow101)

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.

@Zeegaths

Zeegaths commented Aug 4, 2026 •

Copy link
Copy Markdown

I ran this on master and on pr-35742,

bitcoin-cli getdescriptorinfo "wsh(or_i(pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/<0;1>),pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/<2;1>)))"

and this is what I got:

On master
Same public key is generated

{
  "descriptor": "wsh(or_i(pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0),pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/2)))#kzwg47ay",
  "multipath_expansion": [
    "wsh(or_i(pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0),pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/2)))#kzwg47ay",
    "wsh(or_i(pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/1),pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/1)))#jkt2efkp"
  ],
  "checksum": "mn7hqkkh",
  "isrange": false,
  "issolvable": true,
  "hasprivatekeys": false
}

On branch

error code: -5
error message:
or_i(pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/1),pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/1)) is not sane: contains duplicate public keys

I also checked using importdescriptors to observe wallet action.

On master, the unsafe branch is added to the wallet:

bitcoin-cli -rpcwallet=testwallet_master importdescriptors '[{"desc": "wsh(or_i(pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/<0;1>),pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/<2;1>)))#mn7hqkkh", "timestamp": "now", "active": false}]'
[
  {
    "success": true
  }
]

On the PR:

[
  {
    "success": false,
    "error": {
      "code": -5,
      "message": "or_i(pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/1),pk(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/1)) is not sane: contains duplicate public keys"
    }
  }
]

This shows how the PR fixes the silent passing on master

@l0rinc

l0rinc commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Concept ACK, I also found the same issue - please rebase.

@yashbhutwala
yashbhutwala force-pushed the fix/multipath-miniscript-sanity branch from 560cfa2 to 5ced9e7 Compare September 5, 2026 19:07

@vicjuma vicjuma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK

Comment thread src/script/descriptor.cpp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#22838 :-}. This change aligns with BIP389. Hardcoding the 0th index assumes that there is always a single path/branch that contradicts the BIP imo. Reproduced the error.

Before PR

Image

After PR

Image

@l0rinc l0rinc left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suggest splitting this into focused commits so reviewers can understand the old behavior and the problem before assessing the fix: test infrastructure and controls, characterization of the old behavior with TODOs explaining why those expectations are wrong, then the fix and updated expectations. See l0rinc#295 for how I split the same change.

We should also take this opportunity to add coverage for mixed single-path/multipath keys, wildcards, hardened paths with and without private keys, differing origins, nested errors, and valid key sharing across branches and Taproot leaves.

A functional test would demonstrate the wallet impact: these imports currently succeed but prevent reload. With the fix, rejected imports leave the descriptor set unchanged and the wallet reloadable.

Edit: please make the PR description less robotic, show us how you tested this manually for example

Comment thread src/script/descriptor.cpp
Comment on lines +2739 to +2750
// Duplicate keys were checked above for branch 0. Check every remaining branch now
// that all multipath key vectors have been expanded to the same length.
for (size_t i = 1; i < num_multipath; ++i) {
parser.m_multipath_index = i;
node->DuplicateKeyCheck(parser);
if (!node->CheckDuplicateKey()) {
const auto* insane_node = &node.value();
if (const auto sub = node->FindInsaneSub()) insane_node = sub;
error = *insane_node->ToString(parser) + " is not sane: contains duplicate public keys";
return {};
}
}

@l0rinc l0rinc Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Given that sane already checks for duplicates in

bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }
, as far as I can tell this can be simplified:

Suggested change
// Duplicate keys were checked above for branch 0. Check every remaining branch now
// that all multipath key vectors have been expanded to the same length.
for (size_t i = 1; i < num_multipath; ++i) {
parser.m_multipath_index = i;
node->DuplicateKeyCheck(parser);
if (!node->CheckDuplicateKey()) {
const auto* insane_node = &node.value();
if (const auto sub = node->FindInsaneSub()) insane_node = sub;
error = *insane_node->ToString(parser) + " is not sane: contains duplicate public keys";
return {};
}
}
// Recheck duplicate keys after multipath expansion, since parsing checked only branch 0
for (size_t i{1}; i < num_multipath; ++i) {
parser.m_multipath_index = i;
node->DuplicateKeyCheck(parser);
if (!node->CheckDuplicateKey()) {
error = *node->FindInsaneSub()->ToString(parser) + " is not sane: contains duplicate public keys";
return {};
}
}

@DrahtBot

DrahtBot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@fametrano

Copy link
Copy Markdown

@yashbhutwala, I rebased l0rinc's split of this PR (l0rinc#295) onto current master: https://github.com/fametrano/bitcoin/tree/multipath-dupkeys-l0rinc

The only conflict was in KeyParser::ToString, where #35445 added StringType::PUBLIC; I kept it. Authors and co-authors are as in #295. Each commit builds and the descriptor, Miniscript and wallet tests pass.

You can fetch it and force-push it over this branch, or cherry-pick the three commits onto your own rebase. Your call.

@maflcko

maflcko commented Oct 22, 2026

Copy link
Copy Markdown
Member

Closing for now due to inactivity. A new pull can be opened, if this is still relevant.

@maflcko maflcko closed this Oct 22, 2026
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.

Duplicate-key sanity for multipath Miniscript descriptors only checks branch 0, so nonzero-branch key collisions are accepted

7 participants