miden_crypto::rand

Struct RpxRandomCoin

source
pub struct RpxRandomCoin { /* private fields */ }
Expand description

A simplified version of the SPONGE_PRG reseedable pseudo-random number generator algorithm described in https://eprint.iacr.org/2011/499.pdf.

The simplification is related to the following facts:

  1. A call to the reseed method implies one and only one call to the permutation function. This is possible because in our case we never reseed with more than 4 field elements.
  2. As a result of the previous point, we don’t make use of an input buffer to accumulate seed material.

Implementations§

source§

impl RpxRandomCoin

source

pub fn new(seed: Word) -> Self

Returns a new RpxRandomCoin initialize with the specified seed.

source

pub fn from_parts(state: [Felt; 12], current: usize) -> Self

Returns an RpxRandomCoin instantiated from the provided components.

§Panics

Panics if current is smaller than 4 or greater than or equal to 12.

source

pub fn into_parts(self) -> ([Felt; 12], usize)

Returns components of this random coin.

source

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

Fills dest with random data.

Trait Implementations§

source§

impl Clone for RpxRandomCoin

source§

fn clone(&self) -> RpxRandomCoin

Returns a copy of the value. Read more
1.6.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for RpxRandomCoin

source§

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

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

impl Deserializable for RpxRandomCoin

source§

fn read_from<R: ByteReader>( source: &mut R, ) -> Result<Self, DeserializationError>

Reads a sequence of bytes from the provided source, attempts to deserialize these bytes into Self, and returns the result. Read more
source§

fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>

Attempts to deserialize the provided bytes into Self and returns the result. Read more
source§

impl FeltRng for RpxRandomCoin

source§

fn draw_element(&mut self) -> Felt

Draw, uniformly at random, a base field element.
source§

fn draw_word(&mut self) -> Word

Draw, uniformly at random, a Word.
source§

impl PartialEq for RpxRandomCoin

source§

fn eq(&self, other: &RpxRandomCoin) -> bool

Tests for self and other values to be equal, and is used by ==.
1.6.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 RandomCoin for RpxRandomCoin

source§

type BaseField = BaseElement

Base field for random elements which can be generated by this random coin.
source§

type Hasher = Rpx256

Hash function which is used by the random coin to generate random field elements.
source§

fn new(seed: &[Self::BaseField]) -> Self

Returns a new random coin instantiated with the provided seed.
source§

fn reseed(&mut self, data: RpxDigest)

Reseeds the coin with the specified data by setting the new seed to hash(seed || data).
source§

fn check_leading_zeros(&self, value: u64) -> u32

Computes hash(seed || value) and returns the number of leading zeros in the resulting value if it is interpreted as an integer in big-endian byte order.
source§

fn draw<E: FieldElement<BaseField = Felt>>( &mut self, ) -> Result<E, RandomCoinError>

Returns the next pseudo-random field element. Read more
source§

fn draw_integers( &mut self, num_values: usize, domain_size: usize, nonce: u64, ) -> Result<Vec<usize>, RandomCoinError>

Returns a vector of integers selected from the range [0, domain_size) after it reseeds the coin with a nonce. Read more
source§

impl RngCore for RpxRandomCoin

source§

fn next_u32(&mut self) -> u32

Return the next random u32. Read more
source§

fn next_u64(&mut self) -> u64

Return the next random u64. Read more
source§

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

Fill dest with random data. Read more
source§

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

Fill dest entirely with random data. Read more
source§

impl Serializable for RpxRandomCoin

source§

fn write_into<W: ByteWriter>(&self, target: &mut W)

Serializes self into bytes and writes these bytes into the target.
source§

fn to_bytes(&self) -> Vec<u8>

Serializes self into a vector of bytes.
source§

fn get_size_hint(&self) -> usize

Returns an estimate of how many bytes are needed to represent self. Read more
source§

impl Copy for RpxRandomCoin

source§

impl Eq for RpxRandomCoin

source§

impl StructuralPartialEq for RpxRandomCoin

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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: RngCore + ?Sized,

source§

fn gen<T>(&mut self) -> T

Return a random value supporting the Standard distribution. Read more
source§

fn gen_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
source§

fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
source§

fn fill<T>(&mut self, dest: &mut T)
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
source§

fn try_fill<T>(&mut self, dest: &mut T) -> Result<(), Error>
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
source§

fn gen_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
source§

fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. I.e. gen_ratio(2, 3) has chance of 2 in 3, or about 67%, of returning true. If numerator == denominator, then the returned value is guaranteed to be true. If numerator == 0, then the returned value is guaranteed to be false. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V