Add schemes (#71)

* Move br + cbt to schemes/tfhe

* refactor blind rotation

* refactor circuit bootstrapping

* renamed exec -> prepared
This commit is contained in:
Jean-Philippe Bossuat
2025-08-15 15:06:26 +02:00
committed by GitHub
parent 8d9897b88b
commit c7219c35e9
130 changed files with 2631 additions and 3270 deletions

11
Cargo.lock generated
View File

@@ -563,6 +563,17 @@ dependencies = [
"rand_distr", "rand_distr",
] ]
[[package]]
name = "schemes"
version = "0.1.0"
dependencies = [
"backend",
"byteorder",
"core",
"itertools 0.14.0",
"sampling",
]
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.219" version = "1.0.219"

View File

@@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["backend", "core", "sampling", "utils"] members = ["backend", "core", "sampling", "utils", "schemes"]
resolver = "3" resolver = "3"
[workspace.dependencies] [workspace.dependencies]

View File

@@ -115,12 +115,12 @@ In addition to providing a general purpose FHE library over a unified plaintext
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
} }
pub struct GLWESecretExec<D: Data, B: Backend> { pub struct GLWESecrecPrepared<D: Data, B: Backend> {
pub(crate) data: SvpPPol<D, B>, pub(crate) data: SvpPPol<D, B>,
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
} }
impl<D: DataMut, B: Backend> GLWESecretExec<D, B> { impl<D: DataMut, B: Backend> GLWESecretPrepared<D, B> {
pub fn prepare<O>(&mut self, module: &Module<B>, sk: &GLWESecret<O>) pub fn prepare<O>(&mut self, module: &Module<B>, sk: &GLWESecret<O>)
where where
O: DataRef, O: DataRef,

View File

@@ -3,8 +3,7 @@ use backend::{
api::{ api::{
ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyInplace, SvpPPolAlloc, SvpPrepare, VecZnxAddNormal, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyInplace, SvpPPolAlloc, SvpPrepare, VecZnxAddNormal,
VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace,
VecZnxDecodeVeci64, VecZnxDftAlloc, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigTmpA, VecZnxEncodeVeci64, VecZnxDftAlloc, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigTmpA, VecZnxFillUniform, VecZnxNormalizeInplace, ZnxInfos,
VecZnxFillUniform, VecZnxNormalizeInplace, ZnxInfos,
}, },
layouts::{Module, ScalarZnx, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft}, layouts::{Module, ScalarZnx, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft},
}, },
@@ -73,7 +72,7 @@ fn main() {
let mut want: Vec<i64> = vec![0; n]; let mut want: Vec<i64> = vec![0; n];
want.iter_mut() want.iter_mut()
.for_each(|x| *x = source.next_u64n(16, 15) as i64); .for_each(|x| *x = source.next_u64n(16, 15) as i64);
module.encode_vec_i64(basek, &mut m, 0, log_scale, &want, 4); m.encode_vec_i64(basek, 0, log_scale, &want, 4);
module.vec_znx_normalize_inplace(basek, &mut m, 0, scratch.borrow()); module.vec_znx_normalize_inplace(basek, &mut m, 0, scratch.borrow());
// m - BIG(ct[1] * s) // m - BIG(ct[1] * s)
@@ -132,8 +131,7 @@ fn main() {
// have = m * 2^{log_scale} + e // have = m * 2^{log_scale} + e
let mut have: Vec<i64> = vec![i64::default(); n]; let mut have: Vec<i64> = vec![i64::default(); n];
module.decode_vec_i64(basek, &mut res, 0, ct_size * basek, &mut have); res.decode_vec_i64(basek, 0, ct_size * basek, &mut have);
let scale: f64 = (1 << (res.size() * basek - log_scale)) as f64; let scale: f64 = (1 << (res.size() * basek - log_scale)) as f64;
izip!(want.iter(), have.iter()) izip!(want.iter(), have.iter())
.enumerate() .enumerate()

View File

@@ -1,5 +1,4 @@
use rand_distr::Distribution; use rand_distr::Distribution;
use rug::Float;
use sampling::source::Source; use sampling::source::Source;
use crate::hal::layouts::{Backend, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef}; use crate::hal::layouts::{Backend, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef};
@@ -198,13 +197,6 @@ pub trait VecZnxCopy {
A: VecZnxToRef; A: VecZnxToRef;
} }
pub trait VecZnxStd {
/// Returns the standard devaition of the i-th polynomial.
fn vec_znx_std<A>(&self, basek: usize, a: &A, a_col: usize) -> f64
where
A: VecZnxToRef;
}
pub trait VecZnxFillUniform { pub trait VecZnxFillUniform {
/// Fills the first `size` size with uniform values in \[-2^{basek-1}, 2^{basek-1}\] /// Fills the first `size` size with uniform values in \[-2^{basek-1}, 2^{basek-1}\]
fn vec_znx_fill_uniform<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) fn vec_znx_fill_uniform<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source)
@@ -269,75 +261,3 @@ pub trait VecZnxAddNormal {
) where ) where
R: VecZnxToMut; R: VecZnxToMut;
} }
pub trait VecZnxEncodeVeci64 {
/// encode a vector of i64 on the receiver.
///
/// # Arguments
///
/// * `col_i`: the index of the poly where to encode the data.
/// * `basek`: base two negative logarithm decomposition of the receiver.
/// * `k`: base two negative logarithm of the scaling of the data.
/// * `data`: data to encode on the receiver.
/// * `log_max`: base two logarithm of the infinity norm of the input data.
fn encode_vec_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, data: &[i64], log_max: usize)
where
R: VecZnxToMut;
}
pub trait VecZnxEncodeCoeffsi64 {
/// encodes a single i64 on the receiver at the given index.
///
/// # Arguments
///
/// * `res_col`: the index of the poly where to encode the data.
/// * `basek`: base two negative logarithm decomposition of the receiver.
/// * `k`: base two negative logarithm of the scaling of the data.
/// * `i`: index of the coefficient on which to encode the data.
/// * `data`: data to encode on the receiver.
/// * `log_max`: base two logarithm of the infinity norm of the input data.
fn encode_coeff_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, i: usize, data: i64, log_max: usize)
where
R: VecZnxToMut;
}
pub trait VecZnxDecodeVeci64 {
/// decode a vector of i64 from the receiver.
///
/// # Arguments
///
/// * `res_col`: the index of the poly where to encode the data.
/// * `basek`: base two negative logarithm decomposition of the receiver.
/// * `k`: base two logarithm of the scaling of the data.
/// * `data`: data to decode from the receiver.
fn decode_vec_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, data: &mut [i64])
where
R: VecZnxToRef;
}
pub trait VecZnxDecodeCoeffsi64 {
/// decode a single of i64 from the receiver at the given index.
///
/// # Arguments
///
/// * `res_col`: the index of the poly where to encode the data.
/// * `basek`: base two negative logarithm decomposition of the receiver.
/// * `k`: base two negative logarithm of the scaling of the data.
/// * `i`: index of the coefficient to decode.
/// * `data`: data to decode from the receiver.
fn decode_coeff_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, i: usize) -> i64
where
R: VecZnxToRef;
}
pub trait VecZnxDecodeVecFloat {
/// decode a vector of Float from the receiver.
///
/// # Arguments
/// * `col_i`: the index of the poly where to encode the data.
/// * `basek`: base two negative logarithm decomposition of the receiver.
/// * `data`: data to decode from the receiver.
fn decode_vec_float<R>(&self, basek: usize, res: &R, col_i: usize, data: &mut [Float])
where
R: VecZnxToRef;
}

View File

@@ -3,22 +3,19 @@ use sampling::source::Source;
use crate::hal::{ use crate::hal::{
api::{ api::{
VecZnxAdd, VecZnxAddDistF64, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAdd, VecZnxAddDistF64, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism,
VecZnxAutomorphismInplace, VecZnxCopy, VecZnxDecodeCoeffsi64, VecZnxDecodeVecFloat, VecZnxDecodeVeci64, VecZnxAutomorphismInplace, VecZnxCopy, VecZnxFillDistF64, VecZnxFillNormal, VecZnxFillUniform, VecZnxLshInplace,
VecZnxEncodeCoeffsi64, VecZnxEncodeVeci64, VecZnxFillDistF64, VecZnxFillNormal, VecZnxFillUniform, VecZnxLshInplace,
VecZnxMerge, VecZnxMulXpMinusOne, VecZnxMulXpMinusOneInplace, VecZnxNegate, VecZnxNegateInplace, VecZnxNormalize, VecZnxMerge, VecZnxMulXpMinusOne, VecZnxMulXpMinusOneInplace, VecZnxNegate, VecZnxNegateInplace, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSplit, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSplit,
VecZnxStd, VecZnxSub, VecZnxSubABInplace, VecZnxSubBAInplace, VecZnxSubScalarInplace, VecZnxSwithcDegree, VecZnxSub, VecZnxSubABInplace, VecZnxSubBAInplace, VecZnxSubScalarInplace, VecZnxSwithcDegree,
}, },
layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef}, layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef},
oep::{ oep::{
VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl, VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl,
VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxDecodeCoeffsi64Impl, VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxFillDistF64Impl, VecZnxFillNormalImpl,
VecZnxDecodeVecFloatImpl, VecZnxDecodeVeci64Impl, VecZnxEncodeCoeffsi64Impl, VecZnxEncodeVeci64Impl, VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl,
VecZnxFillDistF64Impl, VecZnxFillNormalImpl, VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl,
VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl, VecZnxRotateImpl, VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxSubABInplaceImpl,
VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl, VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, VecZnxSubBAInplaceImpl, VecZnxSubImpl, VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl,
VecZnxSplitImpl, VecZnxStdImpl, VecZnxSubABInplaceImpl, VecZnxSubBAInplaceImpl, VecZnxSubImpl,
VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl,
}, },
}; };
@@ -325,18 +322,6 @@ where
} }
} }
impl<B> VecZnxStd for Module<B>
where
B: Backend + VecZnxStdImpl<B>,
{
fn vec_znx_std<A>(&self, basek: usize, a: &A, a_col: usize) -> f64
where
A: VecZnxToRef,
{
B::vec_znx_std_impl(self, basek, a, a_col)
}
}
impl<B> VecZnxFillUniform for Module<B> impl<B> VecZnxFillUniform for Module<B>
where where
B: Backend + VecZnxFillUniformImpl<B>, B: Backend + VecZnxFillUniformImpl<B>,
@@ -428,63 +413,3 @@ where
B::vec_znx_add_normal_impl(self, basek, res, res_col, k, source, sigma, bound); B::vec_znx_add_normal_impl(self, basek, res, res_col, k, source, sigma, bound);
} }
} }
impl<B> VecZnxEncodeVeci64 for Module<B>
where
B: Backend + VecZnxEncodeVeci64Impl<B>,
{
fn encode_vec_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, data: &[i64], log_max: usize)
where
R: VecZnxToMut,
{
B::encode_vec_i64_impl(self, basek, res, res_col, k, data, log_max);
}
}
impl<B> VecZnxEncodeCoeffsi64 for Module<B>
where
B: Backend + VecZnxEncodeCoeffsi64Impl<B>,
{
fn encode_coeff_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, i: usize, data: i64, log_max: usize)
where
R: VecZnxToMut,
{
B::encode_coeff_i64_impl(self, basek, res, res_col, k, i, data, log_max);
}
}
impl<B> VecZnxDecodeVeci64 for Module<B>
where
B: Backend + VecZnxDecodeVeci64Impl<B>,
{
fn decode_vec_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, data: &mut [i64])
where
R: VecZnxToRef,
{
B::decode_vec_i64_impl(self, basek, res, res_col, k, data);
}
}
impl<B> VecZnxDecodeCoeffsi64 for Module<B>
where
B: Backend + VecZnxDecodeCoeffsi64Impl<B>,
{
fn decode_coeff_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, i: usize) -> i64
where
R: VecZnxToRef,
{
B::decode_coeff_i64_impl(self, basek, res, res_col, k, i)
}
}
impl<B> VecZnxDecodeVecFloat for Module<B>
where
B: Backend + VecZnxDecodeVecFloatImpl<B>,
{
fn decode_vec_float<R>(&self, basek: usize, res: &R, col_i: usize, data: &mut [rug::Float])
where
R: VecZnxToRef,
{
B::decode_vec_float_impl(self, basek, res, col_i, data);
}
}

View File

@@ -0,0 +1,204 @@
use itertools::izip;
use rug::{Assign, Float};
use crate::hal::{
api::{ZnxInfos, ZnxView, ZnxViewMut, ZnxZero},
layouts::{DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef},
};
impl<D: DataMut> VecZnx<D> {
pub fn encode_vec_i64(&mut self, basek: usize, col: usize, k: usize, data: &[i64], log_max: usize) {
let size: usize = k.div_ceil(basek);
#[cfg(debug_assertions)]
{
let a: VecZnx<&mut [u8]> = self.to_mut();
assert!(
size <= a.size(),
"invalid argument k: k.div_ceil(basek)={} > a.size()={}",
size,
a.size()
);
assert!(col < a.cols());
assert!(data.len() <= a.n())
}
let data_len: usize = data.len();
let mut a: VecZnx<&mut [u8]> = self.to_mut();
let k_rem: usize = basek - (k % basek);
// Zeroes coefficients of the i-th column
(0..a.size()).for_each(|i| {
a.zero_at(col, i);
});
// If 2^{basek} * 2^{k_rem} < 2^{63}-1, then we can simply copy
// values on the last limb.
// Else we decompose values base2k.
if log_max + k_rem < 63 || k_rem == basek {
a.at_mut(col, size - 1)[..data_len].copy_from_slice(&data[..data_len]);
} else {
let mask: i64 = (1 << basek) - 1;
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size)
.rev()
.enumerate()
.for_each(|(i, i_rev)| {
let shift: usize = i * basek;
izip!(a.at_mut(col, i_rev).iter_mut(), data.iter()).for_each(|(y, x)| *y = (x >> shift) & mask);
})
}
// Case where self.prec % self.k != 0.
if k_rem != basek {
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size).rev().for_each(|i| {
a.at_mut(col, i)[..data_len]
.iter_mut()
.for_each(|x| *x <<= k_rem);
})
}
}
pub fn encode_coeff_i64(&mut self, basek: usize, col: usize, k: usize, idx: usize, data: i64, log_max: usize) {
let size: usize = k.div_ceil(basek);
#[cfg(debug_assertions)]
{
let a: VecZnx<&mut [u8]> = self.to_mut();
assert!(idx < a.n());
assert!(
size <= a.size(),
"invalid argument k: k.div_ceil(basek)={} > a.size()={}",
size,
a.size()
);
assert!(col < a.cols());
}
let k_rem: usize = basek - (k % basek);
let mut a: VecZnx<&mut [u8]> = self.to_mut();
(0..a.size()).for_each(|j| a.at_mut(col, j)[idx] = 0);
// If 2^{basek} * 2^{k_rem} < 2^{63}-1, then we can simply copy
// values on the last limb.
// Else we decompose values base2k.
if log_max + k_rem < 63 || k_rem == basek {
a.at_mut(col, size - 1)[idx] = data;
} else {
let mask: i64 = (1 << basek) - 1;
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size)
.rev()
.enumerate()
.for_each(|(j, j_rev)| {
a.at_mut(col, j_rev)[idx] = (data >> (j * basek)) & mask;
})
}
// Case where prec % k != 0.
if k_rem != basek {
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size).rev().for_each(|j| {
a.at_mut(col, j)[idx] <<= k_rem;
})
}
}
}
impl<D: DataRef> VecZnx<D> {
pub fn decode_vec_i64(&self, basek: usize, col: usize, k: usize, data: &mut [i64]) {
let size: usize = k.div_ceil(basek);
#[cfg(debug_assertions)]
{
let a: VecZnx<&[u8]> = self.to_ref();
assert!(
data.len() >= a.n(),
"invalid data: data.len()={} < a.n()={}",
data.len(),
a.n()
);
assert!(col < a.cols());
}
let a: VecZnx<&[u8]> = self.to_ref();
data.copy_from_slice(a.at(col, 0));
let rem: usize = basek - (k % basek);
if k < basek {
data.iter_mut().for_each(|x| *x >>= rem);
} else {
(1..size).for_each(|i| {
if i == size - 1 && rem != basek {
let k_rem: usize = basek - rem;
izip!(a.at(col, i).iter(), data.iter_mut()).for_each(|(x, y)| {
*y = (*y << k_rem) + (x >> rem);
});
} else {
izip!(a.at(col, i).iter(), data.iter_mut()).for_each(|(x, y)| {
*y = (*y << basek) + x;
});
}
})
}
}
pub fn decode_coeff_i64(&self, basek: usize, col: usize, k: usize, idx: usize) -> i64 {
#[cfg(debug_assertions)]
{
let a: VecZnx<&[u8]> = self.to_ref();
assert!(idx < a.n());
assert!(col < a.cols())
}
let a: VecZnx<&[u8]> = self.to_ref();
let size: usize = k.div_ceil(basek);
let mut res: i64 = 0;
let rem: usize = basek - (k % basek);
(0..size).for_each(|j| {
let x: i64 = a.at(col, j)[idx];
if j == size - 1 && rem != basek {
let k_rem: usize = basek - rem;
res = (res << k_rem) + (x >> rem);
} else {
res = (res << basek) + x;
}
});
res
}
pub fn decode_vec_float(&self, basek: usize, col: usize, data: &mut [Float]) {
#[cfg(debug_assertions)]
{
let a: VecZnx<&[u8]> = self.to_ref();
assert!(
data.len() >= a.n(),
"invalid data: data.len()={} < a.n()={}",
data.len(),
a.n()
);
assert!(col < a.cols());
}
let a: VecZnx<&[u8]> = self.to_ref();
let size: usize = a.size();
let prec: u32 = (basek * size) as u32;
// 2^{basek}
let base = Float::with_val(prec, (1 << basek) as f64);
// y[i] = sum x[j][i] * 2^{-basek*j}
(0..size).for_each(|i| {
if i == 0 {
izip!(a.at(col, size - i - 1).iter(), data.iter_mut()).for_each(|(x, y)| {
y.assign(*x);
*y /= &base;
});
} else {
izip!(a.at(col, size - i - 1).iter(), data.iter_mut()).for_each(|(x, y)| {
*y += Float::with_val(prec, *x);
*y /= &base;
});
}
});
}
}

View File

@@ -1,8 +1,10 @@
mod encoding;
mod mat_znx; mod mat_znx;
mod module; mod module;
mod scalar_znx; mod scalar_znx;
mod scratch; mod scratch;
mod serialization; mod serialization;
mod stats;
mod svp_ppol; mod svp_ppol;
mod vec_znx; mod vec_znx;
mod vec_znx_big; mod vec_znx_big;

View File

@@ -0,0 +1,32 @@
use rug::{
Float,
float::Round,
ops::{AddAssignRound, DivAssignRound, SubAssignRound},
};
use crate::hal::{
api::ZnxInfos,
layouts::{DataRef, VecZnx},
};
impl<D: DataRef> VecZnx<D> {
pub fn std(&self, basek: usize, col: usize) -> f64 {
let prec: u32 = (self.size() * basek) as u32;
let mut data: Vec<Float> = (0..self.n()).map(|_| Float::with_val(prec, 0)).collect();
self.decode_vec_float(basek, col, &mut data);
// std = sqrt(sum((xi - avg)^2) / n)
let mut avg: Float = Float::with_val(prec, 0);
data.iter().for_each(|x| {
avg.add_assign_round(x, Round::Nearest);
});
avg.div_assign_round(Float::with_val(prec, data.len()), Round::Nearest);
data.iter_mut().for_each(|x| {
x.sub_assign_round(&avg, Round::Nearest);
});
let mut std: Float = Float::with_val(prec, 0);
data.iter().for_each(|x| std += x * x);
std.div_assign_round(Float::with_val(prec, data.len()), Round::Nearest);
std = std.sqrt();
std.to_f64()
}
}

View File

@@ -1,5 +1,4 @@
use rand_distr::Distribution; use rand_distr::Distribution;
use rug::Float;
use sampling::source::Source; use sampling::source::Source;
use crate::hal::layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef}; use crate::hal::layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef};
@@ -288,15 +287,6 @@ pub unsafe trait VecZnxCopyImpl<B: Backend> {
A: VecZnxToRef; A: VecZnxToRef;
} }
/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
/// * See [crate::hal::api::VecZnxStd] for corresponding public API.
/// * See [crate::doc::backend_safety] for safety contract.
pub unsafe trait VecZnxStdImpl<B: Backend> {
fn vec_znx_std_impl<A>(module: &Module<B>, basek: usize, a: &A, a_col: usize) -> f64
where
A: VecZnxToRef;
}
/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
/// * See [crate::hal::api::VecZnxFillUniform] for corresponding public API. /// * See [crate::hal::api::VecZnxFillUniform] for corresponding public API.
/// * See [crate::doc::backend_safety] for safety contract. /// * See [crate::doc::backend_safety] for safety contract.
@@ -373,68 +363,3 @@ pub unsafe trait VecZnxAddNormalImpl<B: Backend> {
) where ) where
R: VecZnxToMut; R: VecZnxToMut;
} }
/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
/// * See \[TODO\] for reference code.
/// * See [crate::hal::api::VecZnxEncodeVeci64] for corresponding public API.
/// * See [crate::doc::backend_safety] for safety contract.
pub unsafe trait VecZnxEncodeVeci64Impl<B: Backend> {
fn encode_vec_i64_impl<R>(
module: &Module<B>,
basek: usize,
res: &mut R,
res_col: usize,
k: usize,
data: &[i64],
log_max: usize,
) where
R: VecZnxToMut;
}
/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
/// * See \[TODO\] for reference code.
/// * See [crate::hal::api::VecZnxEncodeCoeffsi64] for corresponding public API.
/// * See [crate::doc::backend_safety] for safety contract.
pub unsafe trait VecZnxEncodeCoeffsi64Impl<B: Backend> {
fn encode_coeff_i64_impl<R>(
module: &Module<B>,
basek: usize,
res: &mut R,
res_col: usize,
k: usize,
i: usize,
data: i64,
log_max: usize,
) where
R: VecZnxToMut;
}
/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
/// * See \[TODO\] for reference code.
/// * See [crate::hal::api::VecZnxDecodeVeci64] for corresponding public API.
/// * See [crate::doc::backend_safety] for safety contract.
pub unsafe trait VecZnxDecodeVeci64Impl<B: Backend> {
fn decode_vec_i64_impl<R>(module: &Module<B>, basek: usize, res: &R, res_col: usize, k: usize, data: &mut [i64])
where
R: VecZnxToRef;
}
/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
/// * See \[TODO\] for reference code.
/// * See [crate::hal::api::VecZnxDecodeCoeffsi64] for corresponding public API.
/// * See [crate::doc::backend_safety] for safety contract.
pub unsafe trait VecZnxDecodeCoeffsi64Impl<B: Backend> {
fn decode_coeff_i64_impl<R>(module: &Module<B>, basek: usize, res: &R, res_col: usize, k: usize, i: usize) -> i64
where
R: VecZnxToRef;
}
/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
/// * See \[TODO\] for reference code.
/// * See [crate::hal::api::VecZnxDecodeVecFloat] for corresponding public API.
/// * See [crate::doc::backend_safety] for safety contract.
pub unsafe trait VecZnxDecodeVecFloatImpl<B: Backend> {
fn decode_vec_float_impl<R>(module: &Module<B>, basek: usize, res: &R, res_col: usize, data: &mut [Float])
where
R: VecZnxToRef;
}

View File

@@ -0,0 +1,52 @@
use sampling::source::Source;
use crate::hal::{
api::{ZnxInfos, ZnxViewMut},
layouts::VecZnx,
};
pub fn test_vec_znx_encode_vec_i64_lo_norm() {
let n: usize = 32;
let basek: usize = 17;
let size: usize = 5;
let k: usize = size * basek - 5;
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(n, 2, size);
let mut source: Source = Source::new([0u8; 32]);
let raw: &mut [i64] = a.raw_mut();
raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
(0..a.cols()).for_each(|col_i| {
let mut have: Vec<i64> = vec![i64::default(); n];
have.iter_mut()
.for_each(|x| *x = (source.next_i64() << 56) >> 56);
a.encode_vec_i64(basek, col_i, k, &have, 10);
let mut want: Vec<i64> = vec![i64::default(); n];
a.decode_vec_i64(basek, col_i, k, &mut want);
assert_eq!(have, want, "{:?} != {:?}", &have, &want);
});
}
pub fn test_vec_znx_encode_vec_i64_hi_norm() {
let n: usize = 32;
let basek: usize = 17;
let size: usize = 5;
for k in [1, basek / 2, size * basek - 5] {
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(n, 2, size);
let mut source = Source::new([0u8; 32]);
let raw: &mut [i64] = a.raw_mut();
raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
(0..a.cols()).for_each(|col_i| {
let mut have: Vec<i64> = vec![i64::default(); n];
have.iter_mut().for_each(|x| {
if k < 64 {
*x = source.next_u64n(1 << k, (1 << k) - 1) as i64;
} else {
*x = source.next_i64();
}
});
a.encode_vec_i64(basek, col_i, k, &have, 63);
let mut want: Vec<i64> = vec![i64::default(); n];
a.decode_vec_i64(basek, col_i, k, &mut want);
assert_eq!(have, want, "{:?} != {:?}", &have, &want);
})
}
}

View File

@@ -1,14 +1,13 @@
use itertools::izip;
use sampling::source::Source; use sampling::source::Source;
use crate::hal::{ use crate::hal::{
api::{VecZnxAddNormal, VecZnxDecodeVeci64, VecZnxEncodeVeci64, VecZnxFillUniform, VecZnxStd, ZnxInfos, ZnxView, ZnxViewMut}, api::{VecZnxAddNormal, VecZnxFillUniform, ZnxView},
layouts::{Backend, Module, VecZnx}, layouts::{Backend, Module, VecZnx},
}; };
pub fn test_vec_znx_fill_uniform<B: Backend>(module: &Module<B>) pub fn test_vec_znx_fill_uniform<B: Backend>(module: &Module<B>)
where where
Module<B>: VecZnxFillUniform + VecZnxStd, Module<B>: VecZnxFillUniform,
{ {
let n: usize = module.n(); let n: usize = module.n();
let basek: usize = 17; let basek: usize = 17;
@@ -26,7 +25,7 @@ where
assert_eq!(a.at(col_j, limb_i), zero); assert_eq!(a.at(col_j, limb_i), zero);
}) })
} else { } else {
let std: f64 = module.vec_znx_std(basek, &a, col_i); let std: f64 = a.std(basek, col_i);
assert!( assert!(
(std - one_12_sqrt).abs() < 0.01, (std - one_12_sqrt).abs() < 0.01,
"std={} ~!= {}", "std={} ~!= {}",
@@ -40,7 +39,7 @@ where
pub fn test_vec_znx_add_normal<B: Backend>(module: &Module<B>) pub fn test_vec_znx_add_normal<B: Backend>(module: &Module<B>)
where where
Module<B>: VecZnxAddNormal + VecZnxStd, Module<B>: VecZnxAddNormal,
{ {
let n: usize = module.n(); let n: usize = module.n();
let basek: usize = 17; let basek: usize = 17;
@@ -61,61 +60,9 @@ where
assert_eq!(a.at(col_j, limb_i), zero); assert_eq!(a.at(col_j, limb_i), zero);
}) })
} else { } else {
let std: f64 = module.vec_znx_std(basek, &a, col_i) * k_f64; let std: f64 = a.std(basek, col_i) * k_f64;
assert!((std - sigma).abs() < 0.1, "std={} ~!= {}", std, sigma); assert!((std - sigma).abs() < 0.1, "std={} ~!= {}", std, sigma);
} }
}) })
}); });
} }
pub fn test_vec_znx_encode_vec_i64_lo_norm<B: Backend>(module: &Module<B>)
where
Module<B>: VecZnxEncodeVeci64 + VecZnxDecodeVeci64,
{
let n: usize = module.n();
let basek: usize = 17;
let size: usize = 5;
let k: usize = size * basek - 5;
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(n, 2, size);
let mut source: Source = Source::new([0u8; 32]);
let raw: &mut [i64] = a.raw_mut();
raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
(0..a.cols()).for_each(|col_i| {
let mut have: Vec<i64> = vec![i64::default(); n];
have.iter_mut()
.for_each(|x| *x = (source.next_i64() << 56) >> 56);
module.encode_vec_i64(basek, &mut a, col_i, k, &have, 10);
let mut want: Vec<i64> = vec![i64::default(); n];
module.decode_vec_i64(basek, &a, col_i, k, &mut want);
izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
});
}
pub fn test_vec_znx_encode_vec_i64_hi_norm<B: Backend>(module: &Module<B>)
where
Module<B>: VecZnxEncodeVeci64 + VecZnxDecodeVeci64,
{
let n: usize = module.n();
let basek: usize = 17;
let size: usize = 5;
for k in [1, basek / 2, size * basek - 5] {
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(n, 2, size);
let mut source = Source::new([0u8; 32]);
let raw: &mut [i64] = a.raw_mut();
raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
(0..a.cols()).for_each(|col_i| {
let mut have: Vec<i64> = vec![i64::default(); n];
have.iter_mut().for_each(|x| {
if k < 64 {
*x = source.next_u64n(1 << k, (1 << k) - 1) as i64;
} else {
*x = source.next_i64();
}
});
module.encode_vec_i64(basek, &mut a, col_i, k, &have, 63);
let mut want: Vec<i64> = vec![i64::default(); n];
module.decode_vec_i64(basek, &a, col_i, k, &mut want);
izip!(want, have).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
})
}
}

