poulpy/rlwe/benches/gadget_product.rs
rlwe/benches/gadget_product.rsRaw
1use base2k::{BACKEND, Module, Sampling, SvpPPolOps, VecZnx, VecZnxDft, VecZnxDftOps, VecZnxOps, VmpPMat, alloc_aligned_u8};
2use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
3use rlwe::{
4 ciphertext::{Ciphertext, new_gadget_ciphertext},
5 elem::ElemCommon,
6 encryptor::{encrypt_grlwe_sk, encrypt_grlwe_sk_tmp_bytes},
7 gadget_product::{gadget_product_core, gadget_product_core_tmp_bytes},
8 keys::SecretKey,
9 parameters::{Parameters, ParametersLiteral},
10};
11use sampling::source::Source;
12
13fn bench_gadget_product_inplace(c: &mut Criterion) {
14 fn runner<'a>(
15 module: &'a Module,
16 res_dft_0: &'a mut VecZnxDft,
17 res_dft_1: &'a mut VecZnxDft,
18 a: &'a VecZnx,
19 b: &'a Ciphertext<VmpPMat>,
20 b_cols: usize,
21 tmp_bytes: &'a mut [u8],
22 ) -> Box<dyn FnMut() + 'a> {
23 Box::new(move || {
24 gadget_product_core(module, res_dft_0, res_dft_1, a, b, b_cols, tmp_bytes);
25 })
26 }
27
28 let mut b: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime> = c.benchmark_group("gadget_product_inplace");
29
30 for log_n in 10..11 {
31 let params_lit: ParametersLiteral = ParametersLiteral {
32 backend: BACKEND::FFT64,
33 log_n: log_n,
34 log_q: 32,
35 log_p: 0,
36 log_base2k: 16,
37 log_scale: 20,
38 xe: 3.2,
39 xs: 128,
40 };
41
42 let params: Parameters = Parameters::new(¶ms_lit);
43
44 let mut tmp_bytes: Vec<u8> = alloc_aligned_u8(
45 params.encrypt_rlwe_sk_tmp_bytes(params.log_q())
46 | gadget_product_core_tmp_bytes(
47 params.module(),
48 params.log_base2k(),
49 params.log_q(),
50 params.log_q(),
51 params.cols_q(),
52 params.log_qp(),
53 )
54 | encrypt_grlwe_sk_tmp_bytes(
55 params.module(),
56 params.log_base2k(),
57 params.cols_qp(),
58 params.log_qp(),
59 ),
60 );
61
62 let mut source: Source = Source::new([3; 32]);
63
64 let mut sk0: SecretKey = SecretKey::new(params.module());
65 let mut sk1: SecretKey = SecretKey::new(params.module());
66 sk0.fill_ternary_hw(params.xs(), &mut source);
67 sk1.fill_ternary_hw(params.xs(), &mut source);
68
69 let mut source_xe: Source = Source::new([4; 32]);
70 let mut source_xa: Source = Source::new([5; 32]);
71
72 let mut sk0_svp_ppol: base2k::SvpPPol = params.module().new_svp_ppol();
73 params.module().svp_prepare(&mut sk0_svp_ppol, &sk0.0);
74
75 let mut sk1_svp_ppol: base2k::SvpPPol = params.module().new_svp_ppol();
76 params.module().svp_prepare(&mut sk1_svp_ppol, &sk1.0);
77
78 let mut gadget_ct: Ciphertext<VmpPMat> = new_gadget_ciphertext(
79 params.module(),
80 params.log_base2k(),
81 params.cols_q(),
82 params.log_qp(),
83 );
84
85 encrypt_grlwe_sk(
86 params.module(),
87 &mut gadget_ct,
88 &sk0.0,
89 &sk1_svp_ppol,
90 &mut source_xa,
91 &mut source_xe,
92 params.xe(),
93 &mut tmp_bytes,
94 );
95
96 let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(params.log_q());
97
98 params.encrypt_rlwe_sk(
99 &mut ct,
100 None,
101 &sk0_svp_ppol,
102 &mut source_xa,
103 &mut source_xe,
104 &mut tmp_bytes,
105 );
106
107 let mut res_dft_0: VecZnxDft = params.module().new_vec_znx_dft(gadget_ct.cols());
108 let mut res_dft_1: VecZnxDft = params.module().new_vec_znx_dft(gadget_ct.cols());
109
110 let mut a: VecZnx = params.module().new_vec_znx(0, params.cols_q());
111 params
112 .module()
113 .fill_uniform(params.log_base2k(), &mut a, params.cols_q(), &mut source_xa);
114
115 let b_cols: usize = gadget_ct.cols();
116
117 let runners: [(String, Box<dyn FnMut()>); 1] = [(format!("gadget_product"), {
118 runner(
119 params.module(),
120 &mut res_dft_0,
121 &mut res_dft_1,
122 &mut a,
123 &gadget_ct,
124 b_cols,
125 &mut tmp_bytes,
126 )
127 })];
128
129 for (name, mut runner) in runners {
130 let id: BenchmarkId = BenchmarkId::new(name, format!("n={}", 1 << log_n));
131 b.bench_with_input(id, &(), |b: &mut criterion::Bencher<'_>, _| {
132 b.iter(&mut runner)
133 });
134 }
135 }
136}
137
138criterion_group!(benches, bench_gadget_product_inplace);
139criterion_main!(benches);