working on adding rank to glwe (all test passing)

This commit is contained in:
Jean-Philippe Bossuat
2025-05-13 17:21:41 +02:00
parent 31b14ee585
commit dee889dc0c
22 changed files with 2020 additions and 2030 deletions

View File

@@ -60,11 +60,11 @@ impl<D: AsRef<[u8]>> ZnxView for MatZnxDft<D, FFT64> {
} }
impl<D, B: Backend> MatZnxDft<D, B> { impl<D, B: Backend> MatZnxDft<D, B> {
pub(crate) fn cols_in(&self) -> usize { pub fn cols_in(&self) -> usize {
self.cols_in self.cols_in
} }
pub(crate) fn cols_out(&self) -> usize { pub fn cols_out(&self) -> usize {
self.cols_out self.cols_out
} }
} }

View File

@@ -6,7 +6,7 @@ use rlwe::{
external_product::{ external_product::{
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace, ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace,
}, },
ggsw::GGSWCiphertext, ggsw_ciphertext::GGSWCiphertext,
glwe::GLWECiphertext, glwe::GLWECiphertext,
keys::{SecretKey, SecretKeyFourier}, keys::{SecretKey, SecretKeyFourier},
}; };

View File

@@ -1,6 +1,6 @@
use base2k::{Backend, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}; use base2k::{Backend, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos};
use crate::{glwe::GLWECiphertextFourier, utils::derive_size}; use crate::{glwe_ciphertext_fourier::GLWECiphertextFourier, utils::derive_size};
pub trait Infos { pub trait Infos {
type Inner: ZnxInfos; type Inner: ZnxInfos;
@@ -23,7 +23,7 @@ pub trait Infos {
} }
/// Returns the number of polynomials in each row. /// Returns the number of polynomials in each row.
fn rank(&self) -> usize { fn cols(&self) -> usize {
self.inner().cols() self.inner().cols()
} }
@@ -36,7 +36,7 @@ pub trait Infos {
/// Returns the total number of small polynomials. /// Returns the total number of small polynomials.
fn poly_count(&self) -> usize { fn poly_count(&self) -> usize {
self.rows() * self.rank() * self.size() self.rows() * self.cols() * self.size()
} }
/// Returns the base 2 logarithm of the ciphertext base. /// Returns the base 2 logarithm of the ciphertext base.

View File

@@ -1,105 +0,0 @@
use base2k::{Backend, Module, Scratch};
use sampling::source::Source;
pub trait EncryptSkScratchSpace {
fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, ct_size: usize) -> usize;
}
pub trait EncryptSk<DataCt, DataPt, DataSk, B: Backend> {
type Ciphertext;
type Plaintext;
type SecretKey;
fn encrypt_sk(
&self,
module: &Module<B>,
ct: &mut Self::Ciphertext,
pt: &Self::Plaintext,
sk: &Self::SecretKey,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
);
}
pub trait EncryptZeroSkScratchSpace {
fn encrypt_zero_sk_scratch_space<B: Backend>(module: &Module<B>, ct_size: usize) -> usize;
}
pub trait EncryptZeroSk<DatCt, DataSk, B: Backend> {
type Ciphertext;
type SecretKey;
fn encrypt_zero_sk(
&self,
module: &Module<B>,
ct: &mut Self::Ciphertext,
sk: &Self::SecretKey,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
);
}
pub trait EncryptPkScratchSpace {
fn encrypt_pk_scratch_space<B: Backend>(module: &Module<B>, ct_size: usize) -> usize;
}
pub trait EncryptPk<DataCt, DataPt, DataPk, B: Backend> {
type Ciphertext;
type Plaintext;
type PublicKey;
fn encrypt_pk(
&self,
module: &Module<B>,
ct: &mut Self::Ciphertext,
pt: &Self::Plaintext,
pk: &Self::PublicKey,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
);
}
pub trait EncryptZeroPkScratchSpace {
fn encrypt_zero_pk_scratch_space<B: Backend>(module: &Module<B>, ct_size: usize) -> usize;
}
pub trait EncryptZeroPk<DataCt, DataPk, B: Backend> {
type Ciphertext;
type PublicKey;
fn encrypt_zero_pk(
&self,
module: &Module<B>,
ct: &mut Self::Ciphertext,
pk: &Self::PublicKey,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
);
}
pub trait Decrypt<DataPt, DataCt, DataSk, B: Backend> {
type Plaintext;
type Ciphertext;
type SecretKey;
fn decrypt(
&self,
module: &Module<B>,
pt: &mut Self::Plaintext,
ct: &Self::Ciphertext,
sk: &Self::SecretKey,
scratch: &mut Scratch,
);
}

View File

@@ -1,19 +0,0 @@
use base2k::{FFT64, Module, Scratch};
pub trait ExternalProductScratchSpace {
fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize;
}
pub trait ExternalProduct<DataLhs, DataRhs> {
type Lhs;
type Rhs;
fn external_product(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch);
}
pub trait ExternalProductInplaceScratchSpace {
fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize;
}
pub trait ExternalProductInplace<DataRhs> {
type Rhs;
fn external_product_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch);
}

View File

@@ -0,0 +1,253 @@
use base2k::{
Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftScratch, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx,
ScalarZnxDft, ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigOps, VecZnxBigScratch,
VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos,
ZnxZero,
};
use sampling::source::Source;
use crate::{
elem::{GetRow, Infos, SetRow},
glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::SecretKeyFourier,
utils::derive_size,
vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
};
pub struct GGLWECiphertext<C, B: Backend> {
pub(crate) data: MatZnxDft<C, B>,
pub(crate) basek: usize,
pub(crate) k: usize,
}
impl<B: Backend> GGLWECiphertext<Vec<u8>, B> {
pub fn new(module: &Module<B>, base2k: usize, k: usize, rows: usize, rank_in: usize, rank_out: usize) -> Self {
Self {
data: module.new_mat_znx_dft(rows, rank_in, rank_out + 1, derive_size(base2k, k)),
basek: base2k,
k,
}
}
}
impl<T, B: Backend> Infos for GGLWECiphertext<T, B> {
type Inner = MatZnxDft<T, B>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.basek
}
fn k(&self) -> usize {
self.k
}
}
impl<T, B: Backend> GGLWECiphertext<T, B> {
pub fn rank(&self) -> usize {
self.data.cols_out() - 1
}
}
impl<C, B: Backend> MatZnxDftToMut<B> for GGLWECiphertext<C, B>
where
MatZnxDft<C, B>: MatZnxDftToMut<B>,
{
fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> {
self.data.to_mut()
}
}
impl<C, B: Backend> MatZnxDftToRef<B> for GGLWECiphertext<C, B>
where
MatZnxDft<C, B>: MatZnxDftToRef<B>,
{
fn to_ref(&self) -> MatZnxDft<&[u8], B> {
self.data.to_ref()
}
}
impl GGLWECiphertext<Vec<u8>, FFT64> {
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
GLWECiphertext::encrypt_sk_scratch_space(module, rank, size)
+ module.bytes_of_vec_znx(rank + 1, size)
+ module.bytes_of_vec_znx(1, size)
+ module.bytes_of_vec_znx_dft(rank + 1, size)
}
pub fn encrypt_pk_scratch_space(_module: &Module<FFT64>, _rank: usize, _pk_size: usize) -> usize {
unimplemented!()
}
}
impl<DataSelf> GGLWECiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + ZnxInfos,
{
pub fn encrypt_sk<DataPt, DataSk>(
&mut self,
module: &Module<FFT64>,
pt: &ScalarZnx<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
ScalarZnx<DataPt>: ScalarZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(self.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(pt.n(), module.n());
}
let rows: usize = self.rows();
let size: usize = self.size();
let basek: usize = self.basek();
let k: usize = self.k();
let cols: usize = self.rank() + 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, size);
let (tmp_znx_dft_ct, scratch_3) = scrach_2.tmp_vec_znx_dft(module, cols, 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,
};
(0..rows).for_each(|row_i| {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
module.vec_znx_add_scalar_inplace(&mut vec_znx_pt, 0, row_i, pt, 0);
module.vec_znx_normalize_inplace(basek, &mut vec_znx_pt, 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,
bound,
scratch_3,
);
vec_znx_pt.data.zero(); // zeroes for next iteration
// Switch vec_znx_ct into DFT domain
vec_znx_ct.dft(module, &mut vec_znx_ct_dft);
// Stores vec_znx_dft_ct into thw i-th row of the MatZnxDft
module.vmp_prepare_row(self, row_i, 0, &vec_znx_ct_dft);
});
}
}
impl<C> GetRow<FFT64> for GGLWECiphertext<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>,
{
fn get_row<R>(&self, module: &Module<FFT64>, row_i: usize, col_j: usize, res: &mut GLWECiphertextFourier<R, FFT64>)
where
VecZnxDft<R, FFT64>: VecZnxDftToMut<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(col_j, 0);
}
module.vmp_extract_row(res, self, row_i, col_j);
}
}
impl<C> SetRow<FFT64> for GGLWECiphertext<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
{
fn set_row<R>(&mut self, module: &Module<FFT64>, row_i: usize, col_j: usize, a: &GLWECiphertextFourier<R, FFT64>)
where
VecZnxDft<R, FFT64>: VecZnxDftToRef<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(col_j, 0);
}
module.vmp_prepare_row(self, row_i, col_j, a);
}
}
impl VecGLWEProductScratchSpace for GGLWECiphertext<Vec<u8>, FFT64> {
fn prod_with_glwe_scratch_space(module: &Module<FFT64>, res_size: usize, a_size: usize, grlwe_size: usize) -> usize {
module.bytes_of_vec_znx_dft(2, grlwe_size)
+ (module.vec_znx_big_normalize_tmp_bytes()
| (module.vmp_apply_tmp_bytes(res_size, a_size, a_size, 1, 2, grlwe_size)
+ module.bytes_of_vec_znx_dft(1, a_size)))
}
}
impl<C> VecGLWEProduct for GGLWECiphertext<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64> + ZnxInfos,
{
fn prod_with_glwe<R, A>(
&self,
module: &Module<FFT64>,
res: &mut GLWECiphertext<R>,
a: &GLWECiphertext<A>,
scratch: &mut Scratch,
) where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>,
VecZnx<R>: VecZnxToMut,
VecZnx<A>: VecZnxToRef,
{
let log_base2k: usize = self.basek();
#[cfg(debug_assertions)]
{
assert_eq!(res.basek(), log_base2k);
assert_eq!(a.basek(), log_base2k);
assert_eq!(self.n(), module.n());
assert_eq!(res.n(), module.n());
assert_eq!(a.n(), module.n());
}
let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, 2, self.size()); // Todo optimise
{
let (mut a1_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, 1, a.size());
module.vec_znx_dft(&mut a1_dft, 0, a, 1);
module.vmp_apply(&mut res_dft, &a1_dft, self, scratch2);
}
let mut res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft);
module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0);
module.vec_znx_big_normalize(log_base2k, res, 0, &res_big, 0, scratch1);
module.vec_znx_big_normalize(log_base2k, res, 1, &res_big, 1, scratch1);
}
}

View File

@@ -1,324 +0,0 @@
use base2k::{
Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftScratch, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx,
ScalarZnxDft, ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigOps, VecZnxBigScratch,
VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos,
ZnxZero,
};
use sampling::source::Source;
use crate::{
elem::{GetRow, Infos, SetRow},
encryption::EncryptSkScratchSpace,
external_product::{
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace,
},
glwe::{GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext, encrypt_glwe_sk},
keys::SecretKeyFourier,
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace},
keyswitch_key::GLWEKeySwitchKey,
utils::derive_size,
vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
};
pub struct GGSWCiphertext<C, B: Backend> {
pub data: MatZnxDft<C, B>,
pub log_base2k: usize,
pub log_k: usize,
}
impl<B: Backend> GGSWCiphertext<Vec<u8>, B> {
pub fn new(module: &Module<B>, log_base2k: usize, log_k: usize, rows: usize) -> Self {
Self {
data: module.new_mat_znx_dft(rows, 2, 2, derive_size(log_base2k, log_k)),
log_base2k: log_base2k,
log_k: log_k,
}
}
}
impl<T, B: Backend> Infos for GGSWCiphertext<T, B> {
type Inner = MatZnxDft<T, B>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.log_base2k
}
fn k(&self) -> usize {
self.log_k
}
}
impl<C, B: Backend> MatZnxDftToMut<B> for GGSWCiphertext<C, B>
where
MatZnxDft<C, B>: MatZnxDftToMut<B>,
{
fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> {
self.data.to_mut()
}
}
impl<C, B: Backend> MatZnxDftToRef<B> for GGSWCiphertext<C, B>
where
MatZnxDft<C, B>: MatZnxDftToRef<B>,
{
fn to_ref(&self) -> MatZnxDft<&[u8], B> {
self.data.to_ref()
}
}
impl GGSWCiphertext<Vec<u8>, FFT64> {
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, size: usize) -> usize {
GLWECiphertext::encrypt_sk_scratch_space(module, size)
+ module.bytes_of_vec_znx(2, size)
+ module.bytes_of_vec_znx(1, size)
+ module.bytes_of_vec_znx_dft(2, size)
}
}
pub fn encrypt_rgsw_sk<C, P, S>(
module: &Module<FFT64>,
ct: &mut GGSWCiphertext<C, FFT64>,
pt: &ScalarZnx<P>,
sk_dft: &SecretKeyFourier<S, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
ScalarZnx<P>: ScalarZnxToRef,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let size: usize = ct.size();
let log_base2k: usize = ct.basek();
let (tmp_znx_pt, scratch_1) = scratch.tmp_vec_znx(module, 1, size);
let (tmp_znx_ct, scrach_2) = scratch_1.tmp_vec_znx(module, 2, size);
let mut vec_znx_pt: GLWEPlaintext<&mut [u8]> = GLWEPlaintext {
data: tmp_znx_pt,
log_base2k: log_base2k,
log_k: ct.k(),
};
let mut vec_znx_ct: GLWECiphertext<&mut [u8]> = GLWECiphertext {
data: tmp_znx_ct,
log_base2k: log_base2k,
log_k: ct.k(),
};
(0..ct.rows()).for_each(|row_j| {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
module.vec_znx_add_scalar_inplace(&mut vec_znx_pt, 0, row_j, pt, 0);
module.vec_znx_normalize_inplace(log_base2k, &mut vec_znx_pt, 0, scrach_2);
(0..ct.rank()).for_each(|col_i| {
// rlwe encrypt of vec_znx_pt into vec_znx_ct
encrypt_glwe_sk(
module,
&mut vec_znx_ct,
Some((&vec_znx_pt, col_i)),
sk_dft,
source_xa,
source_xe,
sigma,
bound,
scrach_2,
);
// Switch vec_znx_ct into DFT domain
{
let (mut vec_znx_dft_ct, _) = scrach_2.tmp_vec_znx_dft(module, 2, size);
module.vec_znx_dft(&mut vec_znx_dft_ct, 0, &vec_znx_ct, 0);
module.vec_znx_dft(&mut vec_znx_dft_ct, 1, &vec_znx_ct, 1);
module.vmp_prepare_row(ct, row_j, col_i, &vec_znx_dft_ct);
}
});
vec_znx_pt.data.zero(); // zeroes for next iteration
});
}
impl<C> GGSWCiphertext<C, FFT64> {
pub fn encrypt_sk<P, S>(
&mut self,
module: &Module<FFT64>,
pt: &ScalarZnx<P>,
sk_dft: &SecretKeyFourier<S, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
ScalarZnx<P>: ScalarZnxToRef,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
encrypt_rgsw_sk(
module, self, pt, sk_dft, source_xa, source_xe, sigma, bound, scratch,
)
}
}
impl<C> GetRow<FFT64> for GGSWCiphertext<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>,
{
fn get_row<R>(&self, module: &Module<FFT64>, row_i: usize, col_j: usize, res: &mut GLWECiphertextFourier<R, FFT64>)
where
VecZnxDft<R, FFT64>: VecZnxDftToMut<FFT64>,
{
module.vmp_extract_row(res, self, row_i, col_j);
}
}
impl<C> SetRow<FFT64> for GGSWCiphertext<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
{
fn set_row<R>(&mut self, module: &Module<FFT64>, row_i: usize, col_j: usize, a: &GLWECiphertextFourier<R, FFT64>)
where
VecZnxDft<R, FFT64>: VecZnxDftToRef<FFT64>,
{
module.vmp_prepare_row(self, row_i, col_j, a);
}
}
impl KeySwitchScratchSpace for GGSWCiphertext<Vec<u8>, FFT64> {
fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space(
module, res_size, lhs, rhs,
)
}
}
impl<DataSelf, DataLhs, DataRhs> KeySwitch<DataLhs, DataRhs> for GGSWCiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GGSWCiphertext<DataLhs, FFT64>;
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe(module, self, lhs, scratch);
}
}
impl KeySwitchInplaceScratchSpace for GGSWCiphertext<Vec<u8>, FFT64> {
fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf, DataRhs> KeySwitchInplace<DataRhs> for GGSWCiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe(module, self, rhs, scratch);
}
}
impl ExternalProductScratchSpace for GGSWCiphertext<Vec<u8>, FFT64> {
fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space(
module, res_size, lhs, rhs,
)
}
}
impl<DataSelf, DataLhs, DataRhs> ExternalProduct<DataLhs, DataRhs> for GGSWCiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GGSWCiphertext<DataLhs, FFT64>;
type Rhs = GGSWCiphertext<DataRhs, FFT64>;
fn external_product(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe(module, self, lhs, scratch);
}
}
impl ExternalProductInplaceScratchSpace for GGSWCiphertext<Vec<u8>, FFT64> {
fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf, DataRhs> ExternalProductInplace<DataRhs> for GGSWCiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Rhs = GGSWCiphertext<DataRhs, FFT64>;
fn external_product_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe_inplace(module, self, scratch);
}
}
impl VecGLWEProductScratchSpace for GGSWCiphertext<Vec<u8>, FFT64> {
fn prod_with_glwe_scratch_space(module: &Module<FFT64>, res_size: usize, a_size: usize, rgsw_size: usize) -> usize {
module.bytes_of_vec_znx_dft(2, rgsw_size)
+ ((module.bytes_of_vec_znx_dft(2, a_size) + module.vmp_apply_tmp_bytes(res_size, a_size, a_size, 2, 2, rgsw_size))
| module.vec_znx_big_normalize_tmp_bytes())
}
}
impl<C> VecGLWEProduct for GGSWCiphertext<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64> + ZnxInfos,
{
fn prod_with_glwe<R, A>(
&self,
module: &Module<FFT64>,
res: &mut GLWECiphertext<R>,
a: &GLWECiphertext<A>,
scratch: &mut Scratch,
) where
VecZnx<R>: VecZnxToMut,
VecZnx<A>: VecZnxToRef,
{
let log_base2k: usize = self.basek();
#[cfg(debug_assertions)]
{
assert_eq!(res.basek(), log_base2k);
assert_eq!(a.basek(), log_base2k);
assert_eq!(self.n(), module.n());
assert_eq!(res.n(), module.n());
assert_eq!(a.n(), module.n());
}
let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, 2, self.size()); // Todo optimise
{
let (mut a_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, 2, a.size());
module.vec_znx_dft(&mut a_dft, 0, a, 0);
module.vec_znx_dft(&mut a_dft, 1, a, 1);
module.vmp_apply(&mut res_dft, &a_dft, self, scratch2);
}
let res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft);
module.vec_znx_big_normalize(log_base2k, res, 0, &res_big, 0, scratch1);
module.vec_znx_big_normalize(log_base2k, res, 1, &res_big, 1, scratch1);
}
}

