Align miniscript type and ops computations with bitcoin-core
What changed, and why it matters
This commit fixes the Ledger Bitcoin app's internal checker for Miniscript wallet policies. The checker was incorrectly rejecting some valid Bitcoin spending policies as unsafe, and in a few places it was computing safety properties using formulas that differ from the reference implementation in Bitcoin Core. The changes make the Ledger app match Bitcoin Core's stricter-but-correct rules. There is no direct evidence in the commit that user funds were at risk of theft; the main effect was that legitimate wallet setups could be wrongly rejected.
Review the updated formulas against the cited Bitcoin Core source lines to confirm they match. Run the expanded unit tests and, if possible, fuzz-test Miniscript policy parsing to ensure no new invalid policies are accepted. Consider whether any user-facing error messages need updating now that previously rejected valid policies are accepted.
Security signals we found
Logic change in Miniscript policy analysis
Alignment with upstream Bitcoin Core reference implementation
Relaxation of overly strict non-malleability and timelock-mixing checks
Correction of ops-count accounting for and_n/andor
Addition of regression tests for previously rejected valid policies
Evidence from the diff
The patch updates compute_miniscript_policy_ext_info() in src/common/wallet.c to align type/ops calculations with Bitcoin Core. Key changes: (1) and_n(X,Y) is treated as andor(X,Y,0), so non-malleability no longer requires s_X or s_Y and Y’s dissatisfaction ops are not counted; (2) thresh timelock-mixing detection now compares each child against accumulated timelocks before adding the child’s own timelocks, preventing a single child with different timelock types in separate branches from being flagged as mixing; (3) the ‘e’ property for or_b, thresh, and d is aligned with Bitcoin Core (no final-result change because it only differed for non-‘m’ nodes). Unit tests are updated/added to cover these cases.
Changed components
src/common/wallet.c: compute_miniscript_policy_ext_info()unit-tests/test_wallet.c: test_miniscript_types()Inspect captured patch +26 / −13
### src/common/wallet.c
@@ -2513,7 +2513,8 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node,
out->s = x.s | y.s;
out->e = x.s | y.f;
- out->m = x.m & y.m & x.e & (x.s | y.s);
+ // andor(X,Y,0) requires s_X + s_Y + s_0, but s_0 = 1
+ out->m = x.m & y.m & x.e;
out->g = x.g | y.g;
out->h = x.h | y.h;
@@ -2527,7 +2528,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node,
out->script_size = 4 + x.script_size + y.script_size;
out->ops = (miniscript_ops_t) {3 + x.ops.count + y.ops.count,
- maxcheck(sumcheck(y.ops.sat, x.ops.sat), y.ops.dsat),
+ sumcheck(x.ops.sat, y.ops.sat),
x.ops.dsat};
out->ss = (miniscript_stacksize_t) {sumcheck(x.ss.sat, y.ss.sat), x.ss.dsat};
return 0;
@@ -2542,7 +2543,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node,
if (0 > compute_miniscript_policy_ext_info(node->scripts[1], &z, ctx)) return -1;
out->s = x.s & z.s;
- out->e = 1;
+ out->e = x.e & z.e;
out->m = x.m & z.m & x.e & z.e & (x.s | z.s);
@@ -2685,26 +2686,28 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node,
}
cur = cur->next;
- out->g |= t.g;
- out->h |= t.h;
- out->i |= t.i;
- out->j |= t.j;
-
out->k &= t.k; // if any child doesn't have k, thresh doesn't have k
- // if any two children have mixed timelocks, thresh doesn't have k
+ // if any two children have mixed timelocks, thresh doesn't have k.
+ // This must be checked before accumulating the timelocks of the current child,
+ // as a child is not mixing timelocks with itself.
if (node->k >= 2 &&
((t.g & out->h) || (t.h & out->g) || (t.i & out->j) || (t.j & out->i))) {
out->k = 0;
}
+ out->g |= t.g;
+ out->h |= t.h;
+ out->i |= t.i;
+ out->j |= t.j;
+
children_scriptsize += t.script_size;
}
int count_not_s = node->n - count_s;
out->s = count_not_s <= node->k - 1 ? 1 : 0;
- out->e = count_s == node->n ? 1 : 0;
+ out->e = (count_e == node->n && count_s == node->n) ? 1 : 0;
out->m = (count_e == node->n && count_m == node->n && count_not_s <= node->k) ? 1 : 0;
@@ -2806,7 +2809,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node,
if (0 > compute_miniscript_policy_ext_info(node->script, &x, ctx)) return -1;
out->s = x.s;
- out->e = 1;
+ out->e = x.f;
out->m = x.m;
### unit-tests/test_wallet.c
@@ -791,8 +791,9 @@ static void test_miniscript_types(void **state) {
Test("thresh(2,dv:older(42),s:pk(@0/**),s:pk(@1/**))", "7663012ab269687c205cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bcac937c20d30199d74fb5a22d47b6e054e2f378cedacffcb89904a61d75d0dbd407143e65ac935287", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_P2WSH_INVALID, 12, 4);
// Regression test: thresh 'm' (non-malleable) must require all children to have 'm'.
- // or_b(sha256,a:sha256) has e=1 but m=0 (children lack 's'), so thresh must NOT be NONMAL.
- Test("thresh(1,or_b(sha256(e38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f),a:sha256(d1ec675902ef1633427ca360b290b0b3045a0d9058ddb5e648b4c3c3224c5c68)),a:0)", "?", TESTMODE_VALID, -1, -1);
+ // ltv:or_b(sha256,a:sha256) has e=1 (l: has e = f of t:) but m=0 (the children of or_b lack
+ // 's'), so thresh must NOT be NONMAL.
+ Test("thresh(1,ltv:or_b(sha256(e38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f),a:sha256(d1ec675902ef1633427ca360b290b0b3045a0d9058ddb5e648b4c3c3224c5c68)),a:0)", "?", TESTMODE_VALID, -1, -1);
// Regression tests: 'l:' and 'u:' can always be dissatisfied via the constant 0 branch, even if
// the child can't. The or_d must account for the ops of the multi executed after dissatisfying
@@ -810,6 +811,15 @@ static void test_miniscript_types(void **state) {
// 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);
+ // Regression tests: and_n(X,Y) is andor(X,Y,0). Since 0 has the 's' property, non-malleability
+ // does not require a signature in X or Y; and the dissatisfaction of Y is never executed.
+ Test("and_v(v:pk(@0/**),and_n(un:older(144),sha256(e38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f)))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG, 13, 4);
+ Test("and_n(pk(@0/**),or_i(pk(@1/**),and_b(multi(1,@2/**,@3/**,@4/**,@5/**,@6/**),a:0)))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 12, 4);
+
+ // Regression test: a thresh child containing different kinds of timelocks in separate branches
+ // is not mixing timelocks with itself.
+ Test("thresh(2,pk(@0/**),s:pk(@1/**),anl:or_i(and_v(v:pk(@2/**),older(4194305)),and_v(v:pk(@3/**),older(144))))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG, 19, 6);
+
// clang-format on
}
Why this scored 48/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.