mirror of
https://github.com/ziglang/zig.git
synced 2025-12-16 03:03:09 +00:00
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:
parent
b7cb88384c
commit
df909da5d8
@ -109,6 +109,11 @@ pub const Scalar = struct {
|
|||||||
return n.fe.isZero();
|
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.
|
/// Return true if a and b are equivalent.
|
||||||
pub fn equivalent(a: Scalar, b: Scalar) bool {
|
pub fn equivalent(a: Scalar, b: Scalar) bool {
|
||||||
return a.fe.equivalent(b.fe);
|
return a.fe.equivalent(b.fe);
|
||||||
|
|||||||
@ -98,6 +98,11 @@ pub const Scalar = struct {
|
|||||||
return n.fe.isZero();
|
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.
|
/// Return true if a and b are equivalent.
|
||||||
pub fn equivalent(a: Scalar, b: Scalar) bool {
|
pub fn equivalent(a: Scalar, b: Scalar) bool {
|
||||||
return a.fe.equivalent(b.fe);
|
return a.fe.equivalent(b.fe);
|
||||||
|
|||||||
@ -109,6 +109,11 @@ pub const Scalar = struct {
|
|||||||
return n.fe.isZero();
|
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.
|
/// Return true if a and b are equivalent.
|
||||||
pub fn equivalent(a: Scalar, b: Scalar) bool {
|
pub fn equivalent(a: Scalar, b: Scalar) bool {
|
||||||
return a.fe.equivalent(b.fe);
|
return a.fe.equivalent(b.fe);
|
||||||
|
|||||||
@ -134,3 +134,9 @@ test "p256 scalar inverse" {
|
|||||||
const inverse = scalar.invert();
|
const inverse = scalar.invert();
|
||||||
try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@ -144,3 +144,9 @@ test "p384 scalar inverse" {
|
|||||||
const sqr = try sq.sqrt();
|
const sqr = try sq.sqrt();
|
||||||
try testing.expect(sqr.equivalent(scalar));
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@ -135,3 +135,9 @@ test "secp256k1 scalar inverse" {
|
|||||||
const inverse = scalar.invert();
|
const inverse = scalar.invert();
|
||||||
try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
|
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);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user