316
core/src/ggsw_ciphertext.rs Normal file
View File

@@ -0,0 +1,316 @@
use base2k::{
Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftScratch, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx,
ScalarZnxDft, ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigOps, VecZnxBigScratch,
VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos,
ZnxZero,
};
use sampling::source::Source;
use crate::{
elem::{GetRow, Infos, SetRow},
gglwe_ciphertext::GGLWECiphertext,
glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::SecretKeyFourier,
keyswitch_key::GLWESwitchingKey,
utils::derive_size,
vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
};
pub struct GGSWCiphertext<C, B: Backend> {
pub data: MatZnxDft<C, B>,
pub log_base2k: usize,
pub log_k: usize,
}
impl<B: Backend> GGSWCiphertext<Vec<u8>, B> {
pub fn new(module: &Module<B>, log_base2k: usize, log_k: usize, rows: usize, rank: usize) -> Self {
Self {
data: module.new_mat_znx_dft(rows, rank + 1, rank + 1, derive_size(log_base2k, log_k)),
log_base2k: log_base2k,
log_k: log_k,
}
}
}
impl<T, B: Backend> Infos for GGSWCiphertext<T, B> {
type Inner = MatZnxDft<T, B>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.log_base2k
}
fn k(&self) -> usize {
self.log_k
}
}
impl<T, B: Backend> GGSWCiphertext<T, B> {
pub fn rank(&self) -> usize {
self.data.cols_out() - 1
}
}
impl<C, B: Backend> MatZnxDftToMut<B> for GGSWCiphertext<C, B>
where
MatZnxDft<C, B>: MatZnxDftToMut<B>,
{
fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> {
self.data.to_mut()
}
}
impl<C, B: Backend> MatZnxDftToRef<B> for GGSWCiphertext<C, B>
where
MatZnxDft<C, B>: MatZnxDftToRef<B>,
{
fn to_ref(&self) -> MatZnxDft<&[u8], B> {
self.data.to_ref()
}
}
impl GGSWCiphertext<Vec<u8>, FFT64> {
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
GLWECiphertext::encrypt_sk_scratch_space(module, rank, size)
+ module.bytes_of_vec_znx(rank + 1, size)
+ module.bytes_of_vec_znx(1, size)
+ module.bytes_of_vec_znx_dft(rank + 1, size)
}
pub fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space(
module, res_size, lhs, rhs,
)
}
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
pub fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space(
module, res_size, lhs, rhs,
)
}
pub fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf> GGSWCiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
{
pub fn encrypt_sk<DataPt, DataSk>(
&mut self,
module: &Module<FFT64>,
pt: &ScalarZnx<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
ScalarZnx<DataPt>: ScalarZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(self.n(), module.n());
assert_eq!(pt.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
}
let size: usize = self.size();
let log_base2k: usize = self.basek();
let k: usize = self.k();
let cols: usize = self.rank() + 1;
let (tmp_znx_pt, scratch_1) = scratch.tmp_vec_znx(module, 1, size);
let (tmp_znx_ct, scrach_2) = scratch_1.tmp_vec_znx(module, cols, size);
let mut vec_znx_pt: GLWEPlaintext<&mut [u8]> = GLWEPlaintext {
data: tmp_znx_pt,
basek: log_base2k,
k: k,
};
let mut vec_znx_ct: GLWECiphertext<&mut [u8]> = GLWECiphertext {
data: tmp_znx_ct,
basek: log_base2k,
k,
};
(0..self.rows()).for_each(|row_j| {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
module.vec_znx_add_scalar_inplace(&mut vec_znx_pt, 0, row_j, pt, 0);
module.vec_znx_normalize_inplace(log_base2k, &mut vec_znx_pt, 0, scrach_2);
(0..cols).for_each(|col_i| {
// rlwe encrypt of vec_znx_pt into vec_znx_ct
vec_znx_ct.encrypt_sk_private(
module,
Some((&vec_znx_pt, col_i)),
sk_dft,
source_xa,
source_xe,
sigma,
bound,
scrach_2,
);
// Switch vec_znx_ct into DFT domain
{
let (mut vec_znx_dft_ct, _) = scrach_2.tmp_vec_znx_dft(module, cols, size);
(0..cols).for_each(|i| {
module.vec_znx_dft(&mut vec_znx_dft_ct, i, &vec_znx_ct, i);
});
module.vmp_prepare_row(self, row_j, col_i, &vec_znx_dft_ct);
}
});
vec_znx_pt.data.zero(); // zeroes for next iteration
});
}
pub fn keyswitch<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GGSWCiphertext<DataLhs, FFT64>,
rhs: &GLWESwitchingKey<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.0.prod_with_vec_glwe(module, self, lhs, scratch);
}
pub fn keyswitch_inplace<DataRhs>(
&mut self,
module: &Module<FFT64>,
rhs: &GLWESwitchingKey<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.0.prod_with_vec_glwe_inplace(module, self, scratch);
}
pub fn external_product<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GGSWCiphertext<DataLhs, FFT64>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.prod_with_vec_glwe(module, self, lhs, scratch);
}
pub fn external_product_inplace<DataRhs>(
&mut self,
module: &Module<FFT64>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.prod_with_vec_glwe_inplace(module, self, scratch);
}
}
impl<DataSelf> GetRow<FFT64> for GGSWCiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToRef<FFT64>,
{
fn get_row<DataRes>(
&self,
module: &Module<FFT64>,
row_i: usize,
col_j: usize,
res: &mut GLWECiphertextFourier<DataRes, FFT64>,
) where
VecZnxDft<DataRes, FFT64>: VecZnxDftToMut<FFT64>,
{
module.vmp_extract_row(res, self, row_i, col_j);
}
}
impl<DataSelf> SetRow<FFT64> for GGSWCiphertext<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64>,
{
fn set_row<DataRes>(&mut self, module: &Module<FFT64>, row_i: usize, col_j: usize, a: &GLWECiphertextFourier<DataRes, FFT64>)
where
VecZnxDft<DataRes, FFT64>: VecZnxDftToRef<FFT64>,
{
module.vmp_prepare_row(self, row_i, col_j, a);
}
}
impl VecGLWEProductScratchSpace for GGSWCiphertext<Vec<u8>, FFT64> {
fn prod_with_glwe_scratch_space(module: &Module<FFT64>, res_size: usize, a_size: usize, rgsw_size: usize) -> usize {
module.bytes_of_vec_znx_dft(2, rgsw_size)
+ ((module.bytes_of_vec_znx_dft(2, a_size) + module.vmp_apply_tmp_bytes(res_size, a_size, a_size, 2, 2, rgsw_size))
| module.vec_znx_big_normalize_tmp_bytes())
}
}
impl<C> VecGLWEProduct for GGSWCiphertext<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64> + ZnxInfos,
{
fn prod_with_glwe<R, A>(
&self,
module: &Module<FFT64>,
res: &mut GLWECiphertext<R>,
a: &GLWECiphertext<A>,
scratch: &mut Scratch,
) where
VecZnx<R>: VecZnxToMut,
VecZnx<A>: VecZnxToRef,
{
let log_base2k: usize = self.basek();
#[cfg(debug_assertions)]
{
assert_eq!(res.basek(), log_base2k);
assert_eq!(a.basek(), log_base2k);
assert_eq!(self.n(), module.n());
assert_eq!(res.n(), module.n());
assert_eq!(a.n(), module.n());
}
let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, 2, self.size()); // Todo optimise
{
let (mut a_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, 2, a.size());
module.vec_znx_dft(&mut a_dft, 0, a, 0);
module.vec_znx_dft(&mut a_dft, 1, a, 1);
module.vmp_apply(&mut res_dft, &a_dft, self, scratch2);
}
let res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft);
module.vec_znx_big_normalize(log_base2k, res, 0, &res_big, 0, scratch1);
module.vec_znx_big_normalize(log_base2k, res, 1, &res_big, 1, scratch1);
}
}

View File

