package bitgenerators

  1. Overview
  2. Docs

ChaCha is a 64-bit PRNG that uses a counter-based design based on the ChaCha cipher. Instances using different values of the key produce sequences. ChaCha has a period of 2^{128} and supports arbitrary advancing and jumping the sequence in increments of 2^{64}. These features allow multiple non-overlapping sequences to be generated.

The ChaCha state vector consists of a 16-element array of uint32 that capture buffered draws from the distribution, an 8-element array of uint32s holding the seed, and a 2-element array of uint64 that holds the 128-bit counter (low, high). The elements of the seed are the value provided by the user. Typical values for number of rounds are 4, 8, 12, or 20 (for high security).

ChaCha is seeded using a vector of 4 64-bit unsigned integers. By default this is provided by SeedSequence.generate_64bit_state.

type t

t is the state of the bitgenerator.

val next_uint64 : t -> Stdint.uint64 * t

next_uint64 t Generates a random unsigned 64-bit integer and a state of the generator advanced forward by one step.

val next_uint32 : t -> Stdint.uint32 * t

next_uint32 t Generates a random unsigned 32-bit integer and a state of the generator advanced forward by one step.

val next_bounded_uint64 : Stdint.uint64 -> t -> Stdint.uint64 * t

next_bounded_uint64 b t Generates a random unsigned 64-bit integer in the interval [0, b). It returns the integer as well as the state of the generator advanced forward. To generate an integer in the range [a, b), one should generate an integer in [0, b - a) using next_bounded_uint64 (b - a) t and then add a to the resulting integer to get the output in the desired range.

val next_double : t -> float * t

next_double t Generates a random 64 bit float and a state of the generator advanced forward by one step.

val initialize : Bitgen__.Seed.SeedSequence.t -> t

initialize s Returns the initial state of the generator. The random stream is determined by the initialization of the seed sequence s of SeedSequence.t type.

val initialize_full : Bitgen__.Seed.SeedSequence.t -> (Stdint.uint64 * Stdint.uint64) -> int -> t

initialize_full seedseq counter rounds initializes the state of the ChaCha bitgenerator; where seedseq is a SeedSequence.t used to initialize the PRNG's key array, counter is a 2-tuple used to initialize the 128-bit counter, and rounds is the number of rounds to use. rounds must be non-negative, even and greater than 2, else an Invalid_argument exception is raised.

val advance : Stdint.uint128 -> t -> t

advance n Advances the generator forward as if n calls to ChaCha.next_uint32 have been made, and returns the new advanced state.

OCaml

Innovation. Community. Security.