| ... | @@ -7,6 +7,7 @@ | ... | @@ -7,6 +7,7 @@ |
| 7 | | 7 | |
| 8 | const builtin = @import("builtin"); | 8 | const builtin = @import("builtin"); |
| 9 | const std = @import("std"); | 9 | const std = @import("std"); |
| | 10 | const mem = std.mem; |
| 10 | const time = std.time; | 11 | const time = std.time; |
| 11 | const Timer = time.Timer; | 12 | const Timer = time.Timer; |
| 12 | const crypto = std.crypto; | 13 | const crypto = std.crypto; |
| ... | @@ -21,14 +22,6 @@ const Crypto = struct { | ... | @@ -21,14 +22,6 @@ const Crypto = struct { |
| 21 | name: []const u8, | 22 | name: []const u8, |
| 22 | }; | 23 | }; |
| 23 | | 24 | |
| 24 | fn blackBox(x: anytype) void { | | |
| 25 | asm volatile ("" | | |
| 26 | : | | |
| 27 | : [x] "rm" (x) | | |
| 28 | : "memory" | | |
| 29 | ); | | |
| 30 | } | | |
| 31 | | | |
| 32 | const hashes = [_]Crypto{ | 25 | const hashes = [_]Crypto{ |
| 33 | Crypto{ .ty = crypto.hash.Md5, .name = "md5" }, | 26 | Crypto{ .ty = crypto.hash.Md5, .name = "md5" }, |
| 34 | Crypto{ .ty = crypto.hash.Sha1, .name = "sha1" }, | 27 | Crypto{ .ty = crypto.hash.Sha1, .name = "sha1" }, |
| ... | @@ -54,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64 | ... | @@ -54,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64 |
| 54 | while (offset < bytes) : (offset += block.len) { | 47 | while (offset < bytes) : (offset += block.len) { |
| 55 | h.update(block[0..]); | 48 | h.update(block[0..]); |
| 56 | } | 49 | } |
| 57 | blackBox(&h); | 50 | mem.forceEval(&h); |
| 58 | const end = timer.read(); | 51 | const end = timer.read(); |
| 59 | | 52 | |
| 60 | const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; | 53 | const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; |
| ... | @@ -89,7 +82,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 { | ... | @@ -89,7 +82,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 { |
| 89 | const start = timer.lap(); | 82 | const start = timer.lap(); |
| 90 | while (offset < bytes) : (offset += in.len) { | 83 | while (offset < bytes) : (offset += in.len) { |
| 91 | Mac.create(mac[0..], in[0..], key[0..]); | 84 | Mac.create(mac[0..], in[0..], key[0..]); |
| 92 | blackBox(&mac); | 85 | mem.forceEval(&mac); |
| 93 | } | 86 | } |
| 94 | const end = timer.read(); | 87 | const end = timer.read(); |
| 95 | | 88 | |
| ... | @@ -116,7 +109,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c | ... | @@ -116,7 +109,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c |
| 116 | var i: usize = 0; | 109 | var i: usize = 0; |
| 117 | while (i < exchange_count) : (i += 1) { | 110 | while (i < exchange_count) : (i += 1) { |
| 118 | _ = DhKeyExchange.create(out[0..], out[0..], in[0..]); | 111 | _ = DhKeyExchange.create(out[0..], out[0..], in[0..]); |
| 119 | blackBox(&out); | 112 | mem.forceEval(&out); |
| 120 | } | 113 | } |
| 121 | } | 114 | } |
| 122 | const end = timer.read(); | 115 | const end = timer.read(); |
| ... | @@ -141,7 +134,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count | ... | @@ -141,7 +134,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count |
| 141 | var i: usize = 0; | 134 | var i: usize = 0; |
| 142 | while (i < signatures_count) : (i += 1) { | 135 | while (i < signatures_count) : (i += 1) { |
| 143 | const s = try Signature.sign(&msg, key_pair, null); | 136 | const s = try Signature.sign(&msg, key_pair, null); |
| 144 | blackBox(&s); | 137 | mem.forceEval(&s); |
| 145 | } | 138 | } |
| 146 | } | 139 | } |
| 147 | const end = timer.read(); | 140 | const end = timer.read(); |
| ... | @@ -177,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 | ... | @@ -177,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 |
| 177 | Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key); | 170 | Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key); |
| 178 | Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable; | 171 | Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable; |
| 179 | } | 172 | } |
| 180 | blackBox(&in); | 173 | mem.forceEval(&in); |
| 181 | const end = timer.read(); | 174 | const end = timer.read(); |
| 182 | | 175 | |
| 183 | const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; | 176 | const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; |