pub struct RpoRandomCoin { /* 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:
- 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.
- As a result of the previous point, we don’t make use of an input buffer to accumulate seed material.
Implementations§
Source§impl RpoRandomCoin
impl RpoRandomCoin
Sourcepub fn new(seed: Word) -> Self
pub fn new(seed: Word) -> Self
Returns a new RpoRandomCoin initialize with the specified seed.
Sourcepub fn from_parts(state: [Felt; 12], current: usize) -> Self
pub fn from_parts(state: [Felt; 12], current: usize) -> Self
Returns an RpoRandomCoin instantiated from the provided components.
§Panics
Panics if current is smaller than 4 or greater than or equal to 12.
Sourcepub fn into_parts(self) -> ([Felt; 12], usize)
pub fn into_parts(self) -> ([Felt; 12], usize)
Returns components of this random coin.
Sourcepub fn fill_bytes(&mut self, dest: &mut [u8])
pub fn fill_bytes(&mut self, dest: &mut [u8])
Fills dest with random data.
Sourcepub fn draw_basefield(&mut self) -> Felt
pub fn draw_basefield(&mut self) -> Felt
Draws a random base field element from the random coin.
This method applies the Rpo256 permutation when the rate portion of the state is exhausted, then returns the next element from the rate portion.
Sourcepub fn draw(&mut self) -> Felt
pub fn draw(&mut self) -> Felt
Draws a random field element.
This is an alias for Self::draw_basefield.
Sourcepub fn draw_ext_field<E: ExtensionField<Felt>>(&mut self) -> E
pub fn draw_ext_field<E: ExtensionField<Felt>>(&mut self) -> E
Draws a random extension field element.
The extension field element is constructed by drawing E::DIMENSION base field elements
and interpreting them as basis coefficients.
Sourcepub fn reseed(&mut self, data: Word)
pub fn reseed(&mut self, data: Word)
Reseeds the random coin with additional entropy.
The provided data is added to the first half of the rate portion of the state,
then the Rpo256 permutation is applied. The buffer pointer is reset to the start
of the rate portion.
Sourcepub fn check_leading_zeros(&self, value: u64) -> u32
pub fn check_leading_zeros(&self, value: u64) -> u32
Checks how many leading zeros a value would produce when hashed with the current state.
This method creates a temporary copy of the state, adds the provided value to the first
rate element, applies the Rpo256 permutation, and returns the number of trailing zeros
in the resulting first rate element. This is useful for proof-of-work style computations.
Sourcepub fn draw_integers(
&mut self,
num_values: usize,
domain_size: usize,
nonce: u64,
) -> Vec<usize>
pub fn draw_integers( &mut self, num_values: usize, domain_size: usize, nonce: u64, ) -> Vec<usize>
Draws a specified number of unique random integers from a domain of a given size.
§Arguments
num_values- The number of unique integers to draw (must be less thandomain_size)domain_size- The size of the domain (must be a power of two)nonce- A nonce value that is absorbed into the state before drawing
§Returns
A vector of num_values unique integers in the range [0, domain_size)
§Panics
Panics if domain_size is not a power of two or if num_values >= domain_size.
Trait Implementations§
Source§impl Clone for RpoRandomCoin
impl Clone for RpoRandomCoin
Source§fn clone(&self) -> RpoRandomCoin
fn clone(&self) -> RpoRandomCoin
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RpoRandomCoin
impl Debug for RpoRandomCoin
Source§impl Deserializable for RpoRandomCoin
impl Deserializable for RpoRandomCoin
Source§fn read_from<R: ByteReader>(
source: &mut R,
) -> Result<Self, DeserializationError>
fn read_from<R: ByteReader>( source: &mut R, ) -> Result<Self, DeserializationError>
source, attempts to deserialize these bytes
into Self, and returns the result. Read moreSource§fn min_serialized_size() -> usize
fn min_serialized_size() -> usize
Source§fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
Source§fn read_from_bytes_with_budget(
bytes: &[u8],
budget: usize,
) -> Result<Self, DeserializationError>
fn read_from_bytes_with_budget( bytes: &[u8], budget: usize, ) -> Result<Self, DeserializationError>
Self from bytes with a byte budget limit. Read moreSource§impl FeltRng for RpoRandomCoin
impl FeltRng for RpoRandomCoin
Source§impl PartialEq for RpoRandomCoin
impl PartialEq for RpoRandomCoin
Source§impl RngCore for RpoRandomCoin
impl RngCore for RpoRandomCoin
Source§impl Serializable for RpoRandomCoin
impl Serializable for RpoRandomCoin
Source§fn write_into<W: ByteWriter>(&self, target: &mut W)
fn write_into<W: ByteWriter>(&self, target: &mut W)
self into bytes and writes these bytes into the target.Source§fn get_size_hint(&self) -> usize
fn get_size_hint(&self) -> usize
impl Copy for RpoRandomCoin
impl Eq for RpoRandomCoin
impl StructuralPartialEq for RpoRandomCoin
Auto Trait Implementations§
impl Freeze for RpoRandomCoin
impl RefUnwindSafe for RpoRandomCoin
impl Send for RpoRandomCoin
impl Sync for RpoRandomCoin
impl Unpin for RpoRandomCoin
impl UnwindSafe for RpoRandomCoin
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R> Rng for R
impl<R> Rng 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 moreSource§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
Source§fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
Source§fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
random to avoid conflict with the new gen keyword in Rust 2024.Rng::random.Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
random_rangeRng::random_range.Source§impl<R> TryRngCore for R
impl<R> TryRngCore for R
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest entirely with random data.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
UnwrapMut wrapper.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
RngCore to a RngReadAdapter.