use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; #[derive(PartialEq, Eq)] pub struct AutomorphismKeyPrepared { pub(crate) key: GLWESwitchingKeyPrepared, 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: GLWESwitchingKeyPreparedAlloc, { fn alloc_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize, ) -> AutomorphismKeyPrepared, B> { AutomorphismKeyPrepared::, B> { key: self.alloc_glwe_switching_key_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_glwe_switching_key_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 GGLWEAutomorphismKeyPrepared" ); self.bytes_of_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(), infos.dnum(), infos.dsize(), ) } } impl AutomorphismKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} 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 PrepareAutomorphismKey where Self: GLWESwitchingKeyPrepare, { fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { self.prepare_glwe_switching_key_tmp_bytes(infos) } fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, { self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch); res.set_p(other.p()); } } impl PrepareAutomorphismKey for Module where Module: GLWESwitchingKeyPrepare {} impl AutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where M: PrepareAutomorphismKey, { module.prepare_automorphism_key_tmp_bytes(self) } } impl AutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, M: PrepareAutomorphismKey, { module.prepare_automorphism_key(self, other, scratch); } } pub trait AutomorphismKeyPreparedToMut { fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; } impl AutomorphismKeyPreparedToMut for AutomorphismKeyPrepared { fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> { AutomorphismKeyPrepared { p: self.p, key: self.key.to_mut(), } } } pub trait AutomorphismKeyPreparedToRef { fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>; } impl AutomorphismKeyPreparedToRef for AutomorphismKeyPrepared { fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> { AutomorphismKeyPrepared { p: self.p, key: self.key.to_ref(), } } }