arnaucube
arnaucube/poulpyPublic Self-hosted repository
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        let sigma: f64 = 3.2;
 43
 44        let mut ksk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_grlwe, rows, digits, rank_out);
 45        let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_rlwe_in, rank_in);
 46        let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_rlwe_out, rank_out);
 47
 48        let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
 49            GGLWESwitchingKey::encrypt_sk_scratch_space(&module, n, basek, ksk.k(), rank_in, rank_out)
 50                | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct_in.k())
 51                | GLWECiphertext::keyswitch_scratch_space(
 52                    &module,
 53                    n,
 54                    basek,
 55                    ct_out.k(),
 56                    ct_in.k(),
 57                    ksk.k(),
 58                    digits,
 59                    rank_in,
 60                    rank_out,
 61                ),
 62        );
 63
 64        let mut source_xs = Source::new([0u8; 32]);
 65        let mut source_xe = Source::new([0u8; 32]);
 66        let mut source_xa = Source::new([0u8; 32]);
 67
 68        let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in);
 69        sk_in.fill_ternary_prob(0.5, &mut source_xs);
 70        let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow());
 71
 72        let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
 73        sk_out.fill_ternary_prob(0.5, &mut source_xs);
 74
 75        ksk.encrypt_sk(
 76            &module,
 77            -1,
 78            &sk_in,
 79            &mut source_xa,
 80            &mut source_xe,
 81            sigma,
 82            scratch.borrow(),
 83        );
 84
 85        ct_in.encrypt_zero_sk(
 86            &module,
 87            &sk_in_dft,
 88            &mut source_xa,
 89            &mut source_xe,
 90            sigma,
 91            scratch.borrow(),
 92        );
 93
 94        let ksk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, _> = ksk.prepare_alloc(&module, scratch.borrow());
 95
 96        move || {
 97            ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow());
 98            black_box(());
 99        }
100    }
101
102    let digits: usize = 1;
103    let basek: usize = 19;
104
105    let params_set: Vec<Params> = vec![Params {
106        log_n: 15,
107        basek,
108        k_ct_in: 874 - digits * basek,
109        k_ct_out: 874 - digits * basek,
110        k_ksk: 874,
111        digits,
112        rank_in: 1,
113        rank_out: 1,
114    }];
115
116    for params in params_set {
117        let id = BenchmarkId::new("KEYSWITCH_GLWE_FFT64", "");
118        let mut runner = runner(params);
119        group.sample_size(500);
120        group.measurement_time(Duration::from_secs(40));
121        group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
122    }
123
124    group.finish();
125}
126
127fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
128    let mut group = c.benchmark_group("keyswitch_glwe_inplace_fft64");
129
130    struct Params {
131        log_n: usize,
132        basek: usize,
133        k_ct: usize,
134        k_ksk: usize,
135        rank: usize,
136    }
137
138    fn runner(p: Params) -> impl FnMut() {
139        let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
140
141        let n = module.n();
142        let basek: usize = p.basek;
143        let k_ct: usize = p.k_ct;
144        let k_ksk: usize = p.k_ksk;
145        let rank: usize = p.rank;
146        let digits: usize = 1;
147
148        let rows: usize = p.k_ct.div_ceil(p.basek);
149        let sigma: f64 = 3.2;
150
151        let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank, rank);
152        let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
153
154        let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
155            GGLWESwitchingKey::encrypt_sk_scratch_space(&module, n, basek, ksk.k(), rank, rank)
156                | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct.k())
157                | GLWECiphertext::keyswitch_inplace_scratch_space(&module, n, basek, ct.k(), ksk.k(), digits, rank),
158        );
159
160        let mut source_xs: Source = Source::new([0u8; 32]);
161        let mut source_xe: Source = Source::new([0u8; 32]);
162        let mut source_xa: Source = Source::new([0u8; 32]);
163
164        let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
165        sk_in.fill_ternary_prob(0.5, &mut source_xs);
166        let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow());
167
168        let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
169        sk_out.fill_ternary_prob(0.5, &mut source_xs);
170
171        ksk.encrypt_sk(
172            &module,
173            &sk_in,
174            &sk_out,
175            &mut source_xa,
176            &mut source_xe,
177            sigma,
178            scratch.borrow(),
179        );
180
181        ct.encrypt_zero_sk(
182            &module,
183            &sk_in_dft,
184            &mut source_xa,
185            &mut source_xe,
186            sigma,
187            scratch.borrow(),
188        );
189
190        let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, FFT64> = ksk.prepare_alloc(&module, scratch.borrow());
191
192        move || {
193            ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow());
194            black_box(());
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);