Skip to content

Commit

Permalink
feat: add random_fill function body for RandomGaussianDist trait
Browse files Browse the repository at this point in the history
  • Loading branch information
enricobottazzi committed Feb 20, 2024
1 parent d5896d0 commit 98d1f6d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 93 deletions.
71 changes: 0 additions & 71 deletions src/core_crypto/guassian_sampler.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/core_crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod guassian_sampler;
pub mod matrix;
pub mod modulus;
pub mod ntt;
Expand Down
49 changes: 28 additions & 21 deletions src/core_crypto/random.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{borrow::Borrow, cell::RefCell};

use itertools::{izip, Itertools};
use num_traits::Zero;
use rand::{
distributions::{uniform::SampleUniform, Uniform},
thread_rng, CryptoRng, Rng, RngCore, SeedableRng,
};
use std::cell::RefCell;

use itertools::izip;
use rand::{distributions::Uniform, thread_rng, CryptoRng, Rng, RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
use rand_distr::{Distribution, Normal};

use super::matrix::{Matrix, MatrixMut, RowMut};

Expand All @@ -15,6 +12,8 @@ where
M: ?Sized,
{
type Parameters: ?Sized;
const MEAN: f64 = 0.0;
const STD_DEV: f64 = 3.2;
fn random_fill(&mut self, parameters: &Self::Parameters, container: &mut M);
}

Expand Down Expand Up @@ -157,25 +156,33 @@ where
parameters.len()
);

// TODO (Jay)
// izip!(container.iter_rows_mut(), parameters.iter()).for_each(|(r,
// qi)| { izip!(
// r.as_mut().iter_mut(),
// (&mut self.rng).sample_iter(Uniform::new(0, *qi))
// )
// .for_each(|(r_el, random_el)| *r_el = random_el);
// });
let normal = Normal::new(
<Self as RandomGaussianDist<M>>::MEAN,
<Self as RandomGaussianDist<M>>::STD_DEV,
)
.unwrap();

izip!(container.iter_rows_mut(), parameters.iter()).for_each(|(r, qi)| {
izip!(r.as_mut().iter_mut(), normal.sample_iter(&mut self.rng))
.for_each(|(r_el, random_el)| *r_el = (random_el as f64).round() as u64);
});
}
}

impl RandomGaussianDist<[u64]> for DefaultU64SeededRandomGenerator {
type Parameters = u64;
fn random_fill(&mut self, parameters: &Self::Parameters, container: &mut [u64]) {
// izip!(
// container.as_mut().iter_mut(),
// (&mut self.rng).sample_iter(Uniform::new(0, *parameters))
// )
// .for_each(|(r_el, random_el)| *r_el = random_el);
let normal = Normal::new(
<Self as RandomGaussianDist<[u64]>>::MEAN,
<Self as RandomGaussianDist<[u64]>>::STD_DEV,
)
.unwrap();

izip!(
container.as_mut().iter_mut(),
normal.sample_iter(&mut self.rng)
)
.for_each(|(r_el, random_el)| *r_el = (random_el as f64).round() as u64);
}
}

Expand Down

0 comments on commit 98d1f6d

Please sign in to comment.