@@ -1,845 +0,0 @@
use base2k::{
AddNormal, Backend, FFT64, FillUniform, MatZnxDft, MatZnxDftToRef, Module, ScalarZnxAlloc, ScalarZnxDft, ScalarZnxDftAlloc,
ScalarZnxDftOps, ScalarZnxDftToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft,
VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos,
};
use sampling::source::Source;
use crate::{
elem::Infos,
encryption::{EncryptSk, EncryptSkScratchSpace, EncryptZeroSkScratchSpace},
external_product::{
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace,
},
ggsw::GGSWCiphertext,
keys::{PublicKey, SecretDistribution, SecretKeyFourier},
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace},
keyswitch_key::GLWEKeySwitchKey,
utils::derive_size,
vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
};
pub struct GLWECiphertext<C> {
pub data: VecZnx<C>,
pub log_base2k: usize,
pub log_k: usize,
}
impl GLWECiphertext<Vec<u8>> {
pub fn new<B: Backend>(module: &Module<B>, log_base2k: usize, log_k: usize) -> Self {
Self {
data: module.new_vec_znx(2, derive_size(log_base2k, log_k)),
log_base2k: log_base2k,
log_k: log_k,
}
}
}
impl<T> Infos for GLWECiphertext<T> {
type Inner = VecZnx<T>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.log_base2k
}
fn k(&self) -> usize {
self.log_k
}
}
impl<C> VecZnxToMut for GLWECiphertext<C>
where
VecZnx<C>: VecZnxToMut,
{
fn to_mut(&mut self) -> VecZnx<&mut [u8]> {
self.data.to_mut()
}
}
impl<C> VecZnxToRef for GLWECiphertext<C>
where
VecZnx<C>: VecZnxToRef,
{
fn to_ref(&self) -> VecZnx<&[u8]> {
self.data.to_ref()
}
}
impl<C> GLWECiphertext<C>
where
VecZnx<C>: VecZnxToRef,
{
#[allow(dead_code)]
pub(crate) fn dft<R>(&self, module: &Module<FFT64>, res: &mut GLWECiphertextFourier<R, FFT64>)
where
VecZnxDft<R, FFT64>: VecZnxDftToMut<FFT64> + ZnxInfos,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), 2);
assert_eq!(res.rank(), 2);
assert_eq!(self.basek(), res.basek())
}
module.vec_znx_dft(res, 0, self, 0);
module.vec_znx_dft(res, 1, self, 1);
}
}
impl KeySwitchScratchSpace for GLWECiphertext<Vec<u8>> {
fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
}
impl<DataSelf, DataLhs, DataRhs> KeySwitch<DataLhs, DataRhs> for GLWECiphertext<DataSelf>
where
VecZnx<DataSelf>: VecZnxToMut + VecZnxToRef,
VecZnx<DataLhs>: VecZnxToRef,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GLWECiphertext<DataLhs>;
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe(module, self, lhs, scratch);
}
}
impl KeySwitchInplaceScratchSpace for GLWECiphertext<Vec<u8>> {
fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf, DataRhs> KeySwitchInplace<DataRhs> for GLWECiphertext<DataSelf>
where
VecZnx<DataSelf>: VecZnxToMut + VecZnxToRef,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe_inplace(module, self, scratch);
}
}
impl ExternalProductScratchSpace for GLWECiphertext<Vec<u8>> {
fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
}
impl<DataSelf, DataLhs, DataRhs> ExternalProduct<DataLhs, DataRhs> for GLWECiphertext<DataSelf>
where
VecZnx<DataSelf>: VecZnxToMut + VecZnxToRef,
VecZnx<DataLhs>: VecZnxToRef,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GLWECiphertext<DataLhs>;
type Rhs = GGSWCiphertext<DataRhs, FFT64>;
fn external_product(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe(module, self, lhs, scratch);
}
}
impl ExternalProductInplaceScratchSpace for GLWECiphertext<Vec<u8>> {
fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf, DataRhs> ExternalProductInplace<DataRhs> for GLWECiphertext<DataSelf>
where
VecZnx<DataSelf>: VecZnxToMut + VecZnxToRef,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64> + ZnxInfos,
{
type Rhs = GGSWCiphertext<DataRhs, FFT64>;
fn external_product_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe_inplace(module, self, scratch);
}
}
impl GLWECiphertext<Vec<u8>> {
pub fn encrypt_pk_scratch_space<B: Backend>(module: &Module<B>, pk_size: usize) -> usize {
((module.bytes_of_vec_znx_dft(1, pk_size) + module.bytes_of_vec_znx_big(1, pk_size)) | module.bytes_of_scalar_znx(1))
+ module.bytes_of_scalar_znx_dft(1)
+ module.vec_znx_big_normalize_tmp_bytes()
}
pub fn decrypt_scratch_space<B: Backend>(module: &Module<B>, size: usize) -> usize {
(module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_big(1, size)
}
}
impl EncryptSkScratchSpace for GLWECiphertext<Vec<u8>> {
fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, size: usize) -> usize {
(module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_big(1, size)
}
}
impl<DataCt, DataPt, DataSk> EncryptSk<DataCt, DataPt, DataSk, FFT64> for GLWECiphertext<DataCt>
where
VecZnx<DataCt>: VecZnxToMut + VecZnxToRef,
VecZnx<DataPt>: VecZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
type Ciphertext = GLWECiphertext<DataCt>;
type Plaintext = GLWEPlaintext<DataPt>;
type SecretKey = SecretKeyFourier<DataSk, FFT64>;
fn encrypt_sk(
&self,
module: &Module<FFT64>,
ct: &mut Self::Ciphertext,
pt: &Self::Plaintext,
sk: &Self::SecretKey,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) {
encrypt_glwe_sk(
module,
ct,
Some((pt, 0)),
sk,
source_xa,
source_xe,
sigma,
bound,
scratch,
);
}
}
pub(crate) fn encrypt_glwe_sk<DataCt, DataPt, DataSk>(
module: &Module<FFT64>,
ct: &mut GLWECiphertext<DataCt>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<DataCt>: VecZnxToMut + VecZnxToRef,
VecZnx<DataPt>: VecZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let log_base2k: usize = ct.basek();
let log_k: usize = ct.k();
let size: usize = ct.size();
// c1 = a
ct.data.fill_uniform(log_base2k, 1, size, source_xa);
let (mut c0_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, size);
{
let (mut c0_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, size);
module.vec_znx_dft(&mut c0_dft, 0, ct, 1);
// c0_dft = DFT(a) * DFT(s)
module.svp_apply_inplace(&mut c0_dft, 0, sk_dft, 0);
// c0_big = IDFT(c0_dft)
module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut c0_dft, 0);
}
// c0_big = m - c0_big
if let Some((pt, col)) = pt {
match col {
0 => module.vec_znx_big_sub_small_b_inplace(&mut c0_big, 0, pt, 0),
1 => {
module.vec_znx_big_negate_inplace(&mut c0_big, 0);
module.vec_znx_add_inplace(ct, 1, pt, 0);
module.vec_znx_normalize_inplace(log_base2k, ct, 1, scratch_1);
}
_ => panic!("invalid target column: {}", col),
}
} else {
module.vec_znx_big_negate_inplace(&mut c0_big, 0);
}
// c0_big += e
c0_big.add_normal(log_base2k, 0, log_k, source_xe, sigma, bound);
// c0 = norm(c0_big = -as + m + e)
module.vec_znx_big_normalize(log_base2k, ct, 0, &c0_big, 0, scratch_1);
}
pub fn decrypt_glwe<P, C, S>(
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<P>,
ct: &GLWECiphertext<C>,
sk_dft: &SecretKeyFourier<S, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<P>: VecZnxToMut + VecZnxToRef,
VecZnx<C>: VecZnxToRef,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let (mut c0_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, ct.size()); // TODO optimize size when pt << ct
{
let (mut c0_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, ct.size()); // TODO optimize size when pt << ct
module.vec_znx_dft(&mut c0_dft, 0, ct, 1);
// c0_dft = DFT(a) * DFT(s)
module.svp_apply_inplace(&mut c0_dft, 0, sk_dft, 0);
// c0_big = IDFT(c0_dft)
module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut c0_dft, 0);
}
// c0_big = (a * s) + (-a * s + m + e) = BIG(m + e)
module.vec_znx_big_add_small_inplace(&mut c0_big, 0, ct, 0);
// pt = norm(BIG(m + e))
module.vec_znx_big_normalize(ct.basek(), pt, 0, &mut c0_big, 0, scratch_1);
pt.log_base2k = ct.basek();
pt.log_k = pt.k().min(ct.k());
}
impl<MUT> GLWECiphertext<MUT>
where
VecZnx<MUT>: VecZnxToMut + VecZnxToRef,
{
pub fn encrypt_sk<R0, R1>(
&mut self,
module: &Module<FFT64>,
pt: &GLWEPlaintext<R0>,
sk_dft: &SecretKeyFourier<R1, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<R0>: VecZnxToRef,
ScalarZnxDft<R1, FFT64>: ScalarZnxDftToRef<FFT64>,
{
encrypt_glwe_sk(
module,
self,
Some((pt, 0)),
sk_dft,
source_xa,
source_xe,
sigma,
bound,
scratch,
)
}
pub fn encrypt_zero_sk<R>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<R, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
ScalarZnxDft<R, FFT64>: ScalarZnxDftToRef<FFT64>,
{
encrypt_glwe_sk::<MUT, _, R>(
module, self, None, sk_dft, source_xa, source_xe, sigma, bound, scratch,
)
}
pub fn encrypt_pk<R0, R1>(
&mut self,
module: &Module<FFT64>,
pt: &GLWEPlaintext<R0>,
pk: &PublicKey<R1, FFT64>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<R0>: VecZnxToRef,
VecZnxDft<R1, FFT64>: VecZnxDftToRef<FFT64>,
{
encrypt_glwe_pk(
module,
self,
Some(pt),
pk,
source_xu,
source_xe,
sigma,
bound,
scratch,
)
}
pub fn encrypt_zero_pk<R>(
&mut self,
module: &Module<FFT64>,
pk: &PublicKey<R, FFT64>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnxDft<R, FFT64>: VecZnxDftToRef<FFT64>,
{
encrypt_glwe_pk::<MUT, _, R>(
module, self, None, pk, source_xu, source_xe, sigma, bound, scratch,
)
}
}
impl<REF> GLWECiphertext<REF>
where
VecZnx<REF>: VecZnxToRef,
{
pub fn decrypt<MUT, REF1>(
&self,
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<MUT>,
sk_dft: &SecretKeyFourier<REF1, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<MUT>: VecZnxToMut + VecZnxToRef,
ScalarZnxDft<REF1, FFT64>: ScalarZnxDftToRef<FFT64>,
{
decrypt_glwe(module, pt, self, sk_dft, scratch);
}
}
pub(crate) fn encrypt_glwe_pk<C, P, S>(
module: &Module<FFT64>,
ct: &mut GLWECiphertext<C>,
pt: Option<&GLWEPlaintext<P>>,
pk: &PublicKey<S, FFT64>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<C>: VecZnxToMut + VecZnxToRef,
VecZnx<P>: VecZnxToRef,
VecZnxDft<S, FFT64>: VecZnxDftToRef<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(ct.basek(), pk.basek());
assert_eq!(ct.n(), module.n());
assert_eq!(pk.n(), module.n());
if let Some(pt) = pt {
assert_eq!(pt.basek(), pk.basek());
assert_eq!(pt.n(), module.n());
}
}
let log_base2k: usize = pk.basek();
let size_pk: usize = pk.size();
// Generates u according to the underlying secret distribution.
let (mut u_dft, scratch_1) = scratch.tmp_scalar_znx_dft(module, 1);
{
let (mut u, _) = scratch_1.tmp_scalar_znx(module, 1);
match pk.dist {
SecretDistribution::NONE => panic!(
"invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through Self::generate"
),
SecretDistribution::TernaryFixed(hw) => u.fill_ternary_hw(0, hw, source_xu),
SecretDistribution::TernaryProb(prob) => u.fill_ternary_prob(0, prob, source_xu),
SecretDistribution::ZERO => {}
}
module.svp_prepare(&mut u_dft, 0, &u, 0);
}
let (mut tmp_big, scratch_2) = scratch_1.tmp_vec_znx_big(module, 1, size_pk); // TODO optimize size (e.g. when encrypting at low homomorphic capacity)
let (mut tmp_dft, scratch_3) = scratch_2.tmp_vec_znx_dft(module, 1, size_pk); // TODO optimize size (e.g. when encrypting at low homomorphic capacity)
// ct[0] = pk[0] * u + m + e0
module.svp_apply(&mut tmp_dft, 0, &u_dft, 0, pk, 0);
module.vec_znx_idft_tmp_a(&mut tmp_big, 0, &mut tmp_dft, 0);
tmp_big.add_normal(log_base2k, 0, pk.k(), source_xe, sigma, bound);
if let Some(pt) = pt {
module.vec_znx_big_add_small_inplace(&mut tmp_big, 0, pt, 0);
}
module.vec_znx_big_normalize(log_base2k, ct, 0, &tmp_big, 0, scratch_3);
// ct[1] = pk[1] * u + e1
module.svp_apply(&mut tmp_dft, 0, &u_dft, 0, pk, 1);
module.vec_znx_idft_tmp_a(&mut tmp_big, 0, &mut tmp_dft, 0);
tmp_big.add_normal(log_base2k, 0, pk.k(), source_xe, sigma, bound);
module.vec_znx_big_normalize(log_base2k, ct, 1, &tmp_big, 0, scratch_3);
}
pub struct GLWEPlaintext<C> {
pub data: VecZnx<C>,
pub log_base2k: usize,
pub log_k: usize,
}
impl<T> Infos for GLWEPlaintext<T> {
type Inner = VecZnx<T>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.log_base2k
}
fn k(&self) -> usize {
self.log_k
}
}
impl<C> VecZnxToMut for GLWEPlaintext<C>
where
VecZnx<C>: VecZnxToMut,
{
fn to_mut(&mut self) -> VecZnx<&mut [u8]> {
self.data.to_mut()
}
}
impl<C> VecZnxToRef for GLWEPlaintext<C>
where
VecZnx<C>: VecZnxToRef,
{
fn to_ref(&self) -> VecZnx<&[u8]> {
self.data.to_ref()
}
}
impl GLWEPlaintext<Vec<u8>> {
pub fn new<B: Backend>(module: &Module<B>, log_base2k: usize, log_k: usize) -> Self {
Self {
data: module.new_vec_znx(1, derive_size(log_base2k, log_k)),
log_base2k: log_base2k,
log_k: log_k,
}
}
}
pub struct GLWECiphertextFourier<C, B: Backend> {
pub data: VecZnxDft<C, B>,
pub log_base2k: usize,
pub log_k: usize,
}
impl<B: Backend> GLWECiphertextFourier<Vec<u8>, B> {
pub fn new(module: &Module<B>, log_base2k: usize, log_k: usize) -> Self {
Self {
data: module.new_vec_znx_dft(2, derive_size(log_base2k, log_k)),
log_base2k: log_base2k,
log_k: log_k,
}
}
}
impl<T, B: Backend> Infos for GLWECiphertextFourier<T, B> {
type Inner = VecZnxDft<T, B>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.log_base2k
}
fn k(&self) -> usize {
self.log_k
}
}
impl<C, B: Backend> VecZnxDftToMut<B> for GLWECiphertextFourier<C, B>
where
VecZnxDft<C, B>: VecZnxDftToMut<B>,
{
fn to_mut(&mut self) -> VecZnxDft<&mut [u8], B> {
self.data.to_mut()
}
}
impl<C, B: Backend> VecZnxDftToRef<B> for GLWECiphertextFourier<C, B>
where
VecZnxDft<C, B>: VecZnxDftToRef<B>,
{
fn to_ref(&self) -> VecZnxDft<&[u8], B> {
self.data.to_ref()
}
}
impl<C> GLWECiphertextFourier<C, FFT64>
where
GLWECiphertextFourier<C, FFT64>: VecZnxDftToRef<FFT64>,
{
#[allow(dead_code)]
pub(crate) fn idft_scratch_space(module: &Module<FFT64>, size: usize) -> usize {
module.bytes_of_vec_znx(2, size) + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_tmp_bytes())
}
pub(crate) fn idft<R>(&self, module: &Module<FFT64>, res: &mut GLWECiphertext<R>, scratch: &mut Scratch)
where
GLWECiphertext<R>: VecZnxToMut,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), 2);
assert_eq!(res.rank(), 2);
assert_eq!(self.basek(), res.basek())
}
let min_size: usize = self.size().min(res.size());
let (mut res_big, scratch1) = scratch.tmp_vec_znx_big(module, 2, min_size);
module.vec_znx_idft(&mut res_big, 0, self, 0, scratch1);
module.vec_znx_idft(&mut res_big, 1, self, 1, scratch1);
module.vec_znx_big_normalize(self.basek(), res, 0, &res_big, 0, scratch1);
module.vec_znx_big_normalize(self.basek(), res, 1, &res_big, 1, scratch1);
}
}
pub(crate) fn encrypt_zero_glwe_dft_sk<C, S>(
module: &Module<FFT64>,
ct: &mut GLWECiphertextFourier<C, FFT64>,
sk: &SecretKeyFourier<S, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnxDft<C, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let log_base2k: usize = ct.basek();
let log_k: usize = ct.k();
let size: usize = ct.size();
#[cfg(debug_assertions)]
{
match sk.dist {
SecretDistribution::NONE => panic!("invalid sk.dist = SecretDistribution::NONE"),
_ => {}
}
assert_eq!(ct.rank(), 2);
}
// ct[1] = DFT(a)
{
let (mut tmp_znx, _) = scratch.tmp_vec_znx(module, 1, size);
tmp_znx.fill_uniform(log_base2k, 0, size, source_xa);
module.vec_znx_dft(ct, 1, &tmp_znx, 0);
}
let (mut c0_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, size);
{
let (mut tmp_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, size);
// c0_dft = ct[1] * DFT(s)
module.svp_apply(&mut tmp_dft, 0, sk, 0, ct, 1);
// c0_big = IDFT(c0_dft)
module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut tmp_dft, 0);
}
// c0_big += e
c0_big.add_normal(log_base2k, 0, log_k, source_xe, sigma, bound);
// c0 = norm(c0_big = -as - e), NOTE: e is centered at 0.
let (mut tmp_znx, scratch_2) = scratch_1.tmp_vec_znx(module, 1, size);
module.vec_znx_big_normalize(log_base2k, &mut tmp_znx, 0, &c0_big, 0, scratch_2);
module.vec_znx_negate_inplace(&mut tmp_znx, 0);
// ct[0] = DFT(-as + e)
module.vec_znx_dft(ct, 0, &tmp_znx, 0);
}
impl GLWECiphertextFourier<Vec<u8>, FFT64> {
pub fn encrypt_zero_sk_scratch_space(module: &Module<FFT64>, size: usize) -> usize {
(module.bytes_of_vec_znx(1, size) | module.bytes_of_vec_znx_dft(1, size))
+ module.bytes_of_vec_znx_big(1, size)
+ module.bytes_of_vec_znx(1, size)
+ module.vec_znx_big_normalize_tmp_bytes()
}
pub fn decrypt_scratch_space(module: &Module<FFT64>, size: usize) -> usize {
(module.vec_znx_big_normalize_tmp_bytes()
| module.bytes_of_vec_znx_dft(1, size)
| (module.bytes_of_vec_znx_big(1, size) + module.vec_znx_idft_tmp_bytes()))
+ module.bytes_of_vec_znx_big(1, size)
}
}
pub fn decrypt_rlwe_dft<P, C, S>(
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<P>,
ct: &GLWECiphertextFourier<C, FFT64>,
sk: &SecretKeyFourier<S, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<P>: VecZnxToMut + VecZnxToRef,
VecZnxDft<C, FFT64>: VecZnxDftToRef<FFT64>,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let (mut c0_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, ct.size()); // TODO optimize size when pt << ct
{
let (mut c0_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, ct.size()); // TODO optimize size when pt << ct
// c0_dft = DFT(a) * DFT(s)
module.svp_apply(&mut c0_dft, 0, sk, 0, ct, 1);
// c0_big = IDFT(c0_dft)
module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut c0_dft, 0);
}
{
let (mut c1_big, scratch_2) = scratch_1.tmp_vec_znx_big(module, 1, ct.size());
// c0_big = (a * s) + (-a * s + m + e) = BIG(m + e)
module.vec_znx_idft(&mut c1_big, 0, ct, 0, scratch_2);
module.vec_znx_big_add_inplace(&mut c0_big, 0, &c1_big, 0);
}
// pt = norm(BIG(m + e))
module.vec_znx_big_normalize(ct.basek(), pt, 0, &mut c0_big, 0, scratch_1);
pt.log_base2k = ct.basek();
pt.log_k = pt.k().min(ct.k());
}
impl<C> GLWECiphertextFourier<C, FFT64>
where
VecZnxDft<C, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
{
pub(crate) fn encrypt_zero_sk<S>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<S, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
encrypt_zero_glwe_dft_sk(
module, self, sk_dft, source_xa, source_xe, sigma, bound, scratch,
)
}
pub fn decrypt<P, S>(
&self,
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<P>,
sk_dft: &SecretKeyFourier<S, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<P>: VecZnxToMut + VecZnxToRef,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
decrypt_rlwe_dft(module, pt, self, sk_dft, scratch);
}
}
impl KeySwitchScratchSpace for GLWECiphertextFourier<Vec<u8>, FFT64> {
fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
}
impl<DataSelf, DataLhs, DataRhs> KeySwitch<DataLhs, DataRhs> for GLWECiphertextFourier<DataSelf, FFT64>
where
VecZnxDft<DataSelf, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
VecZnxDft<DataLhs, FFT64>: VecZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GLWECiphertextFourier<DataLhs, FFT64>;
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe_fourier(module, self, lhs, scratch);
}
}
impl KeySwitchInplaceScratchSpace for GLWECiphertextFourier<Vec<u8>, FFT64> {
fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf, DataRhs> KeySwitchInplace<DataRhs> for GLWECiphertextFourier<DataSelf, FFT64>
where
VecZnxDft<DataSelf, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe_fourier_inplace(module, self, scratch);
}
}
impl ExternalProductScratchSpace for GLWECiphertextFourier<Vec<u8>, FFT64> {
fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
}
impl<DataSelf, DataLhs, DataRhs> ExternalProduct<DataLhs, DataRhs> for GLWECiphertextFourier<DataSelf, FFT64>
where
VecZnxDft<DataSelf, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
VecZnxDft<DataLhs, FFT64>: VecZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GLWECiphertextFourier<DataLhs, FFT64>;
type Rhs = GGSWCiphertext<DataRhs, FFT64>;
fn external_product(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe_fourier(module, self, lhs, scratch);
}
}
impl ExternalProductInplaceScratchSpace for GLWECiphertextFourier<Vec<u8>, FFT64> {
fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf, DataRhs> ExternalProductInplace<DataRhs> for GLWECiphertextFourier<DataSelf, FFT64>
where
VecZnxDft<DataSelf, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64> + ZnxInfos,
{
type Rhs = GGSWCiphertext<DataRhs, FFT64>;
fn external_product_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_glwe_fourier_inplace(module, self, scratch);
}
}

