| author | |
| committer | |
| log | cc708b4a880e0077c3fb0a077a8a39104701dc9c |
| tree | 833750ae7ff4cd1fba4ab99f647d6538943762f1 |
| parent | 137b115681c1ca205df27c70422c42460b5aa6ec |
| signature |
There's also a valid point with X=0 on each curves.
Fixes #160153 files changed, 18 insertions(+), 6 deletions(-)
lib/std/crypto/pcurves/p256.zig+6-2| ... | ... | @@ -36,7 +36,9 @@ pub const P256 = struct { |
| 36 | 36 | |
| 37 | 37 | /// Reject the neutral element. |
| 38 | 38 | 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) { | |
| 40 | 42 | return error.IdentityElement; |
| 41 | 43 | } |
| 42 | 44 | } |
| ... | ... | @@ -286,12 +288,14 @@ pub const P256 = struct { |
| 286 | 288 | |
| 287 | 289 | /// Return affine coordinates. |
| 288 | 290 | 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; | |
| 289 | 293 | const zinv = p.z.invert(); |
| 290 | 294 | var ret = AffineCoordinates{ |
| 291 | 295 | .x = p.x.mul(zinv), |
| 292 | 296 | .y = p.y.mul(zinv), |
| 293 | 297 | }; |
| 294 | ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero())); | |
| 298 | ret.cMov(AffineCoordinates.identityElement, is_identity); | |
| 295 | 299 | return ret; |
| 296 | 300 | } |
| 297 | 301 |
lib/std/crypto/pcurves/p384.zig+6-2| ... | ... | @@ -36,7 +36,9 @@ pub const P384 = struct { |
| 36 | 36 | |
| 37 | 37 | /// Reject the neutral element. |
| 38 | 38 | 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) { | |
| 40 | 42 | return error.IdentityElement; |
| 41 | 43 | } |
| 42 | 44 | } |
| ... | ... | @@ -286,12 +288,14 @@ pub const P384 = struct { |
| 286 | 288 | |
| 287 | 289 | /// Return affine coordinates. |
| 288 | 290 | 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; | |
| 289 | 293 | const zinv = p.z.invert(); |
| 290 | 294 | var ret = AffineCoordinates{ |
| 291 | 295 | .x = p.x.mul(zinv), |
| 292 | 296 | .y = p.y.mul(zinv), |
| 293 | 297 | }; |
| 294 | ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero())); | |
| 298 | ret.cMov(AffineCoordinates.identityElement, is_identity); | |
| 295 | 299 | return ret; |
| 296 | 300 | } |
| 297 | 301 |
lib/std/crypto/pcurves/secp256k1.zig+6-2| ... | ... | @@ -89,7 +89,9 @@ pub const Secp256k1 = struct { |
| 89 | 89 | |
| 90 | 90 | /// Reject the neutral element. |
| 91 | 91 | 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) { | |
| 93 | 95 | return error.IdentityElement; |
| 94 | 96 | } |
| 95 | 97 | } |
| ... | ... | @@ -314,12 +316,14 @@ pub const Secp256k1 = struct { |
| 314 | 316 | |
| 315 | 317 | /// Return affine coordinates. |
| 316 | 318 | 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; | |
| 317 | 321 | const zinv = p.z.invert(); |
| 318 | 322 | var ret = AffineCoordinates{ |
| 319 | 323 | .x = p.x.mul(zinv), |
| 320 | 324 | .y = p.y.mul(zinv), |
| 321 | 325 | }; |
| 322 | ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero())); | |
| 326 | ret.cMov(AffineCoordinates.identityElement, is_identity); | |
| 323 | 327 | return ret; |
| 324 | 328 | } |
| 325 | 329 |