Skip to content

Commit 792103b

Browse files
committed
Remove context from PublicKey::from_secret_key
We are removing the context from APIs, bit by bit.
1 parent 95e2e25 commit 792103b

File tree

9 files changed

+60
-78
lines changed

9 files changed

+60
-78
lines changed

examples/generate_keys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ fn main() {
88
// First option:
99
let (seckey, pubkey) = secp.generate_keypair(&mut rng);
1010

11-
assert_eq!(pubkey, PublicKey::from_secret_key(&secp, &seckey));
11+
assert_eq!(pubkey, PublicKey::from_secret_key(&seckey));
1212

1313
// Second option:
1414
let seckey = SecretKey::new(&mut rng);
15-
let _pubkey = PublicKey::from_secret_key(&secp, &seckey);
15+
let _pubkey = PublicKey::from_secret_key(&seckey);
1616
}

examples/musig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313
let (seckey1, pubkey1) = secp.generate_keypair(&mut rng);
1414

1515
let seckey2 = SecretKey::new(&mut rng);
16-
let pubkey2 = PublicKey::from_secret_key(&secp, &seckey2);
16+
let pubkey2 = PublicKey::from_secret_key(&seckey2);
1717

1818
let pubkeys = [pubkey1, pubkey2];
1919
let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();

no_std_test/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
7979
let mut secp = Secp256k1::preallocated_new(&mut buf).unwrap();
8080
secp.randomize(&mut FakeRng);
8181
let secret_key = SecretKey::new(&mut FakeRng);
82-
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
82+
let public_key = PublicKey::from_secret_key(&secret_key);
8383
let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
8484