460
core/src/glwe_ciphertext.rs Normal file
View File

@@ -0,0 +1,460 @@
use base2k::{
AddNormal, Backend, FFT64, FillUniform, MatZnxDft, MatZnxDftToRef, Module, ScalarZnxAlloc, ScalarZnxDft, ScalarZnxDftAlloc,
ScalarZnxDftOps, ScalarZnxDftToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch,
VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos,
ZnxZero,
};
use sampling::source::Source;
use crate::{
elem::Infos,
gglwe_ciphertext::GGLWECiphertext,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{GLWEPublicKey, SecretDistribution, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
utils::derive_size,
vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
};
pub struct GLWECiphertext<C> {
pub data: VecZnx<C>,
pub basek: usize,
pub k: usize,
}
impl GLWECiphertext<Vec<u8>> {
pub fn new<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> Self {
Self {
data: module.new_vec_znx(rank + 1, derive_size(basek, k)),
basek,
k,
}
}
}
impl<T> Infos for GLWECiphertext<T> {
type Inner = VecZnx<T>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.basek
}
fn k(&self) -> usize {
self.k
}
}
impl<T> GLWECiphertext<T> {
pub fn rank(&self) -> usize {
self.cols() - 1
}
}
impl<C> VecZnxToMut for GLWECiphertext<C>
where
VecZnx<C>: VecZnxToMut,
{
fn to_mut(&mut self) -> VecZnx<&mut [u8]> {
self.data.to_mut()
}
}
impl<C> VecZnxToRef for GLWECiphertext<C>
where
VecZnx<C>: VecZnxToRef,
{
fn to_ref(&self) -> VecZnx<&[u8]> {
self.data.to_ref()
}
}
impl<C> GLWECiphertext<C>
where
VecZnx<C>: VecZnxToRef,
{
#[allow(dead_code)]
pub(crate) fn dft<R>(&self, module: &Module<FFT64>, res: &mut GLWECiphertextFourier<R, FFT64>)
where
VecZnxDft<R, FFT64>: VecZnxDftToMut<FFT64> + ZnxInfos,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), res.rank());
assert_eq!(self.basek(), res.basek())
}
(0..self.rank() + 1).for_each(|i| {
module.vec_znx_dft(res, i, self, i);
})
}
}
impl GLWECiphertext<Vec<u8>> {
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, _rank: usize, ct_size: usize) -> usize {
module.vec_znx_big_normalize_tmp_bytes()
+ module.bytes_of_vec_znx_dft(1, ct_size)
+ module.bytes_of_vec_znx_big(1, ct_size)
}
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, _rank: usize, pk_size: usize) -> usize {
((module.bytes_of_vec_znx_dft(1, pk_size) + module.bytes_of_vec_znx_big(1, pk_size)) | module.bytes_of_scalar_znx(1))
+ module.bytes_of_scalar_znx_dft(1)
+ module.vec_znx_big_normalize_tmp_bytes()
}
pub fn decrypt_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
(module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, ct_size))
+ module.bytes_of_vec_znx_big(1, ct_size)
}
pub fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
pub fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
pub fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf> GLWECiphertext<DataSelf>
where
VecZnx<DataSelf>: VecZnxToMut + VecZnxToRef,
{
pub fn encrypt_sk<DataPt, DataSk>(
&mut self,
module: &Module<FFT64>,
pt: &GLWEPlaintext<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<DataPt>: VecZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
self.encrypt_sk_private(
module,
Some((pt, 0)),
sk_dft,
source_xa,
source_xe,
sigma,
bound,
scratch,
);
}
pub fn encrypt_zero_sk<DataSk>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
self.encrypt_sk_private(
module, None, sk_dft, source_xa, source_xe, sigma, bound, scratch,
);
}
pub fn encrypt_pk<DataPt, DataPk>(
&mut self,
module: &Module<FFT64>,
pt: &GLWEPlaintext<DataPt>,
pk: &GLWEPublicKey<DataPk, FFT64>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<DataPt>: VecZnxToRef,
VecZnxDft<DataPk, FFT64>: VecZnxDftToRef<FFT64>,
{
self.encrypt_pk_private(
module,
Some((pt, 0)),
pk,
source_xu,
source_xe,
sigma,
bound,
scratch,
);
}
pub fn encrypt_zero_pk<DataPk>(
&mut self,
module: &Module<FFT64>,
pk: &GLWEPublicKey<DataPk, FFT64>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnxDft<DataPk, FFT64>: VecZnxDftToRef<FFT64>,
{
self.encrypt_pk_private(
module, None, pk, source_xu, source_xe, sigma, bound, scratch,
);
}
pub fn keyswitch<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GLWESwitchingKey<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<DataLhs>: VecZnxToRef,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.0.prod_with_glwe(module, self, lhs, scratch);
}
pub fn keyswitch_inplace<DataRhs>(
&mut self,
module: &Module<FFT64>,
rhs: &GLWESwitchingKey<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.0.prod_with_glwe_inplace(module, self, scratch);
}
pub fn external_product<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<DataLhs>: VecZnxToRef,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.prod_with_glwe(module, self, lhs, scratch);
}
pub fn external_product_inplace<DataRhs>(
&mut self,
module: &Module<FFT64>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.prod_with_glwe_inplace(module, self, scratch);
}
pub(crate) fn encrypt_sk_private<DataPt, DataSk>(
&mut self,
module: &Module<FFT64>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<DataPt>: VecZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(sk_dft.n(), module.n());
assert_eq!(self.n(), module.n());
if let Some((pt, col)) = pt {
assert_eq!(pt.n(), module.n());
assert!(col < self.rank() + 1);
}
}
let log_base2k: usize = self.basek();
let log_k: usize = self.k();
let size: usize = self.size();
let cols: usize = self.rank() + 1;
let (mut c0_big, scratch_1) = scratch.tmp_vec_znx(module, 1, size);
c0_big.zero();
{
// c[i] = uniform
// c[0] -= c[i] * s[i],
(1..cols).for_each(|i| {
let (mut ci_dft, scratch_2) = scratch_1.tmp_vec_znx_dft(module, 1, size);
// c[i] = uniform
self.data.fill_uniform(log_base2k, i, size, source_xa);
// c[i] = norm(IDFT(DFT(c[i]) * DFT(s[i])))
module.vec_znx_dft(&mut ci_dft, 0, self, i);
module.svp_apply_inplace(&mut ci_dft, 0, sk_dft, 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
module.vec_znx_big_normalize(log_base2k, self, 0, &ci_big, 0, scratch_2);
// c0_tmp = -c[i] * s[i] (use c[0] as buffer)
module.vec_znx_sub_ab_inplace(&mut c0_big, 0, self, 0);
// c[i] += m if col = i
if let Some((pt, col)) = pt {
if i == col {
module.vec_znx_add_inplace(self, i, pt, 0);
module.vec_znx_normalize_inplace(log_base2k, self, i, scratch_2);
}
}
});
}
// c[0] += e
c0_big.add_normal(log_base2k, 0, log_k, source_xe, sigma, bound);
// c[0] += m if col = 0
if let Some((pt, col)) = pt {
if col == 0 {
module.vec_znx_add_inplace(&mut c0_big, 0, pt, 0);
}
}
// c[0] = norm(c[0])
module.vec_znx_normalize(log_base2k, self, 0, &c0_big, 0, scratch_1);
}
pub(crate) fn encrypt_pk_private<DataPt, DataPk>(
&mut self,
module: &Module<FFT64>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
pk: &GLWEPublicKey<DataPk, FFT64>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
VecZnx<DataPt>: VecZnxToRef,
VecZnxDft<DataPk, FFT64>: VecZnxDftToRef<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.basek(), pk.basek());
assert_eq!(self.n(), module.n());
assert_eq!(pk.n(), module.n());
assert_eq!(self.rank(), pk.rank());
if let Some((pt, _)) = pt {
assert_eq!(pt.basek(), pk.basek());
assert_eq!(pt.n(), module.n());
}
}
let log_base2k: usize = pk.basek();
let size_pk: usize = pk.size();
let cols: usize = self.rank() + 1;
// Generates u according to the underlying secret distribution.
let (mut u_dft, scratch_1) = scratch.tmp_scalar_znx_dft(module, 1);
{
let (mut u, _) = scratch_1.tmp_scalar_znx(module, 1);
match pk.dist {
SecretDistribution::NONE => panic!(
"invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \
Self::generate"
),
SecretDistribution::TernaryFixed(hw) => u.fill_ternary_hw(0, hw, source_xu),
SecretDistribution::TernaryProb(prob) => u.fill_ternary_prob(0, prob, source_xu),
SecretDistribution::ZERO => {}
}
module.svp_prepare(&mut u_dft, 0, &u, 0);
}
// ct[i] = pk[i] * u + ei (+ m if col = i)
(0..cols).for_each(|i| {
let (mut ci_dft, scratch_2) = scratch_1.tmp_vec_znx_dft(module, 1, size_pk);
// ci_dft = DFT(u) * DFT(pk[i])
module.svp_apply(&mut ci_dft, 0, &u_dft, 0, pk, i);
// ci_big = u * p[i]
let mut ci_big = module.vec_znx_idft_consume(ci_dft);
// ci_big = u * pk[i] + e
ci_big.add_normal(log_base2k, 0, pk.k(), source_xe, sigma, bound);
// ci_big = u * pk[i] + e + m (if col = i)
if let Some((pt, col)) = pt {
if col == i {
module.vec_znx_big_add_small_inplace(&mut ci_big, 0, pt, 0);
}
}
// ct[i] = norm(ci_big)
module.vec_znx_big_normalize(log_base2k, self, i, &ci_big, 0, scratch_2);
});
}
}
impl<DataSelf> GLWECiphertext<DataSelf>
where
VecZnx<DataSelf>: VecZnxToRef,
{
pub fn decrypt<DataPt, DataSk>(
&self,
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<DataPt>: VecZnxToMut,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let (mut c0_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, self.size()); // TODO optimize size when pt << ct
{
let (mut c0_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, self.size()); // TODO optimize size when pt << ct
module.vec_znx_dft(&mut c0_dft, 0, self, 1);
// c0_dft = DFT(a) * DFT(s)
module.svp_apply_inplace(&mut c0_dft, 0, sk_dft, 0);
// c0_big = IDFT(c0_dft)
module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut c0_dft, 0);
}
// c0_big = (a * s) + (-a * s + m + e) = BIG(m + e)
module.vec_znx_big_add_small_inplace(&mut c0_big, 0, self, 0);
// pt = norm(BIG(m + e))
module.vec_znx_big_normalize(self.basek(), pt, 0, &mut c0_big, 0, scratch_1);
pt.basek = self.basek();
pt.k = pt.k().min(self.k());
}
}

View File

