arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/compressed/lwe_switching_key.rs
poulpy-core/src/layouts/compressed/lwe_switching_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, LWESwitchingKey, Rank, TorusPrecision,
  9    compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
 10};
 11use std::fmt;
 12
 13#[derive(PartialEq, Eq, Clone)]
 14pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
 15
 16impl<D: Data> LWEInfos for LWESwitchingKeyCompressed<D> {
 17    fn base2k(&self) -> Base2K {
 18        self.0.base2k()
 19    }
 20
 21    fn k(&self) -> TorusPrecision {
 22        self.0.k()
 23    }
 24
 25    fn n(&self) -> Degree {
 26        self.0.n()
 27    }
 28    fn size(&self) -> usize {
 29        self.0.size()
 30    }
 31}
 32impl<D: Data> GLWEInfos for LWESwitchingKeyCompressed<D> {
 33    fn rank(&self) -> Rank {
 34        self.rank_out()
 35    }
 36}
 37
 38impl<D: Data> GGLWEInfos for LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<D> {
 57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 58        write!(f, "{self}")
 59    }
 60}
 61
 62impl<D: DataMut> FillUniform for LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<D> {
 69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 70        write!(f, "(LWESwitchingKeyCompressed) {}", self.0)
 71    }
 72}
 73
 74impl<D: DataMut> ReaderFrom for LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<Vec<u8>> {
 87    pub fn alloc_from_infos<A>(infos: &A) -> Self
 88    where
 89        A: GGLWEInfos,
 90    {
 91        assert_eq!(infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed");
 92        assert_eq!(
 93            infos.rank_in().0,
 94            1,
 95            "rank_in > 1 is not supported for LWESwitchingKeyCompressed"
 96        );
 97        assert_eq!(
 98            infos.rank_out().0,
 99            1,
100            "rank_out > 1 is not supported for LWESwitchingKeyCompressed"
101        );
102        Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
103    }
104
105    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
106        LWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
107            n,
108            base2k,
109            k,
110            Rank(1),
111            Rank(1),
112            dnum,
113            Dsize(1),
114        ))
115    }
116
117    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
118    where
119        A: GGLWEInfos,
120    {
121        assert_eq!(infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed");
122        assert_eq!(
123            infos.rank_in().0,
124            1,
125            "rank_in > 1 is not supported for LWESwitchingKeyCompressed"
126        );
127        assert_eq!(
128            infos.rank_out().0,
129            1,
130            "rank_out > 1 is not supported for LWESwitchingKeyCompressed"
131        );
132        GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
133    }
134
135    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
136        GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1))
137    }
138}
139
140pub trait LWESwitchingKeyDecompress
141where
142    Self: GLWESwitchingKeyDecompress,
143{
144    fn decompress_lwe_switching_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> LWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
154
155impl<D: DataMut> LWESwitchingKey<D> {
156    pub fn decompress<O, M>(&mut self, module: &M, other: &O)
157    where
158        O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
159        M: LWESwitchingKeyDecompress,
160    {
161        module.decompress_lwe_switching_key(self, other);
162    }
163}
164
165impl<D: DataRef> GGLWECompressedToRef for LWESwitchingKeyCompressed<D> {
166    fn to_ref(&self) -> GGLWECompressed<&[u8]> {
167        self.0.to_ref()
168    }
169}
170
171impl<D: DataMut> GGLWECompressedToMut for LWESwitchingKeyCompressed<D> {
172    fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
173        self.0.to_mut()
174    }
175}