Skip to content

Allow specifing a raw TapLeafHash in sighash computation#722

Merged
RCasatta merged 1 commit intorust-bitcoin:masterfrom
afilini:taproot-sighash-given-leaf-hash
Dec 29, 2021
Merged

Allow specifing a raw TapLeafHash in sighash computation#722
RCasatta merged 1 commit intorust-bitcoin:masterfrom
afilini:taproot-sighash-given-leaf-hash

Conversation

@afilini
Copy link
Copy Markdown
Contributor

@afilini afilini commented Nov 25, 2021

Still need to add some tests but the code should be ready for review. Please let me know if you have better ideas for the enum naming.


Instead of always requiring the full raw script and leaf version, allow
just specifying a raw leaf hash to the sighash computation functions.

This is very useful when dealing with PSBTs, because the
PSBT_IN_TAP_BIP32_DERIVATION field only maps a public key to a leaf
hash, so a signer could just take it and produce a signature with it
rathern than having to jump through hoops to recover the full raw
script.

@RCasatta RCasatta added API break This PR requires a version bump for the next release and removed API break This PR requires a version bump for the next release labels Nov 25, 2021
@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Nov 25, 2021

Maybe TaprootSpendPath? Shorter but usnure if clear.

@afilini afilini force-pushed the taproot-sighash-given-leaf-hash branch from eaaa02e to 35a25ca Compare December 6, 2021 13:00
@afilini
Copy link
Copy Markdown
Contributor Author

afilini commented Dec 6, 2021

Renamed the struct and rebased on the lastest master

@afilini afilini force-pushed the taproot-sighash-given-leaf-hash branch from 35a25ca to 656d732 Compare December 6, 2021 13:01
Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

Not sure if I am right, but better to overlook than underlook (pls see 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.

Am I right that this will ignore the parity flag and will result in an incorrect TapLeafHash for non-even cases?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think the parity flag is encoded here, I think it's only in the control block that goes in the witness

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

How about just removing the existing ScriptPath? It has the slight downside of making the callee compute the leaf hash. But makes the code here cleaner.

Unrelated: I think we should really have some helper functions with minimum parameters for taproot_key_spend_sighash, and taproot_script_spend_sighash. Most users really don't care about LeafVersion, annex, and codeseparator.

@afilini afilini force-pushed the taproot-sighash-given-leaf-hash branch from 656d732 to 5aa1d02 Compare December 18, 2021 15:40
@afilini
Copy link
Copy Markdown
Contributor Author

afilini commented Dec 18, 2021

I've done a few changes:

  • kept ScriptPath so that it can be used to easily compute the TapLeafHash, but removed the op_codeseparator value
  • added two taproot_key_spend_signature_hash() and taproot_script_spend_signature_hash() methods as shortcuts. Both lack the ability to provide an annex, since it's currently unused and could just confuse the users. The latter also assumes the default op_codeseparator value.
  • taproot_script_spend_signature_hash() takes a generic that implements Into<TapLeafHash> for the leaf hash, so it can be used directly with a pre-computed leaf hash, or with ScriptPath

apoelstra
apoelstra previously approved these changes Dec 22, 2021
Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 5aa1d02fee2df825e66347abb179d7ef26330afa

Cleaner and more flexible!

sanket1729
sanket1729 previously approved these changes Dec 23, 2021
Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

cr ACK 5aa1d02fee2df825e66347abb179d7ef26330afa . There is a minor spell error, but it should not be merge blocking.

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 Dec 23, 2021

Choose a reason for hiding this comment

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

nit: enging should be spelled engine . Same error in the following line

@RCasatta
Copy link
Copy Markdown
Collaborator

@afilini, unfortunately, I cannot merge, there is an error on test test_sighashes_with_script_path_raw_hash after merging on master

failures:

---- util::sighash::tests::test_sighashes_with_script_path_raw_hash stdout ----
thread 'util::sighash::tests::test_sighashes_with_script_path_raw_hash' panicked at 'assertion failed: `(left == right)`
  left: `[214, 109, 229, 39, 74, 96, 64, 12, 123, 8, 200, 107, 166, 183, 241, 152, 244, 6, 96, 7, 158, 223, 83, 172, 168, 157, 42, 149, 1, 49, 127, 46]`,
 right: `[207, 245, 203, 179, 121, 42, 102, 162, 155, 146, 233, 196, 9, 51, 120, 145, 230, 162, 26, 232, 30, 49, 240, 43, 241, 165, 224, 76, 32, 214, 144, 226]`', src/util/sighash.rs:997:9

@sanket1729
Copy link
Copy Markdown
Member

@afilini after some investigation, I found that the Tapleafhash is not parsed as reverse in master, but is parsed as reverse in this PR.

I changed the hashes to parse in straight order in another test vector PR #695 because the BIP printed them in without reversing.

@apoelstra
Copy link
Copy Markdown
Member

Great catch @RCasatta !!

@RCasatta
Copy link
Copy Markdown
Collaborator

Great catch @RCasatta !!

Thanks! But it was merit of the great tool https://github.com/bitcoin-core/bitcoin-maintainer-tools/blob/main/github-merge.py with testcmd as cargo test

@apoelstra
Copy link
Copy Markdown
Member

TIL about testcmd :)

