poulpy/poulpy-hal/src/reference/zn/sampling.rs
poulpy-hal/src/reference/zn/sampling.rsRaw
1use crate::{
2 layouts::{Zn, ZnToMut, 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 zn_fill_uniform<R>(n: usize, base2k: usize, res: &mut R, res_col: usize, source: &mut Source)
8where
9 R: ZnToMut,
10{
11 let mut res: Zn<&mut [u8]> = res.to_mut();
12 for j in 0..res.size() {
13 znx_fill_uniform_ref(base2k, &mut res.at_mut(res_col, j)[..n], source)
14 }
15}
16
17#[allow(clippy::too_many_arguments)]
18pub fn zn_fill_normal<R>(
19 n: usize,
20 base2k: usize,
21 res: &mut R,
22 res_col: usize,
23 k: usize,
24 source: &mut Source,
25 sigma: f64,
26 bound: f64,
27) where
28 R: ZnToMut,
29{
30 let mut res: Zn<&mut [u8]> = res.to_mut();
31 assert!(
32 (bound.log2().ceil() as i64) < 64,
33 "invalid bound: ceil(log2(bound))={} > 63",
34 (bound.log2().ceil() as i64)
35 );
36
37 let limb: usize = k.div_ceil(base2k) - 1;
38 let scale: f64 = (1 << ((limb + 1) * base2k - k)) as f64;
39 znx_fill_normal_f64_ref(
40 &mut res.at_mut(res_col, limb)[..n],
41 sigma * scale,
42 bound * scale,
43 source,
44 )
45}
46
47#[allow(clippy::too_many_arguments)]
48pub fn zn_add_normal<R>(
49 n: usize,
50 base2k: usize,
51 res: &mut R,
52 res_col: usize,
53 k: usize,
54 source: &mut Source,
55 sigma: f64,
56 bound: f64,
57) where
58 R: ZnToMut,
59{
60 let mut res: Zn<&mut [u8]> = res.to_mut();
61 assert!(
62 (bound.log2().ceil() as i64) < 64,
63 "invalid bound: ceil(log2(bound))={} > 63",
64 (bound.log2().ceil() as i64)
65 );
66
67 let limb: usize = k.div_ceil(base2k) - 1;
68 let scale: f64 = (1 << ((limb + 1) * base2k - k)) as f64;
69 znx_add_normal_f64_ref(
70 &mut res.at_mut(res_col, limb)[..n],
71 sigma * scale,
72 bound * scale,
73 source,
74 )
75}