Skip to content

Commit f95d01b

Browse files
committed
benchmarkKeyExchange: avoid reading and writing output simultaneously
1 parent e529538 commit f95d01b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/std/crypto/benchmark.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,20 @@ const exchanges = [_]Crypto{Crypto{ .ty = crypto.dh.X25519, .name = "x25519" }};
9898
pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_count: comptime_int) !u64 {
9999
std.debug.assert(DhKeyExchange.shared_length >= DhKeyExchange.secret_length);
100100

101-
var in: [DhKeyExchange.shared_length]u8 = undefined;
102-
prng.random.bytes(in[0..]);
101+
var secret: [DhKeyExchange.shared_length]u8 = undefined;
102+
prng.random.bytes(secret[0..]);
103103

104-
var out: [DhKeyExchange.shared_length]u8 = undefined;
105-
prng.random.bytes(out[0..]);
104+
var public: [DhKeyExchange.shared_length]u8 = undefined;
105+
prng.random.bytes(public[0..]);
106106

107107
var timer = try Timer.start();
108108
const start = timer.lap();
109109
{
110110
var i: usize = 0;
111111
while (i < exchange_count) : (i += 1) {
112-
out = try DhKeyExchange.scalarmult(out, in);
112+
const out = try DhKeyExchange.scalarmult(secret, public);
113+
mem.copy(u8, secret[0..16], out[0..16]);
114+
mem.copy(u8, public[0..16], out[16..32]);
113115
mem.doNotOptimizeAway(&out);
114116
}
115117
}

0 commit comments

Comments
 (0)