@@ -0,0 +1,261 @@
use base2k::{
Backend, FFT64, MatZnxDft, MatZnxDftToRef, Module, ScalarZnxDft, ScalarZnxDftOps, ScalarZnxDftToRef, Scratch, VecZnx,
VecZnxAlloc, VecZnxBig, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps,
VecZnxDftToMut, VecZnxDftToRef, VecZnxToMut, VecZnxToRef, ZnxZero,
};
use sampling::source::Source;
use crate::{
elem::Infos,
gglwe_ciphertext::GGLWECiphertext,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext::GLWECiphertext,
glwe_plaintext::GLWEPlaintext,
keys::SecretKeyFourier,
keyswitch_key::GLWESwitchingKey,
utils::derive_size,
vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
};
pub struct GLWECiphertextFourier<C, B: Backend> {
pub data: VecZnxDft<C, B>,
pub basek: usize,
pub k: usize,
}
impl<B: Backend> GLWECiphertextFourier<Vec<u8>, B> {
pub fn new(module: &Module<B>, log_base2k: usize, log_k: usize, rank: usize) -> Self {
Self {
data: module.new_vec_znx_dft(rank + 1, derive_size(log_base2k, log_k)),
basek: log_base2k,
k: log_k,
}
}
}
impl<T, B: Backend> Infos for GLWECiphertextFourier<T, B> {
type Inner = VecZnxDft<T, B>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.basek
}
fn k(&self) -> usize {
self.k
}
}
impl<T, B: Backend> GLWECiphertextFourier<T, B> {
pub fn rank(&self) -> usize {
self.cols() - 1
}
}
impl<C, B: Backend> VecZnxDftToMut<B> for GLWECiphertextFourier<C, B>
where
VecZnxDft<C, B>: VecZnxDftToMut<B>,
{
fn to_mut(&mut self) -> VecZnxDft<&mut [u8], B> {
self.data.to_mut()
}
}
impl<C, B: Backend> VecZnxDftToRef<B> for GLWECiphertextFourier<C, B>
where
VecZnxDft<C, B>: VecZnxDftToRef<B>,
{
fn to_ref(&self) -> VecZnxDft<&[u8], B> {
self.data.to_ref()
}
}
impl GLWECiphertextFourier<Vec<u8>, FFT64> {
#[allow(dead_code)]
pub(crate) fn idft_scratch_space(module: &Module<FFT64>, size: usize) -> usize {
module.bytes_of_vec_znx(1, size) + (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 {
module.bytes_of_vec_znx(rank + 1, ct_size) + GLWECiphertext::encrypt_sk_scratch_space(module, rank, ct_size)
}
pub fn decrypt_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
(module.vec_znx_big_normalize_tmp_bytes()
| module.bytes_of_vec_znx_dft(1, ct_size)
| (module.bytes_of_vec_znx_big(1, ct_size) + module.vec_znx_idft_tmp_bytes()))
+ module.bytes_of_vec_znx_big(1, ct_size)
}
pub fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
pub fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space(module, res_size, lhs, rhs)
}
pub fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs,
)
}
}
impl<DataSelf> GLWECiphertextFourier<DataSelf, FFT64>
where
VecZnxDft<DataSelf, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
{
pub fn encrypt_zero_sk<DataSk>(
&mut self,
module: &Module<FFT64>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let (vec_znx_tmp, scratch_1) = scratch.tmp_vec_znx(module, self.rank() + 1, self.size());
let mut ct_idft = GLWECiphertext {
data: vec_znx_tmp,
basek: self.basek,
k: self.k,
};
ct_idft.encrypt_zero_sk(
module, sk_dft, source_xa, source_xe, sigma, bound, scratch_1,
);
ct_idft.dft(module, self);
}
pub fn keyswitch<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GLWECiphertextFourier<DataLhs, FFT64>,
rhs: &GLWESwitchingKey<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
VecZnxDft<DataLhs, FFT64>: VecZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.0.prod_with_glwe_fourier(module, self, lhs, scratch);
}
pub fn keyswitch_inplace<DataRhs>(
&mut self,
module: &Module<FFT64>,
rhs: &GLWESwitchingKey<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.0.prod_with_glwe_fourier_inplace(module, self, scratch);
}
pub fn external_product<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GLWECiphertextFourier<DataLhs, FFT64>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
VecZnxDft<DataLhs, FFT64>: VecZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.prod_with_glwe_fourier(module, self, lhs, scratch);
}
pub fn external_product_inplace<DataRhs>(
&mut self,
module: &Module<FFT64>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.prod_with_glwe_fourier_inplace(module, self, scratch);
}
}
impl<DataSelf> GLWECiphertextFourier<DataSelf, FFT64>
where
VecZnxDft<DataSelf, FFT64>: VecZnxDftToRef<FFT64>,
{
pub fn decrypt<DataPt, DataSk>(
&self,
module: &Module<FFT64>,
pt: &mut GLWEPlaintext<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
scratch: &mut Scratch,
) where
VecZnx<DataPt>: VecZnxToMut + VecZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), sk_dft.rank());
assert_eq!(self.n(), module.n());
assert_eq!(pt.n(), module.n());
assert_eq!(sk_dft.n(), module.n());
}
let cols = self.rank() + 1;
let (mut pt_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, self.size()); // TODO optimize size when pt << ct
pt_big.zero();
{
(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, i - 1, self, 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);
});
}
{
let (mut c0_big, scratch_2) = scratch_1.tmp_vec_znx_big(module, 1, self.size());
// c0_big = (a * s) + (-a * s + m + e) = BIG(m + e)
module.vec_znx_idft(&mut c0_big, 0, self, 0, scratch_2);
module.vec_znx_big_add_inplace(&mut pt_big, 0, &c0_big, 0);
}
// pt = norm(BIG(m + e))
module.vec_znx_big_normalize(self.basek(), pt, 0, &mut pt_big, 0, scratch_1);
pt.basek = self.basek();
pt.k = pt.k().min(self.k());
}
pub(crate) fn idft<DataRes>(&self, module: &Module<FFT64>, res: &mut GLWECiphertext<DataRes>, scratch: &mut Scratch)
where
GLWECiphertext<DataRes>: VecZnxToMut,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), res.rank());
assert_eq!(self.basek(), res.basek())
}
let min_size: usize = self.size().min(res.size());
let (mut res_big, scratch1) = scratch.tmp_vec_znx_big(module, 1, min_size);
(0..self.rank() + 1).for_each(|i| {
module.vec_znx_idft(&mut res_big, 0, self, i, scratch1);
module.vec_znx_big_normalize(self.basek(), res, i, &res_big, 0, scratch1);
});
}
}

View File

@@ -0,0 +1,53 @@
use base2k::{Backend, Module, VecZnx, VecZnxAlloc, VecZnxToMut, VecZnxToRef};
use crate::{elem::Infos, utils::derive_size};
pub struct GLWEPlaintext<C> {
pub data: VecZnx<C>,
pub basek: usize,
pub k: usize,
}
impl<T> Infos for GLWEPlaintext<T> {
type Inner = VecZnx<T>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.basek
}
fn k(&self) -> usize {
self.k
}
}
impl<C> VecZnxToMut for GLWEPlaintext<C>
where
VecZnx<C>: VecZnxToMut,
{
fn to_mut(&mut self) -> VecZnx<&mut [u8]> {
self.data.to_mut()
}
}
impl<C> VecZnxToRef for GLWEPlaintext<C>
where
VecZnx<C>: VecZnxToRef,
{
fn to_ref(&self) -> VecZnx<&[u8]> {
self.data.to_ref()
}
}
impl GLWEPlaintext<Vec<u8>> {
pub fn new<B: Backend>(module: &Module<B>, base2k: usize, k: usize) -> Self {
Self {
data: module.new_vec_znx(1, derive_size(base2k, k)),
basek: base2k,
k,
}
}
}

View File

