| ... | @@ -34,67 +34,52 @@ const ChaCha20VecImpl = struct { | ... | @@ -34,67 +34,52 @@ const ChaCha20VecImpl = struct { |
| 34 | }; | 34 | }; |
| 35 | } | 35 | } |
| 36 | | 36 | |
| 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 | } |
| 40 | | 40 | |
| | 41 | inline fn chacha20Core(x: *BlockVec, input: BlockVec) void { |
| 41 | x.* = input; | 42 | x.* = input; |
| 42 | | 43 | |
| 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); |
| 48 | | 49 | |
| 49 | x[2] +%= x[3]; | 50 | x[2] +%= x[3]; |
| 50 | x[1] ^= x[2]; | 51 | x[1] ^= x[2]; |
| 51 | | 52 | 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; | | |
| 56 | | 53 | |
| 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); |
| 61 | | 58 | |
| 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 }); |
| 66 | | 63 | 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; | | |
| 71 | | 64 | |
| 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); |
| 75 | | 68 | |
| 76 | x[2] +%= x[3]; | 69 | x[2] +%= x[3]; |
| 77 | x[1] ^= x[2]; | 70 | x[1] ^= x[2]; |
| 78 | | 71 | 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; | | |
| 83 | | 72 | |
| 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); |
| 88 | | 77 | |
| 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 }); |
| 93 | | 82 | 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 | } |
| 100 | | 85 | |