mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
fixed benchmarks
This commit is contained in:
@@ -2,12 +2,8 @@ use base2k::{FFT64, Module, ScalarZnxAlloc, ScratchOwned};
|
|||||||
use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main};
|
use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main};
|
||||||
use rlwe::{
|
use rlwe::{
|
||||||
elem::Infos,
|
elem::Infos,
|
||||||
encryption::EncryptSkScratchSpace,
|
|
||||||
external_product::{
|
|
||||||
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace,
|
|
||||||
},
|
|
||||||
ggsw_ciphertext::GGSWCiphertext,
|
ggsw_ciphertext::GGSWCiphertext,
|
||||||
glwe::GLWECiphertext,
|
glwe_ciphertext::GLWECiphertext,
|
||||||
keys::{SecretKey, SecretKeyFourier},
|
keys::{SecretKey, SecretKeyFourier},
|
||||||
};
|
};
|
||||||
use sampling::source::Source;
|
use sampling::source::Source;
|
||||||
@@ -18,36 +14,38 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
struct Params {
|
struct Params {
|
||||||
log_n: usize,
|
log_n: usize,
|
||||||
basek: usize,
|
basek: usize,
|
||||||
k_rlwe_in: usize,
|
k_ct_in: usize,
|
||||||
k_rlwe_out: usize,
|
k_ct_out: usize,
|
||||||
k_rgsw: usize,
|
k_ggsw: usize,
|
||||||
|
rank: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runner(p: Params) -> impl FnMut() {
|
fn runner(p: Params) -> impl FnMut() {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
||||||
|
|
||||||
let basek: usize = p.basek;
|
let basek: usize = p.basek;
|
||||||
let k_rlwe_in: usize = p.k_rlwe_in;
|
let k_ct_in: usize = p.k_ct_in;
|
||||||
let k_rlwe_out: usize = p.k_rlwe_out;
|
let k_ct_out: usize = p.k_ct_out;
|
||||||
let k_rgsw: usize = p.k_rgsw;
|
let k_ggsw: usize = p.k_ggsw;
|
||||||
|
let rank: usize = p.rank;
|
||||||
|
|
||||||
let rows: usize = (p.k_rlwe_in + p.basek - 1) / p.basek;
|
let rows: usize = (p.k_ct_in + p.basek - 1) / p.basek;
|
||||||
let sigma: f64 = 3.2;
|
let sigma: f64 = 3.2;
|
||||||
let bound: f64 = sigma * 6.0;
|
|
||||||
|
|
||||||
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k_rgsw, rows);
|
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k_ggsw, rows, rank);
|
||||||
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe_in);
|
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct_in, rank);
|
||||||
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe_out);
|
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct_out, rank);
|
||||||
let pt_rgsw: base2k::ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
let pt_rgsw: base2k::ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
||||||
|
|
||||||
let mut scratch = ScratchOwned::new(
|
let mut scratch = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size())
|
||||||
| GLWECiphertext::external_product_scratch_space(
|
| GLWECiphertext::external_product_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_rlwe_out.size(),
|
ct_rlwe_out.size(),
|
||||||
ct_rlwe_in.size(),
|
ct_rlwe_in.size(),
|
||||||
ct_rgsw.size(),
|
ct_rgsw.size(),
|
||||||
|
rank,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -55,9 +53,9 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
let mut source_xe = Source::new([0u8; 32]);
|
let mut source_xe = Source::new([0u8; 32]);
|
||||||
let mut source_xa = Source::new([0u8; 32]);
|
let mut source_xa = Source::new([0u8; 32]);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
||||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
ct_rgsw.encrypt_sk(
|
ct_rgsw.encrypt_sk(
|
||||||
@@ -67,7 +65,6 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -77,7 +74,6 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -94,9 +90,10 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
let params_set: Vec<Params> = vec![Params {
|
let params_set: Vec<Params> = vec![Params {
|
||||||
log_n: 10,
|
log_n: 10,
|
||||||
basek: 7,
|
basek: 7,
|
||||||
k_rlwe_in: 27,
|
k_ct_in: 27,
|
||||||
k_rlwe_out: 27,
|
k_ct_out: 27,
|
||||||
k_rgsw: 27,
|
k_ggsw: 27,
|
||||||
|
rank: 1,
|
||||||
}];
|
}];
|
||||||
|
|
||||||
for params in params_set {
|
for params in params_set {
|
||||||
@@ -114,38 +111,39 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
struct Params {
|
struct Params {
|
||||||
log_n: usize,
|
log_n: usize,
|
||||||
basek: usize,
|
basek: usize,
|
||||||
k_rlwe: usize,
|
k_ct: usize,
|
||||||
k_rgsw: usize,
|
k_ggsw: usize,
|
||||||
|
rank: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runner(p: Params) -> impl FnMut() {
|
fn runner(p: Params) -> impl FnMut() {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
||||||
|
|
||||||
let basek: usize = p.basek;
|
let basek: usize = p.basek;
|
||||||
let k_rlwe: usize = p.k_rlwe;
|
let k_glwe: usize = p.k_ct;
|
||||||
let k_rgsw: usize = p.k_rgsw;
|
let k_ggsw: usize = p.k_ggsw;
|
||||||
|
let rank: usize = p.rank;
|
||||||
|
|
||||||
let rows: usize = (p.k_rlwe + p.basek - 1) / p.basek;
|
let rows: usize = (p.k_ct + p.basek - 1) / p.basek;
|
||||||
let sigma: f64 = 3.2;
|
let sigma: f64 = 3.2;
|
||||||
let bound: f64 = sigma * 6.0;
|
|
||||||
|
|
||||||
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k_rgsw, rows);
|
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k_ggsw, rows, rank);
|
||||||
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe);
|
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_glwe, rank);
|
||||||
let pt_rgsw: base2k::ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
let pt_rgsw: base2k::ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
||||||
|
|
||||||
let mut scratch = ScratchOwned::new(
|
let mut scratch = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
||||||
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size()),
|
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut source_xs = Source::new([0u8; 32]);
|
let mut source_xs = Source::new([0u8; 32]);
|
||||||
let mut source_xe = Source::new([0u8; 32]);
|
let mut source_xe = Source::new([0u8; 32]);
|
||||||
let mut source_xa = Source::new([0u8; 32]);
|
let mut source_xa = Source::new([0u8; 32]);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
||||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
ct_rgsw.encrypt_sk(
|
ct_rgsw.encrypt_sk(
|
||||||
@@ -155,7 +153,6 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -165,13 +162,12 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
move || {
|
move || {
|
||||||
let scratch_borrow = scratch.borrow();
|
let scratch_borrow = scratch.borrow();
|
||||||
(0..1374).for_each(|i| {
|
(0..687).for_each(|_| {
|
||||||
ct_rlwe.external_product_inplace(
|
ct_rlwe.external_product_inplace(
|
||||||
black_box(&module),
|
black_box(&module),
|
||||||
black_box(&ct_rgsw),
|
black_box(&ct_rgsw),
|
||||||
@@ -182,10 +178,11 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let params_set: Vec<Params> = vec![Params {
|
let params_set: Vec<Params> = vec![Params {
|
||||||
log_n: 9,
|
log_n: 12,
|
||||||
basek: 18,
|
basek: 18,
|
||||||
k_rlwe: 27,
|
k_ct: 54,
|
||||||
k_rgsw: 27,
|
k_ggsw: 54,
|
||||||
|
rank: 1,
|
||||||
}];
|
}];
|
||||||
|
|
||||||
for params in params_set {
|
for params in params_set {
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
use base2k::{FFT64, Module, ScalarZnxAlloc, ScratchOwned};
|
use base2k::{FFT64, Module, ScratchOwned};
|
||||||
use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main};
|
use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main};
|
||||||
use rlwe::{
|
use rlwe::{
|
||||||
elem::Infos,
|
elem::Infos,
|
||||||
encryption::EncryptSkScratchSpace,
|
glwe_ciphertext::GLWECiphertext,
|
||||||
glwe::GLWECiphertext,
|
|
||||||
keys::{SecretKey, SecretKeyFourier},
|
keys::{SecretKey, SecretKeyFourier},
|
||||||
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace},
|
keyswitch_key::GLWESwitchingKey,
|
||||||
keyswitch_key::GLWEKeySwitchKey,
|
|
||||||
};
|
};
|
||||||
use sampling::source::Source;
|
use sampling::source::Source;
|
||||||
|
|
||||||
@@ -16,36 +14,40 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
|
|||||||
struct Params {
|
struct Params {
|
||||||
log_n: usize,
|
log_n: usize,
|
||||||
basek: usize,
|
basek: usize,
|
||||||
k_rlwe_in: usize,
|
k_ct_in: usize,
|
||||||
k_rlwe_out: usize,
|
k_ct_out: usize,
|
||||||
k_grlwe: usize,
|
k_ksk: usize,
|
||||||
|
rank_in: usize,
|
||||||
|
rank_out: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runner(p: Params) -> impl FnMut() {
|
fn runner(p: Params) -> impl FnMut() {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
||||||
|
|
||||||
let basek: usize = p.basek;
|
let basek: usize = p.basek;
|
||||||
let k_rlwe_in: usize = p.k_rlwe_in;
|
let k_rlwe_in: usize = p.k_ct_in;
|
||||||
let k_rlwe_out: usize = p.k_rlwe_out;
|
let k_rlwe_out: usize = p.k_ct_out;
|
||||||
let k_grlwe: usize = p.k_grlwe;
|
let k_grlwe: usize = p.k_ksk;
|
||||||
|
let rank_in: usize = p.rank_in;
|
||||||
|
let rank_out: usize = p.rank_out;
|
||||||
|
|
||||||
let rows: usize = (p.k_rlwe_in + p.basek - 1) / p.basek;
|
let rows: usize = (p.k_ct_in + p.basek - 1) / p.basek;
|
||||||
let sigma: f64 = 3.2;
|
let sigma: f64 = 3.2;
|
||||||
let bound: f64 = sigma * 6.0;
|
|
||||||
|
|
||||||
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, basek, k_grlwe, rows);
|
let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, k_grlwe, rows, rank_in, rank_out);
|
||||||
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe_in);
|
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe_in, rank_in);
|
||||||
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe_out);
|
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe_out, rank_out);
|
||||||
let pt_grlwe: base2k::ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
||||||
|
|
||||||
let mut scratch = ScratchOwned::new(
|
let mut scratch = ScratchOwned::new(
|
||||||
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ksk.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
|
||||||
| GLWECiphertext::keyswitch_scratch_space(
|
| GLWECiphertext::keyswitch_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_rlwe_out.size(),
|
ct_out.size(),
|
||||||
ct_rlwe_in.size(),
|
ct_in.size(),
|
||||||
ct_grlwe.size(),
|
ksk.size(),
|
||||||
|
rank_in,
|
||||||
|
rank_out,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -53,37 +55,40 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
|
|||||||
let mut source_xe = Source::new([0u8; 32]);
|
let mut source_xe = Source::new([0u8; 32]);
|
||||||
let mut source_xa = Source::new([0u8; 32]);
|
let mut source_xa = Source::new([0u8; 32]);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module);
|
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_in);
|
||||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
sk_in.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module);
|
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_in);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_in_dft.dft(&module, &sk_in);
|
||||||
|
|
||||||
ct_grlwe.encrypt_sk(
|
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out);
|
||||||
|
sk_out.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
|
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out);
|
||||||
|
sk_out_dft.dft(&module, &sk_out);
|
||||||
|
|
||||||
|
ksk.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_grlwe,
|
&sk_in,
|
||||||
&sk_dft,
|
&sk_out_dft,
|
||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe_in.encrypt_zero_sk(
|
ct_in.encrypt_zero_sk(
|
||||||
&module,
|
&module,
|
||||||
&sk_dft,
|
&sk_in_dft,
|
||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
move || {
|
move || {
|
||||||
ct_rlwe_out.keyswitch(
|
ct_out.keyswitch(
|
||||||
black_box(&module),
|
black_box(&module),
|
||||||
black_box(&ct_rlwe_in),
|
black_box(&ct_in),
|
||||||
black_box(&ct_grlwe),
|
black_box(&ksk),
|
||||||
black_box(scratch.borrow()),
|
black_box(scratch.borrow()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -92,9 +97,11 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
|
|||||||
let params_set: Vec<Params> = vec![Params {
|
let params_set: Vec<Params> = vec![Params {
|
||||||
log_n: 16,
|
log_n: 16,
|
||||||
basek: 50,
|
basek: 50,
|
||||||
k_rlwe_in: 1250,
|
k_ct_in: 1250,
|
||||||
k_rlwe_out: 1250,
|
k_ct_out: 1250,
|
||||||
k_grlwe: 1250 + 66,
|
k_ksk: 1250 + 66,
|
||||||
|
rank_in: 1,
|
||||||
|
rank_out: 1,
|
||||||
}];
|
}];
|
||||||
|
|
||||||
for params in params_set {
|
for params in params_set {
|
||||||
@@ -112,65 +119,68 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
struct Params {
|
struct Params {
|
||||||
log_n: usize,
|
log_n: usize,
|
||||||
basek: usize,
|
basek: usize,
|
||||||
k_rlwe: usize,
|
k_ct: usize,
|
||||||
k_grlwe: usize,
|
k_ksk: usize,
|
||||||
|
rank: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runner(p: Params) -> impl FnMut() {
|
fn runner(p: Params) -> impl FnMut() {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << p.log_n);
|
||||||
|
|
||||||
let basek: usize = p.basek;
|
let basek: usize = p.basek;
|
||||||
let k_rlwe: usize = p.k_rlwe;
|
let k_ct: usize = p.k_ct;
|
||||||
let k_grlwe: usize = p.k_grlwe;
|
let k_ksk: usize = p.k_ksk;
|
||||||
|
let rank: usize = p.rank;
|
||||||
|
|
||||||
let rows: usize = (p.k_rlwe + p.basek - 1) / p.basek;
|
let rows: usize = (p.k_ct + p.basek - 1) / p.basek;
|
||||||
let sigma: f64 = 3.2;
|
let sigma: f64 = 3.2;
|
||||||
let bound: f64 = sigma * 6.0;
|
|
||||||
|
|
||||||
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, basek, k_grlwe, rows);
|
let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank, rank);
|
||||||
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_rlwe);
|
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct, rank);
|
||||||
let pt_grlwe: base2k::ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
||||||
|
|
||||||
let mut scratch = ScratchOwned::new(
|
let mut scratch = ScratchOwned::new(
|
||||||
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ksk.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size())
|
||||||
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct_rlwe.size(), ct_grlwe.size()),
|
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct.size(), ksk.size(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module);
|
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
||||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
sk_in.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module);
|
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_in_dft.dft(&module, &sk_in);
|
||||||
|
|
||||||
ct_grlwe.encrypt_sk(
|
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
||||||
|
sk_out.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
|
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
||||||
|
sk_out_dft.dft(&module, &sk_out);
|
||||||
|
|
||||||
|
ksk.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_grlwe,
|
&sk_in,
|
||||||
&sk_dft,
|
&sk_out_dft,
|
||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe.encrypt_zero_sk(
|
ct.encrypt_zero_sk(
|
||||||
&module,
|
&module,
|
||||||
&sk_dft,
|
&sk_in_dft,
|
||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
&mut source_xe,
|
&mut source_xe,
|
||||||
sigma,
|
sigma,
|
||||||
bound,
|
|
||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
move || {
|
move || {
|
||||||
ct_rlwe.keyswitch_inplace(
|
ct.keyswitch_inplace(
|
||||||
black_box(&module),
|
black_box(&module),
|
||||||
black_box(&ct_grlwe),
|
black_box(&ksk),
|
||||||
black_box(scratch.borrow()),
|
black_box(scratch.borrow()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -179,8 +189,9 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
let params_set: Vec<Params> = vec![Params {
|
let params_set: Vec<Params> = vec![Params {
|
||||||
log_n: 9,
|
log_n: 9,
|
||||||
basek: 18,
|
basek: 18,
|
||||||
k_rlwe: 27,
|
k_ct: 27,
|
||||||
k_grlwe: 27,
|
k_ksk: 27,
|
||||||
|
rank: 1,
|
||||||
}];
|
}];
|
||||||
|
|
||||||
for params in params_set {
|
for params in params_set {
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ where
|
|||||||
|
|
||||||
impl GGLWECiphertext<Vec<u8>, FFT64> {
|
impl GGLWECiphertext<Vec<u8>, FFT64> {
|
||||||
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
|
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(module, rank, size)
|
GLWECiphertext::encrypt_sk_scratch_space(module, size)
|
||||||
+ module.bytes_of_vec_znx(rank + 1, size)
|
+ module.bytes_of_vec_znx(rank + 1, size)
|
||||||
+ module.bytes_of_vec_znx(1, size)
|
+ module.bytes_of_vec_znx(1, size)
|
||||||
+ module.bytes_of_vec_znx_dft(rank + 1, size)
|
+ module.bytes_of_vec_znx_dft(rank + 1, size)
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ where
|
|||||||
|
|
||||||
impl GGSWCiphertext<Vec<u8>, FFT64> {
|
impl GGSWCiphertext<Vec<u8>, FFT64> {
|
||||||
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
|
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(module, rank, size)
|
GLWECiphertext::encrypt_sk_scratch_space(module, size)
|
||||||
+ module.bytes_of_vec_znx(rank + 1, size)
|
+ module.bytes_of_vec_znx(rank + 1, size)
|
||||||
+ module.bytes_of_vec_znx(1, size)
|
+ module.bytes_of_vec_znx(1, size)
|
||||||
+ module.bytes_of_vec_znx_dft(rank + 1, size)
|
+ module.bytes_of_vec_znx_dft(rank + 1, size)
|
||||||
|
|||||||
@@ -97,12 +97,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GLWECiphertext<Vec<u8>> {
|
impl GLWECiphertext<Vec<u8>> {
|
||||||
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, _rank: usize, ct_size: usize) -> usize {
|
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
|
||||||
module.vec_znx_big_normalize_tmp_bytes()
|
module.vec_znx_big_normalize_tmp_bytes()
|
||||||
+ module.bytes_of_vec_znx_dft(1, ct_size)
|
+ module.bytes_of_vec_znx_dft(1, ct_size)
|
||||||
+ module.bytes_of_vec_znx_big(1, ct_size)
|
+ module.bytes_of_vec_znx_big(1, ct_size)
|
||||||
}
|
}
|
||||||
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, _rank: usize, pk_size: usize) -> usize {
|
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, pk_size: usize) -> usize {
|
||||||
((module.bytes_of_vec_znx_dft(1, pk_size) + module.bytes_of_vec_znx_big(1, pk_size)) | module.bytes_of_scalar_znx(1))
|
((module.bytes_of_vec_znx_dft(1, pk_size) + module.bytes_of_vec_znx_big(1, pk_size)) | module.bytes_of_scalar_znx(1))
|
||||||
+ module.bytes_of_scalar_znx_dft(1)
|
+ module.bytes_of_scalar_znx_dft(1)
|
||||||
+ module.vec_znx_big_normalize_tmp_bytes()
|
+ module.vec_znx_big_normalize_tmp_bytes()
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ impl GLWECiphertextFourier<Vec<u8>, FFT64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, ct_size: usize) -> usize {
|
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, ct_size: usize) -> usize {
|
||||||
module.bytes_of_vec_znx(rank + 1, ct_size) + GLWECiphertext::encrypt_sk_scratch_space(module, rank, ct_size)
|
module.bytes_of_vec_znx(rank + 1, ct_size) + GLWECiphertext::encrypt_sk_scratch_space(module, ct_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
|
pub fn decrypt_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
|
||||||
|
|||||||
@@ -69,8 +69,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ct: usize, k_pt: usize, sigma:
|
|||||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct.size())
|
GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct.size()),
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct.size()),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
||||||
@@ -173,9 +172,9 @@ fn test_encrypt_pk(log_n: usize, basek: usize, k_ct: usize, k_pk: usize, sigma:
|
|||||||
pk.generate(&module, &sk_dft, &mut source_xa, &mut source_xe, sigma);
|
pk.generate(&module, &sk_dft, &mut source_xa, &mut source_xe, sigma);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct.size())
|
GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size())
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, ct.size())
|
||||||
| GLWECiphertext::encrypt_pk_scratch_space(&module, rank, pk.size()),
|
| GLWECiphertext::encrypt_pk_scratch_space(&module, pk.size()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut data_want: Vec<i64> = vec![0i64; module.n()];
|
let mut data_want: Vec<i64> = vec![0i64; module.n()];
|
||||||
@@ -244,7 +243,7 @@ fn test_keyswitch(
|
|||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_in, ksk.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_in, ksk.size())
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_out.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, ct_out.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, rank_out, ct_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
|
||||||
| GLWECiphertext::keyswitch_scratch_space(
|
| GLWECiphertext::keyswitch_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_out.size(),
|
ct_out.size(),
|
||||||
@@ -336,7 +335,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
|
|||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
||||||
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct_rlwe.size(), ct_grlwe.size(), rank),
|
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct_rlwe.size(), ct_grlwe.size(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -438,7 +437,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size())
|
||||||
| GLWECiphertext::external_product_scratch_space(
|
| GLWECiphertext::external_product_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_rlwe_out.size(),
|
ct_rlwe_out.size(),
|
||||||
@@ -549,7 +548,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
||||||
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size(), rank),
|
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user