authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-11-02 21:56:45+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-05 17:19:48-05:00
log4417206230913e3a2caab7df910a878a39bce0d0
treefc81298c9672c4188309190a266ab216cb6d884e
parent34502b9c4dfe21bea388d538e9c3e69a724738fd

Now that they support vectors, use math.rot{l,r}


4 files changed, 26 insertions(+), 41 deletions(-)

lib/std/crypto/blake3.zig+2-6
......@@ -66,17 +66,13 @@ const CompressVectorized = struct {
6666 const Lane = Vector(4, u32);
6767 const Rows = [4]Lane;
6868
69 inline fn rot(x: Lane, comptime n: u5) Lane {
70 return (x >> @splat(4, @as(u5, n))) | (x << @splat(4, @as(u5, 1 +% ~n)));
71 }
72
7369 inline fn g(comptime even: bool, rows: *Rows, m: Lane) void {
7470 rows[0] +%= rows[1] +% m;
7571 rows[3] ^= rows[0];
76 rows[3] = rot(rows[3], if (even) 8 else 16);
72 rows[3] = math.rotr(Lane, rows[3], if (even) 8 else 16);
7773 rows[2] +%= rows[3];
7874 rows[1] ^= rows[2];
79 rows[1] = rot(rows[1], if (even) 7 else 12);
75 rows[1] = math.rotr(Lane, rows[1], if (even) 7 else 12);
8076 }
8177
8278 inline fn diagonalize(rows: *Rows) void {
lib/std/crypto/chacha20.zig+14-17
......@@ -6,10 +6,11 @@
66// Based on public domain Supercop by Daniel J. Bernstein
77
88const std = @import("../std.zig");
9const math = std.math;
910const mem = std.mem;
1011const assert = std.debug.assert;
1112const testing = std.testing;
12const maxInt = std.math.maxInt;
13const maxInt = math.maxInt;
1314const Vector = std.meta.Vector;
1415const Poly1305 = std.crypto.onetimeauth.Poly1305;
1516
......@@ -34,10 +35,6 @@ const ChaCha20VecImpl = struct {
3435 };
3536 }
3637
37 inline fn rot(x: Lane, comptime n: comptime_int) Lane {
38 return (x << @splat(4, @as(u5, n))) | (x >> @splat(4, @as(u5, 32 - n)));
39 }
40
4138 inline fn chacha20Core(x: *BlockVec, input: BlockVec) void {
4239 x.* = input;
4340
......@@ -45,41 +42,41 @@ const ChaCha20VecImpl = struct {
4542 while (r < 20) : (r += 2) {
4643 x[0] +%= x[1];
4744 x[3] ^= x[0];
48 x[3] = rot(x[3], 16);
45 x[3] = math.rotl(Lane, x[3], 16);
4946
5047 x[2] +%= x[3];
5148 x[1] ^= x[2];
52 x[1] = rot(x[1], 12);
49 x[1] = math.rotl(Lane, x[1], 12);
5350
5451 x[0] +%= x[1];
5552 x[3] ^= x[0];
5653 x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 3, 0, 1, 2 });
57 x[3] = rot(x[3], 8);
54 x[3] = math.rotl(Lane, x[3], 8);
5855
5956 x[2] +%= x[3];
6057 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });
6158 x[1] ^= x[2];
6259 x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 1, 2, 3, 0 });
63 x[1] = rot(x[1], 7);
60 x[1] = math.rotl(Lane, x[1], 7);
6461
6562 x[0] +%= x[1];
6663 x[3] ^= x[0];
67 x[3] = rot(x[3], 16);
64 x[3] = math.rotl(Lane, x[3], 16);
6865
6966 x[2] +%= x[3];
7067 x[1] ^= x[2];
71 x[1] = rot(x[1], 12);
68 x[1] = math.rotl(Lane, x[1], 12);
7269
7370 x[0] +%= x[1];
7471 x[3] ^= x[0];
7572 x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 1, 2, 3, 0 });
76 x[3] = rot(x[3], 8);
73 x[3] = math.rotl(Lane, x[3], 8);
7774
7875 x[2] +%= x[3];
7976 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });
8077 x[1] ^= x[2];
8178 x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 3, 0, 1, 2 });
82 x[1] = rot(x[1], 7);
79 x[1] = math.rotl(Lane, x[1], 7);
8380 }
8481 }
8582
......@@ -211,13 +208,13 @@ const ChaCha20NonVecImpl = struct {
211208 inline while (j < 20) : (j += 2) {
212209 inline for (rounds) |r| {
213210 x[r.a] +%= x[r.b];
214 x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], @as(u32, 16));
211 x[r.d] = math.rotl(u32, x[r.d] ^ x[r.a], @as(u32, 16));
215212 x[r.c] +%= x[r.d];
216 x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], @as(u32, 12));
213 x[r.b] = math.rotl(u32, x[r.b] ^ x[r.c], @as(u32, 12));
217214 x[r.a] +%= x[r.b];
218 x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], @as(u32, 8));
215 x[r.d] = math.rotl(u32, x[r.d] ^ x[r.a], @as(u32, 8));
219216 x[r.c] +%= x[r.d];
220 x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], @as(u32, 7));
217 x[r.b] = math.rotl(u32, x[r.b] ^ x[r.c], @as(u32, 7));
221218 }
222219 }
223220 }
lib/std/crypto/gimli.zig+2-6
......@@ -120,10 +120,6 @@ pub const State = struct {
120120 return x << @splat(4, @as(u5, n));
121121 }
122122
123 inline fn rot(x: Lane, comptime n: comptime_int) Lane {
124 return (x << @splat(4, @as(u5, n))) | (x >> @splat(4, @as(u5, 32 - n)));
125 }
126
127123 fn permute_vectorized(self: *Self) void {
128124 self.endianSwap();
129125 const state = &self.data;
......@@ -132,8 +128,8 @@ pub const State = struct {
132128 var z = Lane{ state[8], state[9], state[10], state[11] };
133129 var round = @as(u32, 24);
134130 while (round > 0) : (round -= 1) {
135 x = rot(x, 24);
136 y = rot(y, 9);
131 x = math.rotl(Lane, x, 24);
132 y = math.rotl(Lane, y, 9);
137133 const newz = x ^ shift(z, 1) ^ shift(y & z, 2);
138134 const newy = y ^ x ^ shift(x | z, 1);
139135 const newx = z ^ y ^ shift(x & y, 3);
lib/std/crypto/salsa20.zig+8-12
......@@ -36,10 +36,6 @@ const Salsa20VecImpl = struct {
3636 };
3737 }
3838
39 inline fn rot(x: Lane, comptime n: u5) Lane {
40 return (x << @splat(4, @as(u5, n))) | (x >> @splat(4, @as(u5, 1 +% ~n)));
41 }
42
4339 inline fn salsa20Core(x: *BlockVec, input: BlockVec, comptime feedback: bool) void {
4440 const n1n2n3n0 = Lane{ input[3][1], input[3][2], input[3][3], input[3][0] };
4541 const n1n2 = Half{ n1n2n3n0[0], n1n2n3n0[1] };
......@@ -71,13 +67,13 @@ const Salsa20VecImpl = struct {
7167 var i: usize = 0;
7268 while (i < 20) : (i += 2) {
7369 var a0 = diag1 +% diag0;
74 diag3 ^= rot(a0, 7);
70 diag3 ^= math.rotl(Lane, a0, 7);
7571 var a1 = diag0 +% diag3;
76 diag2 ^= rot(a1, 9);
72 diag2 ^= math.rotl(Lane, a1, 9);
7773 var a2 = diag3 +% diag2;
78 diag1 ^= rot(a2, 13);
74 diag1 ^= math.rotl(Lane, a2, 13);
7975 var a3 = diag2 +% diag1;
80 diag0 ^= rot(a3, 18);
76 diag0 ^= math.rotl(Lane, a3, 18);
8177
8278 var diag3_shift = @shuffle(u32, diag3, undefined, [_]i32{ 3, 0, 1, 2 });
8379 var diag2_shift = @shuffle(u32, diag2, undefined, [_]i32{ 2, 3, 0, 1 });
......@@ -87,13 +83,13 @@ const Salsa20VecImpl = struct {
8783 diag1 = diag1_shift;
8884
8985 a0 = diag3 +% diag0;
90 diag1 ^= rot(a0, 7);
86 diag1 ^= math.rotl(Lane, a0, 7);
9187 a1 = diag0 +% diag1;
92 diag2 ^= rot(a1, 9);
88 diag2 ^= math.rotl(Lane, a1, 9);
9389 a2 = diag1 +% diag2;
94 diag3 ^= rot(a2, 13);
90 diag3 ^= math.rotl(Lane, a2, 13);
9591 a3 = diag2 +% diag3;
96 diag0 ^= rot(a3, 18);
92 diag0 ^= math.rotl(Lane, a3, 18);
9793
9894 diag1_shift = @shuffle(u32, diag1, undefined, [_]i32{ 3, 0, 1, 2 });
9995 diag2_shift = @shuffle(u32, diag2, undefined, [_]i32{ 2, 3, 0, 1 });