use poulpy_hal::{ layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWE, GGLWECiphertextToMut, GGLWEInfos, GGLWEToRef, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWESwitchingKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, pub rank_out: Rank, pub dnum: Dnum, pub dsize: Dsize, } impl LWEInfos for GLWESwitchingKeyLayout { fn n(&self) -> Degree { self.n } fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } } impl GLWEInfos for GLWESwitchingKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for GLWESwitchingKeyLayout { fn rank_in(&self) -> Rank { self.rank_in } fn rank_out(&self) -> Rank { self.rank_out } fn dsize(&self) -> Dsize { self.dsize } fn dnum(&self) -> Dnum { self.dnum } } #[derive(PartialEq, Eq, Clone)] pub struct GLWESwitchingKey { pub(crate) key: GGLWE, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } impl LWEInfos for GLWESwitchingKey { 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() } } impl GLWEInfos for GLWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for GLWESwitchingKey { 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 fmt::Debug for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl fmt::Display for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "(GLWESwitchingKey: sk_in_n={} sk_out_n={}) {}", self.sk_in_n, self.sk_out_n, self.key.data() ) } } impl FillUniform for GLWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } impl GLWESwitchingKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { GLWESwitchingKey { key: GGLWE::alloc(infos), sk_in_n: 0, sk_out_n: 0, } } pub fn alloc_with( n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> Self { GLWESwitchingKey { key: GGLWE::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } pub fn alloc_bytes(infos: &A) -> usize where A: GGLWEInfos, { GGLWE::alloc_bytes(infos) } pub fn alloc_bytes_with( n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, ) -> usize { GGLWE::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) } } pub trait GLWESwitchingKeyToMut { fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>; } impl GLWESwitchingKeyToMut for GLWESwitchingKey where GGLWE: GGLWECiphertextToMut, { fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { GLWESwitchingKey { key: self.key.to_mut(), sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, } } } pub trait GLWESwitchingKeyToRef { fn to_ref(&self) -> GLWESwitchingKey<&[u8]>; } impl GLWESwitchingKeyToRef for GLWESwitchingKey where GGLWE: GGLWEToRef, { fn to_ref(&self) -> GLWESwitchingKey<&[u8]> { GLWESwitchingKey { key: self.key.to_ref(), sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, } } } impl GLWESwitchingKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } impl GLWESwitchingKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { self.key.at_mut(row, col) } } impl ReaderFrom for GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; self.key.read_from(reader) } } impl WriterTo for GLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; self.key.write_to(writer) } }