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 {...@@ -109,6 +109,11 @@ pub const Scalar = struct {
109 return n.fe.isZero();109 return n.fe.isZero();
110 }110 }
111111
112 /// Return true if the scalar is odd.
113 pub fn isOdd(n: Scalar) bool {
114 return n.fe.isOdd();
115 }
116
112 /// Return true if a and b are equivalent.117 /// Return true if a and b are equivalent.
113 pub fn equivalent(a: Scalar, b: Scalar) bool {118 pub fn equivalent(a: Scalar, b: Scalar) bool {
114 return a.fe.equivalent(b.fe);119 return a.fe.equivalent(b.fe);
lib/std/crypto/pcurves/p384/scalar.zig+5
...@@ -98,6 +98,11 @@ pub const Scalar = struct {...@@ -98,6 +98,11 @@ pub const Scalar = struct {
98 return n.fe.isZero();98 return n.fe.isZero();
99 }99 }
100100
101 /// Return true if the scalar is odd.
102 pub fn isOdd(n: Scalar) bool {
103 return n.fe.isOdd();
104 }
105
101 /// Return true if a and b are equivalent.106 /// Return true if a and b are equivalent.
102 pub fn equivalent(a: Scalar, b: Scalar) bool {107 pub fn equivalent(a: Scalar, b: Scalar) bool {
103 return a.fe.equivalent(b.fe);108 return a.fe.equivalent(b.fe);
lib/std/crypto/pcurves/secp256k1/scalar.zig+5
...@@ -109,6 +109,11 @@ pub const Scalar = struct {...@@ -109,6 +109,11 @@ pub const Scalar = struct {
109 return n.fe.isZero();109 return n.fe.isZero();
110 }110 }
111111
112 /// Return true if the scalar is odd.
113 pub fn isOdd(n: Scalar) bool {
114 return n.fe.isOdd();
115 }
116
112 /// Return true if a and b are equivalent.117 /// Return true if a and b are equivalent.
113 pub fn equivalent(a: Scalar, b: Scalar) bool {118 pub fn equivalent(a: Scalar, b: Scalar) bool {
114 return a.fe.equivalent(b.fe);119 return a.fe.equivalent(b.fe);
lib/std/crypto/pcurves/tests/p256.zig+6
...@@ -134,3 +134,9 @@ test "p256 scalar inverse" {...@@ -134,3 +134,9 @@ test "p256 scalar inverse" {
134 const inverse = scalar.invert();134 const inverse = scalar.invert();
135 try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));135 try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
136}136}
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" {...@@ -144,3 +144,9 @@ test "p384 scalar inverse" {
144 const sqr = try sq.sqrt();144 const sqr = try sq.sqrt();
145 try testing.expect(sqr.equivalent(scalar));145 try testing.expect(sqr.equivalent(scalar));
146}146}
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" {...@@ -135,3 +135,9 @@ test "secp256k1 scalar inverse" {
135 const inverse = scalar.invert();135 const inverse = scalar.invert();
136 try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));136 try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
137}137}
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}