Skip to content

Commit 59cf7bd

Browse files
committed
refactor: Create keys from owned array values
I updated the constructors to take owned array values of the form [u8; LEN] instead of taking array references of the form &[u8; LEN]. This makes the constructors more canonical. Already in this commit, I could remove a bunch of calls to `&` or `*`. Because this is a breaking change, I added an entry to the changelog.
1 parent e7eea32 commit 59cf7bd

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Next
2+
3+
* Create keys from owned array values instead of from references [#781](https://github.com/rust-bitcoin/rust-secp256k1/pull/781)
4+
15
# 0.30.0 - 2024-10-08
26

37
* Allow signing variable-length messages [#706](https://github.com/rust-bitcoin/rust-secp256k1/pull/706)

src/key.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl str::FromStr for SecretKey {
115115
fn from_str(s: &str) -> Result<SecretKey, Error> {
116116
let mut res = [0u8; constants::SECRET_KEY_SIZE];
117117
match from_hex(s, &mut res) {
118-
Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_byte_array(&res),
118+
Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_byte_array(res),
119119
_ => Err(Error::InvalidSecretKey),
120120
}
121121
}
@@ -138,7 +138,7 @@ impl str::FromStr for SecretKey {
138138
/// use secp256k1::{SecretKey, Secp256k1, PublicKey};
139139
///
140140
/// let secp = Secp256k1::new();
141-
/// let secret_key = SecretKey::from_byte_array(&[0xcd; 32]).expect("32 bytes, within curve order");
141+
/// let secret_key = SecretKey::from_byte_array([0xcd; 32]).expect("32 bytes, within curve order");
142142
/// let public_key = PublicKey::from_secret_key(&secp, &secret_key);
143143
/// # }
144144
/// ```
@@ -175,10 +175,10 @@ impl str::FromStr for PublicKey {
175175
Ok(constants::PUBLIC_KEY_SIZE) => {
176176
let bytes: [u8; constants::PUBLIC_KEY_SIZE] =
177177
res[0..constants::PUBLIC_KEY_SIZE].try_into().unwrap();
178-
PublicKey::from_byte_array_compressed(&bytes)
178+
PublicKey::from_byte_array_compressed(bytes)
179179
}
180180
Ok(constants::UNCOMPRESSED_PUBLIC_KEY_SIZE) =>
181-
PublicKey::from_byte_array_uncompressed(&res),
181+
PublicKey::from_byte_array_uncompressed(res),
182182
_ => Err(Error::InvalidPublicKey),
183183
}
184184
}
@@ -223,7 +223,7 @@ impl SecretKey {
223223
#[inline]
224224
pub fn from_slice(data: &[u8]) -> Result<SecretKey, Error> {
225225
match <[u8; constants::SECRET_KEY_SIZE]>::try_from(data) {
226-
Ok(data) => Self::from_byte_array(&data),
226+
Ok(data) => Self::from_byte_array(data),
227227
Err(_) => Err(InvalidSecretKey),
228228
}
229229
}
@@ -234,18 +234,18 @@ impl SecretKey {
234234
///
235235
/// ```
236236
/// use secp256k1::SecretKey;
237-
/// let sk = SecretKey::from_byte_array(&[0xcd; 32]).expect("32 bytes, within curve order");
237+
/// let sk = SecretKey::from_byte_array([0xcd; 32]).expect("32 bytes, within curve order");
238238
/// ```
239239
#[inline]
240-
pub fn from_byte_array(data: &[u8; constants::SECRET_KEY_SIZE]) -> Result<SecretKey, Error> {
240+
pub fn from_byte_array(data: [u8; constants::SECRET_KEY_SIZE]) -> Result<SecretKey, Error> {
241241
unsafe {
242242
if ffi::secp256k1_ec_seckey_verify(ffi::secp256k1_context_no_precomp, data.as_c_ptr())
243243
== 0
244244
{
245245
return Err(InvalidSecretKey);
246246
}
247247
}
248-
Ok(SecretKey(*data))
248+
Ok(SecretKey(data))
249249
}
250250

251251
/// Creates a new secret key using data from BIP-340 [`Keypair`].
@@ -373,7 +373,7 @@ impl SecretKey {
373373
impl<T: ThirtyTwoByteHash> From<T> for SecretKey {
374374
/// Converts a 32-byte hash directly to a secret key without error paths.
375375
fn from(t: T) -> SecretKey {
376-
SecretKey::from_byte_array(&t.into_32()).expect("failed to create secret key")
376+
SecretKey::from_byte_array(t.into_32()).expect("failed to create secret key")
377377
}
378378
}
379379

@@ -403,7 +403,7 @@ impl<'de> serde::Deserialize<'de> for SecretKey {
403403
} else {
404404
let visitor =
405405
super::serde_util::Tuple32Visitor::new("raw 32 bytes SecretKey", |bytes| {
406-
SecretKey::from_byte_array(&bytes)
406+
SecretKey::from_byte_array(bytes)
407407
});
408408
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
409409
}
@@ -464,10 +464,10 @@ impl PublicKey {
464464
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
465465
match data.len() {
466466
constants::PUBLIC_KEY_SIZE => PublicKey::from_byte_array_compressed(
467-
&<[u8; constants::PUBLIC_KEY_SIZE]>::try_from(data).unwrap(),
467+
<[u8; constants::PUBLIC_KEY_SIZE]>::try_from(data).unwrap(),
468468
),
469469
constants::UNCOMPRESSED_PUBLIC_KEY_SIZE => PublicKey::from_byte_array_uncompressed(
470-
&<[u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]>::try_from(data).unwrap(),
470+
<[u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]>::try_from(data).unwrap(),
471471
),
472472
_ => Err(InvalidPublicKey),
473473
}
@@ -476,7 +476,7 @@ impl PublicKey {
476476
/// Creates a public key from a serialized array in compressed format.
477477
#[inline]
478478
pub fn from_byte_array_compressed(
479-
data: &[u8; constants::PUBLIC_KEY_SIZE],
479+
data: [u8; constants::PUBLIC_KEY_SIZE],
480480
) -> Result<PublicKey, Error> {
481481
unsafe {
482482
let mut pk = ffi::PublicKey::new();
@@ -497,7 +497,7 @@ impl PublicKey {
497497
/// Creates a public key from a serialized array in uncompressed format.
498498
#[inline]
499499
pub fn from_byte_array_uncompressed(
500-
data: &[u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE],
500+
data: [u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE],
501501
) -> Result<PublicKey, Error> {
502502
unsafe {
503503
let mut pk = ffi::PublicKey::new();
@@ -553,7 +553,7 @@ impl PublicKey {
553553
};
554554
buf[1..].clone_from_slice(&pk.serialize());
555555

556-
PublicKey::from_byte_array_compressed(&buf).expect("we know the buffer is valid")
556+
PublicKey::from_byte_array_compressed(buf).expect("we know the buffer is valid")
557557
}
558558

559559
#[inline]
@@ -792,7 +792,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
792792
} else {
793793
let visitor =
794794
super::serde_util::Tuple33Visitor::new("33 bytes compressed public key", |bytes| {
795-
PublicKey::from_byte_array_compressed(&bytes)
795+
PublicKey::from_byte_array_compressed(bytes)
796796
});
797797
d.deserialize_tuple(constants::PUBLIC_KEY_SIZE, visitor)
798798
}
@@ -1193,7 +1193,7 @@ impl str::FromStr for XOnlyPublicKey {
11931193
fn from_str(s: &str) -> Result<XOnlyPublicKey, Error> {
11941194
let mut res = [0u8; constants::SCHNORR_PUBLIC_KEY_SIZE];
11951195
match from_hex(s, &mut res) {
1196-
Ok(constants::SCHNORR_PUBLIC_KEY_SIZE) => XOnlyPublicKey::from_byte_array(&res),
1196+
Ok(constants::SCHNORR_PUBLIC_KEY_SIZE) => XOnlyPublicKey::from_byte_array(res),
11971197
_ => Err(Error::InvalidPublicKey),
11981198
}
11991199
}
@@ -1243,7 +1243,7 @@ impl XOnlyPublicKey {
12431243
#[inline]
12441244
pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error> {
12451245
match <[u8; constants::SCHNORR_PUBLIC_KEY_SIZE]>::try_from(data) {
1246-
Ok(data) => Self::from_byte_array(&data),
1246+
Ok(data) => Self::from_byte_array(data),
12471247
Err(_) => Err(InvalidPublicKey),
12481248
}
12491249
}
@@ -1256,7 +1256,7 @@ impl XOnlyPublicKey {
12561256
/// x coordinate.
12571257
#[inline]
12581258
pub fn from_byte_array(
1259-
data: &[u8; constants::SCHNORR_PUBLIC_KEY_SIZE],
1259+
data: [u8; constants::SCHNORR_PUBLIC_KEY_SIZE],
12601260
) -> Result<XOnlyPublicKey, Error> {
12611261
unsafe {
12621262
let mut pk = ffi::XOnlyPublicKey::new();
@@ -1609,7 +1609,7 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
16091609
} else {
16101610
let visitor = super::serde_util::Tuple32Visitor::new(
16111611
"raw 32 bytes schnorr public key",
1612-
|bytes| XOnlyPublicKey::from_byte_array(&bytes),
1612+
XOnlyPublicKey::from_byte_array,
16131613
);
16141614
d.deserialize_tuple(constants::SCHNORR_PUBLIC_KEY_SIZE, visitor)
16151615
}

0 commit comments

Comments
 (0)