RandomNonZero

Trait RandomNonZero 

Source
pub trait RandomNonZero:
    Sized
    + Zero
    + Random {
    // Provided methods
    fn random_non_zero(rng: impl CryptoRngCore) -> Result<Self, PrimitiveError> { ... }
    fn random_n_non_zero<Container: FromIterator<Self>>(
        rng: impl CryptoRngCore,
        size: usize,
    ) -> Result<Container, PrimitiveError> { ... }
}
Expand description

Generate random non-zero values.

Provided Methods§

Source

fn random_non_zero(rng: impl CryptoRngCore) -> Result<Self, PrimitiveError>

Generates a random non-zero value.

May error out if it cannot find a non-zero value after a certain number of tries, defined so that:

Prob(out == 0) <= 2^-(λ) as long as Prob(random()==0) <= 2^-(size_of::<Self>)

The default implementation repetitively calls random() (rejection sampling). As such, it is not constant-time, but the side channel leakage should not impact security as long as the rng is evaluated in constant time and produces uniformly random values.

If needed, override with a constant-time implementation using ConditionallySelectable and always running for a fixed number of iterations, potentially returning a zero value (with overwhelmingly low probability).

Source

fn random_n_non_zero<Container: FromIterator<Self>>( rng: impl CryptoRngCore, size: usize, ) -> Result<Container, PrimitiveError>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§