Skip to main content

RandomMod

Trait RandomMod 

Source
pub trait RandomMod: Sized + Zero {
    // Required method
    fn try_random_mod_vartime<R: TryRng + ?Sized>(
        rng: &mut R,
        modulus: &NonZero<Self>,
    ) -> Result<Self, R::Error>;

    // Provided methods
    fn random_mod_vartime<R: Rng + ?Sized>(
        rng: &mut R,
        modulus: &NonZero<Self>,
    ) -> Self { ... }
    fn random_mod<R: Rng + ?Sized>(rng: &mut R, modulus: &NonZero<Self>) -> Self { ... }
    fn try_random_mod<R: TryRng + ?Sized>(
        rng: &mut R,
        modulus: &NonZero<Self>,
    ) -> Result<Self, R::Error> { ... }
}
Available on crate feature rand_core only.
Expand description

Modular random number generation support.

Required Methods§

Source

fn try_random_mod_vartime<R: TryRng + ?Sized>( rng: &mut R, modulus: &NonZero<Self>, ) -> Result<Self, R::Error>

Generate a random number which is less than a given modulus.

This uses rejection sampling.

As a result, it runs in variable time that depends in part on modulus. If the generator rng is cryptographically secure (for example, it implements CryptoRng), then this is guaranteed not to leak anything about the output value aside from it being less than modulus.

§Errors
  • Returns R::Error in the event the RNG experienced an internal failure.

Provided Methods§

Source

fn random_mod_vartime<R: Rng + ?Sized>( rng: &mut R, modulus: &NonZero<Self>, ) -> Self

Generate a random number which is less than a given modulus.

This uses rejection sampling.

As a result, it runs in variable time that depends in part on modulus. If the generator rng is cryptographically secure (for example, it implements CryptoRng), then this is guaranteed not to leak anything about the output value aside from it being less than modulus.

Source

fn random_mod<R: Rng + ?Sized>(rng: &mut R, modulus: &NonZero<Self>) -> Self

👎Deprecated since 0.7.0: please use random_mod_vartime instead

Generate a random number which is less than a given modulus.

This uses rejection sampling.

As a result, it runs in variable time that depends in part on modulus. If the generator rng is cryptographically secure (for example, it implements CryptoRng), then this is guaranteed not to leak anything about the output value aside from it being less than modulus.

Source

fn try_random_mod<R: TryRng + ?Sized>( rng: &mut R, modulus: &NonZero<Self>, ) -> Result<Self, R::Error>

👎Deprecated since 0.7.0: please use try_random_mod_vartime instead

Generate a random number which is less than a given modulus.

This uses rejection sampling.

As a result, it runs in variable time that depends in part on modulus. If the generator rng is cryptographically secure (for example, it implements CryptoRng), then this is guaranteed not to leak anything about the output value aside from it being less than modulus.

§Errors
  • Returns R::Error in the event the RNG experienced an internal failure.

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§

Source§

impl RandomMod for BoxedUint

Available on crate feature alloc only.
Source§

impl RandomMod for Limb

Source§

impl<const LIMBS: usize> RandomMod for Uint<LIMBS>