use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; use crate::{ TakeGLWESecretPrepared, layouts::{GGLWECiphertext, GLWESecret, compressed::GGLWESwitchingKeyCompressed, prepared::GLWESecretPrepared}, }; impl GGLWESwitchingKeyCompressed> { pub fn encrypt_sk_scratch_space( module: &Module, basek: usize, k: usize, rank_in: usize, rank_out: usize, ) -> usize where Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { (GGLWECiphertext::encrypt_sk_scratch_space(module, basek, k) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), rank_in) + GLWESecretPrepared::bytes_of(module, rank_out) } } impl GGLWESwitchingKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, sk_in: &GLWESecret, sk_out: &GLWESecret, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where Module: SvpPrepare + SvpPPolAllocBytes + VecZnxSwithcDegree + VecZnxDftAllocBytes + VecZnxBigNormalize + VecZnxDftFromVecZnx + SvpApplyInplace + VecZnxDftToVecZnxBigConsume + VecZnxNormalizeTmpBytes + VecZnxFillUniform + VecZnxSubABInplace + VecZnxAddInplace + VecZnxNormalizeInplace + VecZnxAddNormal + VecZnxNormalize + VecZnxSub + VecZnxAddScalarInplace, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, { #[cfg(debug_assertions)] { use crate::layouts::{GGLWESwitchingKey, Infos}; assert!(sk_in.n() <= module.n()); assert!(sk_out.n() <= module.n()); assert!( scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space( module, self.basek(), self.k(), self.rank_in(), self.rank_out() ), "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", scratch.available(), GGLWESwitchingKey::encrypt_sk_scratch_space( module, self.basek(), self.k(), self.rank_in(), self.rank_out() ) ) } let n: usize = sk_in.n().max(sk_out.n()); let (mut sk_in_tmp, scratch1) = scratch.take_scalar_znx(n, sk_in.rank()); (0..sk_in.rank()).for_each(|i| { module.vec_znx_switch_degree( &mut sk_in_tmp.as_vec_znx_mut(), i, &sk_in.data.as_vec_znx(), i, ); }); 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| { module.vec_znx_switch_degree(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); }); } self.key.encrypt_sk( module, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch2, ); self.sk_in_n = sk_in.n(); self.sk_out_n = sk_out.n(); } }