authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-10 21:12:24+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-10 22:45:41+02:00
log9f109ba0ebb4167a18a80d8ff0c173d574fc4577
tree6c5773c966f0ca5de89a1d0edc9cc79c2b9fc3d5
parent459128e05902008937205ea154e6c556e9264a4d

Simpler ChaCha20 vector code


1 files changed, 18 insertions(+), 33 deletions(-)

lib/std/crypto/chacha20.zig+18-33
...@@ -34,67 +34,52 @@ const ChaCha20VecImpl = struct {...@@ -34,67 +34,52 @@ const ChaCha20VecImpl = struct {
34 };34 };
35 }35 }
3636
37 inline fn chacha20Core(x: *BlockVec, input: BlockVec) void {37 inline fn rot(x: Lane, comptime n: comptime_int) Lane {
38 const rot8 = [_]i32{ 3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14 };38 return (x << @splat(4, @as(u5, n))) | (x >> @splat(4, @as(u5, 32 - n)));
39 const rot16 = [_]i32{ 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13 };39 }
4040
41 inline fn chacha20Core(x: *BlockVec, input: BlockVec) void {
41 x.* = input;42 x.* = input;
4243
43 var r: usize = 0;44 var r: usize = 0;
44 while (r < 20) : (r += 2) {45 while (r < 20) : (r += 2) {
45 x[0] +%= x[1];46 x[0] +%= x[1];
46 x[3] ^= x[0];47 x[3] ^= x[0];
47 x[3] = @bitCast(Vector(4, u32), @shuffle(u8, @bitCast(Vector(16, u8), x[3]), undefined, rot16));48 x[3] = rot(x[3], 16);
4849
49 x[2] +%= x[3];50 x[2] +%= x[3];
50 x[1] ^= x[2];51 x[1] ^= x[2];
5152 x[1] = rot(x[1], 12);
52 var t1 = x[1];
53 x[1] <<= @splat(4, @as(u5, 12));
54 t1 >>= @splat(4, @as(u5, 20));
55 x[1] ^= t1;
5653
57 x[0] +%= x[1];54 x[0] +%= x[1];
58 x[3] ^= x[0];55 x[3] ^= x[0];
59 x[0] = @shuffle(u32, x[0], undefined, Vector(4, i32){ 3, 0, 1, 2 });56 x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 3, 0, 1, 2 });
60 x[3] = @bitCast(Vector(4, u32), @shuffle(u8, @bitCast(Vector(16, u8), x[3]), undefined, rot8));57 x[3] = rot(x[3], 8);
6158
62 x[2] +%= x[3];59 x[2] +%= x[3];
63 x[3] = @shuffle(u32, x[3], undefined, Vector(4, i32){ 2, 3, 0, 1 });60 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });
64 x[1] ^= x[2];61 x[1] ^= x[2];
65 x[2] = @shuffle(u32, x[2], undefined, Vector(4, i32){ 1, 2, 3, 0 });62 x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 1, 2, 3, 0 });
6663 x[1] = rot(x[1], 7);
67 t1 = x[1];
68 x[1] <<= @splat(4, @as(u5, 7));
69 t1 >>= @splat(4, @as(u5, 25));
70 x[1] ^= t1;
7164
72 x[0] +%= x[1];65 x[0] +%= x[1];
73 x[3] ^= x[0];66 x[3] ^= x[0];
74 x[3] = @bitCast(Vector(4, u32), @shuffle(u8, @bitCast(Vector(16, u8), x[3]), undefined, rot16));67 x[3] = rot(x[3], 16);
7568
76 x[2] +%= x[3];69 x[2] +%= x[3];
77 x[1] ^= x[2];70 x[1] ^= x[2];
7871 x[1] = rot(x[1], 12);
79 t1 = x[1];
80 x[1] <<= @splat(4, @as(u5, 12));
81 t1 >>= @splat(4, @as(u5, 20));
82 x[1] ^= t1;
8372
84 x[0] +%= x[1];73 x[0] +%= x[1];
85 x[3] ^= x[0];74 x[3] ^= x[0];
86 x[0] = @shuffle(u32, x[0], undefined, Vector(4, i32){ 1, 2, 3, 0 });75 x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 1, 2, 3, 0 });
87 x[3] = @bitCast(Vector(4, u32), @shuffle(u8, @bitCast(Vector(16, u8), x[3]), undefined, rot8));76 x[3] = rot(x[3], 8);
8877
89 x[2] +%= x[3];78 x[2] +%= x[3];
90 x[3] = @shuffle(u32, x[3], undefined, Vector(4, i32){ 2, 3, 0, 1 });79 x[3] = @shuffle(u32, x[3], undefined, [_]i32{ 2, 3, 0, 1 });
91 x[1] ^= x[2];80 x[1] ^= x[2];
92 x[2] = @shuffle(u32, x[2], undefined, Vector(4, i32){ 3, 0, 1, 2 });81 x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 3, 0, 1, 2 });
9382 x[1] = rot(x[1], 7);
94 t1 = x[1];
95 x[1] <<= @splat(4, @as(u5, 7));
96 t1 >>= @splat(4, @as(u5, 25));
97 x[1] ^= t1;
98 }83 }
99 }84 }
10085