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

Passing array reference instead of owned array #780

Open
uncomputable opened this issue Feb 15, 2025 · 13 comments
Open

Passing array reference instead of owned array #780

uncomputable opened this issue Feb 15, 2025 · 13 comments

Comments

@uncomputable
Copy link
Contributor

The from_byte_array methods take as parameter an array reference &[u8; LEN] instead of an owned array [u8; LEN].

Looking at the implementation, the input bytes are converted into a C pointer as_c_ptr() and passed to a C function. From that perspective, an array reference seems fitting because it is easy to convert to a C pointer. However, passing an array reference seems like uncanonical Rust code to me. Copying 32 bytes is not a problem on any system.

Affected functions:
SecretKey::from_byte_array, XOnlyPublicKey::from_byte_array, PublicKey::from_byte_array_compressed, PublicKey::from_byte_array_uncompressed.

@apoelstra
Copy link
Member

Agreed, this feels un-Rust-like and we should pass 32-byte arrays by value. Same with 64/65-byte arrays. I don't think there are larger ones used by this library.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 16, 2025

FTR if people are worried about performance we can slap #[inline] on those functions and it should take care of it.

@uncomputable
Copy link
Contributor Author

I could update the existing methods to take [u8; LEN] and that would be a breaking change.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 17, 2025

We don't mind doing breaking changes.

@apoelstra
Copy link
Member

Yeah, the API in rust-secp is a real mess right now. We are also at a good time right now to make sweeping API changes because in rust-bitcoin we're focused on non-crypto stuff and won't be pressured to cut a new incomplete release of rust-secp anytime soon.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 17, 2025

and won't be pressured to cut a new incomplete release of rust-secp anytime soon

Frankly, I'm really looking forward to the MuSig stuff, I just also don't have that much time for it yet.

@uncomputable
Copy link
Contributor Author

The coexistence of schnorr::Signature::as_byte_array and schnorr::Signature::to_byte_array is also questionable. The former returns &[u8; 64] while the latter returns [u8; 64].

@uncomputable
Copy link
Contributor Author

> git grep '&\[u8;'
src/ecdsa/mod.rs:        noncedata: Option<&[u8; 32]>,
src/ecdsa/mod.rs:        noncedata: &[u8; 32],
src/ecdsa/recovery.rs:        noncedata: &[u8; 32],
src/ellswift.rs:    pub const fn as_secret_bytes(&self) -> &[u8; 32] { &self.0 }
src/key.rs:    fn as_ref(&self) -> &[u8; constants::SECRET_KEY_SIZE] {
src/lib.rs:    pub fn seeded_randomize(&mut self, seed: &[u8; 32]) {
src/scalar.rs:    pub(crate) fn as_be_bytes(&self) -> &[u8; 32] { &self.0 }
src/schnorr.rs:    pub fn as_byte_array(&self) -> &[u8; constants::SCHNORR_SIGNATURE_SIZE] { &self.0 }
src/schnorr.rs:        aux_rand: &[u8; 32],

@apoelstra
Copy link
Member

Yeah, I think we should drop (deprecate) as_byte_array.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 18, 2025

Yeah, I think we should drop (deprecate) as_byte_array.

Definitely not. It's useful when you need to cast a reference to something to a slice reference without dealing with the temporary value causing borrowing issues.

@apoelstra
Copy link
Member

Ahh that's a great point.

@uncomputable
Copy link
Contributor Author

That sounds like AsRef<[u8]>.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 18, 2025

@uncomputable yes, but having a method too is idiomatic and helpful in some cases. The cost of having the method is near zero.

apoelstra added a commit that referenced this issue Feb 19, 2025
59cf7bd refactor: Create keys from owned array values (Christian Lewe)
e7eea32 feat: KeyPair::from_seckey_byte_array (Christian Lewe)
e723d80 refactor: Parse byte array instead of byte slice (Christian Lewe)

Pull request description:

  Construct KeyPair directly from [u8; 32]. I can change the parameter to &[u8; 32] depending on the discussion in #780.

ACKs for top commit:
  Kixunil:
    ACK 59cf7bd
  apoelstra:
    ACK 59cf7bd; successfully ran local tests; nice! the old function signatures were super weird. and the docs were wrong lol
  tcharding:
    ACK 59cf7bd

Tree-SHA512: 64e3026984c79b2491b83775bd50b203ad0723f97ac02b4aa84bc5d0e1135c7fabb660a7d242beaa3f2799f16b169c6bbe3f24e31668518b59ed01839bb86c69
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

No branches or pull requests

3 participants