Skip to content

Add compile_tr_native() for Taptree-native policy compilation#906

Merged
apoelstra merged 3 commits intorust-bitcoin:masterfrom
privkeyio:compile-tr-native
Mar 16, 2026
Merged

Add compile_tr_native() for Taptree-native policy compilation#906
apoelstra merged 3 commits intorust-bitcoin:masterfrom
privkeyio:compile-tr-native

Conversation

@kwsantiago
Copy link
Copy Markdown
Contributor

Closes #905

Adds compile_tr_native() which decomposes all Or and Thresh branches into separate TapTree leaves instead of using OP_IF/OP_NOTIF within a single leaf. Each leaf is compiled to branch-free Tapscript and assembled into a Huffman-weighted TapTree.

Also refactors enumerate_policy_tree into a shared enumerate_leaves helper to avoid duplication.

@apoelstra
Copy link
Copy Markdown
Member

In ae5c7d3:

I like this approach. Letting the user provide a maximum and returning an error if we go past it is probably best for now. (Much) later I will overhaul the compiler API and provide a general set of control parameters, and hopefully we can unify all the different compilation functions into one, but for now I'd love to see this functionality:

Can you:

  • Run cargo +nightly fmt to make the format job happy (if you don't have a nightly compiler let me know and I'll push a commit that does this)
  • Remove the two dead doc links (see CI job) to MAX_COMPILATION_LEAVES and compile_tr (the latter maybe you just need to fully qualify)
  • Add a unit test for an and-of-ors and(or(A,B),or(C,D)) and check that this explodes into four leaves and semantically matches the other compiler
  • Add a unit test for a 3-of-5 threshold and check that it explodes into 10 leaves (unsure about checking semantic equivalence in this case; just lifting won't be sufficient; maybe just iterating through the leaves and asserting they're all different is enough?)

I'm a little suspicious of your threshold expanding logic. Before I step through it more carefully, I'd like you to add some unit tests just in case it's wrong.

Thanks!

@kwsantiago
Copy link
Copy Markdown
Contributor Author

Ran nightly fmt, fixed the two doc links, and added tests for and(or(A,B),or(C,D)) (4 leaves + semantic match) and thresh(3,5) (10 distinct leaves). Also toned down the issue description re: your feedback.

@apoelstra
Copy link
Copy Markdown
Member

Thanks! Clever use of entails() in a46031c -- I like it.

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK ae5c7d3; successfully ran local tests

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK a46031c; successfully ran local tests

@apoelstra apoelstra merged commit 04f1c58 into rust-bitcoin:master Mar 16, 2026
26 checks passed
@kwsantiago kwsantiago deleted the compile-tr-native branch March 16, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compile_tr: emit Or branches as separate Tapleaves instead of OP_IF/NOTIF

2 participants