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