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 {...@@ -44,7 +44,7 @@ pub const Curve25519 = struct {
44 var z3 = Fe.one;44 var z3 = Fe.one;
45 var swap: u8 = 0;45 var swap: u8 = 0;
46 var pos: usize = bits - 1;46 var pos: usize = bits - 1;
47 while (true) {47 while (true) : (pos -= 1) {
48 const b = (s[pos / 8] >> @intCast(u3, pos & 7)) & 1;48 const b = (s[pos / 8] >> @intCast(u3, pos & 7)) & 1;
49 swap ^= b;49 swap ^= b;
50 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);50 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);
...@@ -68,7 +68,6 @@ pub const Curve25519 = struct {...@@ -68,7 +68,6 @@ pub const Curve25519 = struct {
68 z3 = x1.mul(z2);68 z3 = x1.mul(z2);
69 z2 = tmp1.mul(tmp0);69 z2 = tmp1.mul(tmp0);
70 if (pos == 0) break;70 if (pos == 0) break;
71 pos -= 1;
72 }71 }
73 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);72 Fe.cSwap2(&x2, &x3, &z2, &z3, swap);
74 z2 = z2.invert();73 z2 = z2.invert();
lib/std/crypto/25519/edwards25519.zig+1-2
...@@ -132,12 +132,11 @@ pub const Edwards25519 = struct {...@@ -132,12 +132,11 @@ pub const Edwards25519 = struct {
132 fn pcMul(pc: [16]Edwards25519, s: [32]u8) !Edwards25519 {132 fn pcMul(pc: [16]Edwards25519, s: [32]u8) !Edwards25519 {
133 var q = Edwards25519.identityElement();133 var q = Edwards25519.identityElement();
134 var pos: usize = 252;134 var pos: usize = 252;
135 while (true) {135 while (true) : (pos -= 4) {
136 q = q.dbl().dbl().dbl().dbl();136 q = q.dbl().dbl().dbl().dbl();
137 const b = (s[pos / 8] >> @intCast(u3, pos & 7)) & 0xf;137 const b = (s[pos / 8] >> @intCast(u3, pos & 7)) & 0xf;
138 q = q.add(pcSelect(pc, b));138 q = q.add(pcSelect(pc, b));
139 if (pos == 0) break;139 if (pos == 0) break;
140 pos -= 4;
141 }140 }
142 try q.rejectIdentity();141 try q.rejectIdentity();
143 return q;142 return q;
lib/std/crypto/25519/scalar.zig+1-2
...@@ -116,13 +116,12 @@ pub fn rejectNonCanonical(s: [32]u8) !void {...@@ -116,13 +116,12 @@ pub fn rejectNonCanonical(s: [32]u8) !void {
116 var c: u8 = 0;116 var c: u8 = 0;
117 var n: u8 = 1;117 var n: u8 = 1;
118 var i: usize = 31;118 var i: usize = 31;
119 while (true) {119 while (true) : (i -= 1) {
120 const xs = @as(u16, s[i]);120 const xs = @as(u16, s[i]);
121 const xfield_size = @as(u16, field_size[i]);121 const xfield_size = @as(u16, field_size[i]);
122 c |= @intCast(u8, ((xs -% xfield_size) >> 8) & n);122 c |= @intCast(u8, ((xs -% xfield_size) >> 8) & n);
123 n &= @intCast(u8, ((xs ^ xfield_size) -% 1) >> 8);123 n &= @intCast(u8, ((xs ^ xfield_size) -% 1) >> 8);
124 if (i == 0) break;124 if (i == 0) break;
125 i -= 1;
126 }125 }
127 if (c == 0) {126 if (c == 0) {
128 return error.NonCanonical;127 return error.NonCanonical;