use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] pub struct GLWESwitchingKeyPrepared { pub(crate) key: GGLWEPrepared, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } impl GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared { fn set_sk_in_n(&mut self, sk_in_n: usize) { self.sk_in_n = sk_in_n } fn set_sk_out_n(&mut self, sk_out_n: usize) { self.sk_out_n = sk_out_n } } impl GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared { fn sk_in_n(&self) -> usize { self.sk_in_n } fn sk_out_n(&self) -> usize { self.sk_out_n } } 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), sk_in_n: 0, sk_out_n: 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 Module: GGLWEPreparedAlloc {} impl GLWESwitchingKeyPrepared, B> where Module: GLWESwitchingKeyPreparedAlloc, { pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, { module.alloc_glwe_switching_key_prepared_from_infos(infos) } pub fn alloc( module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> Self { module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { module.bytes_of_glwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of( module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> usize { 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: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, { self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch); res.set_sk_in_n(other.sk_in_n()); res.set_sk_out_n(other.sk_out_n()); } } impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepare {} impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, Module: GLWESwitchingKeyPrepare, { module.prepare_glwe_switching(self, other, scratch); } } impl GLWESwitchingKeyPrepared, B> where Module: GLWESwitchingKeyPrepare, { pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { module.prepare_gglwe_tmp_bytes(self) } } pub trait GLWESwitchingKeyPreparedToMut { fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; } impl GLWESwitchingKeyPreparedToMut for GLWESwitchingKeyPrepared { fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> { GLWESwitchingKeyPrepared { sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, key: self.key.to_mut(), } } } pub trait GLWESwitchingKeyPreparedToRef { fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>; } impl GLWESwitchingKeyPreparedToRef for GLWESwitchingKeyPrepared { fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> { GLWESwitchingKeyPrepared { sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, key: self.key.to_ref(), } } }