Skip to content

Commit 39aaac6

Browse files
committed
Use new trait TryFrom and do small refactoring
1 parent 7d3a149 commit 39aaac6

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

src/ecdsa/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl SerializedSignature {
120120
/// Convert the serialized signature into the Signature struct.
121121
/// (This DER deserializes it)
122122
pub fn to_signature(&self) -> Result<Signature, Error> {
123-
Signature::from_der(&self)
123+
Signature::from_der(self)
124124
}
125125

126126
/// Create a SerializedSignature from a Signature.

src/ecdsa/recovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl RecoveryId {
3636
/// Allows library users to create valid recovery IDs from i32.
3737
pub fn from_i32(id: i32) -> Result<RecoveryId, Error> {
3838
match id {
39-
0 | 1 | 2 | 3 => Ok(RecoveryId(id)),
39+
0..=3 => Ok(RecoveryId(id)),
4040
_ => Err(Error::InvalidRecoveryId)
4141
}
4242
}

src/key.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
//! Public and secret keys.
1717
//!
1818
19-
2019
use core::{fmt, ptr, str};
2120
use core::ops::BitXor;
21+
use core::convert::TryFrom;
2222

2323
use crate::{constants, from_hex, Secp256k1, Signing, Verification};
2424
use crate::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
2525
use crate::ffi::{self, CPtr, impl_array_newtype};
26+
use crate::ffi::types::c_uint;
2627

2728
#[cfg(feature = "global-context")]
2829
use 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

Comments
 (0)