use std::fmt; use poulpy_hal::{ layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Digits, GGLWELayoutInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWESwitchingKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rows: Rows, } 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 GGLWELayoutInfos for LWESwitchingKeyLayout { fn rank_in(&self) -> Rank { Rank(1) } fn digits(&self) -> Digits { Digits(1) } fn rank_out(&self) -> Rank { Rank(1) } fn rows(&self) -> Rows { self.rows } } #[derive(PartialEq, Eq, Clone)] pub struct LWESwitchingKey(pub(crate) GGLWESwitchingKey); 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 GGLWELayoutInfos for LWESwitchingKey { fn digits(&self) -> Digits { self.0.digits() } fn rank_in(&self) -> Rank { self.0.rank_in() } fn rank_out(&self) -> Rank { self.0.rank_out() } fn rows(&self) -> Rows { self.0.rows() } } impl LWESwitchingKey> { pub fn alloc(infos: &A) -> Self where A: GGLWELayoutInfos, { debug_assert_eq!( infos.digits().0, 1, "digits > 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" ); Self(GGLWESwitchingKey::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows) -> Self { Self(GGLWESwitchingKey::alloc_with( n, base2k, k, rows, Digits(1), Rank(1), Rank(1), )) } pub fn alloc_bytes(infos: &A) -> usize where A: GGLWELayoutInfos, { debug_assert_eq!( infos.digits().0, 1, "digits > 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" ); GGLWESwitchingKey::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows) -> usize { GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rows, Digits(1), Rank(1), Rank(1)) } } 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) } }