authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-15 11:48:34+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-16 22:35:27-07:00
log5ab69633b712914cccdf2f08d717387864d6c4c7
tree85df3189ce60f0be3936abb4b665bc3c0e534cee
parentd86cde575239d4e38631d562fba8b4001d436ebd

Constify the ladder


2 files changed, 16 insertions(+), 23 deletions(-)

lib/std/crypto/25519/curve25519.zig+14-21
...@@ -43,28 +43,21 @@ pub const Curve25519 = struct {...@@ -43,28 +43,21 @@ pub const Curve25519 = struct {
43 var swap: u8 = 0;43 var swap: u8 = 0;
44 var pos: usize = bits - 1;44 var pos: usize = bits - 1;
45 while (true) : (pos -= 1) {45 while (true) : (pos -= 1) {
46 const b = (s[pos >> 3] >> @truncate(u3, pos)) & 1;46 const bit = (s[pos >> 3] >> @truncate(u3, pos)) & 1;
47 swap ^= b;47 swap ^= bit;
48 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);48 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);
49 swap = b;49 swap = bit;
50 var tmp0 = x3.sub(z3);50 const a = x2.add(z2);
51 var tmp1 = x2.sub(z2);51 const b = x2.sub(z2);
52 x2 = x2.add(z2);52 const aa = a.sq();
53 z2 = x3.add(z3);53 const bb = b.sq();
54 z3 = tmp0.mul(x2);54 x2 = aa.mul(bb);
55 z2 = z2.mul(tmp1);55 const e = aa.sub(bb);
56 tmp0 = tmp1.sq();56 const da = x3.sub(z3).mul(a);
57 tmp1 = x2.sq();57 const cb = x3.add(z3).mul(b);
58 x3 = z3.add(z2);58 x3 = da.add(cb).sq();
59 z2 = z3.sub(z2);59 z3 = x1.mul(da.sub(cb).sq());
60 x2 = tmp1.mul(tmp0);60 z2 = e.mul(bb.add(e.mul32(121666)));
61 tmp1 = tmp1.sub(tmp0);
62 z2 = z2.sq();
63 z3 = tmp1.mul32(121666);
64 x3 = x3.sq();
65 tmp0 = tmp0.add(z3);
66 z3 = x1.mul(z2);
67 z2 = tmp1.mul(tmp0);
68 if (pos == 0) break;61 if (pos == 0) break;
69 }62 }
70 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);63 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);
lib/std/crypto/25519/edwards25519.zig+2-2
...@@ -130,8 +130,8 @@ pub const Edwards25519 = struct {...@@ -130,8 +130,8 @@ pub const Edwards25519 = struct {
130 var pos: usize = 252;130 var pos: usize = 252;
131 while (true) : (pos -= 4) {131 while (true) : (pos -= 4) {
132 q = q.dbl().dbl().dbl().dbl();132 q = q.dbl().dbl().dbl().dbl();
133 const b = (s[pos >> 3] >> @truncate(u3, pos)) & 0xf;133 const bit = (s[pos >> 3] >> @truncate(u3, pos)) & 0xf;
134 q = q.add(pcSelect(pc, b));134 q = q.add(pcSelect(pc, bit));
135 if (pos == 0) break;135 if (pos == 0) break;
136 }136 }
137 try q.rejectIdentity();137 try q.rejectIdentity();