-
Notifications
You must be signed in to change notification settings - Fork 306
Deprecate from_slice methods in favor of arrays #737
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
Conversation
tcharding
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only did shallow review, but everything apart from try_from thing looks good to me, my reviews don't carry much weight in this repo though.
fd5649b to
d612dc0
Compare
|
There are currently not formatting issues on master so there should be no need for a separate formatting patch in this PR bro. |
d612dc0 to
0cb6b30
Compare
|
Third patch removed and incorporated in first two |
Kixunil
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard no deprecating PublicKey::from_slice (but only that one). Other suggestions are not that critical but I'd obviously prefer to fix them.
0cb6b30 to
1a6339a
Compare
|
Removed deprecation of |
1a6339a to
46e06da
Compare
|
code review ack 46e06da except that the |
Functions have been added to PrivateKey, PublicKey and XOnlyPublicKey to allow the creation of a key directly from a byte array.
The PrivateKey and XOnlyPublicKey from_slice functions have been deprecated and calls to them from within the crate have been changed to use the equivalent array function.
46e06da to
537b85b
Compare
|
Thanks for the review. I have replaced |
apoelstra
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 537b85b successfully ran local tests
tcharding
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 537b85b
My suggestions are just because we all enjoy coding round here and it amused me to find a way to re-write your changes using combinators.
| pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error> { | ||
| if data.is_empty() || data.len() != constants::SCHNORR_PUBLIC_KEY_SIZE { | ||
| return Err(Error::InvalidPublicKey); | ||
| match <[u8; constants::SCHNORR_PUBLIC_KEY_SIZE]>::try_from(data) { | ||
| Ok(data) => Self::from_byte_array(&data), | ||
| Err(_) => Err(InvalidPublicKey), | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function could be written with combinators like this:
pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error> {
<[u8; constants::SCHNORR_PUBLIC_KEY_SIZE]>::try_from(data)
.map_err(|_| InvalidPublicKey)
.and_then(|data| Self::from_byte_array(&data))
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does look quite neat with a combinator
| pub fn from_bytes(bytes: [u8; SHARED_SECRET_SIZE]) -> SharedSecret { SharedSecret(bytes) } | ||
|
|
||
| /// Creates a shared secret from `bytes` slice. | ||
| #[deprecated(since = "TBD", note = "Use `from_bytes` instead.")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected with this addition to see a change to call from_bytes. If you like the combinator style you could use
pub fn from_slice(bytes: &[u8]) -> Result<SharedSecret, Error> {
<[u8; SHARED_SECRET_SIZE]>::try_from(bytes)
.map_err(|_| Error::InvalidSharedSecret)
.and_then(|a| Ok(Self::from_bytes(a)))
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I need to change anything again I'll add this too, thanks
|
I also prefer the combinator version, though of course I won't hold up the PR over it. But @Kixunil we need you to take a look at this one again since your status is "change requested". |
|
I find |
Kixunil
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 537b85b
| Ok(constants::UNCOMPRESSED_PUBLIC_KEY_SIZE) => PublicKey::from_slice(&res), | ||
| Ok(constants::PUBLIC_KEY_SIZE) => { | ||
| let bytes: [u8; constants::PUBLIC_KEY_SIZE] = | ||
| res[0..constants::PUBLIC_KEY_SIZE].try_into().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, it'd be better to create a reference here (simply annotating the type with & should work because the conversion is defined) to avoid useless copying but there's a good chance compiler will optimize it out anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you saying you can convert a &[u8] to a &[u8; N] via try_into and the compiler will just cast the pointers rather than copying?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's a huge win and I feel like a clippy lint should exist for it. (Though it'd be a bit tough in general to tell when the array is only used by reference.)
| let mut res = [0u8; constants::SCHNORR_PUBLIC_KEY_SIZE]; | ||
| match from_hex(s, &mut res) { | ||
| Ok(constants::SCHNORR_PUBLIC_KEY_SIZE) => | ||
| XOnlyPublicKey::from_slice(&res[0..constants::SCHNORR_PUBLIC_KEY_SIZE]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOL, the previous code...
…n favor of arrays 537b85b793862096c6a335e0fd14315f64ef1995 Deprecate and replace `from_slice` (Jamil Lambert, PhD) 1661f57d84140b0800552c10d10454d2ad99632e Add `from_byte_array` functions (Jamil Lambert, PhD) Pull request description: As brought up in issue rust-bitcoin/rust-bitcoin#3102 support for Rust arrays is now much better so slice-accepting methods that require a fixed length can be replaced with a method that accepts an array. `from_slice()` methods have been deprecated and calls to it from within the crate have been changed to use the equivalent array method. ACKs for top commit: apoelstra: ACK 537b85b793862096c6a335e0fd14315f64ef1995 successfully ran local tests tcharding: ACK 537b85b793862096c6a335e0fd14315f64ef1995 Kixunil: ACK 537b85b793862096c6a335e0fd14315f64ef1995 Tree-SHA512: 8f8f807af0032130b64a93ff86cae0df1ccee83de052603968be9e30751c27dfc763a6128472e6d0f3a5c2016e7da8e4d06eefc86d8310bdaacc6be0f8fe3822
…n favor of arrays 537b85b793862096c6a335e0fd14315f64ef1995 Deprecate and replace `from_slice` (Jamil Lambert, PhD) 1661f57d84140b0800552c10d10454d2ad99632e Add `from_byte_array` functions (Jamil Lambert, PhD) Pull request description: As brought up in issue rust-bitcoin/rust-bitcoin#3102 support for Rust arrays is now much better so slice-accepting methods that require a fixed length can be replaced with a method that accepts an array. `from_slice()` methods have been deprecated and calls to it from within the crate have been changed to use the equivalent array method. ACKs for top commit: apoelstra: ACK 537b85b793862096c6a335e0fd14315f64ef1995 successfully ran local tests tcharding: ACK 537b85b793862096c6a335e0fd14315f64ef1995 Kixunil: ACK 537b85b793862096c6a335e0fd14315f64ef1995 Tree-SHA512: 8f8f807af0032130b64a93ff86cae0df1ccee83de052603968be9e30751c27dfc763a6128472e6d0f3a5c2016e7da8e4d06eefc86d8310bdaacc6be0f8fe3822
As brought up in issue rust-bitcoin/rust-bitcoin#3102 support for Rust arrays is now much better so slice-accepting methods that require a fixed length can be replaced with a method that accepts an array.
from_slice()methods have been deprecated and calls to it from within the crate have been changed to use the equivalent array method.