std.crypto: expose Fe isOdd & add basic parity tests for each pcurve (#15734)

* std Secp256k1 Scalar: expose Fe isOdd & add basic parity test

* std.crypto: also add Scalar.isOdd convenience fn for p256 and p384 curves
This commit is contained in:
Chris Heyes 2023-05-21 12:00:48 +01:00 committed by GitHub
parent b7cb88384c
commit df909da5d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 0 deletions

View File

@ -109,6 +109,11 @@ pub const Scalar = struct {
return n.fe.isZero();
}
/// Return true if the scalar is odd.
pub fn isOdd(n: Scalar) bool {
return n.fe.isOdd();
}
/// Return true if a and b are equivalent.
pub fn equivalent(a: Scalar, b: Scalar) bool {
return a.fe.equivalent(b.fe);

View File

@ -98,6 +98,11 @@ pub const Scalar = struct {
return n.fe.isZero();
}
/// Return true if the scalar is odd.
pub fn isOdd(n: Scalar) bool {
return n.fe.isOdd();
}
/// Return true if a and b are equivalent.
pub fn equivalent(a: Scalar, b: Scalar) bool {
return a.fe.equivalent(b.fe);

View File

@ -109,6 +109,11 @@ pub const Scalar = struct {
return n.fe.isZero();
}
/// Return true if the scalar is odd.
pub fn isOdd(n: Scalar) bool {
return n.fe.isOdd();
}
/// Return true if a and b are equivalent.
pub fn equivalent(a: Scalar, b: Scalar) bool {
return a.fe.equivalent(b.fe);

View File

@ -134,3 +134,9 @@ test "p256 scalar inverse" {
const inverse = scalar.invert();
try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
}
test "p256 scalar parity" {
try std.testing.expect(P256.scalar.Scalar.zero.isOdd() == false);
try std.testing.expect(P256.scalar.Scalar.one.isOdd());
try std.testing.expect(P256.scalar.Scalar.one.dbl().isOdd() == false);
}

View File

@ -144,3 +144,9 @@ test "p384 scalar inverse" {
const sqr = try sq.sqrt();
try testing.expect(sqr.equivalent(scalar));
}
test "p384 scalar parity" {
try std.testing.expect(P384.scalar.Scalar.zero.isOdd() == false);
try std.testing.expect(P384.scalar.Scalar.one.isOdd());
try std.testing.expect(P384.scalar.Scalar.one.dbl().isOdd() == false);
}

View File

@ -135,3 +135,9 @@ test "secp256k1 scalar inverse" {
const inverse = scalar.invert();
try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
}
test "secp256k1 scalar parity" {
try std.testing.expect(Secp256k1.scalar.Scalar.zero.isOdd() == false);
try std.testing.expect(Secp256k1.scalar.Scalar.one.isOdd());
try std.testing.expect(Secp256k1.scalar.Scalar.one.dbl().isOdd() == false);
}