Module Nocrypto.Rng

Secure random number generation.

There are several parts of this module:

Usage notes

Interface

type g

A generator (PRNG) with its state.

exception Unseeded_generator

Thrown when using an uninitialized generator.

module S : sig ... end

Module signatures.

module Generators : sig ... end

Ready-to-use RNG algorithms.

val create : ?⁠g:'a -> ?⁠seed:Cstruct.t -> ?⁠strict:bool -> (module S.Generator with type g = 'a) -> g

create module uses a module conforming to the Generator signature to instantiate the generic generator g.

g is the state to use, otherwise a fresh one is created.

seed can be provided to immediately reseed the generator with.

strict puts the generator into a more standards-conformant, but slighty slower mode. Useful if the outputs need to match published test-vectors.

val generator : g Stdlib.ref

Default generator. Functions in this module use this generator when not explicitly supplied one.

Swapping the generator is a way to subvert the random-generation process e.g. to make it fully deterministic.

generator defaults to Fortuna.

val generate : ?⁠g:g -> int -> Cstruct.t

Invoke generate on g or default generator.

val block : g option -> int

Block size of g or default generator.

Generation of common numeric types

module Make_N : functor (N : Numeric.S) -> S.N with type t = N.t

Creates a suite of generating functions over a numeric type.

module Int : S.N with type t = int
module Int32 : S.N with type t = int32
module Int64 : S.N with type t = int64
module Z : S.N with type t = Z.t

Specialized generation

val prime : ?⁠g:g -> ?⁠msb:int -> int -> Z.t

prime ~g ~msb bits generates a prime smaller than 2^bits, with msb most significant bits set.

prime ~g ~msb:1 bits (the default) yields a prime in the interval [2^(bits - 1), 2^bits - 1].

val safe_prime : ?⁠g:g -> int -> Z.t * Z.t

safe_prime ~g bits gives a prime pair (g, p) such that p = 2g + 1 and p has bits significant bits.

Examples

type buffer = Cstruct.t

Type definition to satisfy MirageOS RANDOM signature