use poulpy_hal::{ layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct LWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWESwitchingKeyCompressed { 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 LWESwitchingKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for LWESwitchingKeyCompressed { 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() } } impl fmt::Debug for LWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl FillUniform for LWESwitchingKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.0.fill_uniform(log_bound, source); } } impl fmt::Display for LWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(LWESwitchingKeyCompressed) {}", self.0) } } impl ReaderFrom for LWESwitchingKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.0.read_from(reader) } } impl WriterTo for LWESwitchingKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { self.0.write_to(writer) } } pub trait LWESwitchingKeyCompressedAlloc where Self: GLWESwitchingKeyCompressedAlloc, { fn alloc_lwe_switching_key_compressed( &self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, ) -> LWESwitchingKeyCompressed> { LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) } fn alloc_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWESwitchingKeyCompressed> where A: GGLWEInfos, { assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) } fn bytes_of_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); self.bytes_of_glwe_switching_key_compressed_from_infos(infos) } } impl LWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} impl LWESwitchingKeyCompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, M: LWESwitchingKeyCompressedAlloc, { module.alloc_lwe_switching_key_compressed_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where M: LWESwitchingKeyCompressedAlloc, { module.alloc_lwe_switching_key_compressed(base2k, k, dnum) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, M: LWESwitchingKeyCompressedAlloc, { module.bytes_of_lwe_switching_key_compressed_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where M: LWESwitchingKeyCompressedAlloc, { module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) } } pub trait LWESwitchingKeyDecompress where Self: GLWESwitchingKeyDecompress, { fn decompress_lwe_switching_key(&self, res: &mut R, other: &O) where R: LWESwitchingKeyToMut, O: LWESwitchingKeyCompressedToRef, { self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); } } impl LWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl LWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where O: LWESwitchingKeyCompressedToRef, M: LWESwitchingKeyDecompress, { module.decompress_lwe_switching_key(self, other); } } pub trait LWESwitchingKeyCompressedToRef { fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; } impl LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed where GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, { fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> { LWESwitchingKeyCompressed(self.0.to_ref()) } } pub trait LWESwitchingKeyCompressedToMut { fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; } impl LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed where GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, { fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> { LWESwitchingKeyCompressed(self.0.to_mut()) } }