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