View File

@@ -1,2 +1,5 @@
mod generics; mod generics;
pub use generics::*; pub use generics::*;
#[cfg(test)]
mod encoding;

View File

@@ -2,10 +2,7 @@ use crate::{
hal::{ hal::{
api::ModuleNew, api::ModuleNew,
layouts::Module, layouts::Module,
tests::vec_znx::{ tests::vec_znx::{test_vec_znx_add_normal, test_vec_znx_fill_uniform},
test_vec_znx_add_normal, test_vec_znx_encode_vec_i64_hi_norm, test_vec_znx_encode_vec_i64_lo_norm,
test_vec_znx_fill_uniform,
},
}, },
implementation::cpu_spqlios::FFT64, implementation::cpu_spqlios::FFT64,
}; };
@@ -21,15 +18,3 @@ fn test_vec_znx_add_normal_fft64() {
let module: Module<FFT64> = Module::<FFT64>::new(1 << 12); let module: Module<FFT64> = Module::<FFT64>::new(1 << 12);
test_vec_znx_add_normal(&module); test_vec_znx_add_normal(&module);
} }
#[test]
fn test_vec_znx_encode_vec_lo_norm_fft64() {
let module: Module<FFT64> = Module::<FFT64>::new(1 << 8);
test_vec_znx_encode_vec_i64_lo_norm(&module);
}
#[test]
fn test_vec_znx_encode_vec_hi_norm_fft64() {
let module: Module<FFT64> = Module::<FFT64>::new(1 << 8);
test_vec_znx_encode_vec_i64_hi_norm(&module);
}

View File

@@ -1,29 +1,22 @@
use itertools::izip; use itertools::izip;
use rand_distr::Normal; use rand_distr::Normal;
use rug::{
Assign, Float,
float::Round,
ops::{AddAssignRound, DivAssignRound, SubAssignRound},
};
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
hal::{ hal::{
api::{ api::{
TakeSlice, TakeVecZnx, VecZnxAddDistF64, VecZnxCopy, VecZnxDecodeVecFloat, VecZnxFillDistF64, TakeSlice, TakeVecZnx, VecZnxAddDistF64, VecZnxCopy, VecZnxFillDistF64, VecZnxNormalizeTmpBytes, VecZnxRotate,
VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxSwithcDegree, ZnxInfos, ZnxSliceSize, ZnxView, VecZnxRotateInplace, VecZnxSwithcDegree, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut, ZnxZero,
ZnxViewMut, ZnxZero,
}, },
layouts::{Backend, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, VecZnxToMut, VecZnxToRef}, layouts::{Backend, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, VecZnxToMut, VecZnxToRef},
oep::{ oep::{
VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl, VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl,
VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxDecodeCoeffsi64Impl, VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxFillDistF64Impl, VecZnxFillNormalImpl,
VecZnxDecodeVecFloatImpl, VecZnxDecodeVeci64Impl, VecZnxEncodeCoeffsi64Impl, VecZnxEncodeVeci64Impl, VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxMulXpMinusOneImpl,
VecZnxFillDistF64Impl, VecZnxFillNormalImpl, VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxMulXpMinusOneInplaceImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl,
VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl, VecZnxRotateInplaceImpl,
VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl, VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxSubABInplaceImpl, VecZnxSubBAInplaceImpl, VecZnxSubImpl,
VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxStdImpl, VecZnxSubABInplaceImpl, VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl,
VecZnxSubBAInplaceImpl, VecZnxSubImpl, VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl,
}, },
}, },
implementation::cpu_spqlios::{ implementation::cpu_spqlios::{
@@ -857,35 +850,6 @@ where
}) })
} }
unsafe impl<B: Backend> VecZnxStdImpl<B> for B
where
B: CPUAVX,
{
fn vec_znx_std_impl<A>(module: &Module<B>, basek: usize, a: &A, a_col: usize) -> f64
where
A: VecZnxToRef,
{
let a: VecZnx<&[u8]> = a.to_ref();
let prec: u32 = (a.size() * basek) as u32;
let mut data: Vec<Float> = (0..a.n()).map(|_| Float::with_val(prec, 0)).collect();
module.decode_vec_float(basek, &a, a_col, &mut data);
// std = sqrt(sum((xi - avg)^2) / n)
let mut avg: Float = Float::with_val(prec, 0);
data.iter().for_each(|x| {
avg.add_assign_round(x, Round::Nearest);
});
avg.div_assign_round(Float::with_val(prec, data.len()), Round::Nearest);
data.iter_mut().for_each(|x| {
x.sub_assign_round(&avg, Round::Nearest);
});
let mut std: Float = Float::with_val(prec, 0);
data.iter().for_each(|x| std += x * x);
std.div_assign_round(Float::with_val(prec, data.len()), Round::Nearest);
std = std.sqrt();
std.to_f64()
}
}
unsafe impl<B: Backend> VecZnxFillUniformImpl<B> for B unsafe impl<B: Backend> VecZnxFillUniformImpl<B> for B
where where
B: CPUAVX, B: CPUAVX,
@@ -1053,251 +1017,3 @@ where
); );
} }
} }
unsafe impl<B: Backend> VecZnxEncodeVeci64Impl<B> for B
where
B: CPUAVX,
{
fn encode_vec_i64_impl<R>(
_module: &Module<B>,
basek: usize,
res: &mut R,
res_col: usize,
k: usize,
data: &[i64],
log_max: usize,
) where
R: VecZnxToMut,
{
let size: usize = k.div_ceil(basek);
#[cfg(debug_assertions)]
{
let a: VecZnx<&mut [u8]> = res.to_mut();
assert!(
size <= a.size(),
"invalid argument k: k.div_ceil(basek)={} > a.size()={}",
size,
a.size()
);
assert!(res_col < a.cols());
assert!(data.len() <= a.n())
}
let data_len: usize = data.len();
let mut a: VecZnx<&mut [u8]> = res.to_mut();
let k_rem: usize = basek - (k % basek);
// Zeroes coefficients of the i-th column
(0..a.size()).for_each(|i| {
a.zero_at(res_col, i);
});
// If 2^{basek} * 2^{k_rem} < 2^{63}-1, then we can simply copy
// values on the last limb.
// Else we decompose values base2k.
if log_max + k_rem < 63 || k_rem == basek {
a.at_mut(res_col, size - 1)[..data_len].copy_from_slice(&data[..data_len]);
} else {
let mask: i64 = (1 << basek) - 1;
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size)
.rev()
.enumerate()
.for_each(|(i, i_rev)| {
let shift: usize = i * basek;
izip!(a.at_mut(res_col, i_rev).iter_mut(), data.iter()).for_each(|(y, x)| *y = (x >> shift) & mask);
})
}
// Case where self.prec % self.k != 0.
if k_rem != basek {
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size).rev().for_each(|i| {
a.at_mut(res_col, i)[..data_len]
.iter_mut()
.for_each(|x| *x <<= k_rem);
})
}
}
}
unsafe impl<B: Backend> VecZnxEncodeCoeffsi64Impl<B> for B
where
B: CPUAVX,
{
fn encode_coeff_i64_impl<R>(
_module: &Module<B>,
basek: usize,
res: &mut R,
res_col: usize,
k: usize,
i: usize,
data: i64,
log_max: usize,
) where
R: VecZnxToMut,
{
let size: usize = k.div_ceil(basek);
#[cfg(debug_assertions)]
{
let a: VecZnx<&mut [u8]> = res.to_mut();
assert!(i < a.n());
assert!(
size <= a.size(),
"invalid argument k: k.div_ceil(basek)={} > a.size()={}",
size,
a.size()
);
assert!(res_col < a.cols());
}
let k_rem: usize = basek - (k % basek);
let mut a: VecZnx<&mut [u8]> = res.to_mut();
(0..a.size()).for_each(|j| a.at_mut(res_col, j)[i] = 0);
// If 2^{basek} * 2^{k_rem} < 2^{63}-1, then we can simply copy
// values on the last limb.
// Else we decompose values base2k.
if log_max + k_rem < 63 || k_rem == basek {
a.at_mut(res_col, size - 1)[i] = data;
} else {
let mask: i64 = (1 << basek) - 1;
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size)
.rev()
.enumerate()
.for_each(|(j, j_rev)| {
a.at_mut(res_col, j_rev)[i] = (data >> (j * basek)) & mask;
})
}
// Case where prec % k != 0.
if k_rem != basek {
let steps: usize = size.min(log_max.div_ceil(basek));
(size - steps..size).rev().for_each(|j| {
a.at_mut(res_col, j)[i] <<= k_rem;
})
}
}
}
unsafe impl<B: Backend> VecZnxDecodeVeci64Impl<B> for B
where
B: CPUAVX,
{
fn decode_vec_i64_impl<R>(_module: &Module<B>, basek: usize, res: &R, res_col: usize, k: usize, data: &mut [i64])
where
R: VecZnxToRef,
{
let size: usize = k.div_ceil(basek);
#[cfg(debug_assertions)]
{
let a: VecZnx<&[u8]> = res.to_ref();
assert!(
data.len() >= a.n(),
"invalid data: data.len()={} < a.n()={}",
data.len(),
a.n()
);
assert!(res_col < a.cols());
}
let a: VecZnx<&[u8]> = res.to_ref();
data.copy_from_slice(a.at(res_col, 0));
let rem: usize = basek - (k % basek);
if k < basek {
data.iter_mut().for_each(|x| *x >>= rem);
} else {
(1..size).for_each(|i| {
if i == size - 1 && rem != basek {
let k_rem: usize = basek - rem;
izip!(a.at(res_col, i).iter(), data.iter_mut()).for_each(|(x, y)| {
*y = (*y << k_rem) + (x >> rem);
});
} else {
izip!(a.at(res_col, i).iter(), data.iter_mut()).for_each(|(x, y)| {
*y = (*y << basek) + x;
});
}
})
}
}
}
unsafe impl<B: Backend> VecZnxDecodeCoeffsi64Impl<B> for B
where
B: CPUAVX,
{
fn decode_coeff_i64_impl<R>(_module: &Module<B>, basek: usize, res: &R, res_col: usize, k: usize, i: usize) -> i64
where
R: VecZnxToRef,
{
#[cfg(debug_assertions)]
{
let a: VecZnx<&[u8]> = res.to_ref();
assert!(i < a.n());
assert!(res_col < a.cols())
}
let a: VecZnx<&[u8]> = res.to_ref();
let size: usize = k.div_ceil(basek);
let mut res: i64 = 0;
let rem: usize = basek - (k % basek);
(0..size).for_each(|j| {
let x: i64 = a.at(res_col, j)[i];
if j == size - 1 && rem != basek {
let k_rem: usize = basek - rem;
res = (res << k_rem) + (x >> rem);
} else {
res = (res << basek) + x;
}
});
res
}
}
unsafe impl<B: Backend> VecZnxDecodeVecFloatImpl<B> for B
where
B: CPUAVX,
{
fn decode_vec_float_impl<R>(_module: &Module<B>, basek: usize, res: &R, res_col: usize, data: &mut [Float])
where
R: VecZnxToRef,
{
#[cfg(debug_assertions)]
{
let a: VecZnx<&[u8]> = res.to_ref();
assert!(
data.len() >= a.n(),
"invalid data: data.len()={} < a.n()={}",
data.len(),
a.n()
);
assert!(res_col < a.cols());
}
let a: VecZnx<&[u8]> = res.to_ref();
let size: usize = a.size();
let prec: u32 = (basek * size) as u32;
// 2^{basek}
let base = Float::with_val(prec, (1 << basek) as f64);
// y[i] = sum x[j][i] * 2^{-basek*j}
(0..size).for_each(|i| {
if i == 0 {
izip!(a.at(res_col, size - i - 1).iter(), data.iter_mut()).for_each(|(x, y)| {
y.assign(*x);
*y /= &base;
});
} else {
izip!(a.at(res_col, size - i - 1).iter(), data.iter_mut()).for_each(|(x, y)| {
*y += Float::with_val(prec, *x);
*y /= &base;
});
}
});
}
}

View File

@@ -1,362 +0,0 @@
use backend::hal::{
api::{
FillUniform, Reset, ScratchAvailable, SvpPPolAlloc, SvpPrepare, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace,
VmpPMatAlloc, VmpPMatPrepare, ZnxInfos, ZnxView, ZnxViewMut,
},
layouts::{Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToRef, Scratch, SvpPPol, WriterTo},
};
use sampling::source::Source;
use crate::{
Distribution, Infos,
layouts::{
GGSWCiphertext, LWESecret,
prepared::{GGSWCiphertextExec, GLWESecretExec},
},
};
use std::fmt;
use crate::trait_families::GGSWEncryptSkFamily;
#[derive(Clone)]
pub struct BlindRotationKeyCGGI<D: Data> {
pub(crate) keys: Vec<GGSWCiphertext<D>>,
pub(crate) dist: Distribution,
}
impl<D: DataRef> fmt::Debug for BlindRotationKeyCGGI<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
impl<D: Data> PartialEq for BlindRotationKeyCGGI<D> {
fn eq(&self, other: &Self) -> bool {
if self.keys.len() != other.keys.len() {
return false;
}
for (a, b) in self.keys.iter().zip(other.keys.iter()) {
if a != b {
return false;
}
}
self.dist == other.dist
}
}
impl<D: Data> Eq for BlindRotationKeyCGGI<D> {}
impl<D: DataRef> fmt::Display for BlindRotationKeyCGGI<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, key) in self.keys.iter().enumerate() {
write!(f, "key[{}]: {}", i, key)?;
}
writeln!(f, "{:?}", self.dist)
}
}
impl<D: DataMut> Reset for BlindRotationKeyCGGI<D> {
fn reset(&mut self) {
self.keys.iter_mut().for_each(|key| key.reset());
self.dist = Distribution::NONE;
}
}
impl<D: DataMut> FillUniform for BlindRotationKeyCGGI<D> {
fn fill_uniform(&mut self, source: &mut sampling::source::Source) {
self.keys
.iter_mut()
.for_each(|key| key.fill_uniform(source));
}
}
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
impl<D: DataMut> ReaderFrom for BlindRotationKeyCGGI<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
match Distribution::read_from(reader) {
Ok(dist) => self.dist = dist,
Err(e) => return Err(e),
}
let len: usize = reader.read_u64::<LittleEndian>()? as usize;
if self.keys.len() != len {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("self.keys.len()={} != read len={}", self.keys.len(), len),
));
}
for key in &mut self.keys {
key.read_from(reader)?;
}
Ok(())
}
}
impl<D: DataRef> WriterTo for BlindRotationKeyCGGI<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
match self.dist.write_to(writer) {
Ok(()) => {}
Err(e) => return Err(e),
}
writer.write_u64::<LittleEndian>(self.keys.len() as u64)?;
for key in &self.keys {
key.write_to(writer)?;
}
Ok(())
}
}
impl BlindRotationKeyCGGI<Vec<u8>> {
pub fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self {
let mut data: Vec<GGSWCiphertext<Vec<u8>>> = Vec::with_capacity(n_lwe);
(0..n_lwe).for_each(|_| data.push(GGSWCiphertext::alloc(n_gglwe, basek, k, rows, 1, rank)));
Self {
keys: data,
dist: Distribution::NONE,
}
}
pub fn generate_from_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where
Module<B>: GGSWEncryptSkFamily<B>,
{
GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k, rank)
}
}
impl<D: DataRef> BlindRotationKeyCGGI<D> {
#[allow(dead_code)]
pub(crate) fn n(&self) -> usize {
self.keys[0].n()
}
#[allow(dead_code)]
pub(crate) fn rows(&self) -> usize {
self.keys[0].rows()
}
#[allow(dead_code)]
pub(crate) fn k(&self) -> usize {
self.keys[0].k()
}
#[allow(dead_code)]
pub(crate) fn size(&self) -> usize {
self.keys[0].size()
}
#[allow(dead_code)]
pub(crate) fn rank(&self) -> usize {
self.keys[0].rank()
}
pub(crate) fn basek(&self) -> usize {
self.keys[0].basek()
}
#[allow(dead_code)]
pub(crate) fn block_size(&self) -> usize {
match self.dist {
Distribution::BinaryBlock(value) => value,
_ => 1,
}
}
}
impl<D: DataMut> BlindRotationKeyCGGI<D> {
pub fn generate_from_sk<DataSkGLWE, DataSkLWE, B: Backend>(
&mut self,
module: &Module<B>,
sk_glwe: &GLWESecretExec<DataSkGLWE, B>,
sk_lwe: &LWESecret<DataSkLWE>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
scratch: &mut Scratch<B>,
) where
DataSkGLWE: DataRef,
DataSkLWE: DataRef,
Module<B>: GGSWEncryptSkFamily<B> + VecZnxAddScalarInplace,
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.keys.len(), sk_lwe.n());
assert!(sk_glwe.n() <= module.n());
assert_eq!(sk_glwe.rank(), self.keys[0].rank());
match sk_lwe.dist {
Distribution::BinaryBlock(_)
| Distribution::BinaryFixed(_)
| Distribution::BinaryProb(_)
| Distribution::ZERO => {}
_ => panic!(
"invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)"
),
}
}
self.dist = sk_lwe.dist;
let mut pt: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(sk_glwe.n(), 1);
let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data.to_ref();
self.keys.iter_mut().enumerate().for_each(|(i, ggsw)| {
pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i];
ggsw.encrypt_sk(module, &pt, sk_glwe, source_xa, source_xe, sigma, scratch);
});
}
}
#[derive(PartialEq, Eq)]
pub struct BlindRotationKeyCGGIExec<D: Data, B: Backend> {
pub(crate) data: Vec<GGSWCiphertextExec<D, B>>,
pub(crate) dist: Distribution,
pub(crate) x_pow_a: Option<Vec<SvpPPol<Vec<u8>, B>>>,
}
impl<D: Data, B: Backend> BlindRotationKeyCGGIExec<D, B> {
#[allow(dead_code)]
pub(crate) fn n(&self) -> usize {
self.data[0].n()
}
#[allow(dead_code)]
pub(crate) fn rows(&self) -> usize {
self.data[0].rows()
}
#[allow(dead_code)]
pub(crate) fn k(&self) -> usize {
self.data[0].k()
}
#[allow(dead_code)]
pub(crate) fn size(&self) -> usize {
self.data[0].size()
}
#[allow(dead_code)]
pub(crate) fn rank(&self) -> usize {
self.data[0].rank()
}
pub(crate) fn basek(&self) -> usize {
self.data[0].basek()
}
pub(crate) fn block_size(&self) -> usize {
match self.dist {
Distribution::BinaryBlock(value) => value,
_ => 1,
}
}
}
pub trait BlindRotationKeyCGGIExecLayoutFamily<B: Backend> = SvpPPolAlloc<B> + SvpPrepare<B>;
impl<B: Backend> BlindRotationKeyCGGIExec<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self
where
Module<B>: BlindRotationKeyCGGIExecLayoutFamily<B> + VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut data: Vec<GGSWCiphertextExec<Vec<u8>, B>> = Vec::with_capacity(n_lwe);
(0..n_lwe).for_each(|_| {
data.push(GGSWCiphertextExec::alloc(
module, n_glwe, basek, k, rows, 1, rank,
))
});
Self {
data,
dist: Distribution::NONE,
x_pow_a: None,
}
}
pub fn from<DataOther>(module: &Module<B>, other: &BlindRotationKeyCGGI<DataOther>, scratch: &mut Scratch<B>) -> Self
where
DataOther: DataRef,
Module<B>: BlindRotationKeyCGGIExecLayoutFamily<B> + VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut brk: BlindRotationKeyCGGIExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.keys.len(),
other.basek(),
other.k(),
other.rows(),
other.rank(),
);
brk.prepare(module, other, scratch);
brk
}
}
impl<D: DataMut, B: Backend> BlindRotationKeyCGGIExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &BlindRotationKeyCGGI<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: BlindRotationKeyCGGIExecLayoutFamily<B> + VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.data.len(), other.keys.len());
}
let n: usize = other.n();
self.data
.iter_mut()
.zip(other.keys.iter())
.for_each(|(ggsw_exec, other)| {
ggsw_exec.prepare(module, other, scratch);
});
self.dist = other.dist;
match other.dist {
Distribution::BinaryBlock(_) => {
let mut x_pow_a: Vec<SvpPPol<Vec<u8>, B>> = Vec::with_capacity(n << 1);
let mut buf: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
(0..n << 1).for_each(|i| {
let mut res: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(n, 1);
set_xai_plus_y(module, i, 0, &mut res, &mut buf);
x_pow_a.push(res);
});
self.x_pow_a = Some(x_pow_a);
}
_ => {}
}
}
}
pub fn set_xai_plus_y<A, C, B: Backend>(module: &Module<B>, ai: usize, y: i64, res: &mut SvpPPol<A, B>, buf: &mut ScalarZnx<C>)
where
A: DataMut,
C: DataMut,
Module<B>: SvpPrepare<B>,
{
let n: usize = res.n();
{
let raw: &mut [i64] = buf.at_mut(0, 0);
if ai < n {
raw[ai] = 1;
} else {
raw[(ai - n) & (n - 1)] = -1;
}
raw[0] += y;
}
module.svp_prepare(res, 0, buf, 0);
{
let raw: &mut [i64] = buf.at_mut(0, 0);
if ai < n {
raw[ai] = 0;
} else {
raw[(ai - n) & (n - 1)] = 0;
}
raw[0] = 0;
}
}

View File

@@ -1,217 +0,0 @@
use backend::hal::{
api::{FillUniform, Reset, ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, ZnxView, ZnxViewMut},
layouts::{Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToRef, Scratch, WriterTo},
};
use sampling::source::Source;
use crate::{
Distribution, Infos,
layouts::{LWESecret, compressed::GGSWCiphertextCompressed, prepared::GLWESecretExec},
};
use std::fmt;
use crate::trait_families::GGSWEncryptSkFamily;
#[derive(Clone)]
pub struct BlindRotationKeyCGGICompressed<D: Data> {
pub(crate) keys: Vec<GGSWCiphertextCompressed<D>>,
pub(crate) dist: Distribution,
}
impl<D: DataRef> fmt::Debug for BlindRotationKeyCGGICompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
impl<D: Data> PartialEq for BlindRotationKeyCGGICompressed<D> {
fn eq(&self, other: &Self) -> bool {
if self.keys.len() != other.keys.len() {
return false;
}
for (a, b) in self.keys.iter().zip(other.keys.iter()) {
if a != b {
return false;
}
}
self.dist == other.dist
}
}
impl<D: Data> Eq for BlindRotationKeyCGGICompressed<D> {}
impl<D: DataRef> fmt::Display for BlindRotationKeyCGGICompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, key) in self.keys.iter().enumerate() {
write!(f, "key[{}]: {}", i, key)?;
}
writeln!(f, "{:?}", self.dist)
}
}
impl<D: DataMut> Reset for BlindRotationKeyCGGICompressed<D> {
fn reset(&mut self) {
self.keys.iter_mut().for_each(|key| key.reset());
self.dist = Distribution::NONE;
}
}
impl<D: DataMut> FillUniform for BlindRotationKeyCGGICompressed<D> {
fn fill_uniform(&mut self, source: &mut sampling::source::Source) {
self.keys
.iter_mut()
.for_each(|key| key.fill_uniform(source));
}
}
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
impl<D: DataMut> ReaderFrom for BlindRotationKeyCGGICompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
match Distribution::read_from(reader) {
Ok(dist) => self.dist = dist,
Err(e) => return Err(e),
}
let len: usize = reader.read_u64::<LittleEndian>()? as usize;
if self.keys.len() != len {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("self.keys.len()={} != read len={}", self.keys.len(), len),
));
}
for key in &mut self.keys {
key.read_from(reader)?;
}
Ok(())
}
}
impl<D: DataRef> WriterTo for BlindRotationKeyCGGICompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
match self.dist.write_to(writer) {
Ok(()) => {}
Err(e) => return Err(e),
}
writer.write_u64::<LittleEndian>(self.keys.len() as u64)?;
for key in &self.keys {
key.write_to(writer)?;
}
Ok(())
}
}
impl BlindRotationKeyCGGICompressed<Vec<u8>> {
pub fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self {
let mut data: Vec<GGSWCiphertextCompressed<Vec<u8>>> = Vec::with_capacity(n_lwe);
(0..n_lwe).for_each(|_| {
data.push(GGSWCiphertextCompressed::alloc(
n_gglwe, basek, k, rows, 1, rank,
))
});
Self {
keys: data,
dist: Distribution::NONE,
}
}
pub fn generate_from_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where
Module<B>: GGSWEncryptSkFamily<B>,
{
GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank)
}
}
impl<D: DataRef> BlindRotationKeyCGGICompressed<D> {
#[allow(dead_code)]
pub(crate) fn n(&self) -> usize {
self.keys[0].n()
}
#[allow(dead_code)]
pub(crate) fn rows(&self) -> usize {
self.keys[0].rows()
}
#[allow(dead_code)]
pub(crate) fn k(&self) -> usize {
self.keys[0].k()
}
#[allow(dead_code)]
pub(crate) fn size(&self) -> usize {
self.keys[0].size()
}
#[allow(dead_code)]
pub(crate) fn rank(&self) -> usize {
self.keys[0].rank()
}
#[allow(dead_code)]
pub(crate) fn basek(&self) -> usize {
self.keys[0].basek()
}
#[allow(dead_code)]
pub(crate) fn block_size(&self) -> usize {
match self.dist {
Distribution::BinaryBlock(value) => value,
_ => 1,
}
}
}
impl<D: DataMut> BlindRotationKeyCGGICompressed<D> {
pub fn generate_from_sk<DataSkGLWE, DataSkLWE, B: Backend>(
&mut self,
module: &Module<B>,
sk_glwe: &GLWESecretExec<DataSkGLWE, B>,
sk_lwe: &LWESecret<DataSkLWE>,
seed_xa: [u8; 32],
source_xe: &mut Source,
sigma: f64,
scratch: &mut Scratch<B>,
) where
DataSkGLWE: DataRef,
DataSkLWE: DataRef,
Module<B>: GGSWEncryptSkFamily<B> + VecZnxAddScalarInplace,
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.keys.len(), sk_lwe.n());
assert!(sk_glwe.n() <= module.n());
assert_eq!(sk_glwe.rank(), self.keys[0].rank());
match sk_lwe.dist {
Distribution::BinaryBlock(_)
| Distribution::BinaryFixed(_)
| Distribution::BinaryProb(_)
| Distribution::ZERO => {}
_ => panic!(
"invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)"
),
}
}
self.dist = sk_lwe.dist;
let mut pt: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(sk_glwe.n(), 1);
let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data.to_ref();
let mut source_xa: Source = Source::new(seed_xa);
self.keys.iter_mut().enumerate().for_each(|(i, ggsw)| {
pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i];
ggsw.encrypt_sk(
module,
&pt,
sk_glwe,
source_xa.new_seed(),
source_xe,
sigma,
scratch,
);
});
}
}

