Merge rust-bitcoin/rust-secp256k1#372: Deprecate generate_schnorrsig_keypair method

97524b2da7 Deprecate generate_schnorrsig_keypair (Tobin Harding)
389abddcc7 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 97524b2da7

Tree-SHA512: a10255d04b86c0031d5cfe4b6357224bc7bcd31c7e278d28af414a34ba4f158dd05d712c4878dfdc327ff8cb42b4421cc0a4b2605c6781691a3158b237fda2d3
This commit is contained in:
Andrew Poelstra 2022-01-12 18:13:49 +00:00
commit 83e3372edb
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 32 additions and 19 deletions

View File

@ -650,6 +650,12 @@ impl KeyPair {
Ok(())
}
}
/// Gets the [XOnlyPublicKey] for this [KeyPair].
#[inline]
pub fn public_key(&self) -> XOnlyPublicKey {
XOnlyPublicKey::from_keypair(self)
}
}
impl From<KeyPair> for SecretKey {
@ -1547,7 +1553,10 @@ mod test {
for _ in 0..10 {
let mut tweak = [0u8; 32];
thread_rng().fill_bytes(&mut tweak);
let (mut kp, mut pk) = s.generate_schnorrsig_keypair(&mut thread_rng());
let mut kp = KeyPair::new(&s, &mut thread_rng());
let mut pk = kp.public_key();
let orig_pk = pk;
kp.tweak_add_assign(&s, &tweak).expect("Tweak error");
let parity = pk.tweak_add_assign(&s, &tweak).expect("Tweak error");

View File

@ -248,15 +248,14 @@ impl<C: Verification> Secp256k1<C> {
}
impl <C: Signing> Secp256k1<C> {
/// Generates a random Schnorr KeyPair and its associated Schnorr PublicKey.
/// Convenience function for `schnorrsig::KeyPair::new` and
/// `schnorrsig::PublicKey::from_keypair`; call those functions directly for
/// batch key generation. Requires a signing-capable context. Requires compilation
/// with the "rand" feature.
/// Generates a random Schnorr `KeyPair` and its associated Schnorr `XOnlyPublicKey`.
///
/// Convenience function for [KeyPair::new] and [KeyPair::public_key].
/// Requires a signing-capable context and requires compilation with the "rand" feature.
#[inline]
#[cfg(any(test, feature = "rand"))]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
#[deprecated(since = "0.21.0", note = "Use kp = KeyPair::new() and kp.public_key()")]
pub fn generate_schnorrsig_keypair<R: Rng + ?Sized>(
&self,
rng: &mut R,
@ -325,16 +324,18 @@ mod tests {
let secp = Secp256k1::new();
let mut rng = thread_rng();
let (seckey, pubkey) = secp.generate_schnorrsig_keypair(&mut rng);
let kp = KeyPair::new(&secp, &mut rng);
let pk = kp.public_key();
let mut msg = [0u8; 32];
for _ in 0..100 {
rng.fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let sig = sign(&secp, &msg, &seckey, &mut rng);
let sig = sign(&secp, &msg, &kp, &mut rng);
assert!(secp.verify_schnorr(&sig, &msg, &pubkey).is_ok());
assert!(secp.verify_schnorr(&sig, &msg, &pk).is_ok());
}
}
@ -390,10 +391,12 @@ mod tests {
#[test]
fn test_pubkey_serialize_roundtrip() {
let secp = Secp256k1::new();
let (_, pubkey) = secp.generate_schnorrsig_keypair(&mut thread_rng());
let ser = pubkey.serialize();
let kp = KeyPair::new(&secp, &mut thread_rng());
let pk = kp.public_key();
let ser = pk.serialize();
let pubkey2 = XOnlyPublicKey::from_slice(&ser).unwrap();
assert_eq!(pubkey, pubkey2);
assert_eq!(pk, pubkey2);
}
#[test]
@ -405,7 +408,7 @@ mod tests {
assert_eq!(SecretKey::from_str(sk_str).unwrap(), sk);
let pk = ::key::PublicKey::from_keypair(&keypair);
assert_eq!(::key::PublicKey::from_secret_key(&secp, &sk), pk);
let xpk = XOnlyPublicKey::from_keypair(&keypair);
let xpk = keypair.public_key();
assert_eq!(XOnlyPublicKey::from(pk), xpk);
}
@ -445,12 +448,12 @@ mod tests {
0x63, 0x63, 0x63, 0x63,
];
let sk = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("sk");
let kp = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("sk");
// In fuzzing mode secret->public key derivation is different, so
// hard-code the epected result.
#[cfg(not(fuzzing))]
let pk = XOnlyPublicKey::from_keypair(&sk);
let pk = kp.public_key();
#[cfg(fuzzing)]
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");
@ -512,10 +515,11 @@ mod tests {
}
}
let s = Secp256k1::new();
let (_, pubkey) = s.generate_schnorrsig_keypair(&mut DumbRng(0));
let secp = Secp256k1::new();
let kp = KeyPair::new(&secp, &mut DumbRng(0));
let pk = kp.public_key();
assert_eq!(
&pubkey.serialize()[..],
&pk.serialize()[..],
&[
124, 121, 49, 14, 253, 63, 197, 50, 39, 194, 107, 17, 193, 219, 108, 154, 126, 9,
181, 248, 2, 12, 149, 233, 198, 71, 149, 134, 250, 184, 154, 229