use backend::hal::{ api::{ ScratchAvailable, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxSwithcDegree, ZnxView, ZnxViewMut, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; use sampling::source::Source; use crate::{ TakeGLWESecret, TakeGLWESecretExec, layouts::{GLWESecret, GGLWESwitchingKey, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretExec}, }; use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily}; impl LWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, n: usize, basek: usize, k: usize) -> usize where Module: GGLWEEncryptSkFamily + GLWESecretExecModuleFamily, { GLWESecret::bytes_of(n, 1) + GLWESecretExec::bytes_of(module, n, 1) + GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, 1) } } impl LWESwitchingKey { pub fn encrypt_sk( &mut self, module: &Module, sk_lwe_in: &LWESecret, sk_lwe_out: &LWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, scratch: &mut Scratch, ) where DIn: DataRef, DOut: DataRef, Module: GGLWESwitchingKeyEncryptSkFamily + VecZnxAutomorphismInplace + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily, Scratch: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft + TakeGLWESecretExec + TakeVecZnx, { #[cfg(debug_assertions)] { assert!(sk_lwe_in.n() <= self.n()); assert!(sk_lwe_out.n() <= self.n()); assert!(self.n() <= module.n()); } let (mut sk_in_glwe, scratch1) = scratch.take_glwe_secret(self.n(), 1); let (mut sk_out_glwe, scratch2) = scratch1.take_glwe_secret(self.n(), 1); sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n()].copy_from_slice(sk_lwe_out.data.at(0, 0)); sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n()..].fill(0); module.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0); sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n()].copy_from_slice(sk_lwe_in.data.at(0, 0)); sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n()..].fill(0); module.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0); self.0.encrypt_sk( module, &sk_in_glwe, &sk_out_glwe, source_xa, source_xe, sigma, scratch2, ); } }