View File

@@ -1,12 +0,0 @@
mod cggi;
mod key;
mod key_compressed;
mod lut;
pub use cggi::*;
pub use key::*;
pub use key_compressed::*;
pub use lut::*;
#[cfg(test)]
mod tests;

View File

@@ -1,15 +0,0 @@
use backend::hal::tests::serialization::test_reader_writer_interface;
use crate::{BlindRotationKeyCGGI, BlindRotationKeyCGGICompressed};
#[test]
fn test_cggi_blind_rotation_key_serialization() {
let original: BlindRotationKeyCGGI<Vec<u8>> = BlindRotationKeyCGGI::alloc(256, 64, 12, 54, 2, 2);
test_reader_writer_interface(original);
}
#[test]
fn test_cggi_blind_rotation_key_compressed_serialization() {
let original: BlindRotationKeyCGGICompressed<Vec<u8>> = BlindRotationKeyCGGICompressed::alloc(256, 64, 12, 54, 2, 2);
test_reader_writer_interface(original);
}

View File

@@ -1,396 +0,0 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy,
VecZnxStd, VecZnxSubScalarInplace, VecZnxSwithcDegree,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
},
};
use sampling::source::Source;
use crate::{
AutomorphismKey, AutomorphismKeyCompressed, AutomorphismKeyEncryptSkFamily, AutomorphismKeyExec, GGLWEExecLayoutFamily,
GLWEDecryptFamily, GLWEKeyswitchFamily, GLWEPlaintext, GLWESecret, GLWESecretExec, Infos,
noise::log2_std_noise_gglwe_product,
};
pub(crate) trait AutomorphismTestModuleFamily<B: Backend> = AutomorphismKeyEncryptSkFamily<B>
+ GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism
+ GGLWEExecLayoutFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxAutomorphism
+ VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy;
pub(crate) trait AutomorphismTestScratchFamily<B: Backend> = ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxImpl<B>
+ TakeSvpPPolImpl<B>
+ TakeVecZnxBigImpl<B>;
pub(crate) fn test_automorphisk_key_encrypt_sk<B: Backend>(
module: &Module<B>,
basek: usize,
k_ksk: usize,
digits: usize,
rank: usize,
sigma: f64,
) where
Module<B>: AutomorphismTestModuleFamily<B>,
B: AutomorphismTestScratchFamily<B>,
{
let n: usize = TakeScalarZnx;
let rows: usize = (k_ksk - digits * basek) / (digits * basek);
let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
let mut source_xs: 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 scratch: ScratchOwned<B> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space(
module, n, basek, k_ksk, rank,
));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let p = -5;
atk.encrypt_sk(
module,
p,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut sk_out: GLWESecret<Vec<u8>> = sk.clone();
(0..atk.rank()).for_each(|i| {
module.vec_znx_automorphism(
module.galois_element_inv(p),
&mut sk_out.data.as_vec_znx_mut(),
i,
&sk.data.as_vec_znx(),
i,
);
});
let sk_out_exec = GLWESecretExec::from(module, &sk_out);
atk.key
.key
.assert_noise(module, &sk_out_exec, &sk.data, sigma);
}
pub(crate) fn test_automorphisk_key_encrypt_sk_compressed<B: Backend>(
module: &Module<B>,
basek: usize,
k_ksk: usize,
digits: usize,
rank: usize,
sigma: f64,
) where
Module<B>: AutomorphismTestModuleFamily<B>,
B: AutomorphismTestScratchFamily<B>,
{
let n: usize = TakeScalarZnx;
let rows: usize = (k_ksk - digits * basek) / (digits * basek);
let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> =
AutomorphismKeyCompressed::alloc(n, basek, k_ksk, rows, digits, rank);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space(
module, n, basek, k_ksk, rank,
));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let p = -5;
let seed_xa: [u8; 32] = [1u8; 32];
atk_compressed.encrypt_sk(
module,
p,
&sk,
seed_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut sk_out: GLWESecret<Vec<u8>> = sk.clone();
(0..atk_compressed.rank()).for_each(|i| {
module.vec_znx_automorphism(
module.galois_element_inv(p),
&mut sk_out.data.as_vec_znx_mut(),
i,
&sk.data.as_vec_znx(),
i,
);
});
let sk_out_exec = GLWESecretExec::from(module, &sk_out);
let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
atk.decompress(module, &atk_compressed);
atk.key
.key
.assert_noise(module, &sk_out_exec, &sk.data, sigma);
}
pub(crate) fn test_gglwe_automorphism<B: Backend>(
module: &Module<B>,
p0: i64,
p1: i64,
basek: usize,
digits: usize,
k_in: usize,
k_out: usize,
k_apply: usize,
sigma: f64,
rank: usize,
) where
Module<B>: AutomorphismTestModuleFamily<B>,
B: AutomorphismTestScratchFamily<B>,
{
let n: usize = TakeScalarZnx;
let digits_in: usize = 1;
let rows_in: usize = k_in / (basek * digits);
let rows_apply: usize = k_in.div_ceil(basek * digits);
let mut auto_key_in: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(n, basek, k_in, rows_in, digits_in, rank);
let mut auto_key_out: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(n, basek, k_out, rows_in, digits_in, rank);
let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(n, basek, k_apply, rows_apply, digits, rank);
let mut source_xs: 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 scratch: ScratchOwned<B> = ScratchOwned::alloc(
AutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_apply, rank)
| AutomorphismKey::automorphism_scratch_space(module, n, basek, k_out, k_in, k_apply, digits, rank),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(TakeScalarZnx, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
auto_key_in.encrypt_sk(
module,
p0,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
// gglwe_{s2}(s1) -> s1 -> s2
auto_key_apply.encrypt_sk(
module,
p1,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut auto_key_apply_exec: AutomorphismKeyExec<Vec<u8>, B> =
AutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
auto_key_apply_exec.prepare(module, &auto_key_apply, scratch.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
auto_key_out.automorphism(module, &auto_key_in, &auto_key_apply_exec, scratch.borrow());
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(TakeScalarZnx, basek, k_out);
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc(TakeScalarZnx, rank);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
(0..rank).for_each(|i| {
module.vec_znx_automorphism(
module.galois_element_inv(p0 * p1),
&mut sk_auto.data.as_vec_znx_mut(),
i,
&sk.data.as_vec_znx(),
i,
);
});
let sk_auto_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_auto);
(0..auto_key_out.rank_in()).for_each(|col_i| {
(0..auto_key_out.rows()).for_each(|row_i| {
auto_key_out
.at(row_i, col_i)
.decrypt(module, &mut pt, &sk_auto_dft, scratch.borrow());
module.vec_znx_sub_scalar_inplace(
&mut pt.data,
0,
(digits_in - 1) + row_i * digits_in,
&sk.data,
col_i,
);
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2();
let noise_want: f64 = log2_std_noise_gglwe_product(
TakeScalarZnx as f64,
basek * digits,
0.5,
0.5,
0f64,
sigma * sigma,
0f64,
rank as f64,
k_out,
k_apply,
);
assert!(
noise_have < noise_want + 0.5,
"{} {}",
noise_have,
noise_want
);
});
});
}
pub(crate) fn test_gglwe_automorphism_inplace<B: Backend>(
module: &Module<B>,
p0: i64,
p1: i64,
basek: usize,
digits: usize,
k_in: usize,
k_apply: usize,
sigma: f64,
rank: usize,
) where
Module<B>: AutomorphismTestModuleFamily<B>,
B: AutomorphismTestScratchFamily<B>,
{
let n: usize = TakeScalarZnx;
let digits_in: usize = 1;
let rows_in: usize = k_in / (basek * digits);
let rows_apply: usize = k_in.div_ceil(basek * digits);
let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(n, basek, k_in, rows_in, digits_in, rank);
let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(n, basek, k_apply, rows_apply, digits, rank);
let mut source_xs: 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 scratch: ScratchOwned<B> = ScratchOwned::alloc(
AutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_apply, rank)
| AutomorphismKey::automorphism_inplace_scratch_space(module, n, basek, k_in, k_apply, digits, rank),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(TakeScalarZnx, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
auto_key.encrypt_sk(
module,
p0,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
// gglwe_{s2}(s1) -> s1 -> s2
auto_key_apply.encrypt_sk(
module,
p1,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut auto_key_apply_exec: AutomorphismKeyExec<Vec<u8>, B> =
AutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
auto_key_apply_exec.prepare(module, &auto_key_apply, scratch.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
auto_key.automorphism_inplace(module, &auto_key_apply_exec, scratch.borrow());
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(TakeScalarZnx, basek, k_in);
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc(TakeScalarZnx, rank);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
(0..rank).for_each(|i| {
module.vec_znx_automorphism(
module.galois_element_inv(p0 * p1),
&mut sk_auto.data.as_vec_znx_mut(),
i,
&sk.data.as_vec_znx(),
i,
);
});
let sk_auto_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_auto);
(0..auto_key.rank_in()).for_each(|col_i| {
(0..auto_key.rows()).for_each(|row_i| {
auto_key
.at(row_i, col_i)
.decrypt(module, &mut pt, &sk_auto_dft, scratch.borrow());
module.vec_znx_sub_scalar_inplace(
&mut pt.data,
0,
(digits_in - 1) + row_i * digits_in,
&sk.data,
col_i,
);
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2();
let noise_want: f64 = log2_std_noise_gglwe_product(
TakeScalarZnx as f64,
basek * digits,
0.5,
0.5,
0f64,
sigma * sigma,
0f64,
rank as f64,
k_in,
k_apply,
);
assert!(
noise_have < noise_want + 0.5,
"{} {}",
noise_have,
noise_want
);
});
});
}

View File

@@ -1,321 +0,0 @@
use backend::hal::{
api::{ScratchAvailable, SvpPPolAlloc, SvpPrepare, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, ZnxView, ZnxViewMut},
layouts::{Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToRef, Scratch, SvpPPol, WriterTo},
};
use sampling::source::Source;
use crate::{
Distribution, GGSWCiphertext, GGSWCiphertextExec, GGSWEncryptSkFamily, GGSWLayoutFamily, GLWESecretExec, Infos, LWESecret,
};
pub struct BlindRotationKeyCGGI<D: Data> {
pub(crate) keys: Vec<GGSWCiphertext<D>>,
pub(crate) dist: Distribution,
}
impl<D: Data> PartialEq for BlindRotationKeyCGGI<D> {
fn eq(&self, other: &Self) -> bool {
if self.keys.len() != other.keys.len() {
return false;
}
for (a, b) in self.keys.iter().zip(other.keys.iter()) {
if a != b {
return false;
}
}
self.dist == other.dist
}
}
impl<D: Data> Eq for BlindRotationKeyCGGI<D> {}
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
impl<D: DataMut> ReaderFrom for BlindRotationKeyCGGI<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
match Distribution::read_from(reader) {
Ok(dist) => self.dist = dist,
Err(e) => return Err(e),
}
let len: usize = reader.read_u64::<LittleEndian>()? as usize;
if self.keys.len() != len {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("self.keys.len()={} != read len={}", self.keys.len(), len),
));
}
for key in &mut self.keys {
key.read_from(reader)?;
}
Ok(())
}
}
impl<D: DataRef> WriterTo for BlindRotationKeyCGGI<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
match self.dist.write_to(writer) {
Ok(()) => {}
Err(e) => return Err(e),
}
writer.write_u64::<LittleEndian>(self.keys.len() as u64)?;
for key in &self.keys {
key.write_to(writer)?;
}
Ok(())
}
}
impl BlindRotationKeyCGGI<Vec<u8>> {
pub fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self {
let mut data: Vec<GGSWCiphertext<Vec<u8>>> = Vec::with_capacity(n_lwe);
(0..n_lwe).for_each(|_| data.push(GGSWCiphertext::alloc(n_gglwe, basek, k, rows, 1, rank)));
Self {
keys: data,
dist: Distribution::NONE,
}
}
pub fn generate_from_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where
Module<B>: GGSWEncryptSkFamily<B>,
{
GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k, rank)
}
}
impl<D: DataRef> BlindRotationKeyCGGI<D> {
#[allow(dead_code)]
pub(crate) fn n(&self) -> usize {
self.keys[0].n()
}
#[allow(dead_code)]
pub(crate) fn rows(&self) -> usize {
self.keys[0].rows()
}
#[allow(dead_code)]
pub(crate) fn k(&self) -> usize {
self.keys[0].k()
}
#[allow(dead_code)]
pub(crate) fn size(&self) -> usize {
self.keys[0].size()
}
#[allow(dead_code)]
pub(crate) fn rank(&self) -> usize {
self.keys[0].rank()
}
pub(crate) fn basek(&self) -> usize {
self.keys[0].basek()
}
#[allow(dead_code)]
pub(crate) fn block_size(&self) -> usize {
match self.dist {
Distribution::BinaryBlock(value) => value,
_ => 1,
}
}
}
impl<D: DataMut> BlindRotationKeyCGGI<D> {
pub fn generate_from_sk<DataSkGLWE, DataSkLWE, B: Backend>(
&mut self,
module: &Module<B>,
sk_glwe: &GLWESecretExec<DataSkGLWE, B>,
sk_lwe: &LWESecret<DataSkLWE>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
scratch: &mut Scratch<B>,
) where
DataSkGLWE: DataRef,
DataSkLWE: DataRef,
Module<B>: GGSWEncryptSkFamily<B> + VecZnxAddScalarInplace,
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.keys.len(), sk_lwe.n());
assert!(sk_glwe.n() <= TakeScalarZnx);
assert_eq!(sk_glwe.rank(), self.keys[0].rank());
match sk_lwe.dist {
Distribution::BinaryBlock(_)
| Distribution::BinaryFixed(_)
| Distribution::BinaryProb(_)
| Distribution::ZERO => {}
_ => panic!(
"invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)"
),
}
}
self.dist = sk_lwe.dist;
let mut pt: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(sk_glwe.n(), 1);
let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data.to_ref();
self.keys.iter_mut().enumerate().for_each(|(i, ggsw)| {
pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i];
ggsw.encrypt_sk(module, &pt, sk_glwe, source_xa, source_xe, sigma, scratch);
});
}
}
#[derive(PartialEq, Eq)]
pub struct BlindRotationKeyCGGIExec<D: Data, B: Backend> {
pub(crate) data: Vec<GGSWCiphertextExec<D, B>>,
pub(crate) dist: Distribution,
pub(crate) x_pow_a: Option<Vec<SvpPPol<Vec<u8>, B>>>,
}
impl<D: Data, B: Backend> BlindRotationKeyCGGIExec<D, B> {
#[allow(dead_code)]
pub(crate) fn n(&self) -> usize {
self.data[0].n()
}
#[allow(dead_code)]
pub(crate) fn rows(&self) -> usize {
self.data[0].rows()
}
#[allow(dead_code)]
pub(crate) fn k(&self) -> usize {
self.data[0].k()
}
#[allow(dead_code)]
pub(crate) fn size(&self) -> usize {
self.data[0].size()
}
#[allow(dead_code)]
pub(crate) fn rank(&self) -> usize {
self.data[0].rank()
}
pub(crate) fn basek(&self) -> usize {
self.data[0].basek()
}
pub(crate) fn block_size(&self) -> usize {
match self.dist {
Distribution::BinaryBlock(value) => value,
_ => 1,
}
}
}
pub trait BlindRotationKeyCGGIExecLayoutFamily<B: Backend> = GGSWLayoutFamily<B> + SvpPPolAlloc<B> + SvpPrepare<B>;
impl<B: Backend> BlindRotationKeyCGGIExec<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self
where
Module<B>: BlindRotationKeyCGGIExecLayoutFamily<B>,
{
let mut data: Vec<GGSWCiphertextExec<Vec<u8>, B>> = Vec::with_capacity(n_lwe);
(0..n_lwe).for_each(|_| {
data.push(GGSWCiphertextExec::alloc(
module, n_glwe, basek, k, rows, 1, rank,
))
});
Self {
data,
dist: Distribution::NONE,
x_pow_a: None,
}
}
pub fn from<DataOther>(module: &Module<B>, other: &BlindRotationKeyCGGI<DataOther>, scratch: &mut Scratch<B>) -> Self
where
DataOther: DataRef,
Module<B>: BlindRotationKeyCGGIExecLayoutFamily<B>,
{
let mut brk: BlindRotationKeyCGGIExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.keys.len(),
other.basek(),
other.k(),
other.rows(),
other.rank(),
);
brk.prepare(module, other, scratch);
brk
}
}
impl<D: DataMut, B: Backend> BlindRotationKeyCGGIExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &BlindRotationKeyCGGI<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: BlindRotationKeyCGGIExecLayoutFamily<B>,
{
#[cfg(debug_assertions)]
{
assert_eq!(self.data.len(), other.keys.len());
}
let n: usize = other.n();
self.data
.iter_mut()
.zip(other.keys.iter())
.for_each(|(ggsw_exec, other)| {
ggsw_exec.prepare(module, other, scratch);
});
self.dist = other.dist;
match other.dist {
Distribution::BinaryBlock(_) => {
let mut x_pow_a: Vec<SvpPPol<Vec<u8>, B>> = Vec::with_capacity(n << 1);
let mut buf: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
(0..n << 1).for_each(|i| {
let mut res: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(n, 1);
set_xai_plus_y(module, i, 0, &mut res, &mut buf);
x_pow_a.push(res);
});
self.x_pow_a = Some(x_pow_a);
}
_ => {}
}
}
}
pub fn set_xai_plus_y<A, C, B: Backend>(module: &Module<B>, ai: usize, y: i64, res: &mut SvpPPol<A, B>, buf: &mut ScalarZnx<C>)
where
A: DataMut,
C: DataMut,
Module<B>: SvpPrepare<B>,
{
let n: usize = res.n();
{
let raw: &mut [i64] = buf.at_mut(0, 0);
if ai < n {
raw[ai] = 1;
} else {
raw[(ai - n) & (n - 1)] = -1;
}
raw[0] += y;
}
module.svp_prepare(res, 0, buf, 0);
{
let raw: &mut [i64] = buf.at_mut(0, 0);
if ai < n {
raw[ai] = 0;
} else {
raw[(ai - n) & (n - 1)] = 0;
}
raw[0] = 0;
}
}

View File

@@ -1,4 +0,0 @@
mod cpu_spqlios;
mod generic_cggi;
mod generic_lut;
mod generic_serialization;

View File

@@ -1,6 +0,0 @@
mod circuit_bootstrapping;
pub use circuit_bootstrapping::*;
#[cfg(test)]
mod test_fft64;

View File

@@ -1 +0,0 @@
mod circuit_bootstrapping;

View File

@@ -1,4 +1,7 @@
use core::layouts::{prepared::{GGSWCiphertextExec, GLWESecretExec}, GGSWCiphertext, GLWECiphertext, GLWESecret, Infos}; use core::layouts::{
GGSWCiphertext, GLWECiphertext, GLWESecret, Infos,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
};
use std::hint::black_box; use std::hint::black_box;
use backend::{ use backend::{
@@ -63,7 +66,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk); let sk_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk.prepare_alloc(&module, scratch.borrow());
ct_ggsw.encrypt_sk( ct_ggsw.encrypt_sk(
&module, &module,
@@ -84,10 +87,10 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ggsw_exec: GGSWCiphertextExec<Vec<u8>, FFT64> = GGSWCiphertextExec::from(&module, &ct_ggsw, scratch.borrow()); let ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, FFT64> = ct_ggsw.prepare_alloc(&module, scratch.borrow());
move || { move || {
black_box(ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_exec, scratch.borrow())); black_box(ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()));
} }
} }
@@ -157,7 +160,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk); let sk_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk.prepare_alloc(&module, scratch.borrow());
ct_ggsw.encrypt_sk( ct_ggsw.encrypt_sk(
&module, &module,
@@ -178,11 +181,11 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ggsw_exec: GGSWCiphertextExec<Vec<u8>, FFT64> = GGSWCiphertextExec::from(&module, &ct_ggsw, scratch.borrow()); let ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, FFT64> = ct_ggsw.prepare_alloc(&module, scratch.borrow());
move || { move || {
let scratch_borrow = scratch.borrow(); let scratch_borrow = scratch.borrow();
black_box(ct_glwe.external_product_inplace(&module, &ggsw_exec, scratch_borrow)); black_box(ct_glwe.external_product_inplace(&module, &ggsw_prepared, scratch_borrow));
} }
} }

View File

@@ -1,4 +1,7 @@
use core::layouts::{prepared::{GGLWEAutomorphismKeyExec, GGLWESwitchingKeyExec, GLWESecretExec}, GGLWEAutomorphismKey, GGLWESwitchingKey, GLWECiphertext, GLWESecret, Infos}; use core::layouts::{
GGLWEAutomorphismKey, GGLWESwitchingKey, GLWECiphertext, GLWESecret, Infos,
prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc},
};
use std::{hint::black_box, time::Duration}; use std::{hint::black_box, time::Duration};
use backend::{ use backend::{
@@ -66,7 +69,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk_in); let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
@@ -90,10 +93,10 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ksk_exec: GGLWEAutomorphismKeyExec<Vec<u8>, _> = GGLWEAutomorphismKeyExec::from(&module, &ksk, scratch.borrow()); let ksk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, _> = ksk.prepare_alloc(&module, scratch.borrow());
move || { move || {
black_box(ct_out.automorphism(&module, &ct_in, &ksk_exec, scratch.borrow())); black_box(ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()));
} }
} }
@@ -161,7 +164,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_dft: GLWESecretExec<Vec<u8>, FFT64> = GLWESecretExec::from(&module, &sk_in); let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
@@ -185,10 +188,10 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, FFT64> = GGLWESwitchingKeyExec::from(&module, &ksk, scratch.borrow()); let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, FFT64> = ksk.prepare_alloc(&module, scratch.borrow());
move || { move || {
black_box(ct.keyswitch_inplace(&module, &ksk_exec, scratch.borrow())); black_box(ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()));
} }
} }

View File

@@ -4,7 +4,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GGLWEAutomorphismKey, GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec}, layouts::{GGLWEAutomorphismKey, GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared},
trait_families::GLWEKeyswitchFamily, trait_families::GLWEKeyswitchFamily,
}; };
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>, lhs: &GGLWEAutomorphismKey<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphism + VecZnxAutomorphismInplace, Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphism + VecZnxAutomorphismInplace,
@@ -120,7 +120,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphism + VecZnxAutomorphismInplace, Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphism + VecZnxAutomorphismInplace,

View File

@@ -6,7 +6,7 @@ use backend::hal::{
use crate::{ use crate::{
layouts::{ layouts::{
GGSWCiphertext, GLWECiphertext, Infos, GGSWCiphertext, GLWECiphertext, Infos,
prepared::{GGLWEAutomorphismKeyExec, GGLWETensorKeyExec}, prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared},
}, },
trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily}, trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily},
}; };
@@ -60,8 +60,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSWCiphertext<DataLhs>,
auto_key: &GGLWEAutomorphismKeyExec<DataAk, B>, auto_key: &GGLWEAutomorphismKeyPrepared<DataAk, B>,
tensor_key: &GGLWETensorKeyExec<DataTsk, B>, tensor_key: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes, Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes,
@@ -117,8 +117,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn automorphism_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>( pub fn automorphism_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
auto_key: &GGLWEAutomorphismKeyExec<DataKsk, B>, auto_key: &GGLWEAutomorphismKeyPrepared<DataKsk, B>,
tensor_key: &GGLWETensorKeyExec<DataTsk, B>, tensor_key: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes, Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes,
@@ -134,7 +134,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSWCiphertext<DataLhs>,
auto_key: &GGLWEAutomorphismKeyExec<DataAk, B>, auto_key: &GGLWEAutomorphismKeyPrepared<DataAk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes, Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec}, layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared},
trait_families::GLWEKeyswitchFamily, trait_families::GLWEKeyswitchFamily,
}; };
@@ -49,7 +49,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphismInplace, Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphismInplace,
@@ -64,7 +64,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphismInplace, Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphismInplace,
@@ -80,7 +80,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B>, Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B>,
@@ -102,7 +102,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_add_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_add_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B>, Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B>,
@@ -118,7 +118,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallAInplace<B>, Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallAInplace<B>,
@@ -140,7 +140,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_ab_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_sub_ab_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallAInplace<B>, Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallAInplace<B>,
@@ -156,7 +156,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallBInplace<B>, Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallBInplace<B>,
@@ -178,7 +178,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_ba_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_sub_ba_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallBInplace<B>, Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallBInplace<B>,

View File

@@ -5,7 +5,7 @@ use backend::hal::{
use crate::{ use crate::{
TakeGLWECt, TakeGLWECt,
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::GLWEToLWESwitchingKeyExec}, layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::GLWEToLWESwitchingKeyPrepared},
}; };
use crate::trait_families::GLWEKeyswitchFamily; use crate::trait_families::GLWEKeyswitchFamily;
@@ -50,7 +50,7 @@ impl<DLwe: DataMut> LWECiphertext<DLwe> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
a: &GLWECiphertext<DGlwe>, a: &GLWECiphertext<DGlwe>,
ks: &GLWEToLWESwitchingKeyExec<DKs, B>, ks: &GLWEToLWESwitchingKeyPrepared<DKs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
DGlwe: DataRef, DGlwe: DataRef,

View File

@@ -5,7 +5,7 @@ use backend::hal::{
use crate::{ use crate::{
TakeGLWECt, TakeGLWECt,
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWEToGLWESwitchingKeyExec}, layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWEToGLWESwitchingKeyPrepared},
}; };
use crate::trait_families::GLWEKeyswitchFamily; use crate::trait_families::GLWEKeyswitchFamily;
@@ -33,7 +33,7 @@ impl<D: DataMut> GLWECiphertext<D> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lwe: &LWECiphertext<DLwe>, lwe: &LWECiphertext<DLwe>,
ksk: &LWEToGLWESwitchingKeyExec<DKsk, B>, ksk: &LWEToGLWESwitchingKeyPrepared<DKsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
DLwe: DataRef, DLwe: DataRef,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
trait_families::GLWEDecryptFamily, trait_families::GLWEDecryptFamily,
}; };
@@ -27,7 +27,7 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
&self, &self,
module: &Module<B>, module: &Module<B>,
pt: &mut GLWEPlaintext<DataPt>, pt: &mut GLWEPlaintext<DataPt>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEDecryptFamily<B>, Module<B>: GLWEDecryptFamily<B>,

View File

