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(
38 res.at_mut(res_col, limb),
39 sigma * scale,
40 bound * scale,
41 source,
42 )
43}
44
45pub fn vec_znx_add_normal_ref<R>(
46 base2k: usize,
47 res: &mut R,
48 res_col: usize,
49 k: usize,
50 sigma: f64,
51 bound: f64,
52 source: &mut Source,
53) where
54 R: VecZnxToMut,
55{
56 let mut res: VecZnx<&mut [u8]> = res.to_mut();
57 assert!(
58 (bound.log2().ceil() as i64) < 64,
59 "invalid bound: ceil(log2(bound))={} > 63",
60 (bound.log2().ceil() as i64)
61 );
62
63 let limb: usize = k.div_ceil(base2k) - 1;
64 let scale: f64 = (1 << ((limb + 1) * base2k - k)) as f64;
65 znx_add_normal_f64_ref(
66 res.at_mut(res_col, limb),
67 sigma * scale,
68 bound * scale,
69 source,
70 )
71}