Trait winter_crypto::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 check_leading_zeros(&self, value: u64) -> u32;
    fn draw<E: FieldElement<BaseField = Self::BaseField>>(
        &mut self
    ) -> Result<E, RandomCoinError>;
    fn draw_integers(
        &mut self,
        num_values: usize,
        domain_size: usize,
        nonce: u64
    ) -> 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§

source

type BaseField: StarkField

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

source

type Hasher: ElementHasher<BaseField = Self::BaseField>

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

Required Methods§

source

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

Returns a new random coin instantiated with the provided seed.

source

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).

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 = Self::BaseField>>( &mut self ) -> Result<E, RandomCoinError>

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.

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.

§Errors

Returns an error if the specified number of 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 to domain_size.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<B: StarkField, H: ElementHasher<BaseField = B>> RandomCoin for DefaultRandomCoin<H>

§

type BaseField = B

§

type Hasher = H