use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, compressed::{Decompress, GGLWESwitchingKeyCompressed}, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct LWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); 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) } } impl LWESwitchingKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { debug_assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); debug_assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); debug_assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); Self(GGLWESwitchingKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { Self(GGLWESwitchingKeyCompressed::alloc_with( n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1), )) } pub fn alloc_bytes(infos: &A) -> usize where A: GGLWEInfos, { debug_assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey" ); debug_assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey" ); debug_assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey" ); GGLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) } } impl Decompress> for LWESwitchingKey where Module: VecZnxFillUniform + VecZnxCopy, { fn decompress(&mut self, module: &Module, other: &LWESwitchingKeyCompressed) { self.0.decompress(module, &other.0); } }