authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-09-10 15:41:15+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-09-10 15:41:15+02:00
log9e24727062624ec51eadb833d3c434b15889a9c3
tree3e9d59a023e5785779db820bc105372994e6f661
parent93c6e31cf174c61e1c2e7c0bf53d9a09b3d91734
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Fix compile error for p256 scalar arithmetic (#9715)


2 files changed, 15 insertions(+), 2 deletions(-)

lib/std/crypto/pcurves/p256/p256_scalar_64.zig+2-2
...@@ -1766,7 +1766,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[...@@ -1766,7 +1766,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
1766 var x1: u64 = undefined;1766 var x1: u64 = undefined;
1767 var x2: u1 = undefined;1767 var x2: u1 = undefined;
1768 addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1));1768 addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1));
1769 const x3 = (@as(u1, (x1 >> 63)) & @as(u1, ((arg3[0]) & @as(u64, 0x1))));1769 const x3 = @truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & @as(u64, 0x1)));
1770 var x4: u64 = undefined;1770 var x4: u64 = undefined;
1771 var x5: u1 = undefined;1771 var x5: u1 = undefined;
1772 addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1));1772 addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1));
...@@ -1880,7 +1880,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[...@@ -1880,7 +1880,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
1880 cmovznzU64(&x72, x3, (arg5[2]), x66);1880 cmovznzU64(&x72, x3, (arg5[2]), x66);
1881 var x73: u64 = undefined;1881 var x73: u64 = undefined;
1882 cmovznzU64(&x73, x3, (arg5[3]), x68);1882 cmovznzU64(&x73, x3, (arg5[3]), x68);
1883 const x74 = @as(u1, (x22 & @as(u64, 0x1)));1883 const x74 = @truncate(u1, (x22 & @as(u64, 0x1)));
1884 var x75: u64 = undefined;1884 var x75: u64 = undefined;
1885 cmovznzU64(&x75, x74, @as(u64, 0x0), x7);1885 cmovznzU64(&x75, x74, @as(u64, 0x0), x7);
1886 var x76: u64 = undefined;1886 var x76: u64 = undefined;
lib/std/crypto/pcurves/tests.zig+13
...@@ -111,3 +111,16 @@ test "p256 double base multiplication" {...@@ -111,3 +111,16 @@ test "p256 double base multiplication" {
111 const pr2 = (try p1.mul(s1, .Little)).add(try p2.mul(s2, .Little));111 const pr2 = (try p1.mul(s1, .Little)).add(try p2.mul(s2, .Little));
112 try testing.expect(pr1.equivalent(pr2));112 try testing.expect(pr1.equivalent(pr2));
113}113}
114
115test "p256 scalar inverse" {
116 const expected = "3b549196a13c898a6f6e84dfb3a22c40a8b9b17fb88e408ea674e451cd01d0a6";
117 var out: [32]u8 = undefined;
118 _ = try std.fmt.hexToBytes(&out, expected);
119
120 const scalar = try P256.scalar.Scalar.fromBytes(.{
121 0x94, 0xa1, 0xbb, 0xb1, 0x4b, 0x90, 0x6a, 0x61, 0xa2, 0x80, 0xf2, 0x45, 0xf9, 0xe9, 0x3c, 0x7f,
122 0x3b, 0x4a, 0x62, 0x47, 0x82, 0x4f, 0x5d, 0x33, 0xb9, 0x67, 0x07, 0x87, 0x64, 0x2a, 0x68, 0xde,
123 }, .Big);
124 const inverse = scalar.invert();
125 try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big));
126}