From fa25c7e326afa3ac05e517b49f50cd7e0c36e4f1 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 1 Nov 2022 19:53:46 -0600 Subject: [PATCH] ecdsa: have `SigningKey::verifying_key` return a reference The `SigningKey` is internally a keypair, so callers can borrow the `VerifyingKey`, rather than making a copy. --- ecdsa/src/sign.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ecdsa/src/sign.rs b/ecdsa/src/sign.rs index 5082d76a..08269036 100644 --- a/ecdsa/src/sign.rs +++ b/ecdsa/src/sign.rs @@ -92,12 +92,11 @@ where &self.secret_scalar } - /// Get the [`VerifyingKey`] which corresponds to this [`SigningKey`] - // TODO(tarcieri): make this return `&VerifyingKey` in the next breaking release + /// Get the [`VerifyingKey`] which corresponds to this [`SigningKey`]. #[cfg(feature = "verify")] #[cfg_attr(docsrs, doc(cfg(feature = "verify")))] - pub fn verifying_key(&self) -> VerifyingKey { - self.verifying_key + pub fn verifying_key(&self) -> &VerifyingKey { + &self.verifying_key } } @@ -350,6 +349,18 @@ where } } +#[cfg(feature = "verify")] +impl From> for VerifyingKey +where + C: PrimeCurve + ProjectiveArithmetic, + Scalar: Invert>> + Reduce + SignPrimitive, + SignatureSize: ArrayLength, +{ + fn from(signing_key: SigningKey) -> VerifyingKey { + signing_key.verifying_key + } +} + #[cfg(feature = "verify")] impl From<&SigningKey> for VerifyingKey where @@ -358,7 +369,7 @@ where SignatureSize: ArrayLength, { fn from(signing_key: &SigningKey) -> VerifyingKey { - signing_key.verifying_key() + signing_key.verifying_key } }