use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] pub struct GLWESwitchingKeyPrepared { pub(crate) key: GGLWEPrepared, pub(crate) input_degree: Degree, // Degree of sk_in pub(crate) output_degree: Degree, // Degree of sk_out } impl GLWESwitchingKeyDegrees for GLWESwitchingKeyPrepared { fn output_degree(&self) -> &Degree { &self.output_degree } fn input_degree(&self) -> &Degree { &self.input_degree } } impl GLWESwitchingKeyDegreesMut for GLWESwitchingKeyPrepared { fn output_degree(&mut self) -> &mut Degree { &mut self.output_degree } fn input_degree(&mut self) -> &mut Degree { &mut self.input_degree } } impl LWEInfos for GLWESwitchingKeyPrepared { fn n(&self) -> Degree { self.key.n() } fn base2k(&self) -> Base2K { self.key.base2k() } fn k(&self) -> TorusPrecision { self.key.k() } fn size(&self) -> usize { self.key.size() } } impl GLWEInfos for GLWESwitchingKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for GLWESwitchingKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } fn rank_out(&self) -> Rank { self.key.rank_out() } fn dsize(&self) -> Dsize { self.key.dsize() } fn dnum(&self) -> Dnum { self.key.dnum() } } pub trait GLWESwitchingKeyPreparedAlloc where Self: GGLWEPreparedAlloc, { fn alloc_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> GLWESwitchingKeyPrepared, B> { GLWESwitchingKeyPrepared::, B> { key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize), input_degree: Degree(0), output_degree: Degree(0), } } fn alloc_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> where A: GGLWEInfos, { self.alloc_glwe_switching_key_prepared( infos.base2k(), infos.k(), infos.rank_in(), infos.rank_out(), infos.dnum(), infos.dsize(), ) } fn bytes_of_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> usize { self.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } fn bytes_of_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { self.bytes_of_glwe_switching_key_prepared( infos.base2k(), infos.k(), infos.rank_in(), infos.rank_out(), infos.dnum(), infos.dsize(), ) } } impl GLWESwitchingKeyPreparedAlloc for Module where Self: GGLWEPreparedAlloc {} impl GLWESwitchingKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, M: GLWESwitchingKeyPreparedAlloc, { module.alloc_glwe_switching_key_prepared_from_infos(infos) } pub fn alloc( module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> Self where M: GLWESwitchingKeyPreparedAlloc, { module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, M: GLWESwitchingKeyPreparedAlloc, { module.bytes_of_glwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of( module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> usize where M: GLWESwitchingKeyPreparedAlloc, { module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } } pub trait GLWESwitchingKeyPrepare where Self: GGLWEPrepare, { fn prepare_glwe_switching_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { self.prepare_gglwe_tmp_bytes(infos) } fn prepare_glwe_switching(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, O: GGLWEToRef + GLWESwitchingKeyDegrees, { self.prepare_gglwe(res, other, scratch); *res.input_degree() = *other.input_degree(); *res.output_degree() = *other.output_degree(); } } impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepare {} impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, M: GLWESwitchingKeyPrepare, { module.prepare_glwe_switching(self, other, scratch); } } impl GLWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where M: GLWESwitchingKeyPrepare, { module.prepare_glwe_switching_key_tmp_bytes(self) } } impl GGLWEPreparedToRef for GLWESwitchingKeyPrepared where GGLWEPrepared: GGLWEPreparedToRef, { fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> { self.key.to_ref() } } impl GGLWEPreparedToMut for GLWESwitchingKeyPrepared where GGLWEPrepared: GGLWEPreparedToMut, { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], BE> { self.key.to_mut() } }