8585
let sig = secp.sign_ecdsa(message, &secret_key);
@@ -106,7 +106,7 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
106106
#[cfg(feature = "alloc")]
107107
{
108108
let secp_alloc = Secp256k1::new();
109-
let public_key = PublicKey::from_secret_key(&secp_alloc, &secret_key);
109+
let public_key = PublicKey::from_secret_key(&secret_key);
110110
let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
111111

112112
let sig = secp_alloc.sign_ecdsa(message, &secret_key);

src/ellswift.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,9 @@ impl ElligatorSwift {
138138
/// # Example
139139
/// ```
140140
/// # #[cfg(feature = "alloc")] {
141-
/// use secp256k1::{ellswift::ElligatorSwift, PublicKey, Secp256k1, SecretKey};
142-
/// let secp = Secp256k1::new();
141+
/// use secp256k1::{ellswift::ElligatorSwift, PublicKey, SecretKey};
143142
/// let sk = SecretKey::from_secret_bytes([1; 32]).unwrap();
144-
/// let pk = PublicKey::from_secret_key(&secp, &sk);
143+
/// let pk = PublicKey::from_secret_key(&sk);
145144
/// let es = ElligatorSwift::from_pubkey(pk);
146145
/// # }
147146
///
@@ -375,9 +374,8 @@ mod tests {
375374
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
376375
fn test_elligator_swift_rtt() {
377376
// Test that we can round trip an ElligatorSwift encoding
378-
let secp = crate::Secp256k1::new();
379377
let public_key =
380-
PublicKey::from_secret_key(&secp, &SecretKey::from_secret_bytes([1u8; 32]).unwrap());
378+
PublicKey::from_secret_key(&SecretKey::from_secret_bytes([1u8; 32]).unwrap());
381379

382380
let ell = ElligatorSwift::from_pubkey(public_key);
383381
let pk = PublicKey::from_ellswift(ell);
@@ -393,8 +391,7 @@ mod tests {
393391
let ell =
394392
ElligatorSwift::from_seckey(&secp, SecretKey::from_secret_bytes(rand32).unwrap(), None);
395393
let pk = PublicKey::from_ellswift(ell);
396-
let expected =
397-
PublicKey::from_secret_key(&secp, &SecretKey::from_secret_bytes(priv32).unwrap());
394+
let expected = PublicKey::from_secret_key(&SecretKey::from_secret_bytes(priv32).unwrap());
398395

399396
assert_eq!(pk, expected);
400397
}

src/key/mod.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ use crate::{
3939
///
4040
/// ```
4141
/// # #[cfg(feature = "alloc")] {
42-
/// use secp256k1::{SecretKey, Secp256k1, PublicKey};
42+
/// use secp256k1::{SecretKey, PublicKey};
4343
///
44-
/// let secp = Secp256k1::new();
4544
/// let secret_key = SecretKey::from_byte_array([0xcd; 32]).expect("32 bytes, within curve order");
46-
/// let public_key = PublicKey::from_secret_key(&secp, &secret_key);
45+
/// let public_key = PublicKey::from_secret_key(&secret_key);
4746
/// # }
4847
/// ```
4948
/// [`bincode`]: https://docs.rs/bincode
@@ -108,20 +107,24 @@ impl PublicKey {
108107
///
109108
/// ```
110109
/// # #[cfg(all(feature = "rand", feature = "std"))] {
111-
/// use secp256k1::{rand, Secp256k1, SecretKey, PublicKey};
110+
/// use secp256k1::{rand, SecretKey, PublicKey};
112111
///
113-
/// let secp = Secp256k1::new();
114112
/// let secret_key = SecretKey::new(&mut rand::rng());
115-
/// let public_key = PublicKey::from_secret_key(&secp, &secret_key);
113+
/// let public_key = PublicKey::from_secret_key(&secret_key);
116114
/// # }
117115
/// ```
118116
#[inline]
119-
pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>, sk: &SecretKey) -> PublicKey {
117+
pub fn from_secret_key(sk: &SecretKey) -> PublicKey {
120118
unsafe {
121119
let mut pk = ffi::PublicKey::new();
122120
// We can assume the return value because it's not possible to construct
123121
// an invalid `SecretKey` without transmute trickery or something.
124-
let res = ffi::secp256k1_ec_pubkey_create(secp.ctx.as_ptr(), &mut pk, sk.as_c_ptr());
122+
let res = crate::with_global_context(
123+
|secp: &Secp256k1<crate::AllPreallocated>| {
124+
ffi::secp256k1_ec_pubkey_create(secp.ctx.as_ptr(), &mut pk, sk.as_c_ptr())
125+
},
126+
Some(&sk.to_secret_bytes()),
127+
);
125128
debug_assert_eq!(res, 1);
126129
PublicKey(pk)
127130
}
@@ -130,12 +133,11 @@ impl PublicKey {
130133
#[inline]
131134
pub fn from_ellswift(ellswift: ElligatorSwift) -> PublicKey { ElligatorSwift::decode(ellswift) }
132135

133-
/// Creates a new public key from a [`SecretKey`] and the global [`SECP256K1`] context.
136+
/// Creates a new public key from a [`SecretKey`].
134137
#[inline]
135138
#[cfg(feature = "global-context")]
136-
pub fn from_secret_key_global(sk: &SecretKey) -> PublicKey {
137-
PublicKey::from_secret_key(SECP256K1, sk)
138-
}
139+
#[deprecated(since = "TBD", note = "use from_secret_key instead")]
140+
pub fn from_secret_key_global(sk: &SecretKey) -> PublicKey { PublicKey::from_secret_key(sk) }
139141

140142
/// Creates a public key directly from a slice.
141143
#[inline]
@@ -1296,9 +1298,9 @@ impl<C: Verification> Secp256k1<C> {
12961298
/// # use secp256k1::{Secp256k1, SecretKey, Keypair, PublicKey, pubkey_sort};
12971299
/// # let secp = Secp256k1::new();
12981300
/// # let sk1 = SecretKey::new(&mut rng());
1299-
/// # let pub_key1 = PublicKey::from_secret_key(&secp, &sk1);
1301+
/// # let pub_key1 = PublicKey::from_secret_key(&sk1);
13001302
/// # let sk2 = SecretKey::new(&mut rng());
1301-
/// # let pub_key2 = PublicKey::from_secret_key(&secp, &sk2);
1303+
/// # let pub_key2 = PublicKey::from_secret_key(&sk2);
13021304
/// #
13031305
/// # let pubkeys = [pub_key1, pub_key2];
13041306
/// # let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
@@ -1542,14 +1544,12 @@ mod test {
15421544
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
15431545
];
15441546

1545-
#[cfg(not(secp256k1_fuzz))]
1546-
let s = Secp256k1::signing_only();
15471547
let sk = SecretKey::from_secret_bytes(SK_BYTES).expect("sk");
15481548

15491549
// In fuzzing mode secret->public key derivation is different, so
15501550
// hard-code the expected result.
15511551
#[cfg(not(secp256k1_fuzz))]
1552-
let pk = PublicKey::from_secret_key(&s, &sk);
1552+
let pk = PublicKey::from_secret_key(&sk);
15531553
#[cfg(secp256k1_fuzz)]
15541554
let pk = PublicKey::from_slice(&[
15551555
0x02, 0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30,
@@ -1661,10 +1661,8 @@ mod test {
16611661
#[test]
16621662
#[cfg(feature = "std")]
16631663
fn tweak_add_arbitrary_data() {
1664-
let s = Secp256k1::new();
1665-
16661664
let (sk, pk) = crate::test_random_keypair();
1667-
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk); // Sanity check.
1665+
assert_eq!(PublicKey::from_secret_key(&sk), pk); // Sanity check.
16681666

16691667
// TODO: This would be better tested with a _lot_ of different tweaks.
16701668
let tweak = Scalar::test_random();
@@ -1674,7 +1672,7 @@ mod test {
16741672
let tweaked_pk = pk.add_exp_tweak(&tweak).unwrap();
16751673
assert_ne!(pk, tweaked_pk);
16761674

1677-
assert_eq!(PublicKey::from_secret_key(&s, &tweaked_sk), tweaked_pk);
1675+
assert_eq!(PublicKey::from_secret_key(&tweaked_sk), tweaked_pk);
16781676
}
16791677

16801678
#[test]
@@ -1695,7 +1693,7 @@ mod test {
16951693
let s = Secp256k1::new();
16961694

16971695
let (sk, pk) = crate::test_random_keypair();
1698-
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk); // Sanity check.
1696+
assert_eq!(PublicKey::from_secret_key(&sk), pk); // Sanity check.
16991697

17001698
for _ in 0..10 {
17011699
let tweak = Scalar::test_random();
@@ -1704,7 +1702,7 @@ mod test {
17041702
assert_ne!(sk, tweaked_sk); // Make sure we did something.
17051703
let tweaked_pk = pk.mul_tweak(&s, &tweak).unwrap();
17061704
assert_ne!(pk, tweaked_pk);
1707-
assert_eq!(PublicKey::from_secret_key(&s, &tweaked_sk), tweaked_pk);
1705+
assert_eq!(PublicKey::from_secret_key(&tweaked_sk), tweaked_pk);
17081706
}
17091707
}
17101708

@@ -1719,11 +1717,9 @@ mod test {
17191717
#[test]
17201718
#[cfg(feature = "std")]
17211719
fn test_negation() {
1722-
let s = Secp256k1::new();
1723-
17241720
let (sk, pk) = crate::test_random_keypair();
17251721

1726-
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk); // Sanity check.
1722+
assert_eq!(PublicKey::from_secret_key(&sk), pk); // Sanity check.
17271723

17281724
let neg = sk.negate();
17291725
assert_ne!(sk, neg);
@@ -1735,7 +1731,7 @@ mod test {
17351731
let back_pk = neg.negate();
17361732
assert_eq!(pk, back_pk);
17371733

1738-
assert_eq!(PublicKey::from_secret_key(&s, &back_sk), pk);
1734+
assert_eq!(PublicKey::from_secret_key(&back_sk), pk);
17391735
}
17401736

17411737
#[test]
@@ -1823,8 +1819,6 @@ mod test {
18231819
#[test]
18241820
#[cfg(feature = "std")]
18251821
fn create_pubkey_combine() {
1826-
let s = Secp256k1::new();
1827-
18281822
let (sk1, pk1) = crate::test_random_keypair();
18291823
let (sk2, pk2) = crate::test_random_keypair();
18301824

@@ -1835,7 +1829,7 @@ mod test {
18351829
assert_eq!(sum1, sum2);
18361830

18371831
let tweaked = sk1.add_tweak(&Scalar::from(sk2)).unwrap();
1838-
let sksum = PublicKey::from_secret_key(&s, &tweaked);
1832+
let sksum = PublicKey::from_secret_key(&tweaked);
18391833
assert_eq!(Ok(sksum), sum1);
18401834
}
18411835

@@ -1889,14 +1883,12 @@ mod test {
18891883
];
18901884
static PK_STR: &str = "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166";
18911885

1892-
#[cfg(not(secp256k1_fuzz))]
1893-
let s = Secp256k1::new();
18941886
let sk = SecretKey::from_secret_bytes(SK_BYTES).unwrap();
18951887

18961888
// In fuzzing mode secret->public key derivation is different, so
18971889
// hard-code the expected result.
18981890
#[cfg(not(secp256k1_fuzz))]
1899-
let pk = PublicKey::from_secret_key(&s, &sk);
1891+
let pk = PublicKey::from_secret_key(&sk);
19001892
#[cfg(secp256k1_fuzz)]
19011893
let pk = PublicKey::from_slice(&PK_BYTES).expect("pk");
19021894

@@ -2052,10 +2044,8 @@ mod test {
20522044
#[test]
20532045
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
20542046
fn convert_secret_key_to_public_key() {
2055-
let secp = Secp256k1::new();
2056-
20572047
let (sk, want, _kp, _xonly) = keys();
2058-
let got = sk.public_key(&secp);
2048+
let got = sk.public_key();
20592049

20602050
assert_eq!(got, want)
20612051
}

src/key/secret.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,7 @@ impl SecretKey {
294294
///
295295
/// This is equivalent to using [`PublicKey::from_secret_key`].
296296
#[inline]
297-
pub fn public_key<C: Signing>(&self, secp: &Secp256k1<C>) -> PublicKey {
298-
PublicKey::from_secret_key(secp, self)
299-
}
297+
pub fn public_key(&self) -> PublicKey { PublicKey::from_secret_key(self) }
300298

301299
/// Returns the [`XOnlyPublicKey`] (and its [`Parity`]) for this [`SecretKey`].
302300
///

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
//!
8282
//! let secp = Secp256k1::new();
8383
//! let secret_key = SecretKey::from_secret_bytes([0xcd; 32]).expect("32 bytes, within curve order");
84-
//! let public_key = PublicKey::from_secret_key(&secp, &secret_key);
84+
//! let public_key = PublicKey::from_secret_key(&secret_key);
8585
//! // If the supplied byte slice was *not* the output of a cryptographic hash function this would
8686
//! // be cryptographically broken. It has been trivially used in the past to execute attacks.
8787
//! let message = Message::from_digest(compute_hash(b"CSW is not Satoshi"));
@@ -452,12 +452,13 @@ impl<C: Signing> Secp256k1<C> {
452452
/// [`PublicKey::from_secret_key`].
453453
#[inline]
454454
#[cfg(feature = "rand")]
455+
// TODO: Move this somewhere more meaningful now we don't use the context.
455456
pub fn generate_keypair<R: rand::Rng + ?Sized>(
456457
&self,
457458
rng: &mut R,
458459
) -> (key::SecretKey, key::PublicKey) {
459460
let sk = key::SecretKey::new(rng);
460-
let pk = key::PublicKey::from_secret_key(self, &sk);
461+
let pk = key::PublicKey::from_secret_key(&sk);
461462
(sk, pk)
462463
}
463464
}
@@ -480,10 +481,7 @@ fn test_random_keypair() -> (key::SecretKey, key::PublicKey) { generate_keypair(
480481
#[cfg(not(all(feature = "global-context", feature = "rand", feature = "std")))]
481482
fn test_random_keypair() -> (key::SecretKey, key::PublicKey) {
482483
let sk = SecretKey::test_random();
483-
let pk = with_global_context(
484-
|secp: &Secp256k1<AllPreallocated>| key::PublicKey::from_secret_key(secp, &sk),
485-
Some(&[0xab; 32]),
486-
);
484+
let pk = key::PublicKey::from_secret_key(&sk);
487485
(sk, pk)
488486
}
489487

@@ -850,7 +848,7 @@ mod tests {
850848
let sig = s.sign_ecdsa(msg, &key);
851849
let low_r_sig = s.sign_ecdsa_low_r(msg, &key);
852850
let grind_r_sig = s.sign_ecdsa_grind_r(msg, &key, 1);
853-
let pk = PublicKey::from_secret_key(&s, &key);
851+
let pk = PublicKey::from_secret_key(&key);
854852
assert_eq!(s.verify_ecdsa(&sig, msg, &pk), Ok(()));
855853
assert_eq!(s.verify_ecdsa(&low_r_sig, msg, &pk), Ok(()));
856854
assert_eq!(s.verify_ecdsa(&grind_r_sig, msg, &pk), Ok(()));
@@ -1049,7 +1047,7 @@ mod tests {
10491047
let msg = Message::from_digest(msg_data);
10501048

10511049
// Check usage as explicit parameter
1052-
let pk = PublicKey::from_secret_key(SECP256K1, &sk);
1050+
let pk = PublicKey::from_secret_key(&sk);
10531051

10541052
// Check usage as self
10551053
let sig = SECP256K1.sign_ecdsa(msg, &sk);

0 commit comments

Comments
 (0)