mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
These changes have been made to resolve issue #10037. The `Random` interface was implemented in such a way that causes significant slowdown when calling the `fill` function of the rng used. The `Random` interface is no longer stored in a field of the rng, and is instead returned by the child function `random()` of the rng. This avoids the performance issues caused by the interface.
35 lines
919 B
Zig
35 lines
919 B
Zig
//! CSPRNG
|
|
|
|
const std = @import("std");
|
|
const Random = std.rand.Random;
|
|
const mem = std.mem;
|
|
const Gimli = @This();
|
|
|
|
state: std.crypto.core.Gimli,
|
|
|
|
pub const secret_seed_length = 32;
|
|
|
|
/// The seed must be uniform, secret and `secret_seed_length` bytes long.
|
|
pub fn init(secret_seed: [secret_seed_length]u8) Gimli {
|
|
var initial_state: [std.crypto.core.Gimli.BLOCKBYTES]u8 = undefined;
|
|
mem.copy(u8, initial_state[0..secret_seed_length], &secret_seed);
|
|
mem.set(u8, initial_state[secret_seed_length..], 0);
|
|
var self = Gimli{
|
|
.state = std.crypto.core.Gimli.init(initial_state),
|
|
};
|
|
return self;
|
|
}
|
|
|
|
pub fn random(self: *Gimli) Random {
|
|
return Random.init(self);
|
|
}
|
|
|
|
pub fn fill(self: *Gimli, buf: []u8) void {
|
|
if (buf.len != 0) {
|
|
self.state.squeeze(buf);
|
|
} else {
|
|
self.state.permute();
|
|
}
|
|
mem.set(u8, self.state.toSlice()[0..std.crypto.core.Gimli.RATE], 0);
|
|
}
|