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