| author | |
| committer | |
| log | 4417206230913e3a2caab7df910a878a39bce0d0 |
| tree | fc81298c9672c4188309190a266ab216cb6d884e |
| parent | 34502b9c4dfe21bea388d538e9c3e69a724738fd |
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; |
| 68 | 68 | ||
| 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 | } |
| 81 | 77 | ||
| 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. Bernstein | 6 | // Based on public domain Supercop by Daniel J. Bernstein |
| 7 | 7 | ||
| 8 | const std = @import("../std.zig"); | 8 | const std = @import("../std.zig"); |
| 9 | const math = std.math; | ||
| 9 | const mem = std.mem; | 10 | const mem = std.mem; |
| 10 | const assert = std.debug.assert; | 11 | const assert = std.debug.assert; |
| 11 | const testing = std.testing; | 12 | const testing = std.testing; |
| 12 | const maxInt = std.math.maxInt; | 13 | const maxInt = math.maxInt; |
| 13 | const Vector = std.meta.Vector; | 14 | const Vector = std.meta.Vector; |
| 14 | const Poly1305 = std.crypto.onetimeauth.Poly1305; | 15 | const Poly1305 = std.crypto.onetimeauth.Poly1305; |
| 15 | 16 | ||
| ... | @@ -34,10 +35,6 @@ const ChaCha20VecImpl = struct { | ... | @@ -34,10 +35,6 @@ const ChaCha20VecImpl = struct { |
| 34 | }; | 35 | }; |
| 35 | } | 36 | } |
| 36 | 37 | ||
| 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; |
| 43 | 40 | ||
| ... | @@ -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); |
| 49 | 46 | ||
| 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); |
| 53 | 50 | ||
| 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); |
| 58 | 55 | ||
| 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); |
| 64 | 61 | ||
| 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); |
| 68 | 65 | ||
| 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); |
| 72 | 69 | ||
| 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); |
| 77 | 74 | ||
| 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 | } |
| 85 | 82 | ||
| ... | @@ -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 | } |
| 122 | 122 | ||
| 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 | } |
| 38 | 38 | ||
| 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); |
| 81 | 77 | ||
| 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; |
| 88 | 84 | ||
| 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); |
| 97 | 93 | ||
| 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 }); |