Skip to content

Commit 8131fa1

Browse files
committed
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.
1 parent 8c79d51 commit 8131fa1

13 files changed

Lines changed: 106 additions & 122 deletions

File tree

elliptic-curve/src/arithmetic.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use core::fmt::Debug;
77
use subtle::{ConditionallySelectable, ConstantTimeEq};
88
use zeroize::DefaultIsZeroes;
99

10-
/// Elliptic curve with affine arithmetic implementation.
11-
pub trait AffineArithmetic: Curve + ScalarArithmetic {
10+
/// Elliptic curve with an arithmetic implementation.
11+
pub trait CurveArithmetic: Curve {
1212
/// Elliptic curve point in affine coordinates.
1313
type AffinePoint: 'static
1414
+ AffineXCoordinate<Self>
@@ -23,18 +23,7 @@ pub trait AffineArithmetic: Curve + ScalarArithmetic {
2323
+ Sized
2424
+ Send
2525
+ Sync;
26-
}
27-
28-
/// Prime order elliptic curve with projective arithmetic implementation.
29-
pub trait PrimeCurveArithmetic:
30-
PrimeCurve + ProjectiveArithmetic<ProjectivePoint = Self::CurveGroup>
31-
{
32-
/// Prime order elliptic curve group.
33-
type CurveGroup: group::prime::PrimeCurve<Affine = <Self as AffineArithmetic>::AffinePoint>;
34-
}
3526

36-
/// Elliptic curve with projective arithmetic implementation.
37-
pub trait ProjectiveArithmetic: Curve + AffineArithmetic {
3827
/// Elliptic curve point in projective coordinates.
3928
///
4029
/// Note: the following bounds are provided by [`group::Group`]:
@@ -55,12 +44,8 @@ pub trait ProjectiveArithmetic: Curve + AffineArithmetic {
5544
+ LinearCombination
5645
+ group::Curve<AffineRepr = Self::AffinePoint>
5746
+ group::Group<Scalar = Self::Scalar>;
58-
}
5947

60-
/// Scalar arithmetic.
61-
#[cfg(feature = "arithmetic")]
62-
pub trait ScalarArithmetic: Curve {
63-
/// Scalar field type.
48+
/// Scalar field modulo this curve's order.
6449
///
6550
/// Note: the following bounds are provided by [`ff::Field`]:
6651
/// - `'static`
@@ -80,3 +65,11 @@ pub trait ScalarArithmetic: Curve {
8065
+ ff::Field
8166
+ ff::PrimeField<Repr = FieldBytes<Self>>;
8267
}
68+
69+
/// Prime order elliptic curve with projective arithmetic implementation.
70+
pub trait PrimeCurveArithmetic:
71+
PrimeCurve + CurveArithmetic<ProjectivePoint = Self::CurveGroup>
72+
{
73+
/// Prime order elliptic curve group.
74+
type CurveGroup: group::prime::PrimeCurve<Affine = <Self as CurveArithmetic>::AffinePoint>;
75+
}

elliptic-curve/src/dev.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use crate::{
1212
sec1::{CompressedPoint, FromEncodedPoint, ToEncodedPoint},
1313
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
1414
zeroize::DefaultIsZeroes,
15-
AffineArithmetic, AffineXCoordinate, Curve, IsHigh, PrimeCurve, ProjectiveArithmetic,
16-
ScalarArithmetic,
15+
AffineXCoordinate, Curve, CurveArithmetic, IsHigh, PrimeCurve,
1716
};
1817
use core::{
1918
iter::{Product, Sum},
@@ -73,15 +72,9 @@ impl Curve for MockCurve {
7372

7473
impl PrimeCurve for MockCurve {}
7574

76-
impl AffineArithmetic for MockCurve {
75+
impl CurveArithmetic for MockCurve {
7776
type AffinePoint = AffinePoint;
78-
}
79-
80-
impl ProjectiveArithmetic for MockCurve {
8177
type ProjectivePoint = ProjectivePoint;
82-
}
83-
84-
impl ScalarArithmetic for MockCurve {
8578
type Scalar = Scalar;
8679
}
8780

elliptic-curve/src/ecdh.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
//! [SIGMA]: https://webee.technion.ac.il/~hugo/sigma-pdf.pdf
2828
2929
use crate::{
30-
AffineArithmetic, AffinePoint, AffineXCoordinate, Curve, FieldBytes, NonZeroScalar,
31-
ProjectiveArithmetic, ProjectivePoint, PublicKey,
30+
AffinePoint, AffineXCoordinate, Curve, CurveArithmetic, FieldBytes, NonZeroScalar,
31+
ProjectivePoint, PublicKey,
3232
};
3333
use core::borrow::Borrow;
3434
use digest::{crypto_common::BlockSizeUser, Digest};
@@ -62,7 +62,7 @@ pub fn diffie_hellman<C>(
6262
public_key: impl Borrow<AffinePoint<C>>,
6363
) -> SharedSecret<C>
6464
where
65-
C: Curve + ProjectiveArithmetic,
65+
C: CurveArithmetic,
6666
{
6767
let public_point = ProjectivePoint::<C>::from(*public_key.borrow());
6868
let secret_point = (public_point * secret_key.borrow().as_ref()).to_affine();
@@ -92,14 +92,14 @@ where
9292
/// takes further steps to authenticate the peers in a key exchange.
9393
pub struct EphemeralSecret<C>
9494
where
95-
C: Curve + ProjectiveArithmetic,
95+
C: CurveArithmetic,
9696
{
9797
scalar: NonZeroScalar<C>,
9898
}
9999

100100
impl<C> EphemeralSecret<C>
101101
where
102-
C: Curve + ProjectiveArithmetic,
102+
C: CurveArithmetic,
103103
{
104104
/// Generate a cryptographically random [`EphemeralSecret`].
105105
pub fn random(rng: impl CryptoRng + RngCore) -> Self {
@@ -124,7 +124,7 @@ where
124124

125125
impl<C> From<&EphemeralSecret<C>> for PublicKey<C>
126126
where
127-
C: Curve + ProjectiveArithmetic,
127+
C: CurveArithmetic,
128128
{
129129
fn from(ephemeral_secret: &EphemeralSecret<C>) -> Self {
130130
ephemeral_secret.public_key()
@@ -133,18 +133,18 @@ where
133133

134134
impl<C> Zeroize for EphemeralSecret<C>
135135
where
136-
C: Curve + ProjectiveArithmetic,
136+
C: CurveArithmetic,
137137
{
138138
fn zeroize(&mut self) {
139139
self.scalar.zeroize()
140140
}
141141
}
142142

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

145145
impl<C> Drop for EphemeralSecret<C>
146146
where
147-
C: Curve + ProjectiveArithmetic,
147+
C: CurveArithmetic,
148148
{
149149
fn drop(&mut self) {
150150
self.zeroize();
@@ -162,7 +162,7 @@ impl<C: Curve> SharedSecret<C> {
162162
#[inline]
163163
fn new(point: AffinePoint<C>) -> Self
164164
where
165-
C: AffineArithmetic,
165+
C: CurveArithmetic,
166166
{
167167
Self {
168168
secret_bytes: point.x(),

elliptic-curve/src/hash2curve/group_digest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Traits for handling hash to curve.
22
33
use super::{hash_to_field, ExpandMsg, FromOkm, MapToCurve};
4-
use crate::{ProjectiveArithmetic, ProjectivePoint, Result};
4+
use crate::{CurveArithmetic, ProjectivePoint, Result};
55
use group::cofactor::CofactorGroup;
66

77
/// Adds hashing arbitrary byte sequences to a valid group element
8-
pub trait GroupDigest: ProjectiveArithmetic
8+
pub trait GroupDigest: CurveArithmetic
99
where
1010
ProjectivePoint<Self>: CofactorGroup,
1111
{

elliptic-curve/src/jwk.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
2626
use crate::{
2727
public_key::PublicKey,
2828
sec1::{FromEncodedPoint, ToEncodedPoint},
29-
AffinePoint, ProjectiveArithmetic,
29+
AffinePoint, CurveArithmetic,
3030
};
3131

3232
/// Key Type (`kty`) for elliptic curve keys.
@@ -110,7 +110,7 @@ impl JwkEcKey {
110110
#[cfg(feature = "arithmetic")]
111111
pub fn to_public_key<C>(&self) -> Result<PublicKey<C>>
112112
where
113-
C: Curve + JwkParameters + ProjectiveArithmetic,
113+
C: CurveArithmetic + JwkParameters,
114114
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
115115
FieldSize<C>: ModulusSize,
116116
{
@@ -213,7 +213,7 @@ where
213213
#[cfg(feature = "arithmetic")]
214214
impl<C> From<SecretKey<C>> for JwkEcKey
215215
where
216-
C: Curve + JwkParameters + ProjectiveArithmetic,
216+
C: CurveArithmetic + JwkParameters,
217217
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
218218
FieldSize<C>: ModulusSize,
219219
{
@@ -225,7 +225,7 @@ where
225225
#[cfg(feature = "arithmetic")]
226226
impl<C> From<&SecretKey<C>> for JwkEcKey
227227
where
228-
C: Curve + JwkParameters + ProjectiveArithmetic,
228+
C: CurveArithmetic + JwkParameters,
229229
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
230230
FieldSize<C>: ModulusSize,
231231
{
@@ -241,7 +241,7 @@ where
241241
#[cfg(feature = "arithmetic")]
242242
impl<C> TryFrom<JwkEcKey> for PublicKey<C>
243243
where
244-
C: Curve + JwkParameters + ProjectiveArithmetic,
244+
C: CurveArithmetic + JwkParameters,
245245
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
246246
FieldSize<C>: ModulusSize,
247247
{
@@ -255,7 +255,7 @@ where
255255
#[cfg(feature = "arithmetic")]
256256
impl<C> TryFrom<&JwkEcKey> for PublicKey<C>
257257
where
258-
C: Curve + JwkParameters + ProjectiveArithmetic,
258+
C: CurveArithmetic + JwkParameters,
259259
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
260260
FieldSize<C>: ModulusSize,
261261
{
@@ -269,7 +269,7 @@ where
269269
#[cfg(feature = "arithmetic")]
270270
impl<C> From<PublicKey<C>> for JwkEcKey
271271
where
272-
C: Curve + JwkParameters + ProjectiveArithmetic,
272+
C: CurveArithmetic + JwkParameters,
273273
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
274274
FieldSize<C>: ModulusSize,
275275
{
@@ -281,7 +281,7 @@ where
281281
#[cfg(feature = "arithmetic")]
282282
impl<C> From<&PublicKey<C>> for JwkEcKey
283283
where
284-
C: Curve + JwkParameters + ProjectiveArithmetic,
284+
C: CurveArithmetic + JwkParameters,
285285
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
286286
FieldSize<C>: ModulusSize,
287287
{

elliptic-curve/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ pub use zeroize;
104104
#[cfg(feature = "arithmetic")]
105105
pub use {
106106
crate::{
107-
arithmetic::{
108-
AffineArithmetic, PrimeCurveArithmetic, ProjectiveArithmetic, ScalarArithmetic,
109-
},
107+
arithmetic::{CurveArithmetic, PrimeCurveArithmetic},
110108
public_key::PublicKey,
111109
scalar::{nonzero::NonZeroScalar, Scalar},
112110
},
@@ -175,12 +173,12 @@ pub type FieldBytes<C> = GenericArray<u8, FieldSize<C>>;
175173
/// Affine point type for a given curve with a [`ProjectiveArithmetic`]
176174
/// implementation.
177175
#[cfg(feature = "arithmetic")]
178-
pub type AffinePoint<C> = <C as AffineArithmetic>::AffinePoint;
176+
pub type AffinePoint<C> = <C as CurveArithmetic>::AffinePoint;
179177

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

185183
/// Elliptic curve parameters used by VOPRF.
186184
#[cfg(feature = "voprf")]

0 commit comments

Comments
 (0)