authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-15 08:55:48+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-16 22:35:27-07:00
log263c44473896597346bc244d82a2b436d7d2da02
treefd6989ebe7fb7c753ea6f3ccaacbac329198753d
parented558bfbaa737b187d894eddb8573cde15a3fb33

Move loop decrements into continuations

Suggested by @daurnimator

3 files changed, 3 insertions(+), 6 deletions(-)

lib/std/crypto/25519/curve25519.zig+1-2
......@@ -44,7 +44,7 @@ pub const Curve25519 = struct {
4444 var z3 = Fe.one;
4545 var swap: u8 = 0;
4646 var pos: usize = bits - 1;
47 while (true) {
47 while (true) : (pos -= 1) {
4848 const b = (s[pos / 8] >> @intCast(u3, pos & 7)) & 1;
4949 swap ^= b;
5050 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);
......@@ -68,7 +68,6 @@ pub const Curve25519 = struct {
6868 z3 = x1.mul(z2);
6969 z2 = tmp1.mul(tmp0);
7070 if (pos == 0) break;
71 pos -= 1;
7271 }
7372 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);
7473 z2 = z2.invert();
lib/std/crypto/25519/edwards25519.zig+1-2
......@@ -132,12 +132,11 @@ pub const Edwards25519 = struct {
132132 fn pcMul(pc: [16]Edwards25519, s: [32]u8) !Edwards25519 {
133133 var q = Edwards25519.identityElement();
134134 var pos: usize = 252;
135 while (true) {
135 while (true) : (pos -= 4) {
136136 q = q.dbl().dbl().dbl().dbl();
137137 const b = (s[pos / 8] >> @intCast(u3, pos & 7)) & 0xf;
138138 q = q.add(pcSelect(pc, b));
139139 if (pos == 0) break;
140 pos -= 4;
141140 }
142141 try q.rejectIdentity();
143142 return q;
lib/std/crypto/25519/scalar.zig+1-2
......@@ -116,13 +116,12 @@ pub fn rejectNonCanonical(s: [32]u8) !void {
116116 var c: u8 = 0;
117117 var n: u8 = 1;
118118 var i: usize = 31;
119 while (true) {
119 while (true) : (i -= 1) {
120120 const xs = @as(u16, s[i]);
121121 const xfield_size = @as(u16, field_size[i]);
122122 c |= @intCast(u8, ((xs -% xfield_size) >> 8) & n);
123123 n &= @intCast(u8, ((xs ^ xfield_size) -% 1) >> 8);
124124 if (i == 0) break;
125 i -= 1;
126125 }
127126 if (c == 0) {
128127 return error.NonCanonical;