@@ -1,7 +1,7 @@
use std::io::{Read, Result, Write}; use std::io::{Read, Result, Write};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub(crate) enum Distribution { pub enum Distribution {
TernaryFixed(usize), // Ternary with fixed Hamming weight TernaryFixed(usize), // Ternary with fixed Hamming weight
TernaryProb(f64), // Ternary with probabilistic Hamming weight TernaryProb(f64), // Ternary with probabilistic Hamming weight
BinaryFixed(usize), // Binary with fixed Hamming weight BinaryFixed(usize), // Binary with fixed Hamming weight

View File

@@ -8,19 +8,19 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretExec, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{ layouts::{
GLWESecret, Infos, GLWESecret, Infos,
compressed::{GGLWEAutomorphismKeyCompressed, GGLWESwitchingKeyCompressed}, compressed::{GGLWEAutomorphismKeyCompressed, GGLWESwitchingKeyCompressed},
}, },
}; };
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWEAutomorphismKeyCompressed<Vec<u8>> { impl GGLWEAutomorphismKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank) GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank)
} }
@@ -41,8 +41,8 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKeyCompressed<DataSelf> {
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx, Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {

View File

@@ -7,7 +7,7 @@ use sampling::source::Source;
use crate::{ use crate::{
TakeGLWEPt, TakeGLWEPt,
encryption::glwe_encrypt_sk_internal, encryption::glwe_encrypt_sk_internal,
layouts::{GGLWECiphertext, Infos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretExec}, layouts::{GGLWECiphertext, Infos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily};
@@ -26,7 +26,7 @@ impl<D: DataMut> GGLWECiphertextCompressed<D> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: &ScalarZnx<DataPt>, pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
seed: [u8; 32], seed: [u8; 32],
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -5,13 +5,14 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecretExec, TakeGLWESecretPrepared,
layouts::{ layouts::{
GGLWECiphertext, GLWESecret, GGLWESwitchingKey, Infos, compressed::GGLWESwitchingKeyCompressed, prepared::GLWESecretExec, GGLWECiphertext, GGLWESwitchingKey, GLWESecret, Infos, compressed::GGLWESwitchingKeyCompressed,
prepared::GLWESecretPrepared,
}, },
}; };
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWESwitchingKeyCompressed<Vec<u8>> { impl GGLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>( pub fn encrypt_sk_scratch_space<B: Backend>(
@@ -23,11 +24,11 @@ impl GGLWESwitchingKeyCompressed<Vec<u8>> {
rank_out: usize, rank_out: usize,
) -> usize ) -> usize
where where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
(GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1)) (GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1))
+ ScalarZnx::alloc_bytes(n, rank_in) + ScalarZnx::alloc_bytes(n, rank_in)
+ GLWESecretExec::bytes_of(module, n, rank_out) + GLWESecretPrepared::bytes_of(module, n, rank_out)
} }
} }
@@ -43,8 +44,9 @@ impl<DataSelf: DataMut> GGLWESwitchingKeyCompressed<DataSelf> {
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: Module<B>:
GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>, GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + ScratchAvailable + TakeVecZnx, Scratch<B>:
ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + ScratchAvailable + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
@@ -85,7 +87,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKeyCompressed<DataSelf> {
); );
}); });
let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_exec(n, sk_out.rank()); let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_prepared(n, sk_out.rank());
{ {
let (mut tmp, _) = scratch2.take_scalar_znx(n, 1); let (mut tmp, _) = scratch2.take_scalar_znx(n, 1);
(0..sk_out.rank()).for_each(|i| { (0..sk_out.rank()).for_each(|i| {

View File

@@ -8,17 +8,17 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretExec, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWETensorKey, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed}, layouts::{GGLWETensorKey, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed, prepared::Prepare},
trait_families::GLWEDecryptFamily, trait_families::GLWEDecryptFamily,
}; };
use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWETensorKeyCompressed<Vec<u8>> { impl GGLWETensorKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k, rank) GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k, rank)
} }
@@ -34,8 +34,10 @@ impl<DataSelf: DataMut> GGLWETensorKeyCompressed<DataSelf> {
sigma: f64, sigma: f64,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>, Module<B>:
Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretExec<B> + TakeScalarZnx + TakeVecZnx, GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>:
ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretPrepared<B> + TakeScalarZnx + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
@@ -46,8 +48,8 @@ impl<DataSelf: DataMut> GGLWETensorKeyCompressed<DataSelf> {
let n: usize = sk.n(); let n: usize = sk.n();
let rank: usize = self.rank(); let rank: usize = self.rank();
let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_exec(n, rank); let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank);
sk_dft_prep.prepare(module, &sk); sk_dft_prep.prepare(module, &sk, scratch1);
let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1); let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1);

View File

@@ -7,7 +7,7 @@ use sampling::source::Source;
use crate::{ use crate::{
TakeGLWEPt, TakeGLWEPt,
encryption::glwe_encrypt_sk_internal, encryption::glwe_encrypt_sk_internal,
layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretExec}, layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::GGSWEncryptSkFamily; use crate::trait_families::GGSWEncryptSkFamily;
@@ -26,7 +26,7 @@ impl<DataSelf: DataMut> GGSWCiphertextCompressed<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: &ScalarZnx<DataPt>, pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32], seed_xa: [u8; 32],
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -6,7 +6,7 @@ use sampling::source::Source;
use crate::{ use crate::{
encryption::glwe_ct::glwe_encrypt_sk_internal, encryption::glwe_ct::glwe_encrypt_sk_internal,
layouts::{GLWECiphertext, GLWEPlaintext, Infos, compressed::GLWECiphertextCompressed, prepared::GLWESecretExec}, layouts::{GLWECiphertext, GLWEPlaintext, Infos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::GLWEEncryptSkFamily; use crate::trait_families::GLWEEncryptSkFamily;
@@ -25,7 +25,7 @@ impl<D: DataMut> GLWECiphertextCompressed<D> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: &GLWEPlaintext<DataPt>, pt: &GLWEPlaintext<DataPt>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32], seed_xa: [u8; 32],
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,
@@ -49,7 +49,7 @@ impl<D: DataMut> GLWECiphertextCompressed<D> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>, pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32], seed_xa: [u8; 32],
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -8,16 +8,16 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretExec, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWEAutomorphismKey, GLWESecret, GGLWESwitchingKey, Infos}, layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, GLWESecret, Infos},
}; };
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWEAutomorphismKey<Vec<u8>> { impl GGLWEAutomorphismKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank) GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank)
} }
@@ -42,8 +42,8 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx, Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {

View File

@@ -9,7 +9,7 @@ use sampling::source::Source;
use crate::{ use crate::{
TakeGLWEPt, TakeGLWEPt,
layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::GGLWEEncryptSkFamily; use crate::trait_families::GGLWEEncryptSkFamily;
@@ -33,7 +33,7 @@ impl<DataSelf: DataMut> GGLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: &ScalarZnx<DataPt>, pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -5,11 +5,11 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecretExec, TakeGLWESecretPrepared,
layouts::{GGLWECiphertext, GLWESecret, GGLWESwitchingKey, Infos, prepared::GLWESecretExec}, layouts::{GGLWECiphertext, GGLWESwitchingKey, GLWESecret, Infos, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWESwitchingKey<Vec<u8>> { impl GGLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>( pub fn encrypt_sk_scratch_space<B: Backend>(
@@ -21,11 +21,11 @@ impl GGLWESwitchingKey<Vec<u8>> {
rank_out: usize, rank_out: usize,
) -> usize ) -> usize
where where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
(GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1)) (GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1))
+ ScalarZnx::alloc_bytes(n, rank_in) + ScalarZnx::alloc_bytes(n, rank_in)
+ GLWESecretExec::bytes_of(module, n, rank_out) + GLWESecretPrepared::bytes_of(module, n, rank_out)
} }
pub fn encrypt_pk_scratch_space<B: Backend>( pub fn encrypt_pk_scratch_space<B: Backend>(
@@ -52,8 +52,9 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: Module<B>:
GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>, GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + ScratchAvailable + TakeVecZnx, Scratch<B>:
ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + ScratchAvailable + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
@@ -94,7 +95,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
); );
}); });
let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_exec(n, sk_out.rank()); let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_prepared(n, sk_out.rank());
{ {
let (mut tmp, _) = scratch2.take_scalar_znx(n, 1); let (mut tmp, _) = scratch2.take_scalar_znx(n, 1);
(0..sk_out.rank()).for_each(|i| { (0..sk_out.rank()).for_each(|i| {

View File

@@ -8,19 +8,22 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretExec, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWETensorKey, GLWESecret, GGLWESwitchingKey, Infos, prepared::GLWESecretExec}, layouts::{
GGLWESwitchingKey, GGLWETensorKey, GLWESecret, Infos,
prepared::{GLWESecretPrepared, Prepare},
},
trait_families::GLWEDecryptFamily, trait_families::GLWEDecryptFamily,
}; };
use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWETensorKey<Vec<u8>> { impl GGLWETensorKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GLWESecretExec::bytes_of(module, n, rank) GLWESecretPrepared::bytes_of(module, n, rank)
+ module.vec_znx_dft_alloc_bytes(n, rank, 1) + module.vec_znx_dft_alloc_bytes(n, rank, 1)
+ module.vec_znx_big_alloc_bytes(n, 1, 1) + module.vec_znx_big_alloc_bytes(n, 1, 1)
+ module.vec_znx_dft_alloc_bytes(n, 1, 1) + module.vec_znx_dft_alloc_bytes(n, 1, 1)
@@ -39,8 +42,10 @@ impl<DataSelf: DataMut> GGLWETensorKey<DataSelf> {
sigma: f64, sigma: f64,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>, Module<B>:
Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretExec<B> + TakeScalarZnx + TakeVecZnx, GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>:
ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretPrepared<B> + TakeScalarZnx + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
@@ -52,8 +57,8 @@ impl<DataSelf: DataMut> GGLWETensorKey<DataSelf> {
let rank: usize = self.rank(); let rank: usize = self.rank();
let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_exec(n, rank); let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank);
sk_dft_prep.prepare(module, &sk); sk_dft_prep.prepare(module, &sk, scratch1);
let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1); let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1);

View File

@@ -6,7 +6,7 @@ use sampling::source::Source;
use crate::{ use crate::{
TakeGLWEPt, TakeGLWEPt,
layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GLWESecretExec}, layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::GLWEEncryptSkFamily; use crate::trait_families::GLWEEncryptSkFamily;
@@ -31,7 +31,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: &ScalarZnx<DataPt>, pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -13,7 +13,7 @@ use crate::{
dist::Distribution, dist::Distribution,
layouts::{ layouts::{
GLWECiphertext, GLWEPlaintext, Infos, GLWECiphertext, GLWEPlaintext, Infos,
prepared::{GLWEPublicKeyExec, GLWESecretExec}, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared},
}, },
trait_families::{GLWEEncryptPkFamily, GLWEEncryptSkFamily}, trait_families::{GLWEEncryptPkFamily, GLWEEncryptSkFamily},
}; };
@@ -42,7 +42,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: &GLWEPlaintext<DataPt>, pt: &GLWEPlaintext<DataPt>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,
@@ -78,7 +78,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn encrypt_zero_sk<DataSk: DataRef, B: Backend>( pub fn encrypt_zero_sk<DataSk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,
@@ -113,7 +113,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>, pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,
@@ -143,7 +143,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: &GLWEPlaintext<DataPt>, pt: &GLWEPlaintext<DataPt>,
pk: &GLWEPublicKeyExec<DataPk, B>, pk: &GLWEPublicKeyPrepared<DataPk, B>,
source_xu: &mut Source, source_xu: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,
@@ -166,7 +166,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn encrypt_zero_pk<DataPk: DataRef, B: Backend>( pub fn encrypt_zero_pk<DataPk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pk: &GLWEPublicKeyExec<DataPk, B>, pk: &GLWEPublicKeyPrepared<DataPk, B>,
source_xu: &mut Source, source_xu: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,
@@ -190,7 +190,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>, pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
pk: &GLWEPublicKeyExec<DataPk, B>, pk: &GLWEPublicKeyPrepared<DataPk, B>,
source_xu: &mut Source, source_xu: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,
@@ -283,7 +283,7 @@ pub(crate) fn glwe_encrypt_sk_internal<DataCt: DataMut, DataPt: DataRef, DataSk:
cols: usize, cols: usize,
compressed: bool, compressed: bool,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>, pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -7,7 +7,7 @@ use sampling::source::Source;
use crate::{ use crate::{
dist::Distribution, dist::Distribution,
layouts::{GLWECiphertext, GLWEPublicKey, Infos, prepared::GLWESecretExec}, layouts::{GLWECiphertext, GLWEPublicKey, Infos, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::GLWEEncryptSkFamily; use crate::trait_families::GLWEEncryptSkFamily;
@@ -16,7 +16,7 @@ impl<D: DataMut> GLWEPublicKey<D> {
pub fn generate_from_sk<S: DataRef, B: Backend>( pub fn generate_from_sk<S: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
sk: &GLWESecretExec<S, B>, sk: &GLWESecretPrepared<S, B>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -8,18 +8,18 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretExec, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GLWESecret, GGLWESwitchingKey, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretExec}, layouts::{GGLWESwitchingKey, GLWESecret, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GLWEToLWESwitchingKey<Vec<u8>> { impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize
where where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GLWESecretExec::bytes_of(module, n, rank_in) GLWESecretPrepared::bytes_of(module, n, rank_in)
+ (GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in, 1) | GLWESecret::bytes_of(n, rank_in)) + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in, 1) | GLWESecret::bytes_of(n, rank_in))
} }
} }
@@ -41,8 +41,8 @@ impl<D: DataMut> GLWEToLWESwitchingKey<D> {
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx, Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {

View File

@@ -8,19 +8,19 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretExec, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GLWESecret, GGLWESwitchingKey, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretExec}, layouts::{GGLWESwitchingKey, GLWESecret, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretPrepared},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl LWESwitchingKey<Vec<u8>> { impl LWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize
where where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GLWESecret::bytes_of(n, 1) GLWESecret::bytes_of(n, 1)
+ GLWESecretExec::bytes_of(module, n, 1) + GLWESecretPrepared::bytes_of(module, n, 1)
+ GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, 1) + GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, 1)
} }
} }
@@ -42,8 +42,8 @@ impl<D: DataMut> LWESwitchingKey<D> {
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx, Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {

View File

@@ -8,16 +8,16 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretExec, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GLWESecret, GGLWESwitchingKey, LWESecret, LWEToGLWESwitchingKey}, layouts::{GGLWESwitchingKey, GLWESecret, LWESecret, LWEToGLWESwitchingKey},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl LWEToGLWESwitchingKey<Vec<u8>> { impl LWEToGLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize
where where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, rank_out) + GLWESecret::bytes_of(n, 1) GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, rank_out) + GLWESecret::bytes_of(n, 1)
} }
@@ -40,8 +40,8 @@ impl<D: DataMut> LWEToGLWESwitchingKey<D> {
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx, Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{ {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {

View File

@@ -4,7 +4,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, prepared::GGSWCiphertextExec}, layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily, trait_families::GLWEExternalProductFamily,
}; };
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>, lhs: &GGLWEAutomorphismKey<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,
@@ -58,7 +58,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -4,7 +4,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GLWECiphertext, GGLWESwitchingKey, Infos, prepared::GGSWCiphertextExec}, layouts::{GGLWESwitchingKey, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily, trait_families::GLWEExternalProductFamily,
}; };
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWESwitchingKey<DataLhs>, lhs: &GGLWESwitchingKey<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,
@@ -94,7 +94,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -4,7 +4,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GGSWCiphertextExec}, layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily, trait_families::GLWEExternalProductFamily,
}; };
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSWCiphertext<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,
@@ -105,7 +105,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GLWECiphertext, Infos, prepared::GGSWCiphertextExec}, layouts::{GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily, trait_families::GLWEExternalProductFamily,
}; };
@@ -64,7 +64,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,
@@ -141,7 +141,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>, rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductFamily<B>, Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
use crate::{ use crate::{
GLWEOperations, TakeGLWECt, GLWEOperations, TakeGLWECt,
layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec}, layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared},
}; };
use crate::trait_families::{GLWEKeyswitchFamily, GLWEPackingFamily}; use crate::trait_families::{GLWEKeyswitchFamily, GLWEPackingFamily};
@@ -115,7 +115,7 @@ impl GLWEPacker {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
a: Option<&GLWECiphertext<DataA>>, a: Option<&GLWECiphertext<DataA>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>, auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEPackingFamily<B>, Module<B>: GLWEPackingFamily<B>,
@@ -174,7 +174,7 @@ fn pack_core<D: DataRef, DataAK: DataRef, B: Backend>(
a: Option<&GLWECiphertext<D>>, a: Option<&GLWECiphertext<D>>,
accumulators: &mut [Accumulator], accumulators: &mut [Accumulator],
i: usize, i: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>, auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEPackingFamily<B>, Module<B>: GLWEPackingFamily<B>,
@@ -252,7 +252,7 @@ fn combine<D: DataRef, DataAK: DataRef, B: Backend>(
acc: &mut Accumulator, acc: &mut Accumulator,
b: Option<&GLWECiphertext<D>>, b: Option<&GLWECiphertext<D>>,
i: usize, i: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>, auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEPackingFamily<B>, Module<B>: GLWEPackingFamily<B>,

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use backend::hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; use backend::hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
use crate::{ use crate::{
layouts::{GLWECiphertext, prepared::GGLWEAutomorphismKeyExec}, layouts::{GLWECiphertext, prepared::GGLWEAutomorphismKeyPrepared},
operations::GLWEOperations, operations::GLWEOperations,
}; };
@@ -61,7 +61,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
start: usize, start: usize,
end: usize, end: usize,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>, auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWETraceModuleFamily<B>, Module<B>: GLWETraceModuleFamily<B>,
@@ -76,7 +76,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
module: &Module<B>, module: &Module<B>,
start: usize, start: usize,
end: usize, end: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>, auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWETraceModuleFamily<B>, Module<B>: GLWETraceModuleFamily<B>,

View File

@@ -5,8 +5,8 @@ use backend::hal::{
use crate::{ use crate::{
layouts::{ layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GGLWESwitchingKey, Infos, GGLWEAutomorphismKey, GGLWESwitchingKey, GLWECiphertext, Infos,
prepared::{GGLWEAutomorphismKeyExec, GGLWESwitchingKeyExec}, prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared},
}, },
trait_families::GLWEKeyswitchFamily, trait_families::GLWEKeyswitchFamily,
}; };
@@ -49,7 +49,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>, lhs: &GGLWEAutomorphismKey<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>, rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B>, Module<B>: GLWEKeyswitchFamily<B>,
@@ -61,7 +61,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>, rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B>, Module<B>: GLWEKeyswitchFamily<B>,
@@ -112,7 +112,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWESwitchingKey<DataLhs>, lhs: &GGLWESwitchingKey<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>, rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B>, Module<B>: GLWEKeyswitchFamily<B>,
@@ -160,7 +160,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>, rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B>, Module<B>: GLWEKeyswitchFamily<B>,

View File

@@ -9,7 +9,7 @@ use backend::hal::{
use crate::{ use crate::{
layouts::{ layouts::{
GGLWECiphertext, GGSWCiphertext, GLWECiphertext, Infos, GGLWECiphertext, GGSWCiphertext, GLWECiphertext, Infos,
prepared::{GGLWESwitchingKeyExec, GGLWETensorKeyExec}, prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared},
}, },
operations::GLWEOperations, operations::GLWEOperations,
trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily}, trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily},
@@ -97,7 +97,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
a: &GGLWECiphertext<DataA>, a: &GGLWECiphertext<DataA>,
tsk: &GGLWETensorKeyExec<DataTsk, B>, tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
DataA: DataRef, DataA: DataRef,
@@ -123,8 +123,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSWCiphertext<DataLhs>,
ksk: &GGLWESwitchingKeyExec<DataKsk, B>, ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
tsk: &GGLWETensorKeyExec<DataTsk, B>, tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes, Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,
@@ -137,8 +137,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn keyswitch_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>( pub fn keyswitch_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
ksk: &GGLWESwitchingKeyExec<DataKsk, B>, ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
tsk: &GGLWETensorKeyExec<DataTsk, B>, tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes, Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,
@@ -153,7 +153,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn expand_row<DataTsk: DataRef, B: Backend>( pub fn expand_row<DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
tsk: &GGLWETensorKeyExec<DataTsk, B>, tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes, Module<B>: GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,
@@ -278,7 +278,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSWCiphertext<DataLhs>,
ksk: &GGLWESwitchingKeyExec<DataKsk, B>, ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes, Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
}; };
use crate::{ use crate::{
layouts::{GLWECiphertext, Infos, prepared::GGLWESwitchingKeyExec}, layouts::{GLWECiphertext, Infos, prepared::GGLWESwitchingKeyPrepared},
trait_families::GLWEKeyswitchFamily, trait_families::GLWEKeyswitchFamily,
}; };
@@ -65,7 +65,7 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
&self, &self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>, rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &Scratch<B>, scratch: &Scratch<B>,
) where ) where
DataLhs: DataRef, DataLhs: DataRef,
@@ -136,7 +136,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>, rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B>, Module<B>: GLWEKeyswitchFamily<B>,
@@ -156,7 +156,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>, rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEKeyswitchFamily<B>, Module<B>: GLWEKeyswitchFamily<B>,
@@ -174,7 +174,7 @@ impl<D: DataRef> GLWECiphertext<D> {
&self, &self,
module: &Module<B>, module: &Module<B>,
res_dft: VecZnxDft<DataRes, B>, res_dft: VecZnxDft<DataRes, B>,
rhs: &GGLWESwitchingKeyExec<DataKey, B>, rhs: &GGLWESwitchingKeyPrepared<DataKey, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) -> VecZnxBig<DataRes, B> ) -> VecZnxBig<DataRes, B>
where where

View File

@@ -5,7 +5,7 @@ use backend::hal::{
use crate::{ use crate::{
TakeGLWECt, TakeGLWECt,
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWESwitchingKeyExec}, layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWESwitchingKeyPrepared},
}; };
use crate::trait_families::GLWEKeyswitchFamily; use crate::trait_families::GLWEKeyswitchFamily;
@@ -32,7 +32,7 @@ impl<DLwe: DataMut> LWECiphertext<DLwe> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
a: &LWECiphertext<A>, a: &LWECiphertext<A>,
ksk: &LWESwitchingKeyExec<DKs, B>, ksk: &LWESwitchingKeyPrepared<DKs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
A: DataRef, A: DataRef,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
use crate::layouts::{GLWEToLWESwitchingKey, Infos, compressed::GGLWESwitchingKeyCompressed}; use crate::layouts::{GLWEToLWESwitchingKey, Infos, compressed::GGLWESwitchingKeyCompressed};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>); pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
@@ -91,7 +91,7 @@ impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize
where where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in) GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in)
} }

View File

@@ -6,7 +6,7 @@ use backend::hal::{
use crate::layouts::{Infos, LWESwitchingKey, compressed::GGLWESwitchingKeyCompressed}; use crate::layouts::{Infos, LWESwitchingKey, compressed::GGLWESwitchingKeyCompressed};
use std::fmt; use std::fmt;
use crate::trait_families::{Decompress, GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{Decompress, GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>); pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
@@ -90,7 +90,7 @@ impl LWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize
where where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
LWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k) LWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k)
} }

View File

@@ -9,7 +9,7 @@ use crate::{
}; };
use std::fmt; use std::fmt;
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>); pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
@@ -93,7 +93,7 @@ impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize
where where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>, Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{ {
LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_out) LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_out)
} }

View File

@@ -3,7 +3,7 @@ use backend::hal::{
layouts::{Data, DataMut, DataRef, MatZnx, ReaderFrom, WriterTo}, layouts::{Data, DataMut, DataRef, MatZnx, ReaderFrom, WriterTo},
}; };
use crate::layouts::{GLWECiphertext, GGLWESwitchingKey, Infos}; use crate::layouts::{GGLWESwitchingKey, GLWECiphertext, Infos};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;

View File

@@ -1,3 +1,5 @@
use std::fmt;
use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef}; use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef};
use crate::layouts::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, Infos, SetMetaData}; use crate::layouts::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, Infos, SetMetaData};
@@ -8,6 +10,18 @@ pub struct GLWEPlaintext<D: Data> {
pub k: usize, pub k: usize,
} }
impl<D: DataRef> fmt::Display for GLWEPlaintext<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"GLWEPlaintext: basek={} k={}: {}",
self.basek(),
self.k(),
self.data
)
}
}
impl<D: Data> Infos for GLWEPlaintext<D> { impl<D: Data> Infos for GLWEPlaintext<D> {
type Inner = VecZnx<D>; type Inner = VecZnx<D>;

View File

@@ -13,6 +13,18 @@ pub struct LWECiphertext<D: Data> {
pub(crate) basek: usize, pub(crate) basek: usize,
} }
impl<D: DataRef> LWECiphertext<D> {
pub fn data(&self) -> &VecZnx<D> {
&self.data
}
}
impl<D: DataMut> LWECiphertext<D> {
pub fn data_mut(&mut self) -> &VecZnx<D> {
&mut self.data
}
}
impl<D: DataRef> fmt::Debug for LWECiphertext<D> { impl<D: DataRef> fmt::Debug for LWECiphertext<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self) write!(f, "{}", self)
@@ -31,10 +43,7 @@ impl<D: DataRef> fmt::Display for LWECiphertext<D> {
} }
} }
impl<D: DataMut> Reset for LWECiphertext<D> impl<D: DataMut> Reset for LWECiphertext<D> {
where
VecZnx<D>: Reset,
{
fn reset(&mut self) { fn reset(&mut self) {
self.data.reset(); self.data.reset();
self.basek = 0; self.basek = 0;

View File

@@ -1,3 +1,5 @@
use std::fmt;
use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef}; use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef};
use crate::layouts::{Infos, SetMetaData}; use crate::layouts::{Infos, SetMetaData};
@@ -18,6 +20,18 @@ impl LWEPlaintext<Vec<u8>> {
} }
} }
impl<D: DataRef> fmt::Display for LWEPlaintext<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"LWEPlaintext: basek={} k={}: {}",
self.basek(),
self.k(),
self.data
)
}
}
impl<D: Data> Infos for LWEPlaintext<D> { impl<D: Data> Infos for LWEPlaintext<D> {
type Inner = VecZnx<D>; type Inner = VecZnx<D>;

View File

@@ -1,6 +1,6 @@
use backend::hal::{ use backend::hal::{
api::{ZnxInfos, ZnxZero}, api::{ZnxInfos, ZnxView, ZnxZero},
layouts::{Data, DataMut, ScalarZnx}, layouts::{Data, DataMut, DataRef, ScalarZnx},
}; };
use sampling::source::Source; use sampling::source::Source;
@@ -20,6 +20,20 @@ impl LWESecret<Vec<u8>> {
} }
} }
impl<D: DataRef> LWESecret<D> {
pub fn raw(&self) -> &[i64] {
self.data.at(0, 0)
}
pub fn dist(&self) -> Distribution {
self.dist
}
pub fn data(&self) -> &ScalarZnx<D> {
&self.data
}
}
impl<D: Data> LWESecret<D> { impl<D: Data> LWESecret<D> {
pub fn n(&self) -> usize { pub fn n(&self) -> usize {
self.data.n() self.data.n()

View File

@@ -3,21 +3,24 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{GGLWEAutomorphismKey, Infos, prepared::GGLWESwitchingKeyExec}; use crate::layouts::{
GGLWEAutomorphismKey, Infos,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWEAutomorphismKeyExec<D: Data, B: Backend> { pub struct GGLWEAutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWESwitchingKeyExec<D, B>, pub(crate) key: GGLWESwitchingKeyPrepared<D, B>,
pub(crate) p: i64, pub(crate) p: i64,
} }
impl<B: Backend> GGLWEAutomorphismKeyExec<Vec<u8>, B> { impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
GGLWEAutomorphismKeyExec::<Vec<u8>, B> { GGLWEAutomorphismKeyPrepared::<Vec<u8>, B> {
key: GGLWESwitchingKeyExec::alloc(module, n, basek, k, rows, digits, rank, rank), key: GGLWESwitchingKeyPrepared::alloc(module, n, basek, k, rows, digits, rank, rank),
p: 0, p: 0,
} }
} }
@@ -26,28 +29,11 @@ impl<B: Backend> GGLWEAutomorphismKeyExec<Vec<u8>, B> {
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyExec::bytes_of(module, n, basek, k, rows, digits, rank, rank) GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, rank, rank)
}
pub fn from<DataOther: DataRef>(module: &Module<B>, other: &GGLWEAutomorphismKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut atk_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank(),
);
atk_exec.prepare(module, other, scratch);
atk_exec
} }
} }
impl<D: Data, B: Backend> Infos for GGLWEAutomorphismKeyExec<D, B> { impl<D: Data, B: Backend> Infos for GGLWEAutomorphismKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -63,7 +49,7 @@ impl<D: Data, B: Backend> Infos for GGLWEAutomorphismKeyExec<D, B> {
} }
} }
impl<D: Data, B: Backend> GGLWEAutomorphismKeyExec<D, B> { impl<D: Data, B: Backend> GGLWEAutomorphismKeyPrepared<D, B> {
pub fn p(&self) -> i64 { pub fn p(&self) -> i64 {
self.p self.p
} }
@@ -85,13 +71,31 @@ impl<D: Data, B: Backend> GGLWEAutomorphismKeyExec<D, B> {
} }
} }
impl<D: DataMut, B: Backend> GGLWEAutomorphismKeyExec<D, B> { impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWEAutomorphismKey<DR>> for GGLWEAutomorphismKeyPrepared<D, B>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DataOther>, scratch: &mut Scratch<B>) where
where Module<B>: VmpPMatPrepare<B>,
DataOther: DataRef, {
Module<B>: VmpPMatPrepare<B>, fn prepare(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DR>, scratch: &mut Scratch<B>) {
{
self.key.prepare(module, &other.key, scratch); self.key.prepare(module, &other.key, scratch);
self.p = other.p; self.p = other.p;
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> for GGLWEAutomorphismKey<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank(),
);
atk_prepared.prepare(module, self, scratch);
atk_prepared
}
}

