use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] pub struct AutomorphismKeyPrepared { pub(crate) key: GGLWEPrepared, pub(crate) p: i64, } impl LWEInfos for AutomorphismKeyPrepared { 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() } } pub trait GetAutomorphismGaloisElement { fn p(&self) -> i64; } impl GetAutomorphismGaloisElement for AutomorphismKeyPrepared { fn p(&self) -> i64 { self.p } } pub trait SetAutomorphismGaloisElement { fn set_p(&mut self, p: i64); } impl SetAutomorphismGaloisElement for AutomorphismKeyPrepared { fn set_p(&mut self, p: i64) { self.p = p } } impl GLWEInfos for AutomorphismKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for AutomorphismKeyPrepared { 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 AutomorphismKeyPreparedAlloc where Self: GGLWEPreparedAlloc, { fn alloc_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize, ) -> AutomorphismKeyPrepared, B> { AutomorphismKeyPrepared::, B> { key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize), p: 0, } } fn alloc_automorphism_key_prepared_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> where A: GGLWEInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); self.alloc_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(), infos.dnum(), infos.dsize(), ) } fn bytes_of_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize, ) -> usize { self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize) } fn bytes_of_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); self.bytes_of_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(), infos.dnum(), infos.dsize(), ) } } impl AutomorphismKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} impl AutomorphismKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, M: AutomorphismKeyPreparedAlloc, { module.alloc_automorphism_key_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where M: AutomorphismKeyPreparedAlloc, { module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, M: AutomorphismKeyPreparedAlloc, { module.bytes_of_automorphism_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: AutomorphismKeyPreparedAlloc, { module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } } pub trait AutomorphismKeyPrepare where Self: GGLWEPrepare, { fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { self.prepare_gglwe_tmp_bytes(infos) } fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGLWEPreparedToMut + SetAutomorphismGaloisElement, O: GGLWEToRef + GetAutomorphismGaloisElement, { self.prepare_gglwe(res, other, scratch); res.set_p(other.p()); } } impl AutomorphismKeyPrepare for Module where Module: GGLWEPrepare {} impl AutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where M: AutomorphismKeyPrepare, { module.prepare_automorphism_key_tmp_bytes(self) } } impl AutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GetAutomorphismGaloisElement, M: AutomorphismKeyPrepare, { module.prepare_automorphism_key(self, other, scratch); } } impl GGLWEPreparedToMut for AutomorphismKeyPrepared { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { self.key.to_mut() } } impl GGLWEPreparedToRef for AutomorphismKeyPrepared { fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> { self.key.to_ref() } }