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

Fix SecretKey FromStr bug #296

Merged
merged 1 commit into from
Apr 29, 2021
Merged

Conversation

sanket1729
Copy link
Member

@sanket1729 sanket1729 commented Apr 26, 2021

SecretKey::from_str did not check if the secret key was a valid one or not

src/key.rs Outdated
/// Converts a `SECRET_KEY_SIZE`- array to a secret key
#[inline]
pub fn from_inner(data: [u8; 32])-> Result<SecretKey, Error> {
match data.len() {
Copy link
Member

Choose a reason for hiding this comment

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

you're matching on the data len when it's already 32 bytes.

maybe just call SecretKey::from_slice instead? (altough I do like using the type system to restrict the size of the slice, especially when you can use TryFrom to convert a slice to an array)

Copy link
Member Author

@sanket1729 sanket1729 Apr 26, 2021

Choose a reason for hiding this comment

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

you're matching on the data len when it's already 32 bytes.

Copy-paste error 🤦

I may be misunderstanding this, but I think there was an unnecessary copy_from_slice in SecretKey::from_slice if we actually own a [u8; 32]. The current FromStr implementation provides an owned [u8; 32] from hex which we convert to a secret key, so I added this method.

@elichai
Copy link
Member

elichai commented Apr 26, 2021

Damn, that's a good find, Thanks!

elichai
elichai previously approved these changes Apr 26, 2021
Copy link
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

ACK
(idk about the name of the function, but I'll leave bikeshedding to others)

Copy link
Contributor

@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.

impl FromStr for PublicKey in the library uses from_slice method, which performs checks and returns error if the key is not correct. I assume we need to use the similar approach for impl FromStr for SecretKey, since there is already a SecretKey::from_slice method doing all necessary checks. Introduction of new key construction function which duplicate code of already existing method + will exist for SecretKey type only (and not other) probably is a bad API (or, we need it to be defined for PublicKey as well and move the shared code to yet another private method)

Secret::from_str did not check if the secret key
was a valid one or not.
@sanket1729
Copy link
Member Author

@dr-orlovsky, It's not possible for PublicKey because we don't know the size of PublicKey (33/65). So, it's a bit more involved and would require more discussion for two different methods. Something like from_inner_compresesed and from_inner_uncompressed might lead away from the simple bug fix.

The code duplication is one method call 🤷 . I don't care that much about the slight inefficiencies of avoiding a 32-byte copy. I can just use the from_slice method to avoid more discussion :P

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 6265b25

@elichai elichai merged commit a66f581 into rust-bitcoin:master Apr 29, 2021
@apoelstra
Copy link
Member

There is an argument that this is a breaking change, but I consider it a bugfix so it's not.

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