View File

@@ -3,17 +3,20 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{GGLWECiphertext, Infos}; use crate::layouts::{
GGLWECiphertext, Infos,
prepared::{Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWECiphertextExec<D: Data, B: Backend> { pub struct GGLWECiphertextPrepared<D: Data, B: Backend> {
pub(crate) data: VmpPMat<D, B>, pub(crate) data: VmpPMat<D, B>,
pub(crate) basek: usize, pub(crate) basek: usize,
pub(crate) k: usize, pub(crate) k: usize,
pub(crate) digits: usize, pub(crate) digits: usize,
} }
impl<B: Backend> GGLWECiphertextExec<Vec<u8>, B> { impl<B: Backend> GGLWECiphertextPrepared<Vec<u8>, B> {
pub fn alloc( pub fn alloc(
module: &Module<B>, module: &Module<B>,
n: usize, n: usize,
@@ -84,7 +87,7 @@ impl<B: Backend> GGLWECiphertextExec<Vec<u8>, B> {
} }
} }
impl<D: Data, B: Backend> Infos for GGLWECiphertextExec<D, B> { impl<D: Data, B: Backend> Infos for GGLWECiphertextPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -100,7 +103,7 @@ impl<D: Data, B: Backend> Infos for GGLWECiphertextExec<D, B> {
} }
} }
impl<D: Data, B: Backend> GGLWECiphertextExec<D, B> { impl<D: Data, B: Backend> GGLWECiphertextPrepared<D, B> {
pub fn rank(&self) -> usize { pub fn rank(&self) -> usize {
self.data.cols_out() - 1 self.data.cols_out() - 1
} }
@@ -118,15 +121,34 @@ impl<D: Data, B: Backend> GGLWECiphertextExec<D, B> {
} }
} }
impl<D: DataMut, B: Backend> GGLWECiphertextExec<D, B> { impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWECiphertext<DR>> for GGLWECiphertextPrepared<D, B>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWECiphertext<DataOther>, scratch: &mut Scratch<B>) where
where Module<B>: VmpPMatPrepare<B>,
DataOther: DataRef, {
Module<B>: VmpPMatPrepare<B>, fn prepare(&mut self, module: &Module<B>, other: &GGLWECiphertext<DR>, scratch: &mut Scratch<B>) {
{
module.vmp_prepare(&mut self.data, &other.data, scratch); module.vmp_prepare(&mut self.data, &other.data, scratch);
self.basek = other.basek; self.basek = other.basek;
self.k = other.k; self.k = other.k;
self.digits = other.digits; self.digits = other.digits;
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWECiphertextPrepared<Vec<u8>, B>> for GGLWECiphertext<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWECiphertextPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWECiphertextPrepared<Vec<u8>, B> = GGLWECiphertextPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank_in(),
self.rank_out(),
);
atk_prepared.prepare(module, self, scratch);
atk_prepared
}
}

View File

@@ -3,16 +3,19 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{GGLWESwitchingKey, Infos, prepared::GGLWECiphertextExec}; use crate::layouts::{
GGLWESwitchingKey, Infos,
prepared::{GGLWECiphertextPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWESwitchingKeyExec<D: Data, B: Backend> { pub struct GGLWESwitchingKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWECiphertextExec<D, B>, pub(crate) key: GGLWECiphertextPrepared<D, B>,
pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out pub(crate) sk_out_n: usize, // Degree of sk_out
} }
impl<B: Backend> GGLWESwitchingKeyExec<Vec<u8>, B> { impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc( pub fn alloc(
module: &Module<B>, module: &Module<B>,
n: usize, n: usize,
@@ -26,8 +29,8 @@ impl<B: Backend> GGLWESwitchingKeyExec<Vec<u8>, B> {
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
GGLWESwitchingKeyExec::<Vec<u8>, B> { GGLWESwitchingKeyPrepared::<Vec<u8>, B> {
key: GGLWECiphertextExec::alloc(module, n, basek, k, rows, digits, rank_in, rank_out), key: GGLWECiphertextPrepared::alloc(module, n, basek, k, rows, digits, rank_in, rank_out),
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
} }
@@ -46,29 +49,11 @@ impl<B: Backend> GGLWESwitchingKeyExec<Vec<u8>, B> {
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWECiphertextExec::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out) GGLWECiphertextPrepared::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out)
}
pub fn from<DataOther: DataRef>(module: &Module<B>, other: &GGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank_in(),
other.rank_out(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
} }
} }
impl<D: Data, B: Backend> Infos for GGLWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> Infos for GGLWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -84,7 +69,7 @@ impl<D: Data, B: Backend> Infos for GGLWESwitchingKeyExec<D, B> {
} }
} }
impl<D: Data, B: Backend> GGLWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> GGLWESwitchingKeyPrepared<D, B> {
pub fn rank(&self) -> usize { pub fn rank(&self) -> usize {
self.key.data.cols_out() - 1 self.key.data.cols_out() - 1
} }
@@ -110,14 +95,33 @@ impl<D: Data, B: Backend> GGLWESwitchingKeyExec<D, B> {
} }
} }
impl<D: DataMut, B: Backend> GGLWESwitchingKeyExec<D, B> { impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWESwitchingKey<DR>> for GGLWESwitchingKeyPrepared<D, B>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) where
where Module<B>: VmpPMatPrepare<B>,
DataOther: DataRef, {
Module<B>: VmpPMatPrepare<B>, fn prepare(&mut self, module: &Module<B>, other: &GGLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
{
self.key.prepare(module, &other.key, scratch); self.key.prepare(module, &other.key, scratch);
self.sk_in_n = other.sk_in_n; self.sk_in_n = other.sk_in_n;
self.sk_out_n = other.sk_out_n; self.sk_out_n = other.sk_out_n;
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWESwitchingKeyPrepared<Vec<u8>, B>> for GGLWESwitchingKey<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = GGLWESwitchingKeyPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank_in(),
self.rank_out(),
);
atk_prepared.prepare(module, self, scratch);
atk_prepared
}
}

View File

@@ -3,22 +3,25 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{GGLWETensorKey, Infos, prepared::GGLWESwitchingKeyExec}; use crate::layouts::{
GGLWETensorKey, Infos,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWETensorKeyExec<D: Data, B: Backend> { pub struct GGLWETensorKeyPrepared<D: Data, B: Backend> {
pub(crate) keys: Vec<GGLWESwitchingKeyExec<D, B>>, pub(crate) keys: Vec<GGLWESwitchingKeyPrepared<D, B>>,
} }
impl<B: Backend> GGLWETensorKeyExec<Vec<u8>, B> { impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
let mut keys: Vec<GGLWESwitchingKeyExec<Vec<u8>, B>> = Vec::new(); let mut keys: Vec<GGLWESwitchingKeyPrepared<Vec<u8>, B>> = Vec::new();
let pairs: usize = (((rank + 1) * rank) >> 1).max(1); let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
(0..pairs).for_each(|_| { (0..pairs).for_each(|_| {
keys.push(GGLWESwitchingKeyExec::alloc( keys.push(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, digits, 1, rank, module, n, basek, k, rows, digits, 1, rank,
)); ));
}); });
@@ -30,32 +33,11 @@ impl<B: Backend> GGLWETensorKeyExec<Vec<u8>, B> {
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
let pairs: usize = (((rank + 1) * rank) >> 1).max(1); let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
pairs * GGLWESwitchingKeyExec::bytes_of(module, n, basek, k, rows, digits, 1, rank) pairs * GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, 1, rank)
}
pub fn from<D: DataRef>(
module: &Module<B>,
other: &GGLWETensorKey<D>,
scratch: &mut Scratch<B>,
) -> GGLWETensorKeyExec<Vec<u8>, B>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank(),
);
tsk_exec.prepare(module, other, scratch);
tsk_exec
} }
} }
impl<D: Data, B: Backend> Infos for GGLWETensorKeyExec<D, B> { impl<D: Data, B: Backend> Infos for GGLWETensorKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -71,7 +53,7 @@ impl<D: Data, B: Backend> Infos for GGLWETensorKeyExec<D, B> {
} }
} }
impl<D: Data, B: Backend> GGLWETensorKeyExec<D, B> { impl<D: Data, B: Backend> GGLWETensorKeyPrepared<D, B> {
pub fn rank(&self) -> usize { pub fn rank(&self) -> usize {
self.keys[0].rank() self.keys[0].rank()
} }
@@ -89,9 +71,9 @@ impl<D: Data, B: Backend> GGLWETensorKeyExec<D, B> {
} }
} }
impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> { impl<D: DataMut, B: Backend> GGLWETensorKeyPrepared<D, B> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyExec<D, B> { pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared<D, B> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -100,9 +82,9 @@ impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
} }
} }
impl<D: DataRef, B: Backend> GGLWETensorKeyExec<D, B> { impl<D: DataRef, B: Backend> GGLWETensorKeyPrepared<D, B> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyExec<D, B> { pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared<D, B> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -111,12 +93,11 @@ impl<D: DataRef, B: Backend> GGLWETensorKeyExec<D, B> {
} }
} }
impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> { impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWETensorKey<DR>> for GGLWETensorKeyPrepared<D, B>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWETensorKey<DataOther>, scratch: &mut Scratch<B>) where
where Module<B>: VmpPMatPrepare<B>,
DataOther: DataRef, {
Module<B>: VmpPMatPrepare<B>, fn prepare(&mut self, module: &Module<B>, other: &GGLWETensorKey<DR>, scratch: &mut Scratch<B>) {
{
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
assert_eq!(self.keys.len(), other.keys.len()); assert_eq!(self.keys.len(), other.keys.len());
@@ -129,3 +110,22 @@ impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
}); });
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWETensorKeyPrepared<Vec<u8>, B>> for GGLWETensorKey<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWETensorKeyPrepared<Vec<u8>, B> {
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = GGLWETensorKeyPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank(),
);
tsk_prepared.prepare(module, self, scratch);
tsk_prepared
}
}

View File

@@ -3,17 +3,20 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{GGSWCiphertext, Infos}; use crate::layouts::{
GGSWCiphertext, Infos,
prepared::{Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGSWCiphertextExec<D: Data, B: Backend> { pub struct GGSWCiphertextPrepared<D: Data, B: Backend> {
pub(crate) data: VmpPMat<D, B>, pub(crate) data: VmpPMat<D, B>,
pub(crate) basek: usize, pub(crate) basek: usize,
pub(crate) k: usize, pub(crate) k: usize,
pub(crate) digits: usize, pub(crate) digits: usize,
} }
impl<B: Backend> GGSWCiphertextExec<Vec<u8>, B> { impl<B: Backend> GGSWCiphertextPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
@@ -66,30 +69,9 @@ impl<B: Backend> GGSWCiphertextExec<Vec<u8>, B> {
module.vmp_pmat_alloc_bytes(n, rows, rank + 1, rank + 1, size) module.vmp_pmat_alloc_bytes(n, rows, rank + 1, rank + 1, size)
} }
pub fn from<DataOther: DataRef>(
module: &Module<B>,
other: &GGSWCiphertext<DataOther>,
scratch: &mut Scratch<B>,
) -> GGSWCiphertextExec<Vec<u8>, B>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ggsw_exec: GGSWCiphertextExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank(),
);
ggsw_exec.prepare(module, other, scratch);
ggsw_exec
}
} }
impl<D: Data, B: Backend> Infos for GGSWCiphertextExec<D, B> { impl<D: Data, B: Backend> Infos for GGSWCiphertextPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -105,7 +87,7 @@ impl<D: Data, B: Backend> Infos for GGSWCiphertextExec<D, B> {
} }
} }
impl<D: Data, B: Backend> GGSWCiphertextExec<D, B> { impl<D: Data, B: Backend> GGSWCiphertextPrepared<D, B> {
pub fn rank(&self) -> usize { pub fn rank(&self) -> usize {
self.data.cols_out() - 1 self.data.cols_out() - 1
} }
@@ -115,15 +97,39 @@ impl<D: Data, B: Backend> GGSWCiphertextExec<D, B> {
} }
} }
impl<DataSelf: DataMut, B: Backend> GGSWCiphertextExec<DataSelf, B> { impl<D: DataRef, B: Backend> GGSWCiphertextPrepared<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGSWCiphertext<DataOther>, scratch: &mut Scratch<B>) pub fn data(&self) -> &VmpPMat<D, B> {
where &self.data
DataOther: DataRef, }
Module<B>: VmpPMatPrepare<B>, }
{
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGSWCiphertext<DR>> for GGSWCiphertextPrepared<D, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGSWCiphertext<DR>, scratch: &mut Scratch<B>) {
module.vmp_prepare(&mut self.data, &other.data, scratch); module.vmp_prepare(&mut self.data, &other.data, scratch);
self.k = other.k; self.k = other.k;
self.basek = other.basek; self.basek = other.basek;
self.digits = other.digits; self.digits = other.digits;
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGSWCiphertextPrepared<Vec<u8>, B>> for GGSWCiphertext<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGSWCiphertextPrepared<Vec<u8>, B> {
let mut ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = GGSWCiphertextPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank(),
);
ggsw_prepared.prepare(module, self, scratch);
ggsw_prepared
}
}

View File

@@ -5,18 +5,21 @@ use backend::hal::{
use crate::{ use crate::{
dist::Distribution, dist::Distribution,
layouts::{GLWEPublicKey, Infos}, layouts::{
GLWEPublicKey, Infos,
prepared::{Prepare, PrepareAlloc},
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GLWEPublicKeyExec<D: Data, B: Backend> { pub struct GLWEPublicKeyPrepared<D: Data, B: Backend> {
pub(crate) data: VecZnxDft<D, B>, pub(crate) data: VecZnxDft<D, B>,
pub(crate) basek: usize, pub(crate) basek: usize,
pub(crate) k: usize, pub(crate) k: usize,
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
} }
impl<D: Data, B: Backend> Infos for GLWEPublicKeyExec<D, B> { impl<D: Data, B: Backend> Infos for GLWEPublicKeyPrepared<D, B> {
type Inner = VecZnxDft<D, B>; type Inner = VecZnxDft<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -32,13 +35,13 @@ impl<D: Data, B: Backend> Infos for GLWEPublicKeyExec<D, B> {
} }
} }
impl<D: Data, B: Backend> GLWEPublicKeyExec<D, B> { impl<D: Data, B: Backend> GLWEPublicKeyPrepared<D, B> {
pub fn rank(&self) -> usize { pub fn rank(&self) -> usize {
self.cols() - 1 self.cols() - 1
} }
} }
impl<B: Backend> GLWEPublicKeyExec<Vec<u8>, B> { impl<B: Backend> GLWEPublicKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> Self
where where
Module<B>: VecZnxDftAlloc<B>, Module<B>: VecZnxDftAlloc<B>,
@@ -57,25 +60,25 @@ impl<B: Backend> GLWEPublicKeyExec<Vec<u8>, B> {
{ {
module.vec_znx_dft_alloc_bytes(n, rank + 1, k.div_ceil(basek)) module.vec_znx_dft_alloc_bytes(n, rank + 1, k.div_ceil(basek))
} }
}
pub fn from<DataOther>(module: &Module<B>, other: &GLWEPublicKey<DataOther>, scratch: &mut Scratch<B>) -> Self impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWEPublicKeyPrepared<Vec<u8>, B>> for GLWEPublicKey<D>
where where
DataOther: DataRef, Module<B>: VecZnxDftAlloc<B> + VecZnxDftFromVecZnx<B>,
Module<B>: VecZnxDftAlloc<B> + VecZnxDftFromVecZnx<B>, {
{ fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GLWEPublicKeyPrepared<Vec<u8>, B> {
let mut pk_exec: GLWEPublicKeyExec<Vec<u8>, B> = let mut pk_prepared: GLWEPublicKeyPrepared<Vec<u8>, B> =
GLWEPublicKeyExec::alloc(module, other.n(), other.basek(), other.k(), other.rank()); GLWEPublicKeyPrepared::alloc(module, self.n(), self.basek(), self.k(), self.rank());
pk_exec.prepare(module, other, scratch); pk_prepared.prepare(module, self, scratch);
pk_exec pk_prepared
} }
} }
impl<D: DataMut, B: Backend> GLWEPublicKeyExec<D, B> { impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWEPublicKey<DR>> for GLWEPublicKeyPrepared<DM, B>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GLWEPublicKey<DataOther>, _scratch: &mut Scratch<B>) where
where Module<B>: VecZnxDftFromVecZnx<B>,
DataOther: DataRef, {
Module<B>: VecZnxDftFromVecZnx<B>, fn prepare(&mut self, module: &Module<B>, other: &GLWEPublicKey<DR>, _scratch: &mut Scratch<B>) {
{
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
assert_eq!(self.n(), other.n()); assert_eq!(self.n(), other.n());

View File

@@ -3,17 +3,24 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, SvpPPol}, layouts::{Backend, Data, DataMut, DataRef, Module, SvpPPol},
}; };
use crate::{dist::Distribution, layouts::GLWESecret, trait_families::GLWESecretExecModuleFamily}; use crate::{
dist::Distribution,
layouts::{
GLWESecret,
prepared::{Prepare, PrepareAlloc},
},
trait_families::GLWESecretPreparedModuleFamily,
};
pub struct GLWESecretExec<D: Data, B: Backend> { pub struct GLWESecretPrepared<D: Data, B: Backend> {
pub(crate) data: SvpPPol<D, B>, pub(crate) data: SvpPPol<D, B>,
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
} }
impl<B: Backend> GLWESecretExec<Vec<u8>, B> { impl<B: Backend> GLWESecretPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, rank: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, rank: usize) -> Self
where where
Module<B>: GLWESecretExecModuleFamily<B>, Module<B>: GLWESecretPreparedModuleFamily<B>,
{ {
Self { Self {
data: module.svp_ppol_alloc(n, rank), data: module.svp_ppol_alloc(n, rank),
@@ -23,25 +30,13 @@ impl<B: Backend> GLWESecretExec<Vec<u8>, B> {
pub fn bytes_of(module: &Module<B>, n: usize, rank: usize) -> usize pub fn bytes_of(module: &Module<B>, n: usize, rank: usize) -> usize
where where
Module<B>: GLWESecretExecModuleFamily<B>, Module<B>: GLWESecretPreparedModuleFamily<B>,
{ {
module.svp_ppol_alloc_bytes(n, rank) module.svp_ppol_alloc_bytes(n, rank)
} }
} }
impl<B: Backend> GLWESecretExec<Vec<u8>, B> { impl<D: Data, B: Backend> GLWESecretPrepared<D, B> {
pub fn from<D>(module: &Module<B>, sk: &GLWESecret<D>) -> Self
where
D: DataRef,
Module<B>: GLWESecretExecModuleFamily<B>,
{
let mut sk_dft: GLWESecretExec<Vec<u8>, B> = Self::alloc(module, sk.n(), sk.rank());
sk_dft.prepare(module, sk);
sk_dft
}
}
impl<D: Data, B: Backend> GLWESecretExec<D, B> {
pub fn n(&self) -> usize { pub fn n(&self) -> usize {
self.data.n() self.data.n()
} }
@@ -55,15 +50,29 @@ impl<D: Data, B: Backend> GLWESecretExec<D, B> {
} }
} }
impl<D: DataMut, B: Backend> GLWESecretExec<D, B> { impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWESecretPrepared<Vec<u8>, B>> for GLWESecret<D>
pub(crate) fn prepare<O>(&mut self, module: &Module<B>, sk: &GLWESecret<O>) where
where Module<B>: SvpPrepare<B> + SvpPPolAllocBytes + SvpPPolAlloc<B>,
O: DataRef, {
Module<B>: GLWESecretExecModuleFamily<B>, fn prepare_alloc(
{ &self,
(0..self.rank()).for_each(|i| { module: &Module<B>,
module.svp_prepare(&mut self.data, i, &sk.data, i); scratch: &mut backend::hal::layouts::Scratch<B>,
}); ) -> GLWESecretPrepared<Vec<u8>, B> {
self.dist = sk.dist let mut sk_dft: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, self.n(), self.rank());
sk_dft.prepare(module, self, scratch);
sk_dft
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWESecret<DR>> for GLWESecretPrepared<DM, B>
where
Module<B>: SvpPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GLWESecret<DR>, _scratch: &mut backend::hal::layouts::Scratch<B>) {
(0..self.rank()).for_each(|i| {
module.svp_prepare(&mut self.data, i, &other.data, i);
});
self.dist = other.dist
} }
} }

View File

@@ -3,12 +3,15 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{GLWEToLWESwitchingKey, Infos, prepared::GGLWESwitchingKeyExec}; use crate::layouts::{
GLWEToLWESwitchingKey, Infos,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GLWEToLWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>); pub struct GLWEToLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> Infos for GLWEToLWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> Infos for GLWEToLWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -24,7 +27,7 @@ impl<D: Data, B: Backend> Infos for GLWEToLWESwitchingKeyExec<D, B> {
} }
} }
impl<D: Data, B: Backend> GLWEToLWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
pub fn digits(&self) -> usize { pub fn digits(&self) -> usize {
self.0.digits() self.0.digits()
} }
@@ -42,12 +45,12 @@ impl<D: Data, B: Backend> GLWEToLWESwitchingKeyExec<D, B> {
} }
} }
impl<B: Backend> GLWEToLWESwitchingKeyExec<Vec<u8>, B> { impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_in: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_in: usize) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
Self(GGLWESwitchingKeyExec::alloc( Self(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, 1, rank_in, 1, module, n, basek, k, rows, 1, rank_in, 1,
)) ))
} }
@@ -56,36 +59,33 @@ impl<B: Backend> GLWEToLWESwitchingKeyExec<Vec<u8>, B> {
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1) GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1)
}
pub fn from<DataOther: DataRef>(
module: &Module<B>,
other: &GLWEToLWESwitchingKey<DataOther>,
scratch: &mut Scratch<B>,
) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: GLWEToLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.0.n(),
other.0.basek(),
other.0.k(),
other.0.rows(),
other.0.rank_in(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
} }
} }
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyExec<D, B> { impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>> for GLWEToLWESwitchingKey<D>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GLWEToLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) where
where Module<B>: VmpPMatPrepare<B> + VmpPMatAlloc<B>,
DataOther: DataRef, {
Module<B>: VmpPMatPrepare<B>, fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
{ let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = GLWEToLWESwitchingKeyPrepared::alloc(
module,
self.0.n(),
self.0.basek(),
self.0.k(),
self.0.rows(),
self.0.rank_in(),
);
ksk_prepared.prepare(module, self, scratch);
ksk_prepared
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWEToLWESwitchingKey<DR>> for GLWEToLWESwitchingKeyPrepared<DM, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GLWEToLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch); self.0.prepare(module, &other.0, scratch);
} }
} }

View File

@@ -3,12 +3,15 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{Infos, LWESwitchingKey, prepared::GGLWESwitchingKeyExec}; use crate::layouts::{
Infos, LWESwitchingKey,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct LWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>); pub struct LWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> Infos for LWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -24,7 +27,7 @@ impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<D, B> {
} }
} }
impl<D: Data, B: Backend> LWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> LWESwitchingKeyPrepared<D, B> {
pub fn digits(&self) -> usize { pub fn digits(&self) -> usize {
self.0.digits() self.0.digits()
} }
@@ -42,12 +45,12 @@ impl<D: Data, B: Backend> LWESwitchingKeyExec<D, B> {
} }
} }
impl<B: Backend> LWESwitchingKeyExec<Vec<u8>, B> { impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
Self(GGLWESwitchingKeyExec::alloc( Self(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, 1, 1, 1, module, n, basek, k, rows, 1, 1, 1,
)) ))
} }
@@ -56,31 +59,32 @@ impl<B: Backend> LWESwitchingKeyExec<Vec<u8>, B> {
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1) GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1)
}
pub fn from<DataOther: DataRef>(module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: LWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.0.n(),
other.0.basek(),
other.0.k(),
other.0.rows(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
} }
} }
impl<D: DataMut, B: Backend> LWESwitchingKeyExec<D, B> { impl<D: DataRef, B: Backend> PrepareAlloc<B, LWESwitchingKeyPrepared<Vec<u8>, B>> for LWESwitchingKey<D>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) where
where Module<B>: VmpPMatPrepare<B> + VmpPMatAlloc<B>,
DataOther: DataRef, {
Module<B>: VmpPMatPrepare<B>, fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> LWESwitchingKeyPrepared<Vec<u8>, B> {
{ let mut ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = LWESwitchingKeyPrepared::alloc(
module,
self.0.n(),
self.0.basek(),
self.0.k(),
self.0.rows(),
);
ksk_prepared.prepare(module, self, scratch);
ksk_prepared
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, LWESwitchingKey<DR>> for LWESwitchingKeyPrepared<DM, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &LWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch); self.0.prepare(module, &other.0, scratch);
} }
} }

View File

@@ -3,13 +3,16 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
}; };
use crate::layouts::{Infos, LWEToGLWESwitchingKey, prepared::GGLWESwitchingKeyExec}; use crate::layouts::{
Infos, LWEToGLWESwitchingKey,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
/// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. /// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext].
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct LWEToGLWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>); pub struct LWEToGLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>; type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
@@ -25,7 +28,7 @@ impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyExec<D, B> {
} }
} }
impl<D: Data, B: Backend> LWEToGLWESwitchingKeyExec<D, B> { impl<D: Data, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> {
pub fn digits(&self) -> usize { pub fn digits(&self) -> usize {
self.0.digits() self.0.digits()
} }
@@ -43,12 +46,12 @@ impl<D: Data, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
} }
} }
impl<B: Backend> LWEToGLWESwitchingKeyExec<Vec<u8>, B> { impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
Self(GGLWESwitchingKeyExec::alloc( Self(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, 1, 1, rank_out, module, n, basek, k, rows, 1, 1, rank_out,
)) ))
} }
@@ -57,36 +60,33 @@ impl<B: Backend> LWEToGLWESwitchingKeyExec<Vec<u8>, B> {
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out) GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out)
}
pub fn from<DataOther: DataRef>(
module: &Module<B>,
other: &LWEToGLWESwitchingKey<DataOther>,
scratch: &mut Scratch<B>,
) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: LWEToGLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.0.n(),
other.0.basek(),
other.0.k(),
other.0.rows(),
other.0.rank(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
} }
} }
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyExec<D, B> { impl<D: DataRef, B: Backend> PrepareAlloc<B, LWEToGLWESwitchingKeyPrepared<Vec<u8>, B>> for LWEToGLWESwitchingKey<D>
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) where
where Module<B>: VmpPMatPrepare<B> + VmpPMatAlloc<B>,
DataOther: DataRef, {
Module<B>: VmpPMatPrepare<B>, fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
{ let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = LWEToGLWESwitchingKeyPrepared::alloc(
module,
self.0.n(),
self.0.basek(),
self.0.k(),
self.0.rows(),
self.0.rank_out(),
);
ksk_prepared.prepare(module, self, scratch);
ksk_prepared
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, LWEToGLWESwitchingKey<DR>> for LWEToGLWESwitchingKeyPrepared<DM, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch); self.0.prepare(module, &other.0, scratch);
} }
} }

