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>(basek: 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(basek, res.at_mut(res_col, j), source)
14 }
15}
16
17pub fn vec_znx_fill_normal_ref<R>(
18 basek: 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(basek) - 1;
36 let scale: f64 = (1 << ((limb + 1) * basek - 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>(basek: usize, res: &mut R, res_col: usize, k: usize, sigma: f64, bound: f64, source: &mut Source)
46where
47 R: VecZnxToMut,
48{
49 let mut res: VecZnx<&mut [u8]> = res.to_mut();
50 assert!(
51 (bound.log2().ceil() as i64) < 64,
52 "invalid bound: ceil(log2(bound))={} > 63",
53 (bound.log2().ceil() as i64)
54 );
55
56 let limb: usize = k.div_ceil(basek) - 1;
57 let scale: f64 = (1 << ((limb + 1) * basek - k)) as f64;
58 znx_add_normal_f64_ref(
59 res.at_mut(res_col, limb),
60 sigma * scale,
61 bound * scale,
62 source,
63 )
64}