Skip to main content

Random

Trait Random 

Source
pub trait Random: Sized {
    // Required methods
    fn rng_uniform_n_msb(t: &mut [Self], n: usize);
    fn rng_uniform_n_lsb(k: &mut [Self], n: usize);
    fn rng_uniform(k: &mut [Self]);
    fn openssl_uniform(k: &mut [Self]);
    fn rng_uniform_with_some_zeros(k: &mut [Self], probability: Self);
    fn vectorial_rng_normal(k: &mut [Self], mean: f64, std_dev: f64);
    fn vectorial_openssl_normal(k: &mut [Self], mean: f64, std_dev: f64);
    fn rng_normal(mean: f64, std_dev: f64) -> Self;
    fn openssl_normal(mean: f64, std_dev: f64) -> Self;
    fn openssl_normal_couple(mean: f64, std_dev: f64) -> (Self, Self);
}

Required Methods§

Source

fn rng_uniform_n_msb(t: &mut [Self], n: usize)

Source

fn rng_uniform_n_lsb(k: &mut [Self], n: usize)

Source

fn rng_uniform(k: &mut [Self])

Source

fn openssl_uniform(k: &mut [Self])

Source

fn rng_uniform_with_some_zeros(k: &mut [Self], probability: Self)

Source

fn vectorial_rng_normal(k: &mut [Self], mean: f64, std_dev: f64)

Source

fn vectorial_openssl_normal(k: &mut [Self], mean: f64, std_dev: f64)

Source

fn rng_normal(mean: f64, std_dev: f64) -> Self

Source

fn openssl_normal(mean: f64, std_dev: f64) -> Self

Source

fn openssl_normal_couple(mean: f64, std_dev: f64) -> (Self, Self)

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.

Implementations on Foreign Types§

Source§

impl Random for u32

Source§

fn rng_uniform_n_msb(t: &mut [u32], n: usize)

