From ea5106e0cd0f47a5ee11af0d7313f1f1dc0a5a9f Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 2 Dec 2020 18:06:04 -0800 Subject: [PATCH] elliptic-curve: simplify PublicKey trait bounds (#378) Decouples PublicKey from SEC1 encoding-related concerns, which allows the trait bounds for anything which interacts with PublicKeys to be dramatically simplified. --- elliptic-curve/src/ecdh.rs | 20 ++---------- elliptic-curve/src/public_key.rs | 53 ++++++++++++-------------------- elliptic-curve/src/secret_key.rs | 11 ++----- 3 files changed, 25 insertions(+), 59 deletions(-) diff --git a/elliptic-curve/src/ecdh.rs b/elliptic-curve/src/ecdh.rs index bf83d991c..252d9cd5e 100644 --- a/elliptic-curve/src/ecdh.rs +++ b/elliptic-curve/src/ecdh.rs @@ -24,9 +24,7 @@ use crate::{ consts::U1, public_key::PublicKey, scalar::NonZeroScalar, - sec1::{ - EncodedPoint, FromEncodedPoint, ToEncodedPoint, UncompressedPointSize, UntaggedPointSize, - }, + sec1::{EncodedPoint, ToEncodedPoint, UncompressedPointSize, UntaggedPointSize}, weierstrass::Curve, AffinePoint, FieldBytes, ProjectiveArithmetic, ProjectivePoint, Scalar, }; @@ -55,13 +53,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField> + Clone + Zeroize, - AffinePoint: Clone - + Debug - + Default - + Into> - + FromEncodedPoint - + ToEncodedPoint - + Zeroize, + AffinePoint: Copy + Clone + Debug + ToEncodedPoint + Zeroize, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, @@ -94,13 +86,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField> + Clone + Zeroize, - AffinePoint: Clone - + Debug - + Default - + Into> - + FromEncodedPoint - + ToEncodedPoint - + Zeroize, + AffinePoint: Copy + Clone + Debug + ToEncodedPoint + Zeroize, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, diff --git a/elliptic-curve/src/public_key.rs b/elliptic-curve/src/public_key.rs index 609ed6571..6e0e35bce 100644 --- a/elliptic-curve/src/public_key.rs +++ b/elliptic-curve/src/public_key.rs @@ -20,16 +20,13 @@ use generic_array::ArrayLength; /// /// These are a thin wrapper around [`AffinePoint`] which simplifies /// encoding/decoding. -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] pub struct PublicKey where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, - ProjectivePoint: From>, - UntaggedPointSize: Add + ArrayLength, - UncompressedPointSize: ArrayLength, + AffinePoint: Copy + Clone + Debug, { point: AffinePoint, } @@ -39,10 +36,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, - ProjectivePoint: From>, - UntaggedPointSize: Add + ArrayLength, - UncompressedPointSize: ArrayLength, + AffinePoint: Copy + Clone + Debug, { /// Convert an [`AffinePoint`] into a [`PublicKey`] pub fn from_affine(point: AffinePoint) -> Self { @@ -55,7 +49,12 @@ where /// 2.3.3 (page 10). /// /// - pub fn from_sec1_bytes(bytes: &[u8]) -> Result { + pub fn from_sec1_bytes(bytes: &[u8]) -> Result + where + Self: TryFrom, Error = Error>, + UntaggedPointSize: Add + ArrayLength, + UncompressedPointSize: ArrayLength, + { EncodedPoint::from_bytes(bytes) .map_err(|_| Error) .and_then(TryInto::try_into) @@ -69,7 +68,10 @@ where } /// Convert this [`PublicKey`] to a [`ProjectivePoint`] for the given curve - pub fn to_projective(&self) -> ProjectivePoint { + pub fn to_projective(&self) -> ProjectivePoint + where + ProjectivePoint: From>, + { self.point.clone().into() } } @@ -79,10 +81,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, - ProjectivePoint: From>, - UntaggedPointSize: Add + ArrayLength, - UncompressedPointSize: ArrayLength, + AffinePoint: Copy + Clone + Debug, { fn as_ref(&self) -> &AffinePoint { self.as_affine() @@ -94,7 +93,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, + AffinePoint: Copy + Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, @@ -111,7 +110,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, + AffinePoint: Copy + Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, @@ -128,7 +127,7 @@ where C: Curve + ProjectiveArithmetic + point::Compression, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, + AffinePoint: Copy + Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, @@ -143,7 +142,7 @@ where C: Curve + ProjectiveArithmetic + point::Compression, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, + AffinePoint: Copy + Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, @@ -158,7 +157,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, + AffinePoint: Copy + Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, @@ -174,7 +173,7 @@ where C: Curve + ProjectiveArithmetic, FieldBytes: From> + for<'r> From<&'r Scalar>, Scalar: PrimeField>, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, + AffinePoint: Copy + Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, ProjectivePoint: From>, UntaggedPointSize: Add + ArrayLength, UncompressedPointSize: ArrayLength, @@ -186,18 +185,6 @@ where } } -impl Copy for PublicKey -where - C: Curve + ProjectiveArithmetic, - FieldBytes: From> + for<'r> From<&'r Scalar>, - Scalar: PrimeField>, - AffinePoint: Copy + Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, - ProjectivePoint: From>, - UntaggedPointSize: Add + ArrayLength, - UncompressedPointSize: ArrayLength, -{ -} - impl Eq for PublicKey where C: Curve + ProjectiveArithmetic, diff --git a/elliptic-curve/src/secret_key.rs b/elliptic-curve/src/secret_key.rs index e05e4d22c..48e74e107 100644 --- a/elliptic-curve/src/secret_key.rs +++ b/elliptic-curve/src/secret_key.rs @@ -17,16 +17,12 @@ use zeroize::Zeroize; #[cfg(feature = "arithmetic")] use crate::{ - consts::U1, ff::PrimeField, - generic_array::ArrayLength, group::{Curve as _, Group}, - ops::Add, public_key::PublicKey, rand_core::{CryptoRng, RngCore}, scalar::{NonZeroScalar, Scalar}, - sec1::{FromEncodedPoint, ToEncodedPoint, UncompressedPointSize, UntaggedPointSize}, - weierstrass, AffinePoint, ProjectiveArithmetic, ProjectivePoint, + weierstrass, AffinePoint, ProjectiveArithmetic, }; /// Inner value stored by a [`SecretKey`]. @@ -157,10 +153,7 @@ where C: weierstrass::Curve + ProjectiveArithmetic + SecretValue>, FieldBytes: From> + for<'a> From<&'a Scalar>, Scalar: PrimeField> + Zeroize, - AffinePoint: Clone + Debug + Default + FromEncodedPoint + ToEncodedPoint, - ProjectivePoint: From>, - UntaggedPointSize: Add + ArrayLength, - UncompressedPointSize: ArrayLength, + AffinePoint: Copy + Clone + Debug + Default, { PublicKey::from_affine((C::ProjectivePoint::generator() * self.secret_scalar()).to_affine()) }