@@ -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_slice ( & 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_slice (&[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/// ```
@@ -168,9 +168,13 @@ impl str::FromStr for PublicKey {
168168 fn from_str ( s : & str ) -> Result < PublicKey , Error > {
169169 let mut res = [ 0u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] ;
170170 match from_hex ( s, & mut res) {
171- Ok ( constants:: PUBLIC_KEY_SIZE ) =>
172- PublicKey :: from_slice ( & res[ 0 ..constants:: PUBLIC_KEY_SIZE ] ) ,
173- Ok ( constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ) => PublicKey :: from_slice ( & res) ,
171+ Ok ( constants:: PUBLIC_KEY_SIZE ) => {
172+ let bytes: [ u8 ; constants:: PUBLIC_KEY_SIZE ] =
173+ res[ 0 ..constants:: PUBLIC_KEY_SIZE ] . try_into ( ) . unwrap ( ) ;
174+ PublicKey :: from_byte_array_compressed ( & bytes)
175+ }
176+ Ok ( constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ) =>
177+ PublicKey :: from_byte_array_uncompressed ( & res) ,
174178 _ => Err ( Error :: InvalidPublicKey ) ,
175179 }
176180 }
@@ -203,33 +207,43 @@ impl SecretKey {
203207 SecretKey ( data)
204208 }
205209
206- /// Converts a `SECRET_KEY_SIZE` -byte slice to a secret key.
210+ /// Converts a 32 -byte slice to a secret key.
207211 ///
208212 /// # Examples
209213 ///
210214 /// ```
211215 /// use secp256k1::SecretKey;
212216 /// let sk = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
213217 /// ```
218+ #[ deprecated( since = "TBD" , note = "Use `from_byte_array` instead." ) ]
214219 #[ inline]
215220 pub fn from_slice ( data : & [ u8 ] ) -> Result < SecretKey , Error > {
216221 match <[ u8 ; constants:: SECRET_KEY_SIZE ] >:: try_from ( data) {
217- Ok ( data) => {
218- unsafe {
219- if ffi:: secp256k1_ec_seckey_verify (
220- ffi:: secp256k1_context_no_precomp,
221- data. as_c_ptr ( ) ,
222- ) == 0
223- {
224- return Err ( InvalidSecretKey ) ;
225- }
226- }
227- Ok ( SecretKey ( data) )
228- }
222+ Ok ( data) => Self :: from_byte_array ( & data) ,
229223 Err ( _) => Err ( InvalidSecretKey ) ,
230224 }
231225 }
232226
227+ /// Converts a 32-byte array to a secret key.
228+ ///
229+ /// # Examples
230+ ///
231+ /// ```
232+ /// use secp256k1::SecretKey;
233+ /// let sk = SecretKey::from_byte_array(&[0xcd; 32]).expect("32 bytes, within curve order");
234+ /// ```
235+ #[ inline]
236+ pub fn from_byte_array ( data : & [ u8 ; constants:: SECRET_KEY_SIZE ] ) -> Result < SecretKey , Error > {
237+ unsafe {
238+ if ffi:: secp256k1_ec_seckey_verify ( ffi:: secp256k1_context_no_precomp, data. as_c_ptr ( ) )
239+ == 0
240+ {
241+ return Err ( InvalidSecretKey ) ;
242+ }
243+ }
244+ Ok ( SecretKey ( * data) )
245+ }
246+
233247 /// Creates a new secret key using data from BIP-340 [`Keypair`].
234248 ///
235249 /// # Examples
@@ -353,7 +367,7 @@ impl SecretKey {
353367impl < T : ThirtyTwoByteHash > From < T > for SecretKey {
354368 /// Converts a 32-byte hash directly to a secret key without error paths.
355369 fn from ( t : T ) -> SecretKey {
356- SecretKey :: from_slice ( & t. into_32 ( ) ) . expect ( "failed to create secret key" )
370+ SecretKey :: from_byte_array ( & t. into_32 ( ) ) . expect ( "failed to create secret key" )
357371 }
358372}
359373
@@ -442,17 +456,50 @@ impl PublicKey {
442456 /// Creates a public key directly from a slice.
443457 #[ inline]
444458 pub fn from_slice ( data : & [ u8 ] ) -> Result < PublicKey , Error > {
445- if data. is_empty ( ) {
446- return Err ( Error :: InvalidPublicKey ) ;
459+ match data. len ( ) {
460+ constants:: PUBLIC_KEY_SIZE => PublicKey :: from_byte_array_compressed (
461+ & <[ u8 ; constants:: PUBLIC_KEY_SIZE ] >:: try_from ( data) . unwrap ( ) ,
462+ ) ,
463+ constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE => PublicKey :: from_byte_array_uncompressed (
464+ & <[ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] >:: try_from ( data) . unwrap ( ) ,
465+ ) ,
466+ _ => Err ( InvalidPublicKey ) ,
447467 }
468+ }
448469
470+ /// Creates a public key from a serialized array in compressed format.
471+ #[ inline]
472+ pub fn from_byte_array_compressed (
473+ data : & [ u8 ; constants:: PUBLIC_KEY_SIZE ] ,
474+ ) -> Result < PublicKey , Error > {
449475 unsafe {
450476 let mut pk = ffi:: PublicKey :: new ( ) ;
451477 if ffi:: secp256k1_ec_pubkey_parse (
452478 ffi:: secp256k1_context_no_precomp,
453479 & mut pk,
454480 data. as_c_ptr ( ) ,
455- data. len ( ) ,
481+ constants:: PUBLIC_KEY_SIZE ,
482+ ) == 1
483+ {
484+ Ok ( PublicKey ( pk) )
485+ } else {
486+ Err ( InvalidPublicKey )
487+ }
488+ }
489+ }
490+
491+ /// Creates a public key from a serialized array in uncompressed format.
492+ #[ inline]
493+ pub fn from_byte_array_uncompressed (
494+ data : & [ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] ,
495+ ) -> Result < PublicKey , Error > {
496+ unsafe {
497+ let mut pk = ffi:: PublicKey :: new ( ) ;
498+ if ffi:: secp256k1_ec_pubkey_parse (
499+ ffi:: secp256k1_context_no_precomp,
500+ & mut pk,
501+ data. as_c_ptr ( ) ,
502+ constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ,
456503 ) == 1
457504 {
458505 Ok ( PublicKey ( pk) )
@@ -500,7 +547,7 @@ impl PublicKey {
500547 } ;
501548 buf[ 1 ..] . clone_from_slice ( & pk. serialize ( ) ) ;
502549
503- PublicKey :: from_slice ( & buf) . expect ( "we know the buffer is valid" )
550+ PublicKey :: from_byte_array_compressed ( & buf) . expect ( "we know the buffer is valid" )
504551 }
505552
506553 #[ inline]
@@ -1114,8 +1161,7 @@ impl str::FromStr for XOnlyPublicKey {
11141161 fn from_str ( s : & str ) -> Result < XOnlyPublicKey , Error > {
11151162 let mut res = [ 0u8 ; constants:: SCHNORR_PUBLIC_KEY_SIZE ] ;
11161163 match from_hex ( s, & mut res) {
1117- Ok ( constants:: SCHNORR_PUBLIC_KEY_SIZE ) =>
1118- XOnlyPublicKey :: from_slice ( & res[ 0 ..constants:: SCHNORR_PUBLIC_KEY_SIZE ] ) ,
1164+ Ok ( constants:: SCHNORR_PUBLIC_KEY_SIZE ) => XOnlyPublicKey :: from_byte_array ( & res) ,
11191165 _ => Err ( Error :: InvalidPublicKey ) ,
11201166 }
11211167 }
@@ -1161,12 +1207,25 @@ impl XOnlyPublicKey {
11611207 ///
11621208 /// Returns [`Error::InvalidPublicKey`] if the length of the data slice is not 32 bytes or the
11631209 /// slice does not represent a valid Secp256k1 point x coordinate.
1210+ #[ deprecated( since = "TBD" , note = "Use `from_byte_array` instead." ) ]
11641211 #[ inline]
11651212 pub fn from_slice ( data : & [ u8 ] ) -> Result < XOnlyPublicKey , Error > {
1166- if data. is_empty ( ) || data. len ( ) != constants:: SCHNORR_PUBLIC_KEY_SIZE {
1167- return Err ( Error :: InvalidPublicKey ) ;
1213+ match <[ u8 ; constants:: SCHNORR_PUBLIC_KEY_SIZE ] >:: try_from ( data) {
1214+ Ok ( data) => Self :: from_byte_array ( & data) ,
1215+ Err ( _) => Err ( InvalidPublicKey ) ,
11681216 }
1217+ }
11691218
1219+ /// Creates a schnorr public key directly from a byte array.
1220+ ///
1221+ /// # Errors
1222+ ///
1223+ /// Returns [`Error::InvalidPublicKey`] if the array does not represent a valid Secp256k1 point
1224+ /// x coordinate.
1225+ #[ inline]
1226+ pub fn from_byte_array (
1227+ data : & [ u8 ; constants:: SCHNORR_PUBLIC_KEY_SIZE ] ,
1228+ ) -> Result < XOnlyPublicKey , Error > {
11701229 unsafe {
11711230 let mut pk = ffi:: XOnlyPublicKey :: new ( ) ;
11721231 if ffi:: secp256k1_xonly_pubkey_parse (
0 commit comments