use base2k::{ Backend, FFT64, MatZnxDft, MatZnxDftOps, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, }; use sampling::source::Source; use crate::{ elem::{GetRow, Infos, SetRow}, gglwe_ciphertext::GGLWECiphertext, ggsw_ciphertext::GGSWCiphertext, glwe_ciphertext_fourier::GLWECiphertextFourier, keys::SecretKeyFourier, vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace}, }; pub struct GLWESwitchingKey(pub(crate) GGLWECiphertext); impl GLWESwitchingKey, FFT64> { pub fn new(module: &Module, base2k: usize, k: usize, rows: usize, rank_in: usize, rank_out: usize) -> Self { GLWESwitchingKey(GGLWECiphertext::new( module, base2k, k, rows, rank_in, rank_out, )) } } impl Infos for GLWESwitchingKey { type Inner = MatZnxDft; fn inner(&self) -> &Self::Inner { &self.0.inner() } fn basek(&self) -> usize { self.0.basek() } fn k(&self) -> usize { self.0.k() } } impl MatZnxDftToMut for GLWESwitchingKey where MatZnxDft: MatZnxDftToMut, { fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> { self.0.data.to_mut() } } impl MatZnxDftToRef for GLWESwitchingKey where MatZnxDft: MatZnxDftToRef, { fn to_ref(&self) -> MatZnxDft<&[u8], B> { self.0.data.to_ref() } } impl GetRow for GLWESwitchingKey where MatZnxDft: MatZnxDftToRef, { fn get_row(&self, module: &Module, row_i: usize, col_j: usize, res: &mut GLWECiphertextFourier) where VecZnxDft: VecZnxDftToMut, { #[cfg(debug_assertions)] { assert_eq!(col_j, 0); } module.vmp_extract_row(res, self, row_i, col_j); } } impl SetRow for GLWESwitchingKey where MatZnxDft: MatZnxDftToMut, { fn set_row(&mut self, module: &Module, row_i: usize, col_j: usize, a: &GLWECiphertextFourier) where VecZnxDft: VecZnxDftToRef, { #[cfg(debug_assertions)] { assert_eq!(col_j, 0); } module.vmp_prepare_row(self, row_i, col_j, a); } } impl GLWESwitchingKey, FFT64> { pub fn encrypt_sk_scratch_space(module: &Module, rank: usize, size: usize) -> usize { GGLWECiphertext::encrypt_sk_scratch_space(module, rank, size) } pub fn encrypt_pk_scratch_space(module: &Module, rank: usize, pk_size: usize) -> usize { GGLWECiphertext::encrypt_pk_scratch_space(module, rank, pk_size) } } impl GLWESwitchingKey where MatZnxDft: MatZnxDftToMut + MatZnxDftToRef, { pub fn encrypt_sk( &mut self, module: &Module, pt: &ScalarZnx, sk_dft: &SecretKeyFourier, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, bound: f64, scratch: &mut Scratch, ) where ScalarZnx: ScalarZnxToRef, ScalarZnxDft: ScalarZnxDftToRef, { self.0.encrypt_sk( module, pt, sk_dft, source_xa, source_xe, sigma, bound, scratch, ); } } impl GLWESwitchingKey, FFT64> { pub fn keyswitch_scratch_space(module: &Module, res_size: usize, lhs: usize, rhs: usize) -> usize { , FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space( module, res_size, lhs, rhs, ) } pub fn keyswitch_inplace_scratch_space(module: &Module, res_size: usize, rhs: usize) -> usize { , FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_inplace_scratch_space( module, res_size, rhs, ) } pub fn external_product_scratch_space(module: &Module, res_size: usize, lhs: usize, rhs: usize) -> usize { , FFT64> as VecGLWEProductScratchSpace>::prod_with_vec_glwe_scratch_space( module, res_size, lhs, rhs, ) } pub fn external_product_inplace_scratch_space(module: &Module, res_size: usize, rhs: usize) -> usize { , FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space( module, res_size, rhs, ) } } impl GLWESwitchingKey where MatZnxDft: MatZnxDftToMut + MatZnxDftToRef, { pub fn keyswitch( &mut self, module: &Module, lhs: &GLWESwitchingKey, rhs: &GLWESwitchingKey, scratch: &mut base2k::Scratch, ) where MatZnxDft: MatZnxDftToRef, MatZnxDft: MatZnxDftToRef, { rhs.0 .prod_with_vec_glwe(module, &mut self.0, &lhs.0, scratch); } pub fn keyswitch_inplace( &mut self, module: &Module, rhs: &GLWESwitchingKey, scratch: &mut base2k::Scratch, ) where MatZnxDft: MatZnxDftToRef, { rhs.0 .prod_with_vec_glwe_inplace(module, &mut self.0, scratch); } pub fn external_product( &mut self, module: &Module, lhs: &GLWESwitchingKey, rhs: &GGSWCiphertext, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToRef, MatZnxDft: MatZnxDftToRef, { rhs.prod_with_vec_glwe(module, &mut self.0, &lhs.0, scratch); } pub fn external_product_inplace( &mut self, module: &Module, rhs: &GGSWCiphertext, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToRef, { rhs.prod_with_vec_glwe_inplace(module, &mut self.0, scratch); } }