Struct ChaCha12Rng
pub struct ChaCha12Rng { /* private fields */ }
A cryptographically secure random number generator that uses the ChaCha stream cipher.
See the [crate docs][crate] 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 ChaCha12Rng;
use ;
let seed = ;
let mut rng = from_seed;
let random_u32 = rng.next_u32;
let random_u64 = rng.next_u64;
let mut random_bytes = ;
rng.fill_bytes;
See the rand crate for more advanced RNG functionality.
Implementations
impl ChaCha12Rng
fn get_word_pos(&self) -> u128Get 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.
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 callset_stream()before callingset_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.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 callset_stream()before callingset_block_pos()if you intend on using both of them together.The word pos will be equal to
block_pos * 16 words per block.fn get_block_pos(&self) -> u64Get the block pos.
fn set_stream(&mut self, stream: u64)Set the stream ID and reset the
word_posto 0.fn get_stream(&self) -> u64Get the stream number (nonce).
fn get_seed(&self) -> [u8; 32]Get the RNG seed.
fn serialize_state(&self) -> SerializedRngStateSerialize RNG state.
Warning
Leaking serialized RNG state to an attacker defeats security properties provided by the RNG.
fn deserialize_state(state: &SerializedRngState) -> SelfDeserialize RNG state.
Trait Implementations
impl Debug for ChaCha12Rng
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for ChaCha12Rng
impl PartialEq for ChaCha12Rng
fn eq(&self, rhs: &ChaCha12Rng) -> bool
impl SeedableRng for ChaCha12Rng
type Seed = [u8; 32];fn from_seed(seed: Self::Seed) -> Self
impl TryCryptoRng for ChaCha12Rng
impl TryRng for ChaCha12Rng
type Error = Infallible;fn try_next_u32(&mut self) -> Result<u32, Self::Error>fn try_next_u64(&mut self) -> Result<u64, Self::Error>fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>
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
impl<R> CryptoRng for ChaCha12Rng
where
R: TryCryptoRng<Error = Infallible> + ?Sized,
impl<R> Rng for ChaCha12Rng
where
R: TryRng<Error = Infallible> + ?Sized,
fn next_u32(&mut self) -> u32fn next_u64(&mut self) -> u64fn fill_bytes(&mut self, dst: &mut [u8])
impl<R> RngCore for ChaCha12Rng
where
R: Rng,
impl<R> TryRngCore for ChaCha12Rng
where
R: TryRng,
type Error = <R as TryRng>::Error;
impl<T> Any for ChaCha12Rng
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for ChaCha12Rng
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for ChaCha12Rng
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for ChaCha12Rng
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for ChaCha12Rng
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for ChaCha12Rng
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for ChaCha12Rng
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>