Instead of always requiring the full raw script and leaf version, allow
just specifying a raw leaf hash to the sighash computation functions.

This is very useful when dealing with PSBTs, because the
`PSBT_IN_TAP_BIP32_DERIVATION` field only maps a public key to a leaf
hash, so a signer could just take it and produce a signature with it
rathern than having to jump through hoops to recover the full raw
script.
@afilini afilini dismissed stale reviews from sanket1729 and apoelstra via 2959e04 December 27, 2021 15:18
@afilini afilini force-pushed the taproot-sighash-given-leaf-hash branch from 5aa1d02 to 2959e04 Compare December 27, 2021 15:18
@afilini
Copy link
Copy Markdown
Contributor Author

afilini commented Dec 27, 2021

Fixed and rebased :)

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

Tested locally. ACK 2959e04. Reviewed range-diff with 5aa1d02

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 2959e04

Also checked the range-diff.

@RCasatta RCasatta merged commit e511670 into rust-bitcoin:master Dec 29, 2021
erickcestari pushed a commit to erickcestari/rust-bitcoin that referenced this pull request Feb 4, 2026
When fuzzing my new non-recursive tree parsing logic, I noticed that we
were deviating from the released 12.0 in the way that we display l:0.
This is an ambiguous fragment which can be displayed either as l:0 or
u:0. In our released code we use u:0 so stick with that.

This was unintentially changed as part of rust-bitcoin#722. Change it back.

While we are at it add a regression test for rust-bitcoin/rust-miniscript#735
erickcestari pushed a commit to erickcestari/rust-bitcoin that referenced this pull request Feb 4, 2026
…things

1259375 miniscript: make display prefer 'u' over 'l' in the fragment l:0 (Andrew Poelstra)
67fdc50 descriptor: reject strings of the form "tr(<key>,)" (Andrew Poelstra)
00cac40 descriptor: add unit test demonstrating sanity-checking behavior in <= 12.x (Andrew Poelstra)

Pull request description:

  This PR has three changes which are mostly unrelated except that they were all found when fuzzing my "rewrite expression parser to be nonrecursive" branch against 12.x.

  * First is a unit test demonstrating rust-bitcoin#734. It doesn't fix anything, just tests the current behavior.
  * Second is a fix for rust-bitcoin#736 (backported in rust-bitcoin#735).
  * Third tweaks the new `Display` code from rust-bitcoin#722 to change how the ambiguous `l:0`/`u:0` is serialized, to match 12.x.

ACKs for top commit:
  sanket1729:
    ACK 1259375

Tree-SHA512: 921d65a1efd49bda0f9db488a2854b004e14518f584d832497a9dbc13a845ceec99544375385570c6ac42d4985277e8dcbb3aa8654de93235cf9067ba601f91d
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.

6 participants