Skip to content

Commit 83e3372

Browse files
committed
Merge #372: Deprecate generate_schnorrsig_keypair method
97524b2 Deprecate generate_schnorrsig_keypair (Tobin Harding) 389abdd Add method KeyPair::public_key (Tobin Harding) Pull request description: Recently we deprecated a bunch of functions/methods that used the term `schnorrsig`. Seems we left `generate_schnorrsig_keypair` in there, along with some stale docs on it. - Patch 1: Adds method `KeyPair::public_key` that calls through to `XOnlyPublicKey::from_keypair`. - Patch 2: Deprecates `generate_schnorrsig_keypair` and uses the newly defined `pk.public_key()` everywhere. ### Note to reviewers Please note, this PR has been totally re-written using the suggestions below by @apoelstra. ACKs for top commit: apoelstra: ACK 97524b2 Tree-SHA512: a10255d04b86c0031d5cfe4b6357224bc7bcd31c7e278d28af414a34ba4f158dd05d712c4878dfdc327ff8cb42b4421cc0a4b2605c6781691a3158b237fda2d3
2 parents a9cf678 + 97524b2 commit 83e3372

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/key.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ impl KeyPair {
650650
Ok(())
651651
}
652652
}
653+
654+
/// Gets the [XOnlyPublicKey] for this [KeyPair].
655+
#[inline]
656+
pub fn public_key(&self) -> XOnlyPublicKey {
657+
XOnlyPublicKey::from_keypair(self)
658+
}
653659
}
654660

655661
impl From<KeyPair> for SecretKey {
@@ -1547,7 +1553,10 @@ mod test {
15471553
for _ in 0..10 {
15481554
let mut tweak = [0u8; 32];
15491555
thread_rng().fill_bytes(&mut tweak);
1550-
let (mut kp, mut pk) = s.generate_schnorrsig_keypair(&mut thread_rng());
1556+
1557+
let mut kp = KeyPair::new(&s, &mut thread_rng());
1558+
let mut pk = kp.public_key();
1559+
15511560
let orig_pk = pk;
15521561
kp.tweak_add_assign(&s, &tweak).expect("Tweak error");
15531562
let parity = pk.tweak_add_assign(&s, &tweak).expect("Tweak error");

src/schnorr.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,14 @@ impl<C: Verification> Secp256k1<C> {
248248
}
249249

250250
impl <C: Signing> Secp256k1<C> {
251-
252-
/// Generates a random Schnorr KeyPair and its associated Schnorr PublicKey.
253-
/// Convenience function for `schnorrsig::KeyPair::new` and
254-
/// `schnorrsig::PublicKey::from_keypair`; call those functions directly for
255-
/// batch key generation. Requires a signing-capable context. Requires compilation
256-
/// with the "rand" feature.
251+
/// Generates a random Schnorr `KeyPair` and its associated Schnorr `XOnlyPublicKey`.
252+
///
253+
/// Convenience function for [KeyPair::new] and [KeyPair::public_key].
254+
/// Requires a signing-capable context and requires compilation with the "rand" feature.
257255
#[inline]
258256
#[cfg(any(test, feature = "rand"))]
259257
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
258+
#[deprecated(since = "0.21.0", note = "Use kp = KeyPair::new() and kp.public_key()")]
260259
pub fn generate_schnorrsig_keypair<R: Rng + ?Sized>(
261260
&self,
262261
rng: &mut R,
@@ -325,16 +324,18 @@ mod tests {
325324
let secp = Secp256k1::new();
326325

327326
let mut rng = thread_rng();
328-
let (seckey, pubkey) = secp.generate_schnorrsig_keypair(&mut rng);
327+
let kp = KeyPair::new(&secp, &mut rng);
328+
let pk = kp.public_key();
329+
329330
let mut msg = [0u8; 32];
330331

331332
for _ in 0..100 {
332333
rng.fill_bytes(&mut msg);
333334
let msg = Message::from_slice(&msg).unwrap();
334335

335-
let sig = sign(&secp, &msg, &seckey, &mut rng);
336+
let sig = sign(&secp, &msg, &kp, &mut rng);
336337

337-
assert!(secp.verify_schnorr(&sig, &msg, &pubkey).is_ok());
338+
assert!(secp.verify_schnorr(&sig, &msg, &pk).is_ok());
338339
}
339340
}
340341

@@ -390,10 +391,12 @@ mod tests {
390391
#[test]
391392
fn test_pubkey_serialize_roundtrip() {
392393
let secp = Secp256k1::new();
393-
let (_, pubkey) = secp.generate_schnorrsig_keypair(&mut thread_rng());
394-
let ser = pubkey.serialize();
394+
let kp = KeyPair::new(&secp, &mut thread_rng());
395+
let pk = kp.public_key();
396+
397+
let ser = pk.serialize();
395398
let pubkey2 = XOnlyPublicKey::from_slice(&ser).unwrap();
396-
assert_eq!(pubkey, pubkey2);
399+
assert_eq!(pk, pubkey2);
397400
}
398401

399402
#[test]
@@ -405,7 +408,7 @@ mod tests {
405408
assert_eq!(SecretKey::from_str(sk_str).unwrap(), sk);
406409
let pk = ::key::PublicKey::from_keypair(&keypair);
407410
assert_eq!(::key::PublicKey::from_secret_key(&secp, &sk), pk);
408-
let xpk = XOnlyPublicKey::from_keypair(&keypair);
411+
let xpk = keypair.public_key();
409412
assert_eq!(XOnlyPublicKey::from(pk), xpk);
410413
}
411414

@@ -445,12 +448,12 @@ mod tests {
445448
0x63, 0x63, 0x63, 0x63,
446449
];
447450

448-
let sk = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("sk");
451+
let kp = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("sk");
449452

450453
// In fuzzing mode secret->public key derivation is different, so
451454
// hard-code the epected result.
452455
#[cfg(not(fuzzing))]
453-
let pk = XOnlyPublicKey::from_keypair(&sk);
456+
let pk = kp.public_key();
454457
#[cfg(fuzzing)]
455458
let pk = XOnlyPublicKey::from_slice(&[0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d, 0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54, 0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66]).expect("pk");
456459

@@ -512,10 +515,11 @@ mod tests {
512515
}
513516
}
514517

515-
let s = Secp256k1::new();
516-
let (_, pubkey) = s.generate_schnorrsig_keypair(&mut DumbRng(0));
518+
let secp = Secp256k1::new();
519+
let kp = KeyPair::new(&secp, &mut DumbRng(0));
520+
let pk = kp.public_key();
517521
assert_eq!(
518-
&pubkey.serialize()[..],
522+
&pk.serialize()[..],
519523
&[
520524
124, 121, 49, 14, 253, 63, 197, 50, 39, 194, 107, 17, 193, 219, 108, 154, 126, 9,
521525
181, 248, 2, 12, 149, 233, 198, 71, 149, 134, 250, 184, 154, 229

0 commit comments

Comments
 (0)