Skip to content

Commit

Permalink
crypto.pcurves: don't assume that points with X=0 are at infinity (#1…
Browse files Browse the repository at this point in the history
…6017)

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

Fixes #16015
  • Loading branch information
jedisct1 committed Jun 13, 2023
1 parent 137b115 commit cc708b4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/std/crypto/pcurves/p256.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub const P256 = struct {

/// Reject the neutral element.
pub fn rejectIdentity(p: P256) IdentityElementError!void {
if (p.x.isZero()) {
const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @boolToInt(p.z.isZero()) | affine_0;
if (is_identity != 0) {
return error.IdentityElement;
}
}
Expand Down Expand Up @@ -286,12 +288,14 @@ pub const P256 = struct {

/// Return affine coordinates.
pub fn affineCoordinates(p: P256) AffineCoordinates {
const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @boolToInt(p.z.isZero()) | affine_0;
const zinv = p.z.invert();
var ret = AffineCoordinates{
.x = p.x.mul(zinv),
.y = p.y.mul(zinv),
};
ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero()));
ret.cMov(AffineCoordinates.identityElement, is_identity);
return ret;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/std/crypto/pcurves/p384.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub const P384 = struct {

/// Reject the neutral element.
pub fn rejectIdentity(p: P384) IdentityElementError!void {
if (p.x.isZero()) {
const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @boolToInt(p.z.isZero()) | affine_0;
if (is_identity != 0) {
return error.IdentityElement;
}
}
Expand Down Expand Up @@ -286,12 +288,14 @@ pub const P384 = struct {

/// Return affine coordinates.
pub fn affineCoordinates(p: P384) AffineCoordinates {
const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @boolToInt(p.z.isZero()) | affine_0;
const zinv = p.z.invert();
var ret = AffineCoordinates{
.x = p.x.mul(zinv),
.y = p.y.mul(zinv),
};
ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero()));
ret.cMov(AffineCoordinates.identityElement, is_identity);
return ret;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/std/crypto/pcurves/secp256k1.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ pub const Secp256k1 = struct {

/// Reject the neutral element.
pub fn rejectIdentity(p: Secp256k1) IdentityElementError!void {
if (p.x.isZero()) {
const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @boolToInt(p.z.isZero()) | affine_0;
if (is_identity != 0) {
return error.IdentityElement;
}
}
Expand Down Expand Up @@ -314,12 +316,14 @@ pub const Secp256k1 = struct {

/// Return affine coordinates.
pub fn affineCoordinates(p: Secp256k1) AffineCoordinates {
const affine_0 = @boolToInt(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@boolToInt(p.y.isZero()) | @boolToInt(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @boolToInt(p.z.isZero()) | affine_0;
const zinv = p.z.invert();
var ret = AffineCoordinates{
.x = p.x.mul(zinv),
.y = p.y.mul(zinv),
};
ret.cMov(AffineCoordinates.identityElement, @boolToInt(p.x.isZero()));
ret.cMov(AffineCoordinates.identityElement, is_identity);
return ret;
}

Expand Down

0 comments on commit cc708b4

Please sign in to comment.