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

Add serialize function for schnorr::Signature #607

Merged
merged 1 commit into from
May 10, 2023

Conversation

isaac-asdf
Copy link
Contributor

convert from Signature to a byte_array

@isaac-asdf isaac-asdf changed the title add to_bytes function Add to_bytes function for schnorr::Signature Apr 22, 2023
@isaac-asdf isaac-asdf marked this pull request as draft April 22, 2023 03:30
@apoelstra
Copy link
Member

I'm a bit curious what the use case for this is -- we already have .as_ref to get a reference to the underlying bytes, and you could .clone() that to get an owned copy.

@isaac-asdf
Copy link
Contributor Author

Ah, that worked and got me what I needed. I was struggling with figuring out how to get the bytes for the underlying data but the .as_ref gave me what I needed. I'll close this PR. Thanks!

@isaac-asdf isaac-asdf closed this Apr 23, 2023
@isaac-asdf isaac-asdf deleted the add-schnorr-sig-to-bytes branch April 23, 2023 23:46
@Kixunil
Copy link
Collaborator

Kixunil commented May 3, 2023

I think this would still be nice, especially if we add AsRef<&[u8]>. But I'd call it serialize() to have the name consistent with ECDSA signature.

@sanket1729
Copy link
Member

I think this would still be nice, especially if we add AsRef<&[u8]>. But I'd call it serialize() to have the name consistent with ECDSA signature.

+1 . I would still find having this useful.

@isaac-asdf
Copy link
Contributor Author

Would y'all like me to reopen then and update it as serialize()?

@sanket1729
Copy link
Member

@isaac-asdf, yes. Unless @apoelstra has some reasons against it. As you mentioned, it is not very intuitive to find how to covert schnorr sig into bytes.

@isaac-asdf isaac-asdf restored the add-schnorr-sig-to-bytes branch May 6, 2023 19:14
@isaac-asdf isaac-asdf reopened this May 6, 2023
@Kixunil
Copy link
Collaborator

Kixunil commented May 7, 2023

Please remember to change the commit description too. (I usually don't ask to fix commit messages, I ask here because it'd be confusing to keep the same.)

@isaac-asdf isaac-asdf changed the title Add to_bytes function for schnorr::Signature Add serialize function for schnorr::Signature May 8, 2023
@isaac-asdf isaac-asdf marked this pull request as ready for review May 8, 2023 01:21
src/schnorr.rs Outdated
@@ -86,6 +86,9 @@ impl Signature {
}
}

/// Returns a signature as a byte array.
pub fn serialize<T: AsRef<[u8]>>(self) -> [u8; constants::SCHNORR_SIGNATURE_SIZE] { self.0 }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Kixunil is this what you meant by adding the AsRef? I haven't worked as much with generics yet so want to make sure I got that right

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, I meant that in the future we may add impl AsRef<[u8]> for Signature and if we do the code using .as_ref may become ambiguous. The method helps disambiguate.

So this whole generic is not needed (surprised compiler didn't complain), just delete the entire generic.

Also stylistically I think &self is better since that's what most serialize methods are taking. But self is not strictly wrong.
And it occurred to me that you could put #[inline] on top of the method.

@tcharding
Copy link
Member

Can you squash the commits please. I'm pretty sure you want AsRef<&[u8]> (note the &).

@Kixunil
Copy link
Collaborator

Kixunil commented May 8, 2023

@tcharding the code is wrong for a different reason but note that AsRef<[u8]> without & is actually more common than with &. The reference is implied in as_ref signature.

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 8af2cf1

Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 8af2cf1

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 8af2cf1

@apoelstra apoelstra merged commit 5817d32 into rust-bitcoin:master May 10, 2023
15 of 17 checks passed
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.

None yet

5 participants