BLAKE2b hash-based RNG

class randomgen.blabla.BlaBla(seed=None, *, counter=None, key=None, rounds=10, mode='sequence')

Container for the BlaBla family of counter pseudo-random number generators

Parameters:
seed=None

Random seed initializing the pseudo-random number generator. Can be an integer in [0, 2**256), an array of 4 uint64 values, a SeedSequence instance or None (the default). If seed is None, then a new SeedSequence is created usign data is read from /dev/urandom .

counter=None

Counter to use in the BlaBla state. Can be either a Python int in [0, 2**128) or a 2-element uint64 array. If not provided, the counter is initialized at 0.

key=None

Key to use in the BlaBla state. Unlike seed, which is run through another RNG before use, the value in key is directly set. Can be either a Python int in [0, 2**256) or a 4-element uint64 array. key and seed cannot both be used.

rounds=10

Number of rounds to run the BlaBla mixer. Must be an even integer. The standard number of rounds in 10.

lock

Lock instance that is shared so that the same bit git generator can be used in multiple Generators without corrupting the state. Code that generates values from a bit generator should hold the bit generator’s lock.

Type:

threading.Lock

seed_seq

The SeedSequence instance used to initialize the generator if mode is “sequence” or is seed is a SeedSequence.

Type:

{None, SeedSequence}

Notes

BlaBla is a 64-bit PRNG that uses a counter-based design based on the Blake2b hash function [1]. The idea was first implemented in Swift in [2], and later reimplemented in C++ in [3]. The C implementation is dervied from the C++ version. Instances using different values of the key produce distinct sequences. BlaBla 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.

BlaBla provides a capsule containing function pointers that produce doubles, and unsigned 32 and 64- bit integers. These are not directly consumable in Python and must be consumed by a Generator or similar object that supports low-level access.

See AESCounter and ChaCha for related counter-based PRNGs.

State and Seeding

The BlaBla state vector consists of a 16-element array of uint64s that capture buffered draws from the distribution, an 4-element array of uint64s holding the seed, and an 2-element array of uint64 that holds the counter ([low, high]). The elements of the seed are the value provided by the user (or from the entropy pool). The final value rounds contains the number of rounds used (typically 10). More rounds can be used to improve security (12-16).

BlaBla is seeded using either a single 256-bit unsigned integer or a vector of 4 64-bit unsigned integers. In either case, the seed is used as an input for a second random number generator provided by a SeedSequence, and the output of this PRNG function is used as the initial state. Using a single 64-bit value for the seed can only initialize a small range of the possible initial state values.

Parallel Features

BlaBla can be used in parallel applications by calling the jumped method to advances the state as-if \(2^{64}\) random numbers have been generated. Alternatively, advance can be used to advance the counter for any positive step in [0, 2**128). When using jumped, all generators should be initialized with the same seed to ensure that the segments come from the same sequence.

>>> from numpy.random import Generator
>>> from randomgen import BlaBla
# Advance each BlaBla instances by i jumps
>>> rg = [Generator(BlaBla(1234).jumped(i)) for i in range(10)]

Alternatively, BlaBla can be used in parallel applications by using a sequence of distinct keys where each instance uses different key.

>>> key = 2**93 + 2**65 + 2**33 + 2**17 + 2**9
>>> rg = [Generator(BlaBla(key=key+i)) for i in range(10)]

Compatibility Guarantee

BlaBla makes a guarantee that a fixed seed and will always produce the same random integer stream.

Examples

>>> from numpy.random import Generator
>>> from randomgen import BlaBla
>>> rg = Generator(BlaBla(1234))
>>> rg.standard_normal()
-0.8632  # random

References

Seeding and State

seed([seed, counter, key])

Seed the generator

state

Get or set the PRNG state

Parallel generation

advance(delta)

Advance the state by delta steps

jumped([iter])

Returns a new bit generator with the state jumped

Extending

cffi

CFFI interface

ctypes

ctypes interface

Testing

random_raw([size, output])

Return randoms as generated by the underlying BitGenerator