arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/compressed/glwe_to_lwe_key.rs
poulpy-core/src/layouts/compressed/glwe_to_lwe_key.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, GLWEToLWEKey, 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(infos.n(), infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
104    }
105
106    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
107        GLWEToLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
108            n,
109            base2k,
110            k,
111            rank_in,
112            Rank(1),
113            dnum,
114            Dsize(1),
115        ))
116    }
117
118    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
119    where
120        A: GGLWEInfos,
121    {
122        assert_eq!(
123            infos.rank_out().0,
124            1,
125            "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
126        );
127        assert_eq!(
128            infos.dsize().0,
129            1,
130            "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
131        );
132        GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
133    }
134
135    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize {
136        GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank_in, dnum, Dsize(1))
137    }
138}
139
140pub trait GLWEToLWESwitchingKeyDecompress
141where
142    Self: GLWESwitchingKeyDecompress,
143{
144    fn decompress_glwe_to_lwe_key<R, O>(&self, res: &mut R, other: &O)
145    where
146        R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
147        O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
148    {
149        self.decompress_glwe_switching_key(res, other);
150    }
151}
152
153impl<B: Backend> GLWEToLWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
154
155impl<D: DataMut> GLWEToLWEKey<D> {
156    pub fn decompress<O, M>(&mut self, module: &M, other: &O)
157    where
158        O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
159        M: GLWEToLWESwitchingKeyDecompress,
160    {
161        module.decompress_glwe_to_lwe_key(self, other);
162    }
163}
164
165impl<D: DataRef> GGLWECompressedToRef for GLWEToLWESwitchingKeyCompressed<D> {
166    fn to_ref(&self) -> GGLWECompressed<&[u8]> {
167        self.0.to_ref()
168    }
169}
170
171impl<D: DataMut> GGLWECompressedToMut for GLWEToLWESwitchingKeyCompressed<D> {
172    fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
173        self.0.to_mut()
174    }
175}