ChaCha12Rng

Struct ChaCha12Rng 

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn get_block_pos(&self) -> u64

Get the block pos.

Source

pub fn set_stream(&mut self, stream: u64)

Set the stream ID and reset the word_pos to 0.

Source

pub fn get_stream(&self) -> u64

Get the stream number (nonce).

Source

pub fn get_seed(&self) -> [u8; 32]

Get the RNG seed.

Source

pub fn serialize_state(&self) -> SerializedRngState

Serialize RNG state.

§Warning

Leaking serialized RNG state to an attacker defeats security properties provided by the RNG.

Source

pub fn deserialize_state(state: &SerializedRngState) -> Self

Deserialize RNG state.

Trait Implementations§

Source§

impl Debug for ChaCha12Rng

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ChaCha12Rng

Source§

fn eq(&self, rhs: &ChaCha12Rng) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl SeedableRng for ChaCha12Rng

Source§

type Seed = [u8; 32]

Seed type, which is restricted to types mutably-dereferenceable as u8 arrays (we recommend [u8; N] for some N). Read more
Source§

fn from_seed(seed: Self::Seed) -> Self

Create a new PRNG using the given seed. Read more
Source§

fn seed_from_u64(state: u64) -> Self

Create a new PRNG using a u64 seed. Read more
Source§

fn from_rng<R>(rng: &mut R) -> Self
where R: Rng + ?Sized,

Create a new PRNG seeded from an infallible Rng. Read more
Source§

fn try_from_rng<R>(rng: &mut R) -> Result<Self, <R as TryRng>::Error>
where R: TryRng + ?Sized,

Create a new PRNG seeded from a potentially fallible Rng. Read more
Source§

fn fork(&mut self) -> Self
where Self: Rng,

Fork this PRNG Read more
Source§

fn try_fork(&mut self) -> Result<Self, Self::Error>
where Self: TryRng,

Fork this PRNG Read more
Source§

impl TryRng for ChaCha12Rng

Source§

type Error = Infallible

The type returned in the event of a RNG error. Read more
Source§

fn try_next_u32(&mut self) -> Result<u32, Self::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, Self::Error>

Return the next random u64.
Source§

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>

Fill dst entirely with random data.
Source§

impl Eq for ChaCha12Rng

Source§

impl TryCryptoRng for ChaCha12Rng

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32.
Source§

fn next_u64(&mut self) -> u64

Return the next random u64.
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<R> TryRngCore for R
where R: TryRng,

Source§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0: use TryRng instead
Error type.
Source§

impl<R> CryptoRng for R
where R: TryCryptoRng<Error = Infallible> + ?Sized,

Source§

impl<R> RngCore for R
where R: Rng,