Repository navigation
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #378 +/- ##
==========================================
+ Coverage 59.78% 60.49% +0.71%
==========================================
Files 25 26 +1
Lines 4550 4637 +87
==========================================
+ Hits 2720 2805 +85
- Misses 1830 1832 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@vadim-anfv, can you please check this out when you are free? |
vadim-anfv
left a comment
There was a problem hiding this comment.
Approach ACK; successfully ran local tests.
I checked it against miniscript 12.3.7: the splitting and the Huffman loop follow upstream closely, and the pieces you had to reimplement are indeed private there, so there was nothing to call into.
Needs a rebase. The rest is in the inline comments.
| - Updated dependencies to resolve RUSTSEC advisories in rustls, rustls-webpki, webpki-roots, quinn-proto, h2 and zstd-sys; bumped async-hwi to 0.0.35. | ||
| - Fixed `create_tx`, `create_dns_tx`, `create_sp_tx` and `bump_fee` building transactions over Bitcoin Core's 400,000 wu (100,000 vB) standard weight limit, which no default node relays | ||
| - Fixed `key generate` silently producing a 24-word mnemonic for any `--entropy` value other than 12; values other than 12 and 24 are now rejected | ||
| - Changed `compile --type tr` to give each spending path of a disjunctive policy (`or`, `thresh(1, ..)`) its own taproot leaf, weighted by branch probability, so only the path that is used is revealed on-chain. The internal key is still the randomized unspendable key and `r` is still returned. Policies that exceed 1024 taproot leaves are rejected. |
There was a problem hiding this comment.
Trim to the user-visible part? The weighting is implementation detail, and the internal key didn't change, so it doesn't belong in a changelog.
| - Changed `compile --type tr` to give each spending path of a disjunctive policy (`or`, `thresh(1, ..)`) its own taproot leaf, weighted by branch probability, so only the path that is used is revealed on-chain. The internal key is still the randomized unspendable key and `r` is still returned. Policies that exceed 1024 taproot leaves are rejected. | |
| - Changed `compile --type tr` to give each spending path of a disjunctive policy (`or`, `thresh(1, ..)`) its own taproot leaf, so only the used path is revealed on-chain |
There was a problem hiding this comment.
I have trimmed it to the user-visible part. Although I kept the 1024-leaf note since it's a limit users can hit, I mentioned "branch" instead of "spending path." I can drop the 1024 part if you'd rather.
There was a problem hiding this comment.
Can we move compile_tap_tree and everything added for it, tests too, into a new src/utils/taproot.rs? There's enough taproot-only code here now that it doesn't really fit a file about generating and validating descriptors.
There was a problem hiding this comment.
Done. compile_tap_tree, its helpers, and the unit tests are now in src/utils/taproot.rs.
| /// Flatten root-level disjunctions (`or`, `thresh(1, ..)`) into `(probability, branch)` | ||
| /// pairs, one per spending path. |
There was a problem hiding this comment.
"one per spending path" isn't quite right. A branch becomes one leaf, but a leaf can hold several spending paths:
$ cargo run -q --all-features -- compile "thresh(2,pk(A),pk(B),pk(C))" --type tr --placeholders | jq -r .descriptor
tr(88891c65d4d78b3d74e239c71f91c33d52decf6e2fd4222fed898534ebdfaad8,multi_a(2,A,B,C))#g5sva3h6
One leaf, three ways to spend it: A+B, A+C, B+C.
| /// Flatten root-level disjunctions (`or`, `thresh(1, ..)`) into `(probability, branch)` | |
| /// pairs, one per spending path. | |
| /// Flatten root-level disjunctions (`or`, `thresh(1, ..)`) into `(probability, branch)` | |
| /// pairs, one per disjunctive branch. |
| assert_eq!(leaves("or(pk(A),or(pk(B),pk(C)))").len(), 3); | ||
| assert_eq!(leaves("thresh(1,pk(A),pk(B),pk(C),pk(D))").len(), 4); |
There was a problem hiding this comment.
Both recursion arms are covered here, but never together. A mixed one is cheap to add:
| assert_eq!(leaves("or(pk(A),or(pk(B),pk(C)))").len(), 3); | |
| assert_eq!(leaves("thresh(1,pk(A),pk(B),pk(C),pk(D))").len(), 4); | |
| assert_eq!(leaves("or(pk(A),or(pk(B),pk(C)))").len(), 3); | |
| assert_eq!(leaves("thresh(1,pk(A),pk(B),pk(C),pk(D))").len(), 4); | |
| assert_eq!(leaves("or(pk(A),thresh(1,pk(B),pk(C)))").len(), 3); |
A following commit changes how `compile --type tr` builds its script tree (bitcoindevkit#244). Add integration tests for the behavior that must stay the same, so that commit shows them passing unchanged: - a single key, a conjunction and a k-of-n threshold with k > 1 still compile to one leaf - a key reused across branches is rejected, as it is for wsh - a branch that is not safe on every spending path is rejected - the internal key is the randomized unspendable key, never a key from the policy, and `r` is returned These pass on the current code.
`compile --type tr` put the whole policy in a single leaf, so spending any branch of an `or` revealed all the others on-chain. Compile each root-level `or` and `thresh(1, ..)` branch into its own leaf and arrange the leaves as a Huffman tree weighted by branch probability, so only the spent path is revealed. rust-miniscript's `compile_tr` is not used. It takes the internal key from the policy whenever a key can satisfy it alone, which would replace the randomized unspendable key `H + rG` and make `r` prove nothing (bitcoindevkit#218). It also fails on `pk(A)` in miniscript 12.3.7 and 12.3.8 (rust-miniscript#902). Build the tree here from public APIs and leave the internal key as it is. Validate the whole policy before splitting it, as `Concrete::compile` does. Checking branches one at a time would accept a key reused across two of them. Reject policies that compile to more than 1024 leaves, miniscript's limit. Conjunctions, `thresh(k, n)` with k > 1 and single keys still compile to one leaf. Expanding `thresh(k, n)` is left out. Cover it with unit tests for the tree shape and integration tests for the split and the leaf limit. Closes bitcoindevkit#244
4b64f52 to
9ae9df3
Compare
|
Thanks for the review. |
Description
compile --type trput the whole policy into a single taproot leaf, so spending any branch of anorrevealed every other branch on-chain. This gives each spending path of a disjunctive policy (or,thresh(1,..)) its own leaf, arranged as a probability-weighted Huffman tree, so only the path that is used is revealed.The internal key is unchanged: it is still the randomized unspendable key
H + rG, andris still returned (#218, #225). Conjunctions,thresh(k,n)withk > 1, and single keys still compile to one leaf.or(pk(A),pk(B))before and after:Closes #244.
Notes to the reviewers
Why not
compile_tr? Itsextract_key()picks a policy key as the internal key whenever one can satisfy the policy alone, and the unspendable key is only a fallback. Foror(pk(A),pk(B))it returnstr(A,pk(B)), sorwould no longer prove anything about the internal key. It also returnsEmpty Miniscript compilationforpk(A)on miniscript 12.3.7 and 12.3.8 (rust-miniscript#902). So the tree is built here from public miniscript APIs, and only the internal key comes from the existing code.How it works.
compile_tap_treeinsrc/utils/descriptors.rs:is_valid()plus the safety checks, ascompile_trdoes).orandthresh(1,..)into(probability, branch)pairs.compile::<Tap>()and runssanity_check().TapTree::combine, capped at 1024 leaves (miniscript's limit). Ties break deterministically.The only change to
compile_policyis the line that built the single leaf. It works for bothDescriptorPublicKeyandString(--placeholders) keys.Whole-policy validation matters. Validating each branch alone accepted
or(pk(A),and(pk(A),pk(B))), whichwshrejects with "duplicate keys". Step 1 prevents that, and there is a test for it.Trade-off. Unlike stock
compile_tr,or(A,B)gets no key-path spend through A. That follows from keeping the verifiable unspendable key required by #218.Commits. There are two:
Testing.
thresh(2)stay one leaf, a key reused across branches is rejected, an unsafe branch is rejected, and the internal key is the unspendable key withrreturned.or, splitthresh(1), 1024 leaves accepted, 1025 rejected) and 8 unit tests on tree shape, including that a likely branch sits nearer the root.test_compile_*tests are untouched and pass.Not covered.
Descriptor::new_trshould reject it.thresh(k,n)intoandleaves (thecompile_tr_private_experimentalbehavior) is left for a follow-up.r. Since this PR does not touch the internal key, it should compose with refactor(compile): extract unspendable key derivation #368. I'll add a test that runsverify_unspendable_internal_keyon split trees once refactor(compile): extract unspendable key derivation #368 merges.Checklists
All Submissions:
cargo fmtandcargo clippybefore committingNew Features: