diff --git a/Cargo.lock b/Cargo.lock index 54554c3..bbb23d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -563,6 +563,17 @@ dependencies = [ "rand_distr", ] +[[package]] +name = "schemes" +version = "0.1.0" +dependencies = [ + "backend", + "byteorder", + "core", + "itertools 0.14.0", + "sampling", +] + [[package]] name = "serde" version = "1.0.219" diff --git a/Cargo.toml b/Cargo.toml index 541acfd..b7117c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["backend", "core", "sampling", "utils"] +members = ["backend", "core", "sampling", "utils", "schemes"] resolver = "3" [workspace.dependencies] @@ -11,4 +11,4 @@ rand_distr = "0.5.1" itertools = "0.14.0" criterion = "0.7.0" byteorder = "1.5.0" -zstd = "0.13.3" \ No newline at end of file +zstd = "0.13.3" diff --git a/README.md b/README.md index e04eb08..402f551 100644 --- a/README.md +++ b/README.md @@ -115,12 +115,12 @@ In addition to providing a general purpose FHE library over a unified plaintext pub(crate) dist: Distribution, } - pub struct GLWESecretExec { + pub struct GLWESecrecPrepared { pub(crate) data: SvpPPol, pub(crate) dist: Distribution, } - impl GLWESecretExec { + impl GLWESecretPrepared { pub fn prepare(&mut self, module: &Module, sk: &GLWESecret) where O: DataRef, diff --git a/backend/examples/rlwe_encrypt.rs b/backend/examples/rlwe_encrypt.rs index 58f2183..94a49b3 100644 --- a/backend/examples/rlwe_encrypt.rs +++ b/backend/examples/rlwe_encrypt.rs @@ -3,8 +3,7 @@ use backend::{ api::{ ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyInplace, SvpPPolAlloc, SvpPrepare, VecZnxAddNormal, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, - VecZnxDecodeVeci64, VecZnxDftAlloc, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigTmpA, VecZnxEncodeVeci64, - VecZnxFillUniform, VecZnxNormalizeInplace, ZnxInfos, + VecZnxDftAlloc, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigTmpA, VecZnxFillUniform, VecZnxNormalizeInplace, ZnxInfos, }, layouts::{Module, ScalarZnx, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft}, }, @@ -73,7 +72,7 @@ fn main() { let mut want: Vec = vec![0; n]; want.iter_mut() .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()); // m - BIG(ct[1] * s) @@ -132,8 +131,7 @@ fn main() { // have = m * 2^{log_scale} + e let mut have: Vec = 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; izip!(want.iter(), have.iter()) .enumerate() diff --git a/backend/src/hal/api/vec_znx.rs b/backend/src/hal/api/vec_znx.rs index 7f6ce6b..421199b 100644 --- a/backend/src/hal/api/vec_znx.rs +++ b/backend/src/hal/api/vec_znx.rs @@ -1,5 +1,4 @@ use rand_distr::Distribution; -use rug::Float; use sampling::source::Source; use crate::hal::layouts::{Backend, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef}; @@ -198,13 +197,6 @@ pub trait VecZnxCopy { A: VecZnxToRef; } -pub trait VecZnxStd { - /// Returns the standard devaition of the i-th polynomial. - fn vec_znx_std(&self, basek: usize, a: &A, a_col: usize) -> f64 - where - A: VecZnxToRef; -} - pub trait VecZnxFillUniform { /// Fills the first `size` size with uniform values in \[-2^{basek-1}, 2^{basek-1}\] fn vec_znx_fill_uniform(&self, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) @@ -269,75 +261,3 @@ pub trait VecZnxAddNormal { ) where 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(&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(&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(&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(&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(&self, basek: usize, res: &R, col_i: usize, data: &mut [Float]) - where - R: VecZnxToRef; -} diff --git a/backend/src/hal/delegates/vec_znx.rs b/backend/src/hal/delegates/vec_znx.rs index 3eac8a6..0c45506 100644 --- a/backend/src/hal/delegates/vec_znx.rs +++ b/backend/src/hal/delegates/vec_znx.rs @@ -3,22 +3,19 @@ use sampling::source::Source; use crate::hal::{ api::{ VecZnxAdd, VecZnxAddDistF64, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, - VecZnxAutomorphismInplace, VecZnxCopy, VecZnxDecodeCoeffsi64, VecZnxDecodeVecFloat, VecZnxDecodeVeci64, - VecZnxEncodeCoeffsi64, VecZnxEncodeVeci64, VecZnxFillDistF64, VecZnxFillNormal, VecZnxFillUniform, VecZnxLshInplace, + VecZnxAutomorphismInplace, VecZnxCopy, VecZnxFillDistF64, VecZnxFillNormal, VecZnxFillUniform, VecZnxLshInplace, VecZnxMerge, VecZnxMulXpMinusOne, VecZnxMulXpMinusOneInplace, VecZnxNegate, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSplit, - VecZnxStd, VecZnxSub, VecZnxSubABInplace, VecZnxSubBAInplace, VecZnxSubScalarInplace, VecZnxSwithcDegree, + VecZnxSub, VecZnxSubABInplace, VecZnxSubBAInplace, VecZnxSubScalarInplace, VecZnxSwithcDegree, }, layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef}, oep::{ VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl, - VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxDecodeCoeffsi64Impl, - VecZnxDecodeVecFloatImpl, VecZnxDecodeVeci64Impl, VecZnxEncodeCoeffsi64Impl, VecZnxEncodeVeci64Impl, - VecZnxFillDistF64Impl, VecZnxFillNormalImpl, VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, - VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl, - VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl, VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, - VecZnxSplitImpl, VecZnxStdImpl, VecZnxSubABInplaceImpl, VecZnxSubBAInplaceImpl, VecZnxSubImpl, - VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl, + VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxFillDistF64Impl, VecZnxFillNormalImpl, + VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl, + VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, + VecZnxRotateImpl, VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxSubABInplaceImpl, + VecZnxSubBAInplaceImpl, VecZnxSubImpl, VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl, }, }; @@ -325,18 +322,6 @@ where } } -impl VecZnxStd for Module -where - B: Backend + VecZnxStdImpl, -{ - fn vec_znx_std(&self, basek: usize, a: &A, a_col: usize) -> f64 - where - A: VecZnxToRef, - { - B::vec_znx_std_impl(self, basek, a, a_col) - } -} - impl VecZnxFillUniform for Module where B: Backend + VecZnxFillUniformImpl, @@ -428,63 +413,3 @@ where B::vec_znx_add_normal_impl(self, basek, res, res_col, k, source, sigma, bound); } } - -impl VecZnxEncodeVeci64 for Module -where - B: Backend + VecZnxEncodeVeci64Impl, -{ - fn encode_vec_i64(&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 VecZnxEncodeCoeffsi64 for Module -where - B: Backend + VecZnxEncodeCoeffsi64Impl, -{ - fn encode_coeff_i64(&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 VecZnxDecodeVeci64 for Module -where - B: Backend + VecZnxDecodeVeci64Impl, -{ - fn decode_vec_i64(&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 VecZnxDecodeCoeffsi64 for Module -where - B: Backend + VecZnxDecodeCoeffsi64Impl, -{ - fn decode_coeff_i64(&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 VecZnxDecodeVecFloat for Module -where - B: Backend + VecZnxDecodeVecFloatImpl, -{ - fn decode_vec_float(&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); - } -} diff --git a/backend/src/hal/layouts/encoding.rs b/backend/src/hal/layouts/encoding.rs new file mode 100644 index 0000000..e12e729 --- /dev/null +++ b/backend/src/hal/layouts/encoding.rs @@ -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 VecZnx { + 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 VecZnx { + 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; + }); + } + }); + } +} diff --git a/backend/src/hal/layouts/mod.rs b/backend/src/hal/layouts/mod.rs index 13f138f..5d402ff 100644 --- a/backend/src/hal/layouts/mod.rs +++ b/backend/src/hal/layouts/mod.rs @@ -1,8 +1,10 @@ +mod encoding; mod mat_znx; mod module; mod scalar_znx; mod scratch; mod serialization; +mod stats; mod svp_ppol; mod vec_znx; mod vec_znx_big; diff --git a/backend/src/hal/layouts/stats.rs b/backend/src/hal/layouts/stats.rs new file mode 100644 index 0000000..b67b20d --- /dev/null +++ b/backend/src/hal/layouts/stats.rs @@ -0,0 +1,32 @@ +use rug::{ + Float, + float::Round, + ops::{AddAssignRound, DivAssignRound, SubAssignRound}, +}; + +use crate::hal::{ + api::ZnxInfos, + layouts::{DataRef, VecZnx}, +}; + +impl VecZnx { + pub fn std(&self, basek: usize, col: usize) -> f64 { + let prec: u32 = (self.size() * basek) as u32; + let mut data: Vec = (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() + } +} diff --git a/backend/src/hal/oep/vec_znx.rs b/backend/src/hal/oep/vec_znx.rs index ae9ce44..4ebf42b 100644 --- a/backend/src/hal/oep/vec_znx.rs +++ b/backend/src/hal/oep/vec_znx.rs @@ -1,5 +1,4 @@ use rand_distr::Distribution; -use rug::Float; use sampling::source::Source; use crate::hal::layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef}; @@ -288,15 +287,6 @@ pub unsafe trait VecZnxCopyImpl { 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 { - fn vec_znx_std_impl(module: &Module, basek: usize, a: &A, a_col: usize) -> f64 - where - A: VecZnxToRef; -} - /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::hal::api::VecZnxFillUniform] for corresponding public API. /// * See [crate::doc::backend_safety] for safety contract. @@ -373,68 +363,3 @@ pub unsafe trait VecZnxAddNormalImpl { ) where 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 { - fn encode_vec_i64_impl( - module: &Module, - 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 { - fn encode_coeff_i64_impl( - module: &Module, - 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 { - fn decode_vec_i64_impl(module: &Module, 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 { - fn decode_coeff_i64_impl(module: &Module, 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 { - fn decode_vec_float_impl(module: &Module, basek: usize, res: &R, res_col: usize, data: &mut [Float]) - where - R: VecZnxToRef; -} diff --git a/backend/src/hal/tests/vec_znx/encoding.rs b/backend/src/hal/tests/vec_znx/encoding.rs new file mode 100644 index 0000000..e878ef0 --- /dev/null +++ b/backend/src/hal/tests/vec_znx/encoding.rs @@ -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> = 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 = 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 = 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> = 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 = 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 = vec![i64::default(); n]; + a.decode_vec_i64(basek, col_i, k, &mut want); + assert_eq!(have, want, "{:?} != {:?}", &have, &want); + }) + } +} diff --git a/backend/src/hal/tests/vec_znx/generics.rs b/backend/src/hal/tests/vec_znx/generics.rs index cedb3a7..ec2e68b 100644 --- a/backend/src/hal/tests/vec_znx/generics.rs +++ b/backend/src/hal/tests/vec_znx/generics.rs @@ -1,14 +1,13 @@ -use itertools::izip; use sampling::source::Source; use crate::hal::{ - api::{VecZnxAddNormal, VecZnxDecodeVeci64, VecZnxEncodeVeci64, VecZnxFillUniform, VecZnxStd, ZnxInfos, ZnxView, ZnxViewMut}, + api::{VecZnxAddNormal, VecZnxFillUniform, ZnxView}, layouts::{Backend, Module, VecZnx}, }; pub fn test_vec_znx_fill_uniform(module: &Module) where - Module: VecZnxFillUniform + VecZnxStd, + Module: VecZnxFillUniform, { let n: usize = module.n(); let basek: usize = 17; @@ -26,7 +25,7 @@ where assert_eq!(a.at(col_j, limb_i), zero); }) } else { - let std: f64 = module.vec_znx_std(basek, &a, col_i); + let std: f64 = a.std(basek, col_i); assert!( (std - one_12_sqrt).abs() < 0.01, "std={} ~!= {}", @@ -40,7 +39,7 @@ where pub fn test_vec_znx_add_normal(module: &Module) where - Module: VecZnxAddNormal + VecZnxStd, + Module: VecZnxAddNormal, { let n: usize = module.n(); let basek: usize = 17; @@ -61,61 +60,9 @@ where assert_eq!(a.at(col_j, limb_i), zero); }) } 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); } }) }); } - -pub fn test_vec_znx_encode_vec_i64_lo_norm(module: &Module) -where - Module: 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> = 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 = 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 = 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(module: &Module) -where - Module: 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> = 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 = 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 = 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)); - }) - } -} diff --git a/backend/src/hal/tests/vec_znx/mod.rs b/backend/src/hal/tests/vec_znx/mod.rs index 7a179f3..a87ee38 100644 --- a/backend/src/hal/tests/vec_znx/mod.rs +++ b/backend/src/hal/tests/vec_znx/mod.rs @@ -1,2 +1,5 @@ mod generics; pub use generics::*; + +#[cfg(test)] +mod encoding; diff --git a/backend/src/implementation/cpu_spqlios/test/vec_znx_fft64.rs b/backend/src/implementation/cpu_spqlios/test/vec_znx_fft64.rs index a289439..35043f1 100644 --- a/backend/src/implementation/cpu_spqlios/test/vec_znx_fft64.rs +++ b/backend/src/implementation/cpu_spqlios/test/vec_znx_fft64.rs @@ -2,10 +2,7 @@ use crate::{ hal::{ api::ModuleNew, layouts::Module, - tests::vec_znx::{ - 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, - }, + tests::vec_znx::{test_vec_znx_add_normal, test_vec_znx_fill_uniform}, }, implementation::cpu_spqlios::FFT64, }; @@ -21,15 +18,3 @@ fn test_vec_znx_add_normal_fft64() { let module: Module = Module::::new(1 << 12); test_vec_znx_add_normal(&module); } - -#[test] -fn test_vec_znx_encode_vec_lo_norm_fft64() { - let module: Module = Module::::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 = Module::::new(1 << 8); - test_vec_znx_encode_vec_i64_hi_norm(&module); -} diff --git a/backend/src/implementation/cpu_spqlios/vec_znx.rs b/backend/src/implementation/cpu_spqlios/vec_znx.rs index e1da258..87d99a8 100644 --- a/backend/src/implementation/cpu_spqlios/vec_znx.rs +++ b/backend/src/implementation/cpu_spqlios/vec_znx.rs @@ -1,29 +1,22 @@ use itertools::izip; use rand_distr::Normal; -use rug::{ - Assign, Float, - float::Round, - ops::{AddAssignRound, DivAssignRound, SubAssignRound}, -}; use sampling::source::Source; use crate::{ hal::{ api::{ - TakeSlice, TakeVecZnx, VecZnxAddDistF64, VecZnxCopy, VecZnxDecodeVecFloat, VecZnxFillDistF64, - VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxSwithcDegree, ZnxInfos, ZnxSliceSize, ZnxView, - ZnxViewMut, ZnxZero, + TakeSlice, TakeVecZnx, VecZnxAddDistF64, VecZnxCopy, VecZnxFillDistF64, VecZnxNormalizeTmpBytes, VecZnxRotate, + VecZnxRotateInplace, VecZnxSwithcDegree, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut, ZnxZero, }, layouts::{Backend, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, VecZnxToMut, VecZnxToRef}, oep::{ VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl, - VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxDecodeCoeffsi64Impl, - VecZnxDecodeVecFloatImpl, VecZnxDecodeVeci64Impl, VecZnxEncodeCoeffsi64Impl, VecZnxEncodeVeci64Impl, - VecZnxFillDistF64Impl, VecZnxFillNormalImpl, VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, - VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, - VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl, - VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxStdImpl, VecZnxSubABInplaceImpl, - VecZnxSubBAInplaceImpl, VecZnxSubImpl, VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl, + VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxFillDistF64Impl, VecZnxFillNormalImpl, + VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxMulXpMinusOneImpl, + VecZnxMulXpMinusOneInplaceImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl, + VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl, VecZnxRotateInplaceImpl, + VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxSubABInplaceImpl, VecZnxSubBAInplaceImpl, VecZnxSubImpl, + VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl, }, }, implementation::cpu_spqlios::{ @@ -857,35 +850,6 @@ where }) } -unsafe impl VecZnxStdImpl for B -where - B: CPUAVX, -{ - fn vec_znx_std_impl(module: &Module, 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 = (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 VecZnxFillUniformImpl for B where B: CPUAVX, @@ -1053,251 +1017,3 @@ where ); } } - -unsafe impl VecZnxEncodeVeci64Impl for B -where - B: CPUAVX, -{ - fn encode_vec_i64_impl( - _module: &Module, - 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 VecZnxEncodeCoeffsi64Impl for B -where - B: CPUAVX, -{ - fn encode_coeff_i64_impl( - _module: &Module, - 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 VecZnxDecodeVeci64Impl for B -where - B: CPUAVX, -{ - fn decode_vec_i64_impl(_module: &Module, 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 VecZnxDecodeCoeffsi64Impl for B -where - B: CPUAVX, -{ - fn decode_coeff_i64_impl(_module: &Module, 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 VecZnxDecodeVecFloatImpl for B -where - B: CPUAVX, -{ - fn decode_vec_float_impl(_module: &Module, 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; - }); - } - }); - } -} diff --git a/bin_fhe/blind_rotation/key.rs b/bin_fhe/blind_rotation/key.rs deleted file mode 100644 index 9a33068..0000000 --- a/bin_fhe/blind_rotation/key.rs +++ /dev/null @@ -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 { - pub(crate) keys: Vec>, - pub(crate) dist: Distribution, -} - -impl fmt::Debug for BlindRotationKeyCGGI { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self) - } -} - -impl PartialEq for BlindRotationKeyCGGI { - 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 Eq for BlindRotationKeyCGGI {} - -impl fmt::Display for BlindRotationKeyCGGI { - 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 Reset for BlindRotationKeyCGGI { - fn reset(&mut self) { - self.keys.iter_mut().for_each(|key| key.reset()); - self.dist = Distribution::NONE; - } -} - -impl FillUniform for BlindRotationKeyCGGI { - 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 ReaderFrom for BlindRotationKeyCGGI { - fn read_from(&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::()? 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 WriterTo for BlindRotationKeyCGGI { - fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - match self.dist.write_to(writer) { - Ok(()) => {} - Err(e) => return Err(e), - } - writer.write_u64::(self.keys.len() as u64)?; - for key in &self.keys { - key.write_to(writer)?; - } - Ok(()) - } -} - -impl BlindRotationKeyCGGI> { - pub fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { - let mut data: Vec>> = 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(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize - where - Module: GGSWEncryptSkFamily, - { - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k, rank) - } -} - -impl BlindRotationKeyCGGI { - #[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 BlindRotationKeyCGGI { - pub fn generate_from_sk( - &mut self, - module: &Module, - sk_glwe: &GLWESecretExec, - sk_lwe: &LWESecret, - source_xa: &mut Source, - source_xe: &mut Source, - sigma: f64, - scratch: &mut Scratch, - ) where - DataSkGLWE: DataRef, - DataSkLWE: DataRef, - Module: GGSWEncryptSkFamily + VecZnxAddScalarInplace, - Scratch: TakeVecZnxDft + 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> = 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 { - pub(crate) data: Vec>, - pub(crate) dist: Distribution, - pub(crate) x_pow_a: Option, B>>>, -} - -impl BlindRotationKeyCGGIExec { - #[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 = SvpPPolAlloc + SvpPrepare; - -impl BlindRotationKeyCGGIExec, B> { - pub fn alloc(module: &Module, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self - where - Module: BlindRotationKeyCGGIExecLayoutFamily + VmpPMatAlloc + VmpPMatPrepare, - { - let mut data: Vec, 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(module: &Module, other: &BlindRotationKeyCGGI, scratch: &mut Scratch) -> Self - where - DataOther: DataRef, - Module: BlindRotationKeyCGGIExecLayoutFamily + VmpPMatAlloc + VmpPMatPrepare, - { - let mut brk: BlindRotationKeyCGGIExec, B> = Self::alloc( - module, - other.n(), - other.keys.len(), - other.basek(), - other.k(), - other.rows(), - other.rank(), - ); - brk.prepare(module, other, scratch); - brk - } -} - -impl BlindRotationKeyCGGIExec { - pub fn prepare(&mut self, module: &Module, other: &BlindRotationKeyCGGI, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: BlindRotationKeyCGGIExecLayoutFamily + VmpPMatAlloc + VmpPMatPrepare, - { - #[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, B>> = Vec::with_capacity(n << 1); - let mut buf: ScalarZnx> = ScalarZnx::alloc(n, 1); - (0..n << 1).for_each(|i| { - let mut res: SvpPPol, 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(module: &Module, ai: usize, y: i64, res: &mut SvpPPol, buf: &mut ScalarZnx) -where - A: DataMut, - C: DataMut, - Module: SvpPrepare, -{ - 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; - } -} diff --git a/bin_fhe/blind_rotation/key_compressed.rs b/bin_fhe/blind_rotation/key_compressed.rs deleted file mode 100644 index 52501dd..0000000 --- a/bin_fhe/blind_rotation/key_compressed.rs +++ /dev/null @@ -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 { - pub(crate) keys: Vec>, - pub(crate) dist: Distribution, -} - -impl fmt::Debug for BlindRotationKeyCGGICompressed { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self) - } -} - -impl PartialEq for BlindRotationKeyCGGICompressed { - 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 Eq for BlindRotationKeyCGGICompressed {} - -impl fmt::Display for BlindRotationKeyCGGICompressed { - 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 Reset for BlindRotationKeyCGGICompressed { - fn reset(&mut self) { - self.keys.iter_mut().for_each(|key| key.reset()); - self.dist = Distribution::NONE; - } -} - -impl FillUniform for BlindRotationKeyCGGICompressed { - 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 ReaderFrom for BlindRotationKeyCGGICompressed { - fn read_from(&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::()? 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 WriterTo for BlindRotationKeyCGGICompressed { - fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - match self.dist.write_to(writer) { - Ok(()) => {} - Err(e) => return Err(e), - } - writer.write_u64::(self.keys.len() as u64)?; - for key in &self.keys { - key.write_to(writer)?; - } - Ok(()) - } -} - -impl BlindRotationKeyCGGICompressed> { - pub fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { - let mut data: Vec>> = 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(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize - where - Module: GGSWEncryptSkFamily, - { - GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank) - } -} - -impl BlindRotationKeyCGGICompressed { - #[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 BlindRotationKeyCGGICompressed { - pub fn generate_from_sk( - &mut self, - module: &Module, - sk_glwe: &GLWESecretExec, - sk_lwe: &LWESecret, - seed_xa: [u8; 32], - source_xe: &mut Source, - sigma: f64, - scratch: &mut Scratch, - ) where - DataSkGLWE: DataRef, - DataSkLWE: DataRef, - Module: GGSWEncryptSkFamily + VecZnxAddScalarInplace, - Scratch: TakeVecZnxDft + 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> = 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, - ); - }); - } -} diff --git a/bin_fhe/blind_rotation/mod.rs b/bin_fhe/blind_rotation/mod.rs deleted file mode 100644 index 3ec8f09..0000000 --- a/bin_fhe/blind_rotation/mod.rs +++ /dev/null @@ -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; diff --git a/bin_fhe/blind_rotation/tests/generic_serialization.rs b/bin_fhe/blind_rotation/tests/generic_serialization.rs deleted file mode 100644 index 69592c9..0000000 --- a/bin_fhe/blind_rotation/tests/generic_serialization.rs +++ /dev/null @@ -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> = 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> = BlindRotationKeyCGGICompressed::alloc(256, 64, 12, 54, 2, 2); - test_reader_writer_interface(original); -} diff --git a/bin_fhe/blind_rotation/tests/generics_automorphism_key.rs b/bin_fhe/blind_rotation/tests/generics_automorphism_key.rs deleted file mode 100644 index 5677da1..0000000 --- a/bin_fhe/blind_rotation/tests/generics_automorphism_key.rs +++ /dev/null @@ -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 = AutomorphismKeyEncryptSkFamily - + GLWEKeyswitchFamily - + VecZnxAutomorphism - + GGLWEExecLayoutFamily - + VecZnxSwithcDegree - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxAutomorphismInplace - + GLWEDecryptFamily - + VecZnxSubScalarInplace - + VecZnxStd - + VecZnxCopy; -pub(crate) trait AutomorphismTestScratchFamily = ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl; - -pub(crate) fn test_automorphisk_key_encrypt_sk( - module: &Module, - basek: usize, - k_ksk: usize, - digits: usize, - rank: usize, - sigma: f64, -) where - Module: AutomorphismTestModuleFamily, - B: AutomorphismTestScratchFamily, -{ - let n: usize = TakeScalarZnx; - let rows: usize = (k_ksk - digits * basek) / (digits * basek); - - let mut atk: AutomorphismKey> = 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 = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( - module, n, basek, k_ksk, rank, - )); - - let mut sk: GLWESecret> = 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> = 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( - module: &Module, - basek: usize, - k_ksk: usize, - digits: usize, - rank: usize, - sigma: f64, -) where - Module: AutomorphismTestModuleFamily, - B: AutomorphismTestScratchFamily, -{ - let n: usize = TakeScalarZnx; - let rows: usize = (k_ksk - digits * basek) / (digits * basek); - - let mut atk_compressed: AutomorphismKeyCompressed> = - 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 = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( - module, n, basek, k_ksk, rank, - )); - - let mut sk: GLWESecret> = 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> = 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> = 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( - module: &Module, - p0: i64, - p1: i64, - basek: usize, - digits: usize, - k_in: usize, - k_out: usize, - k_apply: usize, - sigma: f64, - rank: usize, -) where - Module: AutomorphismTestModuleFamily, - B: AutomorphismTestScratchFamily, -{ - 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> = AutomorphismKey::alloc(n, basek, k_in, rows_in, digits_in, rank); - let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc(n, basek, k_out, rows_in, digits_in, rank); - let mut auto_key_apply: AutomorphismKey> = 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 = 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> = 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, 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> = GLWEPlaintext::alloc(TakeScalarZnx, basek, k_out); - - let mut sk_auto: GLWESecret> = 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, 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( - module: &Module, - p0: i64, - p1: i64, - basek: usize, - digits: usize, - k_in: usize, - k_apply: usize, - sigma: f64, - rank: usize, -) where - Module: AutomorphismTestModuleFamily, - B: AutomorphismTestScratchFamily, -{ - 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> = AutomorphismKey::alloc(n, basek, k_in, rows_in, digits_in, rank); - let mut auto_key_apply: AutomorphismKey> = 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 = 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> = 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, 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> = GLWEPlaintext::alloc(TakeScalarZnx, basek, k_in); - - let mut sk_auto: GLWESecret> = 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, 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 - ); - }); - }); -} diff --git a/bin_fhe/blind_rotation/tests/key.rs b/bin_fhe/blind_rotation/tests/key.rs deleted file mode 100644 index 7ae3f4c..0000000 --- a/bin_fhe/blind_rotation/tests/key.rs +++ /dev/null @@ -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 { - pub(crate) keys: Vec>, - pub(crate) dist: Distribution, -} - -impl PartialEq for BlindRotationKeyCGGI { - 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 Eq for BlindRotationKeyCGGI {} - -use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; - -impl ReaderFrom for BlindRotationKeyCGGI { - fn read_from(&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::()? 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 WriterTo for BlindRotationKeyCGGI { - fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - match self.dist.write_to(writer) { - Ok(()) => {} - Err(e) => return Err(e), - } - writer.write_u64::(self.keys.len() as u64)?; - for key in &self.keys { - key.write_to(writer)?; - } - Ok(()) - } -} - -impl BlindRotationKeyCGGI> { - pub fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { - let mut data: Vec>> = 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(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize - where - Module: GGSWEncryptSkFamily, - { - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k, rank) - } -} - -impl BlindRotationKeyCGGI { - #[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 BlindRotationKeyCGGI { - pub fn generate_from_sk( - &mut self, - module: &Module, - sk_glwe: &GLWESecretExec, - sk_lwe: &LWESecret, - source_xa: &mut Source, - source_xe: &mut Source, - sigma: f64, - scratch: &mut Scratch, - ) where - DataSkGLWE: DataRef, - DataSkLWE: DataRef, - Module: GGSWEncryptSkFamily + VecZnxAddScalarInplace, - Scratch: TakeVecZnxDft + 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> = 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 { - pub(crate) data: Vec>, - pub(crate) dist: Distribution, - pub(crate) x_pow_a: Option, B>>>, -} - -impl BlindRotationKeyCGGIExec { - #[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 = GGSWLayoutFamily + SvpPPolAlloc + SvpPrepare; - -impl BlindRotationKeyCGGIExec, B> { - pub fn alloc(module: &Module, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self - where - Module: BlindRotationKeyCGGIExecLayoutFamily, - { - let mut data: Vec, 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(module: &Module, other: &BlindRotationKeyCGGI, scratch: &mut Scratch) -> Self - where - DataOther: DataRef, - Module: BlindRotationKeyCGGIExecLayoutFamily, - { - let mut brk: BlindRotationKeyCGGIExec, B> = Self::alloc( - module, - other.n(), - other.keys.len(), - other.basek(), - other.k(), - other.rows(), - other.rank(), - ); - brk.prepare(module, other, scratch); - brk - } -} - -impl BlindRotationKeyCGGIExec { - pub fn prepare(&mut self, module: &Module, other: &BlindRotationKeyCGGI, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: BlindRotationKeyCGGIExecLayoutFamily, - { - #[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, B>> = Vec::with_capacity(n << 1); - let mut buf: ScalarZnx> = ScalarZnx::alloc(n, 1); - (0..n << 1).for_each(|i| { - let mut res: SvpPPol, 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(module: &Module, ai: usize, y: i64, res: &mut SvpPPol, buf: &mut ScalarZnx) -where - A: DataMut, - C: DataMut, - Module: SvpPrepare, -{ - 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; - } -} diff --git a/bin_fhe/blind_rotation/tests/mod.rs b/bin_fhe/blind_rotation/tests/mod.rs deleted file mode 100644 index a5e931c..0000000 --- a/bin_fhe/blind_rotation/tests/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod cpu_spqlios; -mod generic_cggi; -mod generic_lut; -mod generic_serialization; diff --git a/bin_fhe/circuit_bootstrapping/mod.rs b/bin_fhe/circuit_bootstrapping/mod.rs deleted file mode 100644 index 36ba50e..0000000 --- a/bin_fhe/circuit_bootstrapping/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod circuit_bootstrapping; - -pub use circuit_bootstrapping::*; - -#[cfg(test)] -mod test_fft64; diff --git a/bin_fhe/circuit_bootstrapping/test_fft64/mod.rs b/bin_fhe/circuit_bootstrapping/test_fft64/mod.rs deleted file mode 100644 index cc73725..0000000 --- a/bin_fhe/circuit_bootstrapping/test_fft64/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod circuit_bootstrapping; diff --git a/core/benches/external_product_glwe_fft64.rs b/core/benches/external_product_glwe_fft64.rs index db47ebb..ee74159 100644 --- a/core/benches/external_product_glwe_fft64.rs +++ b/core/benches/external_product_glwe_fft64.rs @@ -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 backend::{ @@ -63,7 +66,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretExec, FFT64> = GLWESecretExec::from(&module, &sk); + let sk_dft: GLWESecretPrepared, FFT64> = sk.prepare_alloc(&module, scratch.borrow()); ct_ggsw.encrypt_sk( &module, @@ -84,10 +87,10 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_exec: GGSWCiphertextExec, FFT64> = GGSWCiphertextExec::from(&module, &ct_ggsw, scratch.borrow()); + let ggsw_prepared: GGSWCiphertextPrepared, FFT64> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); 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> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretExec, FFT64> = GLWESecretExec::from(&module, &sk); + let sk_dft: GLWESecretPrepared, FFT64> = sk.prepare_alloc(&module, scratch.borrow()); ct_ggsw.encrypt_sk( &module, @@ -178,11 +181,11 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_exec: GGSWCiphertextExec, FFT64> = GGSWCiphertextExec::from(&module, &ct_ggsw, scratch.borrow()); + let ggsw_prepared: GGSWCiphertextPrepared, FFT64> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { 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)); } } diff --git a/core/benches/keyswitch_glwe_fft64.rs b/core/benches/keyswitch_glwe_fft64.rs index 29f30d3..3f17dfc 100644 --- a/core/benches/keyswitch_glwe_fft64.rs +++ b/core/benches/keyswitch_glwe_fft64.rs @@ -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 backend::{ @@ -66,7 +69,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut sk_in: GLWESecret> = GLWESecret::alloc(n, rank_in); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_dft: GLWESecretExec, FFT64> = GLWESecretExec::from(&module, &sk_in); + let sk_in_dft: GLWESecretPrepared, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank_out); sk_out.fill_ternary_prob(0.5, &mut source_xs); @@ -90,10 +93,10 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_exec: GGLWEAutomorphismKeyExec, _> = GGLWEAutomorphismKeyExec::from(&module, &ksk, scratch.borrow()); + let ksk_prepared: GGLWEAutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); 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> = GLWESecret::alloc(n, rank); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_dft: GLWESecretExec, FFT64> = GLWESecretExec::from(&module, &sk_in); + let sk_in_dft: GLWESecretPrepared, FFT64> = sk_in.prepare_alloc(&module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank); 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(), ); - let ksk_exec: GGLWESwitchingKeyExec, FFT64> = GGLWESwitchingKeyExec::from(&module, &ksk, scratch.borrow()); + let ksk_prepared: GGLWESwitchingKeyPrepared, FFT64> = ksk.prepare_alloc(&module, scratch.borrow()); move || { - black_box(ct.keyswitch_inplace(&module, &ksk_exec, scratch.borrow())); + black_box(ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow())); } } diff --git a/core/src/automorphism/gglwe_atk.rs b/core/src/automorphism/gglwe_atk.rs index 2a5c7f2..3fcf6db 100644 --- a/core/src/automorphism/gglwe_atk.rs +++ b/core/src/automorphism/gglwe_atk.rs @@ -4,7 +4,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GGLWEAutomorphismKey, GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec}, + layouts::{GGLWEAutomorphismKey, GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared}, trait_families::GLWEKeyswitchFamily, }; @@ -46,7 +46,7 @@ impl GGLWEAutomorphismKey { &mut self, module: &Module, lhs: &GGLWEAutomorphismKey, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxAutomorphismInplace, @@ -120,7 +120,7 @@ impl GGLWEAutomorphismKey { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxAutomorphismInplace, diff --git a/core/src/automorphism/ggsw_ct.rs b/core/src/automorphism/ggsw_ct.rs index fc76dbf..77c14ff 100644 --- a/core/src/automorphism/ggsw_ct.rs +++ b/core/src/automorphism/ggsw_ct.rs @@ -6,7 +6,7 @@ use backend::hal::{ use crate::{ layouts::{ GGSWCiphertext, GLWECiphertext, Infos, - prepared::{GGLWEAutomorphismKeyExec, GGLWETensorKeyExec}, + prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared}, }, trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily}, }; @@ -60,8 +60,8 @@ impl GGSWCiphertext { &mut self, module: &Module, lhs: &GGSWCiphertext, - auto_key: &GGLWEAutomorphismKeyExec, - tensor_key: &GGLWETensorKeyExec, + auto_key: &GGLWEAutomorphismKeyPrepared, + tensor_key: &GGLWETensorKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + GGSWKeySwitchFamily + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes, @@ -117,8 +117,8 @@ impl GGSWCiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - auto_key: &GGLWEAutomorphismKeyExec, - tensor_key: &GGLWETensorKeyExec, + auto_key: &GGLWEAutomorphismKeyPrepared, + tensor_key: &GGLWETensorKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + GGSWKeySwitchFamily + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes, @@ -134,7 +134,7 @@ impl GGSWCiphertext { &mut self, module: &Module, lhs: &GGSWCiphertext, - auto_key: &GGLWEAutomorphismKeyExec, + auto_key: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + GGSWKeySwitchFamily + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes, diff --git a/core/src/automorphism/glwe_ct.rs b/core/src/automorphism/glwe_ct.rs index 673a7a9..3f399b6 100644 --- a/core/src/automorphism/glwe_ct.rs +++ b/core/src/automorphism/glwe_ct.rs @@ -7,7 +7,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec}, + layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared}, trait_families::GLWEKeyswitchFamily, }; @@ -49,7 +49,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxAutomorphismInplace, @@ -64,7 +64,7 @@ impl GLWECiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxAutomorphismInplace, @@ -80,7 +80,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxBigAutomorphismInplace, @@ -102,7 +102,7 @@ impl GLWECiphertext { pub fn automorphism_add_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxBigAutomorphismInplace, @@ -118,7 +118,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallAInplace, @@ -140,7 +140,7 @@ impl GLWECiphertext { pub fn automorphism_sub_ab_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallAInplace, @@ -156,7 +156,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallBInplace, @@ -178,7 +178,7 @@ impl GLWECiphertext { pub fn automorphism_sub_ba_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallBInplace, diff --git a/core/src/conversion/glwe_to_lwe.rs b/core/src/conversion/glwe_to_lwe.rs index e77c93f..4da48b1 100644 --- a/core/src/conversion/glwe_to_lwe.rs +++ b/core/src/conversion/glwe_to_lwe.rs @@ -5,7 +5,7 @@ use backend::hal::{ use crate::{ TakeGLWECt, - layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::GLWEToLWESwitchingKeyExec}, + layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::GLWEToLWESwitchingKeyPrepared}, }; use crate::trait_families::GLWEKeyswitchFamily; @@ -50,7 +50,7 @@ impl LWECiphertext { &mut self, module: &Module, a: &GLWECiphertext, - ks: &GLWEToLWESwitchingKeyExec, + ks: &GLWEToLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where DGlwe: DataRef, diff --git a/core/src/conversion/lwe_to_glwe.rs b/core/src/conversion/lwe_to_glwe.rs index 25aff96..9e65291 100644 --- a/core/src/conversion/lwe_to_glwe.rs +++ b/core/src/conversion/lwe_to_glwe.rs @@ -5,7 +5,7 @@ use backend::hal::{ use crate::{ TakeGLWECt, - layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWEToGLWESwitchingKeyExec}, + layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWEToGLWESwitchingKeyPrepared}, }; use crate::trait_families::GLWEKeyswitchFamily; @@ -33,7 +33,7 @@ impl GLWECiphertext { &mut self, module: &Module, lwe: &LWECiphertext, - ksk: &LWEToGLWESwitchingKeyExec, + ksk: &LWEToGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where DLwe: DataRef, diff --git a/core/src/decryption/glwe_ct.rs b/core/src/decryption/glwe_ct.rs index 196c7d0..1e1ed1c 100644 --- a/core/src/decryption/glwe_ct.rs +++ b/core/src/decryption/glwe_ct.rs @@ -7,7 +7,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, + layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared}, trait_families::GLWEDecryptFamily, }; @@ -27,7 +27,7 @@ impl GLWECiphertext { &self, module: &Module, pt: &mut GLWEPlaintext, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, scratch: &mut Scratch, ) where Module: GLWEDecryptFamily, diff --git a/core/src/dist.rs b/core/src/dist.rs index a168530..415b0b5 100644 --- a/core/src/dist.rs +++ b/core/src/dist.rs @@ -1,7 +1,7 @@ use std::io::{Read, Result, Write}; #[derive(Clone, Copy, Debug)] -pub(crate) enum Distribution { +pub enum Distribution { TernaryFixed(usize), // Ternary with fixed Hamming weight TernaryProb(f64), // Ternary with probabilistic Hamming weight BinaryFixed(usize), // Binary with fixed Hamming weight diff --git a/core/src/encryption/compressed/gglwe_atk.rs b/core/src/encryption/compressed/gglwe_atk.rs index ff9b384..2efc7f9 100644 --- a/core/src/encryption/compressed/gglwe_atk.rs +++ b/core/src/encryption/compressed/gglwe_atk.rs @@ -8,19 +8,19 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecret, TakeGLWESecretExec, + TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ GLWESecret, Infos, compressed::{GGLWEAutomorphismKeyCompressed, GGLWESwitchingKeyCompressed}, }, }; -use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl GGLWEAutomorphismKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize where - Module: GGLWEAutomorphismKeyEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEAutomorphismKeyEncryptSkFamily + GLWESecretPreparedModuleFamily, { GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank) } @@ -41,8 +41,8 @@ impl GGLWEAutomorphismKeyCompressed { + VecZnxSwithcDegree + VecZnxAutomorphism + VecZnxAddScalarInplace - + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + TakeVecZnx, + + GLWESecretPreparedModuleFamily, + Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + TakeVecZnx, { #[cfg(debug_assertions)] { diff --git a/core/src/encryption/compressed/gglwe_ct.rs b/core/src/encryption/compressed/gglwe_ct.rs index 0c3e5b8..4d0ffe9 100644 --- a/core/src/encryption/compressed/gglwe_ct.rs +++ b/core/src/encryption/compressed/gglwe_ct.rs @@ -7,7 +7,7 @@ use sampling::source::Source; use crate::{ TakeGLWEPt, 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}; @@ -26,7 +26,7 @@ impl GGLWECiphertextCompressed { &mut self, module: &Module, pt: &ScalarZnx, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, seed: [u8; 32], source_xe: &mut Source, sigma: f64, diff --git a/core/src/encryption/compressed/gglwe_ksk.rs b/core/src/encryption/compressed/gglwe_ksk.rs index 7a92770..e529fce 100644 --- a/core/src/encryption/compressed/gglwe_ksk.rs +++ b/core/src/encryption/compressed/gglwe_ksk.rs @@ -5,13 +5,14 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecretExec, + TakeGLWESecretPrepared, 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> { pub fn encrypt_sk_scratch_space( @@ -23,11 +24,11 @@ impl GGLWESwitchingKeyCompressed> { rank_out: usize, ) -> usize where - Module: GGLWESwitchingKeyEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWESwitchingKeyEncryptSkFamily + GLWESecretPreparedModuleFamily, { (GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1)) + 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 GGLWESwitchingKeyCompressed { scratch: &mut Scratch, ) where Module: - GGLWESwitchingKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + ScratchAvailable + TakeVecZnx, + GGLWESwitchingKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily, + Scratch: + ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + ScratchAvailable + TakeVecZnx, { #[cfg(debug_assertions)] { @@ -85,7 +87,7 @@ impl GGLWESwitchingKeyCompressed { ); }); - 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); (0..sk_out.rank()).for_each(|i| { diff --git a/core/src/encryption/compressed/gglwe_tsk.rs b/core/src/encryption/compressed/gglwe_tsk.rs index 1a035e1..5ebb402 100644 --- a/core/src/encryption/compressed/gglwe_tsk.rs +++ b/core/src/encryption/compressed/gglwe_tsk.rs @@ -8,17 +8,17 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecret, TakeGLWESecretExec, - layouts::{GGLWETensorKey, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed}, + TakeGLWESecret, TakeGLWESecretPrepared, + layouts::{GGLWETensorKey, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed, prepared::Prepare}, trait_families::GLWEDecryptFamily, }; -use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl GGLWETensorKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize where - Module: GGLWETensorKeyEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWETensorKeyEncryptSkFamily + GLWESecretPreparedModuleFamily, { GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k, rank) } @@ -34,8 +34,10 @@ impl GGLWETensorKeyCompressed { sigma: f64, scratch: &mut Scratch, ) where - Module: GGLWETensorKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeGLWESecretExec + TakeScalarZnx + TakeVecZnx, + Module: + GGLWETensorKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily, + Scratch: + ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeGLWESecretPrepared + TakeScalarZnx + TakeVecZnx, { #[cfg(debug_assertions)] { @@ -46,8 +48,8 @@ impl GGLWETensorKeyCompressed { let n: usize = sk.n(); let rank: usize = self.rank(); - let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_exec(n, rank); - sk_dft_prep.prepare(module, &sk); + let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank); + sk_dft_prep.prepare(module, &sk, scratch1); let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1); diff --git a/core/src/encryption/compressed/ggsw_ct.rs b/core/src/encryption/compressed/ggsw_ct.rs index 9a62765..3925464 100644 --- a/core/src/encryption/compressed/ggsw_ct.rs +++ b/core/src/encryption/compressed/ggsw_ct.rs @@ -7,7 +7,7 @@ use sampling::source::Source; use crate::{ TakeGLWEPt, encryption::glwe_encrypt_sk_internal, - layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretExec}, + layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretPrepared}, }; use crate::trait_families::GGSWEncryptSkFamily; @@ -26,7 +26,7 @@ impl GGSWCiphertextCompressed { &mut self, module: &Module, pt: &ScalarZnx, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, seed_xa: [u8; 32], source_xe: &mut Source, sigma: f64, diff --git a/core/src/encryption/compressed/glwe_ct.rs b/core/src/encryption/compressed/glwe_ct.rs index 959f6dd..270d4af 100644 --- a/core/src/encryption/compressed/glwe_ct.rs +++ b/core/src/encryption/compressed/glwe_ct.rs @@ -6,7 +6,7 @@ use sampling::source::Source; use crate::{ 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; @@ -25,7 +25,7 @@ impl GLWECiphertextCompressed { &mut self, module: &Module, pt: &GLWEPlaintext, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, seed_xa: [u8; 32], source_xe: &mut Source, sigma: f64, @@ -49,7 +49,7 @@ impl GLWECiphertextCompressed { &mut self, module: &Module, pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, seed_xa: [u8; 32], source_xe: &mut Source, sigma: f64, diff --git a/core/src/encryption/gglwe_atk.rs b/core/src/encryption/gglwe_atk.rs index 181289e..b7ce5e5 100644 --- a/core/src/encryption/gglwe_atk.rs +++ b/core/src/encryption/gglwe_atk.rs @@ -8,16 +8,16 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecret, TakeGLWESecretExec, - layouts::{GGLWEAutomorphismKey, GLWESecret, GGLWESwitchingKey, Infos}, + TakeGLWESecret, TakeGLWESecretPrepared, + layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, GLWESecret, Infos}, }; -use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl GGLWEAutomorphismKey> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize where - Module: GGLWEAutomorphismKeyEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEAutomorphismKeyEncryptSkFamily + GLWESecretPreparedModuleFamily, { GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank) } @@ -42,8 +42,8 @@ impl GGLWEAutomorphismKey { + VecZnxAutomorphism + VecZnxSwithcDegree + VecZnxAddScalarInplace - + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + TakeVecZnx, + + GLWESecretPreparedModuleFamily, + Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + TakeVecZnx, { #[cfg(debug_assertions)] { diff --git a/core/src/encryption/gglwe_ct.rs b/core/src/encryption/gglwe_ct.rs index 2a5da8c..5e22b4e 100644 --- a/core/src/encryption/gglwe_ct.rs +++ b/core/src/encryption/gglwe_ct.rs @@ -9,7 +9,7 @@ use sampling::source::Source; use crate::{ TakeGLWEPt, - layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, + layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared}, }; use crate::trait_families::GGLWEEncryptSkFamily; @@ -33,7 +33,7 @@ impl GGLWECiphertext { &mut self, module: &Module, pt: &ScalarZnx, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, diff --git a/core/src/encryption/gglwe_ksk.rs b/core/src/encryption/gglwe_ksk.rs index d18339f..ff698f5 100644 --- a/core/src/encryption/gglwe_ksk.rs +++ b/core/src/encryption/gglwe_ksk.rs @@ -5,11 +5,11 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecretExec, - layouts::{GGLWECiphertext, GLWESecret, GGLWESwitchingKey, Infos, prepared::GLWESecretExec}, + TakeGLWESecretPrepared, + layouts::{GGLWECiphertext, GGLWESwitchingKey, GLWESecret, Infos, prepared::GLWESecretPrepared}, }; -use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl GGLWESwitchingKey> { pub fn encrypt_sk_scratch_space( @@ -21,11 +21,11 @@ impl GGLWESwitchingKey> { rank_out: usize, ) -> usize where - Module: GGLWESwitchingKeyEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWESwitchingKeyEncryptSkFamily + GLWESecretPreparedModuleFamily, { (GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1)) + 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( @@ -52,8 +52,9 @@ impl GGLWESwitchingKey { scratch: &mut Scratch, ) where Module: - GGLWESwitchingKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + ScratchAvailable + TakeVecZnx, + GGLWESwitchingKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily, + Scratch: + ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + ScratchAvailable + TakeVecZnx, { #[cfg(debug_assertions)] { @@ -94,7 +95,7 @@ impl GGLWESwitchingKey { ); }); - 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); (0..sk_out.rank()).for_each(|i| { diff --git a/core/src/encryption/gglwe_tsk.rs b/core/src/encryption/gglwe_tsk.rs index f1f462c..4b5b590 100644 --- a/core/src/encryption/gglwe_tsk.rs +++ b/core/src/encryption/gglwe_tsk.rs @@ -8,19 +8,22 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecret, TakeGLWESecretExec, - layouts::{GGLWETensorKey, GLWESecret, GGLWESwitchingKey, Infos, prepared::GLWESecretExec}, + TakeGLWESecret, TakeGLWESecretPrepared, + layouts::{ + GGLWESwitchingKey, GGLWETensorKey, GLWESecret, Infos, + prepared::{GLWESecretPrepared, Prepare}, + }, trait_families::GLWEDecryptFamily, }; -use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl GGLWETensorKey> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize where - Module: GGLWETensorKeyEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWETensorKeyEncryptSkFamily + GLWESecretPreparedModuleFamily, { - GLWESecretExec::bytes_of(module, n, rank) + GLWESecretPrepared::bytes_of(module, n, rank) + module.vec_znx_dft_alloc_bytes(n, rank, 1) + module.vec_znx_big_alloc_bytes(n, 1, 1) + module.vec_znx_dft_alloc_bytes(n, 1, 1) @@ -39,8 +42,10 @@ impl GGLWETensorKey { sigma: f64, scratch: &mut Scratch, ) where - Module: GGLWETensorKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeGLWESecretExec + TakeScalarZnx + TakeVecZnx, + Module: + GGLWETensorKeyEncryptSkFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily, + Scratch: + ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeGLWESecretPrepared + TakeScalarZnx + TakeVecZnx, { #[cfg(debug_assertions)] { @@ -52,8 +57,8 @@ impl GGLWETensorKey { let rank: usize = self.rank(); - let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_exec(n, rank); - sk_dft_prep.prepare(module, &sk); + let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank); + sk_dft_prep.prepare(module, &sk, scratch1); let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1); diff --git a/core/src/encryption/ggsw_ct.rs b/core/src/encryption/ggsw_ct.rs index 463299e..e09fd65 100644 --- a/core/src/encryption/ggsw_ct.rs +++ b/core/src/encryption/ggsw_ct.rs @@ -6,7 +6,7 @@ use sampling::source::Source; use crate::{ TakeGLWEPt, - layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GLWESecretExec}, + layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GLWESecretPrepared}, }; use crate::trait_families::GLWEEncryptSkFamily; @@ -31,7 +31,7 @@ impl GGSWCiphertext { &mut self, module: &Module, pt: &ScalarZnx, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, diff --git a/core/src/encryption/glwe_ct.rs b/core/src/encryption/glwe_ct.rs index 6b058ae..b82402b 100644 --- a/core/src/encryption/glwe_ct.rs +++ b/core/src/encryption/glwe_ct.rs @@ -13,7 +13,7 @@ use crate::{ dist::Distribution, layouts::{ GLWECiphertext, GLWEPlaintext, Infos, - prepared::{GLWEPublicKeyExec, GLWESecretExec}, + prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, }, trait_families::{GLWEEncryptPkFamily, GLWEEncryptSkFamily}, }; @@ -42,7 +42,7 @@ impl GLWECiphertext { &mut self, module: &Module, pt: &GLWEPlaintext, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -78,7 +78,7 @@ impl GLWECiphertext { pub fn encrypt_zero_sk( &mut self, module: &Module, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -113,7 +113,7 @@ impl GLWECiphertext { &mut self, module: &Module, pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -143,7 +143,7 @@ impl GLWECiphertext { &mut self, module: &Module, pt: &GLWEPlaintext, - pk: &GLWEPublicKeyExec, + pk: &GLWEPublicKeyPrepared, source_xu: &mut Source, source_xe: &mut Source, sigma: f64, @@ -166,7 +166,7 @@ impl GLWECiphertext { pub fn encrypt_zero_pk( &mut self, module: &Module, - pk: &GLWEPublicKeyExec, + pk: &GLWEPublicKeyPrepared, source_xu: &mut Source, source_xe: &mut Source, sigma: f64, @@ -190,7 +190,7 @@ impl GLWECiphertext { &mut self, module: &Module, pt: Option<(&GLWEPlaintext, usize)>, - pk: &GLWEPublicKeyExec, + pk: &GLWEPublicKeyPrepared, source_xu: &mut Source, source_xe: &mut Source, sigma: f64, @@ -283,7 +283,7 @@ pub(crate) fn glwe_encrypt_sk_internal, usize)>, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, diff --git a/core/src/encryption/glwe_pk.rs b/core/src/encryption/glwe_pk.rs index 1738b30..2442824 100644 --- a/core/src/encryption/glwe_pk.rs +++ b/core/src/encryption/glwe_pk.rs @@ -7,7 +7,7 @@ use sampling::source::Source; use crate::{ dist::Distribution, - layouts::{GLWECiphertext, GLWEPublicKey, Infos, prepared::GLWESecretExec}, + layouts::{GLWECiphertext, GLWEPublicKey, Infos, prepared::GLWESecretPrepared}, }; use crate::trait_families::GLWEEncryptSkFamily; @@ -16,7 +16,7 @@ impl GLWEPublicKey { pub fn generate_from_sk( &mut self, module: &Module, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, diff --git a/core/src/encryption/glwe_to_lwe_ksk.rs b/core/src/encryption/glwe_to_lwe_ksk.rs index b746fb6..be4f618 100644 --- a/core/src/encryption/glwe_to_lwe_ksk.rs +++ b/core/src/encryption/glwe_to_lwe_ksk.rs @@ -8,18 +8,18 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecret, TakeGLWESecretExec, - layouts::{GLWESecret, GGLWESwitchingKey, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretExec}, + TakeGLWESecret, TakeGLWESecretPrepared, + layouts::{GGLWESwitchingKey, GLWESecret, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretPrepared}, }; -use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl GLWEToLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank_in: usize) -> usize where - Module: GGLWEEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEEncryptSkFamily + GLWESecretPreparedModuleFamily, { - 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)) } } @@ -41,8 +41,8 @@ impl GLWEToLWESwitchingKey { + VecZnxAutomorphismInplace + VecZnxSwithcDegree + VecZnxAddScalarInplace - + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + TakeVecZnx, + + GLWESecretPreparedModuleFamily, + Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + TakeVecZnx, { #[cfg(debug_assertions)] { diff --git a/core/src/encryption/lwe_ksk.rs b/core/src/encryption/lwe_ksk.rs index d423f69..b9b5058 100644 --- a/core/src/encryption/lwe_ksk.rs +++ b/core/src/encryption/lwe_ksk.rs @@ -8,19 +8,19 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecret, TakeGLWESecretExec, - layouts::{GLWESecret, GGLWESwitchingKey, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretExec}, + TakeGLWESecret, TakeGLWESecretPrepared, + layouts::{GGLWESwitchingKey, GLWESecret, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretPrepared}, }; -use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl LWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize) -> usize where - Module: GGLWEEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEEncryptSkFamily + GLWESecretPreparedModuleFamily, { 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) } } @@ -42,8 +42,8 @@ impl LWESwitchingKey { + VecZnxAutomorphismInplace + VecZnxSwithcDegree + VecZnxAddScalarInplace - + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + TakeVecZnx, + + GLWESecretPreparedModuleFamily, + Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + TakeVecZnx, { #[cfg(debug_assertions)] { diff --git a/core/src/encryption/lwe_to_glwe_ksk.rs b/core/src/encryption/lwe_to_glwe_ksk.rs index 73a261c..d76ceeb 100644 --- a/core/src/encryption/lwe_to_glwe_ksk.rs +++ b/core/src/encryption/lwe_to_glwe_ksk.rs @@ -8,16 +8,16 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - TakeGLWESecret, TakeGLWESecretExec, - layouts::{GLWESecret, GGLWESwitchingKey, LWESecret, LWEToGLWESwitchingKey}, + TakeGLWESecret, TakeGLWESecretPrepared, + layouts::{GGLWESwitchingKey, GLWESecret, LWESecret, LWEToGLWESwitchingKey}, }; -use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; impl LWEToGLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank_out: usize) -> usize where - Module: GGLWEEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEEncryptSkFamily + GLWESecretPreparedModuleFamily, { GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, rank_out) + GLWESecret::bytes_of(n, 1) } @@ -40,8 +40,8 @@ impl LWEToGLWESwitchingKey { + VecZnxAutomorphismInplace + VecZnxSwithcDegree + VecZnxAddScalarInplace - + GLWESecretExecModuleFamily, - Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + TakeVecZnx, + + GLWESecretPreparedModuleFamily, + Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + TakeVecZnx, { #[cfg(debug_assertions)] { diff --git a/core/src/external_product/gglwe_atk.rs b/core/src/external_product/gglwe_atk.rs index e82dbfc..3e711de 100644 --- a/core/src/external_product/gglwe_atk.rs +++ b/core/src/external_product/gglwe_atk.rs @@ -4,7 +4,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, prepared::GGSWCiphertextExec}, + layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, prepared::GGSWCiphertextPrepared}, trait_families::GLWEExternalProductFamily, }; @@ -46,7 +46,7 @@ impl GGLWEAutomorphismKey { &mut self, module: &Module, lhs: &GGLWEAutomorphismKey, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, @@ -58,7 +58,7 @@ impl GGLWEAutomorphismKey { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, diff --git a/core/src/external_product/gglwe_ksk.rs b/core/src/external_product/gglwe_ksk.rs index 4bd34f4..9728cee 100644 --- a/core/src/external_product/gglwe_ksk.rs +++ b/core/src/external_product/gglwe_ksk.rs @@ -4,7 +4,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GLWECiphertext, GGLWESwitchingKey, Infos, prepared::GGSWCiphertextExec}, + layouts::{GGLWESwitchingKey, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared}, trait_families::GLWEExternalProductFamily, }; @@ -46,7 +46,7 @@ impl GGLWESwitchingKey { &mut self, module: &Module, lhs: &GGLWESwitchingKey, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, @@ -94,7 +94,7 @@ impl GGLWESwitchingKey { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, diff --git a/core/src/external_product/ggsw_ct.rs b/core/src/external_product/ggsw_ct.rs index 5074fc8..ee17aa6 100644 --- a/core/src/external_product/ggsw_ct.rs +++ b/core/src/external_product/ggsw_ct.rs @@ -4,7 +4,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GGSWCiphertextExec}, + layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared}, trait_families::GLWEExternalProductFamily, }; @@ -46,7 +46,7 @@ impl GGSWCiphertext { &mut self, module: &Module, lhs: &GGSWCiphertext, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, @@ -105,7 +105,7 @@ impl GGSWCiphertext { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, diff --git a/core/src/external_product/glwe_ct.rs b/core/src/external_product/glwe_ct.rs index b114f1e..b5ab132 100644 --- a/core/src/external_product/glwe_ct.rs +++ b/core/src/external_product/glwe_ct.rs @@ -7,7 +7,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GLWECiphertext, Infos, prepared::GGSWCiphertextExec}, + layouts::{GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared}, trait_families::GLWEExternalProductFamily, }; @@ -64,7 +64,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, @@ -141,7 +141,7 @@ impl GLWECiphertext { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextExec, + rhs: &GGSWCiphertextPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductFamily, diff --git a/core/src/glwe_packing.rs b/core/src/glwe_packing.rs index d7329e2..481bdb0 100644 --- a/core/src/glwe_packing.rs +++ b/core/src/glwe_packing.rs @@ -7,7 +7,7 @@ use backend::hal::{ use crate::{ GLWEOperations, TakeGLWECt, - layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec}, + layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared}, }; use crate::trait_families::{GLWEKeyswitchFamily, GLWEPackingFamily}; @@ -115,7 +115,7 @@ impl GLWEPacker { &mut self, module: &Module, a: Option<&GLWECiphertext>, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: GLWEPackingFamily, @@ -174,7 +174,7 @@ fn pack_core( a: Option<&GLWECiphertext>, accumulators: &mut [Accumulator], i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: GLWEPackingFamily, @@ -252,7 +252,7 @@ fn combine( acc: &mut Accumulator, b: Option<&GLWECiphertext>, i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: GLWEPackingFamily, diff --git a/core/src/glwe_trace.rs b/core/src/glwe_trace.rs index 6891b78..12284a1 100644 --- a/core/src/glwe_trace.rs +++ b/core/src/glwe_trace.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use backend::hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; use crate::{ - layouts::{GLWECiphertext, prepared::GGLWEAutomorphismKeyExec}, + layouts::{GLWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}, operations::GLWEOperations, }; @@ -61,7 +61,7 @@ impl GLWECiphertext { start: usize, end: usize, lhs: &GLWECiphertext, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: GLWETraceModuleFamily, @@ -76,7 +76,7 @@ impl GLWECiphertext { module: &Module, start: usize, end: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: GLWETraceModuleFamily, diff --git a/core/src/keyswitching/gglwe_ct.rs b/core/src/keyswitching/gglwe_ct.rs index 19d8242..b25e0fb 100644 --- a/core/src/keyswitching/gglwe_ct.rs +++ b/core/src/keyswitching/gglwe_ct.rs @@ -5,8 +5,8 @@ use backend::hal::{ use crate::{ layouts::{ - GGLWEAutomorphismKey, GLWECiphertext, GGLWESwitchingKey, Infos, - prepared::{GGLWEAutomorphismKeyExec, GGLWESwitchingKeyExec}, + GGLWEAutomorphismKey, GGLWESwitchingKey, GLWECiphertext, Infos, + prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared}, }, trait_families::GLWEKeyswitchFamily, }; @@ -49,7 +49,7 @@ impl GGLWEAutomorphismKey { &mut self, module: &Module, lhs: &GGLWEAutomorphismKey, - rhs: &GGLWESwitchingKeyExec, + rhs: &GGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily, @@ -61,7 +61,7 @@ impl GGLWEAutomorphismKey { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyExec, + rhs: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily, @@ -112,7 +112,7 @@ impl GGLWESwitchingKey { &mut self, module: &Module, lhs: &GGLWESwitchingKey, - rhs: &GGLWESwitchingKeyExec, + rhs: &GGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily, @@ -160,7 +160,7 @@ impl GGLWESwitchingKey { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWESwitchingKeyExec, + rhs: &GGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily, diff --git a/core/src/keyswitching/ggsw_ct.rs b/core/src/keyswitching/ggsw_ct.rs index 4aaf96b..10802bc 100644 --- a/core/src/keyswitching/ggsw_ct.rs +++ b/core/src/keyswitching/ggsw_ct.rs @@ -9,7 +9,7 @@ use backend::hal::{ use crate::{ layouts::{ GGLWECiphertext, GGSWCiphertext, GLWECiphertext, Infos, - prepared::{GGLWESwitchingKeyExec, GGLWETensorKeyExec}, + prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared}, }, operations::GLWEOperations, trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily}, @@ -97,7 +97,7 @@ impl GGSWCiphertext { &mut self, module: &Module, a: &GGLWECiphertext, - tsk: &GGLWETensorKeyExec, + tsk: &GGLWETensorKeyPrepared, scratch: &mut Scratch, ) where DataA: DataRef, @@ -123,8 +123,8 @@ impl GGSWCiphertext { &mut self, module: &Module, lhs: &GGSWCiphertext, - ksk: &GGLWESwitchingKeyExec, - tsk: &GGLWETensorKeyExec, + ksk: &GGLWESwitchingKeyPrepared, + tsk: &GGLWETensorKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + GGSWKeySwitchFamily + VecZnxNormalizeTmpBytes, @@ -137,8 +137,8 @@ impl GGSWCiphertext { pub fn keyswitch_inplace( &mut self, module: &Module, - ksk: &GGLWESwitchingKeyExec, - tsk: &GGLWETensorKeyExec, + ksk: &GGLWESwitchingKeyPrepared, + tsk: &GGLWETensorKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + GGSWKeySwitchFamily + VecZnxNormalizeTmpBytes, @@ -153,7 +153,7 @@ impl GGSWCiphertext { pub fn expand_row( &mut self, module: &Module, - tsk: &GGLWETensorKeyExec, + tsk: &GGLWETensorKeyPrepared, scratch: &mut Scratch, ) where Module: GGSWKeySwitchFamily + VecZnxNormalizeTmpBytes, @@ -278,7 +278,7 @@ impl GGSWCiphertext { &mut self, module: &Module, lhs: &GGSWCiphertext, - ksk: &GGLWESwitchingKeyExec, + ksk: &GGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily + GGSWKeySwitchFamily + VecZnxNormalizeTmpBytes, diff --git a/core/src/keyswitching/glwe_ct.rs b/core/src/keyswitching/glwe_ct.rs index cfc5297..672a2e3 100644 --- a/core/src/keyswitching/glwe_ct.rs +++ b/core/src/keyswitching/glwe_ct.rs @@ -7,7 +7,7 @@ use backend::hal::{ }; use crate::{ - layouts::{GLWECiphertext, Infos, prepared::GGLWESwitchingKeyExec}, + layouts::{GLWECiphertext, Infos, prepared::GGLWESwitchingKeyPrepared}, trait_families::GLWEKeyswitchFamily, }; @@ -65,7 +65,7 @@ impl GLWECiphertext { &self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWESwitchingKeyExec, + rhs: &GGLWESwitchingKeyPrepared, scratch: &Scratch, ) where DataLhs: DataRef, @@ -136,7 +136,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWESwitchingKeyExec, + rhs: &GGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily, @@ -156,7 +156,7 @@ impl GLWECiphertext { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWESwitchingKeyExec, + rhs: &GGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: GLWEKeyswitchFamily, @@ -174,7 +174,7 @@ impl GLWECiphertext { &self, module: &Module, res_dft: VecZnxDft, - rhs: &GGLWESwitchingKeyExec, + rhs: &GGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) -> VecZnxBig where diff --git a/core/src/keyswitching/lwe_ct.rs b/core/src/keyswitching/lwe_ct.rs index 1623b21..70febf5 100644 --- a/core/src/keyswitching/lwe_ct.rs +++ b/core/src/keyswitching/lwe_ct.rs @@ -5,7 +5,7 @@ use backend::hal::{ use crate::{ TakeGLWECt, - layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWESwitchingKeyExec}, + layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWESwitchingKeyPrepared}, }; use crate::trait_families::GLWEKeyswitchFamily; @@ -32,7 +32,7 @@ impl LWECiphertext { &mut self, module: &Module, a: &LWECiphertext, - ksk: &LWESwitchingKeyExec, + ksk: &LWESwitchingKeyPrepared, scratch: &mut Scratch, ) where A: DataRef, diff --git a/core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/core/src/layouts/compressed/glwe_to_lwe_ksk.rs index a2b752c..3f190d4 100644 --- a/core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -7,7 +7,7 @@ use backend::hal::{ use crate::layouts::{GLWEToLWESwitchingKey, Infos, compressed::GGLWESwitchingKeyCompressed}; -use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily}; #[derive(PartialEq, Eq, Clone)] pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); @@ -91,7 +91,7 @@ impl GLWEToLWESwitchingKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank_in: usize) -> usize where - Module: GGLWEEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEEncryptSkFamily + GLWESecretPreparedModuleFamily, { GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in) } diff --git a/core/src/layouts/compressed/lwe_ksk.rs b/core/src/layouts/compressed/lwe_ksk.rs index 55b2c78..312fd2f 100644 --- a/core/src/layouts/compressed/lwe_ksk.rs +++ b/core/src/layouts/compressed/lwe_ksk.rs @@ -6,7 +6,7 @@ use backend::hal::{ use crate::layouts::{Infos, LWESwitchingKey, compressed::GGLWESwitchingKeyCompressed}; use std::fmt; -use crate::trait_families::{Decompress, GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{Decompress, GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily}; #[derive(PartialEq, Eq, Clone)] pub struct LWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); @@ -90,7 +90,7 @@ impl LWESwitchingKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize) -> usize where - Module: GGLWEEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEEncryptSkFamily + GLWESecretPreparedModuleFamily, { LWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k) } diff --git a/core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/core/src/layouts/compressed/lwe_to_glwe_ksk.rs index 2e0be41..3c3be1c 100644 --- a/core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -9,7 +9,7 @@ use crate::{ }; use std::fmt; -use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily}; #[derive(PartialEq, Eq, Clone)] pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); @@ -93,7 +93,7 @@ impl LWEToGLWESwitchingKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank_out: usize) -> usize where - Module: GGLWEEncryptSkFamily + GLWESecretExecModuleFamily, + Module: GGLWEEncryptSkFamily + GLWESecretPreparedModuleFamily, { LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_out) } diff --git a/core/src/layouts/gglwe_atk.rs b/core/src/layouts/gglwe_atk.rs index 535eda0..3ab67a0 100644 --- a/core/src/layouts/gglwe_atk.rs +++ b/core/src/layouts/gglwe_atk.rs @@ -3,7 +3,7 @@ use backend::hal::{ 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 std::fmt; diff --git a/core/src/layouts/glwe_pt.rs b/core/src/layouts/glwe_pt.rs index e7724c8..abfd867 100644 --- a/core/src/layouts/glwe_pt.rs +++ b/core/src/layouts/glwe_pt.rs @@ -1,3 +1,5 @@ +use std::fmt; + use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef}; use crate::layouts::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, Infos, SetMetaData}; @@ -8,6 +10,18 @@ pub struct GLWEPlaintext { pub k: usize, } +impl fmt::Display for GLWEPlaintext { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "GLWEPlaintext: basek={} k={}: {}", + self.basek(), + self.k(), + self.data + ) + } +} + impl Infos for GLWEPlaintext { type Inner = VecZnx; diff --git a/core/src/layouts/lwe_ct.rs b/core/src/layouts/lwe_ct.rs index c38b1a0..7c0a145 100644 --- a/core/src/layouts/lwe_ct.rs +++ b/core/src/layouts/lwe_ct.rs @@ -13,6 +13,18 @@ pub struct LWECiphertext { pub(crate) basek: usize, } +impl LWECiphertext { + pub fn data(&self) -> &VecZnx { + &self.data + } +} + +impl LWECiphertext { + pub fn data_mut(&mut self) -> &VecZnx { + &mut self.data + } +} + impl fmt::Debug for LWECiphertext { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self) @@ -31,10 +43,7 @@ impl fmt::Display for LWECiphertext { } } -impl Reset for LWECiphertext -where - VecZnx: Reset, -{ +impl Reset for LWECiphertext { fn reset(&mut self) { self.data.reset(); self.basek = 0; diff --git a/core/src/layouts/lwe_pt.rs b/core/src/layouts/lwe_pt.rs index 06e87ed..957bcac 100644 --- a/core/src/layouts/lwe_pt.rs +++ b/core/src/layouts/lwe_pt.rs @@ -1,3 +1,5 @@ +use std::fmt; + use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef}; use crate::layouts::{Infos, SetMetaData}; @@ -18,6 +20,18 @@ impl LWEPlaintext> { } } +impl fmt::Display for LWEPlaintext { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "LWEPlaintext: basek={} k={}: {}", + self.basek(), + self.k(), + self.data + ) + } +} + impl Infos for LWEPlaintext { type Inner = VecZnx; diff --git a/core/src/layouts/lwe_sk.rs b/core/src/layouts/lwe_sk.rs index a2cb2fc..0444b85 100644 --- a/core/src/layouts/lwe_sk.rs +++ b/core/src/layouts/lwe_sk.rs @@ -1,6 +1,6 @@ use backend::hal::{ - api::{ZnxInfos, ZnxZero}, - layouts::{Data, DataMut, ScalarZnx}, + api::{ZnxInfos, ZnxView, ZnxZero}, + layouts::{Data, DataMut, DataRef, ScalarZnx}, }; use sampling::source::Source; @@ -20,6 +20,20 @@ impl LWESecret> { } } +impl LWESecret { + pub fn raw(&self) -> &[i64] { + self.data.at(0, 0) + } + + pub fn dist(&self) -> Distribution { + self.dist + } + + pub fn data(&self) -> &ScalarZnx { + &self.data + } +} + impl LWESecret { pub fn n(&self) -> usize { self.data.n() diff --git a/core/src/layouts/prepared/gglwe_atk.rs b/core/src/layouts/prepared/gglwe_atk.rs index d0fea01..0c60306 100644 --- a/core/src/layouts/prepared/gglwe_atk.rs +++ b/core/src/layouts/prepared/gglwe_atk.rs @@ -3,21 +3,24 @@ use backend::hal::{ 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)] -pub struct GGLWEAutomorphismKeyExec { - pub(crate) key: GGLWESwitchingKeyExec, +pub struct GGLWEAutomorphismKeyPrepared { + pub(crate) key: GGLWESwitchingKeyPrepared, pub(crate) p: i64, } -impl GGLWEAutomorphismKeyExec, B> { +impl GGLWEAutomorphismKeyPrepared, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: VmpPMatAlloc, { - GGLWEAutomorphismKeyExec::, B> { - key: GGLWESwitchingKeyExec::alloc(module, n, basek, k, rows, digits, rank, rank), + GGLWEAutomorphismKeyPrepared::, B> { + key: GGLWESwitchingKeyPrepared::alloc(module, n, basek, k, rows, digits, rank, rank), p: 0, } } @@ -26,28 +29,11 @@ impl GGLWEAutomorphismKeyExec, B> { where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyExec::bytes_of(module, n, basek, k, rows, digits, rank, rank) - } - - pub fn from(module: &Module, other: &GGLWEAutomorphismKey, scratch: &mut Scratch) -> Self - where - Module: VmpPMatAlloc + VmpPMatPrepare, - { - let mut atk_exec: GGLWEAutomorphismKeyExec, B> = Self::alloc( - module, - other.n(), - other.basek(), - other.k(), - other.rows(), - other.digits(), - other.rank(), - ); - atk_exec.prepare(module, other, scratch); - atk_exec + GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, rank, rank) } } -impl Infos for GGLWEAutomorphismKeyExec { +impl Infos for GGLWEAutomorphismKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -63,7 +49,7 @@ impl Infos for GGLWEAutomorphismKeyExec { } } -impl GGLWEAutomorphismKeyExec { +impl GGLWEAutomorphismKeyPrepared { pub fn p(&self) -> i64 { self.p } @@ -85,13 +71,31 @@ impl GGLWEAutomorphismKeyExec { } } -impl GGLWEAutomorphismKeyExec { - pub fn prepare(&mut self, module: &Module, other: &GGLWEAutomorphismKey, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl Prepare> for GGLWEAutomorphismKeyPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &GGLWEAutomorphismKey, scratch: &mut Scratch) { self.key.prepare(module, &other.key, scratch); self.p = other.p; } -} \ No newline at end of file +} + +impl PrepareAlloc, B>> for GGLWEAutomorphismKey +where + Module: VmpPMatAlloc + VmpPMatPrepare, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWEAutomorphismKeyPrepared, B> { + let mut atk_prepared: GGLWEAutomorphismKeyPrepared, B> = GGLWEAutomorphismKeyPrepared::alloc( + module, + self.n(), + self.basek(), + self.k(), + self.rows(), + self.digits(), + self.rank(), + ); + atk_prepared.prepare(module, self, scratch); + atk_prepared + } +} diff --git a/core/src/layouts/prepared/gglwe_ct.rs b/core/src/layouts/prepared/gglwe_ct.rs index aace85a..7bbd4e8 100644 --- a/core/src/layouts/prepared/gglwe_ct.rs +++ b/core/src/layouts/prepared/gglwe_ct.rs @@ -3,17 +3,20 @@ use backend::hal::{ layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, }; -use crate::layouts::{GGLWECiphertext, Infos}; +use crate::layouts::{ + GGLWECiphertext, Infos, + prepared::{Prepare, PrepareAlloc}, +}; #[derive(PartialEq, Eq)] -pub struct GGLWECiphertextExec { +pub struct GGLWECiphertextPrepared { pub(crate) data: VmpPMat, pub(crate) basek: usize, pub(crate) k: usize, pub(crate) digits: usize, } -impl GGLWECiphertextExec, B> { +impl GGLWECiphertextPrepared, B> { pub fn alloc( module: &Module, n: usize, @@ -84,7 +87,7 @@ impl GGLWECiphertextExec, B> { } } -impl Infos for GGLWECiphertextExec { +impl Infos for GGLWECiphertextPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -100,7 +103,7 @@ impl Infos for GGLWECiphertextExec { } } -impl GGLWECiphertextExec { +impl GGLWECiphertextPrepared { pub fn rank(&self) -> usize { self.data.cols_out() - 1 } @@ -118,15 +121,34 @@ impl GGLWECiphertextExec { } } -impl GGLWECiphertextExec { - pub fn prepare(&mut self, module: &Module, other: &GGLWECiphertext, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl Prepare> for GGLWECiphertextPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &GGLWECiphertext, scratch: &mut Scratch) { module.vmp_prepare(&mut self.data, &other.data, scratch); self.basek = other.basek; self.k = other.k; self.digits = other.digits; } } + +impl PrepareAlloc, B>> for GGLWECiphertext +where + Module: VmpPMatAlloc + VmpPMatPrepare, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWECiphertextPrepared, B> { + let mut atk_prepared: GGLWECiphertextPrepared, 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 + } +} diff --git a/core/src/layouts/prepared/gglwe_ksk.rs b/core/src/layouts/prepared/gglwe_ksk.rs index b28ab4e..0fb9bc5 100644 --- a/core/src/layouts/prepared/gglwe_ksk.rs +++ b/core/src/layouts/prepared/gglwe_ksk.rs @@ -3,16 +3,19 @@ use backend::hal::{ 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)] -pub struct GGLWESwitchingKeyExec { - pub(crate) key: GGLWECiphertextExec, +pub struct GGLWESwitchingKeyPrepared { + pub(crate) key: GGLWECiphertextPrepared, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl GGLWESwitchingKeyExec, B> { +impl GGLWESwitchingKeyPrepared, B> { pub fn alloc( module: &Module, n: usize, @@ -26,8 +29,8 @@ impl GGLWESwitchingKeyExec, B> { where Module: VmpPMatAlloc, { - GGLWESwitchingKeyExec::, B> { - key: GGLWECiphertextExec::alloc(module, n, basek, k, rows, digits, rank_in, rank_out), + GGLWESwitchingKeyPrepared::, B> { + key: GGLWECiphertextPrepared::alloc(module, n, basek, k, rows, digits, rank_in, rank_out), sk_in_n: 0, sk_out_n: 0, } @@ -46,29 +49,11 @@ impl GGLWESwitchingKeyExec, B> { where Module: VmpPMatAllocBytes, { - GGLWECiphertextExec::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out) - } - - pub fn from(module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) -> Self - where - Module: VmpPMatAlloc + VmpPMatPrepare, - { - let mut ksk_exec: GGLWESwitchingKeyExec, 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 + GGLWECiphertextPrepared::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out) } } -impl Infos for GGLWESwitchingKeyExec { +impl Infos for GGLWESwitchingKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -84,7 +69,7 @@ impl Infos for GGLWESwitchingKeyExec { } } -impl GGLWESwitchingKeyExec { +impl GGLWESwitchingKeyPrepared { pub fn rank(&self) -> usize { self.key.data.cols_out() - 1 } @@ -110,14 +95,33 @@ impl GGLWESwitchingKeyExec { } } -impl GGLWESwitchingKeyExec { - pub fn prepare(&mut self, module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl Prepare> for GGLWESwitchingKeyPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) { self.key.prepare(module, &other.key, scratch); self.sk_in_n = other.sk_in_n; self.sk_out_n = other.sk_out_n; } } + +impl PrepareAlloc, B>> for GGLWESwitchingKey +where + Module: VmpPMatAlloc + VmpPMatPrepare, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWESwitchingKeyPrepared, B> { + let mut atk_prepared: GGLWESwitchingKeyPrepared, 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 + } +} diff --git a/core/src/layouts/prepared/gglwe_tsk.rs b/core/src/layouts/prepared/gglwe_tsk.rs index a1af02a..adbb13e 100644 --- a/core/src/layouts/prepared/gglwe_tsk.rs +++ b/core/src/layouts/prepared/gglwe_tsk.rs @@ -3,22 +3,25 @@ use backend::hal::{ 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)] -pub struct GGLWETensorKeyExec { - pub(crate) keys: Vec>, +pub struct GGLWETensorKeyPrepared { + pub(crate) keys: Vec>, } -impl GGLWETensorKeyExec, B> { +impl GGLWETensorKeyPrepared, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: VmpPMatAlloc, { - let mut keys: Vec, B>> = Vec::new(); + let mut keys: Vec, B>> = Vec::new(); let pairs: usize = (((rank + 1) * rank) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKeyExec::alloc( + keys.push(GGLWESwitchingKeyPrepared::alloc( module, n, basek, k, rows, digits, 1, rank, )); }); @@ -30,32 +33,11 @@ impl GGLWETensorKeyExec, B> { Module: VmpPMatAllocBytes, { let pairs: usize = (((rank + 1) * rank) >> 1).max(1); - pairs * GGLWESwitchingKeyExec::bytes_of(module, n, basek, k, rows, digits, 1, rank) - } - - pub fn from( - module: &Module, - other: &GGLWETensorKey, - scratch: &mut Scratch, - ) -> GGLWETensorKeyExec, B> - where - Module: VmpPMatAlloc + VmpPMatPrepare, - { - let mut tsk_exec: GGLWETensorKeyExec, B> = Self::alloc( - module, - other.n(), - other.basek(), - other.k(), - other.rows(), - other.digits(), - other.rank(), - ); - tsk_exec.prepare(module, other, scratch); - tsk_exec + pairs * GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, 1, rank) } } -impl Infos for GGLWETensorKeyExec { +impl Infos for GGLWETensorKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -71,7 +53,7 @@ impl Infos for GGLWETensorKeyExec { } } -impl GGLWETensorKeyExec { +impl GGLWETensorKeyPrepared { pub fn rank(&self) -> usize { self.keys[0].rank() } @@ -89,9 +71,9 @@ impl GGLWETensorKeyExec { } } -impl GGLWETensorKeyExec { +impl GGLWETensorKeyPrepared { // 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 { + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -100,9 +82,9 @@ impl GGLWETensorKeyExec { } } -impl GGLWETensorKeyExec { +impl GGLWETensorKeyPrepared { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyExec { + pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -111,12 +93,11 @@ impl GGLWETensorKeyExec { } } -impl GGLWETensorKeyExec { - pub fn prepare(&mut self, module: &Module, other: &GGLWETensorKey, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl Prepare> for GGLWETensorKeyPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &GGLWETensorKey, scratch: &mut Scratch) { #[cfg(debug_assertions)] { assert_eq!(self.keys.len(), other.keys.len()); @@ -129,3 +110,22 @@ impl GGLWETensorKeyExec { }); } } + +impl PrepareAlloc, B>> for GGLWETensorKey +where + Module: VmpPMatAlloc + VmpPMatPrepare, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWETensorKeyPrepared, B> { + let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc( + module, + self.n(), + self.basek(), + self.k(), + self.rows(), + self.digits(), + self.rank(), + ); + tsk_prepared.prepare(module, self, scratch); + tsk_prepared + } +} diff --git a/core/src/layouts/prepared/ggsw_ct.rs b/core/src/layouts/prepared/ggsw_ct.rs index b954e1e..814145b 100644 --- a/core/src/layouts/prepared/ggsw_ct.rs +++ b/core/src/layouts/prepared/ggsw_ct.rs @@ -3,17 +3,20 @@ use backend::hal::{ layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, }; -use crate::layouts::{GGSWCiphertext, Infos}; +use crate::layouts::{ + GGSWCiphertext, Infos, + prepared::{Prepare, PrepareAlloc}, +}; #[derive(PartialEq, Eq)] -pub struct GGSWCiphertextExec { +pub struct GGSWCiphertextPrepared { pub(crate) data: VmpPMat, pub(crate) basek: usize, pub(crate) k: usize, pub(crate) digits: usize, } -impl GGSWCiphertextExec, B> { +impl GGSWCiphertextPrepared, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: VmpPMatAlloc, @@ -66,30 +69,9 @@ impl GGSWCiphertextExec, B> { module.vmp_pmat_alloc_bytes(n, rows, rank + 1, rank + 1, size) } - - pub fn from( - module: &Module, - other: &GGSWCiphertext, - scratch: &mut Scratch, - ) -> GGSWCiphertextExec, B> - where - Module: VmpPMatAlloc + VmpPMatPrepare, - { - let mut ggsw_exec: GGSWCiphertextExec, 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 Infos for GGSWCiphertextExec { +impl Infos for GGSWCiphertextPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -105,7 +87,7 @@ impl Infos for GGSWCiphertextExec { } } -impl GGSWCiphertextExec { +impl GGSWCiphertextPrepared { pub fn rank(&self) -> usize { self.data.cols_out() - 1 } @@ -115,15 +97,39 @@ impl GGSWCiphertextExec { } } -impl GGSWCiphertextExec { - pub fn prepare(&mut self, module: &Module, other: &GGSWCiphertext, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl GGSWCiphertextPrepared { + pub fn data(&self) -> &VmpPMat { + &self.data + } +} + +impl Prepare> for GGSWCiphertextPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &GGSWCiphertext, scratch: &mut Scratch) { module.vmp_prepare(&mut self.data, &other.data, scratch); self.k = other.k; self.basek = other.basek; self.digits = other.digits; } } + +impl PrepareAlloc, B>> for GGSWCiphertext +where + Module: VmpPMatAlloc + VmpPMatPrepare, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGSWCiphertextPrepared, B> { + let mut ggsw_prepared: GGSWCiphertextPrepared, B> = GGSWCiphertextPrepared::alloc( + module, + self.n(), + self.basek(), + self.k(), + self.rows(), + self.digits(), + self.rank(), + ); + ggsw_prepared.prepare(module, self, scratch); + ggsw_prepared + } +} diff --git a/core/src/layouts/prepared/glwe_pk.rs b/core/src/layouts/prepared/glwe_pk.rs index 9e8088c..9885edb 100644 --- a/core/src/layouts/prepared/glwe_pk.rs +++ b/core/src/layouts/prepared/glwe_pk.rs @@ -5,18 +5,21 @@ use backend::hal::{ use crate::{ dist::Distribution, - layouts::{GLWEPublicKey, Infos}, + layouts::{ + GLWEPublicKey, Infos, + prepared::{Prepare, PrepareAlloc}, + }, }; #[derive(PartialEq, Eq)] -pub struct GLWEPublicKeyExec { +pub struct GLWEPublicKeyPrepared { pub(crate) data: VecZnxDft, pub(crate) basek: usize, pub(crate) k: usize, pub(crate) dist: Distribution, } -impl Infos for GLWEPublicKeyExec { +impl Infos for GLWEPublicKeyPrepared { type Inner = VecZnxDft; fn inner(&self) -> &Self::Inner { @@ -32,13 +35,13 @@ impl Infos for GLWEPublicKeyExec { } } -impl GLWEPublicKeyExec { +impl GLWEPublicKeyPrepared { pub fn rank(&self) -> usize { self.cols() - 1 } } -impl GLWEPublicKeyExec, B> { +impl GLWEPublicKeyPrepared, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> Self where Module: VecZnxDftAlloc, @@ -57,25 +60,25 @@ impl GLWEPublicKeyExec, B> { { module.vec_znx_dft_alloc_bytes(n, rank + 1, k.div_ceil(basek)) } +} - pub fn from(module: &Module, other: &GLWEPublicKey, scratch: &mut Scratch) -> Self - where - DataOther: DataRef, - Module: VecZnxDftAlloc + VecZnxDftFromVecZnx, - { - let mut pk_exec: GLWEPublicKeyExec, B> = - GLWEPublicKeyExec::alloc(module, other.n(), other.basek(), other.k(), other.rank()); - pk_exec.prepare(module, other, scratch); - pk_exec +impl PrepareAlloc, B>> for GLWEPublicKey +where + Module: VecZnxDftAlloc + VecZnxDftFromVecZnx, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWEPublicKeyPrepared, B> { + let mut pk_prepared: GLWEPublicKeyPrepared, B> = + GLWEPublicKeyPrepared::alloc(module, self.n(), self.basek(), self.k(), self.rank()); + pk_prepared.prepare(module, self, scratch); + pk_prepared } } -impl GLWEPublicKeyExec { - pub fn prepare(&mut self, module: &Module, other: &GLWEPublicKey, _scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VecZnxDftFromVecZnx, - { +impl Prepare> for GLWEPublicKeyPrepared +where + Module: VecZnxDftFromVecZnx, +{ + fn prepare(&mut self, module: &Module, other: &GLWEPublicKey, _scratch: &mut Scratch) { #[cfg(debug_assertions)] { assert_eq!(self.n(), other.n()); diff --git a/core/src/layouts/prepared/glwe_sk.rs b/core/src/layouts/prepared/glwe_sk.rs index b495a92..97faa9b 100644 --- a/core/src/layouts/prepared/glwe_sk.rs +++ b/core/src/layouts/prepared/glwe_sk.rs @@ -3,17 +3,24 @@ use backend::hal::{ 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 { +pub struct GLWESecretPrepared { pub(crate) data: SvpPPol, pub(crate) dist: Distribution, } -impl GLWESecretExec, B> { +impl GLWESecretPrepared, B> { pub fn alloc(module: &Module, n: usize, rank: usize) -> Self where - Module: GLWESecretExecModuleFamily, + Module: GLWESecretPreparedModuleFamily, { Self { data: module.svp_ppol_alloc(n, rank), @@ -23,25 +30,13 @@ impl GLWESecretExec, B> { pub fn bytes_of(module: &Module, n: usize, rank: usize) -> usize where - Module: GLWESecretExecModuleFamily, + Module: GLWESecretPreparedModuleFamily, { module.svp_ppol_alloc_bytes(n, rank) } } -impl GLWESecretExec, B> { - pub fn from(module: &Module, sk: &GLWESecret) -> Self - where - D: DataRef, - Module: GLWESecretExecModuleFamily, - { - let mut sk_dft: GLWESecretExec, B> = Self::alloc(module, sk.n(), sk.rank()); - sk_dft.prepare(module, sk); - sk_dft - } -} - -impl GLWESecretExec { +impl GLWESecretPrepared { pub fn n(&self) -> usize { self.data.n() } @@ -55,15 +50,29 @@ impl GLWESecretExec { } } -impl GLWESecretExec { - pub(crate) fn prepare(&mut self, module: &Module, sk: &GLWESecret) - where - O: DataRef, - Module: GLWESecretExecModuleFamily, - { - (0..self.rank()).for_each(|i| { - module.svp_prepare(&mut self.data, i, &sk.data, i); - }); - self.dist = sk.dist +impl PrepareAlloc, B>> for GLWESecret +where + Module: SvpPrepare + SvpPPolAllocBytes + SvpPPolAlloc, +{ + fn prepare_alloc( + &self, + module: &Module, + scratch: &mut backend::hal::layouts::Scratch, + ) -> GLWESecretPrepared, B> { + let mut sk_dft: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, self.n(), self.rank()); + sk_dft.prepare(module, self, scratch); + sk_dft + } +} + +impl Prepare> for GLWESecretPrepared +where + Module: SvpPrepare, +{ + fn prepare(&mut self, module: &Module, other: &GLWESecret, _scratch: &mut backend::hal::layouts::Scratch) { + (0..self.rank()).for_each(|i| { + module.svp_prepare(&mut self.data, i, &other.data, i); + }); + self.dist = other.dist } } diff --git a/core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/core/src/layouts/prepared/glwe_to_lwe_ksk.rs index d1b7907..9ee9a1b 100644 --- a/core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -3,12 +3,15 @@ use backend::hal::{ 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)] -pub struct GLWEToLWESwitchingKeyExec(pub(crate) GGLWESwitchingKeyExec); +pub struct GLWEToLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); -impl Infos for GLWEToLWESwitchingKeyExec { +impl Infos for GLWEToLWESwitchingKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -24,7 +27,7 @@ impl Infos for GLWEToLWESwitchingKeyExec { } } -impl GLWEToLWESwitchingKeyExec { +impl GLWEToLWESwitchingKeyPrepared { pub fn digits(&self) -> usize { self.0.digits() } @@ -42,12 +45,12 @@ impl GLWEToLWESwitchingKeyExec { } } -impl GLWEToLWESwitchingKeyExec, B> { +impl GLWEToLWESwitchingKeyPrepared, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rows: usize, rank_in: usize) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyExec::alloc( + Self(GGLWESwitchingKeyPrepared::alloc( module, n, basek, k, rows, 1, rank_in, 1, )) } @@ -56,36 +59,33 @@ impl GLWEToLWESwitchingKeyExec, B> { where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyExec::, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1) - } - - pub fn from( - module: &Module, - other: &GLWEToLWESwitchingKey, - scratch: &mut Scratch, - ) -> Self - where - Module: VmpPMatAlloc + VmpPMatPrepare, - { - let mut ksk_exec: GLWEToLWESwitchingKeyExec, 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 + GGLWESwitchingKeyPrepared::, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1) } } -impl GLWEToLWESwitchingKeyExec { - pub fn prepare(&mut self, module: &Module, other: &GLWEToLWESwitchingKey, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl PrepareAlloc, B>> for GLWEToLWESwitchingKey +where + Module: VmpPMatPrepare + VmpPMatAlloc, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWEToLWESwitchingKeyPrepared, B> { + let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared, 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 Prepare> for GLWEToLWESwitchingKeyPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &GLWEToLWESwitchingKey, scratch: &mut Scratch) { self.0.prepare(module, &other.0, scratch); } } diff --git a/core/src/layouts/prepared/lwe_ksk.rs b/core/src/layouts/prepared/lwe_ksk.rs index ae6c14e..f3676e9 100644 --- a/core/src/layouts/prepared/lwe_ksk.rs +++ b/core/src/layouts/prepared/lwe_ksk.rs @@ -3,12 +3,15 @@ use backend::hal::{ 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)] -pub struct LWESwitchingKeyExec(pub(crate) GGLWESwitchingKeyExec); +pub struct LWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); -impl Infos for LWESwitchingKeyExec { +impl Infos for LWESwitchingKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -24,7 +27,7 @@ impl Infos for LWESwitchingKeyExec { } } -impl LWESwitchingKeyExec { +impl LWESwitchingKeyPrepared { pub fn digits(&self) -> usize { self.0.digits() } @@ -42,12 +45,12 @@ impl LWESwitchingKeyExec { } } -impl LWESwitchingKeyExec, B> { +impl LWESwitchingKeyPrepared, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rows: usize) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyExec::alloc( + Self(GGLWESwitchingKeyPrepared::alloc( module, n, basek, k, rows, 1, 1, 1, )) } @@ -56,31 +59,32 @@ impl LWESwitchingKeyExec, B> { where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyExec::, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1) - } - - pub fn from(module: &Module, other: &LWESwitchingKey, scratch: &mut Scratch) -> Self - where - Module: VmpPMatAlloc + VmpPMatPrepare, - { - let mut ksk_exec: LWESwitchingKeyExec, B> = Self::alloc( - module, - other.0.n(), - other.0.basek(), - other.0.k(), - other.0.rows(), - ); - ksk_exec.prepare(module, other, scratch); - ksk_exec + GGLWESwitchingKeyPrepared::, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1) } } -impl LWESwitchingKeyExec { - pub fn prepare(&mut self, module: &Module, other: &LWESwitchingKey, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl PrepareAlloc, B>> for LWESwitchingKey +where + Module: VmpPMatPrepare + VmpPMatAlloc, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> LWESwitchingKeyPrepared, B> { + let mut ksk_prepared: LWESwitchingKeyPrepared, 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 Prepare> for LWESwitchingKeyPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &LWESwitchingKey, scratch: &mut Scratch) { self.0.prepare(module, &other.0, scratch); } } diff --git a/core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/core/src/layouts/prepared/lwe_to_glwe_ksk.rs index e4c1d52..40a9356 100644 --- a/core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -3,13 +3,16 @@ use backend::hal::{ 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]. #[derive(PartialEq, Eq)] -pub struct LWEToGLWESwitchingKeyExec(pub(crate) GGLWESwitchingKeyExec); +pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); -impl Infos for LWEToGLWESwitchingKeyExec { +impl Infos for LWEToGLWESwitchingKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { @@ -25,7 +28,7 @@ impl Infos for LWEToGLWESwitchingKeyExec { } } -impl LWEToGLWESwitchingKeyExec { +impl LWEToGLWESwitchingKeyPrepared { pub fn digits(&self) -> usize { self.0.digits() } @@ -43,12 +46,12 @@ impl LWEToGLWESwitchingKeyExec { } } -impl LWEToGLWESwitchingKeyExec, B> { +impl LWEToGLWESwitchingKeyPrepared, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyExec::alloc( + Self(GGLWESwitchingKeyPrepared::alloc( module, n, basek, k, rows, 1, 1, rank_out, )) } @@ -57,36 +60,33 @@ impl LWEToGLWESwitchingKeyExec, B> { where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyExec::, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out) - } - - pub fn from( - module: &Module, - other: &LWEToGLWESwitchingKey, - scratch: &mut Scratch, - ) -> Self - where - Module: VmpPMatAlloc + VmpPMatPrepare, - { - let mut ksk_exec: LWEToGLWESwitchingKeyExec, 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 + GGLWESwitchingKeyPrepared::, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out) } } -impl LWEToGLWESwitchingKeyExec { - pub fn prepare(&mut self, module: &Module, other: &LWEToGLWESwitchingKey, scratch: &mut Scratch) - where - DataOther: DataRef, - Module: VmpPMatPrepare, - { +impl PrepareAlloc, B>> for LWEToGLWESwitchingKey +where + Module: VmpPMatPrepare + VmpPMatAlloc, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> LWEToGLWESwitchingKeyPrepared, B> { + let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared, 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 Prepare> for LWEToGLWESwitchingKeyPrepared +where + Module: VmpPMatPrepare, +{ + fn prepare(&mut self, module: &Module, other: &LWEToGLWESwitchingKey, scratch: &mut Scratch) { self.0.prepare(module, &other.0, scratch); } } diff --git a/core/src/layouts/prepared/mod.rs b/core/src/layouts/prepared/mod.rs index 296144a..454d5c7 100644 --- a/core/src/layouts/prepared/mod.rs +++ b/core/src/layouts/prepared/mod.rs @@ -9,6 +9,7 @@ mod glwe_to_lwe_ksk; mod lwe_ksk; mod lwe_to_glwe_ksk; +use backend::hal::layouts::{Backend, Module, Scratch}; pub use gglwe_atk::*; pub use gglwe_ct::*; pub use gglwe_ksk::*; @@ -19,3 +20,11 @@ pub use glwe_sk::*; pub use glwe_to_lwe_ksk::*; pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; + +pub trait PrepareAlloc { + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> T; +} + +pub trait Prepare { + fn prepare(&mut self, module: &Module, other: &T, scratch: &mut Scratch); +} diff --git a/core/src/lib.rs b/core/src/lib.rs index d06addc..53652cd 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -11,10 +11,12 @@ mod keyswitching; mod noise; mod operations; mod scratch; +mod utils; pub use operations::*; pub mod layouts; pub mod trait_families; +pub use dist::*; pub use glwe_packing::*; pub use scratch::*; diff --git a/core/src/noise/gglwe_ct.rs b/core/src/noise/gglwe_ct.rs index 41ab645..77b4122 100644 --- a/core/src/noise/gglwe_ct.rs +++ b/core/src/noise/gglwe_ct.rs @@ -1,11 +1,11 @@ use backend::hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxStd, VecZnxSubScalarInplace, ZnxZero}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxSubScalarInplace, ZnxZero}, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; use crate::{ - layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, + layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared}, trait_families::GLWEDecryptFamily, }; @@ -13,13 +13,13 @@ impl GGLWECiphertext { pub fn assert_noise( self, module: &Module, - sk: &GLWESecretExec, + sk: &GLWESecretPrepared, pt_want: &ScalarZnx, max_noise: f64, ) where DataSk: DataRef, DataWant: DataRef, - Module: GLWEDecryptFamily + VecZnxStd + VecZnxSubScalarInplace, + Module: GLWEDecryptFamily + VecZnxSubScalarInplace, B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let digits: usize = self.digits(); @@ -47,7 +47,7 @@ impl GGLWECiphertext { 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!( noise_have <= max_noise, diff --git a/core/src/noise/ggsw_ct.rs b/core/src/noise/ggsw_ct.rs index 34526f5..a47ea3c 100644 --- a/core/src/noise/ggsw_ct.rs +++ b/core/src/noise/ggsw_ct.rs @@ -1,14 +1,14 @@ use backend::hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxDftAlloc, - VecZnxDftToVecZnxBigTmpA, VecZnxNormalizeTmpBytes, VecZnxStd, VecZnxSubABInplace, ZnxZero, + VecZnxDftToVecZnxBigTmpA, VecZnxNormalizeTmpBytes, VecZnxSubABInplace, ZnxZero, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; use crate::{ - layouts::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec}, + layouts::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared}, trait_families::GGSWAssertNoiseFamily, }; @@ -16,13 +16,13 @@ impl GGSWCiphertext { pub fn assert_noise( &self, module: &Module, - sk_exec: &GLWESecretExec, + sk_prepared: &GLWESecretPrepared, pt_want: &ScalarZnx, max_noise: F, ) where DataSk: DataRef, DataScalar: DataRef, - Module: GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace + VecZnxStd, + Module: GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace, B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, F: Fn(usize) -> f64, { @@ -46,17 +46,17 @@ impl GGSWCiphertext { // mul with sk[col_j-1] if col_j > 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_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow()); } 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); - 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); println!("{} {}", std_pt, noise); assert!(std_pt <= noise, "{} > {}", std_pt, noise); @@ -71,12 +71,12 @@ impl GGSWCiphertext { pub fn print_noise( &self, module: &Module, - sk_exec: &GLWESecretExec, + sk_prepared: &GLWESecretPrepared, pt_want: &ScalarZnx, ) where DataSk: DataRef, DataScalar: DataRef, - Module: GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace + VecZnxStd, + Module: GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace, B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let basek: usize = self.basek(); @@ -99,17 +99,17 @@ impl GGSWCiphertext { // mul with sk[col_j-1] if col_j > 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_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow()); } 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); - 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); pt.data.zero(); }); diff --git a/core/src/noise/glwe_ct.rs b/core/src/noise/glwe_ct.rs index 8c97e06..2a5e9bf 100644 --- a/core/src/noise/glwe_ct.rs +++ b/core/src/noise/glwe_ct.rs @@ -1,12 +1,12 @@ use backend::hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxStd, VecZnxSubABInplace}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubABInplace}, layouts::{Backend, DataRef, Module, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; use crate::{ layouts::GLWEPlaintext, - layouts::prepared::GLWESecretExec, + layouts::prepared::GLWESecretPrepared, layouts::{GLWECiphertext, Infos}, trait_families::GLWEDecryptFamily, }; @@ -15,13 +15,13 @@ impl GLWECiphertext { pub fn assert_noise( &self, module: &Module, - sk_exec: &GLWESecretExec, + sk_prepared: &GLWESecretPrepared, pt_want: &GLWEPlaintext, max_noise: f64, ) where DataSk: DataRef, DataPt: DataRef, - Module: GLWEDecryptFamily + VecZnxSubABInplace + VecZnxNormalizeInplace + VecZnxStd, + Module: GLWEDecryptFamily + VecZnxSubABInplace + VecZnxNormalizeInplace, B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self.n(), self.basek(), self.k()); @@ -33,12 +33,12 @@ impl GLWECiphertext { 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_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); } } diff --git a/core/src/scratch.rs b/core/src/scratch.rs index 47da6fb..7ba0bd0 100644 --- a/core/src/scratch.rs +++ b/core/src/scratch.rs @@ -7,11 +7,11 @@ use backend::hal::{ use crate::{ dist::Distribution, layouts::{ - GGLWEAutomorphismKey, GGLWECiphertext, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWEPublicKey, - GLWESecret, GGLWESwitchingKey, Infos, + GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEPlaintext, + GLWEPublicKey, GLWESecret, Infos, prepared::{ - GGLWEAutomorphismKeyExec, GGLWECiphertextExec, GGSWCiphertextExec, GLWEPublicKeyExec, GLWESecretExec, - GGLWESwitchingKeyExec, GGLWETensorKeyExec, + GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, + GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, }, }, }; @@ -53,8 +53,8 @@ pub trait TakeGGLWE { ) -> (GGLWECiphertext<&mut [u8]>, &mut Self); } -pub trait TakeGGLWEExec { - fn take_gglwe_exec( +pub trait TakeGGLWEPrepared { + fn take_gglwe_prepared( &mut self, n: usize, basek: usize, @@ -63,7 +63,7 @@ pub trait TakeGGLWEExec { digits: usize, rank_in: usize, rank_out: usize, - ) -> (GGLWECiphertextExec<&mut [u8], B>, &mut Self); + ) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self); } pub trait TakeGGSW { @@ -78,8 +78,8 @@ pub trait TakeGGSW { ) -> (GGSWCiphertext<&mut [u8]>, &mut Self); } -pub trait TakeGGSWExec { - fn take_ggsw_exec( +pub trait TakeGGSWPrepared { + fn take_ggsw_prepared( &mut self, n: usize, basek: usize, @@ -87,29 +87,29 @@ pub trait TakeGGSWExec { rows: usize, digits: usize, rank: usize, - ) -> (GGSWCiphertextExec<&mut [u8], B>, &mut Self); + ) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self); } pub trait TakeGLWESecret { fn take_glwe_secret(&mut self, n: usize, rank: usize) -> (GLWESecret<&mut [u8]>, &mut Self); } -pub trait TakeGLWESecretExec { - fn take_glwe_secret_exec(&mut self, n: usize, rank: usize) -> (GLWESecretExec<&mut [u8], B>, &mut Self); +pub trait TakeGLWESecretPrepared { + fn take_glwe_secret_prepared(&mut self, n: usize, rank: usize) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self); } pub trait TakeGLWEPk { fn take_glwe_pk(&mut self, n: usize, basek: usize, k: usize, rank: usize) -> (GLWEPublicKey<&mut [u8]>, &mut Self); } -pub trait TakeGLWEPkExec { - fn take_glwe_pk_exec( +pub trait TakeGLWEPkPrepared { + fn take_glwe_pk_prepared( &mut self, n: usize, basek: usize, k: usize, rank: usize, - ) -> (GLWEPublicKeyExec<&mut [u8], B>, &mut Self); + ) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self); } pub trait TakeGLWESwitchingKey { @@ -125,8 +125,8 @@ pub trait TakeGLWESwitchingKey { ) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self); } -pub trait TakeGLWESwitchingKeyExec { - fn take_glwe_switching_key_exec( +pub trait TakeGLWESwitchingKeyPrepared { + fn take_glwe_switching_key_prepared( &mut self, n: usize, basek: usize, @@ -135,7 +135,7 @@ pub trait TakeGLWESwitchingKeyExec { digits: usize, rank_in: usize, rank_out: usize, - ) -> (GGLWESwitchingKeyExec<&mut [u8], B>, &mut Self); + ) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self); } pub trait TakeTensorKey { @@ -150,8 +150,8 @@ pub trait TakeTensorKey { ) -> (GGLWETensorKey<&mut [u8]>, &mut Self); } -pub trait TakeTensorKeyExec { - fn take_tensor_key_exec( +pub trait TakeTensorKeyPrepared { + fn take_tensor_key_prepared( &mut self, n: usize, basek: usize, @@ -159,7 +159,7 @@ pub trait TakeTensorKeyExec { rows: usize, digits: usize, rank: usize, - ) -> (GGLWETensorKeyExec<&mut [u8], B>, &mut Self); + ) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self); } pub trait TakeAutomorphismKey { @@ -174,8 +174,8 @@ pub trait TakeAutomorphismKey { ) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self); } -pub trait TakeAutomorphismKeyExec { - fn take_automorphism_key_exec( +pub trait TakeAutomorphismKeyPrepared { + fn take_automorphism_key_prepared( &mut self, n: usize, basek: usize, @@ -183,7 +183,7 @@ pub trait TakeAutomorphismKeyExec { rows: usize, digits: usize, rank: usize, - ) -> (GGLWEAutomorphismKeyExec<&mut [u8], B>, &mut Self); + ) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self); } impl TakeGLWECt for Scratch @@ -330,11 +330,11 @@ where } } -impl TakeGGLWEExec for Scratch +impl TakeGGLWEPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_gglwe_exec( + fn take_gglwe_prepared( &mut self, n: usize, basek: usize, @@ -343,7 +343,7 @@ where digits: usize, rank_in: usize, rank_out: usize, - ) -> (GGLWECiphertextExec<&mut [u8], B>, &mut Self) { + ) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) { let (data, scratch) = self.take_vmp_pmat( n, rows.div_ceil(digits), @@ -352,7 +352,7 @@ where k.div_ceil(basek), ); ( - GGLWECiphertextExec { + GGLWECiphertextPrepared { data: data, basek: basek, k, @@ -363,14 +363,14 @@ where } } -impl<'a, B, D> TakeLike<'a, B, GGLWECiphertextExec> for Scratch +impl<'a, B, D> TakeLike<'a, B, GGLWECiphertextPrepared> for Scratch where B: Backend + TakeVmpPMatImpl, D: DataRef, { - type Output = GGLWECiphertextExec<&'a mut [u8], B>; + type Output = GGLWECiphertextPrepared<&'a mut [u8], B>; - fn take_like(&'a mut self, template: &GGLWECiphertextExec) -> (Self::Output, &'a mut Self) { + fn take_like(&'a mut self, template: &GGLWECiphertextPrepared) -> (Self::Output, &'a mut Self) { let (data, scratch) = B::take_vmp_pmat_impl( self, template.n(), @@ -380,7 +380,7 @@ where template.size(), ); ( - GGLWECiphertextExec { + GGLWECiphertextPrepared { data, basek: template.basek(), k: template.k(), @@ -451,11 +451,11 @@ where } } -impl TakeGGSWExec for Scratch +impl TakeGGSWPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_ggsw_exec( + fn take_ggsw_prepared( &mut self, n: usize, basek: usize, @@ -463,7 +463,7 @@ where rows: usize, digits: usize, rank: usize, - ) -> (GGSWCiphertextExec<&mut [u8], B>, &mut Self) { + ) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) { let (data, scratch) = self.take_vmp_pmat( n, rows.div_ceil(digits), @@ -472,7 +472,7 @@ where k.div_ceil(basek), ); ( - GGSWCiphertextExec { + GGSWCiphertextPrepared { data, basek, k, @@ -483,14 +483,14 @@ where } } -impl<'a, B, D> TakeLike<'a, B, GGSWCiphertextExec> for Scratch +impl<'a, B, D> TakeLike<'a, B, GGSWCiphertextPrepared> for Scratch where B: Backend + TakeVmpPMatImpl, D: DataRef, { - type Output = GGSWCiphertextExec<&'a mut [u8], B>; + type Output = GGSWCiphertextPrepared<&'a mut [u8], B>; - fn take_like(&'a mut self, template: &GGSWCiphertextExec) -> (Self::Output, &'a mut Self) { + fn take_like(&'a mut self, template: &GGSWCiphertextPrepared) -> (Self::Output, &'a mut Self) { let (data, scratch) = B::take_vmp_pmat_impl( self, template.n(), @@ -500,7 +500,7 @@ where template.size(), ); ( - GGSWCiphertextExec { + GGSWCiphertextPrepared { data, basek: template.basek(), k: template.k(), @@ -550,20 +550,20 @@ where } } -impl TakeGLWEPkExec for Scratch +impl TakeGLWEPkPrepared for Scratch where Scratch: TakeVecZnxDft, { - fn take_glwe_pk_exec( + fn take_glwe_pk_prepared( &mut self, n: usize, basek: usize, k: 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)); ( - GLWEPublicKeyExec { + GLWEPublicKeyPrepared { data, k, basek, @@ -574,17 +574,17 @@ where } } -impl<'a, B, D> TakeLike<'a, B, GLWEPublicKeyExec> for Scratch +impl<'a, B, D> TakeLike<'a, B, GLWEPublicKeyPrepared> for Scratch where B: Backend + TakeVecZnxDftImpl, D: DataRef, { - type Output = GLWEPublicKeyExec<&'a mut [u8], B>; + type Output = GLWEPublicKeyPrepared<&'a mut [u8], B>; - fn take_like(&'a mut self, template: &GLWEPublicKeyExec) -> (Self::Output, &'a mut Self) { + fn take_like(&'a mut self, template: &GLWEPublicKeyPrepared) -> (Self::Output, &'a mut Self) { let (data, scratch) = B::take_vec_znx_dft_impl(self, template.n(), template.cols(), template.size()); ( - GLWEPublicKeyExec { + GLWEPublicKeyPrepared { data, basek: template.basek(), k: template.k(), @@ -630,14 +630,14 @@ where } } -impl TakeGLWESecretExec for Scratch +impl TakeGLWESecretPrepared for Scratch where Scratch: TakeSvpPPol, { - 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); ( - GLWESecretExec { + GLWESecretPrepared { data, dist: Distribution::NONE, }, @@ -646,17 +646,17 @@ where } } -impl<'a, B, D> TakeLike<'a, B, GLWESecretExec> for Scratch +impl<'a, B, D> TakeLike<'a, B, GLWESecretPrepared> for Scratch where B: Backend + TakeSvpPPolImpl, D: DataRef, { - type Output = GLWESecretExec<&'a mut [u8], B>; + type Output = GLWESecretPrepared<&'a mut [u8], B>; - fn take_like(&'a mut self, template: &GLWESecretExec) -> (Self::Output, &'a mut Self) { + fn take_like(&'a mut self, template: &GLWESecretPrepared) -> (Self::Output, &'a mut Self) { let (data, scratch) = B::take_svp_ppol_impl(self, template.n(), template.rank()); ( - GLWESecretExec { + GLWESecretPrepared { data, dist: template.dist, }, @@ -712,11 +712,11 @@ where } } -impl TakeGLWESwitchingKeyExec for Scratch +impl TakeGLWESwitchingKeyPrepared for Scratch where - Scratch: TakeGGLWEExec, + Scratch: TakeGGLWEPrepared, { - fn take_glwe_switching_key_exec( + fn take_glwe_switching_key_prepared( &mut self, n: usize, basek: usize, @@ -725,10 +725,10 @@ where digits: usize, rank_in: usize, rank_out: usize, - ) -> (GGLWESwitchingKeyExec<&mut [u8], B>, &mut Self) { - let (data, scratch) = self.take_gglwe_exec(n, basek, k, rows, digits, rank_in, rank_out); + ) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) { + let (data, scratch) = self.take_gglwe_prepared(n, basek, k, rows, digits, rank_in, rank_out); ( - GGLWESwitchingKeyExec { + GGLWESwitchingKeyPrepared { key: data, sk_in_n: 0, sk_out_n: 0, @@ -738,18 +738,18 @@ where } } -impl<'a, B, D> TakeLike<'a, B, GGLWESwitchingKeyExec> for Scratch +impl<'a, B, D> TakeLike<'a, B, GGLWESwitchingKeyPrepared> for Scratch where - Scratch: TakeLike<'a, B, GGLWECiphertextExec, Output = GGLWECiphertextExec<&'a mut [u8], B>>, + Scratch: TakeLike<'a, B, GGLWECiphertextPrepared, Output = GGLWECiphertextPrepared<&'a mut [u8], B>>, B: Backend + TakeMatZnxImpl, D: DataRef, { - type Output = GGLWESwitchingKeyExec<&'a mut [u8], B>; + type Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>; - fn take_like(&'a mut self, template: &GGLWESwitchingKeyExec) -> (Self::Output, &'a mut Self) { + fn take_like(&'a mut self, template: &GGLWESwitchingKeyPrepared) -> (Self::Output, &'a mut Self) { let (key, scratch) = self.take_like(&template.key); ( - GGLWESwitchingKeyExec { + GGLWESwitchingKeyPrepared { key, sk_in_n: template.sk_in_n, sk_out_n: template.sk_out_n, @@ -791,11 +791,11 @@ where } } -impl TakeAutomorphismKeyExec for Scratch +impl TakeAutomorphismKeyPrepared for Scratch where - Scratch: TakeGLWESwitchingKeyExec, + Scratch: TakeGLWESwitchingKeyPrepared, { - fn take_automorphism_key_exec( + fn take_automorphism_key_prepared( &mut self, n: usize, basek: usize, @@ -803,23 +803,23 @@ where rows: usize, digits: usize, rank: usize, - ) -> (GGLWEAutomorphismKeyExec<&mut [u8], B>, &mut Self) { - let (data, scratch) = self.take_glwe_switching_key_exec(n, basek, k, rows, digits, rank, rank); - (GGLWEAutomorphismKeyExec { key: data, p: 0 }, scratch) + ) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) { + let (data, scratch) = self.take_glwe_switching_key_prepared(n, basek, k, rows, digits, rank, rank); + (GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) } } -impl<'a, B, D> TakeLike<'a, B, GGLWEAutomorphismKeyExec> for Scratch +impl<'a, B, D> TakeLike<'a, B, GGLWEAutomorphismKeyPrepared> for Scratch where - Scratch: TakeLike<'a, B, GGLWESwitchingKeyExec, Output = GGLWESwitchingKeyExec<&'a mut [u8], B>>, + Scratch: TakeLike<'a, B, GGLWESwitchingKeyPrepared, Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>>, B: Backend + TakeMatZnxImpl, D: DataRef, { - type Output = GGLWEAutomorphismKeyExec<&'a mut [u8], B>; + type Output = GGLWEAutomorphismKeyPrepared<&'a mut [u8], B>; - fn take_like(&'a mut self, template: &GGLWEAutomorphismKeyExec) -> (Self::Output, &'a mut Self) { + fn take_like(&'a mut self, template: &GGLWEAutomorphismKeyPrepared) -> (Self::Output, &'a mut Self) { 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 TakeTensorKeyExec for Scratch +impl TakeTensorKeyPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_tensor_key_exec( + fn take_tensor_key_prepared( &mut self, n: usize, basek: usize, @@ -896,36 +896,36 @@ where rows: usize, digits: usize, rank: usize, - ) -> (GGLWETensorKeyExec<&mut [u8], B>, &mut Self) { - let mut keys: Vec> = Vec::new(); + ) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) { + let mut keys: Vec> = Vec::new(); let pairs: usize = (((rank + 1) * rank) >> 1).max(1); let mut scratch: &mut Scratch = self; 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; keys.push(gglwe); } 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; keys.push(gglwe); } - (GGLWETensorKeyExec { keys }, scratch) + (GGLWETensorKeyPrepared { keys }, scratch) } } -impl<'a, B, D> TakeLike<'a, B, GGLWETensorKeyExec> for Scratch +impl<'a, B, D> TakeLike<'a, B, GGLWETensorKeyPrepared> for Scratch where - Scratch: TakeLike<'a, B, GGLWESwitchingKeyExec, Output = GGLWESwitchingKeyExec<&'a mut [u8], B>>, + Scratch: TakeLike<'a, B, GGLWESwitchingKeyPrepared, Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>>, B: Backend + TakeMatZnxImpl, D: DataRef, { - type Output = GGLWETensorKeyExec<&'a mut [u8], B>; + type Output = GGLWETensorKeyPrepared<&'a mut [u8], B>; - fn take_like(&'a mut self, template: &GGLWETensorKeyExec) -> (Self::Output, &'a mut Self) { - let mut keys: Vec> = Vec::new(); + fn take_like(&'a mut self, template: &GGLWETensorKeyPrepared) -> (Self::Output, &'a mut Self) { + let mut keys: Vec> = Vec::new(); let pairs: usize = template.keys.len(); let mut scratch: &mut Scratch = self; @@ -941,6 +941,6 @@ where keys.push(gglwe); } - (GGLWETensorKeyExec { keys }, scratch) + (GGLWETensorKeyPrepared { keys }, scratch) } } diff --git a/core/src/tests/generics/automorphism/gglwe_atk.rs b/core/src/tests/generics/automorphism/gglwe_atk.rs index 7ebde3f..fb08745 100644 --- a/core/src/tests/generics/automorphism/gglwe_atk.rs +++ b/core/src/tests/generics/automorphism/gglwe_atk.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy, - VecZnxStd, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -14,13 +14,13 @@ use sampling::source::Source; use crate::{ layouts::{ GGLWEAutomorphismKey, GLWEPlaintext, GLWESecret, Infos, - prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec}, + prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, }; -use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub fn test_gglwe_automorphism_key_automorphism( module: &Module, @@ -35,7 +35,7 @@ pub fn test_gglwe_automorphism_key_automorphism( rank: usize, ) where Module: GGLWEAutomorphismKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxSwithcDegree @@ -44,7 +44,6 @@ pub fn test_gglwe_automorphism_key_automorphism( + VecZnxAutomorphismInplace + GLWEDecryptFamily + VecZnxSubScalarInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare, @@ -102,13 +101,18 @@ pub fn test_gglwe_automorphism_key_automorphism( scratch.borrow(), ); - let mut auto_key_apply_exec: GGLWEAutomorphismKeyExec, B> = - GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank); + let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = + 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) - 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> = GLWEPlaintext::alloc(n, basek, k_out); @@ -124,7 +128,7 @@ pub fn test_gglwe_automorphism_key_automorphism( ); }); - let sk_auto_dft: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_auto); + let sk_auto_dft: GLWESecretPrepared, B> = sk_auto.prepare_alloc(module, scratch.borrow()); (0..auto_key_out.rank_in()).for_each(|col_i| { (0..auto_key_out.rows()).for_each(|row_i| { @@ -140,7 +144,7 @@ pub fn test_gglwe_automorphism_key_automorphism( 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( n as f64, basek * digits, @@ -176,7 +180,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace( rank: usize, ) where Module: GGLWEAutomorphismKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxSwithcDegree @@ -185,7 +189,6 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace( + VecZnxAutomorphismInplace + GLWEDecryptFamily + VecZnxSubScalarInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare, @@ -242,13 +245,13 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace( scratch.borrow(), ); - let mut auto_key_apply_exec: GGLWEAutomorphismKeyExec, B> = - GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank); + let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = + 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) - 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> = GLWEPlaintext::alloc(n, basek, k_in); @@ -265,7 +268,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace( ); }); - let sk_auto_dft: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_auto); + let sk_auto_dft: GLWESecretPrepared, B> = sk_auto.prepare_alloc(module, scratch.borrow()); (0..auto_key.rank_in()).for_each(|col_i| { (0..auto_key.rows()).for_each(|row_i| { @@ -280,7 +283,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace( 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( n as f64, basek * digits, diff --git a/core/src/tests/generics/automorphism/ggsw_ct.rs b/core/src/tests/generics/automorphism/ggsw_ct.rs index c1e3913..61967f6 100644 --- a/core/src/tests/generics/automorphism/ggsw_ct.rs +++ b/core/src/tests/generics/automorphism/ggsw_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy, - VecZnxStd, VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScalarZnx, ScratchOwned}, oep::{ @@ -14,14 +14,14 @@ use sampling::source::Source; use crate::{ layouts::{ GGLWEAutomorphismKey, GGLWETensorKey, GGSWCiphertext, GLWESecret, - prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec, GGLWETensorKeyExec}, + prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::noise_ggsw_keyswitch, trait_families::GGSWAssertNoiseFamily, }; use crate::trait_families::{ - GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWKeySwitchFamily, GLWESecretExecModuleFamily, + GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWKeySwitchFamily, GLWESecretPreparedModuleFamily, }; pub fn test_ggsw_automorphism( @@ -37,10 +37,9 @@ pub fn test_ggsw_automorphism( sigma: f64, ) where Module: GGSWAssertNoiseFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + VecZnxAddScalarInplace + VecZnxCopy - + VecZnxStd + VecZnxSubABInplace + VmpPMatAlloc + VmpPMatPrepare @@ -92,7 +91,7 @@ pub fn test_ggsw_automorphism( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(var_xs, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); auto_key.encrypt_sk( module, @@ -117,21 +116,28 @@ pub fn test_ggsw_automorphism( ct_in.encrypt_sk( module, &pt_scalar, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut auto_key_exec: GGLWEAutomorphismKeyExec, B> = - GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); - auto_key_exec.prepare(module, &auto_key, scratch.borrow()); + let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = + GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_exec: GGLWETensorKeyExec, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_tsk, rows, digits, rank); - tsk_exec.prepare(module, &tensor_key, scratch.borrow()); + let mut tsk_prepared: GGLWETensorKeyPrepared, B> = + 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); @@ -151,7 +157,7 @@ pub fn test_ggsw_automorphism( ) + 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( @@ -166,10 +172,9 @@ pub fn test_ggsw_automorphism_inplace( sigma: f64, ) where Module: GGSWAssertNoiseFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + VecZnxAddScalarInplace + VecZnxCopy - + VecZnxStd + VecZnxSubABInplace + VmpPMatAlloc + VmpPMatPrepare @@ -217,7 +222,7 @@ pub fn test_ggsw_automorphism_inplace( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(var_xs, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); auto_key.encrypt_sk( module, @@ -242,21 +247,22 @@ pub fn test_ggsw_automorphism_inplace( ct.encrypt_sk( module, &pt_scalar, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut auto_key_exec: GGLWEAutomorphismKeyExec, B> = - GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); - auto_key_exec.prepare(module, &auto_key, scratch.borrow()); + let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = + GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_exec: GGLWETensorKeyExec, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_tsk, rows, digits, rank); - tsk_exec.prepare(module, &tensor_key, scratch.borrow()); + let mut tsk_prepared: GGLWETensorKeyPrepared, B> = + 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); @@ -276,5 +282,5 @@ pub fn test_ggsw_automorphism_inplace( ) + 0.5 }; - ct.assert_noise(module, &sk_exec, &pt_scalar, &max_noise); + ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise); } diff --git a/core/src/tests/generics/automorphism/glwe_ct.rs b/core/src/tests/generics/automorphism/glwe_ct.rs index edf5f3c..0977171 100644 --- a/core/src/tests/generics/automorphism/glwe_ct.rs +++ b/core/src/tests/generics/automorphism/glwe_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxFillUniform, VecZnxStd, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + VecZnxFillUniform, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -14,13 +14,13 @@ use sampling::source::Source; use crate::{ layouts::{ GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, - prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec}, + prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, }; -use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub fn test_glwe_automorphism( module: &Module, @@ -34,14 +34,13 @@ pub fn test_glwe_automorphism( sigma: f64, ) where Module: GGLWEAutomorphismKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxSwithcDegree + VecZnxAddScalarInplace + VecZnxAutomorphismInplace - + VecZnxStd + VmpPMatAlloc + VmpPMatPrepare, B: TakeVecZnxDftImpl @@ -85,7 +84,7 @@ pub fn test_glwe_automorphism( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); autokey.encrypt_sk( module, @@ -100,18 +99,18 @@ pub fn test_glwe_automorphism( ct_in.encrypt_sk( module, &pt_want, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut autokey_exec: GGLWEAutomorphismKeyExec, B> = - GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); - autokey_exec.prepare(module, &autokey, scratch.borrow()); + let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = + GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + 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( module.n() as f64, @@ -128,7 +127,7 @@ pub fn test_glwe_automorphism( 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( @@ -142,14 +141,13 @@ pub fn test_glwe_automorphism_inplace( sigma: f64, ) where Module: GGLWEAutomorphismKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxSwithcDegree + VecZnxAddScalarInplace + VecZnxAutomorphismInplace - + VecZnxStd + VmpPMatAlloc + VmpPMatPrepare, B: TakeVecZnxDftImpl @@ -183,7 +181,7 @@ pub fn test_glwe_automorphism_inplace( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); autokey.encrypt_sk( module, @@ -198,18 +196,18 @@ pub fn test_glwe_automorphism_inplace( ct.encrypt_sk( module, &pt_want, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut autokey_exec: GGLWEAutomorphismKeyExec, B> = - GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); - autokey_exec.prepare(module, &autokey, scratch.borrow()); + let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = + GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + 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( module.n() as f64, @@ -226,5 +224,5 @@ pub fn test_glwe_automorphism_inplace( 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); } diff --git a/core/src/tests/generics/conversion.rs b/core/src/tests/generics/conversion.rs index 6547cc1..a593805 100644 --- a/core/src/tests/generics/conversion.rs +++ b/core/src/tests/generics/conversion.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxEncodeCoeffsi64, - VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxSwithcDegree, + VmpPMatAlloc, VmpPMatPrepare, ZnxView, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -14,10 +14,10 @@ use sampling::source::Source; use crate::layouts::{ GLWECiphertext, GLWEPlaintext, GLWESecret, GLWEToLWESwitchingKey, Infos, LWECiphertext, LWEPlaintext, LWESecret, 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(module: &Module) where @@ -26,11 +26,10 @@ where + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWEKeyswitchFamily - + VecZnxEncodeCoeffsi64 + VecZnxAutomorphismInplace + VmpPMatAlloc + VmpPMatPrepare - + GLWESecretExecModuleFamily, + + GLWESecretPreparedModuleFamily, B: TakeScalarZnxImpl + TakeVecZnxDftImpl + ScratchAvailableImpl @@ -67,7 +66,7 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_glwe); + let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs); @@ -75,7 +74,7 @@ where let data: i64 = 17; let mut lwe_pt: LWEPlaintext> = 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> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct); lwe_ct.encrypt_sk( @@ -101,12 +100,12 @@ where let mut glwe_ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank); - let ksk_exec: LWEToGLWESwitchingKeyExec, B> = LWEToGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); + let ksk_prepared: LWEToGLWESwitchingKeyPrepared, 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> = 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]); } @@ -118,11 +117,10 @@ where + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWEKeyswitchFamily - + VecZnxEncodeCoeffsi64 + VecZnxAutomorphismInplace + VmpPMatAlloc + VmpPMatPrepare - + GLWESecretExecModuleFamily, + + GLWESecretPreparedModuleFamily, B: TakeScalarZnxImpl + TakeVecZnxDftImpl + ScratchAvailableImpl @@ -159,20 +157,20 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_glwe); + let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let mut sk_lwe = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs); let data: i64 = 17; let mut glwe_pt: GLWEPlaintext> = 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); glwe_ct.encrypt_sk( module, &glwe_pt, - &sk_glwe_exec, + &sk_glwe_prepared, &mut source_xa, &mut source_xe, sigma, @@ -193,9 +191,9 @@ where let mut lwe_ct: LWECiphertext> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct); - let ksk_exec: GLWEToLWESwitchingKeyExec, B> = GLWEToLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); + let ksk_prepared: GLWEToLWESwitchingKeyPrepared, 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> = LWEPlaintext::alloc(basek, k_lwe_ct); lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe); diff --git a/core/src/tests/generics/encryption/gglwe_atk.rs b/core/src/tests/generics/encryption/gglwe_atk.rs index 3564b5e..72fb304 100644 --- a/core/src/tests/generics/encryption/gglwe_atk.rs +++ b/core/src/tests/generics/encryption/gglwe_atk.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy, - VecZnxStd, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -12,11 +12,15 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - layouts::{GGLWEAutomorphismKey, GLWESecret, compressed::GGLWEAutomorphismKeyCompressed, prepared::GLWESecretExec}, + layouts::{ + GGLWEAutomorphismKey, GLWESecret, + compressed::GGLWEAutomorphismKeyCompressed, + prepared::{GLWESecretPrepared, PrepareAlloc}, + }, 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( module: &Module, @@ -27,7 +31,7 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk( sigma: f64, ) where Module: GGLWEAutomorphismKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxSwithcDegree @@ -36,7 +40,6 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk( + VecZnxAutomorphismInplace + GLWEDecryptFamily + VecZnxSubScalarInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare, @@ -87,11 +90,11 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk( i, ); }); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); atk.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( @@ -103,7 +106,7 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk( sigma: f64, ) where Module: GGLWEAutomorphismKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEKeyswitchFamily + VecZnxAutomorphism + VecZnxSwithcDegree @@ -112,7 +115,6 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk( + VecZnxAutomorphismInplace + GLWEDecryptFamily + VecZnxSubScalarInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare, @@ -165,12 +167,12 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk( 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> = GGLWEAutomorphismKey::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); + .assert_noise(module, &sk_out_prepared, &sk.data, sigma); } diff --git a/core/src/tests/generics/encryption/gglwe_ct.rs b/core/src/tests/generics/encryption/gglwe_ct.rs index 169a323..855eb56 100644 --- a/core/src/tests/generics/encryption/gglwe_ct.rs +++ b/core/src/tests/generics/encryption/gglwe_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubScalarInplace, - VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubScalarInplace, VecZnxSwithcDegree, + VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -12,11 +12,15 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - layouts::{GLWESecret, GGLWESwitchingKey, compressed::GGLWESwitchingKeyCompressed, prepared::GLWESecretExec}, + layouts::{ + GGLWESwitchingKey, GLWESecret, + compressed::GGLWESwitchingKeyCompressed, + prepared::{GLWESecretPrepared, PrepareAlloc}, + }, trait_families::{Decompress, GLWEDecryptFamily}, }; -use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub fn test_gglwe_switching_key_encrypt_sk( module: &Module, @@ -28,11 +32,10 @@ pub fn test_gglwe_switching_key_encrypt_sk( sigma: f64, ) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VecZnxCopy + VmpPMatAlloc @@ -67,7 +70,7 @@ pub fn test_gglwe_switching_key_encrypt_sk( let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank_out); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); ksk.encrypt_sk( module, @@ -80,7 +83,7 @@ pub fn test_gglwe_switching_key_encrypt_sk( ); 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( @@ -93,11 +96,10 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk( sigma: f64, ) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VecZnxCopy + VmpPMatAlloc @@ -132,7 +134,7 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk( let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank_out); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); let seed_xa = [1u8; 32]; @@ -150,5 +152,5 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk( ksk.decompress(module, &ksk_compressed); ksk.key - .assert_noise(module, &sk_out_exec, &sk_in.data, sigma); + .assert_noise(module, &sk_out_prepared, &sk_in.data, sigma); } diff --git a/core/src/tests/generics/encryption/ggsw_ct.rs b/core/src/tests/generics/encryption/ggsw_ct.rs index fb16a03..d2c5e6f 100644 --- a/core/src/tests/generics/encryption/ggsw_ct.rs +++ b/core/src/tests/generics/encryption/ggsw_ct.rs @@ -1,6 +1,6 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubABInplace, VmpPMatAlloc, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubABInplace, VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScalarZnx, ScratchOwned}, @@ -12,20 +12,23 @@ use backend::hal::{ use sampling::source::Source; use crate::{ - layouts::{GGSWCiphertext, GLWESecret, compressed::GGSWCiphertextCompressed, prepared::GLWESecretExec}, + layouts::{ + GGSWCiphertext, GLWESecret, + compressed::GGSWCiphertextCompressed, + prepared::{GLWESecretPrepared, PrepareAlloc}, + }, trait_families::{Decompress, GGSWAssertNoiseFamily}, }; -use crate::trait_families::{GGSWEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGSWEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub fn test_ggsw_encrypt_sk(module: &Module, basek: usize, k: usize, digits: usize, rank: usize, sigma: f64) where - Module: GLWESecretExecModuleFamily + Module: GLWESecretPreparedModuleFamily + GGSWEncryptSkFamily + GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare, @@ -60,13 +63,12 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); - sk_exec.prepare(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); ct.encrypt_sk( module, &pt_scalar, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, @@ -75,7 +77,7 @@ where 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( @@ -86,12 +88,11 @@ pub fn test_ggsw_compressed_encrypt_sk( rank: usize, sigma: f64, ) where - Module: GLWESecretExecModuleFamily + Module: GLWESecretPreparedModuleFamily + GGSWEncryptSkFamily + GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare, @@ -125,15 +126,14 @@ pub fn test_ggsw_compressed_encrypt_sk( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); - sk_exec.prepare(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); let seed_xa: [u8; 32] = [1u8; 32]; ct_compressed.encrypt_sk( module, &pt_scalar, - &sk_exec, + &sk_prepared, seed_xa, &mut source_xe, sigma, @@ -145,5 +145,5 @@ pub fn test_ggsw_compressed_encrypt_sk( let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(n, basek, k, rows, digits, rank); 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); } diff --git a/core/src/tests/generics/encryption/glwe_ct.rs b/core/src/tests/generics/encryption/glwe_ct.rs index bd15a9f..9aeebe9 100644 --- a/core/src/tests/generics/encryption/glwe_ct.rs +++ b/core/src/tests/generics/encryption/glwe_ct.rs @@ -1,5 +1,5 @@ use backend::hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy, VecZnxDftAlloc, VecZnxFillUniform, VecZnxStd, VecZnxSubABInplace}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy, VecZnxDftAlloc, VecZnxFillUniform, VecZnxSubABInplace}, layouts::{Backend, Module, ScratchOwned}, oep::{ ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, @@ -12,16 +12,16 @@ use crate::{ layouts::{ GLWECiphertext, GLWEPlaintext, GLWEPublicKey, GLWESecret, Infos, compressed::GLWECiphertextCompressed, - prepared::{GLWEPublicKeyExec, GLWESecretExec}, + prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, operations::GLWEOperations, trait_families::Decompress, }; -use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptPkFamily, GLWEEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptPkFamily, GLWEEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub trait EncryptionTestModuleFamily = - GLWEDecryptFamily + VecZnxStd + GLWESecretExecModuleFamily + GLWEEncryptPkFamily; + GLWEDecryptFamily + GLWESecretPreparedModuleFamily + GLWEEncryptPkFamily; pub fn test_glwe_encrypt_sk(module: &Module, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) where @@ -51,25 +51,25 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa); ct.encrypt_sk( module, &pt_want, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, 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); - 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; assert!(noise_have <= noise_want + 0.2); @@ -110,7 +110,7 @@ pub fn test_glwe_compressed_encrypt_sk( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); 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( ct_compressed.encrypt_sk( module, &pt_want, - &sk_exec, + &sk_prepared, seed_xa, &mut source_xe, sigma, @@ -129,11 +129,11 @@ pub fn test_glwe_compressed_encrypt_sk( let mut ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_ct, rank); 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); - 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; assert!( @@ -163,28 +163,28 @@ where let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); - - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_ct, rank); - let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct) | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct), ); + let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); + sk.fill_ternary_prob(0.5, &mut source_xs); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_ct, rank); + ct.encrypt_zero_sk( module, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, 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(module: &Module, 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_xu: Source = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); - - let mut pk: GLWEPublicKey> = 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 = ScratchOwned::alloc( GLWECiphertext::encrypt_sk_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> = GLWESecret::alloc(n, rank); + sk.fill_ternary_prob(0.5, &mut source_xs); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut pk: GLWEPublicKey> = 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); - let pk_exec: GLWEPublicKeyExec, B> = GLWEPublicKeyExec::from(module, &pk, scratch.borrow()); + let pk_prepared: GLWEPublicKeyPrepared, B> = pk.prepare_alloc(module, scratch.borrow()); ct.encrypt_pk( module, &pt_want, - &pk_exec, + &pk_prepared, &mut source_xu, &mut source_xe, sigma, 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); - 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); assert!( diff --git a/core/src/tests/generics/encryption/glwe_tsk.rs b/core/src/tests/generics/encryption/glwe_tsk.rs index 3b7137b..df20016 100644 --- a/core/src/tests/generics/encryption/glwe_tsk.rs +++ b/core/src/tests/generics/encryption/glwe_tsk.rs @@ -1,6 +1,6 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxCopy, VecZnxDftAlloc, VecZnxStd, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxCopy, VecZnxDftAlloc, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned, VecZnxDft}, @@ -12,20 +12,23 @@ use backend::hal::{ use sampling::source::Source; 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}, }; -use crate::trait_families::{GGLWEEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWEEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub fn test_glwe_tensor_key_encrypt_sk(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VmpPMatAlloc + VmpPMatPrepare @@ -64,8 +67,7 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); - sk_exec.prepare(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); tensor_key.encrypt_sk( module, @@ -89,7 +91,7 @@ where (0..rank).for_each(|i| { (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_big_normalize( basek, @@ -104,11 +106,11 @@ where tensor_key .at(i, j) .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); - 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); }); }); @@ -119,11 +121,10 @@ where pub fn test_glwe_tensor_key_compressed_encrypt_sk(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VmpPMatAlloc + VmpPMatPrepare @@ -147,7 +148,8 @@ where let n: usize = module.n(); let rows: usize = k / basek; - let mut tensor_key_compressed: GGLWETensorKeyCompressed> = GGLWETensorKeyCompressed::alloc(n, basek, k, rows, 1, rank); + let mut tensor_key_compressed: GGLWETensorKeyCompressed> = + GGLWETensorKeyCompressed::alloc(n, basek, k, rows, 1, rank); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -162,8 +164,7 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); - sk_exec.prepare(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); let seed_xa: [u8; 32] = [1u8; 32]; @@ -192,7 +193,7 @@ where (0..rank).for_each(|i| { (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_big_normalize( basek, @@ -207,11 +208,11 @@ where tensor_key .at(i, j) .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); - 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); }); }); diff --git a/core/src/tests/generics/external_product/gglwe_ksk.rs b/core/src/tests/generics/external_product/gglwe_ksk.rs index 4c5bb03..d85729e 100644 --- a/core/src/tests/generics/external_product/gglwe_ksk.rs +++ b/core/src/tests/generics/external_product/gglwe_ksk.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxStd, - VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxSubScalarInplace, + VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, }, layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned}, oep::{ @@ -13,14 +13,14 @@ use sampling::source::Source; use crate::{ layouts::{ - GGSWCiphertext, GLWESecret, GGLWESwitchingKey, - prepared::{GGSWCiphertextExec, GLWESecretExec}, + GGLWESwitchingKey, GGSWCiphertext, GLWESecret, + prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, 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( module: &Module, @@ -34,11 +34,10 @@ pub fn test_gglwe_switching_key_external_product( sigma: f64, ) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VecZnxCopy + VmpPMatAlloc @@ -63,7 +62,8 @@ pub fn test_gglwe_switching_key_external_product( let rows: usize = k_in.div_ceil(basek * digits); let digits_in: usize = 1; - let mut ct_gglwe_in: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(n, basek, k_in, rows, digits_in, rank_in, rank_out); + let mut ct_gglwe_in: GGLWESwitchingKey> = + GGLWESwitchingKey::alloc(n, basek, k_in, rows, digits_in, rank_in, rank_out); let mut ct_gglwe_out: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(n, basek, k_out, rows, digits_in, rank_in, rank_out); let mut ct_rgsw: GGSWCiphertext> = GGSWCiphertext::alloc(n, basek, k_ggsw, rows, digits, rank_out); @@ -91,7 +91,7 @@ pub fn test_gglwe_switching_key_external_product( let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank_out); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_in.encrypt_sk( @@ -107,20 +107,17 @@ pub fn test_gglwe_switching_key_external_product( ct_rgsw.encrypt_sk( module, &pt_rgsw, - &sk_out_exec, + &sk_out_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut ct_rgsw_exec: GGSWCiphertextExec, B> = - GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank_out); - - ct_rgsw_exec.prepare(module, &ct_rgsw, scratch.borrow()); + let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // 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| { 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( ct_gglwe_out .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( @@ -163,11 +160,10 @@ pub fn test_gglwe_switching_key_external_product_inplace( sigma: f64, ) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VecZnxCopy + VmpPMatAlloc @@ -219,7 +215,7 @@ pub fn test_gglwe_switching_key_external_product_inplace( let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank_out); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe.encrypt_sk( @@ -235,20 +231,17 @@ pub fn test_gglwe_switching_key_external_product_inplace( ct_rgsw.encrypt_sk( module, &pt_rgsw, - &sk_out_exec, + &sk_out_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut ct_rgsw_exec: GGSWCiphertextExec, B> = - GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank_out); - - ct_rgsw_exec.prepare(module, &ct_rgsw, scratch.borrow()); + let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // 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| { 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( ct_gglwe .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); } diff --git a/core/src/tests/generics/external_product/ggsw_ct.rs b/core/src/tests/generics/external_product/ggsw_ct.rs index 14cb8e4..8d627cf 100644 --- a/core/src/tests/generics/external_product/ggsw_ct.rs +++ b/core/src/tests/generics/external_product/ggsw_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxStd, - VecZnxSubABInplace, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxSubABInplace, + VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, }, layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned}, oep::{ @@ -14,7 +14,7 @@ use sampling::source::Source; use crate::{ layouts::{ GGSWCiphertext, GLWESecret, - prepared::{GGSWCiphertextExec, GLWESecretExec}, + prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, trait_families::GGSWAssertNoiseFamily, @@ -22,7 +22,7 @@ use crate::{ use crate::trait_families::{ GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily, - GLWESecretExecModuleFamily, + GLWESecretPreparedModuleFamily, }; pub fn test_ggsw_external_product( @@ -35,12 +35,11 @@ pub fn test_ggsw_external_product( rank: usize, sigma: f64, ) where - Module: GLWESecretExecModuleFamily + Module: GLWESecretPreparedModuleFamily + GGSWEncryptSkFamily + GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare @@ -89,12 +88,12 @@ pub fn test_ggsw_external_product( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); ct_ggsw_rhs.encrypt_sk( module, &pt_ggsw_rhs, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, @@ -104,17 +103,16 @@ pub fn test_ggsw_external_product( ct_ggsw_lhs_in.encrypt_sk( module, &pt_ggsw_lhs, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut ct_rhs_exec: GGSWCiphertextExec, B> = GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank); - ct_rhs_exec.prepare(module, &ct_ggsw_rhs, scratch.borrow()); + let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ct_ggsw_rhs.prepare_alloc(module, 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); @@ -141,7 +139,7 @@ pub fn test_ggsw_external_product( ) + 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( @@ -153,12 +151,11 @@ pub fn test_ggsw_external_product_inplace( rank: usize, sigma: f64, ) where - Module: GLWESecretExecModuleFamily + Module: GLWESecretPreparedModuleFamily + GGSWEncryptSkFamily + GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare @@ -207,12 +204,12 @@ pub fn test_ggsw_external_product_inplace( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); ct_ggsw_rhs.encrypt_sk( module, &pt_ggsw_rhs, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, @@ -222,17 +219,16 @@ pub fn test_ggsw_external_product_inplace( ct_ggsw_lhs.encrypt_sk( module, &pt_ggsw_lhs, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let mut ct_rhs_exec: GGSWCiphertextExec, B> = GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank); - ct_rhs_exec.prepare(module, &ct_ggsw_rhs, scratch.borrow()); + let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ct_ggsw_rhs.prepare_alloc(module, 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); @@ -259,5 +255,5 @@ pub fn test_ggsw_external_product_inplace( ) + 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); } diff --git a/core/src/tests/generics/external_product/glwe_ct.rs b/core/src/tests/generics/external_product/glwe_ct.rs index bdb5797..644495d 100644 --- a/core/src/tests/generics/external_product/glwe_ct.rs +++ b/core/src/tests/generics/external_product/glwe_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxRotateInplace, VecZnxStd, - VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxRotateInplace, VmpPMatAlloc, + VmpPMatPrepare, ZnxViewMut, }, layouts::{Backend, Module, ScalarZnx, ScratchOwned}, oep::{ @@ -14,12 +14,12 @@ use sampling::source::Source; use crate::{ layouts::{ GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, - prepared::{GGSWCiphertextExec, GLWESecretExec}, + prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, }, 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( module: &Module, @@ -32,12 +32,11 @@ pub fn test_glwe_external_product( sigma: f64, ) where Module: GLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + GLWEExternalProductFamily + VecZnxAddScalarInplace + VecZnxRotateInplace - + VecZnxStd + VmpPMatAlloc + VmpPMatPrepare, B: TakeVecZnxDftImpl @@ -88,12 +87,12 @@ pub fn test_glwe_external_product( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); ct_ggsw.encrypt_sk( module, &pt_rgsw, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, @@ -103,16 +102,16 @@ pub fn test_glwe_external_product( ct_glwe_in.encrypt_sk( module, &pt_want, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let ct_ggsw_exec: GGSWCiphertextExec, B> = GGSWCiphertextExec::from(module, &ct_ggsw, scratch.borrow()); + let ct_ggsw_prepared: GGSWCiphertextPrepared, 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); @@ -137,7 +136,7 @@ pub fn test_glwe_external_product( 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( @@ -150,12 +149,11 @@ pub fn test_glwe_external_product_inplace( sigma: f64, ) where Module: GLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + GLWEExternalProductFamily + VecZnxAddScalarInplace + VecZnxRotateInplace - + VecZnxStd + VmpPMatAlloc + VmpPMatPrepare, B: TakeVecZnxDftImpl @@ -196,12 +194,12 @@ pub fn test_glwe_external_product_inplace( let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); ct_ggsw.encrypt_sk( module, &pt_rgsw, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, @@ -211,16 +209,16 @@ pub fn test_glwe_external_product_inplace( ct_glwe.encrypt_sk( module, &pt_want, - &sk_exec, + &sk_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let ct_ggsw_exec: GGSWCiphertextExec, B> = GGSWCiphertextExec::from(module, &ct_ggsw, scratch.borrow()); + let ct_ggsw_prepared: GGSWCiphertextPrepared, 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); @@ -245,5 +243,5 @@ pub fn test_glwe_external_product_inplace( 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); } diff --git a/core/src/tests/generics/keyswitch/gglwe_ct.rs b/core/src/tests/generics/keyswitch/gglwe_ct.rs index 4b674d2..fcfc01b 100644 --- a/core/src/tests/generics/keyswitch/gglwe_ct.rs +++ b/core/src/tests/generics/keyswitch/gglwe_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubScalarInplace, - VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubScalarInplace, VecZnxSwithcDegree, + VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -13,14 +13,14 @@ use sampling::source::Source; use crate::{ layouts::{ - GLWESecret, GGLWESwitchingKey, - prepared::{GLWESecretExec, GGLWESwitchingKeyExec}, + GGLWESwitchingKey, GLWESecret, + prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, 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( module: &Module, @@ -35,11 +35,10 @@ pub fn test_gglwe_switching_key_keyswitch( sigma: f64, ) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VecZnxCopy + VmpPMatAlloc @@ -109,7 +108,7 @@ pub fn test_gglwe_switching_key_keyswitch( let mut sk2: GLWESecret> = GLWESecret::alloc(n, rank_out_s1s2); sk2.fill_ternary_prob(0.5, &mut source_xs); - let sk2_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk2); + let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_s0s1.encrypt_sk( @@ -133,14 +132,14 @@ pub fn test_gglwe_switching_key_keyswitch( scratch_enc.borrow(), ); - let ct_gglwe_s1s2_exec: GGLWESwitchingKeyExec, B> = - GGLWESwitchingKeyExec::from(module, &ct_gglwe_s1s2, scratch_apply.borrow()); + let ct_gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + ct_gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) ct_gglwe_s0s2.keyswitch( module, &ct_gglwe_s0s1, - &ct_gglwe_s1s2_exec, + &ct_gglwe_s1s2_prepared, scratch_apply.borrow(), ); @@ -159,7 +158,7 @@ pub fn test_gglwe_switching_key_keyswitch( ct_gglwe_s0s2 .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( @@ -173,11 +172,10 @@ pub fn test_gglwe_switching_key_keyswitch_inplace( sigma: f64, ) where Module: GGLWEEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEDecryptFamily + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxStd + VecZnxSubScalarInplace + VecZnxCopy + VmpPMatAlloc @@ -203,7 +201,8 @@ pub fn test_gglwe_switching_key_keyswitch_inplace( let mut ct_gglwe_s0s1: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(n, basek, k_ct, rows, digits_in, rank_in, rank_out); - let mut ct_gglwe_s1s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank_out, rank_out); + let mut ct_gglwe_s1s2: GGLWESwitchingKey> = + GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank_out, rank_out); let mut source_xs: 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( let mut sk2: GLWESecret> = GLWESecret::alloc(n, rank_out); sk2.fill_ternary_prob(var_xs, &mut source_xs); - let sk2_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk2); + let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_s0s1.encrypt_sk( @@ -255,11 +254,11 @@ pub fn test_gglwe_switching_key_keyswitch_inplace( scratch_enc.borrow(), ); - let ct_gglwe_s1s2_exec: GGLWESwitchingKeyExec, B> = - GGLWESwitchingKeyExec::from(module, &ct_gglwe_s1s2, scratch_apply.borrow()); + let ct_gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + ct_gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // 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> = ct_gglwe_s0s1; @@ -278,5 +277,5 @@ pub fn test_gglwe_switching_key_keyswitch_inplace( ct_gglwe_s0s2 .key - .assert_noise(module, &sk2_exec, &sk0.data, max_noise + 0.5); + .assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5); } diff --git a/core/src/tests/generics/keyswitch/ggsw_ct.rs b/core/src/tests/generics/keyswitch/ggsw_ct.rs index 9c26f93..ea5bf2a 100644 --- a/core/src/tests/generics/keyswitch/ggsw_ct.rs +++ b/core/src/tests/generics/keyswitch/ggsw_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubABInplace, - VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubABInplace, VecZnxSwithcDegree, + VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScalarZnx, ScratchOwned}, oep::{ @@ -13,8 +13,8 @@ use sampling::source::Source; use crate::{ layouts::{ - GGLWETensorKey, GGSWCiphertext, GLWESecret, GGLWESwitchingKey, - prepared::{GLWESecretExec, GGLWESwitchingKeyExec, GGLWETensorKeyExec}, + GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWESecret, + prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_keyswitch, trait_families::GGSWAssertNoiseFamily, @@ -22,7 +22,7 @@ use crate::{ use crate::trait_families::{ GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily, - GLWESecretExecModuleFamily, + GLWESecretPreparedModuleFamily, }; pub fn test_ggsw_keyswitch( @@ -36,12 +36,11 @@ pub fn test_ggsw_keyswitch( rank: usize, sigma: f64, ) where - Module: GLWESecretExecModuleFamily + Module: GLWESecretPreparedModuleFamily + GGSWEncryptSkFamily + GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare @@ -93,11 +92,11 @@ pub fn test_ggsw_keyswitch( let mut sk_in: GLWESecret> = GLWESecret::alloc(n, rank); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let sk_in_dft: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_in); + let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); ksk.encrypt_sk( module, @@ -129,14 +128,16 @@ pub fn test_ggsw_keyswitch( scratch.borrow(), ); - let mut ksk_exec: GGLWESwitchingKeyExec, B> = - GGLWESwitchingKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank, rank); - let mut tsk_exec: GGLWETensorKeyExec, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); + let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); - ksk_exec.prepare(module, &ksk, scratch.borrow()); - tsk_exec.prepare(module, &tsk, scratch.borrow()); - - ct_out.keyswitch(module, &ct_in, &ksk_exec, &tsk_exec, scratch.borrow()); + ct_out.keyswitch( + module, + &ct_in, + &ksk_prepared, + &tsk_prepared, + scratch.borrow(), + ); let max_noise = |col_j: usize| -> f64 { noise_ggsw_keyswitch( @@ -154,7 +155,7 @@ pub fn test_ggsw_keyswitch( ) + 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( @@ -167,12 +168,11 @@ pub fn test_ggsw_keyswitch_inplace( rank: usize, sigma: f64, ) where - Module: GLWESecretExecModuleFamily + Module: GLWESecretPreparedModuleFamily + GGSWEncryptSkFamily + GGSWAssertNoiseFamily + VecZnxAddScalarInplace + VecZnxSubABInplace - + VecZnxStd + VecZnxCopy + VmpPMatAlloc + VmpPMatPrepare @@ -218,11 +218,11 @@ pub fn test_ggsw_keyswitch_inplace( let mut sk_in: GLWESecret> = GLWESecret::alloc(n, rank); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let sk_in_dft: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_in); + let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); ksk.encrypt_sk( module, @@ -254,14 +254,10 @@ pub fn test_ggsw_keyswitch_inplace( scratch.borrow(), ); - let mut ksk_exec: GGLWESwitchingKeyExec, B> = - GGLWESwitchingKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank, rank); - let mut tsk_exec: GGLWETensorKeyExec, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank); + let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); - ksk_exec.prepare(module, &ksk, scratch.borrow()); - tsk_exec.prepare(module, &tsk, scratch.borrow()); - - ct.keyswitch_inplace(module, &ksk_exec, &tsk_exec, scratch.borrow()); + ct.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); let max_noise = |col_j: usize| -> f64 { noise_ggsw_keyswitch( @@ -279,5 +275,5 @@ pub fn test_ggsw_keyswitch_inplace( ) + 0.5 }; - ct.assert_noise(module, &sk_out_exec, &pt_scalar, &max_noise); + ct.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise); } diff --git a/core/src/tests/generics/keyswitch/glwe_ct.rs b/core/src/tests/generics/keyswitch/glwe_ct.rs index a764274..1d74416 100644 --- a/core/src/tests/generics/keyswitch/glwe_ct.rs +++ b/core/src/tests/generics/keyswitch/glwe_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxStd, VecZnxSwithcDegree, - VmpPMatAlloc, VmpPMatPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxSwithcDegree, VmpPMatAlloc, + VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -13,14 +13,14 @@ use sampling::source::Source; use crate::{ layouts::{ - GLWECiphertext, GLWEPlaintext, GLWESecret, GGLWESwitchingKey, Infos, - prepared::{GLWESecretExec, GGLWESwitchingKeyExec}, + GGLWESwitchingKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, + prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, }; -use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub fn test_glwe_keyswitch( module: &Module, @@ -34,10 +34,9 @@ pub fn test_glwe_keyswitch( sigma: f64, ) where Module: GGLWESwitchingKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEKeyswitchFamily + GLWEDecryptFamily - + VecZnxStd + VecZnxSwithcDegree + VecZnxAddScalarInplace + VmpPMatAlloc @@ -83,11 +82,11 @@ pub fn test_glwe_keyswitch( let mut sk_in: GLWESecret> = GLWESecret::alloc(n, rank_in); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_in); + let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank_out); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); ksk.encrypt_sk( module, @@ -102,16 +101,16 @@ pub fn test_glwe_keyswitch( ct_in.encrypt_sk( module, &pt_want, - &sk_in_exec, + &sk_in_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let ksk_exec: GGLWESwitchingKeyExec, B> = GGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); + let ksk_prepared: GGLWESwitchingKeyPrepared, 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( module.n() as f64, @@ -126,7 +125,7 @@ pub fn test_glwe_keyswitch( 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( @@ -139,10 +138,9 @@ pub fn test_glwe_keyswitch_inplace( sigma: f64, ) where Module: GGLWESwitchingKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEKeyswitchFamily + GLWEDecryptFamily - + VecZnxStd + VecZnxSwithcDegree + VecZnxAddScalarInplace + VmpPMatAlloc @@ -177,11 +175,11 @@ pub fn test_glwe_keyswitch_inplace( let mut sk_in: GLWESecret> = GLWESecret::alloc(n, rank); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_in); + let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n, rank); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_out); + let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); ksk.encrypt_sk( module, @@ -196,16 +194,16 @@ pub fn test_glwe_keyswitch_inplace( ct_glwe.encrypt_sk( module, &pt_want, - &sk_in_exec, + &sk_in_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let ksk_exec: GGLWESwitchingKeyExec, B> = GGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); + let ksk_prepared: GGLWESwitchingKeyPrepared, 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( module.n() as f64, @@ -220,5 +218,5 @@ pub fn test_glwe_keyswitch_inplace( 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); } diff --git a/core/src/tests/generics/keyswitch/lwe_ct.rs b/core/src/tests/generics/keyswitch/lwe_ct.rs index a8c0f97..e565fa9 100644 --- a/core/src/tests/generics/keyswitch/lwe_ct.rs +++ b/core/src/tests/generics/keyswitch/lwe_ct.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxEncodeCoeffsi64, - VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxSwithcDegree, + VmpPMatAlloc, VmpPMatPrepare, ZnxView, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -11,9 +11,12 @@ use backend::hal::{ }; 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(module: &Module) where @@ -22,11 +25,10 @@ where + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWEKeyswitchFamily - + VecZnxEncodeCoeffsi64 + VecZnxAutomorphismInplace + VmpPMatAlloc + VmpPMatPrepare - + GLWESecretExecModuleFamily, + + GLWESecretPreparedModuleFamily, B: TakeScalarZnxImpl + TakeVecZnxDftImpl + ScratchAvailableImpl @@ -65,7 +67,8 @@ where let data: i64 = 17; let mut lwe_pt_in: LWEPlaintext> = 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> = LWECiphertext::alloc(n_lwe_in, basek, k_lwe_ct); lwe_ct_in.encrypt_sk( @@ -91,9 +94,9 @@ where let mut lwe_ct_out: LWECiphertext> = LWECiphertext::alloc(n_lwe_out, basek, k_lwe_ct); - let ksk_exec: LWESwitchingKeyExec, B> = LWESwitchingKeyExec::from(module, &ksk, scratch.borrow()); + let ksk_prepared: LWESwitchingKeyPrepared, 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> = LWEPlaintext::alloc(basek, k_lwe_ct); lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out); diff --git a/core/src/tests/generics/packing.rs b/core/src/tests/generics/packing.rs index cde7548..7fd45b1 100644 --- a/core/src/tests/generics/packing.rs +++ b/core/src/tests/generics/packing.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use backend::hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigSubSmallBInplace, - VecZnxEncodeVeci64, VecZnxRotateInplace, VecZnxStd, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + VecZnxRotateInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -17,22 +17,20 @@ use crate::{ GLWEOperations, GLWEPacker, layouts::{ GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec}, + prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily, GLWEPackingFamily}, }; -use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub trait PackingTestModuleFamily = GLWEPackingFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GGLWESwitchingKeyEncryptSkFamily + GLWEKeyswitchFamily + GLWEDecryptFamily - + VecZnxStd + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxEncodeVeci64 + VecZnxRotateInplace + VecZnxAutomorphism + VecZnxBigSubSmallBInplace @@ -76,7 +74,7 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_ct); let mut data: Vec = vec![0i64; n]; @@ -84,11 +82,11 @@ where *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 = GLWEPacker::galois_elements(module); - let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut auto_keys: HashMap, B>> = HashMap::new(); let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( @@ -100,8 +98,8 @@ where sigma, scratch.borrow(), ); - let atk_exec: GGLWEAutomorphismKeyExec, B> = GGLWEAutomorphismKeyExec::from(module, &tmp, scratch.borrow()); - auto_keys.insert(*gal_el, atk_exec); + let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + auto_keys.insert(*gal_el, atk_prepared); }); 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()); 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); assert!( noise_have < -((k_ct - basek) as f64), diff --git a/core/src/tests/generics/trace.rs b/core/src/tests/generics/trace.rs index 3e3ba32..2e08206 100644 --- a/core/src/tests/generics/trace.rs +++ b/core/src/tests/generics/trace.rs @@ -3,9 +3,8 @@ use std::collections::HashMap; use backend::hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigAutomorphismInplace, - VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxEncodeVeci64, VecZnxFillUniform, VecZnxNormalizeInplace, - VecZnxRotateInplace, VecZnxRshInplace, VecZnxStd, VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, - ZnxView, ZnxViewMut, + VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxFillUniform, VecZnxNormalizeInplace, VecZnxRotateInplace, VecZnxRshInplace, + VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, ZnxViewMut, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -18,22 +17,20 @@ use sampling::source::Source; use crate::{ layouts::{ GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, - prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec}, + prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::var_noise_gglwe_product, trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily}, }; -use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; +use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily}; pub trait TraceTestModuleFamily = GGLWESwitchingKeyEncryptSkFamily - + GLWESecretExecModuleFamily + + GLWESecretPreparedModuleFamily + GLWEKeyswitchFamily + GLWEDecryptFamily - + VecZnxStd + VecZnxSwithcDegree + VecZnxAddScalarInplace - + VecZnxEncodeVeci64 + VecZnxRotateInplace + VecZnxBigSubSmallBInplace + VecZnxBigAutomorphismInplace @@ -80,7 +77,7 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); + let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); let mut data_want: Vec = vec![0i64; n]; @@ -100,7 +97,7 @@ where scratch.borrow(), ); - let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut auto_keys: HashMap, B>> = HashMap::new(); let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(n, basek, k_autokey, rows, digits, rank); gal_els.iter().for_each(|gal_el| { @@ -113,8 +110,8 @@ where sigma, scratch.borrow(), ); - let atk_exec: GGLWEAutomorphismKeyExec, B> = GGLWEAutomorphismKeyExec::from(module, &tmp, scratch.borrow()); - auto_keys.insert(*gal_el, atk_exec); + let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + auto_keys.insert(*gal_el, atk_prepared); }); ct.trace_inplace(module, 0, 5, &auto_keys, scratch.borrow()); @@ -127,7 +124,7 @@ where module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0); module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow()); - let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0).log2(); + let noise_have: f64 = pt_want.std().log2(); let mut noise_want: f64 = var_noise_gglwe_product( n as f64, diff --git a/core/src/tests/serialization.rs b/core/src/tests/serialization.rs index 8199723..545bdca 100644 --- a/core/src/tests/serialization.rs +++ b/core/src/tests/serialization.rs @@ -1,11 +1,11 @@ use backend::hal::tests::serialization::test_reader_writer_interface; use crate::layouts::{ - GGLWEAutomorphismKey, GGLWECiphertext, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GGLWESwitchingKey, + GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEToLWESwitchingKey, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, compressed::{ - GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed, - GGLWESwitchingKeyCompressed, GGLWETensorKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, + GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWESwitchingKeyCompressed, GGLWETensorKeyCompressed, + GGSWCiphertextCompressed, GLWECiphertextCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, }, }; diff --git a/core/src/trait_families.rs b/core/src/trait_families.rs index 0c14696..8efb654 100644 --- a/core/src/trait_families.rs +++ b/core/src/trait_families.rs @@ -89,8 +89,8 @@ pub trait GLWEPackingFamily = GLWEKeyswitchFamily + VecZnxAutomorphismInplace + VecZnxBigSubSmallBInplace + VecZnxBigAutomorphismInplace; -pub trait GLWESecretExecModuleFamily = SvpPrepare + SvpPPolAllocBytes + SvpPPolAlloc; -pub trait GGLWEExecLayoutFamily = VmpPMatAlloc + VmpPMatAllocBytes + VmpPMatPrepare; +pub trait GLWESecretPreparedModuleFamily = SvpPrepare + SvpPPolAllocBytes + SvpPPolAlloc; +pub trait GGLWEPreparedLayoutFamily = VmpPMatAlloc + VmpPMatAllocBytes + VmpPMatPrepare; pub trait GLWETraceModuleFamily = GLWEKeyswitchFamily + VecZnxCopy + VecZnxRshInplace + VecZnxBigAutomorphismInplace; pub trait GLWETraceScratchFamily = TakeVecZnxDft + ScratchAvailable; diff --git a/core/src/utils.rs b/core/src/utils.rs new file mode 100644 index 0000000..076a910 --- /dev/null +++ b/core/src/utils.rs @@ -0,0 +1,55 @@ +use crate::layouts::{GLWEPlaintext, Infos, LWEPlaintext}; +use backend::hal::layouts::{DataMut, DataRef}; +use rug::Float; + +impl GLWEPlaintext { + pub fn encode_vec_i64(&mut self, data: &[i64], k: usize) { + let basek: usize = self.basek(); + self.data + .encode_vec_i64(basek, 0, k, data, i64::BITS as usize); + } + + pub fn encode_coeff_i64(&mut self, data: i64, k: usize, idx: usize) { + let basek: usize = self.basek(); + self.data + .encode_coeff_i64(basek, 0, k, idx, data, i64::BITS as usize); + } +} + +impl GLWEPlaintext { + pub fn decode_vec_i64(&self, data: &mut [i64], k: usize) { + self.data.decode_vec_i64(self.basek(), 0, k, data); + } + + pub fn decode_coeff_i64(&self, k: usize, idx: usize) -> i64 { + self.data.decode_coeff_i64(self.basek(), 0, k, idx) + } + + pub fn decode_vec_float(&self, data: &mut [Float]) { + self.data.decode_vec_float(self.basek(), 0, data); + } + + pub fn std(&self) -> f64 { + self.data.std(self.basek(), 0) + } +} + +impl LWEPlaintext { + pub fn encode_i64(&mut self, data: i64, k: usize) { + let basek: usize = self.basek(); + self.data + .encode_coeff_i64(basek, 0, k, 0, data, i64::BITS as usize); + } +} + +impl LWEPlaintext { + pub fn decode_i64(&self, k: usize) -> i64 { + self.data.decode_coeff_i64(self.basek(), 0, k, 0) + } + + pub fn decode_float(&self) -> Float { + let mut data: Vec = vec![Float::new(self.k() as u32)]; + self.data.decode_vec_float(self.basek(), 0, &mut data); + data[0].clone() + } +} diff --git a/schemes/Cargo.toml b/schemes/Cargo.toml new file mode 100644 index 0000000..c1634e7 --- /dev/null +++ b/schemes/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "schemes" +version = "0.1.0" +edition = "2024" + +[dependencies] +backend = {path="../backend"} +core = {path="../core"} +sampling = {path="../sampling"} +itertools = "0.14.0" +byteorder = "1.5.0" \ No newline at end of file diff --git a/schemes/src/lib.rs b/schemes/src/lib.rs new file mode 100644 index 0000000..442ff8f --- /dev/null +++ b/schemes/src/lib.rs @@ -0,0 +1,2 @@ +#![feature(trait_alias)] +pub mod tfhe; diff --git a/bin_fhe/blind_rotation/cggi.rs b/schemes/src/tfhe/blind_rotation/cggi_algo.rs similarity index 87% rename from bin_fhe/blind_rotation/cggi.rs rename to schemes/src/tfhe/blind_rotation/cggi_algo.rs index b64cef3..59c1357 100644 --- a/bin_fhe/blind_rotation/cggi.rs +++ b/schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -10,14 +10,16 @@ use backend::hal::{ }; use itertools::izip; -use crate::{ - GLWEOps, Infos, LookUpTableRotationDirection, TakeGLWECt, - blind_rotation::{key::BlindRotationKeyCGGIExec, lut::LookUpTable}, - dist::Distribution, - layouts::{GLWECiphertext, GLWECiphertextToMut, LWECiphertext, LWECiphertextToRef}, +use core::{ + Distribution, GLWEOperations, TakeGLWECt, + layouts::{GLWECiphertext, GLWECiphertextToMut, Infos, LWECiphertext, LWECiphertextToRef}, trait_families::GLWEExternalProductFamily, }; +use crate::tfhe::blind_rotation::{ + BlincRotationExecute, BlindRotationKeyPrepared, CGGI, LookUpTable, LookUpTableRotationDirection, +}; + pub trait CCGIBlindRotationFamily = VecZnxBigAllocBytes + VecZnxDftAllocBytes + SvpPPolAllocBytes @@ -85,50 +87,47 @@ where } } -pub fn cggi_blind_rotate( - module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, - lut: &LookUpTable, - brk: &BlindRotationKeyCGGIExec, - scratch: &mut Scratch, -) where - DataRes: DataMut, - DataIn: DataRef, - DataBrk: DataRef, +impl BlincRotationExecute for BlindRotationKeyPrepared +where Module: CCGIBlindRotationFamily, - Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx + ScratchAvailable + TakeVecZnxSlice, + Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + TakeVecZnx + ScratchAvailable, { - match brk.dist { - Distribution::BinaryBlock(_) | Distribution::BinaryFixed(_) | Distribution::BinaryProb(_) | Distribution::ZERO => { - if lut.extension_factor() > 1 { - cggi_blind_rotate_block_binary_extended(module, res, lwe, lut, brk, scratch); - } else if brk.block_size() > 1 { - cggi_blind_rotate_block_binary(module, res, lwe, lut, brk, scratch); - } else { - cggi_blind_rotate_binary_standard(module, res, lwe, lut, brk, scratch); + fn execute( + &self, + module: &Module, + res: &mut GLWECiphertext, + lwe: &LWECiphertext, + lut: &LookUpTable, + scratch: &mut Scratch, + ) { + match self.dist { + Distribution::BinaryBlock(_) | Distribution::BinaryFixed(_) | Distribution::BinaryProb(_) | Distribution::ZERO => { + if lut.extension_factor() > 1 { + execute_block_binary_extended(module, res, lwe, lut, self, scratch) + } else if self.block_size() > 1 { + execute_block_binary(module, res, lwe, lut, self, scratch); + } else { + execute_standard(module, res, lwe, lut, self, scratch); + } } + _ => panic!("invalid CGGI distribution"), } - // TODO: ternary distribution ? - _ => panic!( - "invalid BlindRotationKeyCGGI distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)" - ), } } -pub(crate) fn cggi_blind_rotate_block_binary_extended( +fn execute_block_binary_extended( module: &Module, res: &mut GLWECiphertext, lwe: &LWECiphertext, lut: &LookUpTable, - brk: &BlindRotationKeyCGGIExec, + brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, ) where DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, Module: CCGIBlindRotationFamily, - Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice, + Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + ScratchAvailable + TakeVecZnx, { let n_glwe: usize = brk.n(); let extension_factor: usize = lut.extension_factor(); @@ -196,7 +195,7 @@ pub(crate) fn cggi_blind_rotate_block_binary_extended( +fn execute_block_binary( module: &Module, res: &mut GLWECiphertext, lwe: &LWECiphertext, lut: &LookUpTable, - brk: &BlindRotationKeyCGGIExec, + brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, ) where DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, Module: CCGIBlindRotationFamily, - Scratch: TakeVecZnxDft + TakeVecZnxBig, + Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + ScratchAvailable + TakeVecZnx, { let n_glwe: usize = brk.n(); let mut lwe_2n: Vec = vec![0i64; lwe.n() + 1]; // TODO: from scratch space @@ -331,7 +330,7 @@ pub(crate) fn cggi_blind_rotate_block_binary( +fn execute_standard( module: &Module, res: &mut GLWECiphertext, lwe: &LWECiphertext, lut: &LookUpTable, - brk: &BlindRotationKeyCGGIExec, + brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, ) where DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, Module: CCGIBlindRotationFamily, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx + ScratchAvailable, + Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + ScratchAvailable + TakeVecZnx, { #[cfg(debug_assertions)] { @@ -445,7 +444,7 @@ pub(crate) fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWECiphertext<&[u8] let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1; - res.copy_from_slice(&lwe.data.at(0, 0)); + res.copy_from_slice(&lwe.data().at(0, 0)); match rot_dir { LookUpTableRotationDirection::Left => { @@ -465,11 +464,11 @@ pub(crate) fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWECiphertext<&[u8] (1..size).for_each(|i| { if i == size - 1 && rem != basek { let k_rem: usize = basek - rem; - izip!(lwe.data.at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { + izip!(lwe.data().at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { *y = (*y << k_rem) + (x >> rem); }); } else { - izip!(lwe.data.at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { + izip!(lwe.data().at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { *y = (*y << basek) + x; }); } diff --git a/schemes/src/tfhe/blind_rotation/cggi_key.rs b/schemes/src/tfhe/blind_rotation/cggi_key.rs new file mode 100644 index 0000000..f63c070 --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -0,0 +1,188 @@ +use backend::hal::{ + api::{ + ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, VmpPMatAlloc, VmpPMatPrepare, ZnxView, ZnxViewMut, + }, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch}, +}; +use sampling::source::Source; + +use std::marker::PhantomData; + +use core::{ + Distribution, + layouts::{ + GGSWCiphertext, LWESecret, + compressed::GGSWCiphertextCompressed, + prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, + }, + trait_families::GGSWEncryptSkFamily, +}; + +use crate::tfhe::blind_rotation::{ + BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyCompressed, BlindRotationKeyEncryptSk, BlindRotationKeyPrepared, + BlindRotationKeyPreparedAlloc, CGGI, +}; + +impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { + fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { + let mut data: Vec>> = 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, + _phantom: PhantomData, + } + } +} + +impl BlindRotationKey, CGGI> { + pub fn generate_from_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize + where + Module: GGSWEncryptSkFamily, + { + GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k, rank) + } +} + +impl BlindRotationKeyEncryptSk for BlindRotationKey +where + Module: GGSWEncryptSkFamily + VecZnxAddScalarInplace, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +{ + fn encrypt_sk( + &mut self, + module: &Module, + sk_glwe: &GLWESecretPrepared, + sk_lwe: &LWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) where + DataSkGLWE: DataRef, + DataSkLWE: DataRef, + { + #[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> = 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); + }); + } +} + +impl BlindRotationKeyPreparedAlloc for BlindRotationKeyPrepared, CGGI, B> +where + Module: VmpPMatAlloc + VmpPMatPrepare, +{ + fn alloc(module: &Module, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { + let mut data: Vec, B>> = Vec::with_capacity(n_lwe); + (0..n_lwe).for_each(|_| { + data.push(GGSWCiphertextPrepared::alloc( + module, n_glwe, basek, k, rows, 1, rank, + )) + }); + Self { + data, + dist: Distribution::NONE, + x_pow_a: None, + _phantom: PhantomData, + } + } +} + +impl BlindRotationKeyCompressed, CGGI> { + pub fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { + let mut data: Vec>> = 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, + _phantom: PhantomData, + } + } + + pub fn generate_from_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize, rank: usize) -> usize + where + Module: GGSWEncryptSkFamily, + { + GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank) + } +} + +impl BlindRotationKeyCompressed { + pub fn encrypt_sk( + &mut self, + module: &Module, + sk_glwe: &GLWESecretPrepared, + sk_lwe: &LWESecret, + seed_xa: [u8; 32], + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) where + DataSkGLWE: DataRef, + DataSkLWE: DataRef, + Module: GGSWEncryptSkFamily + VecZnxAddScalarInplace, + Scratch: TakeVecZnxDft + 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> = 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, + ); + }); + } +} diff --git a/schemes/src/tfhe/blind_rotation/key.rs b/schemes/src/tfhe/blind_rotation/key.rs new file mode 100644 index 0000000..a788eb9 --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/key.rs @@ -0,0 +1,162 @@ +use backend::hal::{ + api::{FillUniform, Reset}, + layouts::{Backend, Data, DataMut, DataRef, Module, ReaderFrom, Scratch, WriterTo}, +}; +use sampling::source::Source; + +use std::{fmt, marker::PhantomData}; + +use core::{ + Distribution, + layouts::{GGSWCiphertext, Infos, LWESecret, prepared::GLWESecretPrepared}, +}; + +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; + +use crate::tfhe::blind_rotation::BlindRotationAlgo; + +pub trait BlindRotationKeyAlloc { + fn alloc(n_gglwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self; +} + +pub trait BlindRotationKeyEncryptSk { + fn encrypt_sk( + &mut self, + module: &Module, + sk_glwe: &GLWESecretPrepared, + sk_lwe: &LWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) where + DataSkGLWE: DataRef, + DataSkLWE: DataRef; +} + +#[derive(Clone)] +pub struct BlindRotationKey { + pub(crate) keys: Vec>, + pub(crate) dist: Distribution, + pub(crate) _phantom: PhantomData, +} + +impl fmt::Debug for BlindRotationKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self) + } +} + +impl PartialEq for BlindRotationKey { + 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 && self._phantom == other._phantom + } +} + +impl Eq for BlindRotationKey {} + +impl fmt::Display for BlindRotationKey { + 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 Reset for BlindRotationKey { + fn reset(&mut self) { + self.keys.iter_mut().for_each(|key| key.reset()); + self.dist = Distribution::NONE; + } +} + +impl FillUniform for BlindRotationKey { + fn fill_uniform(&mut self, source: &mut sampling::source::Source) { + self.keys + .iter_mut() + .for_each(|key| key.fill_uniform(source)); + } +} + +impl ReaderFrom for BlindRotationKey { + fn read_from(&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::()? 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 WriterTo for BlindRotationKey { + fn write_to(&self, writer: &mut W) -> std::io::Result<()> { + match self.dist.write_to(writer) { + Ok(()) => {} + Err(e) => return Err(e), + } + writer.write_u64::(self.keys.len() as u64)?; + for key in &self.keys { + key.write_to(writer)?; + } + Ok(()) + } +} + +impl BlindRotationKey { + #[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, + } + } +} diff --git a/schemes/src/tfhe/blind_rotation/key_compressed.rs b/schemes/src/tfhe/blind_rotation/key_compressed.rs new file mode 100644 index 0000000..e6f75c3 --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/key_compressed.rs @@ -0,0 +1,141 @@ +use backend::hal::{ + api::{FillUniform, Reset}, + layouts::{Data, DataMut, DataRef, ReaderFrom, WriterTo}, +}; + +use std::{fmt, marker::PhantomData}; + +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use core::{ + Distribution, + layouts::{Infos, compressed::GGSWCiphertextCompressed}, +}; + +use crate::tfhe::blind_rotation::BlindRotationAlgo; + +#[derive(Clone)] +pub struct BlindRotationKeyCompressed { + pub(crate) keys: Vec>, + pub(crate) dist: Distribution, + pub(crate) _phantom: PhantomData, +} + +impl fmt::Debug for BlindRotationKeyCompressed { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self) + } +} + +impl PartialEq for BlindRotationKeyCompressed { + 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 && self._phantom == other._phantom + } +} + +impl Eq for BlindRotationKeyCompressed {} + +impl fmt::Display for BlindRotationKeyCompressed { + 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 Reset for BlindRotationKeyCompressed { + fn reset(&mut self) { + self.keys.iter_mut().for_each(|key| key.reset()); + self.dist = Distribution::NONE; + } +} + +impl FillUniform for BlindRotationKeyCompressed { + fn fill_uniform(&mut self, source: &mut sampling::source::Source) { + self.keys + .iter_mut() + .for_each(|key| key.fill_uniform(source)); + } +} + +impl ReaderFrom for BlindRotationKeyCompressed { + fn read_from(&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::()? 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 WriterTo for BlindRotationKeyCompressed { + fn write_to(&self, writer: &mut W) -> std::io::Result<()> { + match self.dist.write_to(writer) { + Ok(()) => {} + Err(e) => return Err(e), + } + writer.write_u64::(self.keys.len() as u64)?; + for key in &self.keys { + key.write_to(writer)?; + } + Ok(()) + } +} + +impl BlindRotationKeyCompressed { + #[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, + } + } +} diff --git a/schemes/src/tfhe/blind_rotation/key_prepared.rs b/schemes/src/tfhe/blind_rotation/key_prepared.rs new file mode 100644 index 0000000..887716f --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/key_prepared.rs @@ -0,0 +1,125 @@ +use backend::hal::{ + api::{SvpPPolAlloc, SvpPrepare, VmpPMatAlloc, VmpPMatPrepare}, + layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, Scratch, SvpPPol}, +}; + +use std::marker::PhantomData; + +use core::{ + Distribution, + layouts::{ + Infos, + prepared::{GGSWCiphertextPrepared, Prepare, PrepareAlloc}, + }, +}; + +use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey, utils::set_xai_plus_y}; + +pub trait BlindRotationKeyPreparedAlloc { + fn alloc(module: &Module, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self; +} + +#[derive(PartialEq, Eq)] +pub struct BlindRotationKeyPrepared { + pub(crate) data: Vec>, + pub(crate) dist: Distribution, + pub(crate) x_pow_a: Option, B>>>, + pub(crate) _phantom: PhantomData, +} + +impl BlindRotationKeyPrepared { + #[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, + } + } +} + +impl PrepareAlloc, BRA, B>> + for BlindRotationKey +where + BlindRotationKeyPrepared, BRA, B>: BlindRotationKeyPreparedAlloc, + BlindRotationKeyPrepared, BRA, B>: Prepare>, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> BlindRotationKeyPrepared, BRA, B> { + let mut brk: BlindRotationKeyPrepared, BRA, B> = BlindRotationKeyPrepared::alloc( + module, + self.n(), + self.keys.len(), + self.basek(), + self.k(), + self.rows(), + self.rank(), + ); + brk.prepare(module, self, scratch); + brk + } +} + +impl Prepare> + for BlindRotationKeyPrepared +where + Module: VmpPMatAlloc + VmpPMatPrepare + SvpPPolAlloc + SvpPrepare, +{ + fn prepare(&mut self, module: &Module, other: &BlindRotationKey, scratch: &mut Scratch) { + #[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_prepared, other)| { + ggsw_prepared.prepare(module, other, scratch); + }); + + self.dist = other.dist; + + match other.dist { + Distribution::BinaryBlock(_) => { + let mut x_pow_a: Vec, B>> = Vec::with_capacity(n << 1); + let mut buf: ScalarZnx> = ScalarZnx::alloc(n, 1); + (0..n << 1).for_each(|i| { + let mut res: SvpPPol, 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); + } + _ => {} + } + } +} diff --git a/bin_fhe/blind_rotation/lut.rs b/schemes/src/tfhe/blind_rotation/lut.rs similarity index 100% rename from bin_fhe/blind_rotation/lut.rs rename to schemes/src/tfhe/blind_rotation/lut.rs diff --git a/schemes/src/tfhe/blind_rotation/mod.rs b/schemes/src/tfhe/blind_rotation/mod.rs new file mode 100644 index 0000000..42ef4b8 --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/mod.rs @@ -0,0 +1,35 @@ +mod cggi_algo; +mod cggi_key; +mod key; +mod key_compressed; +mod key_prepared; +mod lut; +mod utils; + +pub use cggi_algo::*; +pub use key::*; +pub use key_compressed::*; +pub use key_prepared::*; +pub use lut::*; + +pub mod tests; + +use backend::hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; +use core::layouts::{GLWECiphertext, LWECiphertext}; + +pub trait BlindRotationAlgo {} + +#[derive(Clone)] +pub struct CGGI {} +impl BlindRotationAlgo for CGGI {} + +pub trait BlincRotationExecute { + fn execute( + &self, + module: &Module, + res: &mut GLWECiphertext, + lwe: &LWECiphertext, + lut: &LookUpTable, + scratch: &mut Scratch, + ); +} diff --git a/bin_fhe/blind_rotation/tests/generic_cggi.rs b/schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs similarity index 59% rename from bin_fhe/blind_rotation/tests/generic_cggi.rs rename to schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index bb53d98..832d3c1 100644 --- a/bin_fhe/blind_rotation/tests/generic_cggi.rs +++ b/schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -1,7 +1,7 @@ use backend::hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxEncodeCoeffsi64, VecZnxFillUniform, - VecZnxRotateInplace, VecZnxSub, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, + ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxRotateInplace, + VecZnxSub, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -11,46 +11,42 @@ use backend::hal::{ }; use sampling::source::Source; -use crate::{ - BlindRotationKeyCGGI, BlindRotationKeyCGGIExec, BlindRotationKeyCGGIExecLayoutFamily, CCGIBlindRotationFamily, Infos, - LookUpTable, cggi_blind_rotate, cggi_blind_rotate_scratch_space, - layouts::{ - GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextToRef, LWEPlaintext, LWESecret, - prepared::GLWESecretExec, - }, - mod_switch_2n, +use crate::tfhe::blind_rotation::{ + BlincRotationExecute, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyPrepared, + CCGIBlindRotationFamily, CGGI, LookUpTable, cggi_blind_rotate_scratch_space, mod_switch_2n, }; -use crate::trait_families::{GLWEDecryptFamily, GLWESecretExecModuleFamily}; +use core::{ + layouts::{ + GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWECiphertextToRef, LWEPlaintext, LWESecret, + prepared::{GLWESecretPrepared, PrepareAlloc}, + }, + trait_families::{GLWEDecryptFamily, GLWESecretPreparedModuleFamily}, +}; -pub(crate) trait CGGITestModuleFamily = CCGIBlindRotationFamily - + GLWESecretExecModuleFamily - + GLWEDecryptFamily - + BlindRotationKeyCGGIExecLayoutFamily - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxAddScalarInplace - + VecZnxEncodeCoeffsi64 - + VecZnxRotateInplace - + VecZnxSwithcDegree - + VecZnxSub - + VmpPMatAlloc - + VmpPMatPrepare; -pub(crate) trait CGGITestScratchFamily = VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeVecZnxSliceImpl; - -pub(crate) fn blind_rotatio_test(module: &Module, n_lwe: usize, block_size: usize, extension_factor: usize) +pub fn test_blind_rotation(module: &Module, n_lwe: usize, block_size: usize, extension_factor: usize) where - Module: CGGITestModuleFamily, - B: CGGITestScratchFamily, + Module: CCGIBlindRotationFamily + + GLWESecretPreparedModuleFamily + + GLWEDecryptFamily + + VecZnxFillUniform + + VecZnxAddNormal + + VecZnxAddScalarInplace + + VecZnxRotateInplace + + VecZnxSwithcDegree + + VecZnxSub + + VmpPMatAlloc + + VmpPMatPrepare, + B: VecZnxDftAllocBytesImpl + + VecZnxBigAllocBytesImpl + + ScratchOwnedAllocImpl + + ScratchOwnedBorrowImpl + + TakeVecZnxDftImpl + + TakeVecZnxBigImpl + + TakeVecZnxDftSliceImpl + + ScratchAvailableImpl + + TakeVecZnxImpl + + TakeVecZnxSliceImpl, { let n: usize = module.n(); let basek: usize = 19; @@ -67,17 +63,17 @@ where let mut source_xe: Source = Source::new([2u8; 32]); let mut source_xa: Source = Source::new([1u8; 32]); + let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKey::generate_from_sk_scratch_space( + module, n, basek, k_brk, rank, + )); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_dft: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_glwe); + let sk_glwe_dft: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKeyCGGI::generate_from_sk_scratch_space( - module, n, basek, k_brk, rank, - )); - let mut scratch_br: ScratchOwned = ScratchOwned::::alloc(cggi_blind_rotate_scratch_space( module, n, @@ -90,9 +86,10 @@ where rank, )); - let mut brk: BlindRotationKeyCGGI> = BlindRotationKeyCGGI::alloc(n, n_lwe, basek, k_brk, rows_brk, rank); + let mut brk: BlindRotationKey, CGGI> = + BlindRotationKey::, CGGI>::alloc(n, n_lwe, basek, k_brk, rows_brk, rank); - brk.generate_from_sk( + brk.encrypt_sk( module, &sk_glwe_dft, &sk_lwe, @@ -109,7 +106,7 @@ where let x: i64 = 2; let bits: usize = 8; - module.encode_coeff_i64(basek, &mut pt_lwe.data, 0, bits, 0, x, bits); + pt_lwe.encode_i64(x, bits); lwe.encrypt_sk( module, @@ -130,9 +127,9 @@ where let mut res: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_res, rank); - let brk_exec: BlindRotationKeyCGGIExec, B> = BlindRotationKeyCGGIExec::from(module, &brk, scratch_br.borrow()); + let brk_prepared: BlindRotationKeyPrepared, CGGI, B> = brk.prepare_alloc(module, scratch.borrow()); - cggi_blind_rotate(module, &mut res, &lwe, &lut, &brk_exec, scratch_br.borrow()); + brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow()); let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_res); @@ -150,7 +147,7 @@ where let pt_want: i64 = (lwe_2n[0] + lwe_2n[1..] .iter() - .zip(sk_lwe.data.at(0, 0)) + .zip(sk_lwe.raw()) .map(|(x, y)| x * y) .sum::()) & (2 * lut.domain_size() - 1) as i64; diff --git a/bin_fhe/blind_rotation/tests/generic_lut.rs b/schemes/src/tfhe/blind_rotation/tests/generic_lut.rs similarity index 71% rename from bin_fhe/blind_rotation/tests/generic_lut.rs rename to schemes/src/tfhe/blind_rotation/tests/generic_lut.rs index 0486ed0..f8e46d3 100644 --- a/bin_fhe/blind_rotation/tests/generic_lut.rs +++ b/schemes/src/tfhe/blind_rotation/tests/generic_lut.rs @@ -1,23 +1,16 @@ use std::vec; use backend::hal::{ - api::{ - VecZnxCopy, VecZnxDecodeVeci64, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSwithcDegree, - }, + api::{VecZnxCopy, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSwithcDegree}, layouts::{Backend, Module}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::{DivRound, LookUpTable}; +use crate::tfhe::blind_rotation::{DivRound, LookUpTable}; -pub(crate) fn test_lut_standard(module: &Module) +pub fn test_lut_standard(module: &Module) where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwithcDegree - + VecZnxCopy - + VecZnxDecodeVeci64, + Module: VecZnxRotateInplace + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let n: usize = module.n(); @@ -42,7 +35,7 @@ where let step: usize = lut.domain_size().div_round(message_modulus); let mut lut_dec: Vec = vec![0i64; module.n()]; - module.decode_vec_i64(basek, &lut.data[0], 0, log_scale, &mut lut_dec); + lut.data[0].decode_vec_i64(basek, 0, log_scale, &mut lut_dec); (0..lut.domain_size()).step_by(step).for_each(|i| { (0..step).for_each(|_| { @@ -51,14 +44,9 @@ where }); } -pub(crate) fn test_lut_extended(module: &Module) +pub fn test_lut_extended(module: &Module) where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwithcDegree - + VecZnxCopy - + VecZnxDecodeVeci64, + Module: VecZnxRotateInplace + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let n: usize = module.n(); @@ -85,7 +73,7 @@ where let mut lut_dec: Vec = vec![0i64; module.n()]; (0..extension_factor).for_each(|ext| { - module.decode_vec_i64(basek, &lut.data[ext], 0, log_scale, &mut lut_dec); + lut.data[ext].decode_vec_i64(basek, 0, log_scale, &mut lut_dec); (0..module.n()).step_by(step).for_each(|i| { (0..step).for_each(|_| { assert_eq!(f[i / step] % message_modulus as i64, lut_dec[i]); diff --git a/schemes/src/tfhe/blind_rotation/tests/generic_serialization.rs b/schemes/src/tfhe/blind_rotation/tests/generic_serialization.rs new file mode 100644 index 0000000..f099bb0 --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/tests/generic_serialization.rs @@ -0,0 +1,15 @@ +use backend::hal::tests::serialization::test_reader_writer_interface; + +use crate::tfhe::blind_rotation::{BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyCompressed, CGGI}; + +#[test] +fn test_cggi_blind_rotation_key_serialization() { + let original: BlindRotationKey, CGGI> = BlindRotationKey::alloc(256, 64, 12, 54, 2, 2); + test_reader_writer_interface(original); +} + +#[test] +fn test_cggi_blind_rotation_key_compressed_serialization() { + let original: BlindRotationKeyCompressed, CGGI> = BlindRotationKeyCompressed::alloc(256, 64, 12, 54, 2, 2); + test_reader_writer_interface(original); +} diff --git a/bin_fhe/blind_rotation/tests/cpu_spqlios/fft64.rs b/schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/fft64.rs similarity index 75% rename from bin_fhe/blind_rotation/tests/cpu_spqlios/fft64.rs rename to schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/fft64.rs index f3ad51c..18cfe91 100644 --- a/bin_fhe/blind_rotation/tests/cpu_spqlios/fft64.rs +++ b/schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/fft64.rs @@ -3,8 +3,8 @@ use backend::{ implementation::cpu_spqlios::FFT64, }; -use crate::blind_rotation::tests::{ - generic_cggi::blind_rotatio_test, +use crate::tfhe::blind_rotation::tests::{ + generic_blind_rotation::test_blind_rotation, generic_lut::{test_lut_extended, test_lut_standard}, }; @@ -23,17 +23,17 @@ fn lut_extended() { #[test] fn standard() { let module: Module = Module::::new(512); - blind_rotatio_test(&module, 224, 1, 1); + test_blind_rotation(&module, 224, 1, 1); } #[test] fn block_binary() { let module: Module = Module::::new(512); - blind_rotatio_test(&module, 224, 7, 1); + test_blind_rotation(&module, 224, 7, 1); } #[test] fn block_binary_extended() { let module: Module = Module::::new(512); - blind_rotatio_test(&module, 224, 7, 2); + test_blind_rotation(&module, 224, 7, 2); } diff --git a/bin_fhe/blind_rotation/tests/cpu_spqlios/mod.rs b/schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/mod.rs similarity index 100% rename from bin_fhe/blind_rotation/tests/cpu_spqlios/mod.rs rename to schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/mod.rs diff --git a/schemes/src/tfhe/blind_rotation/tests/implementation/mod.rs b/schemes/src/tfhe/blind_rotation/tests/implementation/mod.rs new file mode 100644 index 0000000..f2bc1d4 --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/tests/implementation/mod.rs @@ -0,0 +1 @@ +mod cpu_spqlios; diff --git a/schemes/src/tfhe/blind_rotation/tests/mod.rs b/schemes/src/tfhe/blind_rotation/tests/mod.rs new file mode 100644 index 0000000..025de5b --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/tests/mod.rs @@ -0,0 +1,7 @@ +#[cfg(test)] +mod generic_serialization; +#[cfg(test)] +mod implementation; + +pub mod generic_blind_rotation; +pub mod generic_lut; diff --git a/schemes/src/tfhe/blind_rotation/utils.rs b/schemes/src/tfhe/blind_rotation/utils.rs new file mode 100644 index 0000000..2917a14 --- /dev/null +++ b/schemes/src/tfhe/blind_rotation/utils.rs @@ -0,0 +1,41 @@ +use backend::hal::{ + api::{SvpPrepare, ZnxInfos, ZnxViewMut}, + layouts::{Backend, DataMut, Module, ScalarZnx, SvpPPol}, +}; + +pub(crate) fn set_xai_plus_y( + module: &Module, + ai: usize, + y: i64, + res: &mut SvpPPol, + buf: &mut ScalarZnx, +) where + A: DataMut, + C: DataMut, + Module: SvpPrepare, +{ + 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; + } +} diff --git a/bin_fhe/circuit_bootstrapping/circuit_bootstrapping.rs b/schemes/src/tfhe/circuit_bootstrapping/circuit_bootstrapping.rs similarity index 58% rename from bin_fhe/circuit_bootstrapping/circuit_bootstrapping.rs rename to schemes/src/tfhe/circuit_bootstrapping/circuit_bootstrapping.rs index de4cd07..7589729 100644 --- a/bin_fhe/circuit_bootstrapping/circuit_bootstrapping.rs +++ b/schemes/src/tfhe/circuit_bootstrapping/circuit_bootstrapping.rs @@ -2,134 +2,28 @@ use std::{collections::HashMap, time::Instant, usize}; use backend::hal::{ api::{ - ScratchAvailable, TakeMatZnx, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, - TakeVecZnxSlice, VecZnxAddInplace, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAutomorphismInplace, VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxDftCopy, VecZnxDftToVecZnxBigTmpA, - VecZnxNegateInplace, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, - VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + ScratchAvailable, TakeMatZnx, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, + VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAutomorphismInplace, VecZnxBigSubSmallBInplace, VecZnxCopy, + VecZnxDftCopy, VecZnxDftToVecZnxBigTmpA, VecZnxNegateInplace, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, + VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, }, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, DataRef, Module, Scratch}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use sampling::source::Source; -use crate::{ - BlindRotationKeyCGGI, BlindRotationKeyCGGIExec, BlindRotationKeyCGGIExecLayoutFamily, CCGIBlindRotationFamily, GLWEOps, - Infos, LookUpTable, LookUpTableRotationDirection, TakeGGLWE, TakeGLWECt, cggi_blind_rotate, - layouts::{ - AutomorphismKey, GGSWCiphertext, GLWECiphertext, GLWESecret, GLWETensorKey, LWECiphertext, LWESecret, - prepared::{AutomorphismKeyExec, GLWESecretExec, GLWETensorKeyExec}, +use core::{GLWEOperations, TakeGGLWE, TakeGLWECt, layouts::Infos, trait_families::GLWETraceModuleFamily}; + +use core::layouts::{GGSWCiphertext, GLWECiphertext, LWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; + +use crate::tfhe::{ + blind_rotation::{ + BlincRotationExecute, BlindRotationAlgo, BlindRotationKeyPrepared, CCGIBlindRotationFamily, LookUpTable, + LookUpTableRotationDirection, }, + circuit_bootstrapping::{CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute}, }; -use crate::trait_families::{ - AutomorphismKeyEncryptSkFamily, GGSWEncryptSkFamily, GLWESecretExecModuleFamily, GLWETensorKeyEncryptSkFamily, - GLWETraceModuleFamily, -}; - -pub struct CircuitBootstrappingKeyCGGI { - pub(crate) brk: BlindRotationKeyCGGI, - pub(crate) tsk: GLWETensorKey>, - pub(crate) atk: HashMap>>, -} - -impl CircuitBootstrappingKeyCGGI> { - pub fn generate( - module: &Module, - basek: usize, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, - k_brk: usize, - rows_brk: usize, - k_trace: usize, - rows_trace: usize, - k_tsk: usize, - rows_tsk: usize, - source_xa: &mut Source, - source_xe: &mut Source, - sigma: f64, - scratch: &mut Scratch, - ) -> Self - where - Module: GGSWEncryptSkFamily - + GLWESecretExecModuleFamily - + VecZnxAddScalarInplace - + AutomorphismKeyEncryptSkFamily - + VecZnxAutomorphism - + VecZnxSwithcDegree - + GLWETensorKeyEncryptSkFamily, - DLwe: DataRef, - DGlwe: DataRef, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, - { - let mut auto_keys: HashMap>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(&module); - gal_els.iter().for_each(|gal_el| { - let mut key: AutomorphismKey> = - AutomorphismKey::alloc(sk_glwe.n(), basek, k_trace, rows_trace, 1, sk_glwe.rank()); - key.encrypt_sk( - &module, *gal_el, &sk_glwe, source_xa, source_xe, sigma, scratch, - ); - auto_keys.insert(*gal_el, key); - }); - - let sk_glwe_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_glwe); - - let mut brk: BlindRotationKeyCGGI> = BlindRotationKeyCGGI::alloc( - sk_glwe.n(), - sk_lwe.n(), - basek, - k_brk, - rows_brk, - sk_glwe.rank(), - ); - brk.generate_from_sk( - module, - &sk_glwe_exec, - sk_lwe, - source_xa, - source_xe, - sigma, - scratch, - ); - - let mut tsk: GLWETensorKey> = GLWETensorKey::alloc(sk_glwe.n(), basek, k_tsk, rows_tsk, 1, sk_glwe.rank()); - tsk.encrypt_sk(module, &sk_glwe, source_xa, source_xe, sigma, scratch); - - Self { - brk, - atk: auto_keys, - tsk, - } - } -} - -pub struct CircuitBootstrappingKeyCGGIExec { - pub(crate) brk: BlindRotationKeyCGGIExec, - pub(crate) tsk: GLWETensorKeyExec, B>, - pub(crate) atk: HashMap, B>>, -} - -impl CircuitBootstrappingKeyCGGIExec, B> { - pub fn from( - module: &Module, - other: &CircuitBootstrappingKeyCGGI, - scratch: &mut Scratch, - ) -> CircuitBootstrappingKeyCGGIExec, B> - where - Module: BlindRotationKeyCGGIExecLayoutFamily + VmpPMatAlloc + VmpPMatPrepare, - { - let brk: BlindRotationKeyCGGIExec, B> = BlindRotationKeyCGGIExec::from(module, &other.brk, scratch); - let tsk: GLWETensorKeyExec, B> = GLWETensorKeyExec::from(module, &other.tsk, scratch); - let mut atk: HashMap, B>> = HashMap::new(); - for (key, value) in &other.atk { - atk.insert(*key, AutomorphismKeyExec::from(module, value, scratch)); - } - CircuitBootstrappingKeyCGGIExec { brk, tsk, atk } - } -} - -pub trait CGGICircuitBootstrapFamily = VecZnxRotateInplace +pub trait CircuitBootstrapFamily = VecZnxRotateInplace + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes + CCGIBlindRotationFamily @@ -148,19 +42,9 @@ pub trait CGGICircuitBootstrapFamily = VecZnxRotateInplace + VecZnxAutomorphismInplace + VecZnxBigSubSmallBInplace; -pub fn circuit_bootstrap_to_constant_cggi( - module: &Module, - res: &mut GGSWCiphertext, - lwe: &LWECiphertext, - log_domain: usize, - extension_factor: usize, - key: &CircuitBootstrappingKeyCGGIExec, - scratch: &mut Scratch, -) where - DRes: DataMut, - DLwe: DataRef, - DBrk: DataRef, - Module: CGGICircuitBootstrapFamily, +impl CirtuitBootstrappingExecute for CircuitBootstrappingKeyPrepared +where + Module: CircuitBootstrapFamily, B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, Scratch: TakeVecZnx + TakeVecZnxDftSlice @@ -169,57 +53,55 @@ pub fn circuit_bootstrap_to_constant_cggi( + TakeMatZnx + ScratchAvailable + TakeVecZnxSlice, + BlindRotationKeyPrepared: BlincRotationExecute, { - circuit_bootstrap_core_cggi( - false, - module, - 0, - res, - lwe, - log_domain, - extension_factor, - key, - scratch, - ); + fn execute_to_constant( + &self, + module: &Module, + res: &mut GGSWCiphertext, + lwe: &LWECiphertext, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) { + circuit_bootstrap_core( + false, + module, + 0, + res, + lwe, + log_domain, + extension_factor, + self, + scratch, + ); + } + + fn execute_to_exponent( + &self, + module: &Module, + log_gap_out: usize, + res: &mut GGSWCiphertext, + lwe: &LWECiphertext, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) { + circuit_bootstrap_core( + true, + module, + log_gap_out, + res, + lwe, + log_domain, + extension_factor, + self, + scratch, + ); + } } -pub fn circuit_bootstrap_to_exponent_cggi( - module: &Module, - log_gap_out: usize, - res: &mut GGSWCiphertext, - lwe: &LWECiphertext, - log_domain: usize, - extension_factor: usize, - key: &CircuitBootstrappingKeyCGGIExec, - scratch: &mut Scratch, -) where - DRes: DataMut, - DLwe: DataRef, - DBrk: DataRef, - Module: CGGICircuitBootstrapFamily, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - Scratch: TakeVecZnx - + TakeVecZnxDftSlice - + TakeVecZnxBig - + TakeVecZnxDft - + TakeMatZnx - + ScratchAvailable - + TakeVecZnxSlice, -{ - circuit_bootstrap_core_cggi( - true, - module, - log_gap_out, - res, - lwe, - log_domain, - extension_factor, - key, - scratch, - ); -} - -pub fn circuit_bootstrap_core_cggi( +pub fn circuit_bootstrap_core( to_exponent: bool, module: &Module, log_gap_out: usize, @@ -227,26 +109,25 @@ pub fn circuit_bootstrap_core_cggi( lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, - key: &CircuitBootstrappingKeyCGGIExec, + key: &CircuitBootstrappingKeyPrepared, scratch: &mut Scratch, ) where DRes: DataMut, DLwe: DataRef, DBrk: DataRef, - Module: CGGICircuitBootstrapFamily, + Module: CircuitBootstrapFamily, B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - Scratch: TakeGGLWE - + TakeVecZnxDftSlice + Scratch: TakeVecZnxDftSlice + TakeVecZnxBig + TakeVecZnxDft + TakeVecZnx + ScratchAvailable - + TakeVecZnxSlice, + + TakeVecZnxSlice + + TakeMatZnx, + BlindRotationKeyPrepared: BlincRotationExecute, { #[cfg(debug_assertions)] { - use crate::Infos; - assert_eq!(res.n(), key.brk.n()); assert_eq!(lwe.basek(), key.brk.basek()); assert_eq!(res.basek(), key.brk.basek()); @@ -287,8 +168,10 @@ pub fn circuit_bootstrap_core_cggi( let (mut tmp_gglwe, scratch2) = scratch1.take_gglwe(n, basek, k, rows, 1, rank, rank); let now: Instant = Instant::now(); - cggi_blind_rotate(module, &mut res_glwe, &lwe, &lut, &key.brk, scratch2); - println!("cggi_blind_rotate: {} ms", now.elapsed().as_millis()); + + key.brk.execute(module, &mut res_glwe, &lwe, &lut, scratch2); + + println!("blind_rotate: {} ms", now.elapsed().as_millis()); let gap: usize = 2 * lut.drift / lut.extension_factor(); @@ -332,12 +215,12 @@ fn post_process( log_gap_in: usize, log_gap_out: usize, log_domain: usize, - auto_keys: &HashMap, B>>, + auto_keys: &HashMap, B>>, scratch: &mut Scratch, ) where DataRes: DataMut, DataA: DataRef, - Module: CGGICircuitBootstrapFamily, + Module: CircuitBootstrapFamily, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, { let log_n: usize = module.log_n(); @@ -385,10 +268,10 @@ pub fn pack( module: &Module, cts: &mut HashMap>, log_gap_out: usize, - auto_keys: &HashMap, B>>, + auto_keys: &HashMap, B>>, scratch: &mut Scratch, ) where - Module: CGGICircuitBootstrapFamily, + Module: CircuitBootstrapFamily, Scratch: TakeVecZnx + TakeVecZnxDft + ScratchAvailable, { let log_n: usize = module.log_n(); @@ -402,7 +285,7 @@ pub fn pack( let t = 16.min(1 << (log_n - 1 - i)); - let auto_key: &AutomorphismKeyExec, B>; + let auto_key: &GGLWEAutomorphismKeyPrepared, B>; if i == 0 { auto_key = auto_keys.get(&-1).unwrap() } else { @@ -444,10 +327,10 @@ fn combine( a: Option<&mut GLWECiphertext>, b: Option<&mut GLWECiphertext>, i: usize, - auto_key: &AutomorphismKeyExec, + auto_key: &GGLWEAutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: CGGICircuitBootstrapFamily, + Module: CircuitBootstrapFamily, Scratch: TakeVecZnx + TakeVecZnxDft + ScratchAvailable, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) diff --git a/schemes/src/tfhe/circuit_bootstrapping/key.rs b/schemes/src/tfhe/circuit_bootstrapping/key.rs new file mode 100644 index 0000000..aeb58bb --- /dev/null +++ b/schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -0,0 +1,150 @@ +use core::layouts::{ + GGLWEAutomorphismKey, GGLWETensorKey, GLWECiphertext, GLWESecret, LWESecret, + prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, +}; +use std::{collections::HashMap, usize}; + +use backend::hal::{ + api::{ + ScratchAvailable, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddScalarInplace, + VecZnxAutomorphism, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, + }, + layouts::{Backend, Data, DataRef, Module, Scratch}, +}; +use sampling::source::Source; + +use core::trait_families::{ + GGLWEAutomorphismKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GLWESecretPreparedModuleFamily, +}; + +use crate::tfhe::blind_rotation::{ + BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyPrepared, +}; + +pub trait CircuitBootstrappingKeyEncryptSk { + fn encrypt_sk( + module: &Module, + basek: usize, + sk_lwe: &LWESecret, + sk_glwe: &GLWESecret, + k_brk: usize, + rows_brk: usize, + k_trace: usize, + rows_trace: usize, + k_tsk: usize, + rows_tsk: usize, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) -> Self + where + DLwe: DataRef, + DGlwe: DataRef; +} + +pub struct CircuitBootstrappingKey { + pub(crate) brk: BlindRotationKey, + pub(crate) tsk: GGLWETensorKey>, + pub(crate) atk: HashMap>>, +} + +impl CircuitBootstrappingKeyEncryptSk for CircuitBootstrappingKey, BRA> +where + BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + Module: GGSWEncryptSkFamily + + GLWESecretPreparedModuleFamily + + VecZnxAddScalarInplace + + GGLWEAutomorphismKeyEncryptSkFamily + + VecZnxAutomorphism + + VecZnxSwithcDegree + + GGLWETensorKeyEncryptSkFamily, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, +{ + fn encrypt_sk( + module: &Module, + basek: usize, + sk_lwe: &LWESecret, + sk_glwe: &GLWESecret, + k_brk: usize, + rows_brk: usize, + k_trace: usize, + rows_trace: usize, + k_tsk: usize, + rows_tsk: usize, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) -> Self + where + DLwe: DataRef, + DGlwe: DataRef, + { + let mut auto_keys: HashMap>> = HashMap::new(); + let gal_els: Vec = GLWECiphertext::trace_galois_elements(&module); + gal_els.iter().for_each(|gal_el| { + let mut key: GGLWEAutomorphismKey> = + GGLWEAutomorphismKey::alloc(sk_glwe.n(), basek, k_trace, rows_trace, 1, sk_glwe.rank()); + key.encrypt_sk( + &module, *gal_el, &sk_glwe, source_xa, source_xe, sigma, scratch, + ); + auto_keys.insert(*gal_el, key); + }); + + let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch); + + let mut brk: BlindRotationKey, BRA> = BlindRotationKey::, BRA>::alloc( + sk_glwe.n(), + sk_lwe.n(), + basek, + k_brk, + rows_brk, + sk_glwe.rank(), + ); + + brk.encrypt_sk( + module, + &sk_glwe_prepared, + sk_lwe, + source_xa, + source_xe, + sigma, + scratch, + ); + + let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(sk_glwe.n(), basek, k_tsk, rows_tsk, 1, sk_glwe.rank()); + tsk.encrypt_sk(module, &sk_glwe, source_xa, source_xe, sigma, scratch); + + Self { + brk, + atk: auto_keys, + tsk, + } + } +} + +pub struct CircuitBootstrappingKeyPrepared { + pub(crate) brk: BlindRotationKeyPrepared, + pub(crate) tsk: GGLWETensorKeyPrepared, B>, + pub(crate) atk: HashMap, B>>, +} + +impl PrepareAlloc, BRA, B>> + for CircuitBootstrappingKey +where + Module: VmpPMatAlloc + VmpPMatPrepare, + BlindRotationKey: PrepareAlloc, BRA, B>>, + GGLWETensorKey: PrepareAlloc, B>>, + GGLWEAutomorphismKey: PrepareAlloc, B>>, +{ + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> CircuitBootstrappingKeyPrepared, BRA, B> { + let brk: BlindRotationKeyPrepared, BRA, B> = self.brk.prepare_alloc(module, scratch); + let tsk: GGLWETensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); + let mut atk: HashMap, B>> = HashMap::new(); + for (key, value) in &self.atk { + atk.insert(*key, value.prepare_alloc(module, scratch)); + } + CircuitBootstrappingKeyPrepared { brk, tsk, atk } + } +} diff --git a/schemes/src/tfhe/circuit_bootstrapping/mod.rs b/schemes/src/tfhe/circuit_bootstrapping/mod.rs new file mode 100644 index 0000000..937de24 --- /dev/null +++ b/schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -0,0 +1,33 @@ +mod circuit_bootstrapping; +mod key; +pub mod tests; + +pub use circuit_bootstrapping::*; +pub use key::*; + +use core::layouts::{GGSWCiphertext, LWECiphertext}; + +use backend::hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; + +pub trait CirtuitBootstrappingExecute { + fn execute_to_constant( + &self, + module: &Module, + res: &mut GGSWCiphertext, + lwe: &LWECiphertext, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ); + + fn execute_to_exponent( + &self, + module: &Module, + log_gap_out: usize, + res: &mut GGSWCiphertext, + lwe: &LWECiphertext, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ); +} diff --git a/bin_fhe/circuit_bootstrapping/test_fft64/circuit_bootstrapping.rs b/schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs similarity index 63% rename from bin_fhe/circuit_bootstrapping/test_fft64/circuit_bootstrapping.rs rename to schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index 0410ec7..349ba2c 100644 --- a/bin_fhe/circuit_bootstrapping/test_fft64/circuit_bootstrapping.rs +++ b/schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -1,63 +1,61 @@ use std::time::Instant; -use backend::{ - hal::{ - api::{ - ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, - VecZnxEncodeCoeffsi64, VecZnxFillUniform, VecZnxNormalizeInplace, VecZnxRotateInplace, VecZnxStd, VecZnxSwithcDegree, - VmpPMatAlloc, VmpPMatPrepare, ZnxView, ZnxViewMut, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, - TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, - }, +use backend::hal::{ + api::{ + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpPPolAlloc, SvpPrepare, VecZnxAddNormal, VecZnxAddScalarInplace, + VecZnxAutomorphism, VecZnxFillUniform, VecZnxNormalizeInplace, VecZnxRotateInplace, VecZnxSwithcDegree, VmpPMatAlloc, + VmpPMatPrepare, ZnxView, ZnxViewMut, + }, + layouts::{Backend, Module, ScalarZnx, ScratchOwned}, + oep::{ + ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, + TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, }, - implementation::cpu_spqlios::FFT64, }; use sampling::source::Source; -use crate::{ - BlindRotationKeyCGGIExecLayoutFamily, - circuit_bootstrapping::circuit_bootstrapping::{ - CGGICircuitBootstrapFamily, CircuitBootstrappingKeyCGGI, CircuitBootstrappingKeyCGGIExec, - circuit_bootstrap_to_constant_cggi, circuit_bootstrap_to_exponent_cggi, +use crate::tfhe::{ + blind_rotation::{ + BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, + BlindRotationKeyPrepared, }, - layouts::{ - GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, - prepared::{GGSWCiphertextExec, GLWESecretExec}, + circuit_bootstrapping::{ + CircuitBootstrapFamily, CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyPrepared, + CirtuitBootstrappingExecute, }, }; -use crate::trait_families::{ - AutomorphismKeyEncryptSkFamily, GGSWAssertNoiseFamily, GGSWEncryptSkFamily, GLWEDecryptFamily, GLWETensorKeyEncryptSkFamily, +use core::{ + layouts::prepared::PrepareAlloc, + trait_families::{ + GGLWEAutomorphismKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWAssertNoiseFamily, GGSWEncryptSkFamily, + GLWEDecryptFamily, + }, }; -#[test] -fn test_to_exponent() { - let module: Module = Module::::new(256); - to_exponent(&module); -} +use core::layouts::{ + GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, + prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, +}; -fn to_exponent(module: &Module) +pub fn test_circuit_bootstrapping_to_exponent(module: &Module) where - Module: VecZnxEncodeCoeffsi64 - + VecZnxFillUniform + Module: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace + GGSWEncryptSkFamily + VecZnxAddScalarInplace - + AutomorphismKeyEncryptSkFamily + + GGLWEAutomorphismKeyEncryptSkFamily + VecZnxAutomorphism + VecZnxSwithcDegree - + GLWETensorKeyEncryptSkFamily - + BlindRotationKeyCGGIExecLayoutFamily - + CGGICircuitBootstrapFamily + + GGLWETensorKeyEncryptSkFamily + + CircuitBootstrapFamily + GLWEDecryptFamily + GGSWAssertNoiseFamily - + VecZnxStd + VmpPMatAlloc - + VmpPMatPrepare, + + VmpPMatPrepare + + SvpPrepare + + SvpPPolAlloc, B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + TakeVecZnxDftImpl @@ -69,6 +67,9 @@ where + TakeVecZnxDftSliceImpl + TakeMatZnxImpl + TakeVecZnxSliceImpl, + BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, + BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, + BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, { let n: usize = module.n(); let basek: usize = 17; @@ -102,14 +103,14 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_glwe); + let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(basek, k_lwe_pt); - module.encode_coeff_i64(basek, &mut pt_lwe.data, 0, k_lwe_pt + 2, 0, data, k_lwe_pt); + pt_lwe.encode_i64(data, k_lwe_pt + 2); - println!("pt_lwe: {}", pt_lwe.data); + println!("pt_lwe: {}", pt_lwe); let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct); ct_lwe.encrypt_sk( @@ -122,7 +123,7 @@ where ); let now: Instant = Instant::now(); - let cbt_key: CircuitBootstrappingKeyCGGI> = CircuitBootstrappingKeyCGGI::generate( + let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( module, basek, &sk_lwe, @@ -147,18 +148,16 @@ where let log_gap_out = 1; - let cbt_exec: CircuitBootstrappingKeyCGGIExec, B> = - CircuitBootstrappingKeyCGGIExec::from(module, &cbt_key, scratch.borrow()); + let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); let now: Instant = Instant::now(); - circuit_bootstrap_to_exponent_cggi( + cbt_prepared.execute_to_exponent( module, log_gap_out, &mut res, &ct_lwe, k_lwe_pt, extension_factor, - &cbt_exec, scratch.borrow(), ); println!("CBT: {} ms", now.elapsed().as_millis()); @@ -168,7 +167,7 @@ where pt_ggsw.at_mut(0, 0)[0] = 1; module.vec_znx_rotate_inplace(data * (1 << log_gap_out), &mut pt_ggsw.as_vec_znx_mut(), 0); - res.print_noise(module, &sk_glwe_exec, &pt_ggsw); + res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); let k_glwe: usize = k_ggsw_res; @@ -179,19 +178,19 @@ where ct_glwe.encrypt_sk( module, &pt_glwe, - &sk_glwe_exec, + &sk_glwe_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let res_exec: GGSWCiphertextExec, B> = GGSWCiphertextExec::from(module, &res, scratch.borrow()); + let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); - ct_glwe.external_product_inplace(module, &res_exec, scratch.borrow()); + ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_glwe); - ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_exec, scratch.borrow()); + ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless. let mut pt_want: Vec = vec![0i64; module.n()]; @@ -199,31 +198,24 @@ where assert_eq!(pt_res.data.at(0, 0), pt_want); } -#[test] -fn test_to_constant() { - let module: Module = Module::::new(256); - to_constant(&module); -} - -fn to_constant(module: &Module) +pub fn test_circuit_bootstrapping_to_constant(module: &Module) where - Module: VecZnxEncodeCoeffsi64 - + VecZnxFillUniform + Module: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace + GGSWEncryptSkFamily + VecZnxAddScalarInplace - + AutomorphismKeyEncryptSkFamily + + GGLWEAutomorphismKeyEncryptSkFamily + VecZnxAutomorphism + VecZnxSwithcDegree - + GLWETensorKeyEncryptSkFamily - + BlindRotationKeyCGGIExecLayoutFamily - + CGGICircuitBootstrapFamily + + GGLWETensorKeyEncryptSkFamily + + CircuitBootstrapFamily + GLWEDecryptFamily + GGSWAssertNoiseFamily - + VecZnxStd + VmpPMatAlloc - + VmpPMatPrepare, + + VmpPMatPrepare + + SvpPrepare + + SvpPPolAlloc, B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + TakeVecZnxDftImpl @@ -235,6 +227,9 @@ where + TakeVecZnxDftSliceImpl + TakeMatZnxImpl + TakeVecZnxSliceImpl, + BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, + BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, + BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, { let n = module.n(); let basek: usize = 14; @@ -268,14 +263,14 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk_glwe); + let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(basek, k_lwe_pt); - module.encode_coeff_i64(basek, &mut pt_lwe.data, 0, k_lwe_pt + 2, 0, data, k_lwe_pt); + pt_lwe.encode_i64(data, k_lwe_pt + 2); - println!("pt_lwe: {}", pt_lwe.data); + println!("pt_lwe: {}", pt_lwe); let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct); ct_lwe.encrypt_sk( @@ -288,7 +283,7 @@ where ); let now: Instant = Instant::now(); - let cbt_key: CircuitBootstrappingKeyCGGI> = CircuitBootstrappingKeyCGGI::generate( + let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( module, basek, &sk_lwe, @@ -311,17 +306,15 @@ where let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(n, basek, k_ggsw_res, rows_ggsw_res, 1, rank); - let cbt_exec: CircuitBootstrappingKeyCGGIExec, B> = - CircuitBootstrappingKeyCGGIExec::from(module, &cbt_key, scratch.borrow()); + let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); let now: Instant = Instant::now(); - circuit_bootstrap_to_constant_cggi( + cbt_prepared.execute_to_constant( module, &mut res, &ct_lwe, k_lwe_pt, extension_factor, - &cbt_exec, scratch.borrow(), ); println!("CBT: {} ms", now.elapsed().as_millis()); @@ -330,7 +323,7 @@ where let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); pt_ggsw.at_mut(0, 0)[0] = data; - res.print_noise(module, &sk_glwe_exec, &pt_ggsw); + res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); let k_glwe: usize = k_ggsw_res; @@ -341,19 +334,19 @@ where ct_glwe.encrypt_sk( module, &pt_glwe, - &sk_glwe_exec, + &sk_glwe_prepared, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - let res_exec: GGSWCiphertextExec, B> = GGSWCiphertextExec::from(module, &res, scratch.borrow()); + let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); - ct_glwe.external_product_inplace(module, &res_exec, scratch.borrow()); + ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_glwe); - ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_exec, scratch.borrow()); + ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless. let mut pt_want: Vec = vec![0i64; module.n()]; diff --git a/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs b/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs new file mode 100644 index 0000000..be1fd95 --- /dev/null +++ b/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs @@ -0,0 +1,23 @@ +use backend::{ + hal::{api::ModuleNew, layouts::Module}, + implementation::cpu_spqlios::FFT64, +}; + +use crate::tfhe::{ + blind_rotation::CGGI, + circuit_bootstrapping::tests::circuit_bootstrapping::{ + test_circuit_bootstrapping_to_constant, test_circuit_bootstrapping_to_exponent, + }, +}; + +#[test] +fn test_to_constant() { + let module: Module = Module::::new(256); + test_circuit_bootstrapping_to_constant::(&module); +} + +#[test] +fn test_to_exponent() { + let module: Module = Module::::new(256); + test_circuit_bootstrapping_to_exponent::(&module); +} diff --git a/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs b/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs new file mode 100644 index 0000000..aebaafb --- /dev/null +++ b/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs @@ -0,0 +1 @@ +mod fft64; diff --git a/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs b/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs new file mode 100644 index 0000000..f2bc1d4 --- /dev/null +++ b/schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs @@ -0,0 +1 @@ +mod cpu_spqlios; diff --git a/schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs b/schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs new file mode 100644 index 0000000..f9bc7d9 --- /dev/null +++ b/schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs @@ -0,0 +1,4 @@ +pub mod circuit_bootstrapping; + +#[cfg(test)] +mod implementation; diff --git a/schemes/src/tfhe/mod.rs b/schemes/src/tfhe/mod.rs new file mode 100644 index 0000000..f00b53d --- /dev/null +++ b/schemes/src/tfhe/mod.rs @@ -0,0 +1,2 @@ +pub mod blind_rotation; +pub mod circuit_bootstrapping;