Skip to content

Require both d and u properties for andor conditions - #167

Closed
Harshil-1603 wants to merge 5 commits into
diybitcoinhardware:masterfrom
Harshil-1603:fix/miniscript-andor-typecheck
Closed

Harshil-1603 wants to merge 5 commits into
diybitcoinhardware:masterfrom
Harshil-1603:fix/miniscript-andor-typecheck

Conversation

@Harshil-1603

Copy link
Copy Markdown

andor and and_n require the left child to have both the dissatisfiable (d) and unit (u) properties. Their checks currently reject only children missing both properties, allowing invalid expressions such as andor(1,1,0) through verification and descriptor parsing.

Reject a left child missing either property. Regression cases cover each missing property and both missing together, plus valid constant and public-key controls, through bare Miniscript verification and descriptor parsing.

Fixes #143.

Validation: six regression subtests fail before the fix. The full suite passes afterward (124 passed, 17 optional-native-binding skips, 12 subtests passed). Wheel/sdist builds, repository artifact verification, and git diff --check pass. Other Python versions and MicroPython were not run locally.

Implemented and tested with Codex assistance.

@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.

tACK 6ec7106

I ran 20 descriptors through getdescriptorinfo on Core v31.1.0 and through Descriptor.from_string on master and on this branch. Happy to share the script. Under wsh(), this branch now rejects andor and and_n whose left child lacks d (1, or_i(1,1)), lacks u (or_i(0,older(10)), dv:older(10)), or lacks both (older(10)), as Core does. Master accepts the first four. The test treats wsh(andor(0,1,0)) and wsh(and_n(0,1)) as valid descriptors. Core rejects both as not satisfiable. Keeping the 0 controls for the bare verify() only would avoid asserting that. The pk() controls are fine.

One gap in this rule remains. tr() leaves are never passed to verify(), so this rule skips tapscript. tr(A,and_n(or_i(0,older(10)),pk(B))) is still accepted here, and Core rejects it. #169 adds that call, and with both changes embit rejects it too.

Zero-left controls satisfy the d/u type requirements but have no
satisfaction. Preserve their bare verify() checks and all invalid-type
rejections, while limiting descriptor positives to public-key controls.

Assisted-by: OpenAI Codex (GPT-6)
@bitcoisas

Copy link
Copy Markdown

tACK e223867

Found this independently through bitcoinfuzz (descriptor_parse, Core vs embit) before seeing this PR. On master, wsh(andor(dv:older(1),pkh(A),pkh(B))) and the and_n equivalent are accepted, while Core rejects them. A valid andor with a pkh condition passes on both, so the mismatch comes from the missing u. With this branch, embit rejects both with “X should be ‘du’”, and test_descriptor.py passes locally.

One suggestion: the new test only covers bare fragments inside wsh. It might be worth adding a wrapped case like dv:older(1), and a taproot one, since d: has u in tapscript, so tr(I,andor(dv:older(1),pk(A),pk(B))) should stay valid. It does on this branch, but nothing guards it.

@bitcoisas

Copy link
Copy Markdown

Small correction to my taproot note. As @fametrano pointed out, tr() leaves don’t go through verify() on this branch, so the tr case being accepted here doesn’t show much on its own. I checked it on top of #169 as well. With both, tr(I,and_n(or_i(0,older(10)),pk(B))) is rejected and tr(I,andor(dv:older(1),pk(A),pk(B))) is still accepted, as expected since d: is u in tapscript. So that test probably belongs in #169.

@jdlcdl

jdlcdl commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

tACK at e223867 atop master or otherwise to be rebased atop branch "hardened".

From sipa's miniscript reference,

miniscript to bitcoin script translations:

andor(X,Y,Z) becomes [X] NOTIF [Z] ELSE [Y] ENDIF,

and

and_n(X,Y) is syntactic sugar for the same form [X] NOTIF 0 ELSE [Y] ENDIF

