@@ -115,7 +115,7 @@ impl str::FromStr for SecretKey {
115
115
fn from_str ( s : & str ) -> Result < SecretKey , Error > {
116
116
let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
117
117
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) ,
119
119
_ => Err ( Error :: InvalidSecretKey ) ,
120
120
}
121
121
}
@@ -138,7 +138,7 @@ impl str::FromStr for SecretKey {
138
138
/// use secp256k1::{SecretKey, Secp256k1, PublicKey};
139
139
///
140
140
/// 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");
142
142
/// let public_key = PublicKey::from_secret_key(&secp, &secret_key);
143
143
/// # }
144
144
/// ```
@@ -175,10 +175,10 @@ impl str::FromStr for PublicKey {
175
175
Ok ( constants:: PUBLIC_KEY_SIZE ) => {
176
176
let bytes: [ u8 ; constants:: PUBLIC_KEY_SIZE ] =
177
177
res[ 0 ..constants:: PUBLIC_KEY_SIZE ] . try_into ( ) . unwrap ( ) ;
178
- PublicKey :: from_byte_array_compressed ( & bytes)
178
+ PublicKey :: from_byte_array_compressed ( bytes)
179
179
}
180
180
Ok ( constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ) =>
181
- PublicKey :: from_byte_array_uncompressed ( & res) ,
181
+ PublicKey :: from_byte_array_uncompressed ( res) ,
182
182
_ => Err ( Error :: InvalidPublicKey ) ,
183
183
}
184
184
}
@@ -223,7 +223,7 @@ impl SecretKey {
223
223
#[ inline]
224
224
pub fn from_slice ( data : & [ u8 ] ) -> Result < SecretKey , Error > {
225
225
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) ,
227
227
Err ( _) => Err ( InvalidSecretKey ) ,
228
228
}
229
229
}
@@ -234,18 +234,18 @@ impl SecretKey {
234
234
///
235
235
/// ```
236
236
/// 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");
238
238
/// ```
239
239
#[ 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 > {
241
241
unsafe {
242
242
if ffi:: secp256k1_ec_seckey_verify ( ffi:: secp256k1_context_no_precomp, data. as_c_ptr ( ) )
243
243
== 0
244
244
{
245
245
return Err ( InvalidSecretKey ) ;
246
246
}
247
247
}
248
- Ok ( SecretKey ( * data) )
248
+ Ok ( SecretKey ( data) )
249
249
}
250
250
251
251
/// Creates a new secret key using data from BIP-340 [`Keypair`].
@@ -373,7 +373,7 @@ impl SecretKey {
373
373
impl < T : ThirtyTwoByteHash > From < T > for SecretKey {
374
374
/// Converts a 32-byte hash directly to a secret key without error paths.
375
375
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" )
377
377
}
378
378
}
379
379
@@ -401,10 +401,10 @@ impl<'de> serde::Deserialize<'de> for SecretKey {
401
401
"a hex string representing 32 byte SecretKey" ,
402
402
) )
403
403
} else {
404
- let visitor = super :: serde_util :: Tuple32Visitor :: new (
405
- "raw 32 bytes SecretKey" ,
406
- SecretKey :: from_slice ,
407
- ) ;
404
+ let visitor =
405
+ super :: serde_util :: Tuple32Visitor :: new ( "raw 32 bytes SecretKey" , |bytes| {
406
+ SecretKey :: from_byte_array ( bytes )
407
+ } ) ;
408
408
d. deserialize_tuple ( constants:: SECRET_KEY_SIZE , visitor)
409
409
}
410
410
}
@@ -464,10 +464,10 @@ impl PublicKey {
464
464
pub fn from_slice ( data : & [ u8 ] ) -> Result < PublicKey , Error > {
465
465
match data. len ( ) {
466
466
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 ( ) ,
468
468
) ,
469
469
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 ( ) ,
471
471
) ,
472
472
_ => Err ( InvalidPublicKey ) ,
473
473
}
@@ -476,7 +476,7 @@ impl PublicKey {
476
476
/// Creates a public key from a serialized array in compressed format.
477
477
#[ inline]
478
478
pub fn from_byte_array_compressed (
479
- data : & [ u8 ; constants:: PUBLIC_KEY_SIZE ] ,
479
+ data : [ u8 ; constants:: PUBLIC_KEY_SIZE ] ,
480
480
) -> Result < PublicKey , Error > {
481
481
unsafe {
482
482
let mut pk = ffi:: PublicKey :: new ( ) ;
@@ -497,7 +497,7 @@ impl PublicKey {
497
497
/// Creates a public key from a serialized array in uncompressed format.
498
498
#[ inline]
499
499
pub fn from_byte_array_uncompressed (
500
- data : & [ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] ,
500
+ data : [ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] ,
501
501
) -> Result < PublicKey , Error > {
502
502
unsafe {
503
503
let mut pk = ffi:: PublicKey :: new ( ) ;
@@ -553,7 +553,7 @@ impl PublicKey {
553
553
} ;
554
554
buf[ 1 ..] . clone_from_slice ( & pk. serialize ( ) ) ;
555
555
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" )
557
557
}
558
558
559
559
#[ inline]
@@ -790,10 +790,10 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
790
790
"an ASCII hex string representing a public key" ,
791
791
) )
792
792
} else {
793
- let visitor = super :: serde_util :: Tuple33Visitor :: new (
794
- "33 bytes compressed public key" ,
795
- PublicKey :: from_slice ,
796
- ) ;
793
+ let visitor =
794
+ super :: serde_util :: Tuple33Visitor :: new ( "33 bytes compressed public key" , |bytes| {
795
+ PublicKey :: from_byte_array_compressed ( bytes )
796
+ } ) ;
797
797
d. deserialize_tuple ( constants:: PUBLIC_KEY_SIZE , visitor)
798
798
}
799
799
}
@@ -859,16 +859,29 @@ impl Keypair {
859
859
/// # Errors
860
860
///
861
861
/// [`Error::InvalidSecretKey`] if the slice is not exactly 32 bytes long,
862
- /// or if the encoded number exceeds the Secp256k1 field `p` value.
862
+ /// or if the encoded number is an invalid scalar.
863
+ #[ deprecated( since = "TBD" , note = "Use `from_seckey_byte_array` instead." ) ]
863
864
#[ inline]
864
865
pub fn from_seckey_slice < C : Signing > (
865
866
secp : & Secp256k1 < C > ,
866
867
data : & [ u8 ] ,
867
868
) -> Result < Keypair , Error > {
868
- if data. is_empty ( ) || data. len ( ) != constants:: SECRET_KEY_SIZE {
869
- return Err ( Error :: InvalidSecretKey ) ;
869
+ match <[ u8 ; constants:: SECRET_KEY_SIZE ] >:: try_from ( data) {
870
+ Ok ( data) => Self :: from_seckey_byte_array ( secp, data) ,
871
+ Err ( _) => Err ( Error :: InvalidSecretKey ) ,
870
872
}
873
+ }
871
874
875
+ /// Creates a [`Keypair`] directly from a secret key byte array.
876
+ ///
877
+ /// # Errors
878
+ ///
879
+ /// [`Error::InvalidSecretKey`] if the encoded number is an invalid scalar.
880
+ #[ inline]
881
+ pub fn from_seckey_byte_array < C : Signing > (
882
+ secp : & Secp256k1 < C > ,
883
+ data : [ u8 ; constants:: SECRET_KEY_SIZE ] ,
884
+ ) -> Result < Keypair , Error > {
872
885
unsafe {
873
886
let mut kp = ffi:: Keypair :: new ( ) ;
874
887
if ffi:: secp256k1_keypair_create ( secp. ctx . as_ptr ( ) , & mut kp, data. as_c_ptr ( ) ) == 1 {
@@ -884,13 +897,12 @@ impl Keypair {
884
897
/// # Errors
885
898
///
886
899
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
887
- /// or if the encoded number exceeds the Secp256k1 field `p` value .
900
+ /// or if the encoded number is an invalid scalar .
888
901
#[ inline]
889
902
pub fn from_seckey_str < C : Signing > ( secp : & Secp256k1 < C > , s : & str ) -> Result < Keypair , Error > {
890
903
let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
891
904
match from_hex ( s, & mut res) {
892
- Ok ( constants:: SECRET_KEY_SIZE ) =>
893
- Keypair :: from_seckey_slice ( secp, & res[ 0 ..constants:: SECRET_KEY_SIZE ] ) ,
905
+ Ok ( constants:: SECRET_KEY_SIZE ) => Keypair :: from_seckey_byte_array ( secp, res) ,
894
906
_ => Err ( Error :: InvalidSecretKey ) ,
895
907
}
896
908
}
@@ -900,7 +912,7 @@ impl Keypair {
900
912
/// # Errors
901
913
///
902
914
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
903
- /// or if the encoded number exceeds the Secp256k1 field `p` value .
915
+ /// or if the encoded number is an invalid scalar .
904
916
#[ inline]
905
917
#[ cfg( feature = "global-context" ) ]
906
918
pub fn from_seckey_str_global ( s : & str ) -> Result < Keypair , Error > {
@@ -1117,7 +1129,7 @@ impl<'de> serde::Deserialize<'de> for Keypair {
1117
1129
let ctx = Secp256k1 :: signing_only ( ) ;
1118
1130
1119
1131
#[ allow( clippy:: needless_borrow) ]
1120
- Keypair :: from_seckey_slice ( & ctx, data)
1132
+ Keypair :: from_seckey_byte_array ( & ctx, data)
1121
1133
} ) ;
1122
1134
d. deserialize_tuple ( constants:: SECRET_KEY_SIZE , visitor)
1123
1135
}
@@ -1181,7 +1193,7 @@ impl str::FromStr for XOnlyPublicKey {
1181
1193
fn from_str ( s : & str ) -> Result < XOnlyPublicKey , Error > {
1182
1194
let mut res = [ 0u8 ; constants:: SCHNORR_PUBLIC_KEY_SIZE ] ;
1183
1195
match from_hex ( s, & mut res) {
1184
- Ok ( constants:: SCHNORR_PUBLIC_KEY_SIZE ) => XOnlyPublicKey :: from_byte_array ( & res) ,
1196
+ Ok ( constants:: SCHNORR_PUBLIC_KEY_SIZE ) => XOnlyPublicKey :: from_byte_array ( res) ,
1185
1197
_ => Err ( Error :: InvalidPublicKey ) ,
1186
1198
}
1187
1199
}
@@ -1231,7 +1243,7 @@ impl XOnlyPublicKey {
1231
1243
#[ inline]
1232
1244
pub fn from_slice ( data : & [ u8 ] ) -> Result < XOnlyPublicKey , Error > {
1233
1245
match <[ u8 ; constants:: SCHNORR_PUBLIC_KEY_SIZE ] >:: try_from ( data) {
1234
- Ok ( data) => Self :: from_byte_array ( & data) ,
1246
+ Ok ( data) => Self :: from_byte_array ( data) ,
1235
1247
Err ( _) => Err ( InvalidPublicKey ) ,
1236
1248
}
1237
1249
}
@@ -1244,7 +1256,7 @@ impl XOnlyPublicKey {
1244
1256
/// x coordinate.
1245
1257
#[ inline]
1246
1258
pub fn from_byte_array (
1247
- data : & [ u8 ; constants:: SCHNORR_PUBLIC_KEY_SIZE ] ,
1259
+ data : [ u8 ; constants:: SCHNORR_PUBLIC_KEY_SIZE ] ,
1248
1260
) -> Result < XOnlyPublicKey , Error > {
1249
1261
unsafe {
1250
1262
let mut pk = ffi:: XOnlyPublicKey :: new ( ) ;
@@ -1597,7 +1609,7 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
1597
1609
} else {
1598
1610
let visitor = super :: serde_util:: Tuple32Visitor :: new (
1599
1611
"raw 32 bytes schnorr public key" ,
1600
- XOnlyPublicKey :: from_slice ,
1612
+ XOnlyPublicKey :: from_byte_array ,
1601
1613
) ;
1602
1614
d. deserialize_tuple ( constants:: SCHNORR_PUBLIC_KEY_SIZE , visitor)
1603
1615
}
@@ -1665,7 +1677,7 @@ mod test {
1665
1677
#[ cfg( all( feature = "std" , not( secp256k1_fuzz) ) ) ]
1666
1678
fn erased_keypair_is_valid ( ) {
1667
1679
let s = Secp256k1 :: new ( ) ;
1668
- let kp = Keypair :: from_seckey_slice ( & s, & [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1680
+ let kp = Keypair :: from_seckey_byte_array ( & s, [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1669
1681
. expect ( "valid secret key" ) ;
1670
1682
let mut kp2 = kp;
1671
1683
kp2. non_secure_erase ( ) ;
@@ -2272,7 +2284,7 @@ mod test {
2272
2284
] ;
2273
2285
static SK_STR : & str = "01010101010101010001020304050607ffff0000ffff00006363636363636363" ;
2274
2286
2275
- let sk = Keypair :: from_seckey_slice ( SECP256K1 , & SK_BYTES ) . unwrap ( ) ;
2287
+ let sk = Keypair :: from_seckey_byte_array ( SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2276
2288
#[ rustfmt:: skip]
2277
2289
assert_tokens ( & sk. compact ( ) , & [
2278
2290
Token :: Tuple { len : 32 } ,
@@ -2452,7 +2464,7 @@ mod test {
2452
2464
2453
2465
static PK_STR : & str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" ;
2454
2466
2455
- let kp = Keypair :: from_seckey_slice ( crate :: SECP256K1 , & SK_BYTES ) . unwrap ( ) ;
2467
+ let kp = Keypair :: from_seckey_byte_array ( crate :: SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2456
2468
let ( pk, _parity) = XOnlyPublicKey :: from_keypair ( & kp) ;
2457
2469
2458
2470
#[ rustfmt:: skip]
0 commit comments