View File

@@ -9,6 +9,7 @@ mod glwe_to_lwe_ksk;
mod lwe_ksk; mod lwe_ksk;
mod lwe_to_glwe_ksk; mod lwe_to_glwe_ksk;
use backend::hal::layouts::{Backend, Module, Scratch};
pub use gglwe_atk::*; pub use gglwe_atk::*;
pub use gglwe_ct::*; pub use gglwe_ct::*;
pub use gglwe_ksk::*; pub use gglwe_ksk::*;
@@ -19,3 +20,11 @@ pub use glwe_sk::*;
pub use glwe_to_lwe_ksk::*; pub use glwe_to_lwe_ksk::*;
pub use lwe_ksk::*; pub use lwe_ksk::*;
pub use lwe_to_glwe_ksk::*; pub use lwe_to_glwe_ksk::*;
pub trait PrepareAlloc<B: Backend, T> {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> T;
}
pub trait Prepare<B: Backend, T> {
fn prepare(&mut self, module: &Module<B>, other: &T, scratch: &mut Scratch<B>);
}

View File

@@ -11,10 +11,12 @@ mod keyswitching;
mod noise; mod noise;
mod operations; mod operations;
mod scratch; mod scratch;
mod utils;
pub use operations::*; pub use operations::*;
pub mod layouts; pub mod layouts;
pub mod trait_families; pub mod trait_families;
pub use dist::*;
pub use glwe_packing::*; pub use glwe_packing::*;
pub use scratch::*; pub use scratch::*;

View File

@@ -1,11 +1,11 @@
use backend::hal::{ use backend::hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxStd, VecZnxSubScalarInplace, ZnxZero}, api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxSubScalarInplace, ZnxZero},
layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned}, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
}; };
use crate::{ use crate::{
layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
trait_families::GLWEDecryptFamily, trait_families::GLWEDecryptFamily,
}; };
@@ -13,13 +13,13 @@ impl<D: DataRef> GGLWECiphertext<D> {
pub fn assert_noise<B: Backend, DataSk, DataWant>( pub fn assert_noise<B: Backend, DataSk, DataWant>(
self, self,
module: &Module<B>, module: &Module<B>,
sk: &GLWESecretExec<DataSk, B>, sk: &GLWESecretPrepared<DataSk, B>,
pt_want: &ScalarZnx<DataWant>, pt_want: &ScalarZnx<DataWant>,
max_noise: f64, max_noise: f64,
) where ) where
DataSk: DataRef, DataSk: DataRef,
DataWant: DataRef, DataWant: DataRef,
Module<B>: GLWEDecryptFamily<B> + VecZnxStd + VecZnxSubScalarInplace, Module<B>: GLWEDecryptFamily<B> + VecZnxSubScalarInplace,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{ {
let digits: usize = self.digits(); let digits: usize = self.digits();
@@ -47,7 +47,7 @@ impl<D: DataRef> GGLWECiphertext<D> {
col_i, col_i,
); );
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2(); let noise_have: f64 = pt.data.std(basek, 0).log2();
assert!( assert!(
noise_have <= max_noise, noise_have <= max_noise,

View File

@@ -1,14 +1,14 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxDftAlloc, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxDftAlloc,
VecZnxDftToVecZnxBigTmpA, VecZnxNormalizeTmpBytes, VecZnxStd, VecZnxSubABInplace, ZnxZero, VecZnxDftToVecZnxBigTmpA, VecZnxNormalizeTmpBytes, VecZnxSubABInplace, ZnxZero,
}, },
layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft}, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
}; };
use crate::{ use crate::{
layouts::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, layouts::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
trait_families::GGSWAssertNoiseFamily, trait_families::GGSWAssertNoiseFamily,
}; };
@@ -16,13 +16,13 @@ impl<D: DataRef> GGSWCiphertext<D> {
pub fn assert_noise<B: Backend, DataSk, DataScalar, F>( pub fn assert_noise<B: Backend, DataSk, DataScalar, F>(
&self, &self,
module: &Module<B>, module: &Module<B>,
sk_exec: &GLWESecretExec<DataSk, B>, sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &ScalarZnx<DataScalar>, pt_want: &ScalarZnx<DataScalar>,
max_noise: F, max_noise: F,
) where ) where
DataSk: DataRef, DataSk: DataRef,
DataScalar: DataRef, DataScalar: DataRef,
Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace + VecZnxStd, Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
F: Fn(usize) -> f64, F: Fn(usize) -> f64,
{ {
@@ -46,17 +46,17 @@ impl<D: DataRef> GGSWCiphertext<D> {
// mul with sk[col_j-1] // mul with sk[col_j-1]
if col_j > 0 { if col_j > 0 {
module.vec_znx_dft_from_vec_znx(1, 0, &mut pt_dft, 0, &pt.data, 0); module.vec_znx_dft_from_vec_znx(1, 0, &mut pt_dft, 0, &pt.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_exec.data, col_j - 1); module.svp_apply_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow()); module.vec_znx_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow());
} }
self.at(row_i, col_j) self.at(row_i, col_j)
.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); .decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0);
let std_pt: f64 = module.vec_znx_std(basek, &pt_have.data, 0).log2(); let std_pt: f64 = pt_have.data.std(basek, 0).log2();
let noise: f64 = max_noise(col_j); let noise: f64 = max_noise(col_j);
println!("{} {}", std_pt, noise); println!("{} {}", std_pt, noise);
assert!(std_pt <= noise, "{} > {}", std_pt, noise); assert!(std_pt <= noise, "{} > {}", std_pt, noise);
@@ -71,12 +71,12 @@ impl<D: DataRef> GGSWCiphertext<D> {
pub fn print_noise<B: Backend, DataSk, DataScalar>( pub fn print_noise<B: Backend, DataSk, DataScalar>(
&self, &self,
module: &Module<B>, module: &Module<B>,
sk_exec: &GLWESecretExec<DataSk, B>, sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &ScalarZnx<DataScalar>, pt_want: &ScalarZnx<DataScalar>,
) where ) where
DataSk: DataRef, DataSk: DataRef,
DataScalar: DataRef, DataScalar: DataRef,
Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace + VecZnxStd, Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{ {
let basek: usize = self.basek(); let basek: usize = self.basek();
@@ -99,17 +99,17 @@ impl<D: DataRef> GGSWCiphertext<D> {
// mul with sk[col_j-1] // mul with sk[col_j-1]
if col_j > 0 { if col_j > 0 {
module.vec_znx_dft_from_vec_znx(1, 0, &mut pt_dft, 0, &pt.data, 0); module.vec_znx_dft_from_vec_znx(1, 0, &mut pt_dft, 0, &pt.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_exec.data, col_j - 1); module.svp_apply_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow()); module.vec_znx_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow());
} }
self.at(row_i, col_j) self.at(row_i, col_j)
.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); .decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0);
let std_pt: f64 = module.vec_znx_std(basek, &pt_have.data, 0).log2(); let std_pt: f64 = pt_have.data.std(basek, 0).log2();
println!("{}", std_pt); println!("{}", std_pt);
pt.data.zero(); pt.data.zero();
}); });

View File

@@ -1,12 +1,12 @@
use backend::hal::{ use backend::hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxStd, VecZnxSubABInplace}, api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubABInplace},
layouts::{Backend, DataRef, Module, ScratchOwned}, layouts::{Backend, DataRef, Module, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
}; };
use crate::{ use crate::{
layouts::GLWEPlaintext, layouts::GLWEPlaintext,
layouts::prepared::GLWESecretExec, layouts::prepared::GLWESecretPrepared,
layouts::{GLWECiphertext, Infos}, layouts::{GLWECiphertext, Infos},
trait_families::GLWEDecryptFamily, trait_families::GLWEDecryptFamily,
}; };
@@ -15,13 +15,13 @@ impl<D: DataRef> GLWECiphertext<D> {
pub fn assert_noise<B: Backend, DataSk, DataPt>( pub fn assert_noise<B: Backend, DataSk, DataPt>(
&self, &self,
module: &Module<B>, module: &Module<B>,
sk_exec: &GLWESecretExec<DataSk, B>, sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &GLWEPlaintext<DataPt>, pt_want: &GLWEPlaintext<DataPt>,
max_noise: f64, max_noise: f64,
) where ) where
DataSk: DataRef, DataSk: DataRef,
DataPt: DataRef, DataPt: DataRef,
Module<B>: GLWEDecryptFamily<B> + VecZnxSubABInplace + VecZnxNormalizeInplace<B> + VecZnxStd, Module<B>: GLWEDecryptFamily<B> + VecZnxSubABInplace + VecZnxNormalizeInplace<B>,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{ {
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), self.basek(), self.k()); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), self.basek(), self.k());
@@ -33,12 +33,12 @@ impl<D: DataRef> GLWECiphertext<D> {
self.k(), self.k(),
)); ));
self.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); self.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
module.vec_znx_normalize_inplace(self.basek(), &mut pt_have.data, 0, scratch.borrow()); module.vec_znx_normalize_inplace(self.basek(), &mut pt_have.data, 0, scratch.borrow());
let noise_have: f64 = module.vec_znx_std(self.basek(), &pt_have.data, 0).log2(); let noise_have: f64 = pt_have.data.std(self.basek(), 0).log2();
assert!(noise_have <= max_noise, "{} {}", noise_have, max_noise); assert!(noise_have <= max_noise, "{} {}", noise_have, max_noise);
} }
} }

View File

@@ -7,11 +7,11 @@ use backend::hal::{
use crate::{ use crate::{
dist::Distribution, dist::Distribution,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWECiphertext, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWEPublicKey, GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEPlaintext,
GLWESecret, GGLWESwitchingKey, Infos, GLWEPublicKey, GLWESecret, Infos,
prepared::{ prepared::{
GGLWEAutomorphismKeyExec, GGLWECiphertextExec, GGSWCiphertextExec, GLWEPublicKeyExec, GLWESecretExec, GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared,
GGLWESwitchingKeyExec, GGLWETensorKeyExec, GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared,
}, },
}, },
}; };
@@ -53,8 +53,8 @@ pub trait TakeGGLWE<B: Backend> {
) -> (GGLWECiphertext<&mut [u8]>, &mut Self); ) -> (GGLWECiphertext<&mut [u8]>, &mut Self);
} }
pub trait TakeGGLWEExec<B: Backend> { pub trait TakeGGLWEPrepared<B: Backend> {
fn take_gglwe_exec( fn take_gglwe_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -63,7 +63,7 @@ pub trait TakeGGLWEExec<B: Backend> {
digits: usize, digits: usize,
rank_in: usize, rank_in: usize,
rank_out: usize, rank_out: usize,
) -> (GGLWECiphertextExec<&mut [u8], B>, &mut Self); ) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self);
} }
pub trait TakeGGSW<B: Backend> { pub trait TakeGGSW<B: Backend> {
@@ -78,8 +78,8 @@ pub trait TakeGGSW<B: Backend> {
) -> (GGSWCiphertext<&mut [u8]>, &mut Self); ) -> (GGSWCiphertext<&mut [u8]>, &mut Self);
} }
pub trait TakeGGSWExec<B: Backend> { pub trait TakeGGSWPrepared<B: Backend> {
fn take_ggsw_exec( fn take_ggsw_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -87,29 +87,29 @@ pub trait TakeGGSWExec<B: Backend> {
rows: usize, rows: usize,
digits: usize, digits: usize,
rank: usize, rank: usize,
) -> (GGSWCiphertextExec<&mut [u8], B>, &mut Self); ) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self);
} }
pub trait TakeGLWESecret<B: Backend> { pub trait TakeGLWESecret<B: Backend> {
fn take_glwe_secret(&mut self, n: usize, rank: usize) -> (GLWESecret<&mut [u8]>, &mut Self); fn take_glwe_secret(&mut self, n: usize, rank: usize) -> (GLWESecret<&mut [u8]>, &mut Self);
} }
pub trait TakeGLWESecretExec<B: Backend> { pub trait TakeGLWESecretPrepared<B: Backend> {
fn take_glwe_secret_exec(&mut self, n: usize, rank: usize) -> (GLWESecretExec<&mut [u8], B>, &mut Self); fn take_glwe_secret_prepared(&mut self, n: usize, rank: usize) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self);
} }
pub trait TakeGLWEPk<B: Backend> { pub trait TakeGLWEPk<B: Backend> {
fn take_glwe_pk(&mut self, n: usize, basek: usize, k: usize, rank: usize) -> (GLWEPublicKey<&mut [u8]>, &mut Self); fn take_glwe_pk(&mut self, n: usize, basek: usize, k: usize, rank: usize) -> (GLWEPublicKey<&mut [u8]>, &mut Self);
} }
pub trait TakeGLWEPkExec<B: Backend> { pub trait TakeGLWEPkPrepared<B: Backend> {
fn take_glwe_pk_exec( fn take_glwe_pk_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
k: usize, k: usize,
rank: usize, rank: usize,
) -> (GLWEPublicKeyExec<&mut [u8], B>, &mut Self); ) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self);
} }
pub trait TakeGLWESwitchingKey<B: Backend> { pub trait TakeGLWESwitchingKey<B: Backend> {
@@ -125,8 +125,8 @@ pub trait TakeGLWESwitchingKey<B: Backend> {
) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self); ) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self);
} }
pub trait TakeGLWESwitchingKeyExec<B: Backend> { pub trait TakeGLWESwitchingKeyPrepared<B: Backend> {
fn take_glwe_switching_key_exec( fn take_glwe_switching_key_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -135,7 +135,7 @@ pub trait TakeGLWESwitchingKeyExec<B: Backend> {
digits: usize, digits: usize,
rank_in: usize, rank_in: usize,
rank_out: usize, rank_out: usize,
) -> (GGLWESwitchingKeyExec<&mut [u8], B>, &mut Self); ) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self);
} }
pub trait TakeTensorKey<B: Backend> { pub trait TakeTensorKey<B: Backend> {
@@ -150,8 +150,8 @@ pub trait TakeTensorKey<B: Backend> {
) -> (GGLWETensorKey<&mut [u8]>, &mut Self); ) -> (GGLWETensorKey<&mut [u8]>, &mut Self);
} }
pub trait TakeTensorKeyExec<B: Backend> { pub trait TakeTensorKeyPrepared<B: Backend> {
fn take_tensor_key_exec( fn take_tensor_key_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -159,7 +159,7 @@ pub trait TakeTensorKeyExec<B: Backend> {
rows: usize, rows: usize,
digits: usize, digits: usize,
rank: usize, rank: usize,
) -> (GGLWETensorKeyExec<&mut [u8], B>, &mut Self); ) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self);
} }
pub trait TakeAutomorphismKey<B: Backend> { pub trait TakeAutomorphismKey<B: Backend> {
@@ -174,8 +174,8 @@ pub trait TakeAutomorphismKey<B: Backend> {
) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self); ) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self);
} }
pub trait TakeAutomorphismKeyExec<B: Backend> { pub trait TakeAutomorphismKeyPrepared<B: Backend> {
fn take_automorphism_key_exec( fn take_automorphism_key_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -183,7 +183,7 @@ pub trait TakeAutomorphismKeyExec<B: Backend> {
rows: usize, rows: usize,
digits: usize, digits: usize,
rank: usize, rank: usize,
) -> (GGLWEAutomorphismKeyExec<&mut [u8], B>, &mut Self); ) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self);
} }
impl<B: Backend> TakeGLWECt<B> for Scratch<B> impl<B: Backend> TakeGLWECt<B> for Scratch<B>
@@ -330,11 +330,11 @@ where
} }
} }
impl<B: Backend> TakeGGLWEExec<B> for Scratch<B> impl<B: Backend> TakeGGLWEPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeVmpPMat<B>, Scratch<B>: TakeVmpPMat<B>,
{ {
fn take_gglwe_exec( fn take_gglwe_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -343,7 +343,7 @@ where
digits: usize, digits: usize,
rank_in: usize, rank_in: usize,
rank_out: usize, rank_out: usize,
) -> (GGLWECiphertextExec<&mut [u8], B>, &mut Self) { ) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_vmp_pmat( let (data, scratch) = self.take_vmp_pmat(
n, n,
rows.div_ceil(digits), rows.div_ceil(digits),
@@ -352,7 +352,7 @@ where
k.div_ceil(basek), k.div_ceil(basek),
); );
( (
GGLWECiphertextExec { GGLWECiphertextPrepared {
data: data, data: data,
basek: basek, basek: basek,
k, k,
@@ -363,14 +363,14 @@ where
} }
} }
impl<'a, B, D> TakeLike<'a, B, GGLWECiphertextExec<D, B>> for Scratch<B> impl<'a, B, D> TakeLike<'a, B, GGLWECiphertextPrepared<D, B>> for Scratch<B>
where where
B: Backend + TakeVmpPMatImpl<B>, B: Backend + TakeVmpPMatImpl<B>,
D: DataRef, D: DataRef,
{ {
type Output = GGLWECiphertextExec<&'a mut [u8], B>; type Output = GGLWECiphertextPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWECiphertextExec<D, B>) -> (Self::Output, &'a mut Self) { fn take_like(&'a mut self, template: &GGLWECiphertextPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_vmp_pmat_impl( let (data, scratch) = B::take_vmp_pmat_impl(
self, self,
template.n(), template.n(),
@@ -380,7 +380,7 @@ where
template.size(), template.size(),
); );
( (
GGLWECiphertextExec { GGLWECiphertextPrepared {
data, data,
basek: template.basek(), basek: template.basek(),
k: template.k(), k: template.k(),
@@ -451,11 +451,11 @@ where
} }
} }
impl<B: Backend> TakeGGSWExec<B> for Scratch<B> impl<B: Backend> TakeGGSWPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeVmpPMat<B>, Scratch<B>: TakeVmpPMat<B>,
{ {
fn take_ggsw_exec( fn take_ggsw_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -463,7 +463,7 @@ where
rows: usize, rows: usize,
digits: usize, digits: usize,
rank: usize, rank: usize,
) -> (GGSWCiphertextExec<&mut [u8], B>, &mut Self) { ) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_vmp_pmat( let (data, scratch) = self.take_vmp_pmat(
n, n,
rows.div_ceil(digits), rows.div_ceil(digits),
@@ -472,7 +472,7 @@ where
k.div_ceil(basek), k.div_ceil(basek),
); );
( (
GGSWCiphertextExec { GGSWCiphertextPrepared {
data, data,
basek, basek,
k, k,
@@ -483,14 +483,14 @@ where
} }
} }
impl<'a, B, D> TakeLike<'a, B, GGSWCiphertextExec<D, B>> for Scratch<B> impl<'a, B, D> TakeLike<'a, B, GGSWCiphertextPrepared<D, B>> for Scratch<B>
where where
B: Backend + TakeVmpPMatImpl<B>, B: Backend + TakeVmpPMatImpl<B>,
D: DataRef, D: DataRef,
{ {
type Output = GGSWCiphertextExec<&'a mut [u8], B>; type Output = GGSWCiphertextPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGSWCiphertextExec<D, B>) -> (Self::Output, &'a mut Self) { fn take_like(&'a mut self, template: &GGSWCiphertextPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_vmp_pmat_impl( let (data, scratch) = B::take_vmp_pmat_impl(
self, self,
template.n(), template.n(),
@@ -500,7 +500,7 @@ where
template.size(), template.size(),
); );
( (
GGSWCiphertextExec { GGSWCiphertextPrepared {
data, data,
basek: template.basek(), basek: template.basek(),
k: template.k(), k: template.k(),
@@ -550,20 +550,20 @@ where
} }
} }
impl<B: Backend> TakeGLWEPkExec<B> for Scratch<B> impl<B: Backend> TakeGLWEPkPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeVecZnxDft<B>, Scratch<B>: TakeVecZnxDft<B>,
{ {
fn take_glwe_pk_exec( fn take_glwe_pk_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
k: usize, k: usize,
rank: usize, rank: usize,
) -> (GLWEPublicKeyExec<&mut [u8], B>, &mut Self) { ) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_vec_znx_dft(n, rank + 1, k.div_ceil(basek)); let (data, scratch) = self.take_vec_znx_dft(n, rank + 1, k.div_ceil(basek));
( (
GLWEPublicKeyExec { GLWEPublicKeyPrepared {
data, data,
k, k,
basek, basek,
@@ -574,17 +574,17 @@ where
} }
} }
impl<'a, B, D> TakeLike<'a, B, GLWEPublicKeyExec<D, B>> for Scratch<B> impl<'a, B, D> TakeLike<'a, B, GLWEPublicKeyPrepared<D, B>> for Scratch<B>
where where
B: Backend + TakeVecZnxDftImpl<B>, B: Backend + TakeVecZnxDftImpl<B>,
D: DataRef, D: DataRef,
{ {
type Output = GLWEPublicKeyExec<&'a mut [u8], B>; type Output = GLWEPublicKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GLWEPublicKeyExec<D, B>) -> (Self::Output, &'a mut Self) { fn take_like(&'a mut self, template: &GLWEPublicKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_vec_znx_dft_impl(self, template.n(), template.cols(), template.size()); let (data, scratch) = B::take_vec_znx_dft_impl(self, template.n(), template.cols(), template.size());
( (
GLWEPublicKeyExec { GLWEPublicKeyPrepared {
data, data,
basek: template.basek(), basek: template.basek(),
k: template.k(), k: template.k(),
@@ -630,14 +630,14 @@ where
} }
} }
impl<B: Backend> TakeGLWESecretExec<B> for Scratch<B> impl<B: Backend> TakeGLWESecretPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeSvpPPol<B>, Scratch<B>: TakeSvpPPol<B>,
{ {
fn take_glwe_secret_exec(&mut self, n: usize, rank: usize) -> (GLWESecretExec<&mut [u8], B>, &mut Self) { fn take_glwe_secret_prepared(&mut self, n: usize, rank: usize) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_svp_ppol(n, rank); let (data, scratch) = self.take_svp_ppol(n, rank);
( (
GLWESecretExec { GLWESecretPrepared {
data, data,
dist: Distribution::NONE, dist: Distribution::NONE,
}, },
@@ -646,17 +646,17 @@ where
} }
} }
impl<'a, B, D> TakeLike<'a, B, GLWESecretExec<D, B>> for Scratch<B> impl<'a, B, D> TakeLike<'a, B, GLWESecretPrepared<D, B>> for Scratch<B>
where where
B: Backend + TakeSvpPPolImpl<B>, B: Backend + TakeSvpPPolImpl<B>,
D: DataRef, D: DataRef,
{ {
type Output = GLWESecretExec<&'a mut [u8], B>; type Output = GLWESecretPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GLWESecretExec<D, B>) -> (Self::Output, &'a mut Self) { fn take_like(&'a mut self, template: &GLWESecretPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_svp_ppol_impl(self, template.n(), template.rank()); let (data, scratch) = B::take_svp_ppol_impl(self, template.n(), template.rank());
( (
GLWESecretExec { GLWESecretPrepared {
data, data,
dist: template.dist, dist: template.dist,
}, },
@@ -712,11 +712,11 @@ where
} }
} }
impl<B: Backend> TakeGLWESwitchingKeyExec<B> for Scratch<B> impl<B: Backend> TakeGLWESwitchingKeyPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeGGLWEExec<B>, Scratch<B>: TakeGGLWEPrepared<B>,
{ {
fn take_glwe_switching_key_exec( fn take_glwe_switching_key_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -725,10 +725,10 @@ where
digits: usize, digits: usize,
rank_in: usize, rank_in: usize,
rank_out: usize, rank_out: usize,
) -> (GGLWESwitchingKeyExec<&mut [u8], B>, &mut Self) { ) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_gglwe_exec(n, basek, k, rows, digits, rank_in, rank_out); let (data, scratch) = self.take_gglwe_prepared(n, basek, k, rows, digits, rank_in, rank_out);
( (
GGLWESwitchingKeyExec { GGLWESwitchingKeyPrepared {
key: data, key: data,
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
@@ -738,18 +738,18 @@ where
} }
} }
impl<'a, B, D> TakeLike<'a, B, GGLWESwitchingKeyExec<D, B>> for Scratch<B> impl<'a, B, D> TakeLike<'a, B, GGLWESwitchingKeyPrepared<D, B>> for Scratch<B>
where where
Scratch<B>: TakeLike<'a, B, GGLWECiphertextExec<D, B>, Output = GGLWECiphertextExec<&'a mut [u8], B>>, Scratch<B>: TakeLike<'a, B, GGLWECiphertextPrepared<D, B>, Output = GGLWECiphertextPrepared<&'a mut [u8], B>>,
B: Backend + TakeMatZnxImpl<B>, B: Backend + TakeMatZnxImpl<B>,
D: DataRef, D: DataRef,
{ {
type Output = GGLWESwitchingKeyExec<&'a mut [u8], B>; type Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWESwitchingKeyExec<D, B>) -> (Self::Output, &'a mut Self) { fn take_like(&'a mut self, template: &GGLWESwitchingKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (key, scratch) = self.take_like(&template.key); let (key, scratch) = self.take_like(&template.key);
( (
GGLWESwitchingKeyExec { GGLWESwitchingKeyPrepared {
key, key,
sk_in_n: template.sk_in_n, sk_in_n: template.sk_in_n,
sk_out_n: template.sk_out_n, sk_out_n: template.sk_out_n,
@@ -791,11 +791,11 @@ where
} }
} }
impl<B: Backend> TakeAutomorphismKeyExec<B> for Scratch<B> impl<B: Backend> TakeAutomorphismKeyPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeGLWESwitchingKeyExec<B>, Scratch<B>: TakeGLWESwitchingKeyPrepared<B>,
{ {
fn take_automorphism_key_exec( fn take_automorphism_key_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -803,23 +803,23 @@ where
rows: usize, rows: usize,
digits: usize, digits: usize,
rank: usize, rank: usize,
) -> (GGLWEAutomorphismKeyExec<&mut [u8], B>, &mut Self) { ) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_glwe_switching_key_exec(n, basek, k, rows, digits, rank, rank); let (data, scratch) = self.take_glwe_switching_key_prepared(n, basek, k, rows, digits, rank, rank);
(GGLWEAutomorphismKeyExec { key: data, p: 0 }, scratch) (GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch)
} }
} }
impl<'a, B, D> TakeLike<'a, B, GGLWEAutomorphismKeyExec<D, B>> for Scratch<B> impl<'a, B, D> TakeLike<'a, B, GGLWEAutomorphismKeyPrepared<D, B>> for Scratch<B>
where where
Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyExec<D, B>, Output = GGLWESwitchingKeyExec<&'a mut [u8], B>>, Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyPrepared<D, B>, Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>>,
B: Backend + TakeMatZnxImpl<B>, B: Backend + TakeMatZnxImpl<B>,
D: DataRef, D: DataRef,
{ {
type Output = GGLWEAutomorphismKeyExec<&'a mut [u8], B>; type Output = GGLWEAutomorphismKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWEAutomorphismKeyExec<D, B>) -> (Self::Output, &'a mut Self) { fn take_like(&'a mut self, template: &GGLWEAutomorphismKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (key, scratch) = self.take_like(&template.key); let (key, scratch) = self.take_like(&template.key);
(GGLWEAutomorphismKeyExec { key, p: template.p }, scratch) (GGLWEAutomorphismKeyPrepared { key, p: template.p }, scratch)
} }
} }
@@ -884,11 +884,11 @@ where
} }
} }
impl<B: Backend> TakeTensorKeyExec<B> for Scratch<B> impl<B: Backend> TakeTensorKeyPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeVmpPMat<B>, Scratch<B>: TakeVmpPMat<B>,
{ {
fn take_tensor_key_exec( fn take_tensor_key_prepared(
&mut self, &mut self,
n: usize, n: usize,
basek: usize, basek: usize,
@@ -896,36 +896,36 @@ where
rows: usize, rows: usize,
digits: usize, digits: usize,
rank: usize, rank: usize,
) -> (GGLWETensorKeyExec<&mut [u8], B>, &mut Self) { ) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) {
let mut keys: Vec<GGLWESwitchingKeyExec<&mut [u8], B>> = Vec::new(); let mut keys: Vec<GGLWESwitchingKeyPrepared<&mut [u8], B>> = Vec::new();
let pairs: usize = (((rank + 1) * rank) >> 1).max(1); let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
let mut scratch: &mut Scratch<B> = self; let mut scratch: &mut Scratch<B> = self;
if pairs != 0 { if pairs != 0 {
let (gglwe, s) = scratch.take_glwe_switching_key_exec(n, basek, k, rows, digits, 1, rank); let (gglwe, s) = scratch.take_glwe_switching_key_prepared(n, basek, k, rows, digits, 1, rank);
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }
for _ in 1..pairs { for _ in 1..pairs {
let (gglwe, s) = scratch.take_glwe_switching_key_exec(n, basek, k, rows, digits, 1, rank); let (gglwe, s) = scratch.take_glwe_switching_key_prepared(n, basek, k, rows, digits, 1, rank);
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }
(GGLWETensorKeyExec { keys }, scratch) (GGLWETensorKeyPrepared { keys }, scratch)
} }
} }
impl<'a, B, D> TakeLike<'a, B, GGLWETensorKeyExec<D, B>> for Scratch<B> impl<'a, B, D> TakeLike<'a, B, GGLWETensorKeyPrepared<D, B>> for Scratch<B>
where where
Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyExec<D, B>, Output = GGLWESwitchingKeyExec<&'a mut [u8], B>>, Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyPrepared<D, B>, Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>>,
B: Backend + TakeMatZnxImpl<B>, B: Backend + TakeMatZnxImpl<B>,
D: DataRef, D: DataRef,
{ {
type Output = GGLWETensorKeyExec<&'a mut [u8], B>; type Output = GGLWETensorKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWETensorKeyExec<D, B>) -> (Self::Output, &'a mut Self) { fn take_like(&'a mut self, template: &GGLWETensorKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let mut keys: Vec<GGLWESwitchingKeyExec<&mut [u8], B>> = Vec::new(); let mut keys: Vec<GGLWESwitchingKeyPrepared<&mut [u8], B>> = Vec::new();
let pairs: usize = template.keys.len(); let pairs: usize = template.keys.len();
let mut scratch: &mut Scratch<B> = self; let mut scratch: &mut Scratch<B> = self;
@@ -941,6 +941,6 @@ where
keys.push(gglwe); keys.push(gglwe);
} }
(GGLWETensorKeyExec { keys }, scratch) (GGLWETensorKeyPrepared { keys }, scratch)
} }
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy,
VecZnxStd, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -14,13 +14,13 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GGLWEAutomorphismKey, GLWEPlaintext, GLWESecret, Infos, GGLWEAutomorphismKey, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec}, prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
}; };
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_automorphism_key_automorphism<B: Backend>( pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -35,7 +35,7 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
rank: usize, rank: usize,
) where ) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwithcDegree + VecZnxSwithcDegree
@@ -44,7 +44,6 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
@@ -102,13 +101,18 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_apply_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank); GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
auto_key_apply_exec.prepare(module, &auto_key_apply, scratch.borrow()); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
auto_key_out.automorphism(module, &auto_key_in, &auto_key_apply_exec, scratch.borrow()); auto_key_out.automorphism(
module,
&auto_key_in,
&auto_key_apply_prepared,
scratch.borrow(),
);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_out); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_out);
@@ -124,7 +128,7 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
); );
}); });
let sk_auto_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_auto); let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow());
(0..auto_key_out.rank_in()).for_each(|col_i| { (0..auto_key_out.rank_in()).for_each(|col_i| {
(0..auto_key_out.rows()).for_each(|row_i| { (0..auto_key_out.rows()).for_each(|row_i| {
@@ -140,7 +144,7 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
col_i, col_i,
); );
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2(); let noise_have: f64 = pt.data.std(basek, 0).log2();
let noise_want: f64 = log2_std_noise_gglwe_product( let noise_want: f64 = log2_std_noise_gglwe_product(
n as f64, n as f64,
basek * digits, basek * digits,
@@ -176,7 +180,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
rank: usize, rank: usize,
) where ) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwithcDegree + VecZnxSwithcDegree
@@ -185,7 +189,6 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
@@ -242,13 +245,13 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_apply_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank); GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
auto_key_apply_exec.prepare(module, &auto_key_apply, scratch.borrow()); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
auto_key.automorphism_inplace(module, &auto_key_apply_exec, scratch.borrow()); auto_key.automorphism_inplace(module, &auto_key_apply_prepared, scratch.borrow());
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_in); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_in);
@@ -265,7 +268,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
); );
}); });
let sk_auto_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_auto); let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow());
(0..auto_key.rank_in()).for_each(|col_i| { (0..auto_key.rank_in()).for_each(|col_i| {
(0..auto_key.rows()).for_each(|row_i| { (0..auto_key.rows()).for_each(|row_i| {
@@ -280,7 +283,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
col_i, col_i,
); );
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2(); let noise_have: f64 = pt.data.std(basek, 0).log2();
let noise_want: f64 = log2_std_noise_gglwe_product( let noise_want: f64 = log2_std_noise_gglwe_product(
n as f64, n as f64,
basek * digits, basek * digits,

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy,
VecZnxStd, VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScalarZnx, ScratchOwned}, layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{ oep::{
@@ -14,14 +14,14 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWETensorKey, GGSWCiphertext, GLWESecret, GGLWEAutomorphismKey, GGLWETensorKey, GGSWCiphertext, GLWESecret,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec, GGLWETensorKeyExec}, prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
}, },
noise::noise_ggsw_keyswitch, noise::noise_ggsw_keyswitch,
trait_families::GGSWAssertNoiseFamily, trait_families::GGSWAssertNoiseFamily,
}; };
use crate::trait_families::{ use crate::trait_families::{
GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWKeySwitchFamily, GLWESecretExecModuleFamily, GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWKeySwitchFamily, GLWESecretPreparedModuleFamily,
}; };
pub fn test_ggsw_automorphism<B: Backend>( pub fn test_ggsw_automorphism<B: Backend>(
@@ -37,10 +37,9 @@ pub fn test_ggsw_automorphism<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGSWAssertNoiseFamily<B> Module<B>: GGSWAssertNoiseFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VecZnxStd
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -92,7 +91,7 @@ pub fn test_ggsw_automorphism<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs); sk.fill_ternary_prob(var_xs, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
auto_key.encrypt_sk( auto_key.encrypt_sk(
module, module,
@@ -117,21 +116,28 @@ pub fn test_ggsw_automorphism<B: Backend>(
ct_in.encrypt_sk( ct_in.encrypt_sk(
module, module,
&pt_scalar, &pt_scalar,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
auto_key_exec.prepare(module, &auto_key, scratch.borrow()); auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_tsk, rows, digits, rank); let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> =
tsk_exec.prepare(module, &tensor_key, scratch.borrow()); GGLWETensorKeyPrepared::alloc(module, n, basek, k_tsk, rows, digits, rank);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct_out.automorphism(module, &ct_in, &auto_key_exec, &tsk_exec, scratch.borrow()); ct_out.automorphism(
module,
&ct_in,
&auto_key_prepared,
&tsk_prepared,
scratch.borrow(),
);
module.vec_znx_automorphism_inplace(p, &mut pt_scalar.as_vec_znx_mut(), 0); module.vec_znx_automorphism_inplace(p, &mut pt_scalar.as_vec_znx_mut(), 0);
@@ -151,7 +157,7 @@ pub fn test_ggsw_automorphism<B: Backend>(
) + 0.5 ) + 0.5
}; };
ct_out.assert_noise(module, &sk_exec, &pt_scalar, &max_noise); ct_out.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise);
} }
pub fn test_ggsw_automorphism_inplace<B: Backend>( pub fn test_ggsw_automorphism_inplace<B: Backend>(
@@ -166,10 +172,9 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGSWAssertNoiseFamily<B> Module<B>: GGSWAssertNoiseFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VecZnxStd
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -217,7 +222,7 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs); sk.fill_ternary_prob(var_xs, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
auto_key.encrypt_sk( auto_key.encrypt_sk(
module, module,
@@ -242,21 +247,22 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
ct.encrypt_sk( ct.encrypt_sk(
module, module,
&pt_scalar, &pt_scalar,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
auto_key_exec.prepare(module, &auto_key, scratch.borrow()); auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_tsk, rows, digits, rank); let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> =
tsk_exec.prepare(module, &tensor_key, scratch.borrow()); GGLWETensorKeyPrepared::alloc(module, n, basek, k_tsk, rows, digits, rank);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct.automorphism_inplace(module, &auto_key_exec, &tsk_exec, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow());
module.vec_znx_automorphism_inplace(p, &mut pt_scalar.as_vec_znx_mut(), 0); module.vec_znx_automorphism_inplace(p, &mut pt_scalar.as_vec_znx_mut(), 0);
@@ -276,5 +282,5 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
) + 0.5 ) + 0.5
}; };
ct.assert_noise(module, &sk_exec, &pt_scalar, &max_noise); ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
VecZnxFillUniform, VecZnxStd, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VecZnxFillUniform, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -14,13 +14,13 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec}, prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
}; };
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_automorphism<B: Backend>( pub fn test_glwe_automorphism<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -34,14 +34,13 @@ pub fn test_glwe_automorphism<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VecZnxStd
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B> B: TakeVecZnxDftImpl<B>
@@ -85,7 +84,7 @@ pub fn test_glwe_automorphism<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
autokey.encrypt_sk( autokey.encrypt_sk(
module, module,
@@ -100,18 +99,18 @@ pub fn test_glwe_automorphism<B: Backend>(
ct_in.encrypt_sk( ct_in.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut autokey_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
autokey_exec.prepare(module, &autokey, scratch.borrow()); autokey_prepared.prepare(module, &autokey, scratch.borrow());
ct_out.automorphism(module, &ct_in, &autokey_exec, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product( let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64, module.n() as f64,
@@ -128,7 +127,7 @@ pub fn test_glwe_automorphism<B: Backend>(
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0); module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
ct_out.assert_noise(module, &sk_exec, &pt_want, max_noise + 1.0); ct_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0);
} }
pub fn test_glwe_automorphism_inplace<B: Backend>( pub fn test_glwe_automorphism_inplace<B: Backend>(
@@ -142,14 +141,13 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VecZnxStd
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B> B: TakeVecZnxDftImpl<B>
@@ -183,7 +181,7 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
autokey.encrypt_sk( autokey.encrypt_sk(
module, module,
@@ -198,18 +196,18 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
ct.encrypt_sk( ct.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut autokey_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
autokey_exec.prepare(module, &autokey, scratch.borrow()); autokey_prepared.prepare(module, &autokey, scratch.borrow());
ct.automorphism_inplace(module, &autokey_exec, scratch.borrow()); ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product( let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64, module.n() as f64,
@@ -226,5 +224,5 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0); module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
ct.assert_noise(module, &sk_exec, &pt_want, max_noise + 1.0); ct.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxEncodeCoeffsi64, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxSwithcDegree,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, VmpPMatAlloc, VmpPMatPrepare, ZnxView,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -14,10 +14,10 @@ use sampling::source::Source;
use crate::layouts::{ use crate::layouts::{
GLWECiphertext, GLWEPlaintext, GLWESecret, GLWEToLWESwitchingKey, Infos, LWECiphertext, LWEPlaintext, LWESecret, GLWECiphertext, GLWEPlaintext, GLWESecret, GLWEToLWESwitchingKey, Infos, LWECiphertext, LWEPlaintext, LWESecret,
LWEToGLWESwitchingKey, LWEToGLWESwitchingKey,
prepared::{GLWESecretExec, GLWEToLWESwitchingKeyExec, LWEToGLWESwitchingKeyExec}, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretPreparedModuleFamily};
pub fn test_lwe_to_glwe<B: Backend>(module: &Module<B>) pub fn test_lwe_to_glwe<B: Backend>(module: &Module<B>)
where where
@@ -26,11 +26,10 @@ where
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxEncodeCoeffsi64
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
B: TakeScalarZnxImpl<B> B: TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B> + TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B> + ScratchAvailableImpl<B>
@@ -67,7 +66,7 @@ where
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs); sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_glwe); let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe); let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
sk_lwe.fill_ternary_prob(0.5, &mut source_xs); sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
@@ -75,7 +74,7 @@ where
let data: i64 = 17; let data: i64 = 17;
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt); let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
module.encode_coeff_i64(basek, &mut lwe_pt.data, 0, k_lwe_pt, 0, data, k_lwe_pt); lwe_pt.encode_i64(data, k_lwe_pt);
let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct); let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
lwe_ct.encrypt_sk( lwe_ct.encrypt_sk(
@@ -101,12 +100,12 @@ where
let mut glwe_ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank); let mut glwe_ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
let ksk_exec: LWEToGLWESwitchingKeyExec<Vec<u8>, B> = LWEToGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
glwe_ct.from_lwe(module, &lwe_ct, &ksk_exec, scratch.borrow()); glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct); let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_exec, scratch.borrow()); glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
} }
@@ -118,11 +117,10 @@ where
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxEncodeCoeffsi64
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
B: TakeScalarZnxImpl<B> B: TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B> + TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B> + ScratchAvailableImpl<B>
@@ -159,20 +157,20 @@ where
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs); sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_glwe); let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let mut sk_lwe = LWESecret::alloc(n_lwe); let mut sk_lwe = LWESecret::alloc(n_lwe);
sk_lwe.fill_ternary_prob(0.5, &mut source_xs); sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
let data: i64 = 17; let data: i64 = 17;
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct); let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
module.encode_coeff_i64(basek, &mut glwe_pt.data, 0, k_lwe_pt, 0, data, k_lwe_pt); glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0);
let mut glwe_ct = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank); let mut glwe_ct = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
glwe_ct.encrypt_sk( glwe_ct.encrypt_sk(
module, module,
&glwe_pt, &glwe_pt,
&sk_glwe_exec, &sk_glwe_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -193,9 +191,9 @@ where
let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct); let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
let ksk_exec: GLWEToLWESwitchingKeyExec<Vec<u8>, B> = GLWEToLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
lwe_ct.from_glwe(module, &glwe_ct, &ksk_exec, scratch.borrow()); lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct); let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct);
lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe); lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy,
VecZnxStd, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -12,11 +12,15 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
layouts::{GGLWEAutomorphismKey, GLWESecret, compressed::GGLWEAutomorphismKeyCompressed, prepared::GLWESecretExec}, layouts::{
GGLWEAutomorphismKey, GLWESecret,
compressed::GGLWEAutomorphismKeyCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GLWEDecryptFamily, GLWEKeyswitchFamily}, trait_families::{Decompress, GLWEDecryptFamily, GLWEKeyswitchFamily},
}; };
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>( pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -27,7 +31,7 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwithcDegree + VecZnxSwithcDegree
@@ -36,7 +40,6 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
@@ -87,11 +90,11 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
i, i,
); );
}); });
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
atk.key atk.key
.key .key
.assert_noise(module, &sk_out_exec, &sk.data, sigma); .assert_noise(module, &sk_out_prepared, &sk.data, sigma);
} }
pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>( pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
@@ -103,7 +106,7 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwithcDegree + VecZnxSwithcDegree
@@ -112,7 +115,6 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
@@ -165,12 +167,12 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
i, i,
); );
}); });
let sk_out_exec = GLWESecretExec::from(module, &sk_out); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow());
let mut atk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank); let mut atk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
atk.decompress(module, &atk_compressed); atk.decompress(module, &atk_compressed);
atk.key atk.key
.key .key
.assert_noise(module, &sk_out_exec, &sk.data, sigma); .assert_noise(module, &sk_out_prepared, &sk.data, sigma);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubScalarInplace, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubScalarInplace, VecZnxSwithcDegree,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -12,11 +12,15 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
layouts::{GLWESecret, GGLWESwitchingKey, compressed::GGLWESwitchingKeyCompressed, prepared::GLWESecretExec}, layouts::{
GGLWESwitchingKey, GLWESecret,
compressed::GGLWESwitchingKeyCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GLWEDecryptFamily}, trait_families::{Decompress, GLWEDecryptFamily},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>( pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -28,11 +32,10 @@ pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -67,7 +70,7 @@ pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk( ksk.encrypt_sk(
module, module,
@@ -80,7 +83,7 @@ pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
); );
ksk.key ksk.key
.assert_noise(module, &sk_out_exec, &sk_in.data, sigma); .assert_noise(module, &sk_out_prepared, &sk_in.data, sigma);
} }
pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>( pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
@@ -93,11 +96,10 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -132,7 +134,7 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
let seed_xa = [1u8; 32]; let seed_xa = [1u8; 32];
@@ -150,5 +152,5 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
ksk.decompress(module, &ksk_compressed); ksk.decompress(module, &ksk_compressed);
ksk.key ksk.key
.assert_noise(module, &sk_out_exec, &sk_in.data, sigma); .assert_noise(module, &sk_out_prepared, &sk_in.data, sigma);
} }

