Skip to main content

RandomCoin

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

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl RandomCoin for RpoRandomCoin

Source§

type BaseField = BaseElement

Source§

type Hasher = Rpo256

Source§

fn new(seed: &[<RpoRandomCoin as RandomCoin>::BaseField]) -> RpoRandomCoin

Source§

fn reseed(&mut self, data: RpoDigest)

Source§

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

Source§

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

Source§

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

Source§

impl RandomCoin for RpxRandomCoin

Source§

type BaseField = BaseElement

Source§

type Hasher = Rpx256

Source§

fn new(seed: &[<RpxRandomCoin as RandomCoin>::BaseField]) -> RpxRandomCoin

Source§

fn reseed(&mut self, data: RpxDigest)

Source§

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

Source§

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

Source§

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

Implementors§

Source§

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