use base2k::{ Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftScratch, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos, ZnxZero, }; use sampling::source::Source; use crate::{ elem::{GetRow, Infos, MatZnxDftProducts, SetRow}, keys::SecretKeyDft, rlwe::{RLWECt, RLWECtDft, RLWEPt}, utils::derive_size, }; pub struct GRLWECt { pub data: MatZnxDft, pub log_base2k: usize, pub log_k: usize, } impl GRLWECt, B> { pub fn new(module: &Module, log_base2k: usize, log_k: usize, rows: usize) -> Self { Self { data: module.new_mat_znx_dft(rows, 1, 2, derive_size(log_base2k, log_k)), log_base2k: log_base2k, log_k: log_k, } } } impl GRLWECt where MatZnxDft: MatZnxDftToRef, { pub fn get_row(&self, module: &Module, row_i: usize, res: &mut RLWECtDft) where VecZnxDft: VecZnxDftToMut, { module.vmp_extract_row(res, self, row_i, 0); } } impl GRLWECt where MatZnxDft: MatZnxDftToMut, { pub fn set_row(&mut self, module: &Module, row_i: usize, a: &RLWECtDft) where VecZnxDft: VecZnxDftToRef, { module.vmp_prepare_row(self, row_i, 0, a); } } impl Infos for GRLWECt { type Inner = MatZnxDft; fn inner(&self) -> &Self::Inner { &self.data } fn log_base2k(&self) -> usize { self.log_base2k } fn log_k(&self) -> usize { self.log_k } } impl MatZnxDftToMut for GRLWECt where MatZnxDft: MatZnxDftToMut, { fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> { self.data.to_mut() } } impl MatZnxDftToRef for GRLWECt where MatZnxDft: MatZnxDftToRef, { fn to_ref(&self) -> MatZnxDft<&[u8], B> { self.data.to_ref() } } impl GRLWECt, FFT64> { pub fn encrypt_sk_scratch_space(module: &Module, size: usize) -> usize { RLWECt::encrypt_sk_scratch_space(module, size) + module.bytes_of_vec_znx(2, size) + module.bytes_of_vec_znx(1, size) + module.bytes_of_vec_znx_dft(2, size) } pub fn mul_rlwe_scratch_space(module: &Module, res_size: usize, a_size: usize, grlwe_size: usize) -> usize { module.bytes_of_vec_znx_dft(2, grlwe_size) + (module.vec_znx_big_normalize_tmp_bytes() | (module.vmp_apply_tmp_bytes(res_size, a_size, a_size, 1, 2, grlwe_size) + module.bytes_of_vec_znx_dft(1, a_size))) } pub fn mul_rlwe_inplace_scratch_space(module: &Module, res_size: usize, grlwe_size: usize) -> usize { Self::mul_rlwe_scratch_space(module, res_size, res_size, grlwe_size) } pub fn mul_rlwe_dft_scratch_space(module: &Module, res_size: usize, a_size: usize, grlwe_size: usize) -> usize { (Self::mul_rlwe_scratch_space(module, res_size, a_size, grlwe_size) | module.vec_znx_idft_tmp_bytes()) + module.bytes_of_vec_znx(2, a_size) + module.bytes_of_vec_znx(2, res_size) } pub fn mul_rlwe_dft_inplace_scratch_space(module: &Module, res_size: usize, grlwe_size: usize) -> usize { (Self::mul_rlwe_inplace_scratch_space(module, res_size, grlwe_size) | module.vec_znx_idft_tmp_bytes()) + module.bytes_of_vec_znx(2, res_size) } pub fn mul_grlwe_scratch_space(module: &Module, res_size: usize, a_size: usize, grlwe_size: usize) -> usize { Self::mul_rlwe_dft_inplace_scratch_space(module, res_size, grlwe_size) + module.bytes_of_vec_znx_dft(2, a_size) } pub fn mul_grlwe_inplace_scratch_space(module: &Module, res_size: usize, a_size: usize, grlwe_size: usize) -> usize { Self::mul_rlwe_dft_inplace_scratch_space(module, res_size, grlwe_size) + module.bytes_of_vec_znx_dft(2, a_size) } } pub fn encrypt_grlwe_sk( module: &Module, ct: &mut GRLWECt, pt: &ScalarZnx

, sk_dft: &SecretKeyDft, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, bound: f64, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToMut, ScalarZnx

: ScalarZnxToRef, ScalarZnxDft: ScalarZnxDftToRef, { let rows: usize = ct.rows(); let size: usize = ct.size(); let log_base2k: usize = ct.log_base2k(); let (tmp_znx_pt, scrach_1) = scratch.tmp_vec_znx(module, 1, size); let (tmp_znx_ct, scrach_2) = scrach_1.tmp_vec_znx(module, 2, size); let (mut vec_znx_dft_ct, scratch_3) = scrach_2.tmp_vec_znx_dft(module, 2, size); let mut vec_znx_pt: RLWEPt<&mut [u8]> = RLWEPt { data: tmp_znx_pt, log_base2k: log_base2k, log_k: ct.log_k(), }; let mut vec_znx_ct: RLWECt<&mut [u8]> = RLWECt { data: tmp_znx_ct, log_base2k: log_base2k, log_k: ct.log_k(), }; (0..rows).for_each(|row_i| { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt module.vec_znx_add_scalar_inplace(&mut vec_znx_pt, 0, row_i, pt, 0); module.vec_znx_normalize_inplace(log_base2k, &mut vec_znx_pt, 0, scratch_3); // rlwe encrypt of vec_znx_pt into vec_znx_ct vec_znx_ct.encrypt_sk( module, Some(&vec_znx_pt), sk_dft, source_xa, source_xe, sigma, bound, scratch_3, ); vec_znx_pt.data.zero(); // zeroes for next iteration // Switch vec_znx_ct into DFT domain module.vec_znx_dft(&mut vec_znx_dft_ct, 0, &vec_znx_ct, 0); module.vec_znx_dft(&mut vec_znx_dft_ct, 1, &vec_znx_ct, 1); // Stores vec_znx_dft_ct into thw i-th row of the MatZnxDft module.vmp_prepare_row(ct, row_i, 0, &vec_znx_dft_ct); }); } impl GRLWECt { pub fn encrypt_sk( &mut self, module: &Module, pt: &ScalarZnx

, sk_dft: &SecretKeyDft, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, bound: f64, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToMut, ScalarZnx

: ScalarZnxToRef, ScalarZnxDft: ScalarZnxDftToRef, { encrypt_grlwe_sk( module, self, pt, sk_dft, source_xa, source_xe, sigma, bound, scratch, ) } pub fn mul_rlwe(&self, module: &Module, res: &mut RLWECt, a: &RLWECt, scratch: &mut Scratch) where MatZnxDft: MatZnxDftToRef, VecZnx: VecZnxToMut, VecZnx: VecZnxToRef, { MatZnxDftProducts::mul_rlwe(self, module, res, a, scratch); } pub fn mul_rlwe_inplace(&self, module: &Module, res: &mut RLWECt, scratch: &mut Scratch) where MatZnxDft: MatZnxDftToRef + ZnxInfos, VecZnx: VecZnxToMut + VecZnxToRef, { MatZnxDftProducts::mul_rlwe_inplace(self, module, res, scratch); } pub fn mul_rlwe_dft( &self, module: &Module, res: &mut RLWECtDft, a: &RLWECtDft, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToRef + ZnxInfos, VecZnxDft: VecZnxDftToMut + VecZnxDftToRef + ZnxInfos, VecZnxDft: VecZnxDftToRef + ZnxInfos, { MatZnxDftProducts::mul_rlwe_dft(self, module, res, a, scratch); } pub fn mul_rlwe_dft_inplace(&self, module: &Module, res: &mut RLWECtDft, scratch: &mut Scratch) where MatZnxDft: MatZnxDftToRef + ZnxInfos, VecZnxDft: VecZnxDftToRef + VecZnxDftToMut, { MatZnxDftProducts::mul_rlwe_dft_inplace(self, module, res, scratch); } pub fn mul_grlwe( &self, module: &Module, res: &mut GRLWECt, a: &GRLWECt, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToRef + ZnxInfos, MatZnxDft: MatZnxDftToMut + MatZnxDftToRef + ZnxInfos, MatZnxDft: MatZnxDftToRef + ZnxInfos, { MatZnxDftProducts::mul_grlwe(self, module, res, a, scratch); } pub fn mul_grlwe_inplace(&self, module: &Module, res: &mut R, scratch: &mut Scratch) where MatZnxDft: MatZnxDftToRef + ZnxInfos, R: GetRow + SetRow + Infos, { MatZnxDftProducts::mul_grlwe_inplace(self, module, res, scratch); } } impl GetRow for GRLWECt where MatZnxDft: MatZnxDftToRef, { fn get_row(&self, module: &Module, row_i: usize, col_j: usize, res: &mut RLWECtDft) where VecZnxDft: VecZnxDftToMut, { #[cfg(debug_assertions)] { assert_eq!(col_j, 0); } module.vmp_extract_row(res, self, row_i, col_j); } } impl SetRow for GRLWECt where MatZnxDft: MatZnxDftToMut, { fn set_row(&mut self, module: &Module, row_i: usize, col_j: usize, a: &RLWECtDft) where VecZnxDft: VecZnxDftToRef, { #[cfg(debug_assertions)] { assert_eq!(col_j, 0); } module.vmp_prepare_row(self, row_i, col_j, a); } } impl MatZnxDftProducts, C> for GRLWECt where MatZnxDft: MatZnxDftToRef + ZnxInfos, { fn mul_rlwe(&self, module: &Module, res: &mut RLWECt, a: &RLWECt, scratch: &mut Scratch) where MatZnxDft: MatZnxDftToRef, VecZnx: VecZnxToMut, VecZnx: VecZnxToRef, { let log_base2k: usize = self.log_base2k(); #[cfg(debug_assertions)] { assert_eq!(res.log_base2k(), log_base2k); assert_eq!(a.log_base2k(), log_base2k); assert_eq!(self.n(), module.n()); assert_eq!(res.n(), module.n()); assert_eq!(a.n(), module.n()); } let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, 2, self.size()); // Todo optimise { let (mut a1_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, 1, a.size()); module.vec_znx_dft(&mut a1_dft, 0, a, 1); module.vmp_apply(&mut res_dft, &a1_dft, self, scratch2); } let mut res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft); module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0); module.vec_znx_big_normalize(log_base2k, res, 0, &res_big, 0, scratch1); module.vec_znx_big_normalize(log_base2k, res, 1, &res_big, 1, scratch1); } }