use std::fmt; use poulpy_hal::{ layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWESwitchingKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub dnum: Dnum, } impl LWEInfos for LWESwitchingKeyLayout { fn n(&self) -> Degree { self.n } fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } } impl GLWEInfos for LWESwitchingKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for LWESwitchingKeyLayout { fn rank_in(&self) -> Rank { Rank(1) } fn dsize(&self) -> Dsize { Dsize(1) } fn rank_out(&self) -> Rank { Rank(1) } fn dnum(&self) -> Dnum { self.dnum } } #[derive(PartialEq, Eq, Clone)] pub struct LWESwitchingKey(pub(crate) GLWESwitchingKey); impl LWEInfos for LWESwitchingKey { fn base2k(&self) -> Base2K { self.0.base2k() } fn k(&self) -> TorusPrecision { self.0.k() } fn n(&self) -> Degree { self.0.n() } fn size(&self) -> usize { self.0.size() } } impl GLWEInfos for LWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for LWESwitchingKey { fn dsize(&self) -> Dsize { self.0.dsize() } fn rank_in(&self) -> Rank { self.0.rank_in() } fn rank_out(&self) -> Rank { self.0.rank_out() } fn dnum(&self) -> Dnum { self.0.dnum() } } pub trait LWESwitchingKeyAlloc where Self: GLWESwitchingKeyAlloc, { fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey> { LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) } fn alloc_lwe_switching_key_from_infos(&self, infos: &A) -> LWESwitchingKey> where A: GGLWEInfos, { assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey" ); assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey" ); assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey" ); self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) } fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } fn bytes_of_lwe_switching_key_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey" ); assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey" ); assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey" ); self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) } } impl LWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} impl LWESwitchingKey> { pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, Module: LWESwitchingKeyAlloc, { module.alloc_lwe_switching_key_from_infos(infos) } pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where Module: LWESwitchingKeyAlloc, { module.alloc_lwe_switching_key(base2k, k, dnum) } pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: LWESwitchingKeyAlloc, { module.bytes_of_glwe_switching_key_from_infos(infos) } pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where Module: LWESwitchingKeyAlloc, { module.bytes_of_lwe_switching_key(base2k, k, dnum) } } impl fmt::Debug for LWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl FillUniform for LWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.0.fill_uniform(log_bound, source); } } impl fmt::Display for LWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(LWESwitchingKey) {}", self.0) } } impl ReaderFrom for LWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.0.read_from(reader) } } impl WriterTo for LWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { self.0.write_to(writer) } } pub trait LWESwitchingKeyToRef { fn to_ref(&self) -> LWESwitchingKey<&[u8]>; } impl LWESwitchingKeyToRef for LWESwitchingKey where GLWESwitchingKey: GLWESwitchingKeyToRef, { fn to_ref(&self) -> LWESwitchingKey<&[u8]> { LWESwitchingKey(self.0.to_ref()) } } pub trait LWESwitchingKeyToMut { fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; } impl LWESwitchingKeyToMut for LWESwitchingKey where GLWESwitchingKey: GLWESwitchingKeyToMut, { fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> { LWESwitchingKey(self.0.to_mut()) } }