pub struct ChaCha8Rng { /* private fields */ }Expand description
A cryptographically secure random number generator that uses the ChaCha stream cipher.
See the crate docs for more information about the underlying stream cipher.
This RNG implementation uses a 64-bit counter and 64-bit stream identifier (a.k.a nonce).
A 64-bit counter over 64-byte (16 word) blocks allows 1 ZiB of output before cycling,
and the stream identifier allows 264 unique streams of output per seed.
Both counter and stream are initialized to zero but may be set via the set_word_pos
and set_stream methods.
§Example
use chacha20::ChaCha8Rng;
use rand_core::{SeedableRng, Rng};
let seed = [42u8; 32];
let mut rng = ChaCha8Rng::from_seed(seed);
let random_u32 = rng.next_u32();
let random_u64 = rng.next_u64();
let mut random_bytes = [0u8; 3];
rng.fill_bytes(&mut random_bytes);See the rand crate for more advanced RNG functionality.
Implementations§
Source§impl ChaCha8Rng
impl ChaCha8Rng
Sourcepub fn get_word_pos(&self) -> u128
pub fn get_word_pos(&self) -> u128
Get the offset from the start of the stream, in 32-bit words.
Since the generated blocks are 64 words (26) long and the counter is 64-bits, the offset is a 68-bit number. Sub-word offsets are not supported, hence the result can simply be multiplied by 4 to get a byte-offset.
Sourcepub fn set_word_pos(&mut self, word_offset: u128)
pub fn set_word_pos(&mut self, word_offset: u128)
Set the offset from the start of the stream, in 32-bit words.
This value will be erased when calling set_stream(),
so call set_stream() before calling set_word_pos()
if you intend on using both of them together.
As with get_word_pos, we use a 68-bit number. Since the generator
simply cycles at the end of its period (1 ZiB), we ignore the upper
60 bits.
Sourcepub fn set_block_pos(&mut self, block_pos: u64)
pub fn set_block_pos(&mut self, block_pos: u64)
Sets the block pos and resets the RNG’s index.
This value will be erased when calling set_stream(),
so call set_stream() before calling set_block_pos()
if you intend on using both of them together.
The word pos will be equal to block_pos * 16 words per block.
Sourcepub fn get_block_pos(&self) -> u64
pub fn get_block_pos(&self) -> u64
Get the block pos.
Sourcepub fn set_stream(&mut self, stream: u64)
pub fn set_stream(&mut self, stream: u64)
Set the stream ID and reset the word_pos to 0.
Sourcepub fn get_stream(&self) -> u64
pub fn get_stream(&self) -> u64
Get the stream number (nonce).
Sourcepub fn serialize_state(&self) -> SerializedRngState
pub fn serialize_state(&self) -> SerializedRngState
Serialize RNG state.
§Warning
Leaking serialized RNG state to an attacker defeats security properties provided by the RNG.
Sourcepub fn deserialize_state(state: &SerializedRngState) -> Self
pub fn deserialize_state(state: &SerializedRngState) -> Self
Deserialize RNG state.
Trait Implementations§
Source§impl Debug for ChaCha8Rng
impl Debug for ChaCha8Rng
Source§impl PartialEq for ChaCha8Rng
impl PartialEq for ChaCha8Rng
Source§impl SeedableRng for ChaCha8Rng
impl SeedableRng for ChaCha8Rng
Source§type Seed = [u8; 32]
type Seed = [u8; 32]
u8
arrays (we recommend [u8; N] for some N). Read moreSource§fn seed_from_u64(state: u64) -> Self
fn seed_from_u64(state: u64) -> Self
u64 seed. Read moreSource§fn from_rng<R>(rng: &mut R) -> Self
fn from_rng<R>(rng: &mut R) -> Self
Rng. Read more