use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use std::marker::PhantomData; use poulpy_core::{ Distribution, layouts::{ GGSW, GGSWInfos, LWESecret, compressed::GGSWCiphertextCompressed, prepared::{GGSWPrepared, GLWESecretPrepared}, }, }; use crate::tfhe::blind_rotation::{ BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyCompressed, BlindRotationKeyEncryptSk, BlindRotationKeyInfos, BlindRotationKeyPrepared, BlindRotationKeyPreparedAlloc, CGGI, }; impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { fn alloc(infos: &A) -> Self where A: BlindRotationKeyInfos, { let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); for _ in 0..infos.n_lwe().as_usize() { data.push(GGSW::alloc(infos)); } Self { keys: data, dist: Distribution::NONE, _phantom: PhantomData, } } } impl BlindRotationKey, CGGI> { pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { GGSW::encrypt_sk_scratch_space(module, infos) } } impl BlindRotationKeyEncryptSk for BlindRotationKey where Module: VecZnxAddScalarInplace + VecZnxDftAllocBytes + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxNormalizeTmpBytes + VecZnxFillUniform + VecZnxSubInplace + VecZnxAddInplace + VecZnxNormalizeInplace + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, 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, scratch: &mut Scratch, ) where DataSkGLWE: DataRef, DataSkLWE: DataRef, { #[cfg(debug_assertions)] { use poulpy_core::layouts::{GLWEInfos, LWEInfos}; assert_eq!(self.keys.len() as u32, sk_lwe.n()); assert!(sk_glwe.n() <= module.n() as u32); assert_eq!(sk_glwe.rank(), self.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().into(), 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, scratch); }); } } impl BlindRotationKeyPreparedAlloc for BlindRotationKeyPrepared, CGGI, B> where Module: VmpPMatAlloc + VmpPrepare, { fn alloc(module: &Module, infos: &A) -> Self where A: BlindRotationKeyInfos, { let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc(module, infos))); Self { data, dist: Distribution::NONE, x_pow_a: None, _phantom: PhantomData, } } } impl BlindRotationKeyCompressed, CGGI> { pub fn alloc(infos: &A) -> Self where A: BlindRotationKeyInfos, { let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextCompressed::alloc(infos))); Self { keys: data, dist: Distribution::NONE, _phantom: PhantomData, } } pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, infos) } } impl BlindRotationKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, sk_glwe: &GLWESecretPrepared, sk_lwe: &LWESecret, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where DataSkGLWE: DataRef, DataSkLWE: DataRef, Module: VecZnxAddScalarInplace + VecZnxDftAllocBytes + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxNormalizeTmpBytes + VecZnxFillUniform + VecZnxSubInplace + VecZnxAddInplace + VecZnxNormalizeInplace + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, { #[cfg(debug_assertions)] { use poulpy_core::layouts::{GLWEInfos, LWEInfos}; assert_eq!(self.n_lwe(), sk_lwe.n()); assert!(sk_glwe.n() <= module.n() as u32); assert_eq!(sk_glwe.rank(), self.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().into(), 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, scratch, ); }); } }