required correctness properties (explicit in reference for andor, implied for and_n:

andor(X,Y,Z) X is Bdu; Y and Z are both B, K, or V,
and_n(X,Y) X is Bdu: Y is B, K, or V

Skipping the valid types of expressions (base verify or key) for X,Y,Z and focusing only on the required properties of the left-side X -- it must have both properties:

  • "d" can be dissatisfied,
  • "u" unit (pushes 1 when satisfied)

my understanding:

Since both "d" and "u" are required, I truly believe that the original implementer meant something more like:

if not ("d" in px and "u" in px):
    raise MiniscriptError("andor: X should be 'du'")  # same for and_n

...which this PR implements, and tests illustrate.

jdlcdl
jdlcdl previously approved these changes Nov 10, 2026
odudex
odudex previously approved these changes Nov 10, 2026

@odudex odudex left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ACK
The fix is correct.

Could be improved with nits:

  1. Unneeded skip in the test. The second commit skips the descriptor check for the 0 controls, with a comment saying they are "not descriptors". In fact wsh(andor(0,1,0)) and wsh(and_n(0,1)) parse fine and the compile-equality assertion passes for them. The if left == "0": continue and its comment can be dropped.
  2. No CHANGELOG entry. hardened keeps a ### Fixed list under Unreleased that already has a similar miniscript line (the t: wrapper fix). This is a public PR that makes parsing stricter, so a one-line entry seems appropriate.

Can be cleanly rebased onto and merged to hardened (branch moving quicker with less consensus requirements)

@Harshil-1603
Harshil-1603 dismissed stale reviews from odudex and jdlcdl via ccff026 November 11, 2026 02:58

@bitcoisas bitcoisas 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.

tACK ccff026

Re-tested at the new commit, thanks for adding the dv:older(1) case. The full suite passes and my earlier cases behave the same.

One IMO non-block question to @Harshil-1603, this commit drops the left == "0" skip from e223867, so the test once again asserts that wsh(andor(0,1,0)) and wsh(and_n(0,1)) are valid descriptors. Running both through bitcoinfuzz, Core rejects them and embit accepts them, which is the point @fametrano raised earlier. Was the change intended? It’s accurate for embit today, but it would have to be undone once sanity checks land (#144).

I also tried the three commits on top of hardened. They apply cleanly except for CHANGELOG.md, where both sides add entries under Unreleased. With that resolved, the full suite passes there too (393 passed, 21 skipped).

@jdlcdl

jdlcdl commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

this commit drops the left == "0" skip

I believe it is in response to @odudex's comment just above, arguing that wsh(andor(0,1,0)) and wsh(and_n(0,1)) should parse fine/valid.

@odudex

odudex commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

One IMO non-block question to @Harshil-1603, this commit drops the left == "0" skip from e223867, so the test once again asserts that wsh(andor(0,1,0)) and wsh(and_n(0,1)) are valid descriptors. Running both through bitcoinfuzz, Core rejects them and embit accepts them, which is the point @fametrano raised earlier. Was the change intended? It’s accurate for embit today, but it would have to be undone once sanity checks land (#144).

Good catch, and that one is on me: the skip was added in e223867 to address @fametrano's point, and my comment asked to remove it because both strings parse in embit today. That's true but not a reason to assert it. They are type-valid controls for the d/u rule, not descriptors we want to pin as accepted, and the assertion would have to be undone with #144.

@Harshil-1603 sorry for the back and forth. Could you restore the left == "0" skip, with a comment along the lines of "type-valid but unsatisfiable: Core rejects these as descriptors; embit has no sanity checks yet (#144), so only verify() is asserted"? The pk() control keeps covering the descriptor path.

Apply the concrete reviewer request without changing runtime behavior.

Assisted-by: OpenAI Codex
@bitcoisas

Copy link
Copy Markdown

Thanks for restoring the skip, the #144 note makes the intent clear. Re-tested at 91c5340 and suite passes.

@jdlcdl

jdlcdl commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

ACK at 91c530, intending to merge these 4 commits to "hardened" after pr162
ACK at e4d3a99, intending to merge these 5 commits to "hardened" after pr161
I did resolve a changelog conflict.

Thank you harshil, fametrano, bitcoisas, tanveer, odudex and miketlk.

jdlcdl
jdlcdl previously approved these changes Nov 13, 2026
@odudex

odudex commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

ACK at 91c5340

Comment thread tests/tests/test_descriptor.py Outdated
"older(10)",
):
source = template % left
with self.subTest(source=source):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

self.subTest isn't in the tests/unittest.py shim, so this breaks run_tests.py on micropython.

@miketlk miketlk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The new descriptor test aborts Micropython test suite.

test_miniscript_compat (DescriptorTest) ... ok
test_invalid_miniscript (DescriptorTest) ... ok
test_andor_requires_dissatisfiable_unit_left_child (DescriptorTest) ... FAIL
Traceback (most recent call last):
  File "./run_tests.py", line 9, in <module>
  File "/Users/mike/Projects/embit/embit/tests/unittest.py", line 233, in main
  File "/Users/mike/Projects/embit/embit/tests/unittest.py", line 165, in run
  File "/Users/mike/Projects/embit/embit/tests/unittest.py", line 202, in run_class
  File "/Users/mike/Projects/embit/embit/tests/tests/test_descriptor.py", line 338, in test_andor_requires_dissatisfiable_unit_left_child
AttributeError: 'DescriptorTest' object has no attribute 'subTest'

Comment thread tests/tests/test_descriptor.py Outdated
"older(10)",
):
source = template % left
with self.subTest(source=source):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This line can be removed, and the body underneath un-tabbed.

Comment thread tests/tests/test_descriptor.py Outdated
Descriptor.from_string("wsh(%s)" % source)
for left in ("0", "pk(%s)" % key):
source = template % left
with self.subTest(source=source):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This line can be removed, as well as its body underneath can be untabbed.

Use plain loops instead of unsupported subTest contexts, preserving all
assertions. Use hexlify for older MicroPython builds where applicable.

Assisted-by: OpenAI Codex

@notTanveer notTanveer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ACK e4d3a99

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.

Bare Miniscript andor / and_n with a satisfiable left child passes verify() and compile()

7 participants