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 {...@@ -66,17 +66,13 @@ const CompressVectorized = struct {
66 const Lane = Vector(4, u32);66 const Lane = Vector(4, u32);
67 const Rows = [4]Lane;67 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
73 inline fn g(comptime even: bool, rows: *Rows, m: Lane) void {69 inline fn g(comptime even: bool, rows: *Rows, m: Lane) void {
74 rows[0] +%= rows[1] +% m;70 rows[0] +%= rows[1] +% m;
75 rows[3] ^= rows[0];71 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);
77 rows[2] +%= rows[3];73 rows[2] +%= rows[3];
78 rows[1] ^= rows[2];74 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);
80 }76 }
8177
82 inline fn diagonalize(rows: *Rows) void {78 inline fn diagonalize(rows: *Rows) void {
lib/std/crypto/chacha20.zig+14-17
...@@ -6,10 +6,11 @@...@@ -6,10 +6,11 @@
6// Based on public domain Supercop by Daniel J. Bernstein6// Based on public domain Supercop by Daniel J. Bernstein
77
8const std = @import("../std.zig");8const std = @import("../std.zig");
9const math = std.math;
9const mem = std.mem;10const mem = std.mem;
10const assert = std.debug.assert;11const assert = std.debug.assert;
11const testing = std.testing;12const testing = std.testing;
12const maxInt = std.math.maxInt;13const maxInt = math.maxInt;
13const Vector = std.meta.Vector;14const Vector = std.meta.Vector;
14const Poly1305 = std.crypto.onetimeauth.Poly1305;15const Poly1305 = std.crypto.onetimeauth.Poly1305;
1516
...@@ -34,10 +35,6 @@ const ChaCha20VecImpl = struct {...@@ -34,10 +35,6 @@ const ChaCha20VecImpl = struct {
34 };35 };
35 }36 }
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
41 inline fn chacha20Core(x: *BlockVec, input: BlockVec) void {38 inline fn chacha20Core(x: *BlockVec, input: BlockVec) void {
42 x.* = input;39 x.* = input;
4340
...@@ -45,41 +42,41 @@ const ChaCha20VecImpl = struct {...@@ -45,41 +42,41 @@ const ChaCha20VecImpl = struct {
45 while (r < 20) : (r += 2) {42 while (r < 20) : (r += 2) {
46 x[0] +%= x[1];43 x[0] +%= x[1];
47 x[3] ^= x[0];44 x[3] ^= x[0];
48 x[3] = rot(x[3], 16);45 x[3] = math.rotl(Lane, x[3], 16);
4946
50 x[2] +%= x[3];47 x[2] +%= x[3];
51 x[1] ^= x[2];48 x[1] ^= x[2];
52 x[1] = rot(x[1], 12);49 x[1] = math.rotl(Lane, x[1], 12);
5350
54 x[0] +%= x[1];51 x[0] +%= x[1];
55 x[3] ^= x[0];52 x[3] ^= x[0];
56 x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 3, 0, 1, 2 });53 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
59 x[2] +%= x[3];56 x[2] +%= x[3];
60 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });57 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });
61 x[1] ^= x[2];58 x[1] ^= x[2];
62 x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 1, 2, 3, 0 });59 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
65 x[0] +%= x[1];62 x[0] +%= x[1];
66 x[3] ^= x[0];63 x[3] ^= x[0];
67 x[3] = rot(x[3], 16);64 x[3] = math.rotl(Lane, x[3], 16);
6865
69 x[2] +%= x[3];66 x[2] +%= x[3];
70 x[1] ^= x[2];67 x[1] ^= x[2];
71 x[1] = rot(x[1], 12);68 x[1] = math.rotl(Lane, x[1], 12);
7269
73 x[0] +%= x[1];70 x[0] +%= x[1];
74 x[3] ^= x[0];71 x[3] ^= x[0];
75 x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 1, 2, 3, 0 });72 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
78 x[2] +%= x[3];75 x[2] +%= x[3];
79 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });76 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });
80 x[1] ^= x[2];77 x[1] ^= x[2];
81 x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 3, 0, 1, 2 });78 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);
83 }80 }
84 }81 }
8582
...@@ -211,13 +208,13 @@ const ChaCha20NonVecImpl = struct {...@@ -211,13 +208,13 @@ const ChaCha20NonVecImpl = struct {
211 inline while (j < 20) : (j += 2) {208 inline while (j < 20) : (j += 2) {
212 inline for (rounds) |r| {209 inline for (rounds) |r| {
213 x[r.a] +%= x[r.b];210 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));
215 x[r.c] +%= x[r.d];212 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));
217 x[r.a] +%= x[r.b];214 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));
219 x[r.c] +%= x[r.d];216 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));
221 }218 }
222 }219 }
223 }220 }
lib/std/crypto/gimli.zig+2-6
...@@ -120,10 +120,6 @@ pub const State = struct {...@@ -120,10 +120,6 @@ pub const State = struct {
120 return x << @splat(4, @as(u5, n));120 return x << @splat(4, @as(u5, n));
121 }121 }
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
127 fn permute_vectorized(self: *Self) void {123 fn permute_vectorized(self: *Self) void {
128 self.endianSwap();124 self.endianSwap();
129 const state = &self.data;125 const state = &self.data;
...@@ -132,8 +128,8 @@ pub const State = struct {...@@ -132,8 +128,8 @@ pub const State = struct {
132 var z = Lane{ state[8], state[9], state[10], state[11] };128 var z = Lane{ state[8], state[9], state[10], state[11] };
133 var round = @as(u32, 24);129 var round = @as(u32, 24);
134 while (round > 0) : (round -= 1) {130 while (round > 0) : (round -= 1) {
135 x = rot(x, 24);131 x = math.rotl(Lane, x, 24);
136 y = rot(y, 9);132 y = math.rotl(Lane, y, 9);
137 const newz = x ^ shift(z, 1) ^ shift(y & z, 2);133 const newz = x ^ shift(z, 1) ^ shift(y & z, 2);
138 const newy = y ^ x ^ shift(x | z, 1);134 const newy = y ^ x ^ shift(x | z, 1);
139 const newx = z ^ y ^ shift(x & y, 3);135 const newx = z ^ y ^ shift(x & y, 3);
lib/std/crypto/salsa20.zig+8-12
...@@ -36,10 +36,6 @@ const Salsa20VecImpl = struct {...@@ -36,10 +36,6 @@ const Salsa20VecImpl = struct {
36 };36 };
37 }37 }
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
43 inline fn salsa20Core(x: *BlockVec, input: BlockVec, comptime feedback: bool) void {39 inline fn salsa20Core(x: *BlockVec, input: BlockVec, comptime feedback: bool) void {
44 const n1n2n3n0 = Lane{ input[3][1], input[3][2], input[3][3], input[3][0] };40 const n1n2n3n0 = Lane{ input[3][1], input[3][2], input[3][3], input[3][0] };
45 const n1n2 = Half{ n1n2n3n0[0], n1n2n3n0[1] };41 const n1n2 = Half{ n1n2n3n0[0], n1n2n3n0[1] };
...@@ -71,13 +67,13 @@ const Salsa20VecImpl = struct {...@@ -71,13 +67,13 @@ const Salsa20VecImpl = struct {
71 var i: usize = 0;67 var i: usize = 0;
72 while (i < 20) : (i += 2) {68 while (i < 20) : (i += 2) {
73 var a0 = diag1 +% diag0;69 var a0 = diag1 +% diag0;
74 diag3 ^= rot(a0, 7);70 diag3 ^= math.rotl(Lane, a0, 7);
75 var a1 = diag0 +% diag3;71 var a1 = diag0 +% diag3;
76 diag2 ^= rot(a1, 9);72 diag2 ^= math.rotl(Lane, a1, 9);
77 var a2 = diag3 +% diag2;73 var a2 = diag3 +% diag2;
78 diag1 ^= rot(a2, 13);74 diag1 ^= math.rotl(Lane, a2, 13);
79 var a3 = diag2 +% diag1;75 var a3 = diag2 +% diag1;
80 diag0 ^= rot(a3, 18);76 diag0 ^= math.rotl(Lane, a3, 18);
8177
82 var diag3_shift = @shuffle(u32, diag3, undefined, [_]i32{ 3, 0, 1, 2 });78 var diag3_shift = @shuffle(u32, diag3, undefined, [_]i32{ 3, 0, 1, 2 });
83 var diag2_shift = @shuffle(u32, diag2, undefined, [_]i32{ 2, 3, 0, 1 });79 var diag2_shift = @shuffle(u32, diag2, undefined, [_]i32{ 2, 3, 0, 1 });
...@@ -87,13 +83,13 @@ const Salsa20VecImpl = struct {...@@ -87,13 +83,13 @@ const Salsa20VecImpl = struct {
87 diag1 = diag1_shift;83 diag1 = diag1_shift;
8884
89 a0 = diag3 +% diag0;85 a0 = diag3 +% diag0;
90 diag1 ^= rot(a0, 7);86 diag1 ^= math.rotl(Lane, a0, 7);
91 a1 = diag0 +% diag1;87 a1 = diag0 +% diag1;
92 diag2 ^= rot(a1, 9);88 diag2 ^= math.rotl(Lane, a1, 9);
93 a2 = diag1 +% diag2;89 a2 = diag1 +% diag2;
94 diag3 ^= rot(a2, 13);90 diag3 ^= math.rotl(Lane, a2, 13);
95 a3 = diag2 +% diag3;91 a3 = diag2 +% diag3;
96 diag0 ^= rot(a3, 18);92 diag0 ^= math.rotl(Lane, a3, 18);
9793
98 diag1_shift = @shuffle(u32, diag1, undefined, [_]i32{ 3, 0, 1, 2 });94 diag1_shift = @shuffle(u32, diag1, undefined, [_]i32{ 3, 0, 1, 2 });
99 diag2_shift = @shuffle(u32, diag2, undefined, [_]i32{ 2, 3, 0, 1 });95 diag2_shift = @shuffle(u32, diag2, undefined, [_]i32{ 2, 3, 0, 1 });