diff --git a/std/crypto/blake2.zig b/std/crypto/blake2.zig index b08caa480e..7c943c752a 100644 --- a/std/crypto/blake2.zig +++ b/std/crypto/blake2.zig @@ -21,6 +21,8 @@ pub const Blake2s256 = Blake2s(256); fn Blake2s(comptime out_len: usize) -> type { return struct { const Self = this; + const block_size = 64; + const digest_size = out_len / 8; const iv = [8]u32 { 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, @@ -236,6 +238,8 @@ pub const Blake2b512 = Blake2b(512); fn Blake2b(comptime out_len: usize) -> type { return struct { const Self = this; + const block_size = 128; + const digest_size = out_len / 8; const iv = [8]u64 { 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, diff --git a/std/crypto/md5.zig b/std/crypto/md5.zig index 02f1954380..b0c40a8424 100644 --- a/std/crypto/md5.zig +++ b/std/crypto/md5.zig @@ -14,14 +14,10 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) -> Round return RoundParam { .a = a, .b = b, .c = c, .d = d, .k = k, .s = s, .t = t }; } -/// const hash1 = Md5.hash("my input"); -/// -/// const hasher = Md5.init(); -/// hasher.update("my "); -/// hasher.update("input"); -/// const hash2 = hasher.final(); pub const Md5 = struct { const Self = this; + const block_size = 64; + const digest_size = 16; s: [4]u32, // Streaming Cache diff --git a/std/crypto/sha1.zig b/std/crypto/sha1.zig index bb68b62983..94f29bde44 100644 --- a/std/crypto/sha1.zig +++ b/std/crypto/sha1.zig @@ -16,6 +16,8 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) -> RoundParam { pub const Sha1 = struct { const Self = this; + const block_size = 64; + const digest_size = 20; s: [5]u32, // Streaming Cache diff --git a/std/crypto/sha2.zig b/std/crypto/sha2.zig index 5ddcb7df91..deb65cd321 100644 --- a/std/crypto/sha2.zig +++ b/std/crypto/sha2.zig @@ -58,6 +58,8 @@ pub const Sha256 = Sha2_32(Sha256Params); fn Sha2_32(comptime params: Sha2Params32) -> type { return struct { const Self = this; + const block_size = 64; + const digest_size = params.out_len / 8; s: [8]u32, // Streaming Cache @@ -372,7 +374,8 @@ pub const Sha512 = Sha2_64(Sha512Params); fn Sha2_64(comptime params: Sha2Params64) -> type { return struct { const Self = this; - const u9 = @IntType(false, 9); + const block_size = 128; + const digest_size = params.out_len / 8; s: [8]u64, // Streaming Cache diff --git a/std/crypto/throughput_test.zig b/std/crypto/throughput_test.zig new file mode 100644 index 0000000000..5285af3ed6 --- /dev/null +++ b/std/crypto/throughput_test.zig @@ -0,0 +1,43 @@ +// Modify the HashFunction variable to the one wanted to test. +// +// NOTE: The throughput measurement may be slightly lower than other measurements since we run +// through our block alignment functions as well. Be aware when comparing against other tests. +// +// ``` +// zig build-exe --release-fast --library c throughput_test.zig +// ./throughput_test +// ``` +const HashFunction = @import("md5.zig").Md5; +const BytesToHash = 1024 * Mb; + +const std = @import("std"); + +const c = @cImport({ + @cInclude("time.h"); +}); + +const Mb = 1024 * 1024; + +pub fn main() -> %void { + var stdout_file = try std.io.getStdOut(); + var stdout_out_stream = std.io.FileOutStream.init(&stdout_file); + const stdout = &stdout_out_stream.stream; + + var block: [HashFunction.block_size]u8 = undefined; + std.mem.set(u8, block[0..], 0); + + var h = HashFunction.init(); + var offset: usize = 0; + + const start = c.clock(); + while (offset < BytesToHash) : (offset += block.len) { + h.update(block[0..]); + } + const end = c.clock(); + + const elapsed_s = f64((end - start) * c.CLOCKS_PER_SEC) / 1000000; + const throughput = u64(BytesToHash / elapsed_s); + + try stdout.print("{}: ", @typeName(HashFunction)); + try stdout.print("{} Mb/s\n", throughput); +}