1616//! Public and secret keys.
1717//!
1818
19-
2019use core:: { fmt, ptr, str} ;
2120use core:: ops:: BitXor ;
21+ use core:: convert:: TryFrom ;
2222
2323use crate :: { constants, from_hex, Secp256k1 , Signing , Verification } ;
2424use crate :: Error :: { self , InvalidPublicKey , InvalidPublicKeySum , InvalidSecretKey } ;
2525use crate :: ffi:: { self , CPtr , impl_array_newtype} ;
26+ use crate :: ffi:: types:: c_uint;
2627
2728#[ cfg( feature = "global-context" ) ]
2829use crate :: { Message , ecdsa, SECP256K1 } ;
@@ -162,9 +163,8 @@ impl SecretKey {
162163 /// ```
163164 #[ inline]
164165 pub fn from_slice ( data : & [ u8 ] ) -> Result < SecretKey , Error > {
165- match data. len ( ) {
166- constants:: SECRET_KEY_SIZE => {
167- let mut ret = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
166+ match <[ u8 ; constants:: SECRET_KEY_SIZE ] >:: try_from ( data) {
167+ Ok ( data) => {
168168 unsafe {
169169 if ffi:: secp256k1_ec_seckey_verify (
170170 ffi:: secp256k1_context_no_precomp,
@@ -174,10 +174,9 @@ impl SecretKey {
174174 return Err ( InvalidSecretKey ) ;
175175 }
176176 }
177- ret[ ..] . copy_from_slice ( data) ;
178- Ok ( SecretKey ( ret) )
177+ Ok ( SecretKey ( data) )
179178 }
180- _ => Err ( InvalidSecretKey )
179+ Err ( _ ) => Err ( InvalidSecretKey )
181180 }
182181 }
183182
@@ -458,39 +457,32 @@ impl PublicKey {
458457 /// represented by only a single bit, as x determines it up to one bit.
459458 pub fn serialize ( & self ) -> [ u8 ; constants:: PUBLIC_KEY_SIZE ] {
460459 let mut ret = [ 0u8 ; constants:: PUBLIC_KEY_SIZE ] ;
461-
462- unsafe {
463- let mut ret_len = constants:: PUBLIC_KEY_SIZE as usize ;
464- let err = ffi:: secp256k1_ec_pubkey_serialize (
465- ffi:: secp256k1_context_no_precomp,
466- ret. as_mut_c_ptr ( ) ,
467- & mut ret_len,
468- self . as_c_ptr ( ) ,
469- ffi:: SECP256K1_SER_COMPRESSED ,
470- ) ;
471- debug_assert_eq ! ( err, 1 ) ;
472- debug_assert_eq ! ( ret_len, ret. len( ) ) ;
473- }
460+ self . serialize_internal ( & mut ret, ffi:: SECP256K1_SER_COMPRESSED ) ;
474461 ret
475462 }
476463
464+ #[ inline]
477465 /// Serializes the key as a byte-encoded pair of values, in uncompressed form.
478466 pub fn serialize_uncompressed ( & self ) -> [ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] {
479467 let mut ret = [ 0u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] ;
468+ self . serialize_internal ( & mut ret, ffi:: SECP256K1_SER_UNCOMPRESSED ) ;
469+ ret
470+ }
480471
481- unsafe {
482- let mut ret_len = constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE as usize ;
483- let err = ffi:: secp256k1_ec_pubkey_serialize (
472+ #[ inline( always) ]
473+ fn serialize_internal ( & self , ret : & mut [ u8 ] , flag : c_uint ) {
474+ let mut ret_len = ret. len ( ) ;
475+ let res = unsafe {
476+ ffi:: secp256k1_ec_pubkey_serialize (
484477 ffi:: secp256k1_context_no_precomp,
485478 ret. as_mut_c_ptr ( ) ,
486479 & mut ret_len,
487480 self . as_c_ptr ( ) ,
488- ffi:: SECP256K1_SER_UNCOMPRESSED ,
489- ) ;
490- debug_assert_eq ! ( err, 1 ) ;
491- debug_assert_eq ! ( ret_len, ret. len( ) ) ;
492- }
493- ret
481+ flag,
482+ )
483+ } ;
484+ debug_assert_eq ! ( res, 1 ) ;
485+ debug_assert_eq ! ( ret_len, ret. len( ) ) ;
494486 }
495487
496488 #[ inline]
0 commit comments