authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-15 18:03:38+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-16 22:35:27-07:00
log08dfbee961b7705d6f9d03fc982ec808f370f64a
tree3b145ae211782c5df7c69446c12ea5b36ead03f1
parent5ab69633b712914cccdf2f08d717387864d6c4c7

Benchmark signatures


1 files changed, 31 insertions(+), 1 deletions(-)

lib/std/crypto/benchmark.zig+31-1
...@@ -90,7 +90,6 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c...@@ -90,7 +90,6 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c
90 var out: [DhKeyExchange.minimum_key_length]u8 = undefined;90 var out: [DhKeyExchange.minimum_key_length]u8 = undefined;
91 prng.random.bytes(out[0..]);91 prng.random.bytes(out[0..]);
9292
93 var offset: usize = 0;
94 var timer = try Timer.start();93 var timer = try Timer.start();
95 const start = timer.lap();94 const start = timer.lap();
96 {95 {
...@@ -107,6 +106,30 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c...@@ -107,6 +106,30 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c
107 return throughput;106 return throughput;
108}107}
109108
109const signatures = [_]Crypto{Crypto{ .ty = crypto.Ed25519, .name = "ed25519" }};
110
111pub fn benchmarkSignatures(comptime Signature: anytype, comptime signatures_count: comptime_int) !u64 {
112 var seed: [Signature.seed_length]u8 = undefined;
113 prng.random.bytes(seed[0..]);
114 const msg = [_]u8{0} ** 64;
115 const key_pair = try Signature.createKeyPair(seed);
116
117 var timer = try Timer.start();
118 const start = timer.lap();
119 {
120 var i: usize = 0;
121 while (i < signatures_count) : (i += 1) {
122 _ = try Signature.sign(&msg, key_pair, null);
123 }
124 }
125 const end = timer.read();
126
127 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
128 const throughput = @floatToInt(u64, signatures_count / elapsed_s);
129
130 return throughput;
131}
132
110fn usage() void {133fn usage() void {
111 std.debug.warn(134 std.debug.warn(
112 \\throughput_test [options]135 \\throughput_test [options]
...@@ -183,4 +206,11 @@ pub fn main() !void {...@@ -183,4 +206,11 @@ pub fn main() !void {
183 try stdout.print("{:>11}: {:5} exchanges/s\n", .{ E.name, throughput });206 try stdout.print("{:>11}: {:5} exchanges/s\n", .{ E.name, throughput });
184 }207 }
185 }208 }
209
210 inline for (signatures) |E| {
211 if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) {
212 const throughput = try benchmarkSignatures(E.ty, mode(1000));
213 try stdout.print("{:>11}: {:5} signatures/s\n", .{ E.name, throughput });
214 }
215 }
186}216}