arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs
poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rsRaw
  1use std::fmt;
  2
  3use poulpy_hal::{
  4    layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
  5    source::Source,
  6};
  7
  8use crate::layouts::{
  9    Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
 10    GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision,
 11    compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
 12};
 13
 14#[derive(PartialEq, Eq, Clone)]
 15pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
 16
 17impl<D: Data> LWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
 18    fn base2k(&self) -> Base2K {
 19        self.0.base2k()
 20    }
 21
 22    fn k(&self) -> TorusPrecision {
 23        self.0.k()
 24    }
 25
 26    fn n(&self) -> Degree {
 27        self.0.n()
 28    }
 29    fn size(&self) -> usize {
 30        self.0.size()
 31    }
 32}
 33
 34impl<D: Data> GLWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
 35    fn rank(&self) -> Rank {
 36        self.rank_out()
 37    }
 38}
 39
 40impl<D: Data> GGLWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
 41    fn rank_in(&self) -> Rank {
 42        self.0.rank_in()
 43    }
 44
 45    fn dsize(&self) -> Dsize {
 46        self.0.dsize()
 47    }
 48
 49    fn rank_out(&self) -> Rank {
 50        self.0.rank_out()
 51    }
 52
 53    fn dnum(&self) -> Dnum {
 54        self.0.dnum()
 55    }
 56}
 57
 58impl<D: DataRef> fmt::Debug for GLWEToLWESwitchingKeyCompressed<D> {
 59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 60        write!(f, "{self}")
 61    }
 62}
 63
 64impl<D: DataMut> FillUniform for GLWEToLWESwitchingKeyCompressed<D> {
 65    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
 66        self.0.fill_uniform(log_bound, source);
 67    }
 68}
 69
 70impl<D: DataRef> fmt::Display for GLWEToLWESwitchingKeyCompressed<D> {
 71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 72        write!(f, "(GLWEToLWESwitchingKeyCompressed) {}", self.0)
 73    }
 74}
 75
 76impl<D: DataMut> ReaderFrom for GLWEToLWESwitchingKeyCompressed<D> {
 77    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
 78        self.0.read_from(reader)
 79    }
 80}
 81
 82impl<D: DataRef> WriterTo for GLWEToLWESwitchingKeyCompressed<D> {
 83    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
 84        self.0.write_to(writer)
 85    }
 86}
 87
 88impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
 89    pub fn alloc_from_infos<A>(infos: &A) -> Self
 90    where
 91        A: GGLWEInfos,
 92    {
 93        assert_eq!(
 94            infos.rank_out().0,
 95            1,
 96            "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
 97        );
 98        assert_eq!(
 99            infos.dsize().0,
100            1,
101            "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
102        );
103        Self::alloc(
104            infos.n(),
105            infos.base2k(),
106            infos.k(),
107            infos.rank_in(),
108            infos.dnum(),
109        )
110    }
111
112    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
113        GLWEToLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
114            n,
115            base2k,
116            k,
117            rank_in,
118            Rank(1),
119            dnum,
120            Dsize(1),
121        ))
122    }
123
124    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
125    where
126        A: GGLWEInfos,
127    {
128        assert_eq!(
129            infos.rank_out().0,
130            1,
131            "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
132        );
133        assert_eq!(
134            infos.dsize().0,
135            1,
136            "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
137        );
138        GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
139    }
140
141    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize {
142        GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank_in, dnum, Dsize(1))
143    }
144}
145
146pub trait GLWEToLWESwitchingKeyDecompress
147where
148    Self: GLWESwitchingKeyDecompress,
149{
150    fn decompress_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O)
151    where
152        R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
153        O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
154    {
155        self.decompress_glwe_switching_key(res, other);
156    }
157}
158
159impl<B: Backend> GLWEToLWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
160
161impl<D: DataMut> GLWEToLWESwitchingKey<D> {
162    pub fn decompress<O, M>(&mut self, module: &M, other: &O)
163    where
164        O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
165        M: GLWEToLWESwitchingKeyDecompress,
166    {
167        module.decompress_glwe_to_lwe_switching_key(self, other);
168    }
169}
170
171impl<D: DataRef> GGLWECompressedToRef for GLWEToLWESwitchingKeyCompressed<D> {
172    fn to_ref(&self) -> GGLWECompressed<&[u8]> {
173        self.0.to_ref()
174    }
175}
176
177impl<D: DataMut> GGLWECompressedToMut for GLWEToLWESwitchingKeyCompressed<D> {
178    fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
179        self.0.to_mut()
180    }
181}