1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::math::random::{Gaussian, RandomGenerable, RandomGenerator};
use crate::math::tensor::Tensor;
use crate::math::torus::UnsignedTorus;
use concrete_commons::dispersion::DispersionParameter;

/// A random number generator which can be used to generate secret keys.
pub struct SecretRandomGenerator(RandomGenerator);

impl SecretRandomGenerator {
    /// Creates a new generator, optionally seeding it with the given value.
    pub fn new(seed: Option<u128>) -> SecretRandomGenerator {
        SecretRandomGenerator(RandomGenerator::new(seed))
    }

    /// Returns the number of remaining bytes, if the generator is bounded.
    pub fn remaining_bytes(&self) -> Option<usize> {
        self.0.remaining_bytes()
    }

    /// Returns whether the generator is bounded.
    pub fn is_bounded(&self) -> bool {
        self.0.is_bounded()
    }

    // Returns a tensor with random uniform binary values.
    pub(crate) fn random_binary_tensor<Scalar>(&mut self, length: usize) -> Tensor<Vec<Scalar>>
    where
        Scalar: UnsignedTorus,
    {
        self.0.random_uniform_binary_tensor(length)
    }

    // Returns a tensor with random uniform ternary values.
    pub(crate) fn random_ternary_tensor<Scalar>(&mut self, length: usize) -> Tensor<Vec<Scalar>>
    where
        Scalar: UnsignedTorus,
    {
        self.0.random_uniform_ternary_tensor(length)
    }

    // Returns a tensor with random uniform values.
    pub(crate) fn random_uniform_tensor<Scalar>(&mut self, length: usize) -> Tensor<Vec<Scalar>>
    where
        Scalar: UnsignedTorus,
    {
        self.0.random_uniform_tensor(length)
    }

    // Returns a tensor with random gaussian values.
    pub(crate) fn random_gaussian_tensor<Scalar>(&mut self, length: usize) -> Tensor<Vec<Scalar>>
    where
        (Scalar, Scalar): RandomGenerable<Gaussian<f64>>,
        Scalar: UnsignedTorus,
    {
        self.0
            .random_gaussian_tensor(length, 0.0, Scalar::GAUSSIAN_KEY_LOG_STD.get_standard_dev())
    }
}