arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/benches/keyswitch_glwe_fft64.rs
core/benches/keyswitch_glwe_fft64.rsRaw
  1use core::{
  2    AutomorphismKey, AutomorphismKeyExec, GLWECiphertext, GLWESecret, GLWESecretExec, GLWESwitchingKey, GLWESwitchingKeyExec,
  3    Infos,
  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 basek: usize = p.basek;
 35        let k_rlwe_in: usize = p.k_ct_in;
 36        let k_rlwe_out: usize = p.k_ct_out;
 37        let k_grlwe: usize = p.k_ksk;
 38        let rank_in: usize = p.rank_in;
 39        let rank_out: usize = p.rank_out;
 40        let digits: usize = p.digits;
 41
 42        let rows: usize = p.k_ct_in.div_ceil(p.basek * digits);
 43        let sigma: f64 = 3.2;
 44
 45        let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&module, basek, k_grlwe, rows, digits, rank_out);
 46        let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_rlwe_in, rank_in);
 47        let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_rlwe_out, rank_out);
 48
 49        let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
 50            GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank_in, rank_out)
 51                | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
 52                | GLWECiphertext::keyswitch_scratch_space(
 53                    &module,
 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(&module, rank_in);
 69        sk_in.fill_ternary_prob(0.5, &mut source_xs);
 70        let sk_in_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk_in);
 71
 72        let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, 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_exec: AutomorphismKeyExec<Vec<u8>, _> = AutomorphismKeyExec::from(&module, &ksk, scratch.borrow());
 95
 96        move || {
 97            black_box(ct_out.automorphism(&module, &ct_in, &ksk_exec, scratch.borrow()));
 98        }
 99    }
100
101    let digits: usize = 1;
102    let basek: usize = 19;
103
104    let params_set: Vec<Params> = vec![Params {
105        log_n: 15,
106        basek: basek,
107        k_ct_in: 874 - digits * basek,
108        k_ct_out: 874 - digits * basek,
109        k_ksk: 874,
110        digits: digits,
111        rank_in: 1,
112        rank_out: 1,
113    }];
114
115    for params in params_set {
116        let id = BenchmarkId::new("KEYSWITCH_GLWE_FFT64", "");
117        let mut runner = runner(params);
118        group.sample_size(500);
119        group.measurement_time(Duration::from_secs(40));
120        group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
121    }
122
123    group.finish();
124}
125
126fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
127    let mut group = c.benchmark_group("keyswitch_glwe_inplace_fft64");
128
129    struct Params {
130        log_n: usize,
131        basek: usize,
132        k_ct: usize,
133        k_ksk: usize,
134        rank: usize,
135    }
136
137    fn runner(p: Params) -> impl FnMut() {
138        let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
139
140        let basek: usize = p.basek;
141        let k_ct: usize = p.k_ct;
142        let k_ksk: usize = p.k_ksk;
143        let rank: usize = p.rank;
144        let digits: usize = 1;
145
146        let rows: usize = p.k_ct.div_ceil(p.basek);
147        let sigma: f64 = 3.2;
148
149        let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&module, basek, k_ksk, rows, digits, rank, rank);
150        let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
151
152        let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
153            GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank, rank)
154                | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
155                | GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct.k(), ksk.k(), digits, rank),
156        );
157
158        let mut source_xs: Source = Source::new([0u8; 32]);
159        let mut source_xe: Source = Source::new([0u8; 32]);
160        let mut source_xa: Source = Source::new([0u8; 32]);
161
162        let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
163        sk_in.fill_ternary_prob(0.5, &mut source_xs);
164        let sk_in_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk_in);
165
166        let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
167        sk_out.fill_ternary_prob(0.5, &mut source_xs);
168
169        ksk.encrypt_sk(
170            &module,
171            &sk_in,
172            &sk_out,
173            &mut source_xa,
174            &mut source_xe,
175            sigma,
176            scratch.borrow(),
177        );
178
179        ct.encrypt_zero_sk(
180            &module,
181            &sk_in_dft,
182            &mut source_xa,
183            &mut source_xe,
184            sigma,
185            scratch.borrow(),
186        );
187
188        let ksk_exec: GLWESwitchingKeyExec<Vec<u8>, FFT64> = GLWESwitchingKeyExec::from(&module, &ksk, scratch.borrow());
189
190        move || {
191            black_box(ct.keyswitch_inplace(&module, &ksk_exec, scratch.borrow()));
192        }
193    }
194
195    let params_set: Vec<Params> = vec![Params {
196        log_n: 9,
197        basek: 18,
198        k_ct: 27,
199        k_ksk: 27,
200        rank: 1,
201    }];
202
203    for params in params_set {
204        let id = BenchmarkId::new("KEYSWITCH_GLWE_INPLACE_FFT64", "");
205        let mut runner = runner(params);
206        group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
207    }
208
209    group.finish();
210}
211
212criterion_group!(
213    benches,
214    bench_keyswitch_glwe_fft64,
215    bench_keyswitch_glwe_inplace_fft64
216);
217criterion_main!(benches);