HC-128 Cipher Generator

class randomgen.hc128.HC128(seed=None, *, mode=None)

Container for the HC-128 cipher-based pseudo-random number generator

Parameters
seed{None, int, array_like[uint64], SeedSequence}, optional

Random seed initializing the pseudo-random number generator. Can be an integer in [0, 2**64), array of integers in [0, 2**64), a SeedSequence instance or None (the default). If seed is None, then data is read from /dev/urandom (or the Windows analog) if available. If unavailable, a hash of the time and process ID is used.

key{int, array_like[uint64]}, optional

Key for HC128. The key is a 256-bit integer that contains both the key (lower 128 bits) and initial values (upper 128-bits) for the HC-128 cipher. key and seed cannot both be used.

mode{None, “sequence”, “legacy”}, optional

The seeding mode to use. “legacy” uses the legacy SplitMix64-based initialization. “sequence” uses a SeedSequence to transforms the seed into an initial state. None defaults to “sequence”.

Notes

HC-128 was developer by Hongjun Wu and is an eSTREAM finalist ([1], [2]). It is a cryptographic quality random number generator that produces a keystream is suitable for encryption. The average cycle length is expected to be 2**(1024*32+10-1) = 2**32777. HC128 is the fastest software-only encryption-quality bit generator, and about 50% as fast as AESCounter when used with AESNI.

HC128 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 PRNGs that uses a block cipher rather than a stream cipher.

State and Seeding

The HC128 state vector consists of a 2 512-element arrays of 32-bit unsigned integers (p and q) and an integer in [0, 1024). In addition, the state contains a 16-element array that buffers values and a buffer index.

HC128 is seeded using either a single 256-bit unsigned integer or a vector of 64-bit unsigned integers. In either case, the seed is used as an input for another simple random number generator, SplitMix64, and the output of this PRNG function is used as the initial state. Alternatively, the key can be set directly using a 256-bit integer.

Parallel Features

HC128 can be used in parallel applications by using distinct keys

>>> from randomgen import Generator, HC128
>>> rg = [Generator(HC128(key=1234 + i)) for i in range(10)]

Compatibility Guarantee

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

References

1

Wu, Hongjun (2008). “The Stream Cipher HC-128.” http://www.ecrypt.eu.org/stream/p3ciphers/hc/hc128_p3.pdf. The eSTREAM Finalists, LNCS 4986, pp. 39–47, Springer-Verlag.

2

Wu, Hongjun, “Stream Ciphers HC-128 and HC-256”. https://www.ntu.edu.sg/home/wuhj/research/hc/index.html)

Examples

>>> from randomgen import Generator, HC128
>>> rg = Generator(HC128(1234))
>>> rg.standard_normal()
0.123  # random
Attributes
lockthreading.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.

seed_seq{None, SeedSequence}

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

Seeding and State

seed([seed])

Seed the generator

state

Get or set the PRNG state

Extending

cffi

CFFI interface

ctypes

ctypes interface

Testing

random_raw([size, output])

Return randoms as generated by the underlying BitGenerator