use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, LWEInfos, Rank, compressed::GGLWETensorKeyCompressed, prepared::Prepare, }, }; impl GGLWETensorKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { GGLWETensorKey::encrypt_sk_scratch_space(module, infos) } } impl GGLWETensorKeyCompressed { pub fn encrypt_sk( &mut self, module: &Module, sk: &GLWESecret, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where Module: SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxDftAllocBytes + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxNormalizeTmpBytes + VecZnxFillUniform + VecZnxSubInplace + VecZnxAddInplace + VecZnxNormalizeInplace + VecZnxAddNormal + VecZnxNormalize + VecZnxSub + VecZnxSwitchRing + VecZnxAddScalarInplace + SvpPrepare + SvpPPolAllocBytes + SvpPPolAlloc, Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretPrepared + ScratchAvailable + TakeVecZnx + TakeVecZnxBig, { #[cfg(debug_assertions)] { assert_eq!(self.rank_out(), sk.rank()); assert_eq!(self.n(), sk.n()); } let n: usize = sk.n().into(); let rank: usize = self.rank_out().into(); let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), self.rank_out()); sk_dft_prep.prepare(module, sk, scratch_1); let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n, rank, 1); for i in 0..rank { module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); } let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n, 1, 1); let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(sk.n(), Rank(1)); let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(n, 1, 1); let mut source_xa: Source = Source::new(seed_xa); for i in 0..rank { for j in i..rank { module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); module.vec_znx_big_normalize( self.base2k().into(), &mut sk_ij.data.as_vec_znx_mut(), 0, self.base2k().into(), &sk_ij_big, 0, scratch_5, ); let (seed_xa_tmp, _) = source_xa.branch(); self.at_mut(i, j) .encrypt_sk(module, &sk_ij, sk, seed_xa_tmp, source_xe, scratch_5); } } } }