Skip to content

Commit

Permalink
k256+primeorder: use batch_normalize_generic for trait impl (#976)
Browse files Browse the repository at this point in the history
Uses the generic function to impl `group::Curve::batch_normalize`
  • Loading branch information
tarcieri authored Nov 15, 2023
1 parent 0007c64 commit ee94d8d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
16 changes: 10 additions & 6 deletions k256/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ impl BatchNormalize<[ProjectivePoint]> for ProjectivePoint {
type Output = Vec<Self::AffineRepr>;

fn batch_normalize(points: &[Self]) -> Vec<Self::AffineRepr> {
let mut zs: Vec<_> = vec![FieldElement::ONE; points.len()];
let mut affine_points: Vec<_> = vec![AffinePoint::IDENTITY; points.len()];
let mut zs = vec![FieldElement::ONE; points.len()];
let mut affine_points = vec![AffinePoint::IDENTITY; points.len()];
batch_normalize_generic(points, zs.as_mut_slice(), &mut affine_points);
affine_points
}
Expand Down Expand Up @@ -449,11 +449,15 @@ impl Curve for ProjectivePoint {
}

#[cfg(feature = "alloc")]
fn batch_normalize(p: &[Self], q: &mut [Self::AffineRepr]) {
assert_eq!(p.len(), q.len());
fn batch_normalize(projective: &[Self], affine: &mut [Self::AffineRepr]) {
assert_eq!(projective.len(), affine.len());

let affine_points: Vec<_> = <Self as BatchNormalize<_>>::batch_normalize(p);
q.copy_from_slice(&affine_points);
for point in affine.iter_mut() {
*point = AffinePoint::IDENTITY;
}

let mut zs = vec![FieldElement::ONE; projective.len()];
batch_normalize_generic(projective, zs.as_mut_slice(), affine);
}
}

Expand Down
18 changes: 11 additions & 7 deletions primeorder/src/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,15 @@ where
}

#[cfg(feature = "alloc")]
fn batch_normalize(p: &[Self], q: &mut [Self::AffineRepr]) {
assert_eq!(p.len(), q.len());
fn batch_normalize(projective: &[Self], affine: &mut [Self::AffineRepr]) {
assert_eq!(projective.len(), affine.len());

let affine_points: Vec<_> = <Self as BatchNormalize>::batch_normalize_vec(p);
q.copy_from_slice(affine_points)
for point in affine.iter_mut() {
*point = AffinePoint::IDENTITY;
}

let mut zs = vec![FieldElement::ONE; projective.len()];
batch_normalize_generic(projective, zs.as_mut_slice(), affine);
}
}

Expand Down Expand Up @@ -363,8 +367,8 @@ where
type Output = Vec<Self::AffineRepr>;

fn batch_normalize(points: &[Self; N]) -> Vec<Self::AffineRepr> {
let mut zs: Vec<_> = vec![C::FieldElement::ONE; points.len()];
let mut affine_points: Vec<_> = vec![AffinePoint::IDENTITY; points.len()];
let mut zs = vec![C::FieldElement::ONE; points.len()];
let mut affine_points = vec![AffinePoint::IDENTITY; points.len()];
batch_normalize_generic(points, zs.as_mut_slice(), &mut affine_points);
affine_points
}
Expand All @@ -375,7 +379,7 @@ fn batch_normalize_generic<C, P, Z, O>(points: &P, zs: &mut Z, out: &mut O)
where
C: PrimeCurveParams,
C::FieldElement: BatchInvert<Z>,
ProjectivePoint<C>: Double,
C::ProjectivePoint: Double,
P: AsRef<[ProjectivePoint<C>]> + ?Sized,
Z: AsMut<[C::FieldElement]> + ?Sized,
O: AsMut<[AffinePoint<C>]> + ?Sized,
Expand Down

0 comments on commit ee94d8d

Please sign in to comment.