Add Hardware Abstraction Layer (#56)

This commit is contained in:
Jean-Philippe Bossuat
2025-08-08 19:22:42 +02:00
committed by GitHub
parent 833520b163
commit 0e0745065e
194 changed files with 17397 additions and 11955 deletions

View File

@@ -1,8 +1,15 @@
use backend::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned};
use core::{FourierGLWESecret, GGSWCiphertext, GLWECiphertext, GLWESecret, Infos};
use core::{GGSWCiphertext, GGSWCiphertextExec, GLWECiphertext, GLWESecret, GLWESecretExec, Infos};
use std::hint::black_box;
use backend::{
hal::{
api::{ModuleNew, ScalarZnxAlloc, ScratchOwnedAlloc, ScratchOwnedBorrow},
layouts::{Module, ScalarZnx, ScratchOwned},
},
implementation::cpu_spqlios::FFT64,
};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use sampling::source::Source;
use std::hint::black_box;
fn bench_external_product_glwe_fft64(c: &mut Criterion) {
let mut group = c.benchmark_group("external_product_glwe_fft64");
@@ -26,15 +33,15 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
let rank: usize = p.rank;
let digits: usize = 1;
let rows: usize = 1; //(p.k_ct_in + p.basek - 1) / p.basek;
let rows: usize = 1; //(p.k_ct_in.div_ceil(p.basek);
let sigma: f64 = 3.2;
let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, digits, rank);
let mut ct_ggsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, digits, rank);
let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank);
let pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
let pt_rgsw: ScalarZnx<Vec<u8>> = module.scalar_znx_alloc(1);
let mut scratch = ScratchOwned::new(
let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, ct_ggsw.k(), rank)
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe_in.k())
| GLWECiphertext::external_product_scratch_space(
@@ -54,7 +61,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk);
let sk_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk);
ct_ggsw.encrypt_sk(
&module,
@@ -75,8 +82,10 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
scratch.borrow(),
);
let ggsw_exec: GGSWCiphertextExec<Vec<u8>, FFT64> = GGSWCiphertextExec::from(&module, &ct_ggsw, scratch.borrow());
move || {
black_box(ct_glwe_out.external_product(&module, &ct_glwe_in, &ct_ggsw, scratch.borrow()));
black_box(ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_exec, scratch.borrow()));
}
}
@@ -118,14 +127,14 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
let rank: usize = p.rank;
let digits: usize = 1;
let rows: usize = (p.k_ct + p.basek - 1) / p.basek;
let rows: usize = p.k_ct.div_ceil(p.basek);
let sigma: f64 = 3.2;
let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, digits, rank);
let mut ct_ggsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, digits, rank);
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_glwe, rank);
let pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
let pt_rgsw: ScalarZnx<Vec<u8>> = module.scalar_znx_alloc(1);
let mut scratch = ScratchOwned::new(
let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, ct_ggsw.k(), rank)
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe.k())
| GLWECiphertext::external_product_inplace_scratch_space(&module, basek, ct_glwe.k(), ct_ggsw.k(), digits, rank),
@@ -137,7 +146,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk);
let sk_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk);
ct_ggsw.encrypt_sk(
&module,
@@ -158,9 +167,11 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
scratch.borrow(),
);
let ggsw_exec: GGSWCiphertextExec<Vec<u8>, FFT64> = GGSWCiphertextExec::from(&module, &ct_ggsw, scratch.borrow());
move || {
let scratch_borrow = scratch.borrow();
black_box(ct_glwe.external_product_inplace(&module, &ct_ggsw, scratch_borrow));
black_box(ct_glwe.external_product_inplace(&module, &ggsw_exec, scratch_borrow));
}
}

View File

@@ -1,8 +1,18 @@
use backend::{FFT64, Module, ScratchOwned};
use core::{FourierGLWESecret, GLWEAutomorphismKey, GLWECiphertext, GLWESecret, GLWESwitchingKey, Infos};
use core::{
AutomorphismKey, AutomorphismKeyExec, GLWECiphertext, GLWESecret, GLWESecretExec, GLWESwitchingKey, GLWESwitchingKeyExec,
Infos,
};
use std::{hint::black_box, time::Duration};
use backend::{
hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
layouts::{Module, ScratchOwned},
},
implementation::cpu_spqlios::FFT64,
};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use sampling::source::Source;
use std::{hint::black_box, time::Duration};
fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
let mut group = c.benchmark_group("keyswitch_glwe_fft64");
@@ -29,15 +39,14 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
let rank_out: usize = p.rank_out;
let digits: usize = p.digits;
let rows: usize = (p.k_ct_in + (p.basek * digits) - 1) / (p.basek * digits);
let rows: usize = p.k_ct_in.div_ceil(p.basek * digits);
let sigma: f64 = 3.2;
let mut ksk: GLWEAutomorphismKey<Vec<u8>, FFT64> =
GLWEAutomorphismKey::alloc(&module, basek, k_grlwe, rows, digits, rank_out);
let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&module, basek, k_grlwe, rows, digits, rank_out);
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_rlwe_in, rank_in);
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_rlwe_out, rank_out);
let mut scratch = ScratchOwned::new(
let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank_in, rank_out)
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
| GLWECiphertext::keyswitch_scratch_space(
@@ -58,7 +67,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_in);
let sk_in_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk_in);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
@@ -82,8 +91,10 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
scratch.borrow(),
);
let ksk_exec: AutomorphismKeyExec<Vec<u8>, _> = AutomorphismKeyExec::from(&module, &ksk, scratch.borrow());
move || {
black_box(ct_out.automorphism(&module, &ct_in, &ksk, scratch.borrow()));
black_box(ct_out.automorphism(&module, &ct_in, &ksk_exec, scratch.borrow()));
}
}
@@ -132,13 +143,13 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
let rank: usize = p.rank;
let digits: usize = 1;
let rows: usize = (p.k_ct + p.basek - 1) / p.basek;
let rows: usize = p.k_ct.div_ceil(p.basek);
let sigma: f64 = 3.2;
let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::alloc(&module, basek, k_ksk, rows, digits, rank, rank);
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&module, basek, k_ksk, rows, digits, rank, rank);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
let mut scratch = ScratchOwned::new(
let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank, rank)
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct.k(), ksk.k(), digits, rank),
@@ -150,16 +161,15 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_in);
let sk_in_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk_in);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_out);
ksk.encrypt_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -175,8 +185,10 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
scratch.borrow(),
);
let ksk_exec: GLWESwitchingKeyExec<Vec<u8>, FFT64> = GLWESwitchingKeyExec::from(&module, &ksk, scratch.borrow());
move || {
black_box(ct.keyswitch_inplace(&module, &ksk, scratch.borrow()));
black_box(ct.keyswitch_inplace(&module, &ksk_exec, scratch.borrow()));
}
}