Repository navigation
sort tap leaf hashes when joining taproot derivations - #76
yuval-block wants to merge 1 commit into
Conversation
The join of (Vec<TapLeafHash>, KeySource) compared leaf lists as multisets, but didn't change the order. Under structural equality that made it neither commutative nor associative: a.try_join(b) and b.try_join(a) could return different Ok values, and a conflict could hold two permutations of the same list. The order has no meaning, but nonetheless this change sorts both leaf lists before comparing, and returns the sorted form, so the result no longer depends on which ordering was seen first. This is primarily so the join law prop tests can cover this type. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| /// `self`'s representation (its leaf order) is kept on `Ok`. | ||
| /// | ||
| /// Both leaf lists are sorted before comparing, and the sorted form is what | ||
| /// the join returns, on `Ok` and inside a conflict alike. The result therefore |
There was a problem hiding this comment.
Conflict join keeps unsorted leaf list
The doc says the sorted form is returned inside a conflict, but JoinResult::join inserts an Ok operand unsorted, so a conflict can hold an unsorted leaf list. Folding the same values left vs right yields unequal conflicts (2 vs 3 entries), and the new law generators feed only sorted lists, so they miss it.
Test that fails on this commit
fn unsorted_ok_operand_breaks_associativity() {
use crate::lattice::join::Join;
let a = (vec![leaf(2), leaf(1)], key_source());
let b = (vec![leaf(1), leaf(2)], key_source());
let c = (vec![leaf(3)], key_source());
let left = a.clone().try_join(b.clone()).join(c.clone().wrap());
let right = a.wrap().join(b.try_join(c));
assert_eq!(left, right);
}
#[test]cargo test -p concurrent-psbt unsorted_ok_operand_breaks_associativity
oak · automated · 562cade
There was a problem hiding this comment.
wrap doesn't sort, so an unsorted value can still end up in a conflict
sorting there too fixes the oak test:
fn wrap(mut self) -> JoinResult<Self> {
self.0.sort_unstable();
Ok(self)
}|
i need to think about this a bit more carefully, i think idempotent insertion is sufficient, we care about equivalence not structural equality, and this is not our type on which we can enforce that behavior, see #104 drafting for now |
|
closing, see #122, i think we want to treat TapLeafHashes as opaque values without a notion of equivalence defined by set equality. |
tap_key_origins stores each key's leaf hashes as TapLeafHashes, whose equality ignores order and repetition: BIP 371 gives the list no order. This replaces the custom join on (Vec<TapLeafHash>, KeySource), which compared the lists as multisets. TapLeafHashes joins as an IdempotentValue, so joining equal values keeps the left one's list. A mirrored field's type may now differ from the psbt_v2 field's: an `=` after the type names a MirroredField that converts between them. tap_key_origins uses TapKeyOrigins to convert each key's leaf hashes. supersedes #76 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tap_key_origins stores each key's leaf hashes as TapLeafHashes, whose equality ignores order and repetition: BIP 371 gives the list no order. This replaces the custom join on (Vec<TapLeafHash>, KeySource), which compared the lists as multisets. TapLeafHashes joins as an IdempotentValue, so joining equal values keeps the left one's list. A mirrored field's type may now differ from the psbt_v2 field's: an `=` after the type names a MirroredField that converts between them. tap_key_origins uses TapKeyOrigins to convert each key's leaf hashes. supersedes #76 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tap_key_origins stores each key's leaf hashes as TapLeafHashes, whose equality ignores order and repetition: BIP 371 gives the list no order. This replaces the custom join on (Vec<TapLeafHash>, KeySource), which compared the lists as multisets. TapLeafHashes joins as an IdempotentValue, so joining equal values keeps the left one's list. A mirrored field's type may now differ from the psbt_v2 field's: an `=` after the type names a MirroredField that converts between them. tap_key_origins uses TapKeyOrigins to convert each key's leaf hashes. supersedes #76 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tap_key_origins stores each key's leaf hashes as TapLeafHashes, whose equality ignores order and repetition: BIP 371 gives the list no order. This replaces the custom join on (Vec<TapLeafHash>, KeySource), which compared the lists as multisets. TapLeafHashes joins as an IdempotentValue, so joining equal values keeps the left one's list. A mirrored field's type may now differ from the psbt_v2 field's: an `=` after the type names a MirroredField that converts between them. tap_key_origins uses TapKeyOrigins to convert each key's leaf hashes. supersedes #76 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tap_key_origins stores each key's leaf hashes as TapLeafHashes, whose equality ignores order and repetition: BIP 371 gives the list no order. This replaces the custom join on (Vec<TapLeafHash>, KeySource), which compared the lists as multisets. TapLeafHashes joins as an IdempotentValue, so joining equal values keeps the left one's list. A mirrored field's type may now differ from the psbt_v2 field's: an `=` after the type names a MirroredField that converts between them. tap_key_origins uses TapKeyOrigins to convert each key's leaf hashes. supersedes #76 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The join of (Vec, KeySource) compared leaf lists as multisets, but didn't change the order.
Under structural equality that made it neither commutative nor associative: a.try_join(b) and b.try_join(a) could return different Ok values, and a conflict could hold two permutations of the same list.
The order has no meaning, but nonetheless this change sorts both leaf lists before comparing, and returns the sorted form, so the result no longer depends on which ordering was seen first. This is primarily so the join law prop tests can cover this type.