Skip to content

Commit a8c113e

Browse files
authored
k256+primeorder: organize group trait impls (#1401)
- Creates a logical comment-delimited section of the source for `group` trait impls - Alphabetizes trait impls by name - Adds some explanatory comments
1 parent 6243a35 commit a8c113e

File tree

2 files changed

+115
-96
lines changed

2 files changed

+115
-96
lines changed

k256/src/arithmetic/projective.rs

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ use elliptic_curve::{
1515
cofactor::CofactorGroup,
1616
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
1717
},
18+
ops::BatchInvert,
19+
point::NonIdentity,
1820
rand_core::TryRngCore,
1921
sec1::{FromEncodedPoint, ToEncodedPoint},
2022
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
2123
zeroize::DefaultIsZeroes,
2224
};
23-
use elliptic_curve::{ops::BatchInvert, point::NonIdentity};
2425

2526
#[cfg(feature = "alloc")]
2627
use alloc::vec::Vec;
@@ -387,6 +388,16 @@ impl ConstantTimeEq for ProjectivePoint {
387388
}
388389
}
389390

391+
impl Default for ProjectivePoint {
392+
fn default() -> Self {
393+
Self::IDENTITY
394+
}
395+
}
396+
397+
impl DefaultIsZeroes for ProjectivePoint {}
398+
399+
impl Eq for ProjectivePoint {}
400+
390401
impl PartialEq for ProjectivePoint {
391402
fn eq(&self, other: &Self) -> bool {
392403
self.ct_eq(other).into()
@@ -405,7 +416,42 @@ impl PartialEq<ProjectivePoint> for AffinePoint {
405416
}
406417
}
407418

408-
impl Eq for ProjectivePoint {}
419+
//
420+
// `group` trait impls
421+
//
422+
423+
/// secp256k1 has a cofactor of 1.
424+
impl CofactorGroup for ProjectivePoint {
425+
type Subgroup = Self;
426+
427+
fn clear_cofactor(&self) -> Self::Subgroup {
428+
*self
429+
}
430+
431+
fn into_subgroup(self) -> CtOption<Self::Subgroup> {
432+
CtOption::new(self, Choice::from(1))
433+
}
434+
435+
fn is_torsion_free(&self) -> Choice {
436+
Choice::from(1)
437+
}
438+
}
439+
440+
impl CurveGroup for ProjectivePoint {
441+
type AffineRepr = AffinePoint;
442+
443+
fn to_affine(&self) -> AffinePoint {
444+
ProjectivePoint::to_affine(self)
445+
}
446+
447+
#[cfg(feature = "alloc")]
448+
#[inline]
449+
fn batch_normalize(projective: &[Self], affine: &mut [Self::AffineRepr]) {
450+
assert_eq!(projective.len(), affine.len());
451+
let mut zs = vec![FieldElement::ONE; projective.len()];
452+
batch_normalize_generic(projective, zs.as_mut_slice(), affine);
453+
}
454+
}
409455

410456
impl Group for ProjectivePoint {
411457
type Scalar = Scalar;
@@ -452,51 +498,15 @@ impl GroupEncoding for ProjectivePoint {
452498
}
453499
}
454500

455-
impl CofactorGroup for ProjectivePoint {
456-
type Subgroup = ProjectivePoint;
457-
458-
fn clear_cofactor(&self) -> Self::Subgroup {
459-
*self
460-
}
461-
462-
fn into_subgroup(self) -> CtOption<Self::Subgroup> {
463-
CtOption::new(self, Choice::from(1))
464-
}
465-
466-
fn is_torsion_free(&self) -> Choice {
467-
Choice::from(1)
468-
}
469-
}
470-
471-
impl PrimeGroup for ProjectivePoint {}
472-
473-
impl CurveGroup for ProjectivePoint {
474-
type AffineRepr = AffinePoint;
475-
476-
fn to_affine(&self) -> AffinePoint {
477-
ProjectivePoint::to_affine(self)
478-
}
479-
480-
#[cfg(feature = "alloc")]
481-
#[inline]
482-
fn batch_normalize(projective: &[Self], affine: &mut [Self::AffineRepr]) {
483-
assert_eq!(projective.len(), affine.len());
484-
let mut zs = vec![FieldElement::ONE; projective.len()];
485-
batch_normalize_generic(projective, zs.as_mut_slice(), affine);
486-
}
487-
}
488-
489501
impl PrimeCurve for ProjectivePoint {
490502
type Affine = AffinePoint;
491503
}
492504

493-
impl Default for ProjectivePoint {
494-
fn default() -> Self {
495-
Self::IDENTITY
496-
}
497-
}
505+
impl PrimeGroup for ProjectivePoint {}
498506

499-
impl DefaultIsZeroes for ProjectivePoint {}
507+
//
508+
// `core::ops` trait impls
509+
//
500510

501511
impl Add<&ProjectivePoint> for &ProjectivePoint {
502512
type Output = ProjectivePoint;

primeorder/src/projective.rs

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,55 @@ where
222222
}
223223
}
224224

