Skip to content

sort tap leaf hashes when joining taproot derivations - #76

Closed
yuval-block wants to merge 1 commit into
mainfrom
tap-derivation-join-laws
Closed

yuval-block wants to merge 1 commit into
mainfrom
tap-derivation-join-laws

Conversation

@yuval-block

Copy link
Copy Markdown
Collaborator

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.

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>
@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main 5346f2b tap-derivation-join-... 562cade +/-
100% 100% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

/// `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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)
}

@arminsabouri arminsabouri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

CodeACK

@bc1cindy bc1cindy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 562cade

@nothingmuch
nothingmuch marked this pull request as draft November 13, 2026 02:23
@nothingmuch

Copy link
Copy Markdown
Contributor

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

@yuval-block

Copy link
Copy Markdown
Collaborator Author

closing, see #122, i think we want to treat TapLeafHashes as opaque values without a notion of equivalence defined by set equality.

yuval-block pushed a commit that referenced this pull request Nov 16, 2026
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>
yuval-block pushed a commit that referenced this pull request Nov 16, 2026
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>
yuval-block pushed a commit that referenced this pull request Nov 16, 2026
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>
yuval-block pushed a commit that referenced this pull request Nov 16, 2026
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>
yuval-block pushed a commit that referenced this pull request Nov 16, 2026
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>
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.

5 participants