@@ -5,7 +5,7 @@ use base2k::{
}; };
use sampling::source::Source; use sampling::source::Source;
use crate::{elem::Infos, glwe::GLWECiphertextFourier}; use crate::{elem::Infos, glwe_ciphertext_fourier::GLWECiphertextFourier};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum SecretDistribution { pub enum SecretDistribution {
@@ -21,25 +21,43 @@ pub struct SecretKey<T> {
} }
impl SecretKey<Vec<u8>> { impl SecretKey<Vec<u8>> {
pub fn new<B: Backend>(module: &Module<B>) -> Self { pub fn new<B: Backend>(module: &Module<B>, rank: usize) -> Self {
Self { Self {
data: module.new_scalar_znx(1), data: module.new_scalar_znx(rank),
dist: SecretDistribution::NONE, dist: SecretDistribution::NONE,
} }
} }
} }
impl<DataSelf> SecretKey<DataSelf> {
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<S> SecretKey<S> impl<S> SecretKey<S>
where where
S: AsMut<[u8]> + AsRef<[u8]>, S: AsMut<[u8]> + AsRef<[u8]>,
{ {
pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) { pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) {
self.data.fill_ternary_prob(0, prob, source); (0..self.rank()).for_each(|i| {
self.data.fill_ternary_prob(i, prob, source);
});
self.dist = SecretDistribution::TernaryProb(prob); self.dist = SecretDistribution::TernaryProb(prob);
} }
pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) { pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) {
self.data.fill_ternary_hw(0, hw, source); (0..self.rank()).for_each(|i| {
self.data.fill_ternary_hw(i, hw, source);
});
self.dist = SecretDistribution::TernaryFixed(hw); self.dist = SecretDistribution::TernaryFixed(hw);
} }
@@ -72,10 +90,24 @@ pub struct SecretKeyFourier<T, B: Backend> {
pub dist: SecretDistribution, pub 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> { impl<B: Backend> SecretKeyFourier<Vec<u8>, B> {
pub fn new(module: &Module<B>) -> Self { pub fn new(module: &Module<B>, rank: usize) -> Self {
Self { Self {
data: module.new_scalar_znx_dft(1), data: module.new_scalar_znx_dft(rank),
dist: SecretDistribution::NONE, dist: SecretDistribution::NONE,
} }
} }
@@ -91,9 +123,15 @@ impl<B: Backend> SecretKeyFourier<Vec<u8>, B> {
SecretDistribution::NONE => panic!("invalid sk: SecretDistribution::NONE"), 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());
} }
module.svp_prepare(self, 0, sk, 0); (0..self.rank()).for_each(|i| {
module.svp_prepare(self, i, sk, i);
});
self.dist = sk.dist; self.dist = sk.dist;
} }
} }
@@ -116,21 +154,21 @@ where
} }
} }
pub struct PublicKey<D, B: Backend> { pub struct GLWEPublicKey<D, B: Backend> {
pub data: GLWECiphertextFourier<D, B>, pub data: GLWECiphertextFourier<D, B>,
pub dist: SecretDistribution, pub dist: SecretDistribution,
} }
impl<B: Backend> PublicKey<Vec<u8>, B> { impl<B: Backend> GLWEPublicKey<Vec<u8>, B> {
pub fn new(module: &Module<B>, log_base2k: usize, log_k: usize) -> Self { pub fn new(module: &Module<B>, log_base2k: usize, log_k: usize, rank: usize) -> Self {
Self { Self {
data: GLWECiphertextFourier::new(module, log_base2k, log_k), data: GLWECiphertextFourier::new(module, log_base2k, log_k, rank),
dist: SecretDistribution::NONE, dist: SecretDistribution::NONE,
} }
} }
} }
impl<T, B: Backend> Infos for PublicKey<T, B> { impl<T, B: Backend> Infos for GLWEPublicKey<T, B> {
type Inner = VecZnxDft<T, B>; type Inner = VecZnxDft<T, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -138,15 +176,21 @@ impl<T, B: Backend> Infos for PublicKey<T, B> {
} }
fn basek(&self) -> usize { fn basek(&self) -> usize {
self.data.log_base2k self.data.basek
} }
fn k(&self) -> usize { fn k(&self) -> usize {
self.data.log_k self.data.k
} }
} }
impl<C, B: Backend> VecZnxDftToMut<B> for PublicKey<C, B> impl<T, B: Backend> GLWEPublicKey<T, B> {
pub fn rank(&self) -> usize {
self.cols() - 1
}
}
impl<C, B: Backend> VecZnxDftToMut<B> for GLWEPublicKey<C, B>
where where
VecZnxDft<C, B>: VecZnxDftToMut<B>, VecZnxDft<C, B>: VecZnxDftToMut<B>,
{ {
@@ -155,7 +199,7 @@ where
} }
} }
impl<C, B: Backend> VecZnxDftToRef<B> for PublicKey<C, B> impl<C, B: Backend> VecZnxDftToRef<B> for GLWEPublicKey<C, B>
where where
VecZnxDft<C, B>: VecZnxDftToRef<B>, VecZnxDft<C, B>: VecZnxDftToRef<B>,
{ {
@@ -164,7 +208,7 @@ where
} }
} }
impl<C> PublicKey<C, FFT64> { impl<C> GLWEPublicKey<C, FFT64> {
pub fn generate<S>( pub fn generate<S>(
&mut self, &mut self,
module: &Module<FFT64>, module: &Module<FFT64>,
@@ -186,8 +230,9 @@ impl<C> PublicKey<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_zero_sk_scratch_space( let mut scratch: ScratchOwned = ScratchOwned::new(GLWECiphertextFourier::encrypt_sk_scratch_space(
module, module,
self.rank(),
self.size(), self.size(),
)); ));
self.data.encrypt_zero_sk( self.data.encrypt_zero_sk(

View File

@@ -1,20 +0,0 @@
use base2k::{FFT64, Module, Scratch};
pub trait KeySwitchScratchSpace {
fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize;
}
pub trait KeySwitch<DataLhs, DataRhs> {
type Lhs;
type Rhs;
fn keyswitch(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch);
}
pub trait KeySwitchInplaceScratchSpace {
fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize;
}
pub trait KeySwitchInplace<DataRhs> {
type Rhs;
fn keyswitch_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch);
}

View File

@@ -1,170 +1,63 @@
use base2k::{ use base2k::{
Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftScratch, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, Backend, FFT64, MatZnxDft, MatZnxDftOps, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftToRef,
ScalarZnxDft, ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigOps, VecZnxBigScratch, ScalarZnxToRef, Scratch, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef,
VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos,
ZnxZero,
}; };
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
elem::{GetRow, Infos, SetRow}, elem::{GetRow, Infos, SetRow},
encryption::EncryptSkScratchSpace, gglwe_ciphertext::GGLWECiphertext,
external_product::{ ggsw_ciphertext::GGSWCiphertext,
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace, glwe_ciphertext_fourier::GLWECiphertextFourier,
},
ggsw::GGSWCiphertext,
glwe::{GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext},
keys::SecretKeyFourier, keys::SecretKeyFourier,
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace},
utils::derive_size,
vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace}, vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
}; };
pub struct GLWEKeySwitchKey<C, B: Backend> { pub struct GLWESwitchingKey<Data, B: Backend>(pub(crate) GGLWECiphertext<Data, B>);
pub data: MatZnxDft<C, B>,
pub log_base2k: usize,
pub log_k: usize,
}
impl<B: Backend> GLWEKeySwitchKey<Vec<u8>, B> { impl GLWESwitchingKey<Vec<u8>, FFT64> {
pub fn new(module: &Module<B>, log_base2k: usize, log_k: usize, rows: usize) -> Self { pub fn new(module: &Module<FFT64>, base2k: usize, k: usize, rows: usize, rank_in: usize, rank_out: usize) -> Self {
Self { GLWESwitchingKey(GGLWECiphertext::new(
data: module.new_mat_znx_dft(rows, 1, 2, derive_size(log_base2k, log_k)), module, base2k, k, rows, rank_in, rank_out,
log_base2k: log_base2k, ))
log_k: log_k,
}
} }
} }
impl<T, B: Backend> Infos for GLWEKeySwitchKey<T, B> { impl<T, B: Backend> Infos for GLWESwitchingKey<T, B> {
type Inner = MatZnxDft<T, B>; type Inner = MatZnxDft<T, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
&self.data &self.0.inner()
} }
fn basek(&self) -> usize { fn basek(&self) -> usize {
self.log_base2k self.0.basek()
} }
fn k(&self) -> usize { fn k(&self) -> usize {
self.log_k self.0.k()
} }
} }
impl<C, B: Backend> MatZnxDftToMut<B> for GLWEKeySwitchKey<C, B> impl<DataSelf, B: Backend> MatZnxDftToMut<B> for GLWESwitchingKey<DataSelf, B>
where where
MatZnxDft<C, B>: MatZnxDftToMut<B>, MatZnxDft<DataSelf, B>: MatZnxDftToMut<B>,
{ {
fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> { fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> {
self.data.to_mut() self.0.data.to_mut()
} }
} }
impl<C, B: Backend> MatZnxDftToRef<B> for GLWEKeySwitchKey<C, B> impl<DataSelf, B: Backend> MatZnxDftToRef<B> for GLWESwitchingKey<DataSelf, B>
where where
MatZnxDft<C, B>: MatZnxDftToRef<B>, MatZnxDft<DataSelf, B>: MatZnxDftToRef<B>,
{ {
fn to_ref(&self) -> MatZnxDft<&[u8], B> { fn to_ref(&self) -> MatZnxDft<&[u8], B> {
self.data.to_ref() self.0.data.to_ref()
} }
} }
impl GLWEKeySwitchKey<Vec<u8>, FFT64> { impl<C> GetRow<FFT64> for GLWESwitchingKey<C, FFT64>
pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, size: usize) -> usize {
GLWECiphertext::encrypt_sk_scratch_space(module, size)
+ module.bytes_of_vec_znx(2, size)
+ module.bytes_of_vec_znx(1, size)
+ module.bytes_of_vec_znx_dft(2, size)
}
}
pub fn encrypt_glwe_key_switch_key_sk<C, P, S>(
module: &Module<FFT64>,
ct: &mut GLWEKeySwitchKey<C, FFT64>,
pt: &ScalarZnx<P>,
sk_dft: &SecretKeyFourier<S, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
ScalarZnx<P>: ScalarZnxToRef,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
let rows: usize = ct.rows();
let size: usize = ct.size();
let log_base2k: usize = ct.basek();
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, 2, size);
let (mut vec_znx_dft_ct, scratch_3) = scrach_2.tmp_vec_znx_dft(module, 2, size);
let mut vec_znx_pt: GLWEPlaintext<&mut [u8]> = GLWEPlaintext {
data: tmp_znx_pt,
log_base2k: log_base2k,
log_k: ct.k(),
};
let mut vec_znx_ct: GLWECiphertext<&mut [u8]> = GLWECiphertext {
data: tmp_znx_ct,
log_base2k: log_base2k,
log_k: ct.k(),
};
(0..rows).for_each(|row_i| {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
module.vec_znx_add_scalar_inplace(&mut vec_znx_pt, 0, row_i, pt, 0);
module.vec_znx_normalize_inplace(log_base2k, &mut vec_znx_pt, 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,
bound,
scratch_3,
);
vec_znx_pt.data.zero(); // zeroes for next iteration
// Switch vec_znx_ct into DFT domain
module.vec_znx_dft(&mut vec_znx_dft_ct, 0, &vec_znx_ct, 0);
module.vec_znx_dft(&mut vec_znx_dft_ct, 1, &vec_znx_ct, 1);
// Stores vec_znx_dft_ct into thw i-th row of the MatZnxDft
module.vmp_prepare_row(ct, row_i, 0, &vec_znx_dft_ct);
});
}
impl<C> GLWEKeySwitchKey<C, FFT64> {
pub fn encrypt_sk<P, S>(
&mut self,
module: &Module<FFT64>,
pt: &ScalarZnx<P>,
sk_dft: &SecretKeyFourier<S, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
ScalarZnx<P>: ScalarZnxToRef,
ScalarZnxDft<S, FFT64>: ScalarZnxDftToRef<FFT64>,
{
encrypt_glwe_key_switch_key_sk(
module, self, pt, sk_dft, source_xa, source_xe, sigma, bound, scratch,
)
}
}
impl<C> GetRow<FFT64> for GLWEKeySwitchKey<C, FFT64>
where where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>, MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>,
{ {
@@ -180,7 +73,7 @@ where
} }
} }
impl<C> SetRow<FFT64> for GLWEKeySwitchKey<C, FFT64> impl<C> SetRow<FFT64> for GLWESwitchingKey<C, FFT64>
where where
MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>, MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
{ {
@@ -196,138 +89,117 @@ where
} }
} }
impl KeySwitchScratchSpace for GLWEKeySwitchKey<Vec<u8>, FFT64> { impl GLWESwitchingKey<Vec<u8>, FFT64> {
fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize { pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space( GGLWECiphertext::encrypt_sk_scratch_space(module, rank, size)
}
pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, rank: usize, pk_size: usize) -> usize {
GGLWECiphertext::encrypt_pk_scratch_space(module, rank, pk_size)
}
}
impl<DataSelf> GLWESwitchingKey<DataSelf, FFT64>
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
{
pub fn encrypt_sk<DataPt, DataSk>(
&mut self,
module: &Module<FFT64>,
pt: &ScalarZnx<DataPt>,
sk_dft: &SecretKeyFourier<DataSk, FFT64>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
bound: f64,
scratch: &mut Scratch,
) where
ScalarZnx<DataPt>: ScalarZnxToRef,
ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
{
self.0.encrypt_sk(
module, pt, sk_dft, source_xa, source_xe, sigma, bound, scratch,
);
}
}
impl GLWESwitchingKey<Vec<u8>, FFT64> {
pub fn keyswitch_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space(
module, res_size, lhs, rhs, module, res_size, lhs, rhs,
) )
} }
}
impl<DataSelf, DataLhs, DataRhs> KeySwitch<DataLhs, DataRhs> for GLWEKeySwitchKey<DataSelf, FFT64> pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
where <GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_inplace_scratch_space(
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GLWEKeySwitchKey<DataLhs, FFT64>;
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe(module, self, lhs, scratch);
}
}
impl KeySwitchInplaceScratchSpace for GLWEKeySwitchKey<Vec<u8>, FFT64> {
fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GLWEKeySwitchKey<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_inplace_scratch_space(
module, res_size, rhs, module, res_size, rhs,
) )
} }
}
impl<DataSelf, DataRhs> KeySwitchInplace<DataRhs> for GLWEKeySwitchKey<DataSelf, FFT64> pub fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Rhs = GLWEKeySwitchKey<DataRhs, FFT64>;
fn keyswitch_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe(module, self, rhs, scratch);
}
}
impl ExternalProductScratchSpace for GLWEKeySwitchKey<Vec<u8>, FFT64> {
fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space( <GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space(
module, res_size, lhs, rhs, module, res_size, lhs, rhs,
) )
} }
}
impl<DataSelf, DataLhs, DataRhs> ExternalProduct<DataLhs, DataRhs> for GLWEKeySwitchKey<DataSelf, FFT64> pub fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
type Lhs = GLWEKeySwitchKey<DataLhs, FFT64>;
type Rhs = GGSWCiphertext<DataRhs, FFT64>;
fn external_product(&mut self, module: &Module<FFT64>, lhs: &Self::Lhs, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe(module, self, lhs, scratch);
}
}
impl ExternalProductInplaceScratchSpace for GLWEKeySwitchKey<Vec<u8>, FFT64> {
fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space( <GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space(
module, res_size, rhs, module, res_size, rhs,
) )
} }
} }
impl<DataSelf, DataRhs> ExternalProductInplace<DataRhs> for GLWEKeySwitchKey<DataSelf, FFT64> impl<DataSelf> GLWESwitchingKey<DataSelf, FFT64>
where where
MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>, MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + MatZnxDftToRef<FFT64>,
{
pub fn keyswitch<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GLWESwitchingKey<DataLhs, FFT64>,
rhs: &GLWESwitchingKey<DataRhs, FFT64>,
scratch: &mut base2k::Scratch,
) where
MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>, MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{ {
type Rhs = GGSWCiphertext<DataRhs, FFT64>; rhs.0
.prod_with_vec_glwe(module, &mut self.0, &lhs.0, scratch);
fn external_product_inplace(&mut self, module: &Module<FFT64>, rhs: &Self::Rhs, scratch: &mut Scratch) {
rhs.prod_with_vec_glwe_inplace(module, self, scratch);
}
} }
impl VecGLWEProductScratchSpace for GLWEKeySwitchKey<Vec<u8>, FFT64> { pub fn keyswitch_inplace<DataRhs>(
fn prod_with_glwe_scratch_space(module: &Module<FFT64>, res_size: usize, a_size: usize, grlwe_size: usize) -> usize { &mut self,
module.bytes_of_vec_znx_dft(2, grlwe_size)
+ (module.vec_znx_big_normalize_tmp_bytes()
| (module.vmp_apply_tmp_bytes(res_size, a_size, a_size, 1, 2, grlwe_size)
+ module.bytes_of_vec_znx_dft(1, a_size)))
}
}
impl<C> VecGLWEProduct for GLWEKeySwitchKey<C, FFT64>
where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64> + ZnxInfos,
{
fn prod_with_glwe<R, A>(
&self,
module: &Module<FFT64>, module: &Module<FFT64>,
res: &mut GLWECiphertext<R>, rhs: &GLWESwitchingKey<DataRhs, FFT64>,
a: &GLWECiphertext<A>, scratch: &mut base2k::Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{
rhs.0
.prod_with_vec_glwe_inplace(module, &mut self.0, scratch);
}
pub fn external_product<DataLhs, DataRhs>(
&mut self,
module: &Module<FFT64>,
lhs: &GLWESwitchingKey<DataLhs, FFT64>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch, scratch: &mut Scratch,
) where ) where
MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>, MatZnxDft<DataLhs, FFT64>: MatZnxDftToRef<FFT64>,
VecZnx<R>: VecZnxToMut, MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
VecZnx<A>: VecZnxToRef,
{ {
let log_base2k: usize = self.basek(); rhs.prod_with_vec_glwe(module, &mut self.0, &lhs.0, scratch);
}
#[cfg(debug_assertions)] pub fn external_product_inplace<DataRhs>(
&mut self,
module: &Module<FFT64>,
rhs: &GGSWCiphertext<DataRhs, FFT64>,
scratch: &mut Scratch,
) where
MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
{ {
assert_eq!(res.basek(), log_base2k); rhs.prod_with_vec_glwe_inplace(module, &mut self.0, scratch);
assert_eq!(a.basek(), log_base2k);
assert_eq!(self.n(), module.n());
assert_eq!(res.n(), module.n());
assert_eq!(a.n(), module.n());
}
let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, 2, self.size()); // Todo optimise
{
let (mut a1_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, 1, a.size());
module.vec_znx_dft(&mut a1_dft, 0, a, 1);
module.vmp_apply(&mut res_dft, &a1_dft, self, scratch2);
}
let mut res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft);
module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0);
module.vec_znx_big_normalize(log_base2k, res, 0, &res_big, 0, scratch1);
module.vec_znx_big_normalize(log_base2k, res, 1, &res_big, 1, scratch1);
} }
} }

View File

@@ -1,10 +1,10 @@
pub mod elem; pub mod elem;
pub mod encryption; pub mod gglwe_ciphertext;
pub mod external_product; pub mod ggsw_ciphertext;
pub mod ggsw; pub mod glwe_ciphertext;
pub mod glwe; pub mod glwe_ciphertext_fourier;
pub mod glwe_plaintext;
pub mod keys; pub mod keys;
pub mod keyswitch;
pub mod keyswitch_key; pub mod keyswitch_key;
#[cfg(test)] #[cfg(test)]
mod test_fft64; mod test_fft64;

View File

