-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract secretkey code/pubkey serde code in macro
- Loading branch information
Showing
3 changed files
with
223 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/// Secret 256-bit key used as `x` in a signature | ||
pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]); | ||
|
||
impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE); | ||
impl_pretty_debug!(SecretKey); | ||
|
||
impl fmt::LowerHex for SecretKey { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
for ch in &self.0[..] { | ||
write!(f, "{:02x}", *ch)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Display for SecretKey { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::LowerHex::fmt(self, f) | ||
} | ||
} | ||
|
||
impl str::FromStr for SecretKey { | ||
type Err = Error; | ||
fn from_str(s: &str) -> Result<SecretKey, Error> { | ||
let mut res = [0; constants::SECRET_KEY_SIZE]; | ||
match from_hex(s, &mut res) { | ||
Ok(constants::SECRET_KEY_SIZE) => Ok(SecretKey(res)), | ||
_ => Err(Error::InvalidSecretKey) | ||
} | ||
} | ||
} | ||
|
||
/// The number 1 encoded as a secret key | ||
pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 1]); | ||
|
||
#[cfg(any(test, feature = "rand"))] | ||
fn random_32_bytes<R: Rng + ?Sized>(rng: &mut R) -> [u8; 32] { | ||
let mut ret = [0u8; 32]; | ||
rng.fill_bytes(&mut ret); | ||
ret | ||
} | ||
|
||
impl SecretKey { | ||
/// Creates a new random secret key. Requires compilation with the "rand" feature. | ||
#[inline] | ||
#[cfg(any(test, feature = "rand"))] | ||
pub fn new<R: Rng + ?Sized>(rng: &mut R) -> SecretKey { | ||
let mut data = random_32_bytes(rng); | ||
unsafe { | ||
while ffi::secp256k1_ec_seckey_verify( | ||
ffi::secp256k1_context_no_precomp, | ||
data.as_c_ptr(), | ||
) == 0 | ||
{ | ||
data = random_32_bytes(rng); | ||
} | ||
} | ||
SecretKey(data) | ||
} | ||
/// Converts a `SECRET_KEY_SIZE`-byte slice to a secret key | ||
#[inline] | ||
pub fn from_slice(data: &[u8])-> Result<SecretKey, Error> { | ||
match data.len() { | ||
constants::SECRET_KEY_SIZE => { | ||
let mut ret = [0; constants::SECRET_KEY_SIZE]; | ||
unsafe { | ||
if ffi::secp256k1_ec_seckey_verify( | ||
ffi::secp256k1_context_no_precomp, | ||
data.as_c_ptr(), | ||
) == 0 | ||
{ | ||
return Err(InvalidSecretKey); | ||
} | ||
} | ||
ret[..].copy_from_slice(data); | ||
Ok(SecretKey(ret)) | ||
} | ||
_ => Err(InvalidSecretKey) | ||
} | ||
} | ||
#[inline] | ||
/// Negates one secret key. | ||
pub fn negate_assign( | ||
&mut self | ||
) { | ||
unsafe { | ||
let res = ffi::secp256k1_ec_seckey_negate( | ||
ffi::secp256k1_context_no_precomp, | ||
self.as_mut_c_ptr() | ||
); | ||
debug_assert_eq!(res, 1); | ||
} | ||
} | ||
#[inline] | ||
/// Adds one secret key to another, modulo the curve order. WIll | ||
/// return an error if the resulting key would be invalid or if | ||
/// the tweak was not a 32-byte length slice. | ||
pub fn add_assign( | ||
&mut self, | ||
other: &[u8], | ||
) -> Result<(), Error> { | ||
if other.len() != 32 { | ||
return Err(Error::InvalidTweak); | ||
} | ||
unsafe { | ||
if ffi::secp256k1_ec_seckey_tweak_add( | ||
ffi::secp256k1_context_no_precomp, | ||
self.as_mut_c_ptr(), | ||
other.as_c_ptr(), | ||
) != 1 | ||
{ | ||
Err(Error::InvalidTweak) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} | ||
#[inline] | ||
/// Multiplies one secret key by another, modulo the curve order. Will | ||
/// return an error if the resulting key would be invalid or if | ||
/// the tweak was not a 32-byte length slice. | ||
pub fn mul_assign( | ||
&mut self, | ||
other: &[u8], | ||
) -> Result<(), Error> { | ||
if other.len() != 32 { | ||
return Err(Error::InvalidTweak); | ||
} | ||
unsafe { | ||
if ffi::secp256k1_ec_seckey_tweak_mul( | ||
ffi::secp256k1_context_no_precomp, | ||
self.as_mut_c_ptr(), | ||
other.as_c_ptr(), | ||
) != 1 | ||
{ | ||
Err(Error::InvalidTweak) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
serde_impl!(SecretKey, constants::SECRET_KEY_SIZE); |
Oops, something went wrong.