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
11 changes: 7 additions & 4 deletions elliptic-curve/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
use crate::{Curve, FieldBytes};
use core::fmt::Debug;
use subtle::{ConditionallySelectable, ConstantTimeEq};
use zeroize::DefaultIsZeroes;

/// Elliptic curve with affine arithmetic implementation.
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait AffineArithmetic: Curve + ScalarArithmetic {
/// Elliptic curve point in affine coordinates.
type AffinePoint: Copy
type AffinePoint: 'static
+ Copy
+ Clone
+ ConditionallySelectable
+ ConstantTimeEq
+ Debug
+ Default
+ DefaultIsZeroes
+ Sized
+ Send
+ Sync
+ 'static;
+ Sync;
}

/// Elliptic curve with projective arithmetic implementation.
Expand All @@ -37,6 +39,7 @@ pub trait ProjectiveArithmetic: Curve + AffineArithmetic {
type ProjectivePoint: ConditionallySelectable
+ ConstantTimeEq
+ Default
+ DefaultIsZeroes
+ From<Self::AffinePoint>
+ Into<Self::AffinePoint>
+ group::Curve<AffineRepr = Self::AffinePoint>
Expand All @@ -59,5 +62,5 @@ pub trait ScalarArithmetic: Curve {
/// - [`Default`]
/// - [`Send`]
/// - [`Sync`]
type Scalar: ff::Field + ff::PrimeField<Repr = FieldBytes<Self>>;
type Scalar: DefaultIsZeroes + ff::Field + ff::PrimeField<Repr = FieldBytes<Self>>;
}
14 changes: 7 additions & 7 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
rand_core::RngCore,
sec1::{FromEncodedPoint, ToEncodedPoint},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeLess, CtOption},
zeroize::Zeroize,
zeroize::DefaultIsZeroes,
AffineArithmetic, AlgorithmParameters, Curve, PrimeCurve, ProjectiveArithmetic,
ScalarArithmetic,
};
Expand Down Expand Up @@ -209,6 +209,8 @@ impl ConstantTimeEq for Scalar {
}
}

impl DefaultIsZeroes for Scalar {}

impl Add<Scalar> for Scalar {
type Output = Scalar;

Expand Down Expand Up @@ -319,12 +321,6 @@ impl From<&Scalar> for FieldBytes {
}
}

impl Zeroize for Scalar {
fn zeroize(&mut self) {
self.0.as_mut().zeroize();
}
}

/// Example affine point type
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AffinePoint {
Expand Down Expand Up @@ -359,6 +355,8 @@ impl Default for AffinePoint {
}
}

impl DefaultIsZeroes for AffinePoint {}