View File

@@ -1,6 +1,6 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubABInplace, VmpPMatAlloc, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubABInplace, VmpPMatAlloc,
VmpPMatPrepare, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScalarZnx, ScratchOwned}, layouts::{Backend, Module, ScalarZnx, ScratchOwned},
@@ -12,20 +12,23 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
layouts::{GGSWCiphertext, GLWESecret, compressed::GGSWCiphertextCompressed, prepared::GLWESecretExec}, layouts::{
GGSWCiphertext, GLWESecret,
compressed::GGSWCiphertextCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GGSWAssertNoiseFamily}, trait_families::{Decompress, GGSWAssertNoiseFamily},
}; };
use crate::trait_families::{GGSWEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGSWEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_ggsw_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, digits: usize, rank: usize, sigma: f64) pub fn test_ggsw_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, digits: usize, rank: usize, sigma: f64)
where where
Module<B>: GLWESecretExecModuleFamily<B> Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B> + GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B> + GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
@@ -60,13 +63,12 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
sk_exec.prepare(module, &sk);
ct.encrypt_sk( ct.encrypt_sk(
module, module,
&pt_scalar, &pt_scalar,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -75,7 +77,7 @@ where
let noise_f = |_col_i: usize| -(k as f64) + sigma.log2() + 0.5; let noise_f = |_col_i: usize| -(k as f64) + sigma.log2() + 0.5;
ct.assert_noise(module, &sk_exec, &pt_scalar, &noise_f); ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f);
} }
pub fn test_ggsw_compressed_encrypt_sk<B: Backend>( pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
@@ -86,12 +88,11 @@ pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
rank: usize, rank: usize,
sigma: f64, sigma: f64,
) where ) where
Module<B>: GLWESecretExecModuleFamily<B> Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B> + GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B> + GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
@@ -125,15 +126,14 @@ pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
sk_exec.prepare(module, &sk);
let seed_xa: [u8; 32] = [1u8; 32]; let seed_xa: [u8; 32] = [1u8; 32];
ct_compressed.encrypt_sk( ct_compressed.encrypt_sk(
module, module,
&pt_scalar, &pt_scalar,
&sk_exec, &sk_prepared,
seed_xa, seed_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -145,5 +145,5 @@ pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
let mut ct: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k, rows, digits, rank); let mut ct: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k, rows, digits, rank);
ct.decompress(module, &ct_compressed); ct.decompress(module, &ct_compressed);
ct.assert_noise(module, &sk_exec, &pt_scalar, &noise_f); ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f);
} }

View File

