Skip to content

descriptor: type-check tapscript leaves in tr() - #169

Open
fametrano wants to merge 5 commits into
diybitcoinhardware:masterfrom
fametrano:tr-leaf-verify
Open

fametrano wants to merge 5 commits into
diybitcoinhardware:masterfrom
fametrano:tr-leaf-verify

Conversation

@fametrano

@fametrano fametrano commented Oct 22, 2026 •

Copy link
Copy Markdown

embit accepts tr() leaves that are not valid miniscript, such as tr(A,and_v(pk(B),1)) and tr(A,v:pk(B)), which Bitcoin Core v31.1.0 rejects. Descriptor.__init__ type-checks a wsh miniscript, but nothing checks a tapscript leaf. TapLeaf now calls verify() and requires type B, as for wsh; the tapscript rules (multi_a instead of multi, u on d:) were already in embit. I compared the test's leaves on Core (getdescriptorinfo) and on embit (Descriptor.from_string): the new test fails on master and the suite passes (124 passed, 17 skipped).

Core also rejects tr(A,and_n(or_i(0,older(10)),pk(B))), which embit still accepts: its and_n check takes d or u instead of both. #167 fixes that.

TapLeaf never called verify(), so tr() accepted leaves that Bitcoin
Core rejects, such as and_v(pk(B),1), and so did a TapLeaf built in
code. Verify the miniscript when the leaf is built and require its type
to be B, as Descriptor.__init__ does for wsh.

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

Tested on its own and merged with #167. The full test suite passes in both cases (124 and 125 passed, the same 17 skipped). This mirrors what Descriptor.init already does for wsh, and since derive/branch/to_public rebuild the leaf, they go through the same check, as wsh does...

With both PRs, 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. On this branch alone the first one still gets through, since the and_n rule itself is fixed in #167. Glad to see the dv:older case already covered in test_leaf_miniscript_is_type_checked; that was the guard I was suggesting there. :)

@jdlcdl

jdlcdl commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

concept ACK

I understand this as a change from "trust me bro, it's a miniscript tapleaf" to "it's a validated miniscript tapleaf"

If you have references, please point me to BIPs that can confirm my understanding.

With better understanding and another approval, intending for this to go atop branch "hardened" after pr167 is merged there.

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.

Fix is correct.
Things that could be improved, none blocking:

  1. One test case doesn't exercise the change. multi(1,A,B) inside tr() was already rejected by Multi.init before this PR. It's harmless as a regression guard, but its comment implies the new check catches it.
  2. Exception type differs from wsh. For a non-B top level, wsh raises DescriptorError and this raises MiniscriptError with the same message. MiniscriptError subclasses DescriptorError, so callers catching the base class are unaffected.
  3. No CHANGELOG entry, same as the andor PR. A one-line ### Fixed entry fits unless this came in as a security report.

@fametrano

Copy link
Copy Markdown
Author

Yes, with one limit: a leaf is now type-checked and has to be of type B, as a wsh() script already is. Timelock mixing, which BIP 379 also makes illegal, is still not checked, for wsh() either.

BIP 386 allows as a leaf "Any Script Expression that is allowed at the level this Tree Expression is in", which includes the BIP 379 Miniscript expressions. BIP 379 says an expression that fails a fragment's requirements "is not valid Miniscript", and that type B is "required for the top level expression".

If you want odudex's three points addressed before you take it, I will push them.

@jdlcdl

jdlcdl commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

If you want odudex's three points addressed before you take it, I will push them.

Thank you fametrano for replying, just seeing it this morning.

Yes, please do push latest commits (we'll look to re-approve quickly), so that this pr is as complete as possible.

I intend to merge this into "hardened" but leave it as an open pr against "master", so I'd like for all commits to be together in both branches eventually.

Descriptor.__init__ raises DescriptorError when a wsh script is not of
type B. Raise the same error for a tapscript leaf.
Multi.__init__ already rejects multi in tr(), before the leaf is
type-checked, and test_multi_variants covers it. Keep only the cases
the leaf check catches.
@fametrano

Copy link
Copy Markdown
Author

Pushed the three points from odudex's review: a non-B leaf now raises DescriptorError as in wsh(), the multi case is dropped (Multi already rejects it, and test_multi_variants covers it), and there is a ### Fixed entry in the changelog.

@jdlcdl

jdlcdl commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

ACK at 6255583 508c05b, intending to merge these 4 5 commits to "hardened" after pr167 pr182.
I did resolve a CHANGELOG conflict.

Thank you fametrano, bitcoisas and odudex.

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

odudex commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Ack at 6255583

Comment thread tests/tests/test_taptree.py Outdated
]
for leaf in invalid:
for tree in (leaf, "{%s,pk(%s)}" % (leaf, b), "{pk(%s),%s}" % (b, leaf)):
with self.subTest(tree=tree):

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.

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.

The three subTest blocks are plain loops in 508c05b, so run_tests.py now runs through the tests/unittest.py shim under CPython.

The tests/unittest.py shim, which run_tests.py uses, has no subTest.
Use plain loops instead.

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

tACK 508c05b

a few things that could still be improved, none blocking:

  • TapLeaf doesn't check miniscript.taproot, so a leaf built in code without taproot=True is checked with wsh rules. TapLeaf(Miniscript.from_string("multi(1,A,B)")) is accepted and compiles to a script ending in OP_CHECKMULTISIG, which can never be spent in tapscript. one if not miniscript.taproot: raise DescriptorError(...) at taptree.py:10 fixes it.
  • type B is needed but not enough. these still parse here, Core (ddb94fd3e1) rejects all of them:
    tr(A,1) not sane: witnesses without signature exist
    tr(A,older(10)) not sane: witnesses without signature exist
    tr(A,sha256(H)) not sane: witnesses without signature exist
    tr(A,and_v(v:pk(B),pk(B))) not sane: contains duplicate public keys
    the first three are anyone-can-spend on the script path. not for this PR, but worth an issue.
  • the wsh check this copies also has the branch length check, tr doesn't get it:
    tr(X/<0;1>/,pk(X/<0;1;2>/))
    embit: accepted, then derive(0, branch_index=2) -> ArgumentError: Invalid branch index
    Core: tr(): Multipath internal key mismatches multipath subscripts lengths
    same on master, so not a regression. probably belongs in Descriptor.init over self.keys.
  • CHANGELOG could say that this is stricter, so descriptors that used to load now raise. tr(A,s:pk(B)), tr(A,pk_k(B)) and tr(A,older(0)) load on master and raise here. Core rejects them too so the change is right, but Krux/SeedSigner/Specter should know before they bump.

@jdlcdl

jdlcdl commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

a few things that could still be improved

Thank you tanveer for improvement comments.

I'll note that I have already merged the changes as they were at 508c05b into "hardened", so if more commits appear here, I can pick and merge them into "hardened" -- or another PR can be made to apply to your comments. I'll leave it up to you, fametrano.

@fametrano

Copy link
Copy Markdown
Author

or another PR can be made to apply to your comments. I'll leave it up to you, fametrano.

Another PR, then: #184, on top of #169, which stays as you merged it. It adds Core's signature and duplicate-key checks on leaves, and the TapLeaf taproot check. Its changelog entry also says that tr() descriptors that used to load now raise, #169's leaves included.

This branch has not been deployed

No deployments
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.

5 participants