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!(
 92            infos.dsize().0,
 93            1,
 94            "dsize > 1 is not supported for LWESwitchingKeyCompressed"
 95        );
 96        assert_eq!(
 97            infos.rank_in().0,
 98            1,
 99            "rank_in > 1 is not supported for LWESwitchingKeyCompressed"
100        );
101        assert_eq!(
102            infos.rank_out().0,
103            1,
104            "rank_out > 1 is not supported for LWESwitchingKeyCompressed"
105        );
106        Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
107    }
108
109    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
110        LWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
111            n,
112            base2k,
113            k,
114            Rank(1),
115            Rank(1),
116            dnum,
117            Dsize(1),
118        ))
119    }
120
121    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
122    where
123        A: GGLWEInfos,
124    {
125        assert_eq!(
126            infos.dsize().0,
127            1,
128            "dsize > 1 is not supported for LWESwitchingKeyCompressed"
129        );
130        assert_eq!(
131            infos.rank_in().0,
132            1,
133            "rank_in > 1 is not supported for LWESwitchingKeyCompressed"
134        );
135        assert_eq!(
136            infos.rank_out().0,
137            1,
138            "rank_out > 1 is not supported for LWESwitchingKeyCompressed"
139        );
140        GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
141    }
142
143    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
144        GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1))
145    }
146}
147
148pub trait LWESwitchingKeyDecompress
149where
150    Self: GLWESwitchingKeyDecompress,
151{
152    fn decompress_lwe_switching_key<R, O>(&self, res: &mut R, other: &O)
153    where
154        R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
155        O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
156    {
157        self.decompress_glwe_switching_key(res, other);
158    }
159}
160
161impl<B: Backend> LWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
162
163impl<D: DataMut> LWESwitchingKey<D> {
164    pub fn decompress<O, M>(&mut self, module: &M, other: &O)
165    where
166        O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
167        M: LWESwitchingKeyDecompress,
168    {
169        module.decompress_lwe_switching_key(self, other);
170    }
171}
172
173impl<D: DataRef> GGLWECompressedToRef for LWESwitchingKeyCompressed<D> {
174    fn to_ref(&self) -> GGLWECompressed<&[u8]> {
175        self.0.to_ref()
176    }
177}
178
179impl<D: DataMut> GGLWECompressedToMut for LWESwitchingKeyCompressed<D> {
180    fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
181        self.0.to_mut()
182    }
183}