Skip to content

Commit 8ee4e05

Browse files
committed
Removing uneeded Secp context arguments
1 parent 7e2f7fe commit 8ee4e05

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

src/key.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use constants;
2727
use ffi::{self, CPtr};
2828

2929
/// Secret 256-bit key used as `x` in an ECDSA signature
30-
pub struct SecretKey(pub(crate) [u8; constants::SECRET_KEY_SIZE]);
30+
pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]);
3131
impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE);
3232
impl_pretty_debug!(SecretKey);
3333

@@ -66,7 +66,7 @@ pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,
6666
/// A Secp256k1 public key, used for verification of signatures
6767
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
6868
#[repr(transparent)]
69-
pub struct PublicKey(pub(crate) ffi::PublicKey);
69+
pub struct PublicKey(ffi::PublicKey);
7070

7171
impl fmt::LowerHex for PublicKey {
7272
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -149,6 +149,21 @@ impl SecretKey {
149149
}
150150
}
151151

152+
/// Creates a new secret key using data from BIP-340 [`::schnorrsig::KeyPair`]
153+
#[inline]
154+
pub fn from_keypair(keypair: &::schnorrsig::KeyPair) -> Self {
155+
let mut sk = [0; constants::SECRET_KEY_SIZE];
156+
unsafe {
157+
let ret = ffi::secp256k1_keypair_sec(
158+
ffi::secp256k1_context_no_precomp,
159+
sk.as_mut_c_ptr(),
160+
keypair.as_ptr()
161+
);
162+
debug_assert_eq!(ret, 1);
163+
}
164+
SecretKey(sk)
165+
}
166+
152167
#[inline]
153168
/// Negates one secret key.
154169
pub fn negate_assign(
@@ -290,6 +305,21 @@ impl PublicKey {
290305
}
291306
}
292307

308+
/// Creates a new compressed public key key using data from BIP-340 [`::schnorrsig::KeyPair`]
309+
#[inline]
310+
pub fn from_keypair(keypair: &::schnorrsig::KeyPair) -> Self {
311+
unsafe {
312+
let mut pk = ffi::PublicKey::new();
313+
let ret = ffi::secp256k1_keypair_pub(
314+
ffi::secp256k1_context_no_precomp,
315+
&mut pk,
316+
keypair.as_ptr()
317+
);
318+
debug_assert_eq!(ret, 1);
319+
PublicKey(pk)
320+
}
321+
}
322+
293323
#[inline]
294324
/// Serialize the key as a byte-encoded pair of values. In compressed form
295325
/// the y-coordinate is represented by only a single bit, as x determines

src/schnorrsig.rs

+26-30
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ impl KeyPair {
213213

214214
/// Serialize the key pair as a secret key byte value
215215
#[inline]
216-
pub fn serialize_sec<V: Verification>(&self, secp: &Secp256k1<V>) -> [u8; constants::SECRET_KEY_SIZE] {
217-
*SecretKey::from_keypair(secp, self).as_ref()
216+
pub fn serialize_secret(&self) -> [u8; constants::SECRET_KEY_SIZE] {
217+
*SecretKey::from_keypair(self).as_ref()
218218
}
219219

220220
/// Tweak a keypair by adding the given tweak to the secret key and updating the
@@ -456,35 +456,31 @@ impl<'de> ::serde::Deserialize<'de> for PublicKey {
456456
}
457457
}
458458

459-
impl SecretKey {
460-
/// Creates a new secret key using data from BIP-340 [`KeyPair`]
461-
pub fn from_keypair<V: Verification>(secp: &Secp256k1<V>, keypair: &KeyPair) -> Self {
462-
let mut sk = [0; constants::SECRET_KEY_SIZE];
463-
unsafe {
464-
let ret = ffi::secp256k1_keypair_sec(
465-
secp.ctx,
466-
sk.as_mut_c_ptr(),
467-
keypair.as_ptr()
468-
);
469-
debug_assert_eq!(ret, 1);
470-
}
471-
SecretKey(sk)
459+
impl From<KeyPair> for SecretKey {
460+
#[inline]
461+
fn from(pair: KeyPair) -> Self {
462+
SecretKey::from_keypair(&pair)
472463
}
473464
}
474465

475-
impl ::key::PublicKey {
476-
/// Creates a new compressed public key key using data from BIP-340 [`KeyPair`]
477-
pub fn from_keypair<C: Signing>(secp: &Secp256k1<C>, keypair: &KeyPair) -> Self {
478-
unsafe {
479-
let mut pk = ffi::PublicKey::new();
480-
let ret = ffi::secp256k1_keypair_pub(
481-
secp.ctx,
482-
&mut pk,
483-
keypair.as_ptr()
484-
);
485-
debug_assert_eq!(ret, 1);
486-
::key::PublicKey(pk)
487-
}
466+
impl<'a> From<&'a KeyPair> for SecretKey {
467+
#[inline]
468+
fn from(pair: &'a KeyPair) -> Self {
469+
SecretKey::from_keypair(pair)
470+
}
471+
}
472+
473+
impl From<KeyPair> for ::key::PublicKey {
474+
#[inline]
475+
fn from(pair: KeyPair) -> Self {
476+
::key::PublicKey::from_keypair(&pair)
477+
}
478+
}
479+
480+
impl<'a> From<&'a KeyPair> for ::key::PublicKey {
481+
#[inline]
482+
fn from(pair: &'a KeyPair) -> Self {
483+
::key::PublicKey::from_keypair(pair)
488484
}
489485
}
490486

@@ -734,9 +730,9 @@ mod tests {
734730
let secp = Secp256k1::new();
735731
let sk_str = "688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF";
736732
let keypair = KeyPair::from_seckey_str(&secp, sk_str).unwrap();
737-
let sk = SecretKey::from_keypair(&secp, &keypair);
733+
let sk = SecretKey::from_keypair(&keypair);
738734
assert_eq!(SecretKey::from_str(sk_str).unwrap(), sk);
739-
let pk = ::key::PublicKey::from_keypair(&secp, &keypair);
735+
let pk = ::key::PublicKey::from_keypair(&keypair);
740736
assert_eq!(::key::PublicKey::from_secret_key(&secp, &sk), pk);
741737
let xpk = PublicKey::from_keypair(&secp, &keypair);
742738
assert_eq!(PublicKey::from(pk), xpk);

0 commit comments

Comments
 (0)