authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-19 23:08:38+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-22 09:58:26+02:00
log047599928a8176ae32d0850638d67d812edd3508
tree774af03bc56db05db3bffee87878bac6b2b16f7a
parent2d9befe9bfbd333d411c1d38062e5d117da4e508

Add a benchmark for signature verifications


1 files changed, 75 insertions(+), 2 deletions(-)

lib/std/crypto/benchmark.zig+75-2
...@@ -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}
148148
149const signature_verifications = [_]Crypto{Crypto{ .ty = crypto.sign.Ed25519, .name = "ed25519" }};
150
151pub 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
176const batch_signature_verifications = [_]Crypto{Crypto{ .ty = crypto.sign.Ed25519, .name = "ed25519" }};
177
178pub 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
149const aeads = [_]Crypto{208const 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 }
328387
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));