Skip to content

Commit b6d5ba0

Browse files
committed
Update to changes in BatchNormalize
1 parent c6efb4e commit b6d5ba0

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ opt-level = 2
2222
[patch.crates-io]
2323
primefield = { path = "primefield" }
2424
primeorder = { path = "primeorder" }
25+
26+
# https://github.com/RustCrypto/traits/pull/1896
27+
elliptic-curve = { git = "https://github.com/RustCrypto/traits", rev = "21d93b8cc1ddd54398836abe5527794a523268a2" }

k256/src/arithmetic/projective.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ impl From<AffinePoint> for ProjectivePoint {
257257
}
258258

259259
impl<const N: usize> BatchNormalize<[ProjectivePoint; N]> for ProjectivePoint {
260-
type Output = [Self::AffineRepr; N];
260+
type Output = [<Self as Curve>::AffineRepr; N];
261261

262262
#[inline]
263-
fn batch_normalize(points: &[Self; N]) -> [Self::AffineRepr; N] {
263+
fn batch_normalize(points: &[Self; N]) -> [<Self as Curve>::AffineRepr; N] {
264264
let zs = [FieldElement::ONE; N];
265265
let mut affine_points = [AffinePoint::IDENTITY; N];
266266
batch_normalize_generic(points, zs, &mut affine_points);
@@ -270,10 +270,10 @@ impl<const N: usize> BatchNormalize<[ProjectivePoint; N]> for ProjectivePoint {
270270

271271
#[cfg(feature = "alloc")]
272272
impl BatchNormalize<[ProjectivePoint]> for ProjectivePoint {
273-
type Output = Vec<Self::AffineRepr>;
273+
type Output = Vec<<Self as Curve>::AffineRepr>;
274274

275275
#[inline]
276-
fn batch_normalize(points: &[Self]) -> Vec<Self::AffineRepr> {
276+
fn batch_normalize(points: &[Self]) -> Vec<<Self as Curve>::AffineRepr> {
277277
let zs = vec![FieldElement::ONE; points.len()];
278278
let mut affine_points = vec![AffinePoint::IDENTITY; points.len()];
279279
batch_normalize_generic(points, zs, &mut affine_points);

p256/tests/projective.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
#![cfg(all(feature = "arithmetic", feature = "test-vectors"))]
44

55
use elliptic_curve::{
6+
BatchNormalize,
67
group::{GroupEncoding, ff::PrimeField},
8+
ops::ReduceNonZero,
9+
point::NonIdentity,
710
sec1::{self, ToEncodedPoint},
811
};
912
use p256::{
10-
AffinePoint, ProjectivePoint, Scalar,
13+
AffinePoint, NonZeroScalar, ProjectivePoint, Scalar,
1114
test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS},
1215
};
1316
use primeorder::{Double, test_projective_arithmetic};
17+
use proptest::{prelude::any, prop_compose, proptest};
1418

1519
test_projective_arithmetic!(
1620
AffinePoint,
@@ -25,3 +29,41 @@ fn projective_identity_to_bytes() {
2529
// This is technically an invalid SEC1 encoding, but is preferable to panicking.
2630
assert_eq!([0; 33], ProjectivePoint::IDENTITY.to_bytes().as_slice());
2731
}
32+
33+
prop_compose! {
34+
fn non_identity()(bytes in any::<[u8; 32]>()) -> NonIdentity<ProjectivePoint> {
35+
NonIdentity::mul_by_generator(&NonZeroScalar::reduce_nonzero_bytes(&bytes.into()))
36+
}
37+
}
38+
39+
// TODO: move to `primeorder::test_projective_arithmetic`.
40+
proptest! {
41+
#[test]
42+
fn batch_normalize(
43+
a in non_identity(),
44+
b in non_identity(),
45+
) {
46+
let points: [NonIdentity<ProjectivePoint>; 2] = [a, b];
47+
48+
let affine_points = NonIdentity::batch_normalize(&points);
49+
50+
for (point, affine_point) in points.into_iter().zip(affine_points) {
51+
assert_eq!(affine_point, point.to_affine());
52+
}
53+
}
54+
55+
#[test]
56+
#[cfg(feature = "alloc")]
57+
fn batch_normalize_alloc(
58+
a in non_identity(),
59+
b in non_identity(),
60+
) {
61+
let points = vec![a, b];
62+
63+
let affine_points = NonIdentity::batch_normalize(points.as_slice());
64+
65+
for (point, affine_point) in points.into_iter().zip(affine_points) {
66+
assert_eq!(affine_point, point.to_affine());
67+
}
68+
}
69+
}

primeorder/src/projective.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use elliptic_curve::{
1313
array::ArraySize,
1414
bigint::ArrayEncoding,
1515
group::{
16-
self, Group, GroupEncoding,
16+
Curve, Group, GroupEncoding,
1717
prime::{PrimeCurve, PrimeGroup},
1818
},
1919
ops::{BatchInvert, LinearCombination},
@@ -308,7 +308,7 @@ where
308308
}
309309
}
310310

311-
impl<C> group::Curve for ProjectivePoint<C>
311+
impl<C> Curve for ProjectivePoint<C>
312312
where
313313
Self: Double,
314314
C: PrimeCurveParams,
@@ -333,10 +333,10 @@ where
333333
Self: Double,
334334
C: PrimeCurveParams,
335335
{
336-
type Output = [Self::AffineRepr; N];
336+
type Output = [<Self as Curve>::AffineRepr; N];
337337

338338
#[inline]
339-
fn batch_normalize(points: &[Self; N]) -> [Self::AffineRepr; N] {
339+
fn batch_normalize(points: &[Self; N]) -> [<Self as Curve>::AffineRepr; N] {
340340
let zs = [C::FieldElement::ONE; N];
341341
let mut affine_points = [C::AffinePoint::IDENTITY; N];
342342
batch_normalize_generic(points, zs, &mut affine_points);
@@ -350,10 +350,10 @@ where
350350
Self: Double,
351351
C: PrimeCurveParams,
352352
{
353-
type Output = Vec<Self::AffineRepr>;
353+
type Output = Vec<<Self as Curve>::AffineRepr>;
354354

355355
#[inline]
356-
fn batch_normalize(points: &[Self]) -> Vec<Self::AffineRepr> {
356+
fn batch_normalize(points: &[Self]) -> Vec<<Self as Curve>::AffineRepr> {
357357
let mut zs = vec![C::FieldElement::ONE; points.len()];
358358
let mut affine_points = vec![AffinePoint::IDENTITY; points.len()];
359359
batch_normalize_generic(points, zs.as_mut_slice(), &mut affine_points);

0 commit comments

Comments
 (0)