16
16
//! Public and secret keys.
17
17
//!
18
18
19
-
20
19
use core:: { fmt, ptr, str} ;
21
20
use core:: ops:: BitXor ;
21
+ use core:: convert:: TryFrom ;
22
22
23
23
use crate :: { constants, from_hex, Secp256k1 , Signing , Verification } ;
24
24
use crate :: Error :: { self , InvalidPublicKey , InvalidPublicKeySum , InvalidSecretKey } ;
25
25
use crate :: ffi:: { self , CPtr , impl_array_newtype} ;
26
+ use crate :: ffi:: types:: c_uint;
26
27
27
28
#[ cfg( feature = "global-context" ) ]
28
29
use crate :: { Message , ecdsa, SECP256K1 } ;
@@ -162,9 +163,8 @@ impl SecretKey {
162
163
/// ```
163
164
#[ inline]
164
165
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) => {
168
168
unsafe {
169
169
if ffi:: secp256k1_ec_seckey_verify (
170
170
ffi:: secp256k1_context_no_precomp,
@@ -174,10 +174,9 @@ impl SecretKey {
174
174
return Err ( InvalidSecretKey ) ;
175
175
}
176
176
}
177
- ret[ ..] . copy_from_slice ( data) ;
178
- Ok ( SecretKey ( ret) )
177
+ Ok ( SecretKey ( data) )
179
178
}
180
- _ => Err ( InvalidSecretKey )
179
+ Err ( _ ) => Err ( InvalidSecretKey )
181
180
}
182
181
}
183
182
@@ -458,39 +457,32 @@ impl PublicKey {
458
457
/// represented by only a single bit, as x determines it up to one bit.
459
458
pub fn serialize ( & self ) -> [ u8 ; constants:: PUBLIC_KEY_SIZE ] {
460
459
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 ) ;
474
461
ret
475
462
}
476
463
464
+ #[ inline]
477
465
/// Serializes the key as a byte-encoded pair of values, in uncompressed form.
478
466
pub fn serialize_uncompressed ( & self ) -> [ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] {
479
467
let mut ret = [ 0u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] ;
468
+ self . serialize_internal ( & mut ret, ffi:: SECP256K1_SER_UNCOMPRESSED ) ;
469
+ ret
470
+ }
480
471
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 (
484
477
ffi:: secp256k1_context_no_precomp,
485
478
ret. as_mut_c_ptr ( ) ,
486
479
& mut ret_len,
487
480
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( ) ) ;
494
486
}
495
487
496
488
#[ inline]
0 commit comments