-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tr compiler v2 #342
Tr compiler v2 #342
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some partial review of the first few commits
src/policy/concrete.rs
Outdated
}, | ||
)?; | ||
Ok(tree) | ||
self.is_valid()?; // Check for validity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to re-engineer this to not disallow repeated keys in the taproot policy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: Following discussions in issue #338, we currently disallow repeated keys in taproot policy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thinking is as follows: we should allow repeated keys in the policy. While doing individual policy compilations to miniscript, we should check this. Not while compiling the entire tr policy. I want to make sure that we support policy compilations that allow same keys in different tree leaves.
As for this PR, this need not concern us.
05f2431
to
219119d
Compare
219119d
to
989176b
Compare
01c3f71
to
da16d05
Compare
2b13694 Add non-trivial multi-node example (Aman Rojjha) 1c2a80e Add validity and malleability checks. (Aman Rojjha) 0866807 Add P2Tr compiler (Aman Rojjha) 285207e Internal-key extraction done (Aman Rojjha) 26fc574 Policy to single-leaf TapTree compilation done (Aman Rojjha) Pull request description: This PR builds on top of #278. This is one in a series of PRs aimed to implement a feature-complete *Pay-to-Taproot* **P2Tr** compiler. Specifically, this introduces a basic compilation for a given policy by collecting the leaf nodes of the *tree* generated by considering root-level disjunctive `OR`and using this to generate a `TapTree`. > Assuming that _duplicate keys_ are **NOT** possible even in different branches of the TapTree. # Follow Up PRs - #342 - Uses heuristic for tree-generation/ _merging_ algorithm while buillding `TapTree` to optimize over the *expected average total cost*. - #352 - A future PR implementing enumerative strategies for optimizing `thresh` `k`-of-`n` and similar structures. ACKs for top commit: apoelstra: ACK 2b13694 sanket1729: ACK 2b13694 . Let's push forward on this. In the interest of ACKs and multiple rebases already done here, I am merging this. We recently shifted to rust 2018. And this code has a bunch of warnings because of it. Tree-SHA512: 4ceca51a383f5d52b572a16937bbcc3a9c53f0247e4b6df57a6547fd0b1c7cc33ff04dd9a476914bcf6d0a09e255918b8f7ebfe176c839d6ae31c84613dce67f
@sanket1729 @apoelstra This PR is ready-for-review, given #291 has been merged now. Kindly take a look at it!
|
da16d05
to
83a685d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approach and algorithm ACK. some code improvments to be done
src/policy/concrete.rs
Outdated
|
||
/// [`Miniscript`] -> leaf probability in policy cache | ||
#[cfg(feature = "compiler")] | ||
type MsTapCache<Pk> = BTreeMap<TapTree<Pk>, f64>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when there is a collision? Making sure that we are dealing with those cases in a sane manner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure I'll update this! This was done considering that there were no duplicate keys in the initial policy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no duplicates, but still, there can be collisions in the compilation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that makes sense! Thanks.
src/policy/concrete.rs
Outdated
}, | ||
)?; | ||
Ok(tree) | ||
self.is_valid()?; // Check for validity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thinking is as follows: we should allow repeated keys in the policy. While doing individual policy compilations to miniscript, we should check this. Not while compiling the entire tr policy. I want to make sure that we support policy compilations that allow same keys in different tree leaves.
As for this PR, this need not concern us.
examples/taproot.rs
Outdated
|
||
fn main() { | ||
let policies_str = [ | ||
"or(1@pk(A),1@pk(B))", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make this example as a tutorial.
- Take a example policy,
- explain what does
- explain the compiled result.
Do this a couple of times for interesting results. Using unspendable internal key, useful internal key being inferred etc..
83a685d
to
d384b63
Compare
Changes in the force-push
|
5b2f024
to
6ce9fb1
Compare
@SarcasticNastik, we recently merged support for no-std. This would require another rebase |
375b94b
to
2940416
Compare
Hey @SarcasticNastik, looks are still some issues. Let me know if you are having difficulty in dealing with CI with no-std. The general thumb rules are 1) use core instead of std, 2) Common things should be available via |
2940416
to
7828b38
Compare
@sanket1729 Done :p! Thanks for the advice. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last iteration(I think :P)
src/policy/mod.rs
Outdated
.compile_tr(Some(unspendable_key.clone())) | ||
.unwrap(); | ||
let opt_expected_desc = Descriptor::Tr( | ||
Tr::<String>::from_str("tr(A,{{pk(D),pk(C)},{pk(B),and_v(v:pk(E),pk(F))}})") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we succeed in finding any policy where these differs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We did find policies where the TapTree leaf nodes re-arranged, but none yet where we noticed policy-merging. One of those were:
Policy: thresh(1,or(1@pk(A),1@pk(B)),or(1@pk(C),1@or(1@and(pk(E),pk(F)),1@pk(D))))
Private compilation: tr(A,{{and_v(v:pk(E),pk(F)),pk(D)},{pk(C),pk(B)}})#y7cmc3xl
Default compilation: tr(A,{{pk(D),pk(C)},{pk(B),and_v(v:pk(E),pk(F))}})#vv7casf3
d4f3245
to
5c456d7
Compare
Changes in the force-push
|
5c456d7
to
7a5db70
Compare
1c34908
to
faa4e16
Compare
@sanket1729 I've added the document explaining how the private compilation is efficient too and refactored the example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last few comments. Almost there
src/policy/concrete.rs
Outdated
@@ -248,6 +248,9 @@ impl<Pk: MiniscriptKey> Policy<Pk> { | |||
/// `[pk(A),pk(B),and(or(pk(C),pk(D)),pk(E)))]`. Each policy in the vector is compiled into | |||
/// the respective miniscripts. A Huffman Tree is created from this vector which optimizes over | |||
/// the probabilitity of satisfaction for the respective branch in the TapTree. | |||
/// | |||
/// Refer to [doc/Tr compiler.pdf] in the root of the repository to understand why such |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we also want the source code of this uploaded somewhere where I can copy/edit it. In case, we find some errors in the document, there would be no way to edit it.
Can you create a new repo with the code for this? And also link the source here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the gist here and linked it in the docs.
faa4e16
to
7dc91d9
Compare
This write-up is aimed to reason about how the private-compilation (as defined in doc) is also cost-efficient.
7dc91d9
to
ef4249d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK ef4249d
169d849 Add policy to descriptor target compilation method (Aman Rojjha) Pull request description: This PR works on top of #291 and #342. It introduces a new function `compile_to_descriptor` which requires a `DescriptorCtx` (suggestions for a better enum name are welcome!) allowing directly compilation of a policy into the specified descriptor type. ACKs for top commit: apoelstra: ACK 169d849 sanket1729: ACK 169d849 Tree-SHA512: 0f895f4774ea4f56db76280ac756df616a70e5d7d60cc128f9a050ef86658f786b2d45885d748dba51751146a6e93b3731f4ae193b7d80284ffaea20be1e8f92
This PR builds on top of #291. This aims to introduce an efficient version of the tapscript compiler by using a few heuristics to optimize over the expected average total cost for the TapTree.
Strategy implemented
A
andB
, check whether the compilation ofPolicy::Or(policy(A), policy(B))
is more efficient than a TapTree with the respective childrenA
andB
.Note: This doesn't include the
thresh(k, ...n..)
enumeration strategy. Planning on working on it separately.