Fast Haskell random number generators: the xoshiro256++ PRNG and the ChaCha8Rand CSPRNG.
Both generators are mutable, living in any PrimMonad (e.g. IO
or ST s); the PRNG is also available as an immutable value, in
Crypto.PRNG.Pure. Use the PRNG for speed where predictability doesn't
matter, e.g. in simulation, and Crypto.CSPRNG everywhere else.
A sample GHCi session:
> :set -XOverloadedStrings
>
> -- import qualified
> import qualified Crypto.PRNG as PRNG
> import qualified Crypto.CSPRNG as CSPRNG
>
> -- a PRNG from a 64-bit seed, or seeded by the OS
> gen <- PRNG.new 42
> PRNG.word64 gen
15021278609987233951
> PRNG.double gen
0.3188210400616611
>
> -- non-overlapping streams, e.g. one per thread
> other <- PRNG.copy gen
> PRNG.jump gen
>
> -- the same generator as a pure value, threaded explicitly
> import qualified Crypto.PRNG.Pure as Pure
> let Pure.Draw w next = Pure.word64 (Pure.new 42)
> w
15021278609987233951
>
> -- a CSPRNG from a 256-bit seed; prefer CSPRNG.new_system, which
> -- seeds from the OS, unless you need a reproducible stream
> Just csprng <- CSPRNG.new "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"
> CSPRNG.word64 csprng
13219109469176600229
> CSPRNG.bytes 8 csprng
"<\188j\166\"\175`\DC1"
>
> -- an independent generator, e.g. for another thread
> child <- CSPRNG.split csprng
>
> -- wipe a CSPRNG's state when finished with it
> CSPRNG.wipe csprng
Haddocks (API documentation, etc.) are hosted at docs.ppad.tech/random.
The aim is best-in-class performance. The CSPRNG uses the hardware accelerated, four-block interleaved ChaCha8 kernels from ppad-chacha, and is about as fast as the PRNG.
Current benchmark figures on an M4 Silicon MacBook Air, generating 1000
words at a time, look like (use cabal bench to run the benchmark
suite):
benchmarking word64 (x1000)/ppad-random (PRNG)
time 2.201 μs (2.198 μs .. 2.204 μs)
benchmarking word64 (x1000)/ppad-random (PRNG.Pure)
time 723.3 ns (722.5 ns .. 724.1 ns)
benchmarking word64 (x1000)/ppad-random (CSPRNG)
time 2.524 μs (2.521 μs .. 2.527 μs)
The pure PRNG keeps its state in registers when inlined into a strict loop, avoiding the memory traffic of a mutable generator. For bytes, the CSPRNG produces 1024 at a time in about 290ns.
You should compile with the 'llvm' flag for maximum performance.
This library aims at the maximum security achievable in a garbage-collected language under an optimizing compiler such as GHC, in which strict constant-timeness can be challenging to achieve.
The CSPRNG erases its key and output as it uses them, and its native ChaCha8 kernels zero the stack they use, so that a later disclosure of process memory reveals nothing about prior output; see the module documentation for the limits of this in a garbage-collected runtime. A forked process inherits a copy of a generator's state, so create a new generator in the child rather than using an inherited one. The CSPRNG passes the sample output from the ChaCha8Rand specification.
If you discover any vulnerabilities, please disclose them via security@ppad.tech.
You'll require Nix with flake support enabled. Enter a development shell with:
$ nix develop
Then do e.g.:
$ cabal repl ppad-random
to get a REPL for the main library.