use base2k::{ Backend, FFT64, MatZnxDft, MatZnxDftOps, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDftOps, ScalarZnxOps, 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::{SecretKey, SecretKeyFourier}, keyswitch_key::GLWESwitchingKey, }; pub struct AutomorphismKey { pub(crate) key: GLWESwitchingKey, pub(crate) p: i64, } impl AutomorphismKey, FFT64> { pub fn new(module: &Module, basek: usize, p: i64, k: usize, rows: usize, rank: usize) -> Self { AutomorphismKey { key: GLWESwitchingKey::new(module, basek, k, rows, rank, rank), p: p, } } } impl Infos for AutomorphismKey { type Inner = MatZnxDft; fn inner(&self) -> &Self::Inner { &self.key.inner() } fn basek(&self) -> usize { self.key.basek() } fn k(&self) -> usize { self.key.k() } } impl AutomorphismKey { pub fn p(&self) -> i64 { self.p } pub fn rank(&self) -> usize { self.key.rank() } pub fn rank_in(&self) -> usize { self.key.rank_in() } pub fn rank_out(&self) -> usize { self.key.rank_out() } } impl MatZnxDftToMut for AutomorphismKey where MatZnxDft: MatZnxDftToMut, { fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> { self.key.to_mut() } } impl MatZnxDftToRef for AutomorphismKey where MatZnxDft: MatZnxDftToRef, { fn to_ref(&self) -> MatZnxDft<&[u8], B> { self.key.to_ref() } } impl GetRow for AutomorphismKey where MatZnxDft: MatZnxDftToRef, { fn get_row(&self, module: &Module, row_i: usize, col_j: usize, res: &mut GLWECiphertextFourier) where VecZnxDft: VecZnxDftToMut, { module.vmp_extract_row(res, self, row_i, col_j); } } impl SetRow for AutomorphismKey where MatZnxDft: MatZnxDftToMut, { fn set_row(&mut self, module: &Module, row_i: usize, col_j: usize, a: &GLWECiphertextFourier) where VecZnxDft: VecZnxDftToRef, { module.vmp_prepare_row(self, row_i, col_j, a); } } impl AutomorphismKey, 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) } pub fn keyswitch_scratch_space( module: &Module, out_size: usize, in_size: usize, ksk_size: usize, rank: usize, ) -> usize { GLWESwitchingKey::keyswitch_scratch_space(module, out_size, rank, in_size, rank, ksk_size) } pub fn keyswitch_inplace_scratch_space(module: &Module, out_size: usize, out_rank: usize, ksk_size: usize) -> usize { GLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_size, out_rank, ksk_size) } pub fn external_product_scratch_space( module: &Module, out_size: usize, in_size: usize, ggsw_size: usize, rank: usize, ) -> usize { GLWESwitchingKey::external_product_scratch_space(module, out_size, in_size, ggsw_size, rank) } pub fn external_product_inplace_scratch_space( module: &Module, out_size: usize, ggsw_size: usize, rank: usize, ) -> usize { GLWESwitchingKey::external_product_inplace_scratch_space(module, out_size, ggsw_size, rank) } } impl AutomorphismKey where MatZnxDft: MatZnxDftToMut + MatZnxDftToRef, { pub fn encrypt_sk( &mut self, module: &Module, p: i64, sk: &SecretKey, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, scratch: &mut Scratch, ) where ScalarZnx: ScalarZnxToRef, { #[cfg(debug_assertions)] { assert_eq!(self.n(), module.n()); assert_eq!(sk.n(), module.n()); assert_eq!(self.rank_out(), self.rank_in()); assert_eq!(sk.rank(), self.rank()); } let (sk_out_dft_data, scratch_1) = scratch.tmp_scalar_znx_dft(module, sk.rank()); let mut sk_out_dft: SecretKeyFourier<&mut [u8], FFT64> = SecretKeyFourier { data: sk_out_dft_data, dist: sk.dist, }; { (0..self.rank()).for_each(|i| { let (mut sk_inv_auto, _) = scratch_1.tmp_scalar_znx(module, 1); module.scalar_znx_automorphism(module.galois_element_inv(p), &mut sk_inv_auto, 0, sk, i); module.svp_prepare(&mut sk_out_dft, i, &sk_inv_auto, 0); }); } self.key.encrypt_sk( module, &sk, &sk_out_dft, source_xa, source_xe, sigma, scratch_1, ); self.p = p; } } impl AutomorphismKey where MatZnxDft: MatZnxDftToMut + MatZnxDftToRef, { pub fn keyswitch( &mut self, module: &Module, lhs: &AutomorphismKey, rhs: &GLWESwitchingKey, scratch: &mut base2k::Scratch, ) where MatZnxDft: MatZnxDftToRef, MatZnxDft: MatZnxDftToRef, { self.key.keyswitch(module, &lhs.key, rhs, scratch); } pub fn keyswitch_inplace( &mut self, module: &Module, rhs: &AutomorphismKey, scratch: &mut base2k::Scratch, ) where MatZnxDft: MatZnxDftToRef, { self.key.keyswitch_inplace(module, &rhs.key, scratch); } pub fn external_product( &mut self, module: &Module, lhs: &AutomorphismKey, rhs: &GGSWCiphertext, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToRef, MatZnxDft: MatZnxDftToRef, { self.key.external_product(module, &lhs.key, rhs, scratch); } pub fn external_product_inplace( &mut self, module: &Module, rhs: &GGSWCiphertext, scratch: &mut Scratch, ) where MatZnxDft: MatZnxDftToRef, { self.key.external_product_inplace(module, rhs, scratch); } }