Skip to content

Commit c2d5947

Browse files
authored
k256: Batch Normalization normalizes_to_zero() bug fix (#1029)
1 parent 5d1c252 commit c2d5947

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

k256/src/arithmetic/projective.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ where
295295
// Even a single zero value will fail inversion for the entire batch.
296296
// Put a dummy value (above `FieldElement::ONE`) so inversion succeeds
297297
// and treat that case specially later-on.
298-
zs.as_mut()[i].conditional_assign(&points[i].z, !points[i].z.ct_eq(&FieldElement::ZERO));
298+
zs.as_mut()[i].conditional_assign(&points[i].z, !points[i].z.normalizes_to_zero());
299299
}
300300

301301
// This is safe to unwrap since we assured that all elements are non-zero
@@ -307,7 +307,7 @@ where
307307
out[i] = AffinePoint::conditional_select(
308308
&points[i].to_affine_internal(zs_inverses.as_ref()[i]),
309309
&AffinePoint::IDENTITY,
310-
points[i].z.ct_eq(&FieldElement::ZERO),
310+
points[i].z.normalizes_to_zero(),
311311
);
312312
}
313313
}
@@ -721,17 +721,20 @@ mod tests {
721721
<ProjectivePoint as group::Curve>::batch_normalize(&[g, h], &mut res);
722722
assert_eq!(res, expected);
723723

724-
let expected = [g.to_affine(), AffinePoint::IDENTITY];
724+
let mut res = [AffinePoint::IDENTITY; 3];
725+
let non_normalized_identity = ProjectivePoint::IDENTITY * Scalar::random(&mut OsRng);
726+
let expected = [g.to_affine(), AffinePoint::IDENTITY, AffinePoint::IDENTITY];
725727
assert_eq!(
726728
<ProjectivePoint as BatchNormalize<_>>::batch_normalize(&[
727729
g,
728-
ProjectivePoint::IDENTITY
730+
ProjectivePoint::IDENTITY,
731+
non_normalized_identity,
729732
]),
730733
expected
731734
);
732735

733736
<ProjectivePoint as group::Curve>::batch_normalize(
734-
&[g, ProjectivePoint::IDENTITY],
737+
&[g, ProjectivePoint::IDENTITY, non_normalized_identity],
735738
&mut res,
736739
);
737740
assert_eq!(res, expected);

0 commit comments

Comments
 (0)