Fills a Torus tensor with uniform random in [0,2**n[ on the MSB

§Arguments
  • t - a Torus tensor (output)
  • n - number of bit to fill with random
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random in the MSB (5 bits)
Random::rng_uniform_n_msb(&mut t, 5);
Source§

fn rng_uniform_n_lsb(k: &mut [u32], n: usize)

Fills a Torus tensor with uniform random in [0,2**n[ on the LSB

§Arguments
  • k - a Torus tensor (output)
  • n - number of bit to fill with random
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random in the LSB (5 bits)
Random::rng_uniform_n_lsb(&mut t, 5);
Source§

fn rng_uniform(k: &mut [u32])

Fills a Torus tensor with uniform random

§Arguments
  • k - a Torus tensor (output)
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random
Random::rng_uniform(&mut t);
Source§

fn openssl_uniform(k: &mut [u32])

Fills a Torus tensor with uniform random using openssl CSPTRNG

§Arguments
  • k - a Torus tensor (output)
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random
Random::openssl_uniform(&mut t);
Source§

fn rng_uniform_with_some_zeros(k: &mut [u32], probability: u32)

Either fills elements of k with uniform random or with a zero according to probability if probability is set to 0.2 (0.2 * 2**TORUS_BIT), then there will be approximately 1/5 random elements and the rest will be set to zero

§Arguments
  • k - Torus slice (output)
  • probability - the probability for an element to be uniformly random
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;
use concrete_lib::Types;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random 20 percents of the time
Random::rng_uniform_with_some_zeros(
    &mut t,
    (0.2 * f64::powi(2., <Torus as Types>::TORUS_BIT as i32)) as Torus,
);
Source§

fn vectorial_rng_normal(k: &mut [u32], mean: f64, std_dev: f64)

Fills a Torus tensor with normal random

§Arguments
  • k - a Torus tensor (output)
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// allocation of a tensor
let mut t: Vec<Torus> = vec![0; k];

// fill with random
Random::vectorial_rng_normal(&mut t, 0., std_dev);
Source§

fn vectorial_openssl_normal(k: &mut [u32], mean: f64, std_dev: f64)

Fills a Torus tensor with normal random using a CSPRNG and Box-Muller algorithm

§Arguments
  • k - a Torus tensor (output)
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// allocation of a tensor
let mut t: Vec<Torus> = vec![0; k];

// fill with random
Random::vectorial_openssl_normal(&mut t, 0., std_dev);
Source§

fn rng_normal(mean: f64, std_dev: f64) -> u32

Returns a Torus element with normal random

§Arguments
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Output
  • returns the Torus element sampled from the desired distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// get a random element
let elt: Torus = Random::rng_normal(0., std_dev);
Source§

fn openssl_normal(mean: f64, std_dev: f64) -> u32

Returns a Torus element with normal random using a CSPRNG and Box-Muller algorithm

§Warning
  • This function discards 1/2 of the sample
§Arguments
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Output
  • returns the Torus element sampled from the desired distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// get a random element
let elt: Torus = Random::openssl_normal(0., std_dev);
Source§

fn openssl_normal_couple(mean: f64, std_dev: f64) -> (u32, u32)

Returns a Torus element with normal random using a CSPRNG and Box-Muller algorithm

§Arguments
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Output
  • returns the Torus element sampled from the desired distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u32;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// get a random element
let elt: Torus = Random::openssl_normal(0., std_dev);
Source§

impl Random for u64

Source§

fn rng_uniform_n_msb(t: &mut [u64], n: usize)

Fills a Torus tensor with uniform random in [0,2**n[ on the MSB

§Arguments
  • t - a Torus tensor (output)
  • n - number of bit to fill with random
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random in the MSB (5 bits)
Random::rng_uniform_n_msb(&mut t, 5);
Source§

fn rng_uniform_n_lsb(k: &mut [u64], n: usize)

Fills a Torus tensor with uniform random in [0,2**n[ on the LSB

§Arguments
  • k - a Torus tensor (output)
  • n - number of bit to fill with random
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random in the LSB (5 bits)
Random::rng_uniform_n_lsb(&mut t, 5);
Source§

fn rng_uniform(k: &mut [u64])

Fills a Torus tensor with uniform random

§Arguments
  • k - a Torus tensor (output)
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random
Random::rng_uniform(&mut t);
Source§

fn openssl_uniform(k: &mut [u64])

Fills a Torus tensor with uniform random using openssl CSPTRNG

§Arguments
  • k - a Torus tensor (output)
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random
Random::openssl_uniform(&mut t);
Source§

fn rng_uniform_with_some_zeros(k: &mut [u64], probability: u64)

Either fills elements of k with uniform random or with a zero according to probability if probability is set to 0.2 (0.2 * 2**TORUS_BIT), then there will be approximately 1/5 random elements and the rest will be set to zero

§Arguments
  • k - Torus slice (output)
  • probability - the probability for an element to be uniformly random
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;
use concrete_lib::Types;

// allocation of a torus
let mut t: Vec<Torus> = vec![0; 100];

// fill with uniform random 20 percents of the time
Random::rng_uniform_with_some_zeros(
    &mut t,
    (0.2 * f64::powi(2., <Torus as Types>::TORUS_BIT as i32)) as Torus,
);
Source§

fn vectorial_rng_normal(k: &mut [u64], mean: f64, std_dev: f64)

Fills a Torus tensor with normal random

§Arguments
  • k - a Torus tensor (output)
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// allocation of a tensor
let mut t: Vec<Torus> = vec![0; k];

// fill with random
Random::vectorial_rng_normal(&mut t, 0., std_dev);
Source§

fn vectorial_openssl_normal(k: &mut [u64], mean: f64, std_dev: f64)

Fills a Torus tensor with normal random using a CSPRNG and Box-Muller algorithm

§Arguments
  • k - a Torus tensor (output)
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// allocation of a tensor
let mut t: Vec<Torus> = vec![0; k];

// fill with random
Random::vectorial_openssl_normal(&mut t, 0., std_dev);
Source§

fn rng_normal(mean: f64, std_dev: f64) -> u64

Returns a Torus element with normal random

§Arguments
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Output
  • returns the Torus element sampled from the desired distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// get a random element
let elt: Torus = Random::rng_normal(0., std_dev);
Source§

fn openssl_normal(mean: f64, std_dev: f64) -> u64

Returns a Torus element with normal random using a CSPRNG and Box-Muller algorithm

§Warning
  • This function discards 1/2 of the sample
§Arguments
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Output
  • returns the Torus element sampled from the desired distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// get a random element
let elt: Torus = Random::openssl_normal(0., std_dev);
Source§

fn openssl_normal_couple(mean: f64, std_dev: f64) -> (u64, u64)

Returns a Torus element with normal random using a CSPRNG and Box-Muller algorithm

§Arguments
  • mean - mean of the normal distribution
  • std_dev - standard deviation of the normal distribution
§Output
  • returns the Torus element sampled from the desired distribution
§Example
use concrete_lib::core_api::math::Random;
type Torus = u64;

// settings
let std_dev: f64 = f64::powi(2., -20);
let k = 100;

// get a random element
let elt: Torus = Random::openssl_normal(0., std_dev);

Implementors§