Crate rand_aes

Source
Expand description

§AES based pseudo-random number generator

This crate implements a pseudo-random number generator (PRNG) using the AES block cipher in counter (CTR) mode. It provides four variants:

  1. Aes128Ctr64: Utilizes AES-128 encryption with a 64-bit counter.
  2. Aes128Ctr128: Utilizes AES-128 encryption with a 128-bit counter.
  3. Aes256Ctr64: Utilizes AES-256 encryption with a 64-bit counter.
  4. Aes256Ctr128: Utilizes AES-256 encryption with a 128-bit counter.

Common functionality is provided using the Random trait or the optionally provided rand_core::RngCore and rand_core::SeedableRng traits.

§Optimal Performance

We provide runtime detection for the hardware accelerated AES instruction set for all supported platforms. Should the executing CPU not support hardware accelerated AES, a software fallback is provided. But we highly recommend to enable the specific target feature on compile time, since the AES instruction sets is available on modern desktop CPU for at least 10 years. Enabling the target feature enables the compiler to more aggressively inline and provides much better performance. The runtime detection is not supported in no_std.

Use the following target features for optimal performance:

  • aarch64: aes (using the cryptographic extension)
  • x86: sse2 and aes (using AES-NI)
  • x86_64: aes (using AES-NI)

There is experimental support for the RISC-V vector crypto extension. Please read the README.md for more information how to use it.

§Security Note

While based on well-established cryptographic primitives, this PRNG is not intended for cryptographic key generation or other sensitive cryptographic operations, simply because safe, automatic re-seeding is not provided. We tested its statistical qualities by running versions with reduced rounds against practrand and TESTu01’s Big Crush. A version with just 3 rounds of AES encryption rounds passes the practrand tests with at least 16 TB. TESTu01’s Big Crush requires at least 5 rounds to be successfully cleared. AES-128 uses 10 rounds, whereas AES-256 uses 14 rounds.

§Parallel Stream Generation

The 128-bit counter PRNG support efficient parallel stream generation through the Jump trait. The provided functions allow you to create multiple independent streams of random numbers, which is particularly useful for parallel or distributed computations. The API is designed to easily create new random number generators for child threads / tasks from a base instance.

§Jump Function

The Jump::jump() function advances the PRNG counter by 2^64 steps. It can be used to create up to 2^64 non-overlapping subsequences.

§Long Jump Function

The Jump::long_jump() function advances the PRNG counter by 2^96 steps. This allows for even larger separations between subsequences, useful for creating up to 2^32 independent streams.

These functions are particularly useful in scenarios requiring multiple independent PRNG streams, such as parallel Monte Carlo simulations or distributed computing tasks.

Modules§

seeds
Seeds are used to properly initialize the provided random number generators.
tlstls
Provides thread local based utility functions for easy random number generation.

Structs§

Aes128Ctr64
A random number generator based on the AES-128 block cipher that runs in CTR mode and has a period of 64-bit.
Aes128Ctr128
A random number generator based on the AES-128 block cipher that runs in CTR mode and has a period of 128-bit.
Aes256Ctr64
A random number generator based on the AES-256 block cipher that runs in CTR mode and has a period of 64-bit.
Aes256Ctr128
A random number generator based on the AES-256 block cipher that runs in CTR mode and has a period of 128-bit.

Traits§

Jump
Provides common jump functionality to RNG with 128-bit period.
Random
Provides common random number generation functionality.