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