pub struct ChaCha12Rng { /* 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::ChaCha12Rng;
use rand_core::{SeedableRng, Rng};
let seed = [42u8; 32];
let mut rng = ChaCha12Rng::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 ChaCha12Rng
impl ChaCha12Rng
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 16 words (24) 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) -> [u8; 49]
pub fn serialize_state(&self) -> [u8; 49]
Serialize RNG state.
§Warning
Leaking serialized RNG state to an attacker defeats security properties provided by the RNG.
Sourcepub fn deserialize_state(state: &[u8; 49]) -> ChaCha12Rng
pub fn deserialize_state(state: &[u8; 49]) -> ChaCha12Rng
Deserialize RNG state.
Trait Implementations§
Source§impl Debug for ChaCha12Rng
impl Debug for ChaCha12Rng
impl Eq for ChaCha12Rng
Source§impl PartialEq for ChaCha12Rng
impl PartialEq for ChaCha12Rng
Source§impl SeedableRng for ChaCha12Rng
impl SeedableRng for ChaCha12Rng
Source§type Seed = [u8; 32]
type Seed = [u8; 32]
u8
arrays (we recommend [u8; N] for some N). Read moreSource§fn from_seed(seed: <ChaCha12Rng as SeedableRng>::Seed) -> ChaCha12Rng
fn from_seed(seed: <ChaCha12Rng as SeedableRng>::Seed) -> ChaCha12Rng
Source§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 moreimpl TryCryptoRng for ChaCha12Rng
Source§impl TryRng for ChaCha12Rng
impl TryRng for ChaCha12Rng
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <ChaCha12Rng as TryRng>::Error>
fn try_next_u32(&mut self) -> Result<u32, <ChaCha12Rng as TryRng>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <ChaCha12Rng as TryRng>::Error>
fn try_next_u64(&mut self) -> Result<u64, <ChaCha12Rng as TryRng>::Error>
u64.Source§fn try_fill_bytes(
&mut self,
dest: &mut [u8],
) -> Result<(), <ChaCha12Rng as TryRng>::Error>
fn try_fill_bytes( &mut self, dest: &mut [u8], ) -> Result<(), <ChaCha12Rng as TryRng>::Error>
dst entirely with random data.Auto Trait Implementations§
impl Freeze for ChaCha12Rng
impl RefUnwindSafe for ChaCha12Rng
impl Send for ChaCha12Rng
impl Sync for ChaCha12Rng
impl Unpin for ChaCha12Rng
impl UnsafeUnpin for ChaCha12Rng
impl UnwindSafe for ChaCha12Rng
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<R> CryptoRng for R
impl<R> RngCore for Rwhere
R: Rng,
Source§impl<R> RngExt for R
impl<R> RngExt for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read more