arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-hal/src/reference/vec_znx/sampling.rs
poulpy-hal/src/reference/vec_znx/sampling.rsRaw
 1use crate::{
 2    layouts::{VecZnx, VecZnxToMut, ZnxInfos, ZnxViewMut},
 3    reference::znx::{znx_add_normal_f64_ref, znx_fill_normal_f64_ref, znx_fill_uniform_ref},
 4    source::Source,
 5};
 6
 7pub fn vec_znx_fill_uniform_ref<R>(base2k: usize, res: &mut R, res_col: usize, source: &mut Source)
 8where
 9    R: VecZnxToMut,
10{
11    let mut res: VecZnx<&mut [u8]> = res.to_mut();
12    for j in 0..res.size() {
13        znx_fill_uniform_ref(base2k, res.at_mut(res_col, j), source)
14    }
15}
16
17pub fn vec_znx_fill_normal_ref<R>(
18    base2k: usize,
19    res: &mut R,
20    res_col: usize,
21    k: usize,
22    sigma: f64,
23    bound: f64,
24    source: &mut Source,
25) where
26    R: VecZnxToMut,
27{
28    let mut res: VecZnx<&mut [u8]> = res.to_mut();
29    assert!(
30        (bound.log2().ceil() as i64) < 64,
31        "invalid bound: ceil(log2(bound))={} > 63",
32        (bound.log2().ceil() as i64)
33    );
34
35    let limb: usize = k.div_ceil(base2k) - 1;
36    let scale: f64 = (1 << ((limb + 1) * base2k - k)) as f64;
37    znx_fill_normal_f64_ref(res.at_mut(res_col, limb), sigma * scale, bound * scale, source)
38}
39
40pub fn vec_znx_add_normal_ref<R>(
41    base2k: usize,
42    res: &mut R,
43    res_col: usize,
44    k: usize,
45    sigma: f64,
46    bound: f64,
47    source: &mut Source,
48) where
49    R: VecZnxToMut,
50{
51    let mut res: VecZnx<&mut [u8]> = res.to_mut();
52    assert!(
53        (bound.log2().ceil() as i64) < 64,
54        "invalid bound: ceil(log2(bound))={} > 63",
55        (bound.log2().ceil() as i64)
56    );
57
58    let limb: usize = k.div_ceil(base2k) - 1;
59    let scale: f64 = (1 << ((limb + 1) * base2k - k)) as f64;
60    znx_add_normal_f64_ref(res.at_mut(res_col, limb), sigma * scale, bound * scale, source)
61}