Trait miden_crypto::rand::RandomCoin
source · pub trait RandomCoin: Sync {
type BaseField: StarkField;
type Hasher: ElementHasher<BaseField = Self::BaseField>;
// Required methods
fn new(seed: &[Self::BaseField]) -> Self;
fn reseed(&mut self, data: <Self::Hasher as Hasher>::Digest);
fn reseed_with_int(&mut self, value: u64);
fn leading_zeros(&self) -> u32;
fn check_leading_zeros(&self, value: u64) -> u32;
fn draw<E>(&mut self) -> Result<E, RandomCoinError>
where E: FieldElement<BaseField = Self::BaseField>;
fn draw_integers(
&mut self,
num_values: usize,
domain_size: usize
) -> Result<Vec<usize>, RandomCoinError>;
}
Expand description
Pseudo-random element generator for finite fields.
A random coin can be used to draw elements uniformly at random from the specified base field or from any extension of the base field.
Internally we use a cryptographic hash function (which is specified via the Hasher
associated
type), to draw elements from the field.
Required Associated Types§
sourcetype BaseField: StarkField
type BaseField: StarkField
Base field for random elements which can be generated by this random coin.
sourcetype Hasher: ElementHasher<BaseField = Self::BaseField>
type Hasher: ElementHasher<BaseField = Self::BaseField>
Hash function which is used by the random coin to generate random field elements.
Required Methods§
sourcefn new(seed: &[Self::BaseField]) -> Self
fn new(seed: &[Self::BaseField]) -> Self
Returns a new random coin instantiated with the provided seed
.
sourcefn reseed(&mut self, data: <Self::Hasher as Hasher>::Digest)
fn reseed(&mut self, data: <Self::Hasher as Hasher>::Digest)
Reseeds the coin with the specified data by setting the new seed to hash(seed
|| data
).
sourcefn reseed_with_int(&mut self, value: u64)
fn reseed_with_int(&mut self, value: u64)
Reseeds the coin with the specified value by setting the new seed to hash(seed
||
value
).
sourcefn leading_zeros(&self) -> u32
fn leading_zeros(&self) -> u32
Returns the number of leading zeros in the seed if it is interpreted as an integer in big-endian byte order.
sourcefn check_leading_zeros(&self, value: u64) -> u32
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.
sourcefn draw<E>(&mut self) -> Result<E, RandomCoinError>where
E: FieldElement<BaseField = Self::BaseField>,
fn draw<E>(&mut self) -> Result<E, RandomCoinError>where E: FieldElement<BaseField = Self::BaseField>,
Returns the next pseudo-random field element.
Errors
Returns an error if a valid field element could not be generated after 1000 calls to the PRNG.
sourcefn draw_integers(
&mut self,
num_values: usize,
domain_size: usize
) -> Result<Vec<usize>, RandomCoinError>
fn draw_integers( &mut self, num_values: usize, domain_size: usize ) -> Result<Vec<usize>, RandomCoinError>
Returns a vector of unique integers selected from the range [0, domain_size).
Errors
Returns an error if the specified number of unique integers could not be generated after 1000 calls to the PRNG.
Panics
Panics if:
domain_size
is not a power of two.num_values
is greater than or equal todomain_size
.