| author | |
| committer | |
| log | dfd53634941ef560971d80dee4b3028e41c9174e |
| tree | f9db29d3deb18a04150e8d6c8be2025d2edbaf5d |
| parent | 7af53d08268a5c84188e1f4ca481e93186a03c44 |
Blake performance numbers for reference:
```
Cpu: Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
```
-- Blake2s
```
Zig --release-fast
485 Mb/s
Zig --release-safe
377 Mb/s
Zig
11 Mb/s
```
-- Blake2b
```
Zig --release-fast
616 Mb/s
Zig --release-safe
573 Mb/s
Zig
18 Mb/s
```5 files changed, 55 insertions(+), 7 deletions(-)
std/crypto/blake2.zig+4| ... | ... | @@ -21,6 +21,8 @@ pub const Blake2s256 = Blake2s(256); |
| 21 | 21 | |
| 22 | 22 | fn Blake2s(comptime out_len: usize) -> type { return struct { |
| 23 | 23 | const Self = this; |
| 24 | const block_size = 64; | |
| 25 | const digest_size = out_len / 8; | |
| 24 | 26 | |
| 25 | 27 | const iv = [8]u32 { |
| 26 | 28 | 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, |
| ... | ... | @@ -236,6 +238,8 @@ pub const Blake2b512 = Blake2b(512); |
| 236 | 238 | |
| 237 | 239 | fn Blake2b(comptime out_len: usize) -> type { return struct { |
| 238 | 240 | const Self = this; |
| 241 | const block_size = 128; | |
| 242 | const digest_size = out_len / 8; | |
| 239 | 243 | |
| 240 | 244 | const iv = [8]u64 { |
| 241 | 245 | 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, |
std/crypto/md5.zig+2-6| ... | ... | @@ -14,14 +14,10 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) -> Round |
| 14 | 14 | return RoundParam { .a = a, .b = b, .c = c, .d = d, .k = k, .s = s, .t = t }; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | /// const hash1 = Md5.hash("my input"); | |
| 18 | /// | |
| 19 | /// const hasher = Md5.init(); | |
| 20 | /// hasher.update("my "); | |
| 21 | /// hasher.update("input"); | |
| 22 | /// const hash2 = hasher.final(); | |
| 23 | 17 | pub const Md5 = struct { |
| 24 | 18 | const Self = this; |
| 19 | const block_size = 64; | |
| 20 | const digest_size = 16; | |
| 25 | 21 | |
| 26 | 22 | s: [4]u32, |
| 27 | 23 | // Streaming Cache |
std/crypto/sha1.zig+2| ... | ... | @@ -16,6 +16,8 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) -> RoundParam { |
| 16 | 16 | |
| 17 | 17 | pub const Sha1 = struct { |
| 18 | 18 | const Self = this; |
| 19 | const block_size = 64; | |
| 20 | const digest_size = 20; | |
| 19 | 21 | |
| 20 | 22 | s: [5]u32, |
| 21 | 23 | // Streaming Cache |
std/crypto/sha2.zig+4-1| ... | ... | @@ -58,6 +58,8 @@ pub const Sha256 = Sha2_32(Sha256Params); |
| 58 | 58 | |
| 59 | 59 | fn Sha2_32(comptime params: Sha2Params32) -> type { return struct { |
| 60 | 60 | const Self = this; |
| 61 | const block_size = 64; | |
| 62 | const digest_size = params.out_len / 8; | |
| 61 | 63 | |
| 62 | 64 | s: [8]u32, |
| 63 | 65 | // Streaming Cache |
| ... | ... | @@ -372,7 +374,8 @@ pub const Sha512 = Sha2_64(Sha512Params); |
| 372 | 374 | |
| 373 | 375 | fn Sha2_64(comptime params: Sha2Params64) -> type { return struct { |
| 374 | 376 | const Self = this; |
| 375 | const u9 = @IntType(false, 9); | |
| 377 | const block_size = 128; | |
| 378 | const digest_size = params.out_len / 8; | |
| 376 | 379 | |
| 377 | 380 | s: [8]u64, |
| 378 | 381 | // Streaming Cache |
std/crypto/throughput_test.zig created+43| ... | ... | @@ -0,0 +1,43 @@ |
| 1 | // Modify the HashFunction variable to the one wanted to test. | |
| 2 | // | |
| 3 | // NOTE: The throughput measurement may be slightly lower than other measurements since we run | |
| 4 | // through our block alignment functions as well. Be aware when comparing against other tests. | |
| 5 | // | |
| 6 | // ``` | |
| 7 | // zig build-exe --release-fast --library c throughput_test.zig | |
| 8 | // ./throughput_test | |
| 9 | // ``` | |
| 10 | const HashFunction = @import("md5.zig").Md5; | |
| 11 | const BytesToHash = 1024 * Mb; | |
| 12 | ||
| 13 | const std = @import("std"); | |
| 14 | ||
| 15 | const c = @cImport({ | |
| 16 | @cInclude("time.h"); | |
| 17 | }); | |
| 18 | ||
| 19 | const Mb = 1024 * 1024; | |
| 20 | ||
| 21 | pub fn main() -> %void { | |
| 22 | var stdout_file = try std.io.getStdOut(); | |
| 23 | var stdout_out_stream = std.io.FileOutStream.init(&stdout_file); | |
| 24 | const stdout = &stdout_out_stream.stream; | |
| 25 | ||
| 26 | var block: [HashFunction.block_size]u8 = undefined; | |
| 27 | std.mem.set(u8, block[0..], 0); | |
| 28 | ||
| 29 | var h = HashFunction.init(); | |
| 30 | var offset: usize = 0; | |
| 31 | ||
| 32 | const start = c.clock(); | |
| 33 | while (offset < BytesToHash) : (offset += block.len) { | |
| 34 | h.update(block[0..]); | |
| 35 | } | |
| 36 | const end = c.clock(); | |
| 37 | ||
| 38 | const elapsed_s = f64((end - start) * c.CLOCKS_PER_SEC) / 1000000; | |
| 39 | const throughput = u64(BytesToHash / elapsed_s); | |
| 40 | ||
| 41 | try stdout.print("{}: ", @typeName(HashFunction)); | |
| 42 | try stdout.print("{} Mb/s\n", throughput); | |
| 43 | } |