Skip to content

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

Open
kwsantiago wants to merge 3 commits intorust-bitcoin:masterfrom
privkeyio:compile-tr-native
Open

Add compile_tr_native() for Taptree-native policy compilation#906
kwsantiago wants to merge 3 commits intorust-bitcoin:masterfrom
privkeyio:compile-tr-native

Conversation

@kwsantiago
Copy link

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

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