aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrank Denis <github@pureftpd.org>2020-09-28 23:23:32 +0200
committerFrank Denis <github@pureftpd.org>2020-09-29 00:29:20 +0200
commit868a46eb43e68971634c046c8317c1b83cae21ae (patch)
tree26d2b972569d5ef088cc380f242d77b1c093c576
parent5c6cd5e2c9e8b2d0feb0026bad7c201035a175b4 (diff)
downloadzig-868a46eb43e68971634c046c8317c1b83cae21ae.tar.gz
zig-868a46eb43e68971634c046c8317c1b83cae21ae.zip
std/crypto: make gimli slightly faster
Before: gimli-hash: 120 MiB/s gimli-aead: 130 MiB/s After: gimli-hash: 195 MiB/s gimli-aead: 208 MiB/s Also fixes in-place decryption by the way. If the input & output buffers were the same, decryption used to fail. Return on decryption error in the benchmark to detect similar issues in future AEADs even in non release-fast mode.
-rw-r--r--lib/std/crypto/benchmark.zig2
-rw-r--r--lib/std/crypto/gimli.zig34
2 files changed, 22 insertions, 14 deletions
diff --git a/lib/std/crypto/benchmark.zig b/lib/std/crypto/benchmark.zig
index 860f1269f0..4397f7312a 100644
--- a/lib/std/crypto/benchmark.zig
+++ b/lib/std/crypto/benchmark.zig
@@ -168,7 +168,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64
const start = timer.lap();
while (offset < bytes) : (offset += in.len) {
Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key);
- Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable;
+ try Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key);
}
mem.doNotOptimizeAway(&in);
const end = timer.read();
diff --git a/lib/std/crypto/gimli.zig b/lib/std/crypto/gimli.zig
index 5b572aad7d..e5f93f5833 100644
--- a/lib/std/crypto/gimli.zig
+++ b/lib/std/crypto/gimli.zig
@@ -40,8 +40,8 @@ pub const State = struct {
pub fn permute(self: *Self) void {
const state = &self.data;
- var round = @as(u32, 24);
- while (round > 0) : (round -= 1) {
+ comptime var round = @as(u32, 24);
+ inline while (round > 0) : (round -= 1) {
var column = @as(usize, 0);
while (column < 4) : (column += 1) {
const x = math.rotl(u32, state[column], 24);
@@ -249,15 +249,19 @@ pub const Aead = struct {
in = in[State.RATE..];
out = out[State.RATE..];
}) {
- for (buf[0..State.RATE]) |*p, i| {
- p.* ^= in[i];
- out[i] = p.*;
+ const d = in[0..State.RATE];
+ for (d) |v, i| {
+ buf[i] ^= v;
+ }
+ for (d) |_, i| {
+ out[i] = buf[i];
}
state.permute();
}
- for (buf[0..in.len]) |*p, i| {
- p.* ^= in[i];
- out[i] = p.*;
+ const d = in[0..];
+ for (d) |v, i| {
+ buf[i] ^= v;
+ out[i] = buf[i];
}
// XOR 1 into the next byte of the state
@@ -291,15 +295,19 @@ pub const Aead = struct {
in = in[State.RATE..];
out = out[State.RATE..];
}) {
- for (buf[0..State.RATE]) |*p, i| {
- out[i] = p.* ^ in[i];
- p.* = in[i];
+ const d = in[0..State.RATE].*;
+ for (d) |v, i| {
+ out[i] = buf[i] ^ v;
+ }
+ for (d) |v, i| {
+ buf[i] = v;
}
state.permute();
}
for (buf[0..in.len]) |*p, i| {
- out[i] = p.* ^ in[i];
- p.* = in[i];
+ const d = in[i];
+ out[i] = p.* ^ d;
+ p.* = d;
}
// XOR 1 into the next byte of the state