use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{ GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; #[derive(PartialEq, Eq)] pub struct AutomorphismKeyPrepared { pub(crate) key: GLWESwitchingKeyPrepared, pub(crate) p: i64, } impl AutomorphismKeyPrepared { pub fn p(&self) -> i64 { self.p } } 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 SetP { fn set_p(&mut self, p: i64); } impl SetP 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() } } impl AutomorphismKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, Module: VmpPMatAlloc, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); AutomorphismKeyPrepared::, B> { key: GLWESwitchingKeyPrepared::alloc(module, infos), p: 0, } } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where Module: VmpPMatAlloc, { AutomorphismKeyPrepared { key: GLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), p: 0, } } pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VmpPMatAllocBytes, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where Module: VmpPMatAllocBytes, { GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) } } pub trait AutomorphismKeyPrepareTmpBytes { fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos; } impl AutomorphismKeyPrepareTmpBytes for Module where Module: GLWESwitchingKeyPrepareTmpBytes, { fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { self.glwe_switching_key_prepare_tmp_bytes(infos) } } impl AutomorphismKeyPrepared { pub fn prepare_tmp_bytes(&self, module: &Module) -> usize where Module: AutomorphismKeyPrepareTmpBytes, { module.automorphism_key_prepare_tmp_bytes(self); } } pub trait AutomorphismKeyPrepare { fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) where R: AutomorphismKeyPreparedToMut, O: AutomorphismKeyToRef; } impl AutomorphismKeyPrepare for Module { fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) where R: AutomorphismKeyPreparedToMut, O: AutomorphismKeyToRef, { self.key.prepare(self, &other.to_ref().key, scratch); self.p = other.p; } } pub trait AutomorphismKeyPrepareAlloc { fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> where O: AutomorphismKeyToRef; } impl AutomorphismKeyPrepareAlloc for Module where Module: VmpPMatAlloc + VmpPrepare, { fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> where O: AutomorphismKeyToRef, { let mut atk_prepared: AutomorphismKeyPrepared, B> = AutomorphismKeyPrepared::alloc(self, &other.to_ref()); atk_prepared.prepare(self, &other.to_ref(), scratch); atk_prepared } } 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(), } } }