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