Skip to content

canonicalize tap leaf hashes by policy - #122

Closed
yuval-block wants to merge 1 commit into
own-psbt-structsfrom
canonical-tap-leaf-hashes
Closed

yuval-block wants to merge 1 commit into
own-psbt-structsfrom
canonical-tap-leaf-hashes

Conversation

@yuval-block

Copy link
Copy Markdown
Collaborator

Follow-up to #116, which stores tap leaf hashes as TapLeafHashes and joins equal values by keeping the left one (IdempotentValue). This PR lets callers choose whether to canonicalize on parse, and makes the join independent of operand order. Nothing in the UnorderedPsbt chain depends on it.

  • TapLeafHashes becomes List(Vec<TapLeafHash>), which keeps the hashes as given, or Set(BTreeSet<TapLeafHash>), the canonical form. Equality is still set equality.
  • New Canonicalizable trait, and a Canonicalization policy chosen by type: Preserve (the default) keeps values as given, and Canonicalize canonicalizes them on conversion. Global::from_v2::<P>, Input::from_v2::<P> and Output::try_from_v2::<P> take the policy, and From/TryFrom use Preserve.
  • joinable_struct! makes each struct Canonicalizable, canonicalizing every field that has a MirroredField conversion through that conversion's new canonicalize method. The code is written once for both policies.
  • Joining equal origins keeps the left one only when both are represented the same way. Otherwise the result is canonical, so the join is structurally commutative and associative, not just up to equality.

Base is own-psbt-structs (#116). Retarget to main once #116 merges.

🤖 Generated with Claude Code

TapLeafHashes is now either a List, which keeps the hashes as they were
given, or a Set, which is the canonical form. Values are still equal
when they hold the same hashes.

Converting from psbt_v2 takes a Canonicalization policy:
Global::from_v2, Input::from_v2 and Output::try_from_v2 take Preserve,
which keeps values as they were given, or Canonicalize, which puts them
into canonical form. From and TryFrom use Preserve. joinable_struct!
makes each struct Canonicalizable by canonicalizing every field that has
a MirroredField conversion, using that conversion's new canonicalize
method.

Joining equal origins keeps the left one only if both are represented
the same way, and canonicalizes it otherwise, so that the join no longer
depends on the order of its operands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
range-diff for force push 26f620c0f5 → be3ad35641 November 16, 2026 00:16 UTC: 1 unchanged; 1 commit; base e4468abab2 → 5cf8f49ac9 (diverged, 1 ahead and 1 behind)

reproduce: git range-diff e4468ab..26f620c 5cf8f49..be3ad35

1:  26f620c0f5 +268 -28 7f = 1:  be3ad35641 +268 -28 7f canonicalize tap leaf hashes by policy

@github-actions

Copy link
Copy Markdown
range-diff for force push 26f620c0f5 → be3ad35641 November 16, 2026 00:17 UTC: 1 unchanged; 1 commit; base e4468abab2 → 5cf8f49ac9 (diverged, 1 ahead and 1 behind)

reproduce: git range-diff e4468ab..26f620c 5cf8f49..be3ad35

1:  26f620c0f5 +268 -28 7f = 1:  be3ad35641 +268 -28 7f canonicalize tap leaf hashes by policy

@yuval-block

Copy link
Copy Markdown
Collaborator Author

Opened mainly to keep a record, this spawned out of a refactor but I'm not sure there is much value to doing this so i'm going to close and we can reopen if anyone sees value in it.

IdempotentValue semantics seems enough for TapLeafHashes even though it is implied to have set semantics by BIP 371. I don't expect any wallets would need to union these by having partial knowledge of the tap tree that each contributes. That seems like an entirely theoretical consideration. This field should also be scrubbed out so not much of a privacy argument to make about canonicalization.

The only other field to which such a policy might apply is BIP 353 DNSSEC proofs, and I think the same argument applies to it as well.

@yuval-block

Copy link
Copy Markdown
Collaborator Author

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.

2 participants