aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFrank Denis <github@pureftpd.org>2020-09-29 13:09:11 +0200
committerFrank Denis <github@pureftpd.org>2020-09-29 13:23:04 +0200
commit4194714965a8080e6faa87d9859cc90aab07fe54 (patch)
tree77b478cb8ae4d41297c89255395858e78cd1612a /lib
parent613f8fe83fc2db4bc39f18ad1a8190d33a4a1181 (diff)
downloadzig-4194714965a8080e6faa87d9859cc90aab07fe54.tar.gz
zig-4194714965a8080e6faa87d9859cc90aab07fe54.zip
Don't unroll the gimli permutation on release-small
Diffstat (limited to 'lib')
-rw-r--r--lib/std/crypto/gimli.zig38
1 files changed, 37 insertions, 1 deletions
diff --git a/lib/std/crypto/gimli.zig b/lib/std/crypto/gimli.zig
index 181aa1ed53..847562e395 100644
--- a/lib/std/crypto/gimli.zig
+++ b/lib/std/crypto/gimli.zig
@@ -38,7 +38,7 @@ pub const State = struct {
return mem.sliceAsBytes(self.data[0..]);
}
- pub fn permute(self: *Self) void {
+ fn _permute_unrolled(self: *Self) void {
const state = &self.data;
comptime var round = @as(u32, 24);
inline while (round > 0) : (round -= 1) {
@@ -66,6 +66,42 @@ pub const State = struct {
}
}
+ fn _permute_small(self: *Self) void {
+ const state = &self.data;
+ var round = @as(u32, 24);
+ while (round > 0) : (round -= 1) {
+ var column = @as(usize, 0);
+ while (column < 4) : (column += 1) {
+ const x = math.rotl(u32, state[column], 24);
+ const y = math.rotl(u32, state[4 + column], 9);
+ const z = state[8 + column];
+ state[8 + column] = ((x ^ (z << 1)) ^ ((y & z) << 2));
+ state[4 + column] = ((y ^ x) ^ ((x | z) << 1));
+ state[column] = ((z ^ y) ^ ((x & y) << 3));
+ }
+ switch (round & 3) {
+ 0 => {
+ mem.swap(u32, &state[0], &state[1]);
+ mem.swap(u32, &state[2], &state[3]);
+ state[0] ^= round | 0x9e377900;
+ },
+ 2 => {
+ mem.swap(u32, &state[0], &state[2]);
+ mem.swap(u32, &state[1], &state[3]);
+ },
+ else => {},
+ }
+ }
+ }
+
+ pub fn permute(self: *Self) void {
+ if (std.builtin.mode == .ReleaseSmall) {
+ self._permute_small();
+ } else {
+ self._permute_unrolled();
+ }
+ }
+
pub fn squeeze(self: *Self, out: []u8) void {
var i = @as(usize, 0);
while (i + RATE <= out.len) : (i += RATE) {