@@ -1,5 +1,5 @@
use backend::hal::{ use backend::hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy, VecZnxDftAlloc, VecZnxFillUniform, VecZnxStd, VecZnxSubABInplace}, api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy, VecZnxDftAlloc, VecZnxFillUniform, VecZnxSubABInplace},
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
@@ -12,16 +12,16 @@ use crate::{
layouts::{ layouts::{
GLWECiphertext, GLWEPlaintext, GLWEPublicKey, GLWESecret, Infos, GLWECiphertext, GLWEPlaintext, GLWEPublicKey, GLWESecret, Infos,
compressed::GLWECiphertextCompressed, compressed::GLWECiphertextCompressed,
prepared::{GLWEPublicKeyExec, GLWESecretExec}, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
operations::GLWEOperations, operations::GLWEOperations,
trait_families::Decompress, trait_families::Decompress,
}; };
use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptPkFamily, GLWEEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptPkFamily, GLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub trait EncryptionTestModuleFamily<B: Backend> = pub trait EncryptionTestModuleFamily<B: Backend> =
GLWEDecryptFamily<B> + VecZnxStd + GLWESecretExecModuleFamily<B> + GLWEEncryptPkFamily<B>; GLWEDecryptFamily<B> + GLWESecretPreparedModuleFamily<B> + GLWEEncryptPkFamily<B>;
pub fn test_glwe_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) pub fn test_glwe_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize)
where where
@@ -51,25 +51,25 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa); module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa);
ct.encrypt_sk( ct.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
pt_want.sub_inplace_ab(module, &pt_have); pt_want.sub_inplace_ab(module, &pt_have);
let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0) * (ct.k() as f64).exp2(); let noise_have: f64 = pt_want.data.std(basek, 0) * (ct.k() as f64).exp2();
let noise_want: f64 = sigma; let noise_want: f64 = sigma;
assert!(noise_have <= noise_want + 0.2); assert!(noise_have <= noise_want + 0.2);
@@ -110,7 +110,7 @@ pub fn test_glwe_compressed_encrypt_sk<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa); module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa);
@@ -119,7 +119,7 @@ pub fn test_glwe_compressed_encrypt_sk<B: Backend>(
ct_compressed.encrypt_sk( ct_compressed.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_exec, &sk_prepared,
seed_xa, seed_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -129,11 +129,11 @@ pub fn test_glwe_compressed_encrypt_sk<B: Backend>(
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank); let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
ct.decompress(module, &ct_compressed); ct.decompress(module, &ct_compressed);
ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
pt_want.sub_inplace_ab(module, &pt_have); pt_want.sub_inplace_ab(module, &pt_have);
let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0) * (ct.k() as f64).exp2(); let noise_have: f64 = pt_want.data.std(basek, 0) * (ct.k() as f64).exp2();
let noise_want: f64 = sigma; let noise_want: f64 = sigma;
assert!( assert!(
@@ -163,28 +163,28 @@ where
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: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct) GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct)
| GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct), | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct),
); );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
ct.encrypt_zero_sk( ct.encrypt_zero_sk(
module, module,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
ct.decrypt(module, &mut pt, &sk_exec, scratch.borrow()); ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
assert!((sigma - module.vec_znx_std(basek, &pt.data, 0) * (k_ct as f64).exp2()) <= 0.2); assert!((sigma - pt.data.std(basek, 0) * (k_ct as f64).exp2()) <= 0.2);
} }
pub fn test_glwe_encrypt_pk<B: Backend>(module: &Module<B>, basek: usize, k_ct: usize, k_pk: usize, sigma: f64, rank: usize) pub fn test_glwe_encrypt_pk<B: Backend>(module: &Module<B>, basek: usize, k_ct: usize, k_pk: usize, sigma: f64, rank: usize)
@@ -210,38 +210,38 @@ where
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: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc(n, basek, k_pk, rank);
pk.generate_from_sk(module, &sk_exec, &mut source_xa, &mut source_xe, sigma);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k()) GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k())
| GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k()) | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k())
| GLWECiphertext::encrypt_pk_scratch_space(module, n, basek, pk.k()), | GLWECiphertext::encrypt_pk_scratch_space(module, n, basek, k_pk),
); );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc(n, basek, k_pk, rank);
pk.generate_from_sk(module, &sk_prepared, &mut source_xa, &mut source_xe, sigma);
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_ct, &mut source_xa); module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_ct, &mut source_xa);
let pk_exec: GLWEPublicKeyExec<Vec<u8>, B> = GLWEPublicKeyExec::from(module, &pk, scratch.borrow()); let pk_prepared: GLWEPublicKeyPrepared<Vec<u8>, B> = pk.prepare_alloc(module, scratch.borrow());
ct.encrypt_pk( ct.encrypt_pk(
module, module,
&pt_want, &pt_want,
&pk_exec, &pk_prepared,
&mut source_xu, &mut source_xu,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
pt_want.sub_inplace_ab(module, &pt_have); pt_want.sub_inplace_ab(module, &pt_have);
let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0).log2(); let noise_have: f64 = pt_want.data.std(basek, 0).log2();
let noise_want: f64 = ((((rank as f64) + 1.0) * n as f64 * 0.5 * sigma * sigma).sqrt()).log2() - (k_ct as f64); let noise_want: f64 = ((((rank as f64) + 1.0) * n as f64 * 0.5 * sigma * sigma).sqrt()).log2() - (k_ct as f64);
assert!( assert!(

View File

@@ -1,6 +1,6 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxCopy, VecZnxDftAlloc, VecZnxStd, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxCopy, VecZnxDftAlloc,
VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned, VecZnxDft}, layouts::{Backend, Module, ScratchOwned, VecZnxDft},
@@ -12,20 +12,23 @@ use backend::hal::{
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
layouts::{GGLWETensorKey, GLWEPlaintext, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed, prepared::GLWESecretExec}, layouts::{
GGLWETensorKey, GLWEPlaintext, GLWESecret, Infos,
compressed::GGLWETensorKeyCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GLWEDecryptFamily}, trait_families::{Decompress, GLWEDecryptFamily},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_tensor_key_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, sigma: f64, rank: usize) pub fn test_glwe_tensor_key_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, sigma: f64, rank: usize)
where where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -64,8 +67,7 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
sk_exec.prepare(module, &sk);
tensor_key.encrypt_sk( tensor_key.encrypt_sk(
module, module,
@@ -89,7 +91,7 @@ where
(0..rank).for_each(|i| { (0..rank).for_each(|i| {
(0..rank).for_each(|j| { (0..rank).for_each(|j| {
module.svp_apply(&mut sk_ij_dft, 0, &sk_exec.data, j, &sk_dft, i); module.svp_apply(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
module.vec_znx_big_normalize( module.vec_znx_big_normalize(
basek, basek,
@@ -104,11 +106,11 @@ where
tensor_key tensor_key
.at(i, j) .at(i, j)
.at(row_i, col_i) .at(row_i, col_i)
.decrypt(module, &mut pt, &sk_exec, scratch.borrow()); .decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
let std_pt: f64 = module.vec_znx_std(basek, &pt.data, 0) * (k as f64).exp2(); let std_pt: f64 = pt.data.std(basek, 0) * (k as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.5, "{} {}", sigma, std_pt); assert!((sigma - std_pt).abs() <= 0.5, "{} {}", sigma, std_pt);
}); });
}); });
@@ -119,11 +121,10 @@ where
pub fn test_glwe_tensor_key_compressed_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, sigma: f64, rank: usize) pub fn test_glwe_tensor_key_compressed_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, sigma: f64, rank: usize)
where where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -147,7 +148,8 @@ where
let n: usize = module.n(); let n: usize = module.n();
let rows: usize = k / basek; let rows: usize = k / basek;
let mut tensor_key_compressed: GGLWETensorKeyCompressed<Vec<u8>> = GGLWETensorKeyCompressed::alloc(n, basek, k, rows, 1, rank); let mut tensor_key_compressed: GGLWETensorKeyCompressed<Vec<u8>> =
GGLWETensorKeyCompressed::alloc(n, basek, k, rows, 1, 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]);
@@ -162,8 +164,7 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
sk_exec.prepare(module, &sk);
let seed_xa: [u8; 32] = [1u8; 32]; let seed_xa: [u8; 32] = [1u8; 32];
@@ -192,7 +193,7 @@ where
(0..rank).for_each(|i| { (0..rank).for_each(|i| {
(0..rank).for_each(|j| { (0..rank).for_each(|j| {
module.svp_apply(&mut sk_ij_dft, 0, &sk_exec.data, j, &sk_dft, i); module.svp_apply(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
module.vec_znx_big_normalize( module.vec_znx_big_normalize(
basek, basek,
@@ -207,11 +208,11 @@ where
tensor_key tensor_key
.at(i, j) .at(i, j)
.at(row_i, col_i) .at(row_i, col_i)
.decrypt(module, &mut pt, &sk_exec, scratch.borrow()); .decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
let std_pt: f64 = module.vec_znx_std(basek, &pt.data, 0) * (k as f64).exp2(); let std_pt: f64 = pt.data.std(basek, 0) * (k as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.5, "{} {}", sigma, std_pt); assert!((sigma - std_pt).abs() <= 0.5, "{} {}", sigma, std_pt);
}); });
}); });

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxStd, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxSubScalarInplace,
VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut,
}, },
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned}, layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned},
oep::{ oep::{
@@ -13,14 +13,14 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GGSWCiphertext, GLWESecret, GGLWESwitchingKey, GGLWESwitchingKey, GGSWCiphertext, GLWESecret,
prepared::{GGSWCiphertextExec, GLWESecretExec}, prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_product, noise::noise_ggsw_product,
trait_families::{GLWEDecryptFamily, GLWEExternalProductFamily}, trait_families::{GLWEDecryptFamily, GLWEExternalProductFamily},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_switching_key_external_product<B: Backend>( pub fn test_gglwe_switching_key_external_product<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -34,11 +34,10 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -63,7 +62,8 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
let rows: usize = k_in.div_ceil(basek * digits); let rows: usize = k_in.div_ceil(basek * digits);
let digits_in: usize = 1; let digits_in: usize = 1;
let mut ct_gglwe_in: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(n, basek, k_in, rows, digits_in, rank_in, rank_out); let mut ct_gglwe_in: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_in, rows, digits_in, rank_in, rank_out);
let mut ct_gglwe_out: GGLWESwitchingKey<Vec<u8>> = let mut ct_gglwe_out: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_out, rows, digits_in, rank_in, rank_out); GGLWESwitchingKey::alloc(n, basek, k_out, rows, digits_in, rank_in, rank_out);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k_ggsw, rows, digits, rank_out); let mut ct_rgsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k_ggsw, rows, digits, rank_out);
@@ -91,7 +91,7 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(var_xs, &mut source_xs); sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
// gglwe_{s1}(s0) = s0 -> s1 // gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_in.encrypt_sk( ct_gglwe_in.encrypt_sk(
@@ -107,20 +107,17 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
ct_rgsw.encrypt_sk( ct_rgsw.encrypt_sk(
module, module,
&pt_rgsw, &pt_rgsw,
&sk_out_exec, &sk_out_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut ct_rgsw_exec: GGSWCiphertextExec<Vec<u8>, B> = let ct_rgsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank_out);
ct_rgsw_exec.prepare(module, &ct_rgsw, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_exec, scratch.borrow()); ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow());
(0..rank_in).for_each(|i| { (0..rank_in).for_each(|i| {
module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data.as_vec_znx_mut(), i); // * X^{r} module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data.as_vec_znx_mut(), i); // * X^{r}
@@ -149,7 +146,7 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
ct_gglwe_out ct_gglwe_out
.key .key
.assert_noise(module, &sk_out_exec, &sk_in.data, max_noise + 0.5); .assert_noise(module, &sk_out_prepared, &sk_in.data, max_noise + 0.5);
} }
pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>( pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
@@ -163,11 +160,10 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -219,7 +215,7 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(var_xs, &mut source_xs); sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
// gglwe_{s1}(s0) = s0 -> s1 // gglwe_{s1}(s0) = s0 -> s1
ct_gglwe.encrypt_sk( ct_gglwe.encrypt_sk(
@@ -235,20 +231,17 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
ct_rgsw.encrypt_sk( ct_rgsw.encrypt_sk(
module, module,
&pt_rgsw, &pt_rgsw,
&sk_out_exec, &sk_out_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut ct_rgsw_exec: GGSWCiphertextExec<Vec<u8>, B> = let ct_rgsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank_out);
ct_rgsw_exec.prepare(module, &ct_rgsw, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe.external_product_inplace(module, &ct_rgsw_exec, scratch.borrow()); ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow());
(0..rank_in).for_each(|i| { (0..rank_in).for_each(|i| {
module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data.as_vec_znx_mut(), i); // * X^{r} module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data.as_vec_znx_mut(), i); // * X^{r}
@@ -277,5 +270,5 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
ct_gglwe ct_gglwe
.key .key
.assert_noise(module, &sk_out_exec, &sk_in.data, max_noise + 0.5); .assert_noise(module, &sk_out_prepared, &sk_in.data, max_noise + 0.5);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxStd, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxSubABInplace,
VecZnxSubABInplace, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut,
}, },
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned}, layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned},
oep::{ oep::{
@@ -14,7 +14,7 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GGSWCiphertext, GLWESecret, GGSWCiphertext, GLWESecret,
prepared::{GGSWCiphertextExec, GLWESecretExec}, prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_product, noise::noise_ggsw_product,
trait_families::GGSWAssertNoiseFamily, trait_families::GGSWAssertNoiseFamily,
@@ -22,7 +22,7 @@ use crate::{
use crate::trait_families::{ use crate::trait_families::{
GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily, GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily,
GLWESecretExecModuleFamily, GLWESecretPreparedModuleFamily,
}; };
pub fn test_ggsw_external_product<B: Backend>( pub fn test_ggsw_external_product<B: Backend>(
@@ -35,12 +35,11 @@ pub fn test_ggsw_external_product<B: Backend>(
rank: usize, rank: usize,
sigma: f64, sigma: f64,
) where ) where
Module<B>: GLWESecretExecModuleFamily<B> Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B> + GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B> + GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -89,12 +88,12 @@ pub fn test_ggsw_external_product<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw_rhs.encrypt_sk( ct_ggsw_rhs.encrypt_sk(
module, module,
&pt_ggsw_rhs, &pt_ggsw_rhs,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -104,17 +103,16 @@ pub fn test_ggsw_external_product<B: Backend>(
ct_ggsw_lhs_in.encrypt_sk( ct_ggsw_lhs_in.encrypt_sk(
module, module,
&pt_ggsw_lhs, &pt_ggsw_lhs,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut ct_rhs_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank); let ct_rhs_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw_rhs.prepare_alloc(module, scratch.borrow());
ct_rhs_exec.prepare(module, &ct_ggsw_rhs, scratch.borrow());
ct_ggsw_lhs_out.external_product(module, &ct_ggsw_lhs_in, &ct_rhs_exec, scratch.borrow()); ct_ggsw_lhs_out.external_product(module, &ct_ggsw_lhs_in, &ct_rhs_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_ggsw_lhs.as_vec_znx_mut(), 0); module.vec_znx_rotate_inplace(k as i64, &mut pt_ggsw_lhs.as_vec_znx_mut(), 0);
@@ -141,7 +139,7 @@ pub fn test_ggsw_external_product<B: Backend>(
) + 0.5 ) + 0.5
}; };
ct_ggsw_lhs_out.assert_noise(module, &sk_exec, &pt_ggsw_lhs, &max_noise); ct_ggsw_lhs_out.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, &max_noise);
} }
pub fn test_ggsw_external_product_inplace<B: Backend>( pub fn test_ggsw_external_product_inplace<B: Backend>(
@@ -153,12 +151,11 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
rank: usize, rank: usize,
sigma: f64, sigma: f64,
) where ) where
Module<B>: GLWESecretExecModuleFamily<B> Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B> + GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B> + GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -207,12 +204,12 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw_rhs.encrypt_sk( ct_ggsw_rhs.encrypt_sk(
module, module,
&pt_ggsw_rhs, &pt_ggsw_rhs,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -222,17 +219,16 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
ct_ggsw_lhs.encrypt_sk( ct_ggsw_lhs.encrypt_sk(
module, module,
&pt_ggsw_lhs, &pt_ggsw_lhs,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let mut ct_rhs_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank); let ct_rhs_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw_rhs.prepare_alloc(module, scratch.borrow());
ct_rhs_exec.prepare(module, &ct_ggsw_rhs, scratch.borrow());
ct_ggsw_lhs.external_product_inplace(module, &ct_rhs_exec, scratch.borrow()); ct_ggsw_lhs.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_ggsw_lhs.as_vec_znx_mut(), 0); module.vec_znx_rotate_inplace(k as i64, &mut pt_ggsw_lhs.as_vec_znx_mut(), 0);
@@ -259,5 +255,5 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
) + 0.5 ) + 0.5
}; };
ct_ggsw_lhs.assert_noise(module, &sk_exec, &pt_ggsw_lhs, &max_noise); ct_ggsw_lhs.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, &max_noise);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxRotateInplace, VecZnxStd, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxRotateInplace, VmpPMatAlloc,
VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, VmpPMatPrepare, ZnxViewMut,
}, },
layouts::{Backend, Module, ScalarZnx, ScratchOwned}, layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{ oep::{
@@ -14,12 +14,12 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGSWCiphertextExec, GLWESecretExec}, prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_product, noise::noise_ggsw_product,
}; };
use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptSkFamily, GLWEExternalProductFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptSkFamily, GLWEExternalProductFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_external_product<B: Backend>( pub fn test_glwe_external_product<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -32,12 +32,11 @@ pub fn test_glwe_external_product<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GLWEEncryptSkFamily<B> Module<B>: GLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ GLWEExternalProductFamily<B> + GLWEExternalProductFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxRotateInplace + VecZnxRotateInplace
+ VecZnxStd
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B> B: TakeVecZnxDftImpl<B>
@@ -88,12 +87,12 @@ pub fn test_glwe_external_product<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw.encrypt_sk( ct_ggsw.encrypt_sk(
module, module,
&pt_rgsw, &pt_rgsw,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -103,16 +102,16 @@ pub fn test_glwe_external_product<B: Backend>(
ct_glwe_in.encrypt_sk( ct_glwe_in.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let ct_ggsw_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::from(module, &ct_ggsw, scratch.borrow()); let ct_ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw.prepare_alloc(module, scratch.borrow());
ct_glwe_out.external_product(module, &ct_glwe_in, &ct_ggsw_exec, scratch.borrow()); ct_glwe_out.external_product(module, &ct_glwe_in, &ct_ggsw_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0); module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
@@ -137,7 +136,7 @@ pub fn test_glwe_external_product<B: Backend>(
k_ggsw, k_ggsw,
); );
ct_glwe_out.assert_noise(module, &sk_exec, &pt_want, max_noise + 0.5); ct_glwe_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 0.5);
} }
pub fn test_glwe_external_product_inplace<B: Backend>( pub fn test_glwe_external_product_inplace<B: Backend>(
@@ -150,12 +149,11 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GLWEEncryptSkFamily<B> Module<B>: GLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ GLWEExternalProductFamily<B> + GLWEExternalProductFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxRotateInplace + VecZnxRotateInplace
+ VecZnxStd
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B>, + VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B> B: TakeVecZnxDftImpl<B>
@@ -196,12 +194,12 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw.encrypt_sk( ct_ggsw.encrypt_sk(
module, module,
&pt_rgsw, &pt_rgsw,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
@@ -211,16 +209,16 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
ct_glwe.encrypt_sk( ct_glwe.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_exec, &sk_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let ct_ggsw_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::from(module, &ct_ggsw, scratch.borrow()); let ct_ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw.prepare_alloc(module, scratch.borrow());
ct_glwe.external_product_inplace(module, &ct_ggsw_exec, scratch.borrow()); ct_glwe.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0); module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
@@ -245,5 +243,5 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
k_ggsw, k_ggsw,
); );
ct_glwe.assert_noise(module, &sk_exec, &pt_want, max_noise + 0.5); ct_glwe.assert_noise(module, &sk_prepared, &pt_want, max_noise + 0.5);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubScalarInplace, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubScalarInplace, VecZnxSwithcDegree,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -13,14 +13,14 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GLWESecret, GGLWESwitchingKey, GGLWESwitchingKey, GLWESecret,
prepared::{GLWESecretExec, GGLWESwitchingKeyExec}, prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
}; };
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_switching_key_keyswitch<B: Backend>( pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -35,11 +35,10 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -109,7 +108,7 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out_s1s2); let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out_s1s2);
sk2.fill_ternary_prob(0.5, &mut source_xs); sk2.fill_ternary_prob(0.5, &mut source_xs);
let sk2_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk2); let sk2_prepared: GLWESecretPrepared<Vec<u8>, B> = sk2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) = s0 -> s1 // gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.encrypt_sk( ct_gglwe_s0s1.encrypt_sk(
@@ -133,14 +132,14 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
scratch_enc.borrow(), scratch_enc.borrow(),
); );
let ct_gglwe_s1s2_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = let ct_gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> =
GGLWESwitchingKeyExec::from(module, &ct_gglwe_s1s2, scratch_apply.borrow()); ct_gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
ct_gglwe_s0s2.keyswitch( ct_gglwe_s0s2.keyswitch(
module, module,
&ct_gglwe_s0s1, &ct_gglwe_s0s1,
&ct_gglwe_s1s2_exec, &ct_gglwe_s1s2_prepared,
scratch_apply.borrow(), scratch_apply.borrow(),
); );
@@ -159,7 +158,7 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
ct_gglwe_s0s2 ct_gglwe_s0s2
.key .key
.assert_noise(module, &sk2_exec, &sk0.data, max_noise + 0.5); .assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5);
} }
pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>( pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
@@ -173,11 +172,10 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWEEncryptSkFamily<B> Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace + VecZnxSubScalarInplace
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -203,7 +201,8 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
let mut ct_gglwe_s0s1: GGLWESwitchingKey<Vec<u8>> = let mut ct_gglwe_s0s1: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_ct, rows, digits_in, rank_in, rank_out); GGLWESwitchingKey::alloc(n, basek, k_ct, rows, digits_in, rank_in, rank_out);
let mut ct_gglwe_s1s2: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank_out, rank_out); let mut ct_gglwe_s1s2: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank_out, 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]);
@@ -231,7 +230,7 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out); let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk2.fill_ternary_prob(var_xs, &mut source_xs); sk2.fill_ternary_prob(var_xs, &mut source_xs);
let sk2_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk2); let sk2_prepared: GLWESecretPrepared<Vec<u8>, B> = sk2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) = s0 -> s1 // gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.encrypt_sk( ct_gglwe_s0s1.encrypt_sk(
@@ -255,11 +254,11 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
scratch_enc.borrow(), scratch_enc.borrow(),
); );
let ct_gglwe_s1s2_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = let ct_gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> =
GGLWESwitchingKeyExec::from(module, &ct_gglwe_s1s2, scratch_apply.borrow()); ct_gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
ct_gglwe_s0s1.keyswitch_inplace(module, &ct_gglwe_s1s2_exec, scratch_apply.borrow()); ct_gglwe_s0s1.keyswitch_inplace(module, &ct_gglwe_s1s2_prepared, scratch_apply.borrow());
let ct_gglwe_s0s2: GGLWESwitchingKey<Vec<u8>> = ct_gglwe_s0s1; let ct_gglwe_s0s2: GGLWESwitchingKey<Vec<u8>> = ct_gglwe_s0s1;
@@ -278,5 +277,5 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
ct_gglwe_s0s2 ct_gglwe_s0s2
.key .key
.assert_noise(module, &sk2_exec, &sk0.data, max_noise + 0.5); .assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubABInplace, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubABInplace, VecZnxSwithcDegree,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScalarZnx, ScratchOwned}, layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{ oep::{
@@ -13,8 +13,8 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GGLWETensorKey, GGSWCiphertext, GLWESecret, GGLWESwitchingKey, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWESecret,
prepared::{GLWESecretExec, GGLWESwitchingKeyExec, GGLWETensorKeyExec}, prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_keyswitch, noise::noise_ggsw_keyswitch,
trait_families::GGSWAssertNoiseFamily, trait_families::GGSWAssertNoiseFamily,
@@ -22,7 +22,7 @@ use crate::{
use crate::trait_families::{ use crate::trait_families::{
GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily, GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily,
GLWESecretExecModuleFamily, GLWESecretPreparedModuleFamily,
}; };
pub fn test_ggsw_keyswitch<B: Backend>( pub fn test_ggsw_keyswitch<B: Backend>(
@@ -36,12 +36,11 @@ pub fn test_ggsw_keyswitch<B: Backend>(
rank: usize, rank: usize,
sigma: f64, sigma: f64,
) where ) where
Module<B>: GLWESecretExecModuleFamily<B> Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B> + GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B> + GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -93,11 +92,11 @@ pub fn test_ggsw_keyswitch<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs); sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let sk_in_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in); let sk_in_dft: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs); sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk( ksk.encrypt_sk(
module, module,
@@ -129,14 +128,16 @@ pub fn test_ggsw_keyswitch<B: Backend>(
scratch.borrow(), scratch.borrow(),
); );
let mut ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
GGLWESwitchingKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank, rank); let tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow());
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
ksk_exec.prepare(module, &ksk, scratch.borrow()); ct_out.keyswitch(
tsk_exec.prepare(module, &tsk, scratch.borrow()); module,
&ct_in,
ct_out.keyswitch(module, &ct_in, &ksk_exec, &tsk_exec, scratch.borrow()); &ksk_prepared,
&tsk_prepared,
scratch.borrow(),
);
let max_noise = |col_j: usize| -> f64 { let max_noise = |col_j: usize| -> f64 {
noise_ggsw_keyswitch( noise_ggsw_keyswitch(
@@ -154,7 +155,7 @@ pub fn test_ggsw_keyswitch<B: Backend>(
) + 0.5 ) + 0.5
}; };
ct_out.assert_noise(module, &sk_out_exec, &pt_scalar, &max_noise); ct_out.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise);
} }
pub fn test_ggsw_keyswitch_inplace<B: Backend>( pub fn test_ggsw_keyswitch_inplace<B: Backend>(
@@ -167,12 +168,11 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
rank: usize, rank: usize,
sigma: f64, sigma: f64,
) where ) where
Module<B>: GLWESecretExecModuleFamily<B> Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B> + GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B> + GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubABInplace + VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy + VecZnxCopy
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
@@ -218,11 +218,11 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs); sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let sk_in_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in); let sk_in_dft: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs); sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk( ksk.encrypt_sk(
module, module,
@@ -254,14 +254,10 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
scratch.borrow(), scratch.borrow(),
); );
let mut ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
GGLWESwitchingKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank, rank); let tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow());
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
ksk_exec.prepare(module, &ksk, scratch.borrow()); ct.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow());
tsk_exec.prepare(module, &tsk, scratch.borrow());
ct.keyswitch_inplace(module, &ksk_exec, &tsk_exec, scratch.borrow());
let max_noise = |col_j: usize| -> f64 { let max_noise = |col_j: usize| -> f64 {
noise_ggsw_keyswitch( noise_ggsw_keyswitch(
@@ -279,5 +275,5 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
) + 0.5 ) + 0.5
}; };
ct.assert_noise(module, &sk_out_exec, &pt_scalar, &max_noise); ct.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxStd, VecZnxSwithcDegree, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxSwithcDegree, VmpPMatAlloc,
VmpPMatAlloc, VmpPMatPrepare, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -13,14 +13,14 @@ use sampling::source::Source;
use crate::{ use crate::{
layouts::{ layouts::{
GLWECiphertext, GLWEPlaintext, GLWESecret, GGLWESwitchingKey, Infos, GGLWESwitchingKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
prepared::{GLWESecretExec, GGLWESwitchingKeyExec}, prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
}; };
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_keyswitch<B: Backend>( pub fn test_glwe_keyswitch<B: Backend>(
module: &Module<B>, module: &Module<B>,
@@ -34,10 +34,9 @@ pub fn test_glwe_keyswitch<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> Module<B>: GGLWESwitchingKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxStd
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -83,11 +82,11 @@ pub fn test_glwe_keyswitch<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in); let sk_in_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk( ksk.encrypt_sk(
module, module,
@@ -102,16 +101,16 @@ pub fn test_glwe_keyswitch<B: Backend>(
ct_in.encrypt_sk( ct_in.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_in_exec, &sk_in_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = GGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
ct_out.keyswitch(module, &ct_in, &ksk_exec, scratch.borrow()); ct_out.keyswitch(module, &ct_in, &ksk_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product( let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64, module.n() as f64,
@@ -126,7 +125,7 @@ pub fn test_glwe_keyswitch<B: Backend>(
k_ksk, k_ksk,
); );
ct_out.assert_noise(module, &sk_out_exec, &pt_want, max_noise + 0.5); ct_out.assert_noise(module, &sk_out_prepared, &pt_want, max_noise + 0.5);
} }
pub fn test_glwe_keyswitch_inplace<B: Backend>( pub fn test_glwe_keyswitch_inplace<B: Backend>(
@@ -139,10 +138,9 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
sigma: f64, sigma: f64,
) where ) where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> Module<B>: GGLWESwitchingKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxStd
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
@@ -177,11 +175,11 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in); let sk_in_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs); sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk( ksk.encrypt_sk(
module, module,
@@ -196,16 +194,16 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
ct_glwe.encrypt_sk( ct_glwe.encrypt_sk(
module, module,
&pt_want, &pt_want,
&sk_in_exec, &sk_in_prepared,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = GGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
ct_glwe.keyswitch_inplace(module, &ksk_exec, scratch.borrow()); ct_glwe.keyswitch_inplace(module, &ksk_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product( let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64, module.n() as f64,
@@ -220,5 +218,5 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
k_ksk, k_ksk,
); );
ct_glwe.assert_noise(module, &sk_out_exec, &pt_want, max_noise + 0.5); ct_glwe.assert_noise(module, &sk_out_prepared, &pt_want, max_noise + 0.5);
} }

View File

@@ -1,7 +1,7 @@
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxEncodeCoeffsi64, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxSwithcDegree,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, VmpPMatAlloc, VmpPMatPrepare, ZnxView,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -11,9 +11,12 @@ use backend::hal::{
}; };
use sampling::source::Source; use sampling::source::Source;
use crate::layouts::{Infos, LWECiphertext, LWEPlaintext, LWESecret, LWESwitchingKey, prepared::LWESwitchingKeyExec}; use crate::layouts::{
Infos, LWECiphertext, LWEPlaintext, LWESecret, LWESwitchingKey,
prepared::{LWESwitchingKeyPrepared, PrepareAlloc},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretPreparedModuleFamily};
pub fn test_lwe_keyswitch<B: Backend>(module: &Module<B>) pub fn test_lwe_keyswitch<B: Backend>(module: &Module<B>)
where where
@@ -22,11 +25,10 @@ where
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ VecZnxEncodeCoeffsi64
+ VecZnxAutomorphismInplace + VecZnxAutomorphismInplace
+ VmpPMatAlloc<B> + VmpPMatAlloc<B>
+ VmpPMatPrepare<B> + VmpPMatPrepare<B>
+ GLWESecretExecModuleFamily<B>, + GLWESecretPreparedModuleFamily<B>,
B: TakeScalarZnxImpl<B> B: TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B> + TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B> + ScratchAvailableImpl<B>
@@ -65,7 +67,8 @@ where
let data: i64 = 17; let data: i64 = 17;
let mut lwe_pt_in: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt); let mut lwe_pt_in: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
module.encode_coeff_i64(basek, &mut lwe_pt_in.data, 0, k_lwe_pt, 0, data, k_lwe_pt);
lwe_pt_in.encode_i64(data, k_lwe_pt);
let mut lwe_ct_in: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_in, basek, k_lwe_ct); let mut lwe_ct_in: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_in, basek, k_lwe_ct);
lwe_ct_in.encrypt_sk( lwe_ct_in.encrypt_sk(
@@ -91,9 +94,9 @@ where
let mut lwe_ct_out: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_out, basek, k_lwe_ct); let mut lwe_ct_out: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_out, basek, k_lwe_ct);
let ksk_exec: LWESwitchingKeyExec<Vec<u8>, B> = LWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); let ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_exec, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow());
let mut lwe_pt_out: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct); let mut lwe_pt_out: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct);
lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out); lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out);

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use backend::hal::{ use backend::hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigSubSmallBInplace, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigSubSmallBInplace,
VecZnxEncodeVeci64, VecZnxRotateInplace, VecZnxStd, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, VecZnxRotateInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
}, },
layouts::{Backend, Module, ScratchOwned}, layouts::{Backend, Module, ScratchOwned},
oep::{ oep::{
@@ -17,22 +17,20 @@ use crate::{
GLWEOperations, GLWEPacker, GLWEOperations, GLWEPacker,
layouts::{ layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec}, prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily, GLWEPackingFamily}, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily, GLWEPackingFamily},
}; };
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub trait PackingTestModuleFamily<B: Backend> = GLWEPackingFamily<B> pub trait PackingTestModuleFamily<B: Backend> = GLWEPackingFamily<B>
+ GLWESecretExecModuleFamily<B> + GLWESecretPreparedModuleFamily<B>
+ GGLWESwitchingKeyEncryptSkFamily<B> + GGLWESwitchingKeyEncryptSkFamily<B>
+ GLWEKeyswitchFamily<B> + GLWEKeyswitchFamily<B>
+ GLWEDecryptFamily<B> + GLWEDecryptFamily<B>
+ VecZnxStd
+ VecZnxSwithcDegree + VecZnxSwithcDegree
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxEncodeVeci64
+ VecZnxRotateInplace + VecZnxRotateInplace
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxBigSubSmallBInplace<B> + VecZnxBigSubSmallBInplace<B>
@@ -76,7 +74,7 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk); let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct);
let mut data: Vec<i64> = vec![0i64; n]; let mut data: Vec<i64> = vec![0i64; n];
@@ -84,11 +82,11 @@ where
*x = i as i64; *x = i as i64;
}); });
module.encode_vec_i64(basek, &mut pt.data, 0, pt_k, &data, 32); pt.encode_vec_i64(&data, pt_k);
let gal_els: Vec<i64> = GLWEPacker::galois_elements(module); let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyExec<Vec<u8>, B>> = HashMap::new(); let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank); let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
gal_els.iter().for_each(|gal_el| { gal_els.iter().for_each(|gal_el| {
tmp.encrypt_sk( tmp.encrypt_sk(
@@ -100,8 +98,8 @@ where
sigma, sigma,
scratch.borrow(), scratch.borrow(),
); );
let atk_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = GGLWEAutomorphismKeyExec::from(module, &tmp, scratch.borrow()); let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
auto_keys.insert(*gal_el, atk_exec); auto_keys.insert(*gal_el, atk_prepared);
}); });
let log_batch: usize = 0; let log_batch: usize = 0;
@@ -158,13 +156,13 @@ where
} }
}); });
module.encode_vec_i64(basek, &mut pt_want.data, 0, pt_k, &data, 32); pt_want.encode_vec_i64(&data, pt_k);
res.decrypt(module, &mut pt, &sk_dft, scratch.borrow()); res.decrypt(module, &mut pt, &sk_dft, scratch.borrow());
pt.sub_inplace_ab(module, &pt_want); pt.sub_inplace_ab(module, &pt_want);
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2(); let noise_have: f64 = pt.std().log2();
// println!("noise_have: {}", noise_have); // println!("noise_have: {}", noise_have);
assert!( assert!(
noise_have < -((k_ct - basek) as f64), noise_have < -((k_ct - basek) as f64),

Some files were not shown because too many files have changed in this diff Show More