authorgravatar for 22148308+hazeycode@users.noreply.github.comChris Heyes <22148308+hazeycode@users.noreply.github.com> 2023-05-21 12:00:48+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-21 13:00:48+02:00
logdf909da5d87fda01245c564858522c6c57dd8a75
treea5e1c579a29af55a4c346cb3c9f4d9fc09e4c990
parentb7cb88384c39a15e3ce9dbeaa5a191786d186d60
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

6 files changed, 33 insertions(+), 0 deletions(-)

lib/std/crypto/pcurves/p256/scalar.zig+5
......@@ -109,6 +109,11 @@ pub const Scalar = struct {
109109 return n.fe.isZero();
110110 }
111111
112 /// Return true if the scalar is odd.
113 pub fn isOdd(n: Scalar) bool {
114 return n.fe.isOdd();
115 }
116
112117 /// Return true if a and b are equivalent.
113118 pub fn equivalent(a: Scalar, b: Scalar) bool {
114119 return a.fe.equivalent(b.fe);
lib/std/crypto/pcurves/p384/scalar.zig+5
......@@ -98,6 +98,11 @@ pub const Scalar = struct {
9898 return n.fe.isZero();
9999 }
100100
101 /// Return true if the scalar is odd.
102 pub fn isOdd(n: Scalar) bool {
103 return n.fe.isOdd();
104 }
105
101106 /// Return true if a and b are equivalent.
102107 pub fn equivalent(a: Scalar, b: Scalar) bool {
103108 return a.fe.equivalent(b.fe);
lib/std/crypto/pcurves/secp256k1/scalar.zig+5
......@@ -109,6 +109,11 @@ pub const Scalar = struct {
109109 return n.fe.isZero();
110110 }
111111
112 /// Return true if the scalar is odd.
113 pub fn isOdd(n: Scalar) bool {
114 return n.fe.isOdd();
115 }
116
112117 /// Return true if a and b are equivalent.
113118 pub fn equivalent(a: Scalar, b: Scalar) bool {
114119 return a.fe.equivalent(b.fe);
lib/std/crypto/pcurves/tests/p256.zig+6
......@@ -134,3 +134,9 @@ test "p256 scalar inverse" {
134134 const inverse = scalar.invert();
135135 try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
136136}
137
138test "p256 scalar parity" {
139 try std.testing.expect(P256.scalar.Scalar.zero.isOdd() == false);
140 try std.testing.expect(P256.scalar.Scalar.one.isOdd());
141 try std.testing.expect(P256.scalar.Scalar.one.dbl().isOdd() == false);
142}
lib/std/crypto/pcurves/tests/p384.zig+6
......@@ -144,3 +144,9 @@ test "p384 scalar inverse" {
144144 const sqr = try sq.sqrt();
145145 try testing.expect(sqr.equivalent(scalar));
146146}
147
148test "p384 scalar parity" {
149 try std.testing.expect(P384.scalar.Scalar.zero.isOdd() == false);
150 try std.testing.expect(P384.scalar.Scalar.one.isOdd());
151 try std.testing.expect(P384.scalar.Scalar.one.dbl().isOdd() == false);
152}
lib/std/crypto/pcurves/tests/secp256k1.zig+6
......@@ -135,3 +135,9 @@ test "secp256k1 scalar inverse" {
135135 const inverse = scalar.invert();
136136 try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
137137}
138
139test "secp256k1 scalar parity" {
140 try std.testing.expect(Secp256k1.scalar.Scalar.zero.isOdd() == false);
141 try std.testing.expect(Secp256k1.scalar.Scalar.one.isOdd());
142 try std.testing.expect(Secp256k1.scalar.Scalar.one.dbl().isOdd() == false);
143}