refactored secret key

This commit is contained in:
Jean-Philippe Bossuat
2025-05-30 15:50:01 +02:00
parent 82810a87c8
commit de4a5e9bfc
24 changed files with 358 additions and 680 deletions

View File

@@ -2,8 +2,8 @@ use crate::ffi::svp;
use crate::ffi::vec_znx_dft::vec_znx_dft_t;
use crate::znx_base::{ZnxInfos, ZnxView, ZnxViewMut};
use crate::{
Backend, FFT64, Module, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef, ScalarZnxToRef, VecZnxDft,
VecZnxDftToMut, VecZnxDftToRef,
Backend, FFT64, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef, ScalarZnxToMut,
ScalarZnxToRef, Scratch, VecZnxDft, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps,
};
pub trait ScalarZnxDftAlloc<B: Backend> {
@@ -28,6 +28,11 @@ pub trait ScalarZnxDftOps<BACKEND: Backend> {
where
R: VecZnxDftToMut<BACKEND>,
A: ScalarZnxDftToRef<BACKEND>;
fn svp_idft<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch)
where
R: ScalarZnxToMut,
A: ScalarZnxDftToRef<BACKEND>;
}
impl<B: Backend> ScalarZnxDftAlloc<B> for Module<B> {
@@ -45,6 +50,18 @@ impl<B: Backend> ScalarZnxDftAlloc<B> for Module<B> {
}
impl ScalarZnxDftOps<FFT64> for Module<FFT64> {
fn svp_idft<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch)
where
R: ScalarZnxToMut,
A: ScalarZnxDftToRef<FFT64>,
{
let res_mut: &mut ScalarZnx<&mut [u8]> = &mut res.to_mut();
let a_ref: &ScalarZnxDft<&[u8], FFT64> = &a.to_ref();
let (mut vec_znx_big, scratch1) = scratch.tmp_vec_znx_big(self, 1, 1);
self.vec_znx_idft(&mut vec_znx_big, 0, a_ref, a_col, scratch1);
self.vec_znx_copy(res_mut, res_col, &vec_znx_big.to_vec_znx_small(), 0);
}
fn svp_prepare<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
where
R: ScalarZnxDftToMut<FFT64>,

View File

@@ -1,10 +1,5 @@
use backend::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned};
use core::{
elem::Infos,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext::GLWECiphertext,
keys::{SecretKey, SecretKeyFourier},
};
use core::{GGSWCiphertext, GLWECiphertext, GLWESecret, Infos};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use sampling::source::Source;
use std::hint::black_box;
@@ -55,15 +50,13 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
let mut source_xe = Source::new([0u8; 32]);
let mut source_xa = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -72,7 +65,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
ct_glwe_in.encrypt_zero_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -143,15 +136,13 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
let mut source_xe = Source::new([0u8; 32]);
let mut source_xa = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -160,7 +151,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
ct_glwe.encrypt_zero_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,

View File