@@ -3,15 +3,12 @@ use sampling::source::Source;
use crate::{ use crate::{
elem::{GetRow, Infos}, elem::{GetRow, Infos},
external_product::{ ggsw_ciphertext::GGSWCiphertext,
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace, glwe_ciphertext_fourier::GLWECiphertextFourier,
}, glwe_plaintext::GLWEPlaintext,
ggsw::GGSWCiphertext,
glwe::{GLWECiphertextFourier, GLWEPlaintext},
keys::{SecretKey, SecretKeyFourier}, keys::{SecretKey, SecretKeyFourier},
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace}, keyswitch_key::GLWESwitchingKey,
keyswitch_key::GLWEKeySwitchKey, test_fft64::ggsw::noise_rgsw_product,
test_fft64::rgsw::noise_rgsw_product,
}; };
#[test] #[test]
@@ -20,11 +17,13 @@ fn encrypt_sk() {
let log_base2k: usize = 8; let log_base2k: usize = 8;
let log_k_ct: usize = 54; let log_k_ct: usize = 54;
let rows: usize = 4; let rows: usize = 4;
let rank: usize = 1;
let rank_out: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_ct, rows); let mut ct: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, log_base2k, log_k_ct, rows, rank, rank_out);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct);
let mut pt_scalar: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_scalar: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
@@ -35,14 +34,15 @@ fn encrypt_sk() {
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(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct.size()), | GLWECiphertextFourier::decrypt_scratch_space(&module, ct.size()),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); // sk.fill_ternary_prob(0.5, &mut source_xs);
sk.fill_zero();
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct.encrypt_sk( ct.encrypt_sk(
@@ -56,7 +56,7 @@ fn encrypt_sk() {
scratch.borrow(), scratch.borrow(),
); );
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_ct); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_ct, rank);
(0..ct.rows()).for_each(|row_i| { (0..ct.rows()).for_each(|row_i| {
ct.get_row(&module, row_i, 0, &mut ct_rlwe_dft); ct.get_row(&module, row_i, 0, &mut ct_rlwe_dft);
@@ -74,21 +74,26 @@ fn keyswitch() {
let log_k_grlwe: usize = 60; let log_k_grlwe: usize = 60;
let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k; let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe_s0s1: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe_s0s1: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_grlwe_s1s2: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_grlwe_s0s2: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe_s1s2: GLWESwitchingKey<Vec<u8>, FFT64> =
GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_grlwe_s0s2: GLWESwitchingKey<Vec<u8>, FFT64> =
GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe_s0s1.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe_s0s1.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_s0s2.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_s0s2.size())
| GLWEKeySwitchKey::keyswitch_scratch_space( | GLWESwitchingKey::keyswitch_scratch_space(
&module, &module,
ct_grlwe_s0s2.size(), ct_grlwe_s0s2.size(),
ct_grlwe_s0s1.size(), ct_grlwe_s0s1.size(),
@@ -96,22 +101,22 @@ fn keyswitch() {
), ),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk2.fill_ternary_prob(0.5, &mut source_xs); sk2.fill_ternary_prob(0.5, &mut source_xs);
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk2_dft.dft(&module, &sk2); sk2_dft.dft(&module, &sk2);
// GRLWE_{s1}(s0) = s0 -> s1 // GRLWE_{s1}(s0) = s0 -> s1
@@ -142,7 +147,7 @@ fn keyswitch() {
ct_grlwe_s0s2.keyswitch(&module, &ct_grlwe_s0s1, &ct_grlwe_s1s2, scratch.borrow()); ct_grlwe_s0s2.keyswitch(&module, &ct_grlwe_s0s1, &ct_grlwe_s1s2, scratch.borrow());
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe); GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe);
(0..ct_grlwe_s0s2.rows()).for_each(|row_i| { (0..ct_grlwe_s0s2.rows()).for_each(|row_i| {
@@ -179,38 +184,43 @@ fn keyswitch_inplace() {
let log_k_grlwe: usize = 60; let log_k_grlwe: usize = 60;
let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k; let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let rank_out: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe_s0s1: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe_s0s1: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_grlwe_s1s2: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank_out);
let mut ct_grlwe_s1s2: GLWESwitchingKey<Vec<u8>, FFT64> =
GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank_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]);
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(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe_s0s1.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe_s0s1.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_s0s1.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_s0s1.size())
| GLWEKeySwitchKey::keyswitch_inplace_scratch_space(&module, ct_grlwe_s0s1.size(), ct_grlwe_s1s2.size()), | GLWESwitchingKey::keyswitch_inplace_scratch_space(&module, ct_grlwe_s0s1.size(), ct_grlwe_s1s2.size()),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk2.fill_ternary_prob(0.5, &mut source_xs); sk2.fill_ternary_prob(0.5, &mut source_xs);
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk2_dft.dft(&module, &sk2); sk2_dft.dft(&module, &sk2);
// GRLWE_{s1}(s0) = s0 -> s1 // GRLWE_{s1}(s0) = s0 -> s1
@@ -240,10 +250,10 @@ fn keyswitch_inplace() {
// GRLWE_{s1}(s0) (x) GRLWE_{s2}(s1) = GRLWE_{s2}(s0) // GRLWE_{s1}(s0) (x) GRLWE_{s2}(s1) = GRLWE_{s2}(s0)
ct_grlwe_s0s1.keyswitch_inplace(&module, &ct_grlwe_s1s2, scratch.borrow()); ct_grlwe_s0s1.keyswitch_inplace(&module, &ct_grlwe_s1s2, scratch.borrow());
let ct_grlwe_s0s2: GLWEKeySwitchKey<Vec<u8>, FFT64> = ct_grlwe_s0s1; let ct_grlwe_s0s2: GLWESwitchingKey<Vec<u8>, FFT64> = ct_grlwe_s0s1;
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe); GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe);
(0..ct_grlwe_s0s2.rows()).for_each(|row_i| { (0..ct_grlwe_s0s2.rows()).for_each(|row_i| {
@@ -280,12 +290,17 @@ fn external_product() {
let log_k_grlwe: usize = 60; let log_k_grlwe: usize = 60;
let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k; let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let rank_out: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe_in: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe_in: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_grlwe_out: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank_out);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe_out: GLWESwitchingKey<Vec<u8>, FFT64> =
GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank_out);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, 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_grlwe: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_grlwe: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
@@ -295,15 +310,15 @@ fn external_product() {
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(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe_in.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe_in.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_out.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_out.size())
| GLWEKeySwitchKey::external_product_scratch_space( | GLWESwitchingKey::external_product_scratch_space(
&module, &module,
ct_grlwe_out.size(), ct_grlwe_out.size(),
ct_grlwe_in.size(), ct_grlwe_in.size(),
ct_rgsw.size(), ct_rgsw.size(),
) )
| GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw.size()), | GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size()),
); );
let k: usize = 1; let k: usize = 1;
@@ -312,10 +327,10 @@ fn external_product() {
pt_grlwe.fill_ternary_prob(0, 0.5, &mut source_xs); pt_grlwe.fill_ternary_prob(0, 0.5, &mut source_xs);
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
// GRLWE_{s1}(s0) = s0 -> s1 // GRLWE_{s1}(s0) = s0 -> s1
@@ -345,7 +360,7 @@ fn external_product() {
ct_grlwe_out.external_product(&module, &ct_grlwe_in, &ct_rgsw, scratch.borrow()); ct_grlwe_out.external_product(&module, &ct_grlwe_in, &ct_rgsw, scratch.borrow());
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe); GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe);
module.vec_znx_rotate_inplace(k as i64, &mut pt_grlwe, 0); module.vec_znx_rotate_inplace(k as i64, &mut pt_grlwe, 0);
@@ -393,11 +408,15 @@ fn external_product_inplace() {
let log_k_grlwe: usize = 60; let log_k_grlwe: usize = 60;
let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k; let rows: usize = (log_k_grlwe + log_base2k - 1) / log_base2k;
let rank = 1;
let rank_out = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank_out);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, 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_grlwe: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_grlwe: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
@@ -407,10 +426,10 @@ fn external_product_inplace() {
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(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe.size())
| GLWEKeySwitchKey::external_product_inplace_scratch_space(&module, ct_grlwe.size(), ct_rgsw.size()) | GLWESwitchingKey::external_product_inplace_scratch_space(&module, ct_grlwe.size(), ct_rgsw.size())
| GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw.size()), | GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size()),
); );
let k: usize = 1; let k: usize = 1;
@@ -419,10 +438,10 @@ fn external_product_inplace() {
pt_grlwe.fill_ternary_prob(0, 0.5, &mut source_xs); pt_grlwe.fill_ternary_prob(0, 0.5, &mut source_xs);
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
// GRLWE_{s1}(s0) = s0 -> s1 // GRLWE_{s1}(s0) = s0 -> s1
@@ -452,7 +471,7 @@ fn external_product_inplace() {
ct_grlwe.external_product_inplace(&module, &ct_rgsw, scratch.borrow()); ct_grlwe.external_product_inplace(&module, &ct_rgsw, scratch.borrow());
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe); GLWECiphertextFourier::new(&module, log_base2k, log_k_grlwe, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_grlwe);
module.vec_znx_rotate_inplace(k as i64, &mut pt_grlwe, 0); module.vec_znx_rotate_inplace(k as i64, &mut pt_grlwe, 0);

View File

@@ -6,15 +6,12 @@ use sampling::source::Source;
use crate::{ use crate::{
elem::{GetRow, Infos}, elem::{GetRow, Infos},
external_product::{ ggsw_ciphertext::GGSWCiphertext,
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace, glwe_ciphertext_fourier::GLWECiphertextFourier,
}, glwe_plaintext::GLWEPlaintext,
ggsw::GGSWCiphertext,
glwe::{GLWECiphertextFourier, GLWEPlaintext},
keys::{SecretKey, SecretKeyFourier}, keys::{SecretKey, SecretKeyFourier},
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace}, keyswitch_key::GLWESwitchingKey,
keyswitch_key::GLWEKeySwitchKey, test_fft64::gglwe::noise_grlwe_rlwe_product,
test_fft64::grlwe::noise_grlwe_rlwe_product,
}; };
#[test] #[test]
@@ -23,11 +20,12 @@ fn encrypt_sk() {
let log_base2k: usize = 8; let log_base2k: usize = 8;
let log_k_ct: usize = 54; let log_k_ct: usize = 54;
let rows: usize = 4; let rows: usize = 4;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_ct, rows); let mut ct: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_ct, rows, rank);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct);
let mut pt_scalar: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_scalar: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
@@ -39,14 +37,14 @@ fn encrypt_sk() {
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, ct.size()) GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct.size()), | GLWECiphertextFourier::decrypt_scratch_space(&module, ct.size()),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct.encrypt_sk( ct.encrypt_sk(
@@ -60,7 +58,7 @@ fn encrypt_sk() {
scratch.borrow(), scratch.borrow(),
); );
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_ct); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_ct, 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());
@@ -98,12 +96,15 @@ fn keyswitch() {
let log_k_rgsw_out: usize = 45; let log_k_rgsw_out: usize = 45;
let rows: usize = (log_k_rgsw_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rgsw_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_rgsw_in: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_in, rows); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_rgsw_out: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_out, rows); let mut ct_rgsw_in: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_in, rows, rank);
let mut ct_rgsw_out: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_out, rows, 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 source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -114,9 +115,9 @@ fn keyswitch() {
pt_rgsw.fill_ternary_prob(0, 0.5, &mut source_xs); pt_rgsw.fill_ternary_prob(0, 0.5, &mut source_xs);
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw_out.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw_out.size())
| GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw_in.size()) | GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw_in.size())
| GGSWCiphertext::keyswitch_scratch_space( | GGSWCiphertext::keyswitch_scratch_space(
&module, &module,
ct_rgsw_out.size(), ct_rgsw_out.size(),
@@ -125,16 +126,16 @@ fn keyswitch() {
), ),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
ct_grlwe.encrypt_sk( ct_grlwe.encrypt_sk(
@@ -161,7 +162,8 @@ fn keyswitch() {
ct_rgsw_out.keyswitch(&module, &ct_rgsw_in, &ct_grlwe, scratch.borrow()); ct_rgsw_out.keyswitch(&module, &ct_rgsw_in, &ct_grlwe, scratch.borrow());
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw_out); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw_out, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw_out); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw_out);
let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw_out.size()); let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw_out.size());
let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw_out.size()); let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw_out.size());
@@ -215,12 +217,14 @@ fn keyswitch_inplace() {
let log_k_grlwe: usize = 60; let log_k_grlwe: usize = 60;
let log_k_rgsw: usize = 45; let log_k_rgsw: usize = 45;
let rows: usize = (log_k_rgsw + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rgsw + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw, rows); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw, rows, 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 source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -231,22 +235,22 @@ fn keyswitch_inplace() {
pt_rgsw.fill_ternary_prob(0, 0.5, &mut source_xs); pt_rgsw.fill_ternary_prob(0, 0.5, &mut source_xs);
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw.size())
| GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw.size()) | GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
| GGSWCiphertext::keyswitch_inplace_scratch_space(&module, ct_rgsw.size(), ct_grlwe.size()), | GGSWCiphertext::keyswitch_inplace_scratch_space(&module, ct_rgsw.size(), ct_grlwe.size()),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
ct_grlwe.encrypt_sk( ct_grlwe.encrypt_sk(
@@ -273,7 +277,8 @@ fn keyswitch_inplace() {
ct_rgsw.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow()); ct_rgsw.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow());
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw);
let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw.size()); let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw.size());
let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw.size()); let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw.size());
@@ -328,13 +333,16 @@ fn external_product() {
let log_k_rgsw_lhs_in: usize = 45; let log_k_rgsw_lhs_in: usize = 45;
let log_k_rgsw_lhs_out: usize = 45; let log_k_rgsw_lhs_out: usize = 45;
let rows: usize = (log_k_rgsw_lhs_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rgsw_lhs_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_rgsw_rhs: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_rhs, rows); let mut ct_rgsw_rhs: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_rhs, rows, rank);
let mut ct_rgsw_lhs_in: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_lhs_in, rows); let mut ct_rgsw_lhs_in: GGSWCiphertext<Vec<u8>, FFT64> =
let mut ct_rgsw_lhs_out: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_lhs_out, rows); GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_lhs_in, rows, rank);
let mut ct_rgsw_lhs_out: GGSWCiphertext<Vec<u8>, FFT64> =
GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_lhs_out, rows, rank);
let mut pt_rgsw_lhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_rgsw_lhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
let mut pt_rgsw_rhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_rgsw_rhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
@@ -350,9 +358,9 @@ fn external_product() {
pt_rgsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k} pt_rgsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k}
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_rgsw_rhs.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_rgsw_rhs.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw_lhs_out.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw_lhs_out.size())
| GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw_lhs_in.size()) | GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw_lhs_in.size())
| GGSWCiphertext::external_product_scratch_space( | GGSWCiphertext::external_product_scratch_space(
&module, &module,
ct_rgsw_lhs_out.size(), ct_rgsw_lhs_out.size(),
@@ -361,10 +369,10 @@ fn external_product() {
), ),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct_rgsw_rhs.encrypt_sk( ct_rgsw_rhs.encrypt_sk(
@@ -392,7 +400,7 @@ fn external_product() {
ct_rgsw_lhs_out.external_product(&module, &ct_rgsw_lhs_in, &ct_rgsw_rhs, scratch.borrow()); ct_rgsw_lhs_out.external_product(&module, &ct_rgsw_lhs_in, &ct_rgsw_rhs, scratch.borrow());
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw_lhs_out); GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw_lhs_out, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw_lhs_out); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw_lhs_out);
let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw_lhs_out.size()); let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw_lhs_out.size());
let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw_lhs_out.size()); let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw_lhs_out.size());
@@ -457,12 +465,13 @@ fn external_product_inplace() {
let log_k_rgsw_rhs: usize = 60; let log_k_rgsw_rhs: usize = 60;
let log_k_rgsw_lhs: usize = 45; let log_k_rgsw_lhs: usize = 45;
let rows: usize = (log_k_rgsw_lhs + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rgsw_lhs + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_rgsw_rhs: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_rhs, rows); let mut ct_rgsw_rhs: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_rhs, rows, rank);
let mut ct_rgsw_lhs: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_lhs, rows); let mut ct_rgsw_lhs: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_rgsw_lhs, rows, rank);
let mut pt_rgsw_lhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_rgsw_lhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
let mut pt_rgsw_rhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let mut pt_rgsw_rhs: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
@@ -478,16 +487,16 @@ fn external_product_inplace() {
pt_rgsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k} pt_rgsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k}
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_rgsw_rhs.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_rgsw_rhs.size())
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw_lhs.size()) | GLWECiphertextFourier::decrypt_scratch_space(&module, ct_rgsw_lhs.size())
| GGSWCiphertext::encrypt_sk_scratch_space(&module, ct_rgsw_lhs.size()) | GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw_lhs.size())
| GGSWCiphertext::external_product_inplace_scratch_space(&module, ct_rgsw_lhs.size(), ct_rgsw_rhs.size()), | GGSWCiphertext::external_product_inplace_scratch_space(&module, ct_rgsw_lhs.size(), ct_rgsw_rhs.size()),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct_rgsw_rhs.encrypt_sk( ct_rgsw_rhs.encrypt_sk(
@@ -514,7 +523,8 @@ fn external_product_inplace() {
ct_rgsw_lhs.external_product_inplace(&module, &ct_rgsw_rhs, scratch.borrow()); ct_rgsw_lhs.external_product_inplace(&module, &ct_rgsw_rhs, scratch.borrow());
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw_lhs); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rgsw_lhs, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw_lhs); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rgsw_lhs);
let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw_lhs.size()); let mut pt_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_rgsw_lhs.size());
let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw_lhs.size()); let mut pt_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_rgsw_lhs.size());

View File

