Skip to content

Commit 7e26f33

Browse files
committed
key: update a couple arbitrary API functions to no longer take a context
This updates a couple functions, and their associated unit tests (which no longer need any std/alloc/global-context feature gates). This runs clean in valgrind, providing some evidence that my new code is sound.
1 parent a860dfe commit 7e26f33

File tree

3 files changed

+59
-78
lines changed

3 files changed

+59
-78
lines changed

src/key.rs

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ use crate::ThirtyTwoByteHash;
2020
#[cfg(feature = "global-context")]
2121
use crate::SECP256K1;
2222
use crate::{
23-
constants, ecdsa, from_hex, schnorr, AllPreallocated, Message, Scalar, Secp256k1, Signing,
24-
Verification,
23+
constants, ecdsa, from_hex, schnorr, Message, Scalar, Secp256k1, Signing, Verification,
2524
};
2625

2726
/// Secret key - a 256-bit key used to create ECDSA and Taproot signatures.
@@ -610,11 +609,12 @@ impl PublicKey {
610609
/// Negates the public key.
611610
#[inline]
612611
#[must_use = "you forgot to use the negated public key"]
613-
pub fn negate<C: Verification>(mut self, secp: &Secp256k1<C>) -> PublicKey {
614-
unsafe {
615-
let res = ffi::secp256k1_ec_pubkey_negate(secp.ctx.as_ptr(), &mut self.0);
616-
debug_assert_eq!(res, 1);
617-
}
612+
pub fn negate(mut self) -> PublicKey {
613+
let res = crate::with_raw_global_context(
614+
|ctx| unsafe { ffi::secp256k1_ec_pubkey_negate(ctx.as_ptr(), &mut self.0) },
615+
None,
616+
);
617+
debug_assert_eq!(res, 1);
618618
self
619619
}
620620

@@ -624,19 +624,17 @@ impl PublicKey {
624624
///
625625
/// Returns an error if the resulting key would be invalid.
626626
#[inline]
627-
pub fn add_exp_tweak<C: Verification>(
628-
mut self,
629-
secp: &Secp256k1<C>,
630-
tweak: &Scalar,
631-
) -> Result<PublicKey, Error> {
632-
unsafe {
633-
if ffi::secp256k1_ec_pubkey_tweak_add(secp.ctx.as_ptr(), &mut self.0, tweak.as_c_ptr())
634-
== 1
635-
{
636-
Ok(self)
637-
} else {
638-
Err(Error::InvalidTweak)
639-
}
627+
pub fn add_exp_tweak(mut self, tweak: &Scalar) -> Result<PublicKey, Error> {
628+
if crate::with_raw_global_context(
629+
|ctx| unsafe {
630+
ffi::secp256k1_ec_pubkey_tweak_add(ctx.as_ptr(), &mut self.0, tweak.as_c_ptr())
631+
},
632+
None,
633+
) == 1
634+
{
635+
Ok(self)
636+
} else {
637+
Err(Error::InvalidTweak)
640638
}
641639
}
642640

@@ -880,12 +878,9 @@ impl Keypair {
880878
/// or if the encoded number is an invalid scalar.
881879
#[deprecated(since = "TBD", note = "Use `from_seckey_byte_array` instead.")]
882880
#[inline]
883-
pub fn from_seckey_slice<C: Signing>(
884-
secp: &Secp256k1<C>,
885-
data: &[u8],
886-
) -> Result<Keypair, Error> {
881+
pub fn from_seckey_slice(data: &[u8]) -> Result<Keypair, Error> {
887882
match <[u8; constants::SECRET_KEY_SIZE]>::try_from(data) {
888-
Ok(data) => Self::from_seckey_byte_array(secp, data),
883+
Ok(data) => Self::from_seckey_byte_array(data),
889884
Err(_) => Err(Error::InvalidSecretKey),
890885
}
891886
}
@@ -896,13 +891,16 @@ impl Keypair {
896891
///
897892
/// [`Error::InvalidSecretKey`] if the encoded number is an invalid scalar.
898893
#[inline]
899-
pub fn from_seckey_byte_array<C: Signing>(
900-
secp: &Secp256k1<C>,
894+
pub fn from_seckey_byte_array(
901895
data: [u8; constants::SECRET_KEY_SIZE],
902896
) -> Result<Keypair, Error> {
903897
unsafe {
904898
let mut kp = ffi::Keypair::new();
905-
if ffi::secp256k1_keypair_create(secp.ctx.as_ptr(), &mut kp, data.as_c_ptr()) == 1 {
899+
if crate::with_raw_global_context(
900+
|ctx| ffi::secp256k1_keypair_create(ctx.as_ptr(), &mut kp, data.as_c_ptr()),
901+
Some(&data),
902+
) == 1
903+
{
906904
Ok(Keypair(kp))
907905
} else {
908906
Err(Error::InvalidSecretKey)
@@ -917,13 +915,8 @@ impl Keypair {
917915
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
918916
/// or if the encoded number is an invalid scalar.
919917
#[inline]
920-
pub fn from_seckey_str<C: Signing>(secp: &Secp256k1<C>, s: &str) -> Result<Keypair, Error> {
921-
let mut res = [0u8; constants::SECRET_KEY_SIZE];
922-
match from_hex(s, &mut res) {
923-
Ok(constants::SECRET_KEY_SIZE) => Keypair::from_seckey_byte_array(secp, res),
924-
_ => Err(Error::InvalidSecretKey),
925-
}
926-
}
918+
#[deprecated(note = "use FromStr or parse instead")]
919+
pub fn from_seckey_str(s: &str) -> Result<Self, Error> { s.parse() }
927920

928921
/// Creates a [`Keypair`] directly from a secret key string and the global [`SECP256K1`] context.
929922
///
@@ -932,10 +925,8 @@ impl Keypair {
932925
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
933926
/// or if the encoded number is an invalid scalar.
934927
#[inline]
935-
#[cfg(feature = "global-context")]
936-
pub fn from_seckey_str_global(s: &str) -> Result<Keypair, Error> {
937-
Keypair::from_seckey_str(SECP256K1, s)
938-
}
928+
#[deprecated(note = "use FromStr or parse instead")]
929+
pub fn from_seckey_str_global(s: &str) -> Result<Keypair, Error> { s.parse() }
939930

940931
/// Generates a new random key pair.
941932
/// # Examples
@@ -1107,10 +1098,11 @@ impl str::FromStr for Keypair {
11071098
type Err = Error;
11081099

11091100
fn from_str(s: &str) -> Result<Self, Self::Err> {
1110-
crate::with_global_context(
1111-
|secp: &Secp256k1<AllPreallocated>| Self::from_seckey_str(&secp, s),
1112-
None,
1113-
)
1101+
let mut res = [0u8; constants::SECRET_KEY_SIZE];
1102+
match from_hex(s, &mut res) {
1103+
Ok(constants::SECRET_KEY_SIZE) => Keypair::from_seckey_byte_array(res),
1104+
_ => Err(Error::InvalidSecretKey),
1105+
}
11141106
}
11151107
}
11161108

@@ -1141,12 +1133,10 @@ impl<'de> serde::Deserialize<'de> for Keypair {
11411133
"a hex string representing 32 byte Keypair",
11421134
))
11431135
} else {
1144-
let visitor = super::serde_util::Tuple32Visitor::new("raw 32 bytes Keypair", |data| {
1145-
crate::with_global_context(
1146-
|secp: &Secp256k1<AllPreallocated>| Self::from_seckey_byte_array(&secp, data),
1147-
None,
1148-
)
1149-
});
1136+
let visitor = super::serde_util::Tuple32Visitor::new(
1137+
"raw 32 bytes Keypair",
1138+
Keypair::from_seckey_byte_array,
1139+
);
11501140
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
11511141
}
11521142
}
@@ -1725,10 +1715,9 @@ mod test {
17251715
}
17261716

17271717
#[test]
1728-
#[cfg(all(feature = "std", not(secp256k1_fuzz)))]
1718+
#[cfg(not(secp256k1_fuzz))]
17291719
fn erased_keypair_is_valid() {
1730-
let s = Secp256k1::new();
1731-
let kp = Keypair::from_seckey_byte_array(&s, [1u8; constants::SECRET_KEY_SIZE])
1720+
let kp = Keypair::from_seckey_byte_array([1u8; constants::SECRET_KEY_SIZE])
17321721
.expect("valid secret key");
17331722
let mut kp2 = kp;
17341723
kp2.non_secure_erase();
@@ -2005,24 +1994,21 @@ mod test {
20051994

20061995
let tweaked_sk = sk.add_tweak(&tweak).unwrap();
20071996
assert_ne!(sk, tweaked_sk); // Make sure we did something.
2008-
let tweaked_pk = pk.add_exp_tweak(&s, &tweak).unwrap();
1997+
let tweaked_pk = pk.add_exp_tweak(&tweak).unwrap();
20091998
assert_ne!(pk, tweaked_pk);
20101999

20112000
assert_eq!(PublicKey::from_secret_key(&s, &tweaked_sk), tweaked_pk);
20122001
}
20132002

20142003
#[test]
2015-
#[cfg(feature = "std")]
20162004
fn tweak_add_zero() {
2017-
let s = Secp256k1::new();
2018-
20192005
let (sk, pk) = crate::test_random_keypair();
20202006

20212007
let tweak = Scalar::ZERO;
20222008

20232009
let tweaked_sk = sk.add_tweak(&tweak).unwrap();
20242010
assert_eq!(sk, tweaked_sk); // Tweak by zero does nothing.
2025-
let tweaked_pk = pk.add_exp_tweak(&s, &tweak).unwrap();
2011+
let tweaked_pk = pk.add_exp_tweak(&tweak).unwrap();
20262012
assert_eq!(pk, tweaked_pk);
20272013
}
20282014

@@ -2067,9 +2053,9 @@ mod test {
20672053
let back_sk = neg.negate();
20682054
assert_eq!(sk, back_sk);
20692055

2070-
let neg = pk.negate(&s);
2056+
let neg = pk.negate();
20712057
assert_ne!(pk, neg);
2072-
let back_pk = neg.negate(&s);
2058+
let back_pk = neg.negate();
20732059
assert_eq!(pk, back_pk);
20742060

20752061
assert_eq!(PublicKey::from_secret_key(&s, &back_sk), pk);
@@ -2327,7 +2313,7 @@ mod test {
23272313
];
23282314
static SK_STR: &str = "01010101010101010001020304050607ffff0000ffff00006363636363636363";
23292315

2330-
let sk = Keypair::from_seckey_byte_array(SECP256K1, SK_BYTES).unwrap();
2316+
let sk = Keypair::from_seckey_byte_array(SK_BYTES).unwrap();
23312317
#[rustfmt::skip]
23322318
assert_tokens(&sk.compact(), &[
23332319
Token::Tuple{ len: 32 },
@@ -2507,7 +2493,7 @@ mod test {
25072493

25082494
static PK_STR: &str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166";
25092495

2510-
let kp = Keypair::from_seckey_byte_array(crate::SECP256K1, SK_BYTES).unwrap();
2496+
let kp = Keypair::from_seckey_byte_array(SK_BYTES).unwrap();
25112497
let (pk, _parity) = XOnlyPublicKey::from_keypair(&kp);
25122498

25132499
#[rustfmt::skip]
@@ -2535,11 +2521,10 @@ mod test {
25352521
}
25362522

25372523
#[test]
2538-
#[cfg(all(any(feature = "alloc", feature = "global-context"), feature = "serde"))]
2524+
#[cfg(feature = "serde")]
25392525
fn test_keypair_deserialize_serde() {
2540-
let ctx = crate::Secp256k1::new();
25412526
let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242";
2542-
let keypair = Keypair::from_seckey_str(&ctx, sec_key_str).unwrap();
2527+
let keypair = Keypair::from_str(sec_key_str).unwrap();
25432528

25442529
serde_test::assert_tokens(&keypair.readable(), &[Token::String(sec_key_str)]);
25452530

src/schnorr.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,9 @@ mod tests {
279279
let secp = Secp256k1::new();
280280

281281
let msg = hex_32!("E48441762FB75010B2AA31A512B62B4148AA3FB08EB0765D76B252559064A614");
282-
let sk = Keypair::from_seckey_str(
283-
&secp,
284-
"688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF",
285-
)
286-
.unwrap();
282+
let sk =
283+
Keypair::from_str("688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF")
284+
.unwrap();
287285
let aux_rand: [u8; 32] =
288286
hex_32!("02CCE08E913F22A36C5648D6405A2C7C50106E7AA2F1649E381C7F09D16B80AB");
289287
let expected_sig = Signature::from_str("6470FD1303DDA4FDA717B9837153C24A6EAB377183FC438F939E0ED2B620E9EE5077C4A8B8DCA28963D772A94F5F0DDF598E1C47C137F91933274C7C3EDADCE8").unwrap();
@@ -376,7 +374,7 @@ mod tests {
376374
fn test_xonly_key_extraction() {
377375
let secp = Secp256k1::new();
378376
let sk_str = "688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF";
379-
let keypair = Keypair::from_seckey_str(&secp, sk_str).unwrap();
377+
let keypair = Keypair::from_str(sk_str).unwrap();
380378
let sk = SecretKey::from_keypair(&keypair);
381379
assert_eq!(SecretKey::from_str(sk_str).unwrap(), sk);
382380
let pk = crate::key::PublicKey::from_keypair(&keypair);
@@ -390,14 +388,13 @@ mod tests {
390388
fn test_pubkey_display_output() {
391389
#[cfg(not(secp256k1_fuzz))]
392390
let pk = {
393-
let secp = Secp256k1::new();
394391
static SK_BYTES: [u8; 32] = [
395392
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
396393
0x06, 0x07, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x63, 0x63, 0x63, 0x63,
397394
0x63, 0x63, 0x63, 0x63,
398395
];
399396

400-
let kp = Keypair::from_seckey_byte_array(&secp, SK_BYTES).expect("sk");
397+
let kp = Keypair::from_seckey_byte_array(SK_BYTES).expect("sk");
401398

402399
// In fuzzing mode secret->public key derivation is different, so
403400
// hard-code the expected result.
@@ -477,7 +474,7 @@ mod tests {
477474
let s = Secp256k1::new();
478475

479476
let msg = [1; 32];
480-
let keypair = Keypair::from_seckey_byte_array(&s, [2; 32]).unwrap();
477+
let keypair = Keypair::from_seckey_byte_array([2; 32]).unwrap();
481478
let aux = [3u8; 32];
482479
let sig = s.sign_schnorr_with_aux_rand(&msg, &keypair, &aux);
483480
static SIG_BYTES: [u8; constants::SCHNORR_SIGNATURE_SIZE] = [
@@ -710,7 +707,7 @@ mod tests {
710707
} in vectors
711708
{
712709
if let (Some(secret_key), Some(aux_rand)) = (secret_key, aux_rand) {
713-
let keypair = Keypair::from_seckey_byte_array(&secp, secret_key).unwrap();
710+
let keypair = Keypair::from_seckey_byte_array(secret_key).unwrap();
714711
assert_eq!(keypair.x_only_public_key().0.serialize(), public_key);
715712
let sig = secp.sign_schnorr_with_aux_rand(&message, &keypair, &aux_rand);
716713
assert_eq!(sig.to_byte_array(), signature);

tests/serde.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ extern crate bincode;
44
extern crate secp256k1;
55
extern crate serde_cbor;
66

7-
use secp256k1::{musig, PublicKey, SecretKey, XOnlyPublicKey};
87
#[cfg(feature = "global-context")]
9-
use secp256k1::{Keypair, Secp256k1};
8+
use secp256k1::Keypair;
9+
use secp256k1::{musig, PublicKey, SecretKey, XOnlyPublicKey};
1010

1111
// Arbitrary key data.
1212

@@ -97,8 +97,7 @@ fn bincode_public_key() {
9797
#[test]
9898
#[cfg(feature = "global-context")]
9999
fn bincode_keypair() {
100-
let secp = Secp256k1::new();
101-
let kp = Keypair::from_seckey_byte_array(&secp, SK_BYTES).expect("failed to create keypair");
100+
let kp = Keypair::from_seckey_byte_array(SK_BYTES).expect("failed to create keypair");
102101
let ser = bincode::serialize(&kp).unwrap();
103102

104103
assert_eq!(ser, SK_BYTES);

0 commit comments

Comments
 (0)