mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
Updated arguments to get scratch space size for ops
This commit is contained in:
@@ -33,19 +33,20 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
let rows: usize = (p.k_ct_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 mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
||||||
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
|
let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
|
||||||
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, 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.new_scalar_znx(1);
|
||||||
|
|
||||||
let mut scratch = ScratchOwned::new(
|
let mut scratch = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, ct_ggsw.k(), rank)
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe_in.k())
|
||||||
| GLWECiphertext::external_product_scratch_space(
|
| GLWECiphertext::external_product_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_rlwe_out.size(),
|
basek,
|
||||||
ct_rlwe_in.size(),
|
ct_glwe_out.k(),
|
||||||
ct_rgsw.size(),
|
ct_glwe_in.k(),
|
||||||
|
ct_ggsw.k(),
|
||||||
rank,
|
rank,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -59,7 +60,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
ct_rgsw.encrypt_sk(
|
ct_ggsw.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_rgsw,
|
&pt_rgsw,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
@@ -69,7 +70,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe_in.encrypt_zero_sk(
|
ct_glwe_in.encrypt_zero_sk(
|
||||||
&module,
|
&module,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
@@ -79,10 +80,10 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
move || {
|
move || {
|
||||||
ct_rlwe_out.external_product(
|
ct_glwe_out.external_product(
|
||||||
black_box(&module),
|
black_box(&module),
|
||||||
black_box(&ct_rlwe_in),
|
black_box(&ct_glwe_in),
|
||||||
black_box(&ct_rgsw),
|
black_box(&ct_ggsw),
|
||||||
black_box(scratch.borrow()),
|
black_box(scratch.borrow()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -128,14 +129,14 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
let rows: usize = (p.k_ct + 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 mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
||||||
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_glwe, 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.new_scalar_znx(1);
|
||||||
|
|
||||||
let mut scratch = ScratchOwned::new(
|
let mut scratch = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, ct_ggsw.k(), rank)
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe.k())
|
||||||
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size(), rank),
|
| GLWECiphertext::external_product_inplace_scratch_space(&module, basek, ct_glwe.k(), ct_ggsw.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut source_xs = Source::new([0u8; 32]);
|
let mut source_xs = Source::new([0u8; 32]);
|
||||||
@@ -147,7 +148,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
ct_rgsw.encrypt_sk(
|
ct_ggsw.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_rgsw,
|
&pt_rgsw,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
@@ -157,7 +158,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe.encrypt_zero_sk(
|
ct_glwe.encrypt_zero_sk(
|
||||||
&module,
|
&module,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
&mut source_xa,
|
&mut source_xa,
|
||||||
@@ -169,9 +170,9 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
move || {
|
move || {
|
||||||
let scratch_borrow = scratch.borrow();
|
let scratch_borrow = scratch.borrow();
|
||||||
(0..687).for_each(|_| {
|
(0..687).for_each(|_| {
|
||||||
ct_rlwe.external_product_inplace(
|
ct_glwe.external_product_inplace(
|
||||||
black_box(&module),
|
black_box(&module),
|
||||||
black_box(&ct_rgsw),
|
black_box(&ct_ggsw),
|
||||||
black_box(scratch_borrow),
|
black_box(scratch_borrow),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -40,13 +40,14 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
|
|||||||
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_rlwe_out, rank_out);
|
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::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ksk.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank_out)
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
|
||||||
| GLWECiphertext::keyswitch_scratch_space(
|
| GLWECiphertext::keyswitch_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_out.size(),
|
basek,
|
||||||
ct_in.size(),
|
ct_out.k(),
|
||||||
ksk.size(),
|
ct_in.k(),
|
||||||
|
ksk.k(),
|
||||||
rank_in,
|
rank_in,
|
||||||
rank_out,
|
rank_out,
|
||||||
),
|
),
|
||||||
@@ -140,9 +141,9 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
|
|||||||
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
||||||
|
|
||||||
let mut scratch = ScratchOwned::new(
|
let mut scratch = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ksk.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank)
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct.size(), ksk.size(), rank),
|
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct.k(), rank, ksk.k()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use backend::{
|
use backend::{
|
||||||
Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, ScalarZnxDftAlloc, ScalarZnxDftOps, ScalarZnxOps, Scratch, VecZnx,
|
Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, ScalarZnxDftAlloc, ScalarZnxDftOps, ScalarZnxOps, Scratch, VecZnx,
|
||||||
VecZnxBigAlloc, VecZnxDftAlloc, VecZnxDftOps, VecZnxOps, ZnxZero,
|
VecZnxDftOps, VecZnxOps, ZnxZero,
|
||||||
};
|
};
|
||||||
use sampling::source::Source;
|
use sampling::source::Source;
|
||||||
|
|
||||||
@@ -86,63 +86,79 @@ impl<C: AsMut<[u8]> + AsRef<[u8]>> SetRow<FFT64> for AutomorphismKey<C, FFT64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AutomorphismKey<Vec<u8>, FFT64> {
|
impl AutomorphismKey<Vec<u8>, FFT64> {
|
||||||
pub fn generate_from_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
|
pub fn generate_from_sk_scratch_space(module: &Module<FFT64>, basek: usize, k: usize, rank: usize) -> usize {
|
||||||
GGLWECiphertext::generate_from_sk_scratch_space(module, rank, size) + module.bytes_of_scalar_znx_dft(rank)
|
GGLWECiphertext::generate_from_sk_scratch_space(module, basek, k, rank) + module.bytes_of_scalar_znx_dft(rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_from_pk_scratch_space(module: &Module<FFT64>, rank: usize, pk_size: usize) -> usize {
|
pub fn generate_from_pk_scratch_space(module: &Module<FFT64>, _basek: usize, _k: usize, _rank: usize) -> usize {
|
||||||
GGLWECiphertext::generate_from_pk_scratch_space(module, rank, pk_size)
|
GGLWECiphertext::generate_from_pk_scratch_space(module, _basek, _k, _rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_scratch_space(
|
pub fn keyswitch_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ksk_size: usize,
|
in_k: usize,
|
||||||
|
ksk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GLWESwitchingKey::keyswitch_scratch_space(module, out_size, rank, in_size, rank, ksk_size)
|
GLWESwitchingKey::keyswitch_scratch_space(module, basek, out_k, rank, in_k, rank, ksk_k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, out_size: usize, out_rank: usize, ksk_size: usize) -> usize {
|
pub fn keyswitch_inplace_scratch_space(
|
||||||
GLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_size, out_rank, ksk_size)
|
module: &Module<FFT64>,
|
||||||
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
|
out_rank: usize,
|
||||||
|
ksk_k: usize,
|
||||||
|
) -> usize {
|
||||||
|
GLWESwitchingKey::keyswitch_inplace_scratch_space(module, basek, out_k, out_rank, ksk_k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn automorphism_scratch_space(
|
pub fn automorphism_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ksk_size: usize,
|
in_k: usize,
|
||||||
|
atk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let tmp_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, in_size);
|
let tmp_dft: usize = GLWECiphertextFourier::bytes_of(module, basek, in_k, rank);
|
||||||
let tmp_idft: usize = module.bytes_of_vec_znx_big(rank + 1, out_size);
|
let tmp_idft: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, rank);
|
||||||
let idft: usize = module.vec_znx_idft_tmp_bytes();
|
let idft: usize = module.vec_znx_idft_tmp_bytes();
|
||||||
let keyswitch: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, out_size, rank, ksk_size);
|
let keyswitch: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, basek, out_k, rank, atk_k);
|
||||||
tmp_dft + tmp_idft + idft + keyswitch
|
tmp_dft + tmp_idft + idft + keyswitch
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn automorphism_inplace_scratch_space(module: &Module<FFT64>, out_size: usize, ksk_size: usize, rank: usize) -> usize {
|
pub fn automorphism_inplace_scratch_space(
|
||||||
AutomorphismKey::automorphism_scratch_space(module, out_size, out_size, ksk_size, rank)
|
module: &Module<FFT64>,
|
||||||
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
|
ksk_k: usize,
|
||||||
|
rank: usize,
|
||||||
|
) -> usize {
|
||||||
|
AutomorphismKey::automorphism_scratch_space(module, basek, out_k, out_k, ksk_k, rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_scratch_space(
|
pub fn external_product_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ggsw_size: usize,
|
in_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GLWESwitchingKey::external_product_scratch_space(module, out_size, in_size, ggsw_size, rank)
|
GLWESwitchingKey::external_product_scratch_space(module, basek, out_k, in_k, ggsw_k, rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_inplace_scratch_space(
|
pub fn external_product_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
ggsw_size: usize,
|
out_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GLWESwitchingKey::external_product_inplace_scratch_space(module, out_size, ggsw_size, rank)
|
GLWESwitchingKey::external_product_inplace_scratch_space(module, basek, out_k, ggsw_k, rank)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,13 +180,14 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> AutomorphismKey<DataSelf, FFT64> {
|
|||||||
assert_eq!(self.rank_out(), self.rank_in());
|
assert_eq!(self.rank_out(), self.rank_in());
|
||||||
assert_eq!(sk.rank(), self.rank());
|
assert_eq!(sk.rank(), self.rank());
|
||||||
assert!(
|
assert!(
|
||||||
scratch.available() >= AutomorphismKey::generate_from_sk_scratch_space(module, self.rank(), self.size()),
|
scratch.available()
|
||||||
|
>= AutomorphismKey::generate_from_sk_scratch_space(module, self.basek(), self.k(), self.rank()),
|
||||||
"scratch.available(): {} < AutomorphismKey::generate_from_sk_scratch_space(module, self.rank()={}, \
|
"scratch.available(): {} < AutomorphismKey::generate_from_sk_scratch_space(module, self.rank()={}, \
|
||||||
self.size()={}): {}",
|
self.size()={}): {}",
|
||||||
scratch.available(),
|
scratch.available(),
|
||||||
self.rank(),
|
self.rank(),
|
||||||
self.size(),
|
self.size(),
|
||||||
AutomorphismKey::generate_from_sk_scratch_space(module, self.rank(), self.size())
|
AutomorphismKey::generate_from_sk_scratch_space(module, self.basek(), self.k(), self.rank())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,14 +57,15 @@ impl<T, B: Backend> GGLWECiphertext<T, B> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GGLWECiphertext<Vec<u8>, FFT64> {
|
impl GGLWECiphertext<Vec<u8>, FFT64> {
|
||||||
pub fn generate_from_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
|
pub fn generate_from_sk_scratch_space(module: &Module<FFT64>, basek: usize, k: usize, rank: usize) -> usize {
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(module, size)
|
let size = derive_size(basek, k);
|
||||||
|
GLWECiphertext::encrypt_sk_scratch_space(module, basek, k)
|
||||||
+ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_from_pk_scratch_space(_module: &Module<FFT64>, _rank: usize, _pk_size: usize) -> usize {
|
pub fn generate_from_pk_scratch_space(_module: &Module<FFT64>, _basek: usize, _k: usize, _rank: usize) -> usize {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,13 +89,14 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGLWECiphertext<DataSelf, FFT64> {
|
|||||||
assert_eq!(sk_dft.n(), module.n());
|
assert_eq!(sk_dft.n(), module.n());
|
||||||
assert_eq!(pt.n(), module.n());
|
assert_eq!(pt.n(), module.n());
|
||||||
assert!(
|
assert!(
|
||||||
scratch.available() >= GGLWECiphertext::generate_from_sk_scratch_space(module, self.rank(), self.size()),
|
scratch.available()
|
||||||
|
>= GGLWECiphertext::generate_from_sk_scratch_space(module, self.basek(), self.k(), self.rank()),
|
||||||
"scratch.available: {} < GGLWECiphertext::generate_from_sk_scratch_space(module, self.rank()={}, \
|
"scratch.available: {} < GGLWECiphertext::generate_from_sk_scratch_space(module, self.rank()={}, \
|
||||||
self.size()={}): {}",
|
self.size()={}): {}",
|
||||||
scratch.available(),
|
scratch.available(),
|
||||||
self.rank(),
|
self.rank(),
|
||||||
self.size(),
|
self.size(),
|
||||||
GGLWECiphertext::generate_from_sk_scratch_space(module, self.rank(), self.size())
|
GGLWECiphertext::generate_from_sk_scratch_space(module, self.basek(), self.k(), self.rank())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,8 +60,9 @@ impl<T, B: Backend> GGSWCiphertext<T, B> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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>, basek: usize, k: usize, rank: usize) -> usize {
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(module, size)
|
let size = derive_size(basek, k);
|
||||||
|
GLWECiphertext::encrypt_sk_scratch_space(module, basek, k)
|
||||||
+ 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)
|
||||||
@@ -69,112 +70,116 @@ impl GGSWCiphertext<Vec<u8>, FFT64> {
|
|||||||
|
|
||||||
pub(crate) fn expand_row_scratch_space(
|
pub(crate) fn expand_row_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
self_size: usize,
|
basek: usize,
|
||||||
tensor_key_size: usize,
|
self_k: usize,
|
||||||
|
tsk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let tmp_dft_i: usize = module.bytes_of_vec_znx_dft(rank + 1, tensor_key_size);
|
let tsk_size: usize = derive_size(basek, tsk_k);
|
||||||
|
let self_size: usize = derive_size(basek, self_k);
|
||||||
|
let tmp_dft_i: usize = module.bytes_of_vec_znx_dft(rank + 1, tsk_size);
|
||||||
let tmp_dft_col_data: usize = module.bytes_of_vec_znx_dft(1, self_size);
|
let tmp_dft_col_data: usize = module.bytes_of_vec_znx_dft(1, self_size);
|
||||||
let vmp: usize =
|
let vmp: usize = tmp_dft_col_data + module.vmp_apply_tmp_bytes(self_size, self_size, self_size, rank, rank, tsk_size);
|
||||||
tmp_dft_col_data + module.vmp_apply_tmp_bytes(self_size, self_size, self_size, rank, rank, tensor_key_size);
|
let tmp_idft: usize = module.bytes_of_vec_znx_big(1, tsk_size);
|
||||||
let tmp_idft: usize = module.bytes_of_vec_znx_big(1, tensor_key_size);
|
|
||||||
let norm: usize = module.vec_znx_big_normalize_tmp_bytes();
|
let norm: usize = module.vec_znx_big_normalize_tmp_bytes();
|
||||||
tmp_dft_i + ((tmp_dft_col_data + vmp) | (tmp_idft + norm))
|
tmp_dft_i + ((tmp_dft_col_data + vmp) | (tmp_idft + norm))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn keyswitch_internal_col0_scratch_space(
|
pub(crate) fn keyswitch_internal_col0_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ksk_size: usize,
|
in_k: usize,
|
||||||
|
ksk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GLWECiphertext::keyswitch_from_fourier_scratch_space(module, out_size, rank, in_size, rank, ksk_size)
|
GLWECiphertext::keyswitch_from_fourier_scratch_space(module, basek, out_k, rank, in_k, rank, ksk_k)
|
||||||
+ module.bytes_of_vec_znx_dft(rank + 1, in_size)
|
+ module.bytes_of_vec_znx_dft(rank + 1, derive_size(basek, in_k))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_scratch_space(
|
pub fn keyswitch_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ksk_size: usize,
|
in_k: usize,
|
||||||
tensor_key_size: usize,
|
ksk_k: usize,
|
||||||
|
tsk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
|
let out_size: usize = derive_size(basek, out_k);
|
||||||
|
|
||||||
let res_znx: usize = module.bytes_of_vec_znx(rank + 1, out_size);
|
let res_znx: usize = module.bytes_of_vec_znx(rank + 1, out_size);
|
||||||
let ci_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
let ci_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
||||||
let ks: usize = GGSWCiphertext::keyswitch_internal_col0_scratch_space(module, out_size, in_size, ksk_size, rank);
|
let ks: usize = GGSWCiphertext::keyswitch_internal_col0_scratch_space(module, basek, out_k, in_k, ksk_k, rank);
|
||||||
let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, out_size, tensor_key_size, rank);
|
let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, basek, out_k, tsk_k, rank);
|
||||||
let res_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
let res_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
||||||
res_znx + ci_dft + (ks | expand_rows | res_dft)
|
res_znx + ci_dft + (ks | expand_rows | res_dft)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_inplace_scratch_space(
|
pub fn keyswitch_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
ksk_size: usize,
|
out_k: usize,
|
||||||
tensor_key_size: usize,
|
ksk_k: usize,
|
||||||
|
tsk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GGSWCiphertext::keyswitch_scratch_space(module, out_size, out_size, ksk_size, tensor_key_size, rank)
|
GGSWCiphertext::keyswitch_scratch_space(module, basek, out_k, out_k, ksk_k, tsk_k, rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn automorphism_scratch_space(
|
pub fn automorphism_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
auto_key_size: usize,
|
in_k: usize,
|
||||||
tensor_key_size: usize,
|
atk_k: usize,
|
||||||
|
tsk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let cols: usize = rank + 1;
|
let cols: usize = rank + 1;
|
||||||
|
let out_size: usize = derive_size(basek, out_k);
|
||||||
let res: usize = module.bytes_of_vec_znx(cols, out_size);
|
let res: usize = module.bytes_of_vec_znx(cols, out_size);
|
||||||
let res_dft: usize = module.bytes_of_vec_znx_dft(cols, out_size);
|
let res_dft: usize = module.bytes_of_vec_znx_dft(cols, out_size);
|
||||||
let ci_dft: usize = module.bytes_of_vec_znx_dft(cols, out_size);
|
let ci_dft: usize = module.bytes_of_vec_znx_dft(cols, out_size);
|
||||||
let ks_internal: usize =
|
let ks_internal: usize = GGSWCiphertext::keyswitch_internal_col0_scratch_space(module, basek, out_k, in_k, atk_k, rank);
|
||||||
GGSWCiphertext::keyswitch_internal_col0_scratch_space(module, out_size, in_size, auto_key_size, rank);
|
let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, basek, out_k, tsk_k, rank);
|
||||||
let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, out_size, tensor_key_size, rank);
|
|
||||||
res + ci_dft + (ks_internal | expand | res_dft)
|
res + ci_dft + (ks_internal | expand | res_dft)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn automorphism_inplace_scratch_space(
|
pub fn automorphism_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
auto_key_size: usize,
|
out_k: usize,
|
||||||
tensor_key_size: usize,
|
atk_k: usize,
|
||||||
|
tsk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GGSWCiphertext::automorphism_scratch_space(
|
GGSWCiphertext::automorphism_scratch_space(module, basek, out_k, out_k, atk_k, tsk_k, rank)
|
||||||
module,
|
|
||||||
out_size,
|
|
||||||
out_size,
|
|
||||||
auto_key_size,
|
|
||||||
tensor_key_size,
|
|
||||||
rank,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_scratch_space(
|
pub fn external_product_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ggsw_size: usize,
|
in_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let tmp_in: usize = module.bytes_of_vec_znx_dft(rank + 1, in_size);
|
let tmp_in: usize = GLWECiphertextFourier::bytes_of(module, basek, in_k, rank);
|
||||||
let tmp_out: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
let tmp_out: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, rank);
|
||||||
let ggsw: usize = GLWECiphertextFourier::external_product_scratch_space(module, out_size, in_size, ggsw_size, rank);
|
let ggsw: usize = GLWECiphertextFourier::external_product_scratch_space(module, basek, out_k, in_k, ggsw_k, rank);
|
||||||
tmp_in + tmp_out + ggsw
|
tmp_in + tmp_out + ggsw
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_inplace_scratch_space(
|
pub fn external_product_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
ggsw_size: usize,
|
out_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let tmp: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
let tmp: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, rank);
|
||||||
let ggsw: usize = GLWECiphertextFourier::external_product_inplace_scratch_space(module, out_size, ggsw_size, rank);
|
let ggsw: usize = GLWECiphertextFourier::external_product_inplace_scratch_space(module, basek, out_k, ggsw_k, rank);
|
||||||
tmp + ggsw
|
tmp + ggsw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,7 +253,9 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
|
|||||||
{
|
{
|
||||||
let cols: usize = self.rank() + 1;
|
let cols: usize = self.rank() + 1;
|
||||||
|
|
||||||
assert!(scratch.available() >= GGSWCiphertext::expand_row_scratch_space(module, self.size(), tsk.size(), tsk.rank()));
|
assert!(
|
||||||
|
scratch.available() >= GGSWCiphertext::expand_row_scratch_space(module, self.basek(), self.k(), tsk.k(), self.rank())
|
||||||
|
);
|
||||||
|
|
||||||
// Example for rank 3:
|
// Example for rank 3:
|
||||||
//
|
//
|
||||||
@@ -414,10 +421,11 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
|
|||||||
scratch.available()
|
scratch.available()
|
||||||
>= GGSWCiphertext::automorphism_scratch_space(
|
>= GGSWCiphertext::automorphism_scratch_space(
|
||||||
module,
|
module,
|
||||||
self.size(),
|
self.basek(),
|
||||||
lhs.size(),
|
self.k(),
|
||||||
auto_key.size(),
|
lhs.k(),
|
||||||
tensor_key.size(),
|
auto_key.k(),
|
||||||
|
tensor_key.k(),
|
||||||
self.rank()
|
self.rank()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -570,9 +578,10 @@ impl<DataSelf: AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
|
|||||||
scratch.available()
|
scratch.available()
|
||||||
>= GGSWCiphertext::keyswitch_internal_col0_scratch_space(
|
>= GGSWCiphertext::keyswitch_internal_col0_scratch_space(
|
||||||
module,
|
module,
|
||||||
res.size(),
|
self.basek(),
|
||||||
self.size(),
|
res.k(),
|
||||||
ksk.size(),
|
self.k(),
|
||||||
|
ksk.k(),
|
||||||
ksk.rank()
|
ksk.rank()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -76,106 +76,117 @@ impl<C: AsRef<[u8]>> GLWECiphertext<C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GLWECiphertext<Vec<u8>> {
|
impl GLWECiphertext<Vec<u8>> {
|
||||||
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
|
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, basek: usize, k: usize) -> usize {
|
||||||
module.vec_znx_big_normalize_tmp_bytes() + module.bytes_of_vec_znx_dft(1, ct_size) + module.bytes_of_vec_znx(1, ct_size)
|
let size: usize = derive_size(basek, k);
|
||||||
|
module.vec_znx_big_normalize_tmp_bytes() + module.bytes_of_vec_znx_dft(1, size) + module.bytes_of_vec_znx(1, size)
|
||||||
}
|
}
|
||||||
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, pk_size: usize) -> usize {
|
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, basek: usize, k: 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))
|
let size: usize = derive_size(basek, k);
|
||||||
|
((module.bytes_of_vec_znx_dft(1, size) + module.bytes_of_vec_znx_big(1, 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
|
pub fn decrypt_scratch_space(module: &Module<FFT64>, basek: usize, k: usize) -> usize {
|
||||||
(module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, ct_size))
|
let size: usize = derive_size(basek, k);
|
||||||
+ module.bytes_of_vec_znx_big(1, ct_size)
|
(module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_big(1, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_scratch_space(
|
pub fn keyswitch_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
out_rank: usize,
|
out_rank: usize,
|
||||||
in_size: usize,
|
in_k: usize,
|
||||||
in_rank: usize,
|
in_rank: usize,
|
||||||
ksk_size: usize,
|
ksk_k: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let res_dft: usize = module.bytes_of_vec_znx_dft(out_rank + 1, ksk_size);
|
let res_dft: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, out_rank);
|
||||||
|
let in_size: usize = derive_size(basek, in_k);
|
||||||
|
let out_size: usize = derive_size(basek, out_k);
|
||||||
|
let ksk_size: usize = derive_size(basek, ksk_k);
|
||||||
let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, in_rank, out_rank + 1, ksk_size)
|
let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, in_rank, out_rank + 1, ksk_size)
|
||||||
+ module.bytes_of_vec_znx_dft(in_rank, in_size);
|
+ module.bytes_of_vec_znx_dft(in_rank, in_size);
|
||||||
let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
|
let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
|
||||||
|
|
||||||
return res_dft + (vmp | normalize);
|
return res_dft + (vmp | normalize);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_from_fourier_scratch_space(
|
pub fn keyswitch_from_fourier_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
out_rank: usize,
|
out_rank: usize,
|
||||||
in_size: usize,
|
in_k: usize,
|
||||||
in_rank: usize,
|
in_rank: usize,
|
||||||
ksk_size: usize,
|
ksk_k: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let res_dft = module.bytes_of_vec_znx_dft(out_rank + 1, ksk_size);
|
Self::keyswitch_scratch_space(module, basek, out_k, out_rank, in_k, in_rank, ksk_k)
|
||||||
|
|
||||||
let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, in_rank, out_rank + 1, ksk_size)
|
|
||||||
+ module.bytes_of_vec_znx_dft(in_rank, in_size);
|
|
||||||
|
|
||||||
let norm: usize = module.vec_znx_big_normalize_tmp_bytes();
|
|
||||||
|
|
||||||
res_dft + (vmp | norm)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, out_size: usize, out_rank: usize, ksk_size: usize) -> usize {
|
pub fn keyswitch_inplace_scratch_space(
|
||||||
GLWECiphertext::keyswitch_scratch_space(module, out_size, out_rank, out_size, out_rank, ksk_size)
|
module: &Module<FFT64>,
|
||||||
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
|
out_rank: usize,
|
||||||
|
ksk_k: usize,
|
||||||
|
) -> usize {
|
||||||
|
Self::keyswitch_scratch_space(module, basek, out_k, out_rank, out_k, out_rank, ksk_k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn automorphism_scratch_space(
|
pub fn automorphism_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
out_rank: usize,
|
out_k: usize,
|
||||||
in_size: usize,
|
in_k: usize,
|
||||||
autokey_size: usize,
|
atk_k: usize,
|
||||||
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GLWECiphertext::keyswitch_scratch_space(module, out_size, out_rank, in_size, out_rank, autokey_size)
|
Self::keyswitch_scratch_space(module, basek, out_k, rank, in_k, rank, atk_k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn automorphism_inplace_scratch_space(
|
pub fn automorphism_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
out_rank: usize,
|
out_k: usize,
|
||||||
autokey_size: usize,
|
atk_k: usize,
|
||||||
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GLWECiphertext::keyswitch_scratch_space(module, out_size, out_rank, out_size, out_rank, autokey_size)
|
Self::keyswitch_scratch_space(module, basek, out_k, rank, out_k, rank, atk_k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_scratch_space(
|
pub fn external_product_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
out_rank: usize,
|
out_k: usize,
|
||||||
in_size: usize,
|
in_k: usize,
|
||||||
ggsw_size: usize,
|
ggsw_k: usize,
|
||||||
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let res_dft: usize = module.bytes_of_vec_znx_dft(out_rank + 1, ggsw_size);
|
let res_dft: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, rank);
|
||||||
let vmp: usize = module.bytes_of_vec_znx_dft(out_rank + 1, in_size)
|
let in_size: usize = derive_size(basek, in_k);
|
||||||
|
let out_size: usize = derive_size(basek, out_k);
|
||||||
|
let ggsw_size: usize = derive_size(basek, ggsw_k);
|
||||||
|
let vmp: usize = module.bytes_of_vec_znx_dft(rank + 1, in_size)
|
||||||
+ module.vmp_apply_tmp_bytes(
|
+ module.vmp_apply_tmp_bytes(
|
||||||
out_size,
|
out_size,
|
||||||
in_size,
|
in_size,
|
||||||
in_size, // rows
|
in_size, // rows
|
||||||
out_rank + 1, // cols in
|
rank + 1, // cols in
|
||||||
out_rank + 1, // cols out
|
rank + 1, // cols out
|
||||||
ggsw_size,
|
ggsw_size,
|
||||||
);
|
);
|
||||||
let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
|
let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
|
||||||
|
|
||||||
res_dft + (vmp | normalize)
|
res_dft + (vmp | normalize)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_inplace_scratch_space(
|
pub fn external_product_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
out_rank: usize,
|
out_k: usize,
|
||||||
ggsw_size: usize,
|
ggsw_k: usize,
|
||||||
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
GLWECiphertext::external_product_scratch_space(module, out_size, out_rank, out_size, ggsw_size)
|
Self::external_product_scratch_space(module, basek, out_k, out_k, ggsw_k, rank)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,11 +396,12 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
|
|||||||
scratch.available()
|
scratch.available()
|
||||||
>= GLWECiphertext::keyswitch_from_fourier_scratch_space(
|
>= GLWECiphertext::keyswitch_from_fourier_scratch_space(
|
||||||
module,
|
module,
|
||||||
self.size(),
|
self.basek(),
|
||||||
|
self.k(),
|
||||||
self.rank(),
|
self.rank(),
|
||||||
lhs.size(),
|
lhs.k(),
|
||||||
lhs.rank(),
|
lhs.rank(),
|
||||||
rhs.size(),
|
rhs.k(),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -452,11 +464,12 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
|
|||||||
scratch.available()
|
scratch.available()
|
||||||
>= GLWECiphertext::keyswitch_scratch_space(
|
>= GLWECiphertext::keyswitch_scratch_space(
|
||||||
module,
|
module,
|
||||||
self.size(),
|
self.basek(),
|
||||||
|
self.k(),
|
||||||
self.rank(),
|
self.rank(),
|
||||||
lhs.size(),
|
lhs.k(),
|
||||||
lhs.rank(),
|
lhs.rank(),
|
||||||
rhs.size(),
|
rhs.k(),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -576,10 +589,10 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
|
|||||||
assert!(col < self.rank() + 1);
|
assert!(col < self.rank() + 1);
|
||||||
}
|
}
|
||||||
assert!(
|
assert!(
|
||||||
scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self.size()),
|
scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k()),
|
||||||
"scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}",
|
"scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}",
|
||||||
scratch.available(),
|
scratch.available(),
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(module, self.size())
|
GLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,59 +53,72 @@ impl<T, B: Backend> GLWECiphertextFourier<T, B> {
|
|||||||
|
|
||||||
impl GLWECiphertextFourier<Vec<u8>, FFT64> {
|
impl GLWECiphertextFourier<Vec<u8>, FFT64> {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub(crate) fn idft_scratch_space(module: &Module<FFT64>, size: usize) -> usize {
|
pub(crate) fn idft_scratch_space(module: &Module<FFT64>, basek: usize, k: usize) -> usize {
|
||||||
module.bytes_of_vec_znx(1, size) + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_tmp_bytes())
|
module.bytes_of_vec_znx(1, derive_size(basek, k))
|
||||||
|
+ (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_tmp_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, ct_size: usize) -> usize {
|
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, basek: usize, k: usize, rank: usize) -> usize {
|
||||||
module.bytes_of_vec_znx(rank + 1, ct_size) + GLWECiphertext::encrypt_sk_scratch_space(module, ct_size)
|
module.bytes_of_vec_znx(rank + 1, derive_size(basek, k)) + GLWECiphertext::encrypt_sk_scratch_space(module, basek, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
|
pub fn decrypt_scratch_space(module: &Module<FFT64>, basek: usize, k: usize) -> usize {
|
||||||
|
let size: usize = derive_size(basek, k);
|
||||||
(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, size)
|
||||||
| (module.bytes_of_vec_znx_big(1, ct_size) + module.vec_znx_idft_tmp_bytes()))
|
| (module.bytes_of_vec_znx_big(1, size) + module.vec_znx_idft_tmp_bytes()))
|
||||||
+ module.bytes_of_vec_znx_big(1, ct_size)
|
+ module.bytes_of_vec_znx_big(1, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_scratch_space(
|
pub fn keyswitch_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
out_rank: usize,
|
out_rank: usize,
|
||||||
in_size: usize,
|
in_k: usize,
|
||||||
in_rank: usize,
|
in_rank: usize,
|
||||||
ksk_size: usize,
|
ksk_k: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
module.bytes_of_vec_znx(out_rank + 1, out_size)
|
GLWECiphertext::bytes_of(module, basek, out_k, out_rank)
|
||||||
+ GLWECiphertext::keyswitch_from_fourier_scratch_space(module, out_size, out_rank, in_size, in_rank, ksk_size)
|
+ GLWECiphertext::keyswitch_from_fourier_scratch_space(module, basek, out_k, out_rank, in_k, in_rank, ksk_k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, out_size: usize, out_rank: usize, ksk_size: usize) -> usize {
|
pub fn keyswitch_inplace_scratch_space(
|
||||||
Self::keyswitch_scratch_space(module, out_size, out_rank, out_size, out_rank, ksk_size)
|
module: &Module<FFT64>,
|
||||||
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
|
out_rank: usize,
|
||||||
|
ksk_k: usize,
|
||||||
|
) -> usize {
|
||||||
|
Self::keyswitch_scratch_space(module, basek, out_k, out_rank, out_k, out_rank, ksk_k)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_scratch_space(
|
pub fn external_product_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ggsw_size: usize,
|
in_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let res_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
let res_dft: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, rank);
|
||||||
|
let out_size: usize = derive_size(basek, out_k);
|
||||||
|
let in_size: usize = derive_size(basek, in_k);
|
||||||
|
let ggsw_size: usize = derive_size(basek, ggsw_k);
|
||||||
let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, rank + 1, rank + 1, ggsw_size);
|
let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, rank + 1, rank + 1, ggsw_size);
|
||||||
let res_small: usize = module.bytes_of_vec_znx(rank + 1, out_size);
|
let res_small: usize = GLWECiphertext::bytes_of(module, basek, out_k, rank);
|
||||||
let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
|
let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
|
||||||
|
|
||||||
res_dft + (vmp | (res_small + normalize))
|
res_dft + (vmp | (res_small + normalize))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_inplace_scratch_space(
|
pub fn external_product_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
ggsw_size: usize,
|
out_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
Self::external_product_scratch_space(module, out_size, out_size, ggsw_size, rank)
|
Self::external_product_scratch_space(module, basek, out_k, out_k, ggsw_k, rank)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::{ScratchCore, automorphism::AutomorphismKey, elem::Infos, glwe_ciphertext::GLWECiphertext, glwe_ops::GLWEOps};
|
use crate::{ScratchCore, automorphism::AutomorphismKey, elem::Infos, glwe_ciphertext::GLWECiphertext, glwe_ops::GLWEOps};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use backend::{FFT64, Module, Scratch, VecZnxAlloc};
|
use backend::{FFT64, Module, Scratch};
|
||||||
|
|
||||||
/// [StreamPacker] enables only the fly GLWE packing
|
/// [StreamPacker] enables only the fly GLWE packing
|
||||||
/// with constant memory of Log(N) ciphertexts.
|
/// with constant memory of Log(N) ciphertexts.
|
||||||
@@ -74,8 +74,8 @@ impl StreamPacker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Number of scratch space bytes required to call [Self::add].
|
/// Number of scratch space bytes required to call [Self::add].
|
||||||
pub fn scratch_space(module: &Module<FFT64>, ct_size: usize, autokey_size: usize, rank: usize) -> usize {
|
pub fn scratch_space(module: &Module<FFT64>, basek: usize, ct_k: usize, atk_k: usize, rank: usize) -> usize {
|
||||||
pack_core_scratch_space(module, ct_size, autokey_size, rank)
|
pack_core_scratch_space(module, basek, ct_k, atk_k, rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn galois_elements(module: &Module<FFT64>) -> Vec<i64> {
|
pub fn galois_elements(module: &Module<FFT64>) -> Vec<i64> {
|
||||||
@@ -142,8 +142,8 @@ impl StreamPacker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pack_core_scratch_space(module: &Module<FFT64>, ct_size: usize, autokey_size: usize, rank: usize) -> usize {
|
fn pack_core_scratch_space(module: &Module<FFT64>, basek: usize, ct_k: usize, atk_k: usize, rank: usize) -> usize {
|
||||||
combine_scratch_space(module, ct_size, autokey_size, rank)
|
combine_scratch_space(module, basek, ct_k, atk_k, rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pack_core<D: AsRef<[u8]>, DataAK: AsRef<[u8]>>(
|
fn pack_core<D: AsRef<[u8]>, DataAK: AsRef<[u8]>>(
|
||||||
@@ -203,10 +203,10 @@ fn pack_core<D: AsRef<[u8]>, DataAK: AsRef<[u8]>>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn combine_scratch_space(module: &Module<FFT64>, ct_size: usize, autokey_size: usize, rank: usize) -> usize {
|
fn combine_scratch_space(module: &Module<FFT64>, basek: usize, ct_k: usize, atk_k: usize, rank: usize) -> usize {
|
||||||
2 * module.bytes_of_vec_znx(rank + 1, ct_size)
|
GLWECiphertext::bytes_of(module, basek, ct_k, rank)
|
||||||
+ (GLWECiphertext::rsh_scratch_space(module)
|
+ (GLWECiphertext::rsh_scratch_space(module)
|
||||||
| GLWECiphertext::automorphism_scratch_space(module, ct_size, rank, ct_size, autokey_size))
|
| GLWECiphertext::automorphism_scratch_space(module, basek, ct_k, ct_k, atk_k, rank))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [combine] merges two ciphertexts together.
|
/// [combine] merges two ciphertexts together.
|
||||||
@@ -232,46 +232,52 @@ fn combine<D: AsRef<[u8]>, DataAK: AsRef<[u8]>>(
|
|||||||
gal_el = module.galois_element(1 << (i - 1))
|
gal_el = module.galois_element(1 << (i - 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) X^t(a*X^-t + b - phi(a*X^-t + b))
|
let t: i64 = 1 << (log_n - i - 1);
|
||||||
|
|
||||||
|
// Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t))
|
||||||
|
// We also use the identity: AUTO(a * X^t, g) = -X^t * AUTO(a, g)
|
||||||
|
// where t = 2^(log_n - i - 1) and g = 5^{2^(i - 1)}
|
||||||
// Different cases for wether a and/or b are zero.
|
// Different cases for wether a and/or b are zero.
|
||||||
if acc.value {
|
//
|
||||||
// Implicite RSH without modulus switch, introduces extra I(X) * Q/2 on decryption.
|
// Implicite RSH without modulus switch, introduces extra I(X) * Q/2 on decryption.
|
||||||
// Necessary so that the scaling of the plaintext remains constant.
|
// Necessary so that the scaling of the plaintext remains constant.
|
||||||
// It however is ok to do so here because coefficients are eventually
|
// It however is ok to do so here because coefficients are eventually
|
||||||
// either mapped to garbage or twice their value which vanishes I(X)
|
// either mapped to garbage or twice their value which vanishes I(X)
|
||||||
// since 2*(I(X) * Q/2) = I(X) * Q = 0 mod Q.
|
// since 2*(I(X) * Q/2) = I(X) * Q = 0 mod Q.
|
||||||
a.rsh(1, scratch);
|
if acc.value {
|
||||||
|
|
||||||
if let Some(b) = b {
|
if let Some(b) = b {
|
||||||
let (mut tmp_b, scratch_1) = scratch.tmp_glwe_ct(module, basek, k, rank);
|
let (mut tmp_b, scratch_1) = scratch.tmp_glwe_ct(module, basek, k, rank);
|
||||||
{
|
|
||||||
let (mut tmp_a, scratch_2) = scratch_1.tmp_glwe_ct(module, basek, k, rank); //TODO can we skip tmp_a by reordering X^k ?
|
|
||||||
|
|
||||||
// tmp_a = b * X^t
|
// a = a * X^-t
|
||||||
tmp_a.rotate(module, 1 << (log_n - i - 1), b);
|
a.rotate_inplace(module, -t);
|
||||||
|
|
||||||
// tmp_a >>= 1
|
// tmp_b = a * X^-t - b
|
||||||
tmp_a.rsh(1, scratch_2);
|
tmp_b.sub(module, a, b);
|
||||||
|
tmp_b.rsh(1, scratch_1);
|
||||||
|
|
||||||
// tmp_b = a - b*X^t
|
// a = a * X^-t + b
|
||||||
tmp_b.sub(module, a, &tmp_a);
|
a.add_inplace(module, b);
|
||||||
tmp_b.normalize_inplace(module, scratch_2);
|
a.rsh(1, scratch_1);
|
||||||
|
|
||||||
// a = a + b * X^t
|
tmp_b.normalize_inplace(module, scratch_1);
|
||||||
a.add_inplace(module, &tmp_a);
|
|
||||||
}
|
|
||||||
|
|
||||||
// tmp_b = phi(a - b * X^t)
|
// tmp_b = phi(a * X^-t - b)
|
||||||
if let Some(key) = auto_keys.get(&gal_el) {
|
if let Some(key) = auto_keys.get(&gal_el) {
|
||||||
tmp_b.automorphism_inplace(module, key, scratch_1);
|
tmp_b.automorphism_inplace(module, key, scratch_1);
|
||||||
} else {
|
} else {
|
||||||
panic!("auto_key[{}] not found", gal_el);
|
panic!("auto_key[{}] not found", gal_el);
|
||||||
}
|
}
|
||||||
|
|
||||||
// a = a + b*X^t + phi(a - b*X^t))
|
// a = a * X^-t + b - phi(a * X^-t - b)
|
||||||
a.add_inplace(module, &tmp_b);
|
a.sub_inplace_ab(module, &tmp_b);
|
||||||
a.normalize_inplace(module, scratch_1);
|
a.normalize_inplace(module, scratch_1);
|
||||||
|
|
||||||
|
// a = a + b * X^t - phi(a * X^-t - b) * X^t
|
||||||
|
// = a + b * X^t - phi(a * X^-t - b) * - phi(X^t)
|
||||||
|
// = a + b * X^t + phi(a - b * X^t)
|
||||||
|
a.rotate_inplace(module, t);
|
||||||
} else {
|
} else {
|
||||||
|
a.rsh(1, scratch);
|
||||||
// a = a + phi(a)
|
// a = a + phi(a)
|
||||||
if let Some(key) = auto_keys.get(&gal_el) {
|
if let Some(key) = auto_keys.get(&gal_el) {
|
||||||
a.automorphism_add_inplace(module, key, scratch);
|
a.automorphism_add_inplace(module, key, scratch);
|
||||||
|
|||||||
@@ -180,9 +180,11 @@ impl<C: AsRef<[u8]> + AsMut<[u8]>> GLWEPublicKey<C, FFT64> {
|
|||||||
// Its ok to allocate scratch space here since pk is usually generated only once.
|
// Its ok to allocate scratch space here since pk is usually generated only once.
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(GLWECiphertextFourier::encrypt_sk_scratch_space(
|
let mut scratch: ScratchOwned = ScratchOwned::new(GLWECiphertextFourier::encrypt_sk_scratch_space(
|
||||||
module,
|
module,
|
||||||
|
self.basek(),
|
||||||
|
self.k(),
|
||||||
self.rank(),
|
self.rank(),
|
||||||
self.size(),
|
|
||||||
));
|
));
|
||||||
|
|
||||||
self.data.encrypt_zero_sk(
|
self.data.encrypt_zero_sk(
|
||||||
module,
|
module,
|
||||||
sk_dft,
|
sk_dft,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use backend::{Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, Scratch, VecZnxDftAlloc, ZnxZero};
|
use backend::{Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, Scratch, ZnxZero};
|
||||||
use sampling::source::Source;
|
use sampling::source::Source;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -79,55 +79,64 @@ impl<C: AsMut<[u8]> + AsRef<[u8]>> SetRow<FFT64> for GLWESwitchingKey<C, FFT64>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GLWESwitchingKey<Vec<u8>, FFT64> {
|
impl GLWESwitchingKey<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>, basek: usize, k: usize, rank: usize) -> usize {
|
||||||
GGLWECiphertext::generate_from_sk_scratch_space(module, rank, size)
|
GGLWECiphertext::generate_from_sk_scratch_space(module, basek, k, rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, rank: usize, pk_size: usize) -> usize {
|
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, _basek: usize, _k: usize, _rank: usize) -> usize {
|
||||||
GGLWECiphertext::generate_from_pk_scratch_space(module, rank, pk_size)
|
GGLWECiphertext::generate_from_pk_scratch_space(module, _basek, _k, _rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_scratch_space(
|
pub fn keyswitch_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
out_rank: usize,
|
out_rank: usize,
|
||||||
in_size: usize,
|
in_k: usize,
|
||||||
in_rank: usize,
|
in_rank: usize,
|
||||||
ksk_size: usize,
|
ksk_k: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let tmp_in: usize = module.bytes_of_vec_znx_dft(in_rank + 1, in_size);
|
let tmp_in: usize = GLWECiphertextFourier::bytes_of(module, basek, in_k, in_rank);
|
||||||
let tmp_out: usize = module.bytes_of_vec_znx_dft(out_rank + 1, out_size);
|
let tmp_out: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, out_rank);
|
||||||
let ksk: usize = GLWECiphertextFourier::keyswitch_scratch_space(module, out_size, out_rank, in_size, in_rank, ksk_size);
|
let ksk: usize = GLWECiphertextFourier::keyswitch_scratch_space(module, basek, out_k, out_rank, in_k, in_rank, ksk_k);
|
||||||
tmp_in + tmp_out + ksk
|
tmp_in + tmp_out + ksk
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, out_size: usize, out_rank: usize, ksk_size: usize) -> usize {
|
pub fn keyswitch_inplace_scratch_space(
|
||||||
let tmp: usize = module.bytes_of_vec_znx_dft(out_rank + 1, out_size);
|
module: &Module<FFT64>,
|
||||||
let ksk: usize = GLWECiphertextFourier::keyswitch_inplace_scratch_space(module, out_size, out_rank, ksk_size);
|
basek: usize,
|
||||||
|
out_k: usize,
|
||||||
|
out_rank: usize,
|
||||||
|
ksk_k: usize,
|
||||||
|
) -> usize {
|
||||||
|
let tmp: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, out_rank);
|
||||||
|
let ksk: usize = GLWECiphertextFourier::keyswitch_inplace_scratch_space(module, basek, out_k, out_rank, ksk_k);
|
||||||
tmp + ksk
|
tmp + ksk
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_scratch_space(
|
pub fn external_product_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
ggsw_size: usize,
|
in_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let tmp_in: usize = module.bytes_of_vec_znx_dft(rank + 1, in_size);
|
let tmp_in: usize = GLWECiphertextFourier::bytes_of(module, basek, in_k, rank);
|
||||||
let tmp_out: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
let tmp_out: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, rank);
|
||||||
let ggsw: usize = GLWECiphertextFourier::external_product_scratch_space(module, out_size, in_size, ggsw_size, rank);
|
let ggsw: usize = GLWECiphertextFourier::external_product_scratch_space(module, basek, out_k, in_k, ggsw_k, rank);
|
||||||
tmp_in + tmp_out + ggsw
|
tmp_in + tmp_out + ggsw
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_product_inplace_scratch_space(
|
pub fn external_product_inplace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
ggsw_size: usize,
|
out_k: usize,
|
||||||
|
ggsw_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let tmp: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
|
let tmp: usize = GLWECiphertextFourier::bytes_of(module, basek, out_k, rank);
|
||||||
let ggsw: usize = GLWECiphertextFourier::external_product_inplace_scratch_space(module, out_size, ggsw_size, rank);
|
let ggsw: usize = GLWECiphertextFourier::external_product_inplace_scratch_space(module, basek, out_k, ggsw_k, rank);
|
||||||
tmp + ggsw
|
tmp + ggsw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ impl<T, B: Backend> TensorKey<T, B> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TensorKey<Vec<u8>, FFT64> {
|
impl TensorKey<Vec<u8>, FFT64> {
|
||||||
pub fn generate_from_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
|
pub fn generate_from_sk_scratch_space(module: &Module<FFT64>, basek: usize, k: usize, rank: usize) -> usize {
|
||||||
module.bytes_of_scalar_znx_dft(1) + GLWESwitchingKey::encrypt_sk_scratch_space(module, rank, size)
|
module.bytes_of_scalar_znx_dft(1) + GLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,15 +39,9 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
|
|||||||
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(
|
||||||
AutomorphismKey::generate_from_sk_scratch_space(&module, rank, auto_key_in.size())
|
AutomorphismKey::generate_from_sk_scratch_space(&module, basek, k_ksk, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, auto_key_out.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk)
|
||||||
| AutomorphismKey::automorphism_scratch_space(
|
| AutomorphismKey::automorphism_scratch_space(&module, basek, k_ksk, k_ksk, k_ksk, rank),
|
||||||
&module,
|
|
||||||
auto_key_out.size(),
|
|
||||||
auto_key_in.size(),
|
|
||||||
auto_key_apply.size(),
|
|
||||||
rank,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -132,7 +126,7 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
|
|||||||
|
|
||||||
fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank: usize) {
|
fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank: usize) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
let rows = (k_ksk + basek - 1) / basek;
|
let rows: usize = (k_ksk + basek - 1) / basek;
|
||||||
|
|
||||||
let mut auto_key: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_ksk, rows, rank);
|
let mut auto_key: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_ksk, rows, rank);
|
||||||
let mut auto_key_apply: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_ksk, rows, rank);
|
let mut auto_key_apply: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_ksk, rows, rank);
|
||||||
@@ -142,9 +136,9 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk
|
|||||||
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(
|
||||||
AutomorphismKey::generate_from_sk_scratch_space(&module, rank, auto_key.size())
|
AutomorphismKey::generate_from_sk_scratch_space(&module, basek, k_ksk, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, auto_key.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk)
|
||||||
| AutomorphismKey::automorphism_inplace_scratch_space(&module, auto_key.size(), auto_key_apply.size(), rank),
|
| AutomorphismKey::automorphism_inplace_scratch_space(&module, basek, k_ksk, k_ksk, rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
|
|||||||
@@ -84,8 +84,8 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in
|
|||||||
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(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ksk.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k_ksk, rank_out)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ksk.size()),
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
|
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
|
||||||
@@ -148,15 +148,16 @@ fn test_key_switch(
|
|||||||
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(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_in_s0s1 | rank_out_s0s1, ct_gglwe_s0s1.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k_ksk, rank_in_s0s1 | rank_out_s0s1)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe_s0s2.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk)
|
||||||
| GLWESwitchingKey::keyswitch_scratch_space(
|
| GLWESwitchingKey::keyswitch_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_gglwe_s0s2.size(),
|
basek,
|
||||||
|
ct_gglwe_s0s2.k(),
|
||||||
ct_gglwe_s0s2.rank(),
|
ct_gglwe_s0s2.rank(),
|
||||||
ct_gglwe_s0s1.size(),
|
ct_gglwe_s0s1.k(),
|
||||||
ct_gglwe_s0s1.rank(),
|
ct_gglwe_s0s1.rank(),
|
||||||
ct_gglwe_s1s2.size(),
|
ct_gglwe_s1s2.k(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -251,13 +252,14 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64,
|
|||||||
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(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out_s0s1, ct_gglwe_s0s1.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k_ksk, rank_out_s0s1)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe_s0s1.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk)
|
||||||
| GLWESwitchingKey::keyswitch_inplace_scratch_space(
|
| GLWESwitchingKey::keyswitch_inplace_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_gglwe_s0s1.size(),
|
basek,
|
||||||
|
ct_gglwe_s0s1.k(),
|
||||||
ct_gglwe_s0s1.rank(),
|
ct_gglwe_s0s1.rank(),
|
||||||
ct_gglwe_s1s2.size(),
|
ct_gglwe_s1s2.k(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -356,16 +358,17 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
|
|||||||
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(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ct_gglwe_in.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k, rank_out)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe_out.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| GLWESwitchingKey::external_product_scratch_space(
|
| GLWESwitchingKey::external_product_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_gglwe_out.size(),
|
basek,
|
||||||
ct_gglwe_in.size(),
|
ct_gglwe_out.k(),
|
||||||
ct_rgsw.size(),
|
ct_gglwe_in.k(),
|
||||||
|
ct_rgsw.k(),
|
||||||
rank_out,
|
rank_out,
|
||||||
)
|
)
|
||||||
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank_out, ct_rgsw.size()),
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank_out),
|
||||||
);
|
);
|
||||||
|
|
||||||
let r: usize = 1;
|
let r: usize = 1;
|
||||||
@@ -409,16 +412,17 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
|
|||||||
ct_gglwe_out.external_product(&module, &ct_gglwe_in, &ct_rgsw, scratch.borrow());
|
ct_gglwe_out.external_product(&module, &ct_gglwe_in, &ct_rgsw, scratch.borrow());
|
||||||
|
|
||||||
scratch = ScratchOwned::new(
|
scratch = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ct_gglwe_in.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k, rank_out)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe_out.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| GLWESwitchingKey::external_product_scratch_space(
|
| GLWESwitchingKey::external_product_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_gglwe_out.size(),
|
basek,
|
||||||
ct_gglwe_in.size(),
|
ct_gglwe_out.k(),
|
||||||
ct_rgsw.size(),
|
ct_gglwe_in.k(),
|
||||||
|
ct_rgsw.k(),
|
||||||
rank_out,
|
rank_out,
|
||||||
)
|
)
|
||||||
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank_out, ct_rgsw.size()),
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank_out),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k, rank_out);
|
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k, rank_out);
|
||||||
@@ -482,10 +486,10 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6
|
|||||||
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(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ct_gglwe.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k, rank_out)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| GLWESwitchingKey::external_product_inplace_scratch_space(&module, ct_gglwe.size(), ct_rgsw.size(), rank_out)
|
| GLWESwitchingKey::external_product_inplace_scratch_space(&module, basek, k, k, rank_out)
|
||||||
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank_out, ct_rgsw.size()),
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank_out),
|
||||||
);
|
);
|
||||||
|
|
||||||
let r: usize = 1;
|
let r: usize = 1;
|
||||||
|
|||||||
@@ -73,14 +73,14 @@ fn external_product_inplace() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_encrypt_sk(log_n: usize, basek: usize, k_ggsw: usize, sigma: f64, rank: usize) {
|
fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
|
|
||||||
let rows: usize = (k_ggsw + basek - 1) / basek;
|
let rows: usize = (k + basek - 1) / basek;
|
||||||
|
|
||||||
let mut ct: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
let mut ct: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k, rows, rank);
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ggsw);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ggsw);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k);
|
||||||
let mut pt_scalar: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
let mut pt_scalar: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
||||||
|
|
||||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||||
@@ -90,8 +90,8 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ggsw: usize, sigma: f64, rank:
|
|||||||
pt_scalar.fill_ternary_hw(0, module.n(), &mut source_xs);
|
pt_scalar.fill_ternary_hw(0, module.n(), &mut source_xs);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct.size()),
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -110,7 +110,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ggsw: usize, sigma: f64, rank:
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut ct_glwe_fourier: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ggsw, rank);
|
let mut ct_glwe_fourier: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k, rank);
|
||||||
let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct.size());
|
let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct.size());
|
||||||
let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct.size());
|
let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct.size());
|
||||||
|
|
||||||
@@ -132,7 +132,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ggsw: usize, sigma: f64, rank:
|
|||||||
|
|
||||||
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
|
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
|
||||||
|
|
||||||
let std_pt: f64 = pt_have.data.std(0, basek) * (k_ggsw as f64).exp2();
|
let std_pt: f64 = pt_have.data.std(0, basek) * (k as f64).exp2();
|
||||||
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
|
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
|
||||||
|
|
||||||
pt_want.data.zero();
|
pt_want.data.zero();
|
||||||
@@ -157,16 +157,17 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
|
|||||||
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(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_in.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_out.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ksk.size())
|
| GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| TensorKey::generate_from_sk_scratch_space(&module, rank, ksk.size())
|
| TensorKey::generate_from_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GGSWCiphertext::keyswitch_scratch_space(
|
| GGSWCiphertext::keyswitch_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_out.size(),
|
basek,
|
||||||
ct_in.size(),
|
ct_out.k(),
|
||||||
ksk.size(),
|
ct_in.k(),
|
||||||
tsk.size(),
|
ksk.k(),
|
||||||
|
tsk.k(),
|
||||||
rank,
|
rank,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -283,11 +284,11 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
|
|||||||
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(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ksk.size())
|
| GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| TensorKey::generate_from_sk_scratch_space(&module, rank, ksk.size())
|
| TensorKey::generate_from_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GGSWCiphertext::keyswitch_inplace_scratch_space(&module, ct.size(), ksk.size(), tsk.size(), rank),
|
| GGSWCiphertext::keyswitch_inplace_scratch_space(&module, basek, ct.k(), ksk.k(), tsk.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let var_xs: f64 = 0.5;
|
let var_xs: f64 = 0.5;
|
||||||
@@ -452,16 +453,17 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
|
|||||||
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(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_in.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_out.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| AutomorphismKey::generate_from_sk_scratch_space(&module, rank, auto_key.size())
|
| AutomorphismKey::generate_from_sk_scratch_space(&module, basek, k, rank)
|
||||||
| TensorKey::generate_from_sk_scratch_space(&module, rank, tensor_key.size())
|
| TensorKey::generate_from_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GGSWCiphertext::automorphism_scratch_space(
|
| GGSWCiphertext::automorphism_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_out.size(),
|
basek,
|
||||||
ct_in.size(),
|
ct_out.k(),
|
||||||
auto_key.size(),
|
ct_in.k(),
|
||||||
tensor_key.size(),
|
auto_key.k(),
|
||||||
|
tensor_key.k(),
|
||||||
rank,
|
rank,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -572,11 +574,11 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
|
|||||||
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(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| AutomorphismKey::generate_from_sk_scratch_space(&module, rank, auto_key.size())
|
| AutomorphismKey::generate_from_sk_scratch_space(&module, basek, k, rank)
|
||||||
| TensorKey::generate_from_sk_scratch_space(&module, rank, tensor_key.size())
|
| TensorKey::generate_from_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GGSWCiphertext::automorphism_inplace_scratch_space(&module, ct.size(), auto_key.size(), tensor_key.size(), rank),
|
| GGSWCiphertext::automorphism_inplace_scratch_space(&module, basek, ct.k(), auto_key.k(), tensor_key.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let var_xs: f64 = 0.5;
|
let var_xs: f64 = 0.5;
|
||||||
@@ -691,13 +693,14 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize,
|
|||||||
pt_ggsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k}
|
pt_ggsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k}
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWECiphertextFourier::decrypt_scratch_space(&module, ct_ggsw_lhs_out.size())
|
GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_ggsw_lhs_in.size())
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GGSWCiphertext::external_product_scratch_space(
|
| GGSWCiphertext::external_product_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_ggsw_lhs_out.size(),
|
basek,
|
||||||
ct_ggsw_lhs_in.size(),
|
ct_ggsw_lhs_out.k(),
|
||||||
ct_ggsw_rhs.size(),
|
ct_ggsw_lhs_in.k(),
|
||||||
|
ct_ggsw_rhs.k(),
|
||||||
rank,
|
rank,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -809,10 +812,10 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank
|
|||||||
pt_ggsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k}
|
pt_ggsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k}
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_ggsw_rhs.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_ggsw_lhs.size())
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k)
|
||||||
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_ggsw_lhs.size())
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, k, rank)
|
||||||
| GGSWCiphertext::external_product_inplace_scratch_space(&module, ct_ggsw_lhs.size(), ct_ggsw_rhs.size(), rank),
|
| GGSWCiphertext::external_product_inplace_scratch_space(&module, basek, ct_ggsw_lhs.k(), ct_ggsw_rhs.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
|
|||||||
@@ -43,10 +43,10 @@ fn encrypt_pk() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn keyswitch() {
|
fn keyswitch() {
|
||||||
(1..4).for_each(|rank_in| {
|
(1..4).for_each(|in_rank| {
|
||||||
(1..4).for_each(|rank_out| {
|
(1..4).for_each(|out_rank| {
|
||||||
println!("test keyswitch rank_in: {} rank_out: {}", rank_in, rank_out);
|
println!("test keyswitch in_rank: {} out_rank: {}", in_rank, out_rank);
|
||||||
test_keyswitch(12, 12, 60, 45, 60, rank_in, rank_out, 3.2);
|
test_keyswitch(12, 12, 60, 45, 60, in_rank, out_rank, 3.2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -91,10 +91,10 @@ fn automorphism() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_encrypt_sk(log_n: usize, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) {
|
fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma: f64, rank: usize) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
|
|
||||||
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k, rank);
|
||||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_pt);
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_pt);
|
||||||
|
|
||||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||||
@@ -102,7 +102,8 @@ 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, ct.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct.size()),
|
GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
|
||||||
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -151,10 +152,10 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ct: usize, k_pt: usize, sigma:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_encrypt_zero_sk(log_n: usize, basek: usize, k_ct: usize, sigma: f64, rank: usize) {
|
fn test_encrypt_zero_sk(log_n: usize, basek: usize, ct_k: usize, sigma: f64, rank: usize) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
|
|
||||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
|
|
||||||
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([1u8; 32]);
|
let mut source_xe: Source = Source::new([1u8; 32]);
|
||||||
@@ -165,11 +166,11 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, k_ct: usize, sigma: f64, ran
|
|||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
let mut ct_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ct, rank);
|
let mut ct_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, ct_k, rank);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWECiphertextFourier::decrypt_scratch_space(&module, ct_dft.size())
|
GLWECiphertextFourier::decrypt_scratch_space(&module, basek, ct_k)
|
||||||
| GLWECiphertextFourier::encrypt_sk_scratch_space(&module, rank, ct_dft.size()),
|
| GLWECiphertextFourier::encrypt_sk_scratch_space(&module, basek, ct_k, rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_dft.encrypt_zero_sk(
|
ct_dft.encrypt_zero_sk(
|
||||||
@@ -182,14 +183,14 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, k_ct: usize, sigma: f64, ran
|
|||||||
);
|
);
|
||||||
ct_dft.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
|
ct_dft.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
|
||||||
|
|
||||||
assert!((sigma - pt.data.std(0, basek) * (k_ct as f64).exp2()) <= 0.2);
|
assert!((sigma - pt.data.std(0, basek) * (ct_k as f64).exp2()) <= 0.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_encrypt_pk(log_n: usize, basek: usize, k_ct: usize, k_pk: usize, sigma: f64, rank: usize) {
|
fn test_encrypt_pk(log_n: usize, basek: usize, ct_k: usize, k_pk: usize, sigma: f64, rank: usize) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
|
|
||||||
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k, rank);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
|
|
||||||
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]);
|
||||||
@@ -205,9 +206,9 @@ fn test_encrypt_pk(log_n: usize, basek: usize, k_ct: usize, k_pk: usize, sigma:
|
|||||||
pk.generate_from_sk(&module, &sk_dft, &mut source_xa, &mut source_xe, sigma);
|
pk.generate_from_sk(&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, ct.size())
|
GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertext::encrypt_pk_scratch_space(&module, pk.size()),
|
| GLWECiphertext::encrypt_pk_scratch_space(&module, basek, pk.k()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut data_want: Vec<i64> = vec![0i64; module.n()];
|
let mut data_want: Vec<i64> = vec![0i64; module.n()];
|
||||||
@@ -216,7 +217,7 @@ fn test_encrypt_pk(log_n: usize, basek: usize, k_ct: usize, k_pk: usize, sigma:
|
|||||||
.iter_mut()
|
.iter_mut()
|
||||||
.for_each(|x| *x = source_xa.next_i64() & 0);
|
.for_each(|x| *x = source_xa.next_i64() & 0);
|
||||||
|
|
||||||
pt_want.data.encode_vec_i64(0, basek, k_ct, &data_want, 10);
|
pt_want.data.encode_vec_i64(0, basek, ct_k, &data_want, 10);
|
||||||
|
|
||||||
ct.encrypt_pk(
|
ct.encrypt_pk(
|
||||||
&module,
|
&module,
|
||||||
@@ -228,14 +229,14 @@ fn test_encrypt_pk(log_n: usize, basek: usize, k_ct: usize, k_pk: usize, sigma:
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
|
|
||||||
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
||||||
|
|
||||||
module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
|
module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
|
||||||
|
|
||||||
let noise_have: f64 = pt_want.data.std(0, basek).log2();
|
let noise_have: f64 = pt_want.data.std(0, basek).log2();
|
||||||
let noise_want: f64 = ((((rank as f64) + 1.0) * module.n() as f64 * 0.5 * sigma * sigma).sqrt()).log2() - (k_ct as f64);
|
let noise_want: f64 = ((((rank as f64) + 1.0) * module.n() as f64 * 0.5 * sigma * sigma).sqrt()).log2() - (ct_k as f64);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
(noise_have - noise_want).abs() < 0.2,
|
(noise_have - noise_want).abs() < 0.2,
|
||||||
@@ -249,20 +250,20 @@ fn test_keyswitch(
|
|||||||
log_n: usize,
|
log_n: usize,
|
||||||
basek: usize,
|
basek: usize,
|
||||||
k_keyswitch: usize,
|
k_keyswitch: usize,
|
||||||
k_ct_in: usize,
|
ct_k_in: usize,
|
||||||
k_ct_out: usize,
|
ct_k_out: usize,
|
||||||
rank_in: usize,
|
in_rank: usize,
|
||||||
rank_out: usize,
|
out_rank: usize,
|
||||||
sigma: f64,
|
sigma: f64,
|
||||||
) {
|
) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
let rows: usize = (k_ct_in + basek - 1) / basek;
|
let rows: usize = (ct_k_in + basek - 1) / basek;
|
||||||
|
|
||||||
let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::alloc(&module, basek, k_keyswitch, rows, rank_in, rank_out);
|
let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::alloc(&module, basek, k_keyswitch, rows, in_rank, out_rank);
|
||||||
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank_in);
|
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k_in, in_rank);
|
||||||
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank_out);
|
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k_out, out_rank);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_in);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k_in);
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_out);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k_out);
|
||||||
|
|
||||||
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]);
|
||||||
@@ -274,29 +275,30 @@ fn test_keyswitch(
|
|||||||
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
||||||
|
|
||||||
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, basek, ksk.k(), out_rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_out.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_out.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
|
||||||
| GLWECiphertext::keyswitch_scratch_space(
|
| GLWECiphertext::keyswitch_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_out.size(),
|
basek,
|
||||||
rank_out,
|
ct_out.k(),
|
||||||
ct_in.size(),
|
out_rank,
|
||||||
rank_in,
|
ct_in.k(),
|
||||||
ksk.size(),
|
in_rank,
|
||||||
|
ksk.k(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
|
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, in_rank);
|
||||||
sk_in.fill_ternary_prob(0.5, &mut source_xs);
|
sk_in.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
|
|
||||||
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
|
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, in_rank);
|
||||||
sk_in_dft.dft(&module, &sk_in);
|
sk_in_dft.dft(&module, &sk_in);
|
||||||
|
|
||||||
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
|
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, out_rank);
|
||||||
sk_out.fill_ternary_prob(0.5, &mut source_xs);
|
sk_out.fill_ternary_prob(0.5, &mut source_xs);
|
||||||
|
|
||||||
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
|
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, out_rank);
|
||||||
sk_out_dft.dft(&module, &sk_out);
|
sk_out_dft.dft(&module, &sk_out);
|
||||||
|
|
||||||
ksk.generate_from_sk(
|
ksk.generate_from_sk(
|
||||||
@@ -334,8 +336,8 @@ fn test_keyswitch(
|
|||||||
0f64,
|
0f64,
|
||||||
sigma * sigma,
|
sigma * sigma,
|
||||||
0f64,
|
0f64,
|
||||||
rank_in as f64,
|
in_rank as f64,
|
||||||
k_ct_in,
|
ct_k_in,
|
||||||
k_keyswitch,
|
k_keyswitch,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -347,14 +349,14 @@ fn test_keyswitch(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize, rank: usize, sigma: f64) {
|
fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize, rank: usize, sigma: f64) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
let rows: usize = (k_ct + basek - 1) / basek;
|
let rows: usize = (ct_k + basek - 1) / basek;
|
||||||
|
|
||||||
let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::alloc(&module, basek, k_ksk, rows, rank, rank);
|
let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::alloc(&module, basek, k_ksk, rows, rank, rank);
|
||||||
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k, rank);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
|
|
||||||
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]);
|
||||||
@@ -366,10 +368,10 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
|
|||||||
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
||||||
|
|
||||||
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, basek, ct_grlwe.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_glwe.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe.k())
|
||||||
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct_rlwe.size(), rank, ct_grlwe.size()),
|
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct_glwe.k(), rank, ct_grlwe.k()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -394,7 +396,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe.encrypt_sk(
|
ct_glwe.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_want,
|
&pt_want,
|
||||||
&sk0_dft,
|
&sk0_dft,
|
||||||
@@ -404,9 +406,9 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow());
|
ct_glwe.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow());
|
||||||
|
|
||||||
ct_rlwe.decrypt(&module, &mut pt_have, &sk1_dft, scratch.borrow());
|
ct_glwe.decrypt(&module, &mut pt_have, &sk1_dft, scratch.borrow());
|
||||||
|
|
||||||
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
|
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
|
||||||
|
|
||||||
@@ -420,7 +422,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
|
|||||||
sigma * sigma,
|
sigma * sigma,
|
||||||
0f64,
|
0f64,
|
||||||
rank as f64,
|
rank as f64,
|
||||||
k_ct,
|
ct_k,
|
||||||
k_ksk,
|
k_ksk,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -437,19 +439,19 @@ fn test_automorphism(
|
|||||||
basek: usize,
|
basek: usize,
|
||||||
p: i64,
|
p: i64,
|
||||||
k_autokey: usize,
|
k_autokey: usize,
|
||||||
k_ct_in: usize,
|
ct_k_in: usize,
|
||||||
k_ct_out: usize,
|
ct_k_out: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
sigma: f64,
|
sigma: f64,
|
||||||
) {
|
) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
let rows: usize = (k_ct_in + basek - 1) / basek;
|
let rows: usize = (ct_k_in + basek - 1) / basek;
|
||||||
|
|
||||||
let mut autokey: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_autokey, rows, rank);
|
let mut autokey: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_autokey, rows, rank);
|
||||||
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
|
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k_in, rank);
|
||||||
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank);
|
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k_out, rank);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_in);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k_in);
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_out);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k_out);
|
||||||
|
|
||||||
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]);
|
||||||
@@ -460,10 +462,10 @@ fn test_automorphism(
|
|||||||
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, autokey.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_out.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_out.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
|
||||||
| GLWECiphertext::automorphism_scratch_space(&module, ct_out.size(), rank, ct_in.size(), autokey.size()),
|
| GLWECiphertext::automorphism_scratch_space(&module, basek, ct_out.k(), ct_in.k(), autokey.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -511,7 +513,7 @@ fn test_automorphism(
|
|||||||
sigma * sigma,
|
sigma * sigma,
|
||||||
0f64,
|
0f64,
|
||||||
rank as f64,
|
rank as f64,
|
||||||
k_ct_in,
|
ct_k_in,
|
||||||
k_autokey,
|
k_autokey,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -523,14 +525,14 @@ fn test_automorphism(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usize, k_ct: usize, rank: usize, sigma: f64) {
|
fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usize, ct_k: usize, rank: usize, sigma: f64) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
let rows: usize = (k_ct + basek - 1) / basek;
|
let rows: usize = (ct_k + basek - 1) / basek;
|
||||||
|
|
||||||
let mut autokey: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_autokey, rows, rank);
|
let mut autokey: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_autokey, rows, rank);
|
||||||
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k, rank);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
|
|
||||||
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]);
|
||||||
@@ -542,10 +544,10 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
|
|||||||
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, autokey.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertext::automorphism_inplace_scratch_space(&module, ct.size(), rank, autokey.size()),
|
| GLWECiphertext::automorphism_inplace_scratch_space(&module, basek, ct.k(), autokey.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -590,7 +592,7 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
|
|||||||
sigma * sigma,
|
sigma * sigma,
|
||||||
0f64,
|
0f64,
|
||||||
rank as f64,
|
rank as f64,
|
||||||
k_ct,
|
ct_k,
|
||||||
k_autokey,
|
k_autokey,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -602,17 +604,17 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usize, k_ct_out: usize, rank: usize, sigma: f64) {
|
fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usize, ct_k_out: usize, rank: usize, sigma: f64) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
|
|
||||||
let rows: usize = (k_ct_in + basek - 1) / basek;
|
let rows: usize = (ct_k_in + basek - 1) / basek;
|
||||||
|
|
||||||
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
||||||
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
|
let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k_in, rank);
|
||||||
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank);
|
let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k_out, rank);
|
||||||
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_in);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k_in);
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_out);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k_out);
|
||||||
|
|
||||||
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]);
|
||||||
@@ -630,14 +632,15 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
||||||
|
|
||||||
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, basek, ct_ggsw.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_glwe_out.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe_in.k())
|
||||||
| GLWECiphertext::external_product_scratch_space(
|
| GLWECiphertext::external_product_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_rlwe_out.size(),
|
basek,
|
||||||
ct_rlwe_in.size(),
|
ct_glwe_out.k(),
|
||||||
ct_rgsw.size(),
|
ct_glwe_in.k(),
|
||||||
|
ct_ggsw.k(),
|
||||||
rank,
|
rank,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -648,7 +651,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
ct_rgsw.encrypt_sk(
|
ct_ggsw.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_rgsw,
|
&pt_rgsw,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
@@ -658,7 +661,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe_in.encrypt_sk(
|
ct_glwe_in.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_want,
|
&pt_want,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
@@ -668,9 +671,9 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe_out.external_product(&module, &ct_rlwe_in, &ct_rgsw, scratch.borrow());
|
ct_glwe_out.external_product(&module, &ct_glwe_in, &ct_ggsw, scratch.borrow());
|
||||||
|
|
||||||
ct_rlwe_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
ct_glwe_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
||||||
|
|
||||||
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
|
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
|
||||||
|
|
||||||
@@ -695,7 +698,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
var_gct_err_lhs,
|
var_gct_err_lhs,
|
||||||
var_gct_err_rhs,
|
var_gct_err_rhs,
|
||||||
rank as f64,
|
rank as f64,
|
||||||
k_ct_in,
|
ct_k_in,
|
||||||
k_ggsw,
|
k_ggsw,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -707,15 +710,15 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct: usize, rank: usize, sigma: f64) {
|
fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k: usize, rank: usize, sigma: f64) {
|
||||||
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
||||||
let rows: usize = (k_ct + basek - 1) / basek;
|
let rows: usize = (ct_k + basek - 1) / basek;
|
||||||
|
|
||||||
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
||||||
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k, rank);
|
||||||
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
|
|
||||||
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]);
|
||||||
@@ -733,10 +736,10 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
||||||
|
|
||||||
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, basek, ct_ggsw.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_glwe.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe.k())
|
||||||
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size(), rank),
|
| GLWECiphertext::external_product_inplace_scratch_space(&module, basek, ct_glwe.k(), ct_ggsw.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -745,7 +748,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
ct_rgsw.encrypt_sk(
|
ct_ggsw.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_rgsw,
|
&pt_rgsw,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
@@ -755,7 +758,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe.encrypt_sk(
|
ct_glwe.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_want,
|
&pt_want,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
@@ -765,9 +768,9 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
scratch.borrow(),
|
scratch.borrow(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ct_rlwe.external_product_inplace(&module, &ct_rgsw, scratch.borrow());
|
ct_glwe.external_product_inplace(&module, &ct_ggsw, scratch.borrow());
|
||||||
|
|
||||||
ct_rlwe.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
ct_glwe.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
||||||
|
|
||||||
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
|
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
|
||||||
|
|
||||||
@@ -792,7 +795,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
var_gct_err_lhs,
|
var_gct_err_lhs,
|
||||||
var_gct_err_rhs,
|
var_gct_err_rhs,
|
||||||
rank as f64,
|
rank as f64,
|
||||||
k_ct,
|
ct_k,
|
||||||
k_ggsw,
|
k_ggsw,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
GLWEOps,
|
||||||
elem::Infos,
|
elem::Infos,
|
||||||
ggsw_ciphertext::GGSWCiphertext,
|
ggsw_ciphertext::GGSWCiphertext,
|
||||||
glwe_ciphertext::GLWECiphertext,
|
glwe_ciphertext::GLWECiphertext,
|
||||||
@@ -79,16 +80,17 @@ fn test_keyswitch(
|
|||||||
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ksk.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, k_ksk, rank_out)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_glwe_out.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, k_ct_out)
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_glwe_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, k_ct_in)
|
||||||
| GLWECiphertextFourier::keyswitch_scratch_space(
|
| GLWECiphertextFourier::keyswitch_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
ct_glwe_out.size(),
|
basek,
|
||||||
|
ct_glwe_out.k(),
|
||||||
rank_out,
|
rank_out,
|
||||||
ct_glwe_in.size(),
|
ct_glwe_in.k(),
|
||||||
rank_in,
|
rank_in,
|
||||||
ksk.size(),
|
ksk.k(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -174,10 +176,10 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
|
|||||||
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ksk.size())
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_glwe.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_glwe.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_glwe.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe.k())
|
||||||
| GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, ct_rlwe_dft.size(), ksk.size(), rank),
|
| GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, basek, ct_rlwe_dft.k(), ksk.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -247,7 +249,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
|
|
||||||
let rows: usize = (k_ct_in + basek - 1) / basek;
|
let rows: usize = (k_ct_in + basek - 1) / basek;
|
||||||
|
|
||||||
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
|
||||||
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
|
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
|
||||||
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank);
|
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank);
|
||||||
let mut ct_in_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ct_in, rank);
|
let mut ct_in_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ct_in, rank);
|
||||||
@@ -267,15 +269,16 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
|
|
||||||
pt_want.data.at_mut(0, 0)[1] = 1;
|
pt_want.data.at_mut(0, 0)[1] = 1;
|
||||||
|
|
||||||
let k: usize = 1;
|
let k: i64 = 1;
|
||||||
|
|
||||||
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
pt_rgsw.raw_mut()[0] = 1; // X^{0}
|
||||||
|
module.vec_znx_rotate_inplace(k, &mut pt_rgsw, 0); // X^{k}
|
||||||
|
|
||||||
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, basek, ct_ggsw.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_out.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_out.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
|
||||||
| GLWECiphertextFourier::external_product_scratch_space(&module, ct_out.size(), ct_in.size(), ct_rgsw.size(), rank),
|
| GLWECiphertextFourier::external_product_scratch_space(&module, basek, ct_out.k(), ct_in.k(), ct_ggsw.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -284,7 +287,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
ct_rgsw.encrypt_sk(
|
ct_ggsw.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt_rgsw,
|
&pt_rgsw,
|
||||||
&sk_dft,
|
&sk_dft,
|
||||||
@@ -305,14 +308,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
|
|||||||
);
|
);
|
||||||
|
|
||||||
ct_in.dft(&module, &mut ct_in_dft);
|
ct_in.dft(&module, &mut ct_in_dft);
|
||||||
ct_out_dft.external_product(&module, &ct_in_dft, &ct_rgsw, scratch.borrow());
|
ct_out_dft.external_product(&module, &ct_in_dft, &ct_ggsw, scratch.borrow());
|
||||||
ct_out_dft.idft(&module, &mut ct_out, scratch.borrow());
|
ct_out_dft.idft(&module, &mut ct_out, scratch.borrow());
|
||||||
|
|
||||||
ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
||||||
|
|
||||||
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
|
pt_want.rotate_inplace(&module, k);
|
||||||
|
pt_have.sub_inplace_ab(&module, &pt_want);
|
||||||
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
|
|
||||||
|
|
||||||
let noise_have: f64 = pt_have.data.std(0, basek).log2();
|
let noise_have: f64 = pt_have.data.std(0, basek).log2();
|
||||||
|
|
||||||
@@ -367,15 +369,16 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
|
|
||||||
pt_want.data.at_mut(0, 0)[1] = 1;
|
pt_want.data.at_mut(0, 0)[1] = 1;
|
||||||
|
|
||||||
let k: usize = 1;
|
let k: i64 = 1;
|
||||||
|
|
||||||
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
pt_rgsw.raw_mut()[0] = 1; // X^{0}
|
||||||
|
module.vec_znx_rotate_inplace(k, &mut pt_rgsw, 0); // X^{k}
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_ggsw.size())
|
GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, ct_ggsw.k(), rank)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size())
|
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertextFourier::external_product_inplace_scratch_space(&module, ct.size(), ct_ggsw.size(), rank),
|
| GLWECiphertextFourier::external_product_inplace_scratch_space(&module, basek, ct.k(), ct_ggsw.k(), rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -410,9 +413,8 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
|
|||||||
|
|
||||||
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
|
||||||
|
|
||||||
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
|
pt_want.rotate_inplace(&module, k);
|
||||||
|
pt_have.sub_inplace_ab(&module, &pt_want);
|
||||||
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
|
|
||||||
|
|
||||||
let noise_have: f64 = pt_have.data.std(0, basek).log2();
|
let noise_have: f64 = pt_have.data.std(0, basek).log2();
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use backend::{Encoding, FFT64, Module, ScratchOwned, Stats};
|
use backend::{Encoding, FFT64, Module, ScratchOwned, Stats};
|
||||||
use sampling::source::Source;
|
use sampling::source::Source;
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn packing() {
|
fn packing() {
|
||||||
@@ -22,20 +21,18 @@ fn packing() {
|
|||||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||||
|
|
||||||
let basek: usize = 18;
|
let basek: usize = 18;
|
||||||
let k_ct: usize = 36;
|
let ct_k: usize = 36;
|
||||||
let k_auto_key: usize = k_ct + basek;
|
let atk_k: usize = ct_k + basek;
|
||||||
let k_pt: usize = 18;
|
let pt_k: usize = 18;
|
||||||
let rank: usize = 3;
|
let rank: usize = 3;
|
||||||
let rows: usize = (k_ct + basek - 1) / basek;
|
let rows: usize = (ct_k + basek - 1) / basek;
|
||||||
let sigma: f64 = 3.2;
|
let sigma: f64 = 3.2;
|
||||||
let ct_size: usize = rows;
|
|
||||||
let auto_key_size: usize = (k_auto_key + basek - 1) / basek;
|
|
||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
||||||
GLWECiphertext::encrypt_sk_scratch_space(&module, ct_size)
|
GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_k)
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct_size)
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_k)
|
||||||
| AutomorphismKey::generate_from_sk_scratch_space(&module, rank, auto_key_size)
|
| AutomorphismKey::generate_from_sk_scratch_space(&module, basek, atk_k, rank)
|
||||||
| StreamPacker::scratch_space(&module, ct_size, auto_key_size, rank),
|
| StreamPacker::scratch_space(&module, basek, ct_k, atk_k, rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
@@ -44,18 +41,18 @@ fn packing() {
|
|||||||
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
|
||||||
sk_dft.dft(&module, &sk);
|
sk_dft.dft(&module, &sk);
|
||||||
|
|
||||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
let mut data: Vec<i64> = vec![0i64; module.n()];
|
let mut data: Vec<i64> = vec![0i64; module.n()];
|
||||||
data.iter_mut().enumerate().for_each(|(i, x)| {
|
data.iter_mut().enumerate().for_each(|(i, x)| {
|
||||||
*x = i as i64;
|
*x = i as i64;
|
||||||
});
|
});
|
||||||
pt.data.encode_vec_i64(0, basek, k_pt, &data, 32);
|
pt.data.encode_vec_i64(0, basek, pt_k, &data, 32);
|
||||||
|
|
||||||
let gal_els: Vec<i64> = StreamPacker::galois_elements(&module);
|
let gal_els: Vec<i64> = StreamPacker::galois_elements(&module);
|
||||||
|
|
||||||
let mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>, FFT64>> = HashMap::new();
|
let mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>, FFT64>> = HashMap::new();
|
||||||
gal_els.iter().for_each(|gal_el| {
|
gal_els.iter().for_each(|gal_el| {
|
||||||
let mut key: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_auto_key, rows, rank);
|
let mut key: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, atk_k, rows, rank);
|
||||||
key.generate_from_sk(
|
key.generate_from_sk(
|
||||||
&module,
|
&module,
|
||||||
*gal_el,
|
*gal_el,
|
||||||
@@ -70,9 +67,9 @@ fn packing() {
|
|||||||
|
|
||||||
let log_batch: usize = 0;
|
let log_batch: usize = 0;
|
||||||
|
|
||||||
let mut packer: StreamPacker = StreamPacker::new(&module, log_batch, basek, k_ct, rank);
|
let mut packer: StreamPacker = StreamPacker::new(&module, log_batch, basek, ct_k, rank);
|
||||||
|
|
||||||
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
|
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, ct_k, rank);
|
||||||
|
|
||||||
ct.encrypt_sk(
|
ct.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
@@ -86,9 +83,7 @@ fn packing() {
|
|||||||
|
|
||||||
let mut res: Vec<GLWECiphertext<Vec<u8>>> = Vec::new();
|
let mut res: Vec<GLWECiphertext<Vec<u8>>> = Vec::new();
|
||||||
|
|
||||||
let start = Instant::now();
|
|
||||||
(0..module.n() >> log_batch).for_each(|i| {
|
(0..module.n() >> log_batch).for_each(|i| {
|
||||||
println!("pt {}", pt.data);
|
|
||||||
ct.encrypt_sk(
|
ct.encrypt_sk(
|
||||||
&module,
|
&module,
|
||||||
&pt,
|
&pt,
|
||||||
@@ -113,15 +108,11 @@ fn packing() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let duration = start.elapsed();
|
|
||||||
println!("Elapsed time: {} ms", duration.as_millis());
|
|
||||||
|
|
||||||
packer.flush(&module, &mut res, &auto_keys, scratch.borrow());
|
packer.flush(&module, &mut res, &auto_keys, scratch.borrow());
|
||||||
packer.reset();
|
packer.reset();
|
||||||
|
|
||||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
|
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
|
||||||
|
|
||||||
println!("{}", res.len());
|
|
||||||
|
|
||||||
res.iter().enumerate().for_each(|(i, res_i)| {
|
res.iter().enumerate().for_each(|(i, res_i)| {
|
||||||
let mut data: Vec<i64> = vec![0i64; module.n()];
|
let mut data: Vec<i64> = vec![0i64; module.n()];
|
||||||
@@ -130,12 +121,10 @@ fn packing() {
|
|||||||
*x = reverse_bits_msb(i, log_n as u32) as i64;
|
*x = reverse_bits_msb(i, log_n as u32) as i64;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
pt_want.data.encode_vec_i64(0, basek, k_pt, &data, 32);
|
pt_want.data.encode_vec_i64(0, basek, pt_k, &data, 32);
|
||||||
|
|
||||||
res_i.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
|
res_i.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
|
||||||
|
|
||||||
println!("{}", pt.data);
|
|
||||||
|
|
||||||
if i & 1 == 0 {
|
if i & 1 == 0 {
|
||||||
pt.sub_inplace_ab(&module, &pt_want);
|
pt.sub_inplace_ab(&module, &pt_want);
|
||||||
} else {
|
} else {
|
||||||
@@ -143,9 +132,9 @@ fn packing() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let noise_have = pt.data.std(0, basek).log2();
|
let noise_have = pt.data.std(0, basek).log2();
|
||||||
println!("noise_have: {}", noise_have);
|
// println!("noise_have: {}", noise_have);
|
||||||
assert!(
|
assert!(
|
||||||
noise_have < -((k_ct - basek) as f64),
|
noise_have < -((ct_k - basek) as f64),
|
||||||
"noise: {}",
|
"noise: {}",
|
||||||
noise_have
|
noise_have
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -32,8 +32,9 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
|
|||||||
|
|
||||||
let mut scratch: ScratchOwned = ScratchOwned::new(TensorKey::generate_from_sk_scratch_space(
|
let mut scratch: ScratchOwned = ScratchOwned::new(TensorKey::generate_from_sk_scratch_space(
|
||||||
&module,
|
&module,
|
||||||
|
basek,
|
||||||
|
tensor_key.k(),
|
||||||
rank,
|
rank,
|
||||||
tensor_key.size(),
|
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
|
|||||||
@@ -36,10 +36,10 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us
|
|||||||
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, ct.size())
|
GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
|
||||||
| GLWECiphertext::decrypt_scratch_space(&module, ct.size())
|
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k())
|
||||||
| AutomorphismKey::generate_from_sk_scratch_space(&module, rank, k_autokey)
|
| AutomorphismKey::generate_from_sk_scratch_space(&module, basek, k_autokey, rank)
|
||||||
| GLWECiphertext::trace_inplace_scratch_space(&module, ct.size(), k_autokey, rank),
|
| GLWECiphertext::trace_inplace_scratch_space(&module, basek, ct.k(), k_autokey, rank),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
|
||||||
|
|||||||
@@ -24,16 +24,17 @@ impl GLWECiphertext<Vec<u8>> {
|
|||||||
|
|
||||||
pub fn trace_scratch_space(
|
pub fn trace_scratch_space(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
out_size: usize,
|
basek: usize,
|
||||||
in_size: usize,
|
out_k: usize,
|
||||||
autokey_size: usize,
|
in_k: usize,
|
||||||
|
atk_k: usize,
|
||||||
rank: usize,
|
rank: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
Self::automorphism_inplace_scratch_space(module, out_size.max(in_size), rank, autokey_size)
|
Self::automorphism_inplace_scratch_space(module, basek, out_k.min(in_k), atk_k, rank)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trace_inplace_scratch_space(module: &Module<FFT64>, out_size: usize, autokey_size: usize, rank: usize) -> usize {
|
pub fn trace_inplace_scratch_space(module: &Module<FFT64>, basek: usize, out_k: usize, atk_k: usize, rank: usize) -> usize {
|
||||||
Self::automorphism_inplace_scratch_space(module, out_size, rank, autokey_size)
|
Self::automorphism_inplace_scratch_space(module, basek, out_k, atk_k, rank)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user