poulpy/core/benches/keyswitch_glwe_fft64.rs
core/benches/keyswitch_glwe_fft64.rsRaw
1use core::layouts::{
2 GGLWEAutomorphismKey, GGLWESwitchingKey, GLWECiphertext, GLWESecret, Infos,
3 prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc},
4};
5use std::{hint::black_box, time::Duration};
6
7use backend::{
8 hal::{
9 api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
10 layouts::{Module, ScratchOwned},
11 },
12 implementation::cpu_spqlios::FFT64,
13};
14use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
15use sampling::source::Source;
16
17fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
18 let mut group = c.benchmark_group("keyswitch_glwe_fft64");
19
20 struct Params {
21 log_n: usize,
22 basek: usize,
23 k_ct_in: usize,
24 k_ct_out: usize,
25 k_ksk: usize,
26 digits: usize,
27 rank_in: usize,
28 rank_out: usize,
29 }
30
31 fn runner(p: Params) -> impl FnMut() {
32 let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
33
34 let n = module.n();
35 let basek: usize = p.basek;
36 let k_rlwe_in: usize = p.k_ct_in;
37 let k_rlwe_out: usize = p.k_ct_out;
38 let k_grlwe: usize = p.k_ksk;
39 let rank_in: usize = p.rank_in;
40 let rank_out: usize = p.rank_out;
41 let digits: usize = p.digits;
42
43 let rows: usize = p.k_ct_in.div_ceil(p.basek * digits);
44 let sigma: f64 = 3.2;
45
46 let mut ksk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_grlwe, rows, digits, rank_out);
47 let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_rlwe_in, rank_in);
48 let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_rlwe_out, rank_out);
49
50 let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
51 GGLWESwitchingKey::encrypt_sk_scratch_space(&module, n, basek, ksk.k(), rank_in, rank_out)
52 | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct_in.k())
53 | GLWECiphertext::keyswitch_scratch_space(
54 &module,
55 n,
56 basek,
57 ct_out.k(),
58 ct_in.k(),
59 ksk.k(),
60 digits,
61 rank_in,
62 rank_out,
63 ),
64 );
65
66 let mut source_xs = Source::new([0u8; 32]);
67 let mut source_xe = Source::new([0u8; 32]);
68 let mut source_xa = Source::new([0u8; 32]);
69
70 let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in);
71 sk_in.fill_ternary_prob(0.5, &mut source_xs);
72 let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow());
73
74 let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
75 sk_out.fill_ternary_prob(0.5, &mut source_xs);
76
77 ksk.encrypt_sk(
78 &module,
79 -1,
80 &sk_in,
81 &mut source_xa,
82 &mut source_xe,
83 sigma,
84 scratch.borrow(),
85 );
86
87 ct_in.encrypt_zero_sk(
88 &module,
89 &sk_in_dft,
90 &mut source_xa,
91 &mut source_xe,
92 sigma,
93 scratch.borrow(),
94 );
95
96 let ksk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, _> = ksk.prepare_alloc(&module, scratch.borrow());
97
98 move || {
99 black_box(ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()));
100 }
101 }
102
103 let digits: usize = 1;
104 let basek: usize = 19;
105
106 let params_set: Vec<Params> = vec![Params {
107 log_n: 15,
108 basek: basek,
109 k_ct_in: 874 - digits * basek,
110 k_ct_out: 874 - digits * basek,
111 k_ksk: 874,
112 digits: digits,
113 rank_in: 1,
114 rank_out: 1,
115 }];
116
117 for params in params_set {
118 let id = BenchmarkId::new("KEYSWITCH_GLWE_FFT64", "");
119 let mut runner = runner(params);
120 group.sample_size(500);
121 group.measurement_time(Duration::from_secs(40));
122 group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
123 }
124
125 group.finish();
126}
127
128fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
129 let mut group = c.benchmark_group("keyswitch_glwe_inplace_fft64");
130
131 struct Params {
132 log_n: usize,
133 basek: usize,
134 k_ct: usize,
135 k_ksk: usize,
136 rank: usize,
137 }
138
139 fn runner(p: Params) -> impl FnMut() {
140 let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
141
142 let n = module.n();
143 let basek: usize = p.basek;
144 let k_ct: usize = p.k_ct;
145 let k_ksk: usize = p.k_ksk;
146 let rank: usize = p.rank;
147 let digits: usize = 1;
148
149 let rows: usize = p.k_ct.div_ceil(p.basek);
150 let sigma: f64 = 3.2;
151
152 let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank, rank);
153 let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
154
155 let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
156 GGLWESwitchingKey::encrypt_sk_scratch_space(&module, n, basek, ksk.k(), rank, rank)
157 | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct.k())
158 | GLWECiphertext::keyswitch_inplace_scratch_space(&module, n, basek, ct.k(), ksk.k(), digits, rank),
159 );
160
161 let mut source_xs: Source = Source::new([0u8; 32]);
162 let mut source_xe: Source = Source::new([0u8; 32]);
163 let mut source_xa: Source = Source::new([0u8; 32]);
164
165 let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
166 sk_in.fill_ternary_prob(0.5, &mut source_xs);
167 let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow());
168
169 let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
170 sk_out.fill_ternary_prob(0.5, &mut source_xs);
171
172 ksk.encrypt_sk(
173 &module,
174 &sk_in,
175 &sk_out,
176 &mut source_xa,
177 &mut source_xe,
178 sigma,
179 scratch.borrow(),
180 );
181
182 ct.encrypt_zero_sk(
183 &module,
184 &sk_in_dft,
185 &mut source_xa,
186 &mut source_xe,
187 sigma,
188 scratch.borrow(),
189 );
190
191 let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, FFT64> = ksk.prepare_alloc(&module, scratch.borrow());
192
193 move || {
194 black_box(ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()));
195 }
196 }
197
198 let params_set: Vec<Params> = vec![Params {
199 log_n: 9,
200 basek: 18,
201 k_ct: 27,
202 k_ksk: 27,
203 rank: 1,
204 }];
205
206 for params in params_set {
207 let id = BenchmarkId::new("KEYSWITCH_GLWE_INPLACE_FFT64", "");
208 let mut runner = runner(params);
209 group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
210 }
211
212 group.finish();
213}
214
215criterion_group!(
216 benches,
217 bench_keyswitch_glwe_fft64,
218 bench_keyswitch_glwe_inplace_fft64
219);
220criterion_main!(benches);