impl FromEncodedPoint<MockCurve> for AffinePoint {
fn from_encoded_point(point: &EncodedPoint) -> Option<Self> {
if point.is_identity() {
Expand Down Expand Up @@ -431,6 +429,8 @@ impl Default for ProjectivePoint {
}
}

impl DefaultIsZeroes for ProjectivePoint {}

impl From<AffinePoint> for ProjectivePoint {
fn from(point: AffinePoint) -> ProjectivePoint {
match point {
Expand Down
12 changes: 1 addition & 11 deletions elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
//! [SIGMA]: https://webee.technion.ac.il/~hugo/sigma-pdf.pdf

use crate::{
AffinePoint, Curve, FieldBytes, NonZeroScalar, ProjectiveArithmetic, ProjectivePoint,
PublicKey, Scalar,
AffinePoint, Curve, FieldBytes, NonZeroScalar, ProjectiveArithmetic, ProjectivePoint, PublicKey,
};
use core::borrow::Borrow;
use group::Curve as _;
Expand Down Expand Up @@ -61,8 +60,6 @@ pub fn diffie_hellman<C>(
) -> SharedSecret<C>
where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: Zeroize,
Scalar<C>: Zeroize,
SharedSecret<C>: for<'a> From<&'a AffinePoint<C>>,
{
let public_point = ProjectivePoint::<C>::from(*public_key.borrow());
Expand Down Expand Up @@ -96,16 +93,13 @@ where
pub struct EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
Scalar<C>: Zeroize,
{
scalar: NonZeroScalar<C>,
}

impl<C> EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: Zeroize,
Scalar<C>: Zeroize,
SharedSecret<C>: for<'a> From<&'a AffinePoint<C>>,
{
/// Generate a cryptographically random [`EphemeralSecret`].
Expand All @@ -132,8 +126,6 @@ where
impl<C> From<&EphemeralSecret<C>> for PublicKey<C>
where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: Zeroize,
Scalar<C>: Zeroize,
SharedSecret<C>: for<'a> From<&'a AffinePoint<C>>,
{
fn from(ephemeral_secret: &EphemeralSecret<C>) -> Self {
Expand All @@ -144,7 +136,6 @@ where
impl<C> Zeroize for EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
Scalar<C>: Zeroize,
{
fn zeroize(&mut self) {
self.scalar.zeroize()
Expand All @@ -154,7 +145,6 @@ where
impl<C> Drop for EphemeralSecret<C>
where
C: Curve + ProjectiveArithmetic,
Scalar<C>: Zeroize,
{
fn drop(&mut self) {
self.zeroize();
Expand Down
4 changes: 1 addition & 3 deletions elliptic-curve/src/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use zeroize::Zeroize;
use crate::{
public_key::PublicKey,
sec1::{FromEncodedPoint, ToEncodedPoint},
AffinePoint, ProjectiveArithmetic, Scalar,
AffinePoint, ProjectiveArithmetic,
};

/// Key Type (`kty`) for elliptic curve keys.
Expand Down Expand Up @@ -270,7 +270,6 @@ impl<C> From<SecretKey<C>> for JwkEcKey
where
C: PrimeCurve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
Scalar<C>: Zeroize,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
Expand All @@ -286,7 +285,6 @@ impl<C> From<&SecretKey<C>> for JwkEcKey
where
C: PrimeCurve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
Scalar<C>: Zeroize,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
Expand Down
1 change: 0 additions & 1 deletion elliptic-curve/src/scalar/non_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ where
impl<C> Zeroize for NonZeroScalar<C>
where
C: Curve + ProjectiveArithmetic,
Scalar<C>: Zeroize,
{
fn zeroize(&mut self) {
self.scalar.zeroize();
Expand Down
7 changes: 1 addition & 6 deletions elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ use alloc::boxed::Box;
use crate::{point::DecompressPoint, AffinePoint, ProjectiveArithmetic};

#[cfg(all(feature = "arithmetic"))]
use crate::{
group::{Curve as _, Group},
Scalar,
};
use crate::group::{Curve as _, Group};

/// Size of a compressed point for the given elliptic curve when encoded
/// using the SEC1 `Elliptic-Curve-Point-to-Octet-String` algorithm
Expand Down Expand Up @@ -123,7 +120,6 @@ where
where
C: PrimeCurve + ProjectiveArithmetic,
AffinePoint<C>: ToEncodedPoint<C>,
Scalar<C>: Zeroize,
{
(C::ProjectivePoint::generator() * secret_key.to_secret_scalar().as_ref())
.to_affine()
Expand Down Expand Up @@ -552,7 +548,6 @@ impl<C> ValidatePublicKey for C
where
C: PrimeCurve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
Scalar<C>: Zeroize,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
Expand Down
9 changes: 3 additions & 6 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use zeroize::Zeroize;
#[cfg(feature = "arithmetic")]
use crate::{
rand_core::{CryptoRng, RngCore},
NonZeroScalar, ProjectiveArithmetic, PublicKey, Scalar,
NonZeroScalar, ProjectiveArithmetic, PublicKey,
};

#[cfg(feature = "jwk")]
Expand Down Expand Up @@ -87,7 +87,6 @@ where
pub fn random(rng: impl CryptoRng + RngCore) -> Self
where
C: ProjectiveArithmetic,
Scalar<C>: Zeroize,
{
Self {
inner: NonZeroScalar::<C>::random(rng).into(),
Expand Down Expand Up @@ -139,7 +138,6 @@ where
pub fn to_secret_scalar(&self) -> NonZeroScalar<C>
where
C: Curve + ProjectiveArithmetic,
Scalar<C>: Zeroize,
{
self.into()
}
Expand All @@ -150,7 +148,6 @@ where
pub fn public_key(&self) -> PublicKey<C>
where
C: Curve + ProjectiveArithmetic,
Scalar<C>: Zeroize,
{
PublicKey::from_secret_scalar(&self.to_secret_scalar())
}
Expand Down Expand Up @@ -187,7 +184,7 @@ where
where
C: PrimeCurve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
Scalar<C>: Zeroize,

UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
Expand All @@ -202,7 +199,7 @@ where
where
C: PrimeCurve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
Scalar<C>: Zeroize,

UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
Expand Down
2 changes: 0 additions & 2 deletions elliptic-curve/src/secret_key/pkcs8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use pkcs8::{
#[cfg(all(feature = "arithmetic", feature = "pem"))]
use {
crate::{
scalar::Scalar,
sec1::{FromEncodedPoint, ToEncodedPoint},
AffinePoint, ProjectiveArithmetic,
},
Expand Down Expand Up @@ -97,7 +96,6 @@ impl<C> ToPrivateKey for SecretKey<C>
where
C: PrimeCurve + AlgorithmParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
Scalar<C>: Zeroize,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
Expand Down