Skip to content

Commit

Permalink
Use new trait TryFrom and do small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Jun 7, 2022
1 parent 7d3a149 commit 39aaac6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/ecdsa/mod.rs
Expand Up @@ -120,7 +120,7 @@ impl SerializedSignature {
/// Convert the serialized signature into the Signature struct.
/// (This DER deserializes it)
pub fn to_signature(&self) -> Result<Signature, Error> {
Signature::from_der(&self)
Signature::from_der(self)
}

/// Create a SerializedSignature from a Signature.
Expand Down
2 changes: 1 addition & 1 deletion src/ecdsa/recovery.rs
Expand Up @@ -36,7 +36,7 @@ impl RecoveryId {
/// Allows library users to create valid recovery IDs from i32.
pub fn from_i32(id: i32) -> Result<RecoveryId, Error> {
match id {
0 | 1 | 2 | 3 => Ok(RecoveryId(id)),
0..=3 => Ok(RecoveryId(id)),
_ => Err(Error::InvalidRecoveryId)
}
}
Expand Down
50 changes: 21 additions & 29 deletions src/key.rs
Expand Up @@ -16,13 +16,14 @@
//! Public and secret keys.
//!


use core::{fmt, ptr, str};
use core::ops::BitXor;
use core::convert::TryFrom;

use crate::{constants, from_hex, Secp256k1, Signing, Verification};
use crate::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
use crate::ffi::{self, CPtr, impl_array_newtype};
use crate::ffi::types::c_uint;

#[cfg(feature = "global-context")]
use crate::{Message, ecdsa, SECP256K1};
Expand Down Expand Up @@ -162,9 +163,8 @@ impl SecretKey {
/// ```
#[inline]
pub fn from_slice(data: &[u8])-> Result<SecretKey, Error> {
match data.len() {
constants::SECRET_KEY_SIZE => {
let mut ret = [0u8; constants::SECRET_KEY_SIZE];
match <[u8; constants::SECRET_KEY_SIZE]>::try_from(data) {
Ok(data) => {
unsafe {
if ffi::secp256k1_ec_seckey_verify(
ffi::secp256k1_context_no_precomp,
Expand All @@ -174,10 +174,9 @@ impl SecretKey {
return Err(InvalidSecretKey);
}
}
ret[..].copy_from_slice(data);
Ok(SecretKey(ret))
Ok(SecretKey(data))
}
_ => Err(InvalidSecretKey)
Err(_) => Err(InvalidSecretKey)
}
}

Expand Down Expand Up @@ -458,39 +457,32 @@ impl PublicKey {
/// represented by only a single bit, as x determines it up to one bit.
pub fn serialize(&self) -> [u8; constants::PUBLIC_KEY_SIZE] {
let mut ret = [0u8; constants::PUBLIC_KEY_SIZE];

unsafe {
let mut ret_len = constants::PUBLIC_KEY_SIZE as usize;
let err = ffi::secp256k1_ec_pubkey_serialize(
ffi::secp256k1_context_no_precomp,
ret.as_mut_c_ptr(),
&mut ret_len,
self.as_c_ptr(),
ffi::SECP256K1_SER_COMPRESSED,
);
debug_assert_eq!(err, 1);
debug_assert_eq!(ret_len, ret.len());
}
self.serialize_internal(&mut ret, ffi::SECP256K1_SER_COMPRESSED);
ret
}

#[inline]
/// Serializes the key as a byte-encoded pair of values, in uncompressed form.
pub fn serialize_uncompressed(&self) -> [u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE] {
let mut ret = [0u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
self.serialize_internal(&mut ret, ffi::SECP256K1_SER_UNCOMPRESSED);
ret
}

unsafe {
let mut ret_len = constants::UNCOMPRESSED_PUBLIC_KEY_SIZE as usize;
let err = ffi::secp256k1_ec_pubkey_serialize(
#[inline(always)]
fn serialize_internal(&self, ret: &mut [u8], flag: c_uint) {
let mut ret_len = ret.len();
let res = unsafe {
ffi::secp256k1_ec_pubkey_serialize(
ffi::secp256k1_context_no_precomp,
ret.as_mut_c_ptr(),
&mut ret_len,
self.as_c_ptr(),
ffi::SECP256K1_SER_UNCOMPRESSED,
);
debug_assert_eq!(err, 1);
debug_assert_eq!(ret_len, ret.len());
}
ret
flag,
)
};
debug_assert_eq!(res, 1);
debug_assert_eq!(ret_len, ret.len());
}

#[inline]
Expand Down

0 comments on commit 39aaac6

Please sign in to comment.