| ... | ... | @@ -34,67 +34,52 @@ const ChaCha20VecImpl = struct { |
| 34 | 34 | }; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | | inline fn chacha20Core(x: *BlockVec, input: BlockVec) void { |
| 38 | | const rot8 = [_]i32{ 3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14 }; |
| 39 | | const rot16 = [_]i32{ 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13 }; |
| 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 | 40 | |
| 41 | inline fn chacha20Core(x: *BlockVec, input: BlockVec) void { |
| 41 | 42 | x.* = input; |
| 42 | 43 | |
| 43 | 44 | var r: usize = 0; |
| 44 | 45 | while (r < 20) : (r += 2) { |
| 45 | 46 | x[0] +%= x[1]; |
| 46 | 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); |
| 48 | 49 | |
| 49 | 50 | x[2] +%= x[3]; |
| 50 | 51 | x[1] ^= x[2]; |
| 51 | | |
| 52 | | var t1 = x[1]; |
| 53 | | x[1] <<= @splat(4, @as(u5, 12)); |
| 54 | | t1 >>= @splat(4, @as(u5, 20)); |
| 55 | | x[1] ^= t1; |
| 52 | x[1] = rot(x[1], 12); |
| 56 | 53 | |
| 57 | 54 | x[0] +%= x[1]; |
| 58 | 55 | x[3] ^= x[0]; |
| 59 | | x[0] = @shuffle(u32, x[0], undefined, Vector(4, i32){ 3, 0, 1, 2 }); |
| 60 | | x[3] = @bitCast(Vector(4, u32), @shuffle(u8, @bitCast(Vector(16, u8), x[3]), undefined, rot8)); |
| 56 | x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 3, 0, 1, 2 }); |
| 57 | x[3] = rot(x[3], 8); |
| 61 | 58 | |
| 62 | 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 | 61 | x[1] ^= x[2]; |
| 65 | | x[2] = @shuffle(u32, x[2], undefined, Vector(4, i32){ 1, 2, 3, 0 }); |
| 66 | | |
| 67 | | t1 = x[1]; |
| 68 | | x[1] <<= @splat(4, @as(u5, 7)); |
| 69 | | t1 >>= @splat(4, @as(u5, 25)); |
| 70 | | x[1] ^= t1; |
| 62 | x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 1, 2, 3, 0 }); |
| 63 | x[1] = rot(x[1], 7); |
| 71 | 64 | |
| 72 | 65 | x[0] +%= x[1]; |
| 73 | 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); |
| 75 | 68 | |
| 76 | 69 | x[2] +%= x[3]; |
| 77 | 70 | x[1] ^= x[2]; |
| 78 | | |
| 79 | | t1 = x[1]; |
| 80 | | x[1] <<= @splat(4, @as(u5, 12)); |
| 81 | | t1 >>= @splat(4, @as(u5, 20)); |
| 82 | | x[1] ^= t1; |
| 71 | x[1] = rot(x[1], 12); |
| 83 | 72 | |
| 84 | 73 | x[0] +%= x[1]; |
| 85 | 74 | x[3] ^= x[0]; |
| 86 | | x[0] = @shuffle(u32, x[0], undefined, Vector(4, i32){ 1, 2, 3, 0 }); |
| 87 | | x[3] = @bitCast(Vector(4, u32), @shuffle(u8, @bitCast(Vector(16, u8), x[3]), undefined, rot8)); |
| 75 | x[0] = @shuffle(u32, x[0], undefined, [_]i32{ 1, 2, 3, 0 }); |
| 76 | x[3] = rot(x[3], 8); |
| 88 | 77 | |
| 89 | 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 | 80 | x[1] ^= x[2]; |
| 92 | | x[2] = @shuffle(u32, x[2], undefined, Vector(4, i32){ 3, 0, 1, 2 }); |
| 93 | | |
| 94 | | t1 = x[1]; |
| 95 | | x[1] <<= @splat(4, @as(u5, 7)); |
| 96 | | t1 >>= @splat(4, @as(u5, 25)); |
| 97 | | x[1] ^= t1; |
| 81 | x[2] = @shuffle(u32, x[2], undefined, [_]i32{ 3, 0, 1, 2 }); |
| 82 | x[1] = rot(x[1], 7); |
| 98 | 83 | } |
| 99 | 84 | } |
| 100 | 85 | |