Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions elliptic-curve/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>
Expand All @@ -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<ProjectivePoint = Self::CurveGroup>
{
/// Prime order elliptic curve group.
type CurveGroup: group::prime::PrimeCurve<Affine = <Self as AffineArithmetic>::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`]:
Expand All @@ -55,12 +44,8 @@ pub trait ProjectiveArithmetic: Curve + AffineArithmetic {
+ LinearCombination
+ group::Curve<AffineRepr = Self::AffinePoint>
+ group::Group<Scalar = Self::Scalar>;
}

/// 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`
Expand All @@ -80,3 +65,11 @@ pub trait ScalarArithmetic: Curve {
+ ff::Field
+ ff::PrimeField<Repr = FieldBytes<Self>>;
}

/// Prime order elliptic curve with projective arithmetic implementation.
pub trait PrimeCurveArithmetic:
PrimeCurve + CurveArithmetic<ProjectivePoint = Self::CurveGroup>
{
/// Prime order elliptic curve group.
type CurveGroup: group::prime::PrimeCurve<Affine = <Self as CurveArithmetic>::AffinePoint>;
}
11 changes: 2 additions & 9 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 10 additions & 10 deletions elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn diffie_hellman<C>(
public_key: impl Borrow<AffinePoint<C>>,
) -> SharedSecret<C>
where
C: Curve + ProjectiveArithmetic,
C: CurveArithmetic,
{
let public_point = ProjectivePoint::<C>::from(*public_key.borrow());
let secret_point = (public_point * secret_key.borrow().as_ref()).to_affine();
Expand Down Expand Up @@ -92,14 +92,14 @@ where
/// takes further steps to authenticate the peers in a key exchange.
pub struct EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
C: CurveArithmetic,
{
scalar: NonZeroScalar<C>,
}

impl<C> EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
C: CurveArithmetic,
{
/// Generate a cryptographically random [`EphemeralSecret`].
pub fn random(rng: impl CryptoRng + RngCore) -> Self {
Expand All @@ -124,7 +124,7 @@ where

impl<C> From<&EphemeralSecret<C>> for PublicKey<C>
where
C: Curve + ProjectiveArithmetic,
C: CurveArithmetic,
{
fn from(ephemeral_secret: &EphemeralSecret<C>) -> Self {
ephemeral_secret.public_key()
Expand All @@ -133,18 +133,18 @@ where

impl<C> Zeroize for EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
C: CurveArithmetic,
{
fn zeroize(&mut self) {
self.scalar.zeroize()
}
}

impl<C> ZeroizeOnDrop for EphemeralSecret<C> where C: Curve + ProjectiveArithmetic {}
impl<C> ZeroizeOnDrop for EphemeralSecret<C> where C: CurveArithmetic {}

impl<C> Drop for EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
C: CurveArithmetic,
{
fn drop(&mut self) {
self.zeroize();
Expand All @@ -162,7 +162,7 @@ impl<C: Curve> SharedSecret<C> {
#[inline]
fn new(point: AffinePoint<C>) -> Self
where
C: AffineArithmetic,
C: CurveArithmetic,
{
Self {
secret_bytes: point.x(),
Expand Down
4 changes: 2 additions & 2 deletions elliptic-curve/src/hash2curve/group_digest.rs
Original file line number Diff line number Diff line change
@@ -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<Self>: CofactorGroup,
{
Expand Down
16 changes: 8 additions & 8 deletions elliptic-curve/src/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -110,7 +110,7 @@ impl JwkEcKey {
#[cfg(feature = "arithmetic")]
pub fn to_public_key<C>(&self) -> Result<PublicKey<C>>
where
C: Curve + JwkParameters + ProjectiveArithmetic,
C: CurveArithmetic + JwkParameters,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
Expand Down Expand Up @@ -213,7 +213,7 @@ where
#[cfg(feature = "arithmetic")]
impl<C> From<SecretKey<C>> for JwkEcKey
where
C: Curve + JwkParameters + ProjectiveArithmetic,
C: CurveArithmetic + JwkParameters,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
Expand All @@ -225,7 +225,7 @@ where
#[cfg(feature = "arithmetic")]
impl<C> From<&SecretKey<C>> for JwkEcKey
where
C: Curve + JwkParameters + ProjectiveArithmetic,
C: CurveArithmetic + JwkParameters,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
Expand All @@ -241,7 +241,7 @@ where
#[cfg(feature = "arithmetic")]
impl<C> TryFrom<JwkEcKey> for PublicKey<C>
where
C: Curve + JwkParameters + ProjectiveArithmetic,
C: CurveArithmetic + JwkParameters,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
Expand All @@ -255,7 +255,7 @@ where
#[cfg(feature = "arithmetic")]
impl<C> TryFrom<&JwkEcKey> for PublicKey<C>
where
C: Curve + JwkParameters + ProjectiveArithmetic,
C: CurveArithmetic + JwkParameters,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
Expand All @@ -269,7 +269,7 @@ where
#[cfg(feature = "arithmetic")]
impl<C> From<PublicKey<C>> for JwkEcKey
where
C: Curve + JwkParameters + ProjectiveArithmetic,
C: CurveArithmetic + JwkParameters,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
Expand All @@ -281,7 +281,7 @@ where
#[cfg(feature = "arithmetic")]
impl<C> From<&PublicKey<C>> for JwkEcKey
where
C: Curve + JwkParameters + ProjectiveArithmetic,
C: CurveArithmetic + JwkParameters,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
Expand Down
8 changes: 3 additions & 5 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand Down Expand Up @@ -175,12 +173,12 @@ pub type FieldBytes<C> = GenericArray<u8, FieldSize<C>>;
/// Affine point type for a given curve with a [`ProjectiveArithmetic`]
/// implementation.
#[cfg(feature = "arithmetic")]
pub type AffinePoint<C> = <C as AffineArithmetic>::AffinePoint;
pub type AffinePoint<C> = <C as CurveArithmetic>::AffinePoint;

/// Projective point type for a given curve with a [`ProjectiveArithmetic`]
/// implementation.
#[cfg(feature = "arithmetic")]
pub type ProjectivePoint<C> = <C as ProjectiveArithmetic>::ProjectivePoint;
pub type ProjectivePoint<C> = <C as CurveArithmetic>::ProjectivePoint;

/// Elliptic curve parameters used by VOPRF.
#[cfg(feature = "voprf")]
Expand Down
Loading