225+
//
226+
// `group` trait impls
227+
//
228+
229+
/// Prime order elliptic curves have a cofactor of 1.
230+
impl<C> CofactorGroup for ProjectivePoint<C>
231+
where
232+
C: PrimeCurveParams,
233+
CompressedPoint<C>: Send + Sync,
234+
FieldBytes<C>: Copy,
235+
FieldBytesSize<C>: ModulusSize,
236+
CompressedPoint<C>: Copy,
237+
<UncompressedPointSize<C> as ArraySize>::ArrayType<u8>: Copy,
238+
{
239+
type Subgroup = ProjectivePoint<C>;
240+
241+
fn clear_cofactor(&self) -> Self::Subgroup {
242+
*self
243+
}
244+
245+
fn into_subgroup(self) -> CtOption<Self::Subgroup> {
246+
CtOption::new(self, Choice::from(1))
247+
}
248+
249+
fn is_torsion_free(&self) -> Choice {
250+
Choice::from(1)
251+
}
252+
}
253+
254+
impl<C> CurveGroup for ProjectivePoint<C>
255+
where
256+
C: PrimeCurveParams,
257+
FieldBytes<C>: Copy,
258+
{
259+
type AffineRepr = AffinePoint<C>;
260+
261+
fn to_affine(&self) -> AffinePoint<C> {
262+
ProjectivePoint::to_affine(self)
263+
}
264+
265+
#[cfg(feature = "alloc")]
266+
#[inline]
267+
fn batch_normalize(projective: &[Self], affine: &mut [Self::AffineRepr]) {
268+
assert_eq!(projective.len(), affine.len());
269+
let mut zs = vec![C::FieldElement::ONE; projective.len()];
270+
batch_normalize_generic(projective, zs.as_mut_slice(), affine);
271+
}
272+
}
273+
225274
impl<C> Group for ProjectivePoint<C>
226275
where
227276
C: PrimeCurveParams,
@@ -275,50 +324,33 @@ where
275324
}
276325
}
277326

278-
impl<C> CofactorGroup for ProjectivePoint<C>
327+
impl<C> PrimeCurve for ProjectivePoint<C>
279328
where
329+
Self: Double,
280330
C: PrimeCurveParams,
281-
CompressedPoint<C>: Send + Sync,
331+
CompressedPoint<C>: Copy + Send + Sync,
282332
FieldBytes<C>: Copy,
283333
FieldBytesSize<C>: ModulusSize,
284-
CompressedPoint<C>: Copy,
285334
<UncompressedPointSize<C> as ArraySize>::ArrayType<u8>: Copy,
286335
{
287-
type Subgroup = ProjectivePoint<C>;
288-
289-
fn clear_cofactor(&self) -> Self::Subgroup {
290-
*self
291-
}
292-
293-
fn into_subgroup(self) -> CtOption<Self::Subgroup> {
294-
CtOption::new(self, Choice::from(1))
295-
}
296-
297-
fn is_torsion_free(&self) -> Choice {
298-
Choice::from(1)
299-
}
336+
type Affine = AffinePoint<C>;
300337
}
301338

302-
impl<C> CurveGroup for ProjectivePoint<C>
339+
impl<C> PrimeGroup for ProjectivePoint<C>
303340
where
341+
Self: Double,
304342
C: PrimeCurveParams,
343+
CompressedPoint<C>: Copy + Send + Sync,
305344
FieldBytes<C>: Copy,
345+
FieldBytesSize<C>: ModulusSize,
346+
<UncompressedPointSize<C> as ArraySize>::ArrayType<u8>: Copy,
306347
{
307-
type AffineRepr = AffinePoint<C>;
308-
309-
fn to_affine(&self) -> AffinePoint<C> {
310-
ProjectivePoint::to_affine(self)
311-
}
312-
313-
#[cfg(feature = "alloc")]
314-
#[inline]
315-
fn batch_normalize(projective: &[Self], affine: &mut [Self::AffineRepr]) {
316-
assert_eq!(projective.len(), affine.len());
317-
let mut zs = vec![C::FieldElement::ONE; projective.len()];
318-
batch_normalize_generic(projective, zs.as_mut_slice(), affine);
319-
}
320348
}
321349

350+
//
351+
// Batch trait impls
352+
//
353+
322354
impl<const N: usize, C> BatchNormalize<[ProjectivePoint<C>; N]> for ProjectivePoint<C>
323355
where
324356
C: PrimeCurveParams,
@@ -481,29 +513,6 @@ impl<C: PrimeCurveParams> LookupTable<C> {
481513
}
482514
}
483515

484-
impl<C> PrimeGroup for ProjectivePoint<C>
485-
where
486-
Self: Double,
487-
C: PrimeCurveParams,
488-
CompressedPoint<C>: Copy + Send + Sync,
489-
FieldBytes<C>: Copy,
490-
FieldBytesSize<C>: ModulusSize,
491-
<UncompressedPointSize<C> as ArraySize>::ArrayType<u8>: Copy,
492-
{
493-
}
494-
495-
impl<C> PrimeCurve for ProjectivePoint<C>
496-
where
497-
Self: Double,
498-
C: PrimeCurveParams,
499-
CompressedPoint<C>: Copy + Send + Sync,
500-
FieldBytes<C>: Copy,
501-
FieldBytesSize<C>: ModulusSize,
502-
<UncompressedPointSize<C> as ArraySize>::ArrayType<u8>: Copy,
503-
{
504-
type Affine = AffinePoint<C>;
505-
}
506-
507516
impl<C> PartialEq for ProjectivePoint<C>
508517
where
509518
C: PrimeCurveParams,
@@ -560,7 +569,7 @@ where
560569
}
561570

562571
//
563-
// Arithmetic trait impls
572+
// `core::ops` trait impls
564573
//
565574

566575
impl<C> Add<ProjectivePoint<C>> for ProjectivePoint<C>

0 commit comments

Comments
 (0)