Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make schnorr sign/verify accept a message slice instead of 32 bytes Message #706

Merged
merged 1 commit into from
Jul 5, 2024

Conversation

elichai
Copy link
Member

@elichai elichai commented Jul 4, 2024

As discussed on #702 and on IRC,
BIP340 has evolved from supporting only "pre-hashed" 32 byte messages, to supporting messages of "any length" and as such we should allow the users to pass a message of any length.
Note that passing exactly 32 bytes will make the API behave exactly as before (ie it will produce the same signatures).

I added all the test vectors from: https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv To make sure the API is correct even for empty messages and shorter/longer ones :)

@elichai elichai requested a review from apoelstra July 4, 2024 11:48
@@ -959,8 +959,8 @@ impl Keypair {
/// Constructs an schnorr signature for `msg` using the global [`SECP256K1`] context.
#[inline]
#[cfg(all(feature = "global-context", feature = "rand-std"))]
pub fn sign_schnorr(&self, msg: Message) -> schnorr::Signature {
SECP256K1.sign_schnorr(&msg, self)
pub fn sign_schnorr(&self, msg: &[u8]) -> schnorr::Signature {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not really sure about this. On one hand Message worked like a lint on the other this API makes much more sense in the context of schnorr sigs. I'm tentatively ACK on this but I'll take more time to think about it and possibly hear other opinions.

Other than that, the PR looks OK.

Copy link
Member

Choose a reason for hiding this comment

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

Message forced the user to pre-hash their message, which is not required by the upstream API, has no security benefit, and cannot be worked around (it's not just a lint, it's a meaningful API restriction).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking of having two methods or something. But I guess this is fine. I can't find a good enough reason to do it differently.

Choose a reason for hiding this comment

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

Please publish this change soon. We'd rather not have to fork the crate to support BIP-340 signatures.
@ChristopherA

@apoelstra
Copy link
Member

Can you rebase to get CI passing?

@elichai elichai force-pushed the schnorr_var_msg branch from 38b88ba to 50391fb Compare July 5, 2024 19:06
@elichai
Copy link
Member Author

elichai commented Jul 5, 2024

Can you rebase to get CI passing?

Done :)

@apoelstra
Copy link
Member

Ok, now the CI failures look real -- formatting and MSRV.

@elichai elichai force-pushed the schnorr_var_msg branch from 50391fb to df98b16 Compare July 5, 2024 19:29
Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK df98b16

Copy link
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 df98b16 thanks for all the new test vectors!

@apoelstra apoelstra merged commit ad9d9d0 into rust-bitcoin:master Jul 5, 2024
21 checks passed
@Kixunil
Copy link
Collaborator

Kixunil commented Jul 6, 2024

I've realized that this adds another reason to have a first-class signing API in bitcoin since this double hashing is not intuitive.

@elichai elichai deleted the schnorr_var_msg branch July 6, 2024 08:35
@apoelstra
Copy link
Member

For sure -- in Bitcoin we want a signing API that takes the sighash types (or something equivalent) and does the right thing. And which doesn't let you sign arbitrary crap, at least not without first extracting a rust-secp key.

@wolfmcnally
Copy link

Looking forward to seeing this released!
@ChristopherA

mmtftr added a commit to mmtftr/rust-bitcoin that referenced this pull request Jan 17, 2025
Migrated `from_slice` usage to `from_byte_array` with proper error
forwarding when needed.
Converted to Message::as_ref<[u8]> for signing due to API change of
receiving arbitrary byte arrays in lieu of Message hashes only.
Updated lockfiles.

`from_slice` has been deprecated by rust-bitcoin#3102 due to better support for arrays in Rust.
BIP340 supports arbitrary byte arrays as discussed on rust-bitcoin/rust-secp256k1#706
mmtftr added a commit to mmtftr/rust-bitcoin that referenced this pull request Jan 17, 2025
Migrated `from_slice` usage to `from_byte_array` with proper error
forwarding when needed.
Converted to Message::as_ref<[u8]> for signing due to API change of
receiving arbitrary byte arrays in lieu of Message hashes only.
Updated lockfiles.

`from_slice` has been deprecated by rust-bitcoin#3102 due to better support for arrays in Rust.
BIP340 supports arbitrary byte arrays as discussed on rust-bitcoin/rust-secp256k1#706

Closes rust-bitcoin#3482
mmtftr added a commit to mmtftr/rust-bitcoin that referenced this pull request Jan 17, 2025
Migrated `from_slice` usage to `from_byte_array` with proper error
forwarding when needed.
Converted to Message::as_ref<[u8]> for signing due to API change of
receiving arbitrary byte arrays in lieu of Message hashes only.
Updated lockfiles.

`from_slice` has been deprecated by rust-bitcoin#3102 due to better support for arrays in Rust.
BIP340 supports arbitrary byte arrays as discussed on rust-bitcoin/rust-secp256k1#706

Closes rust-bitcoin#3482
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.

4 participants