Fix ops computation for andor fragments
What changed, and why it matters
This commit fixes a bug in how the Ledger Bitcoin app counts the number of Bitcoin script operations (ops) needed to spend certain wallet policies that use a 'miniscript andor' fragment. Because the count was wrong, the app could allow users to register wallet policies that look valid but cannot actually be spent later because they exceed Bitcoin's 201-op limit. The fix changes one variable in a formula so the correct dissatisfaction branch is counted, matching Bitcoin Core's reference implementation. New tests confirm the fix.
Review and merge the fix, then ensure the updated app is distributed to users. Wallet policies already registered under the buggy check should be considered potentially unspendable; users should be advised to re-register or verify affected policies after updating.
Security signals we found
Incorrect miniscript ops accounting for andor fragment
Policy sanity check bypass leading to unspendable registered wallets
Reference alignment with Bitcoin Core implementation
Regression tests added for the specific miscount
Evidence from the diff
In src/common/wallet.c, compute_miniscript_policy_ext_info() computes the worst-case operation count for miniscript policy nodes. For andor(X,Y,Z), the satisfaction path via Z executes X’s dissatisfaction plus Z’s satisfaction. The original code incorrectly used y.ops.dsat in the maxcheck expression: maxcheck(sumcheck(y.ops.sat, x.ops.sat), sumcheck(y.ops.dsat, z.ops.sat)). It should use x.ops.dsat, because the Z-satisfaction path requires dissatisfying X, not Y. This caused the ops budget to be undercounted when Y has no dissatisfaction (e.g., a ‘V’ fragment), allowing policies whose Z branch exceeds 201 ops to be registered as sane. The patch changes y.ops.dsat to x.ops.dsat, aligning with Bitcoin Core’s miniscript.h lines 1046-1051. Regression tests are added in tests/test_register_wallet.py and unit-tests/test_wallet.c.
Changed components
src/common/wallet.ccompute_miniscript_policy_ext_info()andor miniscript fragment handlingwallet policy registration sanity checksInspect captured patch +40 / −1
### src/common/wallet.c
@@ -2427,7 +2427,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node,
out->ops = (miniscript_ops_t) {
3 + x.ops.count + y.ops.count + z.ops.count,
- maxcheck(sumcheck(y.ops.sat, x.ops.sat), sumcheck(y.ops.dsat, z.ops.sat)),
+ maxcheck(sumcheck(y.ops.sat, x.ops.sat), sumcheck(x.ops.dsat, z.ops.sat)),
sumcheck(x.ops.dsat, z.ops.dsat)};
out->ss = (miniscript_stacksize_t) {
maxcheck(sumcheck(x.ss.sat, y.ss.sat), sumcheck(x.ss.dsat, z.ss.sat)),
### tests/test_register_wallet.py
@@ -528,6 +528,37 @@ def test_register_wallet_not_sane_policy(navigator: Navigator, firmware: Firmwar
error_code = int.from_bytes(e.value.data, 'big')
assert error_code == EC_REGISTER_WALLET_POLICY_NOT_SANE
+ # Miniscript policy exceeding the 201 ops limit only when satisfying the multi in the Z branch
+ # of the andor. The ops of that branch are only accounted for if the dissatisfaction of X (rather
+ # than of Y, which can't be dissatisfied) is used for that satisfaction path.
+ with pytest.raises(ExceptionRAPDU) as e:
+ client.register_wallet(WalletPolicy(
+ name="Too many ops",
+ descriptor_template=f"wsh(t:andor(pk(@10/**),v:or_i(jjjjjjjjjjj:pk(@11/**),jjjjjjjjjjj:pk(@12/**)),v:or_i(jjjjjjjjjjj:multi(1,{keys}),jjjjjjjjjjj:pk(@13/**))))",
+ keys_info=[
+ "[f5acc2fd/48'/1'/0'/2']tpubDFAqEGNyad35aBCKUAXbQGDjdVhNueno5ZZVEn3sQbW5ci457gLR7HyTmHBg93oourBssgUxuWz1jX5uhc1qaqFo9VsybY1J5FuedLfm4dK",
+ "tpubDE7NQymr4AFtewpAsWtnreyq9ghkzQBXpCZjWLFVRAvnbf7vya2eMTvT2fPapNqL8SuVvLQdbUbMfWLVDCZKnsEBqp6UK93QEzL8Ck23AwF",
+ "tpubDDV6FDLcCieWUeN7R3vZK2Qs3KuQed3ScTY9EiwMXvyCkLjDbCb8RXaAgWDbkG4tW1BMKVF1zERHnyt78QKd4ZaAYGMJMpvHPwgSSU1AxZ3",
+ "tpubDF6JT5K4izwALMpFv7fQrpWr5bGUMEoWphkzTVJH8jTfgirNEgGZnxsWJDCCxhg2UnW5RcD9Tx8aVAdoM734X5bnRGmJUujz26uQ5gAC1nE",
+ "tpubDF4kujkh5dAhC1pFgBToZybXdvJFXXGX4BWdDxWqP7EUpG8gxkfMQe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jaiS8DSz1QcNZ8D1qxUMx1g",
+ ]),
+ navigator,
+ testname=test_name
+ )
+ assert DeviceException.exc.get(e.value.status) == NotSupportedError
+ assert len(e.value.data) == 2
+ error_code = int.from_bytes(e.value.data, 'big')
+ assert error_code == EC_REGISTER_WALLET_POLICY_NOT_SANE
+
def test_register_unusual_singlesig_accounts(navigator: Navigator, firmware: Firmware, client:
RaggerClient, test_name: str, speculos_globals):
### unit-tests/test_wallet.c
@@ -802,6 +802,14 @@ static void test_miniscript_types(void **state) {
// Only the right branch of the or_d exceeds the 201 ops limit
Test("or_d(utv:or_i(jjjjjjjjjj:pk(@10/**),jjjjjjjjjj:pk(@11/**)),or_i(jjjjjjjjjjjjj:multi(1,@0/**,@1/**,@2/**,@3/**,@4/**,@5/**,@6/**,@7/**,@8/**,@9/**),jjjjjjjjjjjjj:pk(@12/**)))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 211, -1);
+ // Regression tests: the satisfaction of andor(X,Y,Z) via Z executes the dissatisfaction of X,
+ // not of Y. With a 'V' Y (no dissatisfaction), the ops of the multi in Z must still be counted.
+ Test("t:andor(pk(@0/**),v:pk(@1/**),v:multi(1,@2/**,@3/**,@4/**))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 9, 4);
+ // Asymmetric costs: dissatisfying X is more expensive than dissatisfying Y
+ Test("andor(multi(1,@0/**,@1/**),pk(@2/**),multi(1,@3/**,@4/**,@5/**))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 11, 5);
+ // Only the Z branch of the andor exceeds the 201 ops limit
+ Test("t:andor(pk(@10/**),v:or_i(jjjjjjjjjjj:pk(@11/**),jjjjjjjjjjj:pk(@12/**)),v:or_i(jjjjjjjjjjj:multi(1,@0/**,@1/**,@2/**,@3/**,@4/**,@5/**,@6/**,@7/**,@8/**,@9/**),jjjjjjjjjjj:pk(@13/**)))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 202, -1);
+
// clang-format on
}
Why this scored 68/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.