Crate aes_prng

source ·
Expand description

Pure Rust implementation of a PRNG based on Advanced Encryption Standard

The underlying implementation is based on the pipelined version of AES from aes create. The PRNG supports two version can take a seed either given by the user or taken from ThreadRng which internally uses /dev/random or dev/urandom using [getrandom rust lib] to generate randomness.

By default the package is compiled with AES-NI implementation for i686/x86_64 target architectures with target-feature=+aes.

The underlying algorithms are inspired from MP-SPDZ and SCALE-MAMBA implementations which generate randomness in batches of 8 * 16 bytes i.e. select a random key k and compute AES_k(0), …, AES_k(7) giving out 128 bytes of randomness as long as the key is random since AES acts as a PRF. At the next iteration AES_k(8), …, AES_k(15) is computed and so on.

§Usage example for already seeded PRNG

use rand::{RngCore, SeedableRng};
use aes_prng::AesRng;

// initialize PRNG seed using (true) entropy from ThreadRng
// this internally using syscalls to /dev/random or /dev/urandom
let mut rng = AesRng::from_random_seed();

// sample random bytes
let mut bytes = [0; 1024];
rng.fill_bytes(&mut bytes);

// sample random u32
let output32 = rng.next_u32();

// sample random u64
let output64 = rng.next_u64();

§Usage example for setting manually the seed

use aes_prng::{AesRng, SEED_SIZE};
use rand::{RngCore, SeedableRng};

// generate fresh seed
let seed = AesRng::generate_random_seed();
// seed generator
let mut rng = AesRng::from_seed(seed);

For implementations of block cipher modes of operation see block-modes crate.


  • Necessary data to compute randomness, a state and an initialized AES blockcipher.


Type Aliases§