authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2023-06-13 20:20:24+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-06-13 18:20:24+00:00
logcc708b4a880e0077c3fb0a077a8a39104701dc9c
tree833750ae7ff4cd1fba4ab99f647d6538943762f1
parent137b115681c1ca205df27c70422c42460b5aa6ec
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

crypto.pcurves: don't assume that points with X=0 are at infinity (#16017)

There's also a valid point with X=0 on each curves. Fixes #16015

3 files changed, 18 insertions(+), 6 deletions(-)

lib/std/crypto/pcurves/p256.zig+6-2
......@@ -36,7 +36,9 @@ pub const P256 = struct {
3636
3737 /// Reject the neutral element.
3838 pub fn rejectIdentity(p: P256) IdentityElementError!void {
39 if (p.x.isZero()) {
39 const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
40 const is_identity = @boolToInt(p.z.isZero()) | affine_0;
41 if (is_identity != 0) {
4042 return error.IdentityElement;
4143 }
4244 }
......@@ -286,12 +288,14 @@ pub const P256 = struct {
286288
287289 /// Return affine coordinates.
288290 pub fn affineCoordinates(p: P256) AffineCoordinates {
291 const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
292 const is_identity = @boolToInt(p.z.isZero()) | affine_0;
289293 const zinv = p.z.invert();
290294 var ret = AffineCoordinates{
291295 .x = p.x.mul(zinv),
292296 .y = p.y.mul(zinv),
293297 };
294 ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero()));
298 ret.cMov(AffineCoordinates.identityElement, is_identity);
295299 return ret;
296300 }
297301
lib/std/crypto/pcurves/p384.zig+6-2
......@@ -36,7 +36,9 @@ pub const P384 = struct {
3636
3737 /// Reject the neutral element.
3838 pub fn rejectIdentity(p: P384) IdentityElementError!void {
39 if (p.x.isZero()) {
39 const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
40 const is_identity = @boolToInt(p.z.isZero()) | affine_0;
41 if (is_identity != 0) {
4042 return error.IdentityElement;
4143 }
4244 }
......@@ -286,12 +288,14 @@ pub const P384 = struct {
286288
287289 /// Return affine coordinates.
288290 pub fn affineCoordinates(p: P384) AffineCoordinates {
291 const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
292 const is_identity = @boolToInt(p.z.isZero()) | affine_0;
289293 const zinv = p.z.invert();
290294 var ret = AffineCoordinates{
291295 .x = p.x.mul(zinv),
292296 .y = p.y.mul(zinv),
293297 };
294 ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero()));
298 ret.cMov(AffineCoordinates.identityElement, is_identity);
295299 return ret;
296300 }
297301
lib/std/crypto/pcurves/secp256k1.zig+6-2
......@@ -89,7 +89,9 @@ pub const Secp256k1 = struct {
8989
9090 /// Reject the neutral element.
9191 pub fn rejectIdentity(p: Secp256k1) IdentityElementError!void {
92 if (p.x.isZero()) {
92 const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
93 const is_identity = @boolToInt(p.z.isZero()) | affine_0;
94 if (is_identity != 0) {
9395 return error.IdentityElement;
9496 }
9597 }
......@@ -314,12 +316,14 @@ pub const Secp256k1 = struct {
314316
315317 /// Return affine coordinates.
316318 pub fn affineCoordinates(p: Secp256k1) AffineCoordinates {
319 const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
320 const is_identity = @boolToInt(p.z.isZero()) | affine_0;
317321 const zinv = p.z.invert();
318322 var ret = AffineCoordinates{
319323 .x = p.x.mul(zinv),
320324 .y = p.y.mul(zinv),
321325 };
322 ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero()));
326 ret.cMov(AffineCoordinates.identityElement, is_identity);
323327 return ret;
324328 }
325329