| ... | @@ -134,8 +134,8 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count | ... | @@ -134,8 +134,8 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count |
| 134 | { | 134 | { |
| 135 | var i: usize = 0; | 135 | var i: usize = 0; |
| 136 | while (i < signatures_count) : (i += 1) { | 136 | while (i < signatures_count) : (i += 1) { |
| 137 | const s = try Signature.sign(&msg, key_pair, null); | 137 | const sig = try Signature.sign(&msg, key_pair, null); |
| 138 | mem.doNotOptimizeAway(&s); | 138 | mem.doNotOptimizeAway(&sig); |
| 139 | } | 139 | } |
| 140 | } | 140 | } |
| 141 | const end = timer.read(); | 141 | const end = timer.read(); |
| ... | @@ -146,6 +146,65 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count | ... | @@ -146,6 +146,65 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count |
| 146 | return throughput; | 146 | return throughput; |
| 147 | } | 147 | } |
| 148 | | 148 | |
| | 149 | const signature_verifications = [_]Crypto{Crypto{ .ty = crypto.sign.Ed25519, .name = "ed25519" }}; |
| | 150 | |
| | 151 | pub fn benchmarkSignatureVerification(comptime Signature: anytype, comptime signatures_count: comptime_int) !u64 { |
| | 152 | var seed: [Signature.seed_length]u8 = undefined; |
| | 153 | prng.random.bytes(seed[0..]); |
| | 154 | const msg = [_]u8{0} ** 64; |
| | 155 | const key_pair = try Signature.createKeyPair(seed); |
| | 156 | const public_key = Signature.publicKey(key_pair); |
| | 157 | const sig = try Signature.sign(&msg, key_pair, null); |
| | 158 | |
| | 159 | var timer = try Timer.start(); |
| | 160 | const start = timer.lap(); |
| | 161 | { |
| | 162 | var i: usize = 0; |
| | 163 | while (i < signatures_count) : (i += 1) { |
| | 164 | try Signature.verify(sig, &msg, public_key); |
| | 165 | mem.doNotOptimizeAway(&sig); |
| | 166 | } |
| | 167 | } |
| | 168 | const end = timer.read(); |
| | 169 | |
| | 170 | const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; |
| | 171 | const throughput = @floatToInt(u64, signatures_count / elapsed_s); |
| | 172 | |
| | 173 | return throughput; |
| | 174 | } |
| | 175 | |
| | 176 | const batch_signature_verifications = [_]Crypto{Crypto{ .ty = crypto.sign.Ed25519, .name = "ed25519" }}; |
| | 177 | |
| | 178 | pub fn benchmarkBatchSignatureVerification(comptime Signature: anytype, comptime signatures_count: comptime_int) !u64 { |
| | 179 | var seed: [Signature.seed_length]u8 = undefined; |
| | 180 | prng.random.bytes(seed[0..]); |
| | 181 | const msg = [_]u8{0} ** 64; |
| | 182 | const key_pair = try Signature.createKeyPair(seed); |
| | 183 | const public_key = Signature.publicKey(key_pair); |
| | 184 | const sig = try Signature.sign(&msg, key_pair, null); |
| | 185 | |
| | 186 | var batch: [64]Signature.BatchElement = undefined; |
| | 187 | for (batch) |*element| { |
| | 188 | element.* = Signature.BatchElement{ .sig = sig, .msg = &msg, .public_key = public_key }; |
| | 189 | } |
| | 190 | |
| | 191 | var timer = try Timer.start(); |
| | 192 | const start = timer.lap(); |
| | 193 | { |
| | 194 | var i: usize = 0; |
| | 195 | while (i < signatures_count) : (i += 1) { |
| | 196 | try Signature.verifyBatch(batch.len, batch); |
| | 197 | mem.doNotOptimizeAway(&sig); |
| | 198 | } |
| | 199 | } |
| | 200 | const end = timer.read(); |
| | 201 | |
| | 202 | const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; |
| | 203 | const throughput = batch.len * @floatToInt(u64, signatures_count / elapsed_s); |
| | 204 | |
| | 205 | return throughput; |
| | 206 | } |
| | 207 | |
| 149 | const aeads = [_]Crypto{ | 208 | const aeads = [_]Crypto{ |
| 150 | Crypto{ .ty = crypto.aead.ChaCha20Poly1305, .name = "chacha20Poly1305" }, | 209 | Crypto{ .ty = crypto.aead.ChaCha20Poly1305, .name = "chacha20Poly1305" }, |
| 151 | Crypto{ .ty = crypto.aead.XChaCha20Poly1305, .name = "xchacha20Poly1305" }, | 210 | Crypto{ .ty = crypto.aead.XChaCha20Poly1305, .name = "xchacha20Poly1305" }, |
| ... | @@ -326,6 +385,20 @@ pub fn main() !void { | ... | @@ -326,6 +385,20 @@ pub fn main() !void { |
| 326 | } | 385 | } |
| 327 | } | 386 | } |
| 328 | | 387 | |
| | 388 | inline for (signature_verifications) |E| { |
| | 389 | if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) { |
| | 390 | const throughput = try benchmarkSignatureVerification(E.ty, mode(1000)); |
| | 391 | try stdout.print("{:>17}: {:10} verifications/s\n", .{ E.name, throughput }); |
| | 392 | } |
| | 393 | } |
| | 394 | |
| | 395 | inline for (batch_signature_verifications) |E| { |
| | 396 | if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) { |
| | 397 | const throughput = try benchmarkBatchSignatureVerification(E.ty, mode(1000)); |
| | 398 | try stdout.print("{:>17}: {:10} verifications/s (batch)\n", .{ E.name, throughput }); |
| | 399 | } |
| | 400 | } |
| | 401 | |
| 329 | inline for (aeads) |E| { | 402 | inline for (aeads) |E| { |
| 330 | if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) { | 403 | if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) { |
| 331 | const throughput = try benchmarkAead(E.ty, mode(128 * MiB)); | 404 | const throughput = try benchmarkAead(E.ty, mode(128 * MiB)); |