diff options
| author | daurnimator <quae@daurnimator.com> | 2019-07-08 02:01:12 +1000 |
|---|---|---|
| committer | daurnimator <quae@daurnimator.com> | 2020-02-06 00:16:00 +1100 |
| commit | 5a095a3f083f5e2248b0d37bf499c350ad18179f (patch) | |
| tree | f0a8b53f66201c6d00211647528556f7a2b7e8c5 /lib/std | |
| parent | 25cbee0b84074368c23b7540a63cba83162e4b73 (diff) | |
| download | zig-5a095a3f083f5e2248b0d37bf499c350ad18179f.tar.gz zig-5a095a3f083f5e2248b0d37bf499c350ad18179f.zip | |
std: add Gimli based PRNG to std.rand
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/rand.zig | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/std/rand.zig b/lib/std/rand.zig index f9fa4a2d66..0bdc593545 100644 --- a/lib/std/rand.zig +++ b/lib/std/rand.zig @@ -733,6 +733,32 @@ test "xoroshiro sequence" { } } +// Gimli +// +// CSPRNG +pub const Gimli = struct { + random: Random, + state: std.crypto.gimli.State, + + pub fn init(init_s: u64) Gimli { + var self = Gimli{ + .random = Random{ .fillFn = fill }, + .state = std.crypto.gimli.State{ + .data = [_]u32{0} ** (std.crypto.gimli.State.BLOCKBYTES / 4), + }, + }; + self.state.data[0] = @truncate(u32, init_s >> 32); + self.state.data[1] = @truncate(u32, init_s); + return self; + } + + fn fill(r: *Random, buf: []u8) void { + const self = @fieldParentPtr(Gimli, "random", r); + + self.state.squeeze(buf); + } +}; + // ISAAC64 - http://www.burtleburtle.net/bob/rand/isaacafa.html // // CSPRNG |