@@ -1,10 +1,5 @@
use backend::{FFT64, Module, ScratchOwned};
use core::{
elem::Infos,
glwe_ciphertext::GLWECiphertext,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
};
use core::{GLWECiphertext, GLWESecret, GLWESwitchingKey, Infos};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use sampling::source::Source;
use std::hint::black_box;
@@ -57,20 +52,16 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
let mut source_xe = Source::new([0u8; 32]);
let mut source_xa = Source::new([0u8; 32]);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -79,7 +70,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
ct_in.encrypt_zero_sk(
&module,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -150,20 +141,16 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(&&module, 0.5, &mut source_xs);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(&&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -172,7 +159,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
ct.encrypt_zero_sk(
&module,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,

View File

@@ -1,12 +1,9 @@
use backend::{
Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, ScalarZnxDftAlloc, ScalarZnxDftOps, ScalarZnxOps, Scratch, VecZnx,
VecZnxDftOps, VecZnxOps, ZnxZero,
};
use backend::{Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, ScalarZnxOps, Scratch, VecZnx, VecZnxDftOps, VecZnxOps, ZnxZero};
use sampling::source::Source;
use crate::{
GGLWECiphertext, GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWESwitchingKey, GetRow, Infos, ScratchCore,
SecretKey, SetRow,
GGLWECiphertext, GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWESecret, GLWESwitchingKey, GetRow, Infos,
ScratchCore, SetRow,
};
pub struct AutomorphismKey<Data, B: Backend> {
@@ -87,7 +84,7 @@ impl<C: AsMut<[u8]> + AsRef<[u8]>> SetRow<FFT64> for AutomorphismKey<C, FFT64> {
impl AutomorphismKey<Vec<u8>, FFT64> {
pub fn generate_from_sk_scratch_space(module: &Module<FFT64>, basek: usize, k: usize, rank: usize) -> usize {
GGLWECiphertext::generate_from_sk_scratch_space(module, basek, k, rank) + module.bytes_of_scalar_znx_dft(rank)
GGLWECiphertext::generate_from_sk_scratch_space(module, basek, k, rank) + GLWESecret::bytes_of(module, rank)
}
pub fn generate_from_pk_scratch_space(module: &Module<FFT64>, _basek: usize, _k: usize, _rank: usize) -> usize {
@@ -167,7 +164,7 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> AutomorphismKey<DataSelf, FFT64> {
&mut self,
module: &Module<FFT64>,
p: i64,
sk: &SecretKey<DataSk>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -191,31 +188,21 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> AutomorphismKey<DataSelf, FFT64> {
)
}
let (mut sk_out_dft, scratch_1) = scratch.tmp_sk_fourier(module, sk.rank());
let (mut sk_out, scratch_1) = scratch.tmp_sk(module, sk.rank());
(0..self.rank()).for_each(|i| {
module.scalar_znx_automorphism(
module.galois_element_inv(p),
&mut sk_out.data,
i,
&sk.data,
i,
);
});
{
(0..self.rank()).for_each(|i| {
let (mut sk_inv_auto, _) = scratch_1.tmp_scalar_znx(module, 1);
module.scalar_znx_automorphism(
module.galois_element_inv(p),
&mut sk_inv_auto,
0,
&sk.data,
i,
);
module.svp_prepare(&mut sk_out_dft.data, i, &sk_inv_auto, 0);
});
}
sk_out.prep_fourier(module);
self.key.generate_from_sk(
module,
&sk,
&sk_out_dft,
source_xa,
source_xe,
sigma,
scratch_1,
);
self.key
.generate_from_sk(module, &sk, &sk_out, source_xa, source_xe, sigma, scratch_1);
self.p = p;
}

View File

@@ -4,7 +4,7 @@ use backend::{
};
use sampling::source::Source;
use crate::{GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext, GetRow, Infos, SecretKeyFourier, SetRow, derive_size};
use crate::{GLWECiphertext, GLWECiphertextFourier, GLWESecret, GetRow, Infos, ScratchCore, SetRow, derive_size};
pub struct GGLWECiphertext<C, B: Backend> {
pub(crate) data: MatZnxDft<C, B>,
@@ -75,7 +75,7 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGLWECiphertext<DataSelf, FFT64> {
&mut self,
module: &Module<FFT64>,
pt: &ScalarZnx<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -84,9 +84,9 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGLWECiphertext<DataSelf, FFT64> {
#[cfg(debug_assertions)]
{
assert_eq!(self.rank_in(), pt.cols());
assert_eq!(self.rank_out(), sk_dft.rank());
assert_eq!(self.rank_out(), sk.rank());
assert_eq!(self.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(sk.n(), module.n());
assert_eq!(pt.n(), module.n());
assert!(
scratch.available()
@@ -101,34 +101,14 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGLWECiphertext<DataSelf, FFT64> {
}
let rows: usize = self.rows();
let size: usize = self.size();
let basek: usize = self.basek();
let k: usize = self.k();
let rank_in: usize = self.rank_in();
let rank_out: usize = self.rank_out();
let cols_in: usize = self.rank_in();
let cols_out: usize = self.rank_out() + 1;
let (tmp_znx_pt, scrach_1) = scratch.tmp_vec_znx(module, 1, size);
let (tmp_znx_ct, scrach_2) = scrach_1.tmp_vec_znx(module, cols_out, size);
let (tmp_znx_dft_ct, scratch_3) = scrach_2.tmp_vec_znx_dft(module, cols_out, size);
let mut vec_znx_pt: GLWEPlaintext<&mut [u8]> = GLWEPlaintext {
data: tmp_znx_pt,
basek,
k,
};
let mut vec_znx_ct: GLWECiphertext<&mut [u8]> = GLWECiphertext {
data: tmp_znx_ct,
basek,
k,
};
let mut vec_znx_ct_dft: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier {
data: tmp_znx_dft_ct,
basek,
k,
};
let (mut tmp_pt, scrach_1) = scratch.tmp_glwe_pt(module, basek, k);
let (mut tmp_ct, scrach_2) = scrach_1.tmp_glwe_ct(module, basek, k, rank_out);
let (mut tmp_ct_dft, scratch_3) = scrach_2.tmp_glwe_fourier(module, basek, k, rank_out);
// For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns
//
@@ -141,29 +121,21 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGLWECiphertext<DataSelf, FFT64> {
//
// (-(a*s) + s0, a)
// (-(b*s) + s1, b)
(0..cols_in).for_each(|col_i| {
(0..rank_in).for_each(|col_i| {
(0..rows).for_each(|row_i| {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
vec_znx_pt.data.zero(); // zeroes for next iteration
module.vec_znx_add_scalar_inplace(&mut vec_znx_pt.data, 0, row_i, pt, col_i); // Selects the i-th
module.vec_znx_normalize_inplace(basek, &mut vec_znx_pt.data, 0, scratch_3);
tmp_pt.data.zero(); // zeroes for next iteration
module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, row_i, pt, col_i); // Selects the i-th
module.vec_znx_normalize_inplace(basek, &mut tmp_pt.data, 0, scratch_3);
// rlwe encrypt of vec_znx_pt into vec_znx_ct
vec_znx_ct.encrypt_sk(
module,
&vec_znx_pt,
sk_dft,
source_xa,
source_xe,
sigma,
scratch_3,
);
tmp_ct.encrypt_sk(module, &tmp_pt, sk, source_xa, source_xe, sigma, scratch_3);
// Switch vec_znx_ct into DFT domain
vec_znx_ct.dft(module, &mut vec_znx_ct_dft);
tmp_ct.dft(module, &mut tmp_ct_dft);
// Stores vec_znx_dft_ct into thw i-th row of the MatZnxDft
module.vmp_prepare_row(&mut self.data, row_i, col_i, &vec_znx_ct_dft.data);
self.set_row(module, row_i, col_i, &tmp_ct_dft);
});
});
}

View File

@@ -6,15 +6,8 @@ use backend::{
use sampling::source::Source;
use crate::{
ScratchCore,
automorphism::AutomorphismKey,
elem::{GetRow, Infos, SetRow},
glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
keys::SecretKeyFourier,
keyswitch_key::GLWESwitchingKey,
tensor_key::TensorKey,
utils::derive_size,
AutomorphismKey, GLWECiphertext, GLWECiphertextFourier, GLWESecret, GLWESwitchingKey, GetRow, Infos, ScratchCore, SetRow,
TensorKey, derive_size,
};
pub struct GGSWCiphertext<C, B: Backend> {
@@ -189,7 +182,7 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
&mut self,
module: &Module<FFT64>,
pt: &ScalarZnx<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -197,10 +190,10 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
) {
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(self.rank(), sk.rank());
assert_eq!(self.n(), module.n());
assert_eq!(pt.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(sk.n(), module.n());
}
let basek: usize = self.basek();
@@ -223,7 +216,7 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
tmp_ct.encrypt_sk_private(
module,
Some((&tmp_pt, col_j)),
sk_dft,
sk,
source_xa,
source_xe,
sigma,

View File

@@ -6,16 +6,8 @@ use backend::{
use sampling::source::Source;
use crate::{
SIX_SIGMA,
automorphism::AutomorphismKey,
elem::{Infos, SetMetaData},
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_ops::GLWEOps,
glwe_plaintext::GLWEPlaintext,
keys::{GLWEPublicKey, SecretDistribution, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
utils::derive_size,
AutomorphismKey, GGSWCiphertext, GLWECiphertextFourier, GLWEOps, GLWEPlaintext, GLWEPublicKey, GLWESecret, GLWESwitchingKey,
Infos, SIX_SIGMA, SecretDistribution, SetMetaData, derive_size,
};
pub struct GLWECiphertext<C> {
@@ -205,7 +197,7 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<FFT64>,
pt: &GLWEPlaintext<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -214,7 +206,7 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
self.encrypt_sk_private(
module,
Some((pt, 0)),
sk_dft,
sk,
source_xa,
source_xe,
sigma,
@@ -225,7 +217,7 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
pub fn encrypt_zero_sk<DataSk: AsRef<[u8]>>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -234,7 +226,7 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
self.encrypt_sk_private(
module,
None::<(&GLWEPlaintext<Vec<u8>>, usize)>,
sk_dft,
sk,
source_xa,
source_xe,
sigma,
@@ -573,7 +565,7 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<FFT64>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -581,8 +573,8 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
) {
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(self.rank(), sk.rank());
assert_eq!(sk.n(), module.n());
assert_eq!(self.n(), module.n());
if let Some((pt, col)) = pt {
assert_eq!(pt.n(), module.n());
@@ -615,7 +607,7 @@ impl<DataSelf: AsRef<[u8]> + AsMut<[u8]>> GLWECiphertext<DataSelf> {
// c[i] = norm(IDFT(DFT(c[i]) * DFT(s[i])))
module.vec_znx_dft(&mut ci_dft, 0, &self.data, i);
module.svp_apply_inplace(&mut ci_dft, 0, &sk_dft.data, i - 1);
module.svp_apply_inplace(&mut ci_dft, 0, &sk.data_fourier, i - 1);
let ci_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(ci_dft);
// use c[0] as buffer, which is overwritten later by the normalization step
@@ -730,15 +722,15 @@ impl<DataSelf: AsRef<[u8]>> GLWECiphertext<DataSelf> {
&self,
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
scratch: &mut Scratch,
) {
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(self.rank(), sk.rank());
assert_eq!(self.n(), module.n());
assert_eq!(pt.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(sk.n(), module.n());
}
let cols: usize = self.rank() + 1;
@@ -751,7 +743,7 @@ impl<DataSelf: AsRef<[u8]>> GLWECiphertext<DataSelf> {
// ci_dft = DFT(a[i]) * DFT(s[i])
let (mut ci_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, self.size()); // TODO optimize size when pt << ct
module.vec_znx_dft(&mut ci_dft, 0, &self.data, i);
module.svp_apply_inplace(&mut ci_dft, 0, &sk_dft.data, i - 1);
module.svp_apply_inplace(&mut ci_dft, 0, &sk.data_fourier, i - 1);
let ci_big = module.vec_znx_idft_consume(ci_dft);
// c0_big += a[i] * s[i]

View File

@@ -4,10 +4,7 @@ use backend::{
};
use sampling::source::Source;
use crate::{
ScratchCore, elem::Infos, ggsw_ciphertext::GGSWCiphertext, glwe_ciphertext::GLWECiphertext, glwe_plaintext::GLWEPlaintext,
keys::SecretKeyFourier, keyswitch_key::GLWESwitchingKey, utils::derive_size,
};
use crate::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, GLWESwitchingKey, Infos, ScratchCore, derive_size};
pub struct GLWECiphertextFourier<C, B: Backend> {
pub data: VecZnxDft<C, B>,
@@ -126,14 +123,14 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GLWECiphertextFourier<DataSelf, FFT64>
pub fn encrypt_zero_sk<DataSk: AsRef<[u8]>>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
scratch: &mut Scratch,
) {
let (mut tmp_ct, scratch1) = scratch.tmp_glwe_ct(module, self.basek(), self.k(), self.rank());
tmp_ct.encrypt_zero_sk(module, sk_dft, source_xa, source_xe, sigma, scratch1);
tmp_ct.encrypt_zero_sk(module, sk, source_xa, source_xe, sigma, scratch1);
tmp_ct.dft(module, self);
}
@@ -219,15 +216,15 @@ impl<DataSelf: AsRef<[u8]>> GLWECiphertextFourier<DataSelf, FFT64> {
&self,
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
scratch: &mut Scratch,
) {
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(self.rank(), sk.rank());
assert_eq!(self.n(), module.n());
assert_eq!(pt.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(sk.n(), module.n());
}
let cols = self.rank() + 1;
@@ -238,7 +235,7 @@ impl<DataSelf: AsRef<[u8]>> GLWECiphertextFourier<DataSelf, FFT64> {
{
(1..cols).for_each(|i| {
let (mut ci_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, self.size()); // TODO optimize size when pt << ct
module.svp_apply(&mut ci_dft, 0, &sk_dft.data, i - 1, &self.data, i);
module.svp_apply(&mut ci_dft, 0, &sk.data_fourier, i - 1, &self.data, i);
let ci_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(ci_dft);
module.vec_znx_big_add_inplace(&mut pt_big, 0, &ci_big, 0);
});

View File

@@ -4,7 +4,7 @@ use backend::{
};
use sampling::source::Source;
use crate::{elem::Infos, glwe_ciphertext_fourier::GLWECiphertextFourier};
use crate::{GLWECiphertextFourier, Infos};
#[derive(Clone, Copy, Debug)]
pub(crate) enum SecretDistribution {
@@ -14,25 +14,27 @@ pub(crate) enum SecretDistribution {
NONE,
}
pub struct SecretKey<T> {
pub struct GLWESecret<T, B: Backend> {
pub(crate) data: ScalarZnx<T>,
pub(crate) data_fourier: ScalarZnxDft<T, B>,
pub(crate) dist: SecretDistribution,
}
impl SecretKey<Vec<u8>> {
pub fn alloc<B: Backend>(module: &Module<B>, rank: usize) -> Self {
impl<B: Backend> GLWESecret<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, rank: usize) -> Self {
Self {
data: module.new_scalar_znx(rank),
data_fourier: module.new_scalar_znx_dft(rank),
dist: SecretDistribution::NONE,
}
}
pub fn bytes_of(module: &Module<FFT64>, rank: usize) -> usize {
module.bytes_of_scalar_znx(rank + 1)
pub fn bytes_of(module: &Module<B>, rank: usize) -> usize {
module.bytes_of_scalar_znx(rank) + module.bytes_of_scalar_znx_dft(rank)
}
}
impl<DataSelf> SecretKey<DataSelf> {
impl<DataSelf, B: Backend> GLWESecret<DataSelf, B> {
pub fn n(&self) -> usize {
self.data.n()
}
@@ -46,18 +48,20 @@ impl<DataSelf> SecretKey<DataSelf> {
}
}
impl<S: AsMut<[u8]> + AsRef<[u8]>> SecretKey<S> {
pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) {
impl<S: AsMut<[u8]> + AsRef<[u8]>> GLWESecret<S, FFT64> {
pub fn fill_ternary_prob(&mut self, module: &Module<FFT64>, prob: f64, source: &mut Source) {
(0..self.rank()).for_each(|i| {
self.data.fill_ternary_prob(i, prob, source);
});
self.prep_fourier(module);
self.dist = SecretDistribution::TernaryProb(prob);
}
pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) {
pub fn fill_ternary_hw(&mut self, module: &Module<FFT64>, hw: usize, source: &mut Source) {
(0..self.rank()).for_each(|i| {
self.data.fill_ternary_hw(i, hw, source);
});
self.prep_fourier(module);
self.dist = SecretDistribution::TernaryFixed(hw);
}
@@ -65,58 +69,11 @@ impl<S: AsMut<[u8]> + AsRef<[u8]>> SecretKey<S> {
self.data.zero();
self.dist = SecretDistribution::ZERO;
}
}
pub struct SecretKeyFourier<T, B: Backend> {
pub(crate) data: ScalarZnxDft<T, B>,
pub(crate) dist: SecretDistribution,
}
impl<DataSelf, B: Backend> SecretKeyFourier<DataSelf, B> {
pub fn n(&self) -> usize {
self.data.n()
}
pub fn log_n(&self) -> usize {
self.data.log_n()
}
pub fn rank(&self) -> usize {
self.data.cols()
}
}
impl<B: Backend> SecretKeyFourier<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, rank: usize) -> Self {
Self {
data: module.new_scalar_znx_dft(rank),
dist: SecretDistribution::NONE,
}
}
pub fn bytes_of(module: &Module<B>, rank: usize) -> usize {
module.bytes_of_scalar_znx_dft(rank + 1)
}
}
impl<D: AsRef<[u8]> + AsMut<[u8]>> SecretKeyFourier<D, FFT64> {
pub fn dft<S: AsRef<[u8]>>(&mut self, module: &Module<FFT64>, sk: &SecretKey<S>) {
#[cfg(debug_assertions)]
{
match sk.dist {
SecretDistribution::NONE => panic!("invalid sk: SecretDistribution::NONE"),
_ => {}
}
assert_eq!(self.n(), module.n());
assert_eq!(sk.n(), module.n());
assert_eq!(self.rank(), sk.rank());
}
pub(crate) fn prep_fourier(&mut self, module: &Module<FFT64>) {
(0..self.rank()).for_each(|i| {
module.svp_prepare(&mut self.data, i, &sk.data, i);
module.svp_prepare(&mut self.data_fourier, i, &self.data, i);
});
self.dist = sk.dist;
}
}
@@ -164,15 +121,15 @@ impl<C: AsRef<[u8]> + AsMut<[u8]>> GLWEPublicKey<C, FFT64> {
pub fn generate_from_sk<S: AsRef<[u8]>>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<S, FFT64>,
sk: &GLWESecret<S, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
) {
#[cfg(debug_assertions)]
{
match sk_dft.dist {
SecretDistribution::NONE => panic!("invalid sk_dft: SecretDistribution::NONE"),
match sk.dist {
SecretDistribution::NONE => panic!("invalid sk: SecretDistribution::NONE"),
_ => {}
}
}
@@ -185,14 +142,8 @@ impl<C: AsRef<[u8]> + AsMut<[u8]>> GLWEPublicKey<C, FFT64> {
self.rank(),
));
self.data.encrypt_zero_sk(
module,
sk_dft,
source_xa,
source_xe,
sigma,
scratch.borrow(),
);
self.dist = sk_dft.dist;
self.data
.encrypt_zero_sk(module, sk, source_xa, source_xe, sigma, scratch.borrow());
self.dist = sk.dist;
}
}

View File

@@ -1,9 +1,6 @@
use backend::{FFT64, Module, Scratch, VecZnx, VecZnxOps, ZnxZero};
use crate::{
elem::{Infos, SetMetaData},
glwe_ciphertext::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef},
};
use crate::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, Infos, SetMetaData};
pub trait GLWEOps: GLWECiphertextToMut + Infos + SetMetaData {
fn add<A, B>(&mut self, module: &Module<FFT64>, a: &A, b: &B)

View File

@@ -1,4 +1,4 @@
use crate::{ScratchCore, automorphism::AutomorphismKey, elem::Infos, glwe_ciphertext::GLWECiphertext, glwe_ops::GLWEOps};
use crate::{AutomorphismKey, GLWECiphertext, GLWEOps, Infos, ScratchCore};
use std::collections::HashMap;
use backend::{FFT64, Module, Scratch};

View File

@@ -1,11 +1,6 @@
use backend::{Backend, FFT64, Module, VecZnx, VecZnxAlloc, VecZnxToMut, VecZnxToRef};
use crate::{
elem::{Infos, SetMetaData},
glwe_ciphertext::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef},
glwe_ops::GLWEOps,
utils::derive_size,
};
use crate::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEOps, Infos, SetMetaData, derive_size};
pub struct GLWEPlaintext<C> {
pub data: VecZnx<C>,

View File

@@ -1,14 +1,7 @@
use backend::{Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, Scratch, ZnxZero};
use sampling::source::Source;
use crate::{
ScratchCore,
elem::{GetRow, Infos, SetRow},
gglwe_ciphertext::GGLWECiphertext,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
keys::{SecretKey, SecretKeyFourier},
};
use crate::{GGLWECiphertext, GGSWCiphertext, GLWECiphertextFourier, GLWESecret, GetRow, Infos, ScratchCore, SetRow};
pub struct GLWESwitchingKey<Data, B: Backend>(pub(crate) GGLWECiphertext<Data, B>);
@@ -144,8 +137,8 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GLWESwitchingKey<DataSelf, FFT64> {
pub fn generate_from_sk<DataSkIn: AsRef<[u8]>, DataSkOut: AsRef<[u8]>>(
&mut self,
module: &Module<FFT64>,
sk_in: &SecretKey<DataSkIn>,
sk_out_dft: &SecretKeyFourier<DataSkOut, FFT64>,
sk_in: &GLWESecret<DataSkIn, FFT64>,
sk_out: &GLWESecret<DataSkOut, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -154,7 +147,7 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GLWESwitchingKey<DataSelf, FFT64> {
self.0.encrypt_sk(
module,
&sk_in.data,
sk_out_dft,
sk_out,
source_xa,
source_xe,
sigma,

View File

@@ -4,10 +4,10 @@ pub mod gglwe_ciphertext;
pub mod ggsw_ciphertext;
pub mod glwe_ciphertext;
pub mod glwe_ciphertext_fourier;
pub mod glwe_keys;
pub mod glwe_ops;
pub mod glwe_packing;
pub mod glwe_plaintext;
pub mod keys;
pub mod keyswitch_key;
pub mod tensor_key;
#[cfg(test)]
@@ -24,10 +24,10 @@ pub use gglwe_ciphertext::*;
pub use ggsw_ciphertext::*;
pub use glwe_ciphertext::*;
pub use glwe_ciphertext_fourier::*;
pub use glwe_keys::*;
pub use glwe_ops::*;
pub use glwe_packing::*;
pub use glwe_plaintext::*;
pub use keys::*;
pub use keyswitch_key::*;
pub use tensor_key::*;
@@ -64,8 +64,7 @@ pub trait ScratchCore<B: Backend> {
k: usize,
rank: usize,
) -> (GLWECiphertextFourier<&mut [u8], B>, &mut Self);
fn tmp_sk(&mut self, module: &Module<B>, rank: usize) -> (SecretKey<&mut [u8]>, &mut Self);
fn tmp_sk_fourier(&mut self, module: &Module<B>, rank: usize) -> (SecretKeyFourier<&mut [u8], B>, &mut Self);
fn tmp_sk(&mut self, module: &Module<B>, rank: usize) -> (GLWESecret<&mut [u8], B>, &mut Self);
fn tmp_glwe_pk(
&mut self,
module: &Module<B>,
@@ -184,25 +183,16 @@ impl ScratchCore<FFT64> for Scratch {
)
}
fn tmp_sk(&mut self, module: &Module<FFT64>, rank: usize) -> (SecretKey<&mut [u8]>, &mut Self) {
let (data, scratch) = self.tmp_scalar_znx(module, rank + 1);
fn tmp_sk(&mut self, module: &Module<FFT64>, rank: usize) -> (GLWESecret<&mut [u8], FFT64>, &mut Self) {
let (data, scratch) = self.tmp_scalar_znx(module, rank);
let (data_fourier, scratch1) = scratch.tmp_scalar_znx_dft(module, rank);
(
SecretKey {
GLWESecret {
data,
data_fourier,
dist: SecretDistribution::NONE,
},
scratch,
)
}
fn tmp_sk_fourier(&mut self, module: &Module<FFT64>, rank: usize) -> (SecretKeyFourier<&mut [u8], FFT64>, &mut Self) {
let (data, scratch) = self.tmp_scalar_znx_dft(module, rank);
(
SecretKeyFourier {
data,
dist: SecretDistribution::NONE,
},
scratch,
scratch1,
)
}

View File

@@ -1,11 +1,7 @@
use backend::{Backend, FFT64, MatZnxDft, Module, ScalarZnx, ScalarZnxDftAlloc, ScalarZnxDftOps, Scratch, VecZnxDftOps};
use backend::{Backend, FFT64, MatZnxDft, Module, ScalarZnxDftOps, Scratch};
use sampling::source::Source;
use crate::{
elem::Infos,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
};
use crate::{GLWESecret, GLWESwitchingKey, Infos, ScratchCore};
pub struct TensorKey<C, B: Backend> {
pub(crate) keys: Vec<GLWESwitchingKey<C, B>>,
@@ -59,7 +55,7 @@ impl<T, B: Backend> TensorKey<T, B> {
impl TensorKey<Vec<u8>, FFT64> {
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, basek, k, rank)
GLWESecret::bytes_of(module, 1) + GLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank)
}
}
@@ -67,7 +63,7 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> TensorKey<DataSelf, FFT64> {
pub fn generate_from_sk<DataSk: AsRef<[u8]>>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
sk: &GLWESecret<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -75,29 +71,28 @@ impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> TensorKey<DataSelf, FFT64> {
) {
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(self.rank(), sk.rank());
assert_eq!(self.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(sk.n(), module.n());
}
let rank: usize = self.rank();
let (mut sk_ij, scratch1) = scratch.tmp_sk(module, 1);
(0..rank).for_each(|i| {
(i..rank).for_each(|j| {
let (mut sk_ij_dft, scratch1) = scratch.tmp_scalar_znx_dft(module, 1);
module.svp_apply(&mut sk_ij_dft, 0, &sk_dft.data, i, &sk_dft.data, j);
let sk_ij: ScalarZnx<&mut [u8]> = module
.vec_znx_idft_consume(sk_ij_dft.as_vec_znx_dft())
.to_vec_znx_small()
.to_scalar_znx();
let sk_ij: SecretKey<&mut [u8]> = SecretKey {
data: sk_ij,
dist: sk_dft.dist,
};
self.at_mut(i, j).generate_from_sk(
module, &sk_ij, sk_dft, source_xa, source_xe, sigma, scratch1,
module.svp_apply(
&mut sk_ij.data_fourier,
0,
&sk.data_fourier,
i,
&sk.data_fourier,
j,
);
module.svp_idft(&mut sk_ij.data, 0, &sk_ij.data_fourier, 0, scratch1);
self.at_mut(i, j)
.generate_from_sk(module, &sk_ij, sk, source_xa, source_xe, sigma, scratch1);
});
})
}

View File

@@ -2,11 +2,7 @@ use backend::{FFT64, Module, ScalarZnxOps, ScratchOwned, Stats, VecZnxOps};
use sampling::source::Source;
use crate::{
automorphism::AutomorphismKey,
elem::{GetRow, Infos},
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
AutomorphismKey, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GetRow, Infos,
test_fft64::gglwe::log2_std_noise_gglwe_product,
};
@@ -44,11 +40,8 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
| AutomorphismKey::automorphism_scratch_space(&module, basek, k_ksk, k_ksk, k_ksk, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
auto_key_in.generate_from_sk(
@@ -78,7 +71,7 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ksk, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ksk);
let mut sk_auto: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
let mut sk_auto: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
(0..rank).for_each(|i| {
module.scalar_znx_automorphism(
@@ -90,14 +83,13 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
);
});
let mut sk_auto_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_auto_dft.dft(&module, &sk_auto);
sk_auto.prep_fourier(&module);
(0..auto_key_out.rank_in()).for_each(|col_i| {
(0..auto_key_out.rows()).for_each(|row_i| {
auto_key_out.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -141,11 +133,8 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk
| AutomorphismKey::automorphism_inplace_scratch_space(&module, basek, k_ksk, k_ksk, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
auto_key.generate_from_sk(
@@ -175,7 +164,7 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ksk, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ksk);
let mut sk_auto: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
let mut sk_auto: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
(0..rank).for_each(|i| {
module.scalar_znx_automorphism(
@@ -187,14 +176,13 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk
);
});
let mut sk_auto_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_auto_dft.dft(&module, &sk_auto);
sk_auto.prep_fourier(&module);
(0..auto_key.rank_in()).for_each(|col_i| {
(0..auto_key.rows()).for_each(|row_i| {
auto_key.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();

View File

@@ -2,12 +2,7 @@ use backend::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToMut, ScratchO
use sampling::source::Source;
use crate::{
elem::{GetRow, Infos},
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
GGSWCiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GetRow, Infos,
test_fft64::ggsw::noise_ggsw_product,
};
@@ -88,22 +83,16 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk),
);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -116,7 +105,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in
(0..ksk.rank_in()).for_each(|col_i| {
(0..ksk.rows()).for_each(|row_i| {
ksk.get_row(&module, row_i, col_i, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_out, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i);
let std_pt: f64 = pt.data.std(0, basek) * (k_ksk as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
@@ -161,29 +150,20 @@ fn test_key_switch(
),
);
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in_s0s1);
sk0_dft.dft(&module, &sk0);
let mut sk1: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1);
sk1_dft.dft(&module, &sk1);
let mut sk2: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s1s2);
sk2.fill_ternary_prob(0.5, &mut source_xs);
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s1s2);
sk2_dft.dft(&module, &sk2);
let mut sk2: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s1s2);
sk2.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.generate_from_sk(
&module,
&sk0,
&sk1_dft,
&sk1,
&mut source_xa,
&mut source_xe,
sigma,
@@ -194,7 +174,7 @@ fn test_key_switch(
ct_gglwe_s1s2.generate_from_sk(
&module,
&sk1,
&sk2_dft,
&sk2,
&mut source_xa,
&mut source_xe,
sigma,
@@ -211,7 +191,7 @@ fn test_key_switch(
(0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| {
(0..ct_gglwe_s0s2.rows()).for_each(|row_i| {
ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk2, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk0.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -263,29 +243,20 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64,
),
);
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in_s0s1);
sk0_dft.dft(&module, &sk0);
let mut sk1: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1);
sk1_dft.dft(&module, &sk1);
let mut sk2: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s0s1);
sk2.fill_ternary_prob(0.5, &mut source_xs);
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1);
sk2_dft.dft(&module, &sk2);
let mut sk2: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1);
sk2.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.generate_from_sk(
&module,
&sk0,
&sk1_dft,
&sk1,
&mut source_xa,
&mut source_xe,
sigma,
@@ -296,7 +267,7 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64,
ct_gglwe_s1s2.generate_from_sk(
&module,
&sk1,
&sk2_dft,
&sk2,
&mut source_xa,
&mut source_xe,
sigma,
@@ -315,7 +286,7 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64,
(0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| {
(0..ct_gglwe_s0s2.rows()).for_each(|row_i| {
ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk2, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk0.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -375,23 +346,17 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_in.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -401,7 +366,7 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
ct_rgsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -435,7 +400,7 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
(0..rank_in).for_each(|col_i| {
(0..ct_gglwe_out.rows()).for_each(|row_i| {
ct_gglwe_out.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -496,23 +461,17 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6
pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -522,7 +481,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6
ct_rgsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -542,7 +501,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6
(0..rank_in).for_each(|col_i| {
(0..ct_gglwe.rows()).for_each(|row_i| {
ct_gglwe.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();

View File

@@ -5,14 +5,8 @@ use backend::{
use sampling::source::Source;
use crate::{
GGSWCiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GetRow, Infos, TensorKey,
automorphism::AutomorphismKey,
elem::{GetRow, Infos},
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
tensor_key::TensorKey,
};
use super::gglwe::var_noise_gglwe_product;
@@ -94,16 +88,13 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct.encrypt_sk(
&module,
&pt_scalar,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -121,14 +112,14 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -174,22 +165,16 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
let var_xs: f64 = 0.5;
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(&module, var_xs, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(&module, var_xs, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -197,7 +182,7 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
);
tsk.generate_from_sk(
&module,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -209,7 +194,7 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
ct_in.encrypt_sk(
&module,
&pt_scalar,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -229,14 +214,14 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -293,22 +278,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
let var_xs: f64 = 0.5;
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(&module, var_xs, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(&module, var_xs, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -316,7 +295,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
);
tsk.generate_from_sk(
&module,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -328,7 +307,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
ct.encrypt_sk(
&module,
&pt_scalar,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -348,14 +327,14 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -470,11 +449,8 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
let var_xs: f64 = 0.5;
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, var_xs, &mut source_xs);
auto_key.generate_from_sk(
&module,
@@ -487,7 +463,7 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
);
tensor_key.generate_from_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -499,7 +475,7 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
ct_in.encrypt_sk(
&module,
&pt_scalar,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -521,14 +497,14 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -583,11 +559,8 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
let var_xs: f64 = 0.5;
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, var_xs, &mut source_xs);
auto_key.generate_from_sk(
&module,
@@ -600,7 +573,7 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
);
tensor_key.generate_from_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -612,7 +585,7 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
ct.encrypt_sk(
&module,
&pt_scalar,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -634,14 +607,14 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -705,16 +678,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize,
),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw_rhs.encrypt_sk(
&module,
&pt_ggsw_rhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -724,7 +694,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize,
ct_ggsw_lhs_in.encrypt_sk(
&module,
&pt_ggsw_lhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -747,13 +717,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize,
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_ggsw_lhs_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt.data, 0, &pt_want.data, 0);
@@ -818,16 +788,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, 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);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw_rhs.encrypt_sk(
&module,
&pt_ggsw_rhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -837,7 +804,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank
ct_ggsw_lhs.encrypt_sk(
&module,
&pt_ggsw_lhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -860,13 +827,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_ggsw_lhs.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt.data, 0, &pt_want.data, 0);

View File

@@ -6,13 +6,8 @@ use itertools::izip;
use sampling::source::Source;
use crate::{
GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWEPublicKey, GLWESecret, Infos,
automorphism::AutomorphismKey,
elem::Infos,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{GLWEPublicKey, SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
test_fft64::{gglwe::log2_std_noise_gglwe_product, ggsw::noise_ggsw_product},
};
@@ -106,11 +101,8 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma:
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k()),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut data_want: Vec<i64> = vec![0i64; module.n()];
@@ -123,7 +115,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma:
ct.encrypt_sk(
&module,
&pt,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -132,7 +124,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma:
pt.data.zero();
ct.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt, &sk, scratch.borrow());
let mut data_have: Vec<i64> = vec![0i64; module.n()];
@@ -161,10 +153,8 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, ct_k: usize, sigma: f64, ran
let mut source_xe: Source = Source::new([1u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut ct_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, ct_k, rank);
@@ -175,13 +165,13 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, ct_k: usize, sigma: f64, ran
ct_dft.encrypt_zero_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
ct_dft.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct_dft.decrypt(&module, &mut pt, &sk, scratch.borrow());
assert!((sigma - pt.data.std(0, basek) * (ct_k as f64).exp2()) <= 0.2);
}
@@ -197,13 +187,11 @@ fn test_encrypt_pk(log_n: usize, basek: usize, ct_k: usize, k_pk: usize, sigma:
let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xu: Source = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut pk: GLWEPublicKey<Vec<u8>, FFT64> = GLWEPublicKey::alloc(&module, basek, k_pk, rank);
pk.generate_from_sk(&module, &sk_dft, &mut source_xa, &mut source_xe, sigma);
pk.generate_from_sk(&module, &sk, &mut source_xa, &mut source_xe, sigma);
let mut scratch: ScratchOwned = ScratchOwned::new(
GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
@@ -231,7 +219,7 @@ fn test_encrypt_pk(log_n: usize, basek: usize, ct_k: usize, k_pk: usize, sigma:
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, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
@@ -289,22 +277,16 @@ fn test_keyswitch(
),
);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, in_rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, in_rank);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, in_rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, out_rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, out_rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, out_rank);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -314,7 +296,7 @@ fn test_keyswitch(
ct_in.encrypt_sk(
&module,
&pt_want,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -323,7 +305,7 @@ fn test_keyswitch(
ct_out.keyswitch(&module, &ct_in, &ksk, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -374,22 +356,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize,
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct_glwe.k(), rank, ct_grlwe.k()),
);
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk0.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk1_dft.dft(&module, &sk1);
let mut sk1: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk1.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_grlwe.generate_from_sk(
&module,
&sk0,
&sk1_dft,
&sk1,
&mut source_xa,
&mut source_xe,
sigma,
@@ -399,7 +375,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize,
ct_glwe.encrypt_sk(
&module,
&pt_want,
&sk0_dft,
&sk0,
&mut source_xa,
&mut source_xe,
sigma,
@@ -408,7 +384,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize,
ct_glwe.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk1_dft, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk1, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -462,17 +438,14 @@ fn test_automorphism(
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank)
AutomorphismKey::generate_from_sk_scratch_space(&module, basek, autokey.k(), rank)
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_out.k())
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
| 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);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
autokey.generate_from_sk(
&module,
@@ -487,7 +460,7 @@ fn test_automorphism(
ct_in.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -495,7 +468,7 @@ fn test_automorphism(
);
ct_out.automorphism(&module, &ct_in, &autokey, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
module.vec_znx_normalize_inplace(basek, &mut pt_have.data, 0, scratch.borrow());
@@ -544,17 +517,14 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank)
AutomorphismKey::generate_from_sk_scratch_space(&module, basek, autokey.k(), rank)
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k())
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
| GLWECiphertext::automorphism_inplace_scratch_space(&module, basek, ct.k(), autokey.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
autokey.generate_from_sk(
&module,
@@ -569,7 +539,7 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
ct.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -577,7 +547,7 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
);
ct.automorphism_inplace(&module, &autokey, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
module.vec_znx_normalize_inplace(basek, &mut pt_have.data, 0, scratch.borrow());
@@ -645,16 +615,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi
),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -664,7 +631,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi
ct_glwe_in.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -673,7 +640,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi
ct_glwe_out.external_product(&module, &ct_glwe_in, &ct_ggsw, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
@@ -742,16 +709,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k
| 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);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -761,7 +725,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k
ct_glwe.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -770,7 +734,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k
ct_glwe.external_product_inplace(&module, &ct_ggsw, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);

View File

@@ -1,12 +1,5 @@
use crate::{
GLWEOps,
elem::Infos,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWEOps, GLWEPlaintext, GLWESecret, GLWESwitchingKey, Infos,
test_fft64::{gglwe::log2_std_noise_gglwe_product, ggsw::noise_ggsw_product},
};
use backend::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, ZnxViewMut};
@@ -94,22 +87,16 @@ fn test_keyswitch(
),
);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -119,7 +106,7 @@ fn test_keyswitch(
ct_glwe_in.encrypt_sk(
&module,
&pt_want,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -130,7 +117,7 @@ fn test_keyswitch(
ct_glwe_dft_out.keyswitch(&module, &ct_glwe_dft_in, &ksk, scratch.borrow());
ct_glwe_dft_out.idft(&module, &mut ct_glwe_out, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -182,22 +169,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
| 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);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -207,7 +188,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
ct_glwe.encrypt_sk(
&module,
&pt_want,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -218,7 +199,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
ct_rlwe_dft.keyswitch_inplace(&module, &ksk, scratch.borrow());
ct_rlwe_dft.idft(&module, &mut ct_glwe, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -281,16 +262,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
| 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);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -300,7 +278,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
ct_in.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -311,7 +289,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
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.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
pt_want.rotate_inplace(&module, k);
pt_have.sub_inplace_ab(&module, &pt_want);
@@ -381,16 +359,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
| GLWECiphertextFourier::external_product_inplace_scratch_space(&module, basek, ct.k(), ct_ggsw.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -400,7 +375,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
ct.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -411,7 +386,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
ct_rlwe_dft.external_product_inplace(&module, &ct_ggsw, scratch.borrow());
ct_rlwe_dft.idft(&module, &mut ct, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
pt_want.rotate_inplace(&module, k);
pt_have.sub_inplace_ab(&module, &pt_want);

View File

@@ -1,11 +1,4 @@
use crate::{
automorphism::AutomorphismKey,
glwe_ciphertext::GLWECiphertext,
glwe_ops::GLWEOps,
glwe_packing::StreamPacker,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
};
use crate::{AutomorphismKey, GLWECiphertext, GLWEOps, GLWEPlaintext, GLWESecret, StreamPacker};
use std::collections::HashMap;
use backend::{Encoding, FFT64, Module, ScratchOwned, Stats};
@@ -35,11 +28,8 @@ fn packing() {
| StreamPacker::scratch_space(&module, basek, ct_k, atk_k, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
let mut data: Vec<i64> = vec![0i64; module.n()];
@@ -74,7 +64,7 @@ fn packing() {
ct.encrypt_sk(
&module,
&pt,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -87,7 +77,7 @@ fn packing() {
ct.encrypt_sk(
&module,
&pt,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -123,7 +113,7 @@ fn packing() {
});
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, scratch.borrow());
if i & 1 == 0 {
pt.sub_inplace_ab(&module, &pt_want);

View File

@@ -1,15 +1,7 @@
use backend::{
FFT64, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScratchOwned, Stats, VecZnxDftOps, VecZnxOps,
};
use backend::{FFT64, Module, ScalarZnxDftOps, ScratchOwned, Stats, VecZnxOps};
use sampling::source::Source;
use crate::{
elem::{GetRow, Infos},
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
tensor_key::TensorKey,
};
use crate::{GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GetRow, Infos, TensorKey};
#[test]
fn encrypt_sk() {
@@ -37,15 +29,12 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
rank,
));
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
tensor_key.generate_from_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -55,22 +44,26 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
let mut ct_glwe_fourier: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k);
let mut sk_ij = GLWESecret::alloc(&module, 1);
(0..rank).for_each(|i| {
(0..rank).for_each(|j| {
let mut sk_ij_dft: ScalarZnxDft<Vec<u8>, FFT64> = module.new_scalar_znx_dft(1);
module.svp_apply(&mut sk_ij_dft, 0, &sk_dft.data, i, &sk_dft.data, j);
let sk_ij: ScalarZnx<Vec<u8>> = module
.vec_znx_idft_consume(sk_ij_dft.as_vec_znx_dft())
.to_vec_znx_small()
.to_scalar_znx();
module.svp_apply(
&mut sk_ij.data_fourier,
0,
&sk.data_fourier,
i,
&sk.data_fourier,
j,
);
module.svp_idft(&mut sk_ij.data, 0, &sk_ij.data_fourier, 0, scratch.borrow());
(0..tensor_key.rank_in()).for_each(|col_i| {
(0..tensor_key.rows()).for_each(|row_i| {
tensor_key
.at(i, j)
.get_row(&module, row_i, col_i, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij, col_i);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
let std_pt: f64 = pt.data.std(0, basek) * (k as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
});

View File

@@ -3,14 +3,7 @@ use std::collections::HashMap;
use backend::{FFT64, FillUniform, Module, ScratchOwned, Stats, VecZnxOps, ZnxView, ZnxViewMut};
use sampling::source::Source;
use crate::{
automorphism::AutomorphismKey,
elem::Infos,
glwe_ciphertext::GLWECiphertext,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
test_fft64::gglwe::var_noise_gglwe_product,
};
use crate::{AutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, test_fft64::gglwe::var_noise_gglwe_product};
#[test]
fn trace_inplace() {
@@ -42,11 +35,8 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us
| GLWECiphertext::trace_inplace_scratch_space(&module, basek, ct.k(), k_autokey, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&&module, 0.5, &mut source_xs);
let mut data_want: Vec<i64> = vec![0i64; module.n()];
@@ -61,7 +51,7 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us
ct.encrypt_sk(
&module,
&pt_have,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -89,7 +79,7 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us
(0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]);
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow());

View File

@@ -2,12 +2,7 @@ use std::collections::HashMap;
use backend::{FFT64, Module, Scratch};
use crate::{
automorphism::AutomorphismKey,
elem::{Infos, SetMetaData},
glwe_ciphertext::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef},
glwe_ops::GLWEOps,
};
use crate::{AutomorphismKey, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEOps, Infos, SetMetaData};
impl GLWECiphertext<Vec<u8>> {
pub fn trace_galois_elements(module: &Module<FFT64>) -> Vec<i64> {