From 921a4f9865270e64ac3c0fb05a0f6dc6ded384f4 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 13 Jan 2023 20:09:27 -0700 Subject: [PATCH] elliptic-curve: consolidate `CurveArithmetic` trait Consolidates the following former three traits into a single trait: - `AffineArithmetic` - `ProjectiveArithmetic` - `ScalarArithmetic` It doesn't make sense to impl one of these traits without impl'ing them all, so this commit combines them into a single trait. --- elliptic-curve/src/arithmetic.rs | 29 ++++------ elliptic-curve/src/dev.rs | 11 +--- elliptic-curve/src/ecdh.rs | 20 +++---- elliptic-curve/src/hash2curve/group_digest.rs | 4 +- elliptic-curve/src/jwk.rs | 16 ++--- elliptic-curve/src/lib.rs | 8 +-- elliptic-curve/src/public_key.rs | 45 +++++++------- elliptic-curve/src/scalar.rs | 4 +- elliptic-curve/src/scalar/core.rs | 6 +- elliptic-curve/src/scalar/nonzero.rs | 58 +++++++++---------- elliptic-curve/src/sec1.rs | 4 +- elliptic-curve/src/secret_key.rs | 20 +++---- elliptic-curve/src/secret_key/pkcs8.rs | 4 +- 13 files changed, 106 insertions(+), 123 deletions(-) diff --git a/elliptic-curve/src/arithmetic.rs b/elliptic-curve/src/arithmetic.rs index 7dd2f7748..1d8d76c49 100644 --- a/elliptic-curve/src/arithmetic.rs +++ b/elliptic-curve/src/arithmetic.rs @@ -7,8 +7,8 @@ use core::fmt::Debug; use subtle::{ConditionallySelectable, ConstantTimeEq}; use zeroize::DefaultIsZeroes; -/// Elliptic curve with affine arithmetic implementation. -pub trait AffineArithmetic: Curve + ScalarArithmetic { +/// Elliptic curve with an arithmetic implementation. +pub trait CurveArithmetic: Curve { /// Elliptic curve point in affine coordinates. type AffinePoint: 'static + AffineXCoordinate @@ -23,18 +23,7 @@ pub trait AffineArithmetic: Curve + ScalarArithmetic { + Sized + Send + Sync; -} - -/// Prime order elliptic curve with projective arithmetic implementation. -pub trait PrimeCurveArithmetic: - PrimeCurve + ProjectiveArithmetic -{ - /// Prime order elliptic curve group. - type CurveGroup: group::prime::PrimeCurve::AffinePoint>; -} -/// Elliptic curve with projective arithmetic implementation. -pub trait ProjectiveArithmetic: Curve + AffineArithmetic { /// Elliptic curve point in projective coordinates. /// /// Note: the following bounds are provided by [`group::Group`]: @@ -55,12 +44,8 @@ pub trait ProjectiveArithmetic: Curve + AffineArithmetic { + LinearCombination + group::Curve + group::Group; -} -/// Scalar arithmetic. -#[cfg(feature = "arithmetic")] -pub trait ScalarArithmetic: Curve { - /// Scalar field type. + /// Scalar field modulo this curve's order. /// /// Note: the following bounds are provided by [`ff::Field`]: /// - `'static` @@ -80,3 +65,11 @@ pub trait ScalarArithmetic: Curve { + ff::Field + ff::PrimeField>; } + +/// Prime order elliptic curve with projective arithmetic implementation. +pub trait PrimeCurveArithmetic: + PrimeCurve + CurveArithmetic +{ + /// Prime order elliptic curve group. + type CurveGroup: group::prime::PrimeCurve::AffinePoint>; +} diff --git a/elliptic-curve/src/dev.rs b/elliptic-curve/src/dev.rs index d316d33d1..4f759ae25 100644 --- a/elliptic-curve/src/dev.rs +++ b/elliptic-curve/src/dev.rs @@ -12,8 +12,7 @@ use crate::{ sec1::{CompressedPoint, FromEncodedPoint, ToEncodedPoint}, subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}, zeroize::DefaultIsZeroes, - AffineArithmetic, AffineXCoordinate, Curve, IsHigh, PrimeCurve, ProjectiveArithmetic, - ScalarArithmetic, + AffineXCoordinate, Curve, CurveArithmetic, IsHigh, PrimeCurve, }; use core::{ iter::{Product, Sum}, @@ -73,15 +72,9 @@ impl Curve for MockCurve { impl PrimeCurve for MockCurve {} -impl AffineArithmetic for MockCurve { +impl CurveArithmetic for MockCurve { type AffinePoint = AffinePoint; -} - -impl ProjectiveArithmetic for MockCurve { type ProjectivePoint = ProjectivePoint; -} - -impl ScalarArithmetic for MockCurve { type Scalar = Scalar; } diff --git a/elliptic-curve/src/ecdh.rs b/elliptic-curve/src/ecdh.rs index 1e9f7bc31..2d0d3d9a9 100644 --- a/elliptic-curve/src/ecdh.rs +++ b/elliptic-curve/src/ecdh.rs @@ -27,8 +27,8 @@ //! [SIGMA]: https://webee.technion.ac.il/~hugo/sigma-pdf.pdf use crate::{ - AffineArithmetic, AffinePoint, AffineXCoordinate, Curve, FieldBytes, NonZeroScalar, - ProjectiveArithmetic, ProjectivePoint, PublicKey, + AffinePoint, AffineXCoordinate, Curve, CurveArithmetic, FieldBytes, NonZeroScalar, + ProjectivePoint, PublicKey, }; use core::borrow::Borrow; use digest::{crypto_common::BlockSizeUser, Digest}; @@ -62,7 +62,7 @@ pub fn diffie_hellman( public_key: impl Borrow>, ) -> SharedSecret where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { let public_point = ProjectivePoint::::from(*public_key.borrow()); let secret_point = (public_point * secret_key.borrow().as_ref()).to_affine(); @@ -92,14 +92,14 @@ where /// takes further steps to authenticate the peers in a key exchange. pub struct EphemeralSecret where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { scalar: NonZeroScalar, } impl EphemeralSecret where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { /// Generate a cryptographically random [`EphemeralSecret`]. pub fn random(rng: impl CryptoRng + RngCore) -> Self { @@ -124,7 +124,7 @@ where impl From<&EphemeralSecret> for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { fn from(ephemeral_secret: &EphemeralSecret) -> Self { ephemeral_secret.public_key() @@ -133,18 +133,18 @@ where impl Zeroize for EphemeralSecret where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { fn zeroize(&mut self) { self.scalar.zeroize() } } -impl ZeroizeOnDrop for EphemeralSecret where C: Curve + ProjectiveArithmetic {} +impl ZeroizeOnDrop for EphemeralSecret where C: CurveArithmetic {} impl Drop for EphemeralSecret where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { fn drop(&mut self) { self.zeroize(); @@ -162,7 +162,7 @@ impl SharedSecret { #[inline] fn new(point: AffinePoint) -> Self where - C: AffineArithmetic, + C: CurveArithmetic, { Self { secret_bytes: point.x(), diff --git a/elliptic-curve/src/hash2curve/group_digest.rs b/elliptic-curve/src/hash2curve/group_digest.rs index dbcb1512b..4de06140c 100644 --- a/elliptic-curve/src/hash2curve/group_digest.rs +++ b/elliptic-curve/src/hash2curve/group_digest.rs @@ -1,11 +1,11 @@ //! Traits for handling hash to curve. use super::{hash_to_field, ExpandMsg, FromOkm, MapToCurve}; -use crate::{ProjectiveArithmetic, ProjectivePoint, Result}; +use crate::{CurveArithmetic, ProjectivePoint, Result}; use group::cofactor::CofactorGroup; /// Adds hashing arbitrary byte sequences to a valid group element -pub trait GroupDigest: ProjectiveArithmetic +pub trait GroupDigest: CurveArithmetic where ProjectivePoint: CofactorGroup, { diff --git a/elliptic-curve/src/jwk.rs b/elliptic-curve/src/jwk.rs index 23c3e8845..cac1f79ba 100644 --- a/elliptic-curve/src/jwk.rs +++ b/elliptic-curve/src/jwk.rs @@ -26,7 +26,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop}; use crate::{ public_key::PublicKey, sec1::{FromEncodedPoint, ToEncodedPoint}, - AffinePoint, ProjectiveArithmetic, + AffinePoint, CurveArithmetic, }; /// Key Type (`kty`) for elliptic curve keys. @@ -110,7 +110,7 @@ impl JwkEcKey { #[cfg(feature = "arithmetic")] pub fn to_public_key(&self) -> Result> where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -213,7 +213,7 @@ where #[cfg(feature = "arithmetic")] impl From> for JwkEcKey where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -225,7 +225,7 @@ where #[cfg(feature = "arithmetic")] impl From<&SecretKey> for JwkEcKey where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -241,7 +241,7 @@ where #[cfg(feature = "arithmetic")] impl TryFrom for PublicKey where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -255,7 +255,7 @@ where #[cfg(feature = "arithmetic")] impl TryFrom<&JwkEcKey> for PublicKey where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -269,7 +269,7 @@ where #[cfg(feature = "arithmetic")] impl From> for JwkEcKey where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -281,7 +281,7 @@ where #[cfg(feature = "arithmetic")] impl From<&PublicKey> for JwkEcKey where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { diff --git a/elliptic-curve/src/lib.rs b/elliptic-curve/src/lib.rs index 0c7d7cf9f..be154b5f6 100644 --- a/elliptic-curve/src/lib.rs +++ b/elliptic-curve/src/lib.rs @@ -104,9 +104,7 @@ pub use zeroize; #[cfg(feature = "arithmetic")] pub use { crate::{ - arithmetic::{ - AffineArithmetic, PrimeCurveArithmetic, ProjectiveArithmetic, ScalarArithmetic, - }, + arithmetic::{CurveArithmetic, PrimeCurveArithmetic}, public_key::PublicKey, scalar::{nonzero::NonZeroScalar, Scalar}, }, @@ -175,12 +173,12 @@ pub type FieldBytes = GenericArray>; /// Affine point type for a given curve with a [`ProjectiveArithmetic`] /// implementation. #[cfg(feature = "arithmetic")] -pub type AffinePoint = ::AffinePoint; +pub type AffinePoint = ::AffinePoint; /// Projective point type for a given curve with a [`ProjectiveArithmetic`] /// implementation. #[cfg(feature = "arithmetic")] -pub type ProjectivePoint = ::ProjectivePoint; +pub type ProjectivePoint = ::ProjectivePoint; /// Elliptic curve parameters used by VOPRF. #[cfg(feature = "voprf")] diff --git a/elliptic-curve/src/public_key.rs b/elliptic-curve/src/public_key.rs index ef5216613..a916cd8a8 100644 --- a/elliptic-curve/src/public_key.rs +++ b/elliptic-curve/src/public_key.rs @@ -1,8 +1,7 @@ //! Elliptic curve public keys. use crate::{ - point::NonIdentity, AffinePoint, Curve, Error, NonZeroScalar, ProjectiveArithmetic, - ProjectivePoint, Result, + point::NonIdentity, AffinePoint, CurveArithmetic, Error, NonZeroScalar, ProjectivePoint, Result, }; use core::fmt::Debug; use group::{Curve as _, Group}; @@ -20,7 +19,7 @@ use core::str::FromStr; use { crate::{ sec1::{EncodedPoint, FromEncodedPoint, ModulusSize, ToEncodedPoint}, - FieldSize, PointCompression, + Curve, FieldSize, PointCompression, }, core::cmp::Ordering, subtle::CtOption, @@ -83,14 +82,14 @@ use { #[derive(Clone, Debug, Eq, PartialEq)] pub struct PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { point: AffinePoint, } impl PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { /// Convert an [`AffinePoint`] into a [`PublicKey`] pub fn from_affine(point: AffinePoint) -> Result { @@ -136,7 +135,7 @@ where #[cfg(feature = "alloc")] pub fn to_sec1_bytes(&self) -> Box<[u8]> where - C: Curve + ProjectiveArithmetic + PointCompression, + C: CurveArithmetic + PointCompression, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -203,19 +202,19 @@ where impl AsRef> for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { fn as_ref(&self) -> &AffinePoint { self.as_affine() } } -impl Copy for PublicKey where C: Curve + ProjectiveArithmetic {} +impl Copy for PublicKey where C: CurveArithmetic {} #[cfg(feature = "sec1")] impl FromEncodedPoint for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -231,7 +230,7 @@ where #[cfg(feature = "sec1")] impl ToEncodedPoint for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -245,7 +244,7 @@ where #[cfg(feature = "sec1")] impl From> for EncodedPoint where - C: Curve + ProjectiveArithmetic + PointCompression, + C: CurveArithmetic + PointCompression, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -257,7 +256,7 @@ where #[cfg(feature = "sec1")] impl From<&PublicKey> for EncodedPoint where - C: Curve + ProjectiveArithmetic + PointCompression, + C: CurveArithmetic + PointCompression, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -268,7 +267,7 @@ where impl From> for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, P: Copy + Into>, { fn from(value: NonIdentity

) -> Self { @@ -278,7 +277,7 @@ where impl From<&NonIdentity

> for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, P: Copy + Into>, { fn from(value: &NonIdentity

) -> Self { @@ -291,7 +290,7 @@ where #[cfg(feature = "sec1")] impl PartialOrd for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -303,7 +302,7 @@ where #[cfg(feature = "sec1")] impl Ord for PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -318,7 +317,7 @@ where #[cfg(all(feature = "pkcs8", feature = "sec1"))] impl TryFrom> for PublicKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -334,7 +333,7 @@ where #[cfg(all(feature = "pkcs8", feature = "sec1"))] impl DecodePublicKey for PublicKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -343,7 +342,7 @@ where #[cfg(all(feature = "alloc", feature = "pkcs8"))] impl EncodePublicKey for PublicKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -366,7 +365,7 @@ where #[cfg(feature = "pem")] impl FromStr for PublicKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -380,7 +379,7 @@ where #[cfg(feature = "pem")] impl ToString for PublicKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -393,7 +392,7 @@ where #[cfg(all(feature = "pkcs8", feature = "serde"))] impl Serialize for PublicKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -409,7 +408,7 @@ where #[cfg(all(feature = "pkcs8", feature = "serde"))] impl<'de, C> Deserialize<'de> for PublicKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { diff --git a/elliptic-curve/src/scalar.rs b/elliptic-curve/src/scalar.rs index 0081a97fe..aedd57ea8 100644 --- a/elliptic-curve/src/scalar.rs +++ b/elliptic-curve/src/scalar.rs @@ -8,11 +8,11 @@ pub(crate) mod core; pub(crate) mod nonzero; #[cfg(feature = "arithmetic")] -use crate::ScalarArithmetic; +use crate::CurveArithmetic; /// Scalar field element for a particular elliptic curve. #[cfg(feature = "arithmetic")] -pub type Scalar = ::Scalar; +pub type Scalar = ::Scalar; /// Bit representation of a scalar field element of a given curve. #[cfg(feature = "bits")] diff --git a/elliptic-curve/src/scalar/core.rs b/elliptic-curve/src/scalar/core.rs index 94fd8bfab..472e8376f 100644 --- a/elliptic-curve/src/scalar/core.rs +++ b/elliptic-curve/src/scalar/core.rs @@ -21,7 +21,7 @@ use zeroize::DefaultIsZeroes; #[cfg(feature = "arithmetic")] use { - super::{Scalar, ScalarArithmetic}, + super::{CurveArithmetic, Scalar}, ff::PrimeField, }; @@ -41,7 +41,7 @@ use serdect::serde::{de, ser, Deserialize, Serialize}; /// /// The serialization is a fixed-width big endian encoding. When used with /// textual formats, the binary data is encoded as hexadecimal. -// TODO(tarcieri): make this a fully generic `Scalar` type and use it for `ScalarArithmetic` +// TODO(tarcieri): make this a fully generic `Scalar` type and use it for `CurveArithmetic` #[derive(Copy, Clone, Debug, Default)] pub struct ScalarCore { /// Inner unsigned integer type. @@ -144,7 +144,7 @@ where #[cfg(feature = "arithmetic")] impl ScalarCore where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { /// Convert [`ScalarCore`] into a given curve's scalar type // TODO(tarcieri): replace curve-specific scalars with `ScalarCore` diff --git a/elliptic-curve/src/scalar/nonzero.rs b/elliptic-curve/src/scalar/nonzero.rs index 06ef1155c..311d2a465 100644 --- a/elliptic-curve/src/scalar/nonzero.rs +++ b/elliptic-curve/src/scalar/nonzero.rs @@ -3,7 +3,7 @@ use crate::{ ops::{Invert, Reduce, ReduceNonZero}, rand_core::{CryptoRng, RngCore}, - Curve, Error, FieldBytes, IsHigh, PrimeCurve, Scalar, ScalarArithmetic, ScalarCore, SecretKey, + CurveArithmetic, Error, FieldBytes, IsHigh, PrimeCurve, Scalar, ScalarCore, SecretKey, }; use base16ct::HexDisplay; use core::{ @@ -31,14 +31,14 @@ use serdect::serde::{de, ser, Deserialize, Serialize}; #[derive(Clone)] pub struct NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { scalar: Scalar, } impl NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { /// Generate a random `NonZeroScalar`. pub fn random(mut rng: impl CryptoRng + RngCore) -> Self { @@ -70,7 +70,7 @@ where impl AsRef> for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn as_ref(&self) -> &Scalar { &self.scalar @@ -79,7 +79,7 @@ where impl ConditionallySelectable for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Self { @@ -90,18 +90,18 @@ where impl ConstantTimeEq for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn ct_eq(&self, other: &Self) -> Choice { self.scalar.ct_eq(&other.scalar) } } -impl Copy for NonZeroScalar where C: Curve + ScalarArithmetic {} +impl Copy for NonZeroScalar where C: CurveArithmetic {} impl Deref for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { type Target = Scalar; @@ -112,7 +112,7 @@ where impl From> for FieldBytes where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn from(scalar: NonZeroScalar) -> FieldBytes { Self::from(&scalar) @@ -121,7 +121,7 @@ where impl From<&NonZeroScalar> for FieldBytes where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn from(scalar: &NonZeroScalar) -> FieldBytes { scalar.to_repr() @@ -130,7 +130,7 @@ where impl From> for ScalarCore where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn from(scalar: NonZeroScalar) -> ScalarCore { ScalarCore::from_be_bytes(scalar.to_repr()).unwrap() @@ -139,7 +139,7 @@ where impl From<&NonZeroScalar> for ScalarCore where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn from(scalar: &NonZeroScalar) -> ScalarCore { ScalarCore::from_be_bytes(scalar.to_repr()).unwrap() @@ -148,7 +148,7 @@ where impl From> for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn from(sk: SecretKey) -> NonZeroScalar { Self::from(&sk) @@ -157,7 +157,7 @@ where impl From<&SecretKey> for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn from(sk: &SecretKey) -> NonZeroScalar { let scalar = sk.as_scalar_core().to_scalar(); @@ -168,7 +168,7 @@ where impl Invert for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { type Output = Self; @@ -182,7 +182,7 @@ where impl IsHigh for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn is_high(&self) -> Choice { self.scalar.is_high() @@ -191,7 +191,7 @@ where impl Neg for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { type Output = NonZeroScalar; @@ -204,7 +204,7 @@ where impl Mul> for NonZeroScalar where - C: PrimeCurve + ScalarArithmetic, + C: PrimeCurve + CurveArithmetic, { type Output = Self; @@ -216,7 +216,7 @@ where impl Mul<&NonZeroScalar> for NonZeroScalar where - C: PrimeCurve + ScalarArithmetic, + C: PrimeCurve + CurveArithmetic, { type Output = Self; @@ -232,7 +232,7 @@ where /// Note: implementation is the same as `ReduceNonZero` impl Reduce for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, I: Integer + ArrayEncoding, Scalar: ReduceNonZero, { @@ -243,7 +243,7 @@ where impl ReduceNonZero for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, I: Integer + ArrayEncoding, Scalar: ReduceNonZero, { @@ -256,7 +256,7 @@ where impl TryFrom<&[u8]> for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { type Error = Error; @@ -274,7 +274,7 @@ where impl Zeroize for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn zeroize(&mut self) { // Use zeroize's volatile writes to ensure value is cleared. @@ -288,7 +288,7 @@ where impl fmt::Display for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:X}", self) @@ -297,7 +297,7 @@ where impl fmt::LowerHex for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:x}", HexDisplay(&self.to_repr())) @@ -306,7 +306,7 @@ where impl fmt::UpperHex for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:}", HexDisplay(&self.to_repr())) @@ -315,7 +315,7 @@ where impl str::FromStr for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { type Err = Error; @@ -333,7 +333,7 @@ where #[cfg(feature = "serde")] impl Serialize for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn serialize(&self, serializer: S) -> Result where @@ -346,7 +346,7 @@ where #[cfg(feature = "serde")] impl<'de, C> Deserialize<'de> for NonZeroScalar where - C: Curve + ScalarArithmetic, + C: CurveArithmetic, { fn deserialize(deserializer: D) -> Result where diff --git a/elliptic-curve/src/sec1.rs b/elliptic-curve/src/sec1.rs index 3e1635941..ea0b27541 100644 --- a/elliptic-curve/src/sec1.rs +++ b/elliptic-curve/src/sec1.rs @@ -9,7 +9,7 @@ use generic_array::GenericArray; use subtle::CtOption; #[cfg(feature = "arithmetic")] -use crate::{AffinePoint, Error, ProjectiveArithmetic}; +use crate::{AffinePoint, CurveArithmetic, Error}; /// Encoded elliptic curve point with point compression. pub type CompressedPoint = GenericArray>; @@ -96,7 +96,7 @@ where #[cfg(all(feature = "arithmetic"))] impl ValidatePublicKey for C where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { diff --git a/elliptic-curve/src/secret_key.rs b/elliptic-curve/src/secret_key.rs index fc777ab39..bc335f952 100644 --- a/elliptic-curve/src/secret_key.rs +++ b/elliptic-curve/src/secret_key.rs @@ -28,7 +28,7 @@ use { #[cfg(feature = "arithmetic")] use crate::{ rand_core::{CryptoRng, RngCore}, - NonZeroScalar, ProjectiveArithmetic, PublicKey, + CurveArithmetic, NonZeroScalar, PublicKey, }; #[cfg(feature = "jwk")] @@ -99,7 +99,7 @@ where #[cfg(feature = "arithmetic")] pub fn random(rng: impl CryptoRng + RngCore) -> Self where - C: ProjectiveArithmetic, + C: CurveArithmetic, { Self { inner: NonZeroScalar::::random(rng).into(), @@ -132,7 +132,7 @@ where #[cfg(feature = "arithmetic")] pub fn to_nonzero_scalar(&self) -> NonZeroScalar where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { self.into() } @@ -141,7 +141,7 @@ where #[cfg(feature = "arithmetic")] pub fn public_key(&self) -> PublicKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { PublicKey::from_secret_scalar(&self.to_nonzero_scalar()) } @@ -185,7 +185,7 @@ where #[cfg(all(feature = "alloc", feature = "arithmetic", feature = "sec1"))] pub fn to_sec1_der(&self) -> der::Result>> where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -237,7 +237,7 @@ where #[cfg(feature = "pem")] pub fn to_pem(&self, line_ending: pem::LineEnding) -> Result> where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -272,7 +272,7 @@ where #[cfg(all(feature = "arithmetic", feature = "jwk"))] pub fn to_jwk(&self) -> JwkEcKey where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -283,7 +283,7 @@ where #[cfg(all(feature = "arithmetic", feature = "jwk"))] pub fn to_jwk_string(&self) -> Zeroizing where - C: Curve + JwkParameters + ProjectiveArithmetic, + C: CurveArithmetic + JwkParameters, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, { @@ -361,7 +361,7 @@ where #[cfg(feature = "arithmetic")] impl From> for SecretKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { fn from(scalar: NonZeroScalar) -> SecretKey { SecretKey::from(&scalar) @@ -371,7 +371,7 @@ where #[cfg(feature = "arithmetic")] impl From<&NonZeroScalar> for SecretKey where - C: Curve + ProjectiveArithmetic, + C: CurveArithmetic, { fn from(scalar: &NonZeroScalar) -> SecretKey { SecretKey { diff --git a/elliptic-curve/src/secret_key/pkcs8.rs b/elliptic-curve/src/secret_key/pkcs8.rs index f30832cfd..52090a23b 100644 --- a/elliptic-curve/src/secret_key/pkcs8.rs +++ b/elliptic-curve/src/secret_key/pkcs8.rs @@ -13,7 +13,7 @@ use sec1::EcPrivateKey; use { crate::{ sec1::{FromEncodedPoint, ToEncodedPoint}, - AffinePoint, ProjectiveArithmetic, + AffinePoint, CurveArithmetic, }, pkcs8::{der, EncodePrivateKey}, }; @@ -52,7 +52,7 @@ where #[cfg(all(feature = "alloc", feature = "arithmetic"))] impl EncodePrivateKey for SecretKey where - C: Curve + AssociatedOid + ProjectiveArithmetic, + C: AssociatedOid + CurveArithmetic, AffinePoint: FromEncodedPoint + ToEncodedPoint, FieldSize: ModulusSize, {