@@ -7,16 +7,13 @@ use sampling::source::Source;
use crate::{ use crate::{
elem::Infos, elem::Infos,
encryption::EncryptSkScratchSpace, ggsw_ciphertext::GGSWCiphertext,
external_product::{ glwe_ciphertext::GLWECiphertext,
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace, glwe_ciphertext_fourier::GLWECiphertextFourier,
}, glwe_plaintext::GLWEPlaintext,
ggsw::GGSWCiphertext, keys::{GLWEPublicKey, SecretKey, SecretKeyFourier},
glwe::{GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext}, keyswitch_key::GLWESwitchingKey,
keys::{PublicKey, SecretKey, SecretKeyFourier}, test_fft64::{gglwe::noise_grlwe_rlwe_product, ggsw::noise_rgsw_product},
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace},
keyswitch_key::GLWEKeySwitchKey,
test_fft64::{grlwe::noise_grlwe_rlwe_product, rgsw::noise_rgsw_product},
}; };
#[test] #[test]
@@ -25,11 +22,12 @@ fn encrypt_sk() {
let log_base2k: usize = 8; let log_base2k: usize = 8;
let log_k_ct: usize = 54; let log_k_ct: usize = 54;
let log_k_pt: usize = 30; let log_k_pt: usize = 30;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_ct); let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_ct, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_pt); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_pt);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -37,13 +35,14 @@ fn encrypt_sk() {
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, rank, ct.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct.size()),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
let mut data_want: Vec<i64> = vec![0i64; module.n()]; let mut data_want: Vec<i64> = vec![0i64; module.n()];
@@ -93,6 +92,7 @@ fn encrypt_zero_sk() {
let module: Module<FFT64> = Module::<FFT64>::new(1024); let module: Module<FFT64> = Module::<FFT64>::new(1024);
let log_base2k: usize = 8; let log_base2k: usize = 8;
let log_k_ct: usize = 55; let log_k_ct: usize = 55;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
@@ -103,16 +103,16 @@ fn encrypt_zero_sk() {
let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
let mut ct_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_ct); let mut ct_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_ct, 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, ct_dft.size())
| GLWECiphertextFourier::encrypt_zero_sk_scratch_space(&module, ct_dft.size()), | GLWECiphertextFourier::encrypt_sk_scratch_space(&module, rank, ct_dft.size()),
); );
ct_dft.encrypt_zero_sk( ct_dft.encrypt_zero_sk(
@@ -135,11 +135,12 @@ fn encrypt_pk() {
let log_base2k: usize = 8; let log_base2k: usize = 8;
let log_k_ct: usize = 54; let log_k_ct: usize = 54;
let log_k_pk: usize = 64; let log_k_pk: usize = 64;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_ct); let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_ct, rank);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_ct);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -147,12 +148,12 @@ fn encrypt_pk() {
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xu: Source = Source::new([0u8; 32]); let mut source_xu: Source = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
let mut pk: PublicKey<Vec<u8>, FFT64> = PublicKey::new(&module, log_base2k, log_k_pk); let mut pk: GLWEPublicKey<Vec<u8>, FFT64> = GLWEPublicKey::new(&module, log_base2k, log_k_pk, rank);
pk.generate( pk.generate(
&module, &module,
&sk_dft, &sk_dft,
@@ -163,9 +164,9 @@ fn encrypt_pk() {
); );
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, rank, ct.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct.size())
| GLWECiphertext::encrypt_pk_scratch_space(&module, pk.size()), | GLWECiphertext::encrypt_pk_scratch_space(&module, rank, pk.size()),
); );
let mut data_want: Vec<i64> = vec![0i64; module.n()]; let mut data_want: Vec<i64> = vec![0i64; module.n()];
@@ -206,13 +207,15 @@ fn keyswitch() {
let log_k_rlwe_in: usize = 45; let log_k_rlwe_in: usize = 45;
let log_k_rlwe_out: usize = 60; let log_k_rlwe_out: usize = 60;
let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out); let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank);
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out, rank);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out);
@@ -226,9 +229,9 @@ fn keyswitch() {
.fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa); .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe_in.size())
| GLWECiphertext::keyswitch_scratch_space( | GLWECiphertext::keyswitch_scratch_space(
&module, &module,
ct_rlwe_out.size(), ct_rlwe_out.size(),
@@ -237,16 +240,16 @@ fn keyswitch() {
), ),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
ct_grlwe.encrypt_sk( ct_grlwe.encrypt_sk(
@@ -305,12 +308,14 @@ fn keyswich_inplace() {
let log_k_grlwe: usize = 60; let log_k_grlwe: usize = 60;
let log_k_rlwe: usize = 45; let log_k_rlwe: usize = 45;
let rows: usize = (log_k_rlwe + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe, rank);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe);
@@ -324,22 +329,22 @@ fn keyswich_inplace() {
.fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa); .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size())
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct_rlwe.size(), ct_grlwe.size()), | GLWECiphertext::keyswitch_inplace_scratch_space(&module, ct_rlwe.size(), ct_grlwe.size()),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
ct_grlwe.encrypt_sk( ct_grlwe.encrypt_sk(
@@ -399,13 +404,14 @@ fn external_product() {
let log_k_rlwe_in: usize = 45; let log_k_rlwe_in: usize = 45;
let log_k_rlwe_out: usize = 60; let log_k_rlwe_out: usize = 60;
let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, rank);
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in); let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank);
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out); let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_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::new(&module, log_base2k, log_k_rlwe_in); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out);
@@ -426,9 +432,9 @@ fn external_product() {
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, ct_rgsw.size()) GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe_in.size())
| GLWECiphertext::external_product_scratch_space( | GLWECiphertext::external_product_scratch_space(
&module, &module,
ct_rlwe_out.size(), ct_rlwe_out.size(),
@@ -437,10 +443,10 @@ fn external_product() {
), ),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct_rgsw.encrypt_sk( ct_rgsw.encrypt_sk(
@@ -511,12 +517,13 @@ fn external_product_inplace() {
let log_k_rlwe_in: usize = 45; let log_k_rlwe_in: usize = 45;
let log_k_rlwe_out: usize = 60; let log_k_rlwe_out: usize = 60;
let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, rank);
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in); let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, 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::new(&module, log_base2k, log_k_rlwe_in); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out);
@@ -537,16 +544,16 @@ fn external_product_inplace() {
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, ct_rgsw.size()) GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size())
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size()), | GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size()),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct_rgsw.encrypt_sk( ct_rgsw.encrypt_sk(

View File

@@ -1,15 +1,12 @@
use crate::{ use crate::{
elem::Infos, elem::Infos,
encryption::EncryptSkScratchSpace, ggsw_ciphertext::GGSWCiphertext,
external_product::{ glwe_ciphertext::GLWECiphertext,
ExternalProduct, ExternalProductInplace, ExternalProductInplaceScratchSpace, ExternalProductScratchSpace, glwe_ciphertext_fourier::GLWECiphertextFourier,
}, glwe_plaintext::GLWEPlaintext,
ggsw::GGSWCiphertext,
glwe::{GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext},
keys::{SecretKey, SecretKeyFourier}, keys::{SecretKey, SecretKeyFourier},
keyswitch::{KeySwitch, KeySwitchInplace, KeySwitchInplaceScratchSpace, KeySwitchScratchSpace}, keyswitch_key::GLWESwitchingKey,
keyswitch_key::GLWEKeySwitchKey, test_fft64::{gglwe::noise_grlwe_rlwe_product, ggsw::noise_rgsw_product},
test_fft64::{grlwe::noise_grlwe_rlwe_product, rgsw::noise_rgsw_product},
}; };
use base2k::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, VecZnxToMut, ZnxViewMut}; use base2k::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, VecZnxToMut, ZnxViewMut};
use sampling::source::Source; use sampling::source::Source;
@@ -23,16 +20,19 @@ fn keyswitch() {
let log_k_rlwe_out: usize = 60; let log_k_rlwe_out: usize = 60;
let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank);
let mut ct_rlwe_in_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_in_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in); GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in, rank);
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out); let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out, rank);
let mut ct_rlwe_out_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_out_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_out); GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_out, rank);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out);
@@ -46,9 +46,9 @@ fn keyswitch() {
.fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa); .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe_in.size())
| GLWECiphertextFourier::keyswitch_scratch_space( | GLWECiphertextFourier::keyswitch_scratch_space(
&module, &module,
ct_rlwe_out.size(), ct_rlwe_out.size(),
@@ -57,16 +57,16 @@ fn keyswitch() {
), ),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
ct_grlwe.encrypt_sk( ct_grlwe.encrypt_sk(
@@ -127,13 +127,16 @@ fn keyswich_inplace() {
let log_k_grlwe: usize = 60; let log_k_grlwe: usize = 60;
let log_k_rlwe: usize = 45; let log_k_rlwe: usize = 45;
let rows: usize = (log_k_rlwe + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_grlwe: GLWEKeySwitchKey<Vec<u8>, FFT64> = GLWEKeySwitchKey::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe); GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank);
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe); let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe, rank);
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe, rank);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe);
@@ -147,22 +150,22 @@ fn keyswich_inplace() {
.fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa); .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new( let mut scratch: ScratchOwned = ScratchOwned::new(
GLWEKeySwitchKey::encrypt_sk_scratch_space(&module, ct_grlwe.size()) GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size())
| GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, ct_rlwe_dft.size(), ct_grlwe.size()), | GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, ct_rlwe_dft.size(), ct_grlwe.size()),
); );
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs); sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk0_dft.dft(&module, &sk0); sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs); sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk1_dft.dft(&module, &sk1); sk1_dft.dft(&module, &sk1);
ct_grlwe.encrypt_sk( ct_grlwe.encrypt_sk(
@@ -224,17 +227,18 @@ fn external_product() {
let log_k_rlwe_in: usize = 45; let log_k_rlwe_in: usize = 45;
let log_k_rlwe_out: usize = 60; let log_k_rlwe_out: usize = 60;
let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, rank);
let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in); let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank);
let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out); let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out, rank);
let mut ct_rlwe_dft_in: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_dft_in: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in); GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in, rank);
let mut ct_rlwe_dft_out: GLWECiphertextFourier<Vec<u8>, FFT64> = let mut ct_rlwe_dft_out: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_out); GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_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::new(&module, log_base2k, log_k_rlwe_in); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out);
@@ -255,9 +259,9 @@ fn external_product() {
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, ct_rgsw.size()) GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe_in.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe_in.size())
| GLWECiphertext::external_product_scratch_space( | GLWECiphertext::external_product_scratch_space(
&module, &module,
ct_rlwe_out.size(), ct_rlwe_out.size(),
@@ -266,10 +270,10 @@ fn external_product() {
), ),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct_rgsw.encrypt_sk( ct_rgsw.encrypt_sk(
@@ -342,13 +346,15 @@ fn external_product_inplace() {
let log_k_rlwe_in: usize = 45; let log_k_rlwe_in: usize = 45;
let log_k_rlwe_out: usize = 60; let log_k_rlwe_out: usize = 60;
let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k;
let rank: usize = 1;
let sigma: f64 = 3.2; let sigma: f64 = 3.2;
let bound: f64 = sigma * 6.0; let bound: f64 = sigma * 6.0;
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows); let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, rank);
let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in); let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank);
let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in, 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::new(&module, log_base2k, log_k_rlwe_in); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out);
@@ -369,16 +375,16 @@ fn external_product_inplace() {
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, ct_rgsw.size()) GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
| GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size())
| GLWECiphertext::encrypt_sk_scratch_space(&module, ct_rlwe.size()) | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size())
| GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size()), | GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size()),
); );
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module); let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
sk_dft.dft(&module, &sk); sk_dft.dft(&module, &sk);
ct_rgsw.encrypt_sk( ct_rgsw.encrypt_sk(

View File

@@ -1,4 +1,4 @@
mod grlwe; mod gglwe;
mod rgsw; mod ggsw;
mod rlwe; mod glwe;
mod rlwe_dft; mod glwe_fourier;

View File

@@ -5,7 +5,8 @@ use base2k::{
use crate::{ use crate::{
elem::{GetRow, Infos, SetRow}, elem::{GetRow, Infos, SetRow},
glwe::{GLWECiphertext, GLWECiphertextFourier}, glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
}; };
pub(crate) trait VecGLWEProductScratchSpace { pub(crate) trait VecGLWEProductScratchSpace {
@@ -81,8 +82,8 @@ pub(crate) trait VecGLWEProduct: Infos {
let mut a_idft: GLWECiphertext<&mut [u8]> = GLWECiphertext::<&mut [u8]> { let mut a_idft: GLWECiphertext<&mut [u8]> = GLWECiphertext::<&mut [u8]> {
data: a_data, data: a_data,
log_base2k: a.basek(), basek: a.basek(),
log_k: a.k(), k: a.k(),
}; };
a.idft(module, &mut a_idft, scratch_1); a.idft(module, &mut a_idft, scratch_1);
@@ -91,8 +92,8 @@ pub(crate) trait VecGLWEProduct: Infos {
let mut res_idft: GLWECiphertext<&mut [u8]> = GLWECiphertext::<&mut [u8]> { let mut res_idft: GLWECiphertext<&mut [u8]> = GLWECiphertext::<&mut [u8]> {
data: res_data, data: res_data,
log_base2k: res.basek(), basek: res.basek(),
log_k: res.k(), k: res.k(),
}; };
self.prod_with_glwe(module, &mut res_idft, &a_idft, scratch_2); self.prod_with_glwe(module, &mut res_idft, &a_idft, scratch_2);
@@ -122,8 +123,8 @@ pub(crate) trait VecGLWEProduct: Infos {
let mut res_idft: GLWECiphertext<&mut [u8]> = GLWECiphertext::<&mut [u8]> { let mut res_idft: GLWECiphertext<&mut [u8]> = GLWECiphertext::<&mut [u8]> {
data: res_data, data: res_data,
log_base2k: res.basek(), basek: res.basek(),
log_k: res.k(), k: res.k(),
}; };
res.idft(module, &mut res_idft, scratch_1); res.idft(module, &mut res_idft, scratch_1);
@@ -143,22 +144,22 @@ pub(crate) trait VecGLWEProduct: Infos {
let mut tmp_a_row: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier::<&mut [u8], FFT64> { let mut tmp_a_row: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier::<&mut [u8], FFT64> {
data: tmp_row_data, data: tmp_row_data,
log_base2k: a.basek(), basek: a.basek(),
log_k: a.k(), k: a.k(),
}; };
let (tmp_res_data, scratch2) = scratch1.tmp_vec_znx_dft(module, 2, res.size()); let (tmp_res_data, scratch2) = scratch1.tmp_vec_znx_dft(module, 2, res.size());
let mut tmp_res_row: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier::<&mut [u8], FFT64> { let mut tmp_res_row: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier::<&mut [u8], FFT64> {
data: tmp_res_data, data: tmp_res_data,
log_base2k: res.basek(), basek: res.basek(),
log_k: res.k(), k: res.k(),
}; };
let min_rows: usize = res.rows().min(a.rows()); let min_rows: usize = res.rows().min(a.rows());
(0..res.rows()).for_each(|row_i| { (0..res.rows()).for_each(|row_i| {
(0..res.rank()).for_each(|col_j| { (0..res.cols()).for_each(|col_j| {
a.get_row(module, row_i, col_j, &mut tmp_a_row); a.get_row(module, row_i, col_j, &mut tmp_a_row);
self.prod_with_glwe_fourier(module, &mut tmp_res_row, &tmp_a_row, scratch2); self.prod_with_glwe_fourier(module, &mut tmp_res_row, &tmp_a_row, scratch2);
res.set_row(module, row_i, col_j, &tmp_res_row); res.set_row(module, row_i, col_j, &tmp_res_row);
@@ -168,7 +169,7 @@ pub(crate) trait VecGLWEProduct: Infos {
tmp_res_row.data.zero(); tmp_res_row.data.zero();
(min_rows..res.rows()).for_each(|row_i| { (min_rows..res.rows()).for_each(|row_i| {
(0..self.rank()).for_each(|col_j| { (0..self.cols()).for_each(|col_j| {
res.set_row(module, row_i, col_j, &tmp_res_row); res.set_row(module, row_i, col_j, &tmp_res_row);
}); });
}); });
@@ -182,12 +183,12 @@ pub(crate) trait VecGLWEProduct: Infos {
let mut tmp_row: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier::<&mut [u8], FFT64> { let mut tmp_row: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier::<&mut [u8], FFT64> {
data: tmp_row_data, data: tmp_row_data,
log_base2k: res.basek(), basek: res.basek(),
log_k: res.k(), k: res.k(),
}; };
(0..res.rows()).for_each(|row_i| { (0..res.rows()).for_each(|row_i| {
(0..res.rank()).for_each(|col_j| { (0..res.cols()).for_each(|col_j| {
res.get_row(module, row_i, col_j, &mut tmp_row); res.get_row(module, row_i, col_j, &mut tmp_row);
self.prod_with_glwe_fourier_inplace(module, &mut tmp_row, scratch1); self.prod_with_glwe_fourier_inplace(module, &mut tmp_row, scratch1);
res.set_row(module, row_i, col_j, &tmp_row); res.set_row(module, row_i, col_j, &tmp_row);