arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs
poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.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, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank,
  8    TorusPrecision,
  9    compressed::{
 10        GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut,
 11        GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
 12    },
 13};
 14use std::fmt;
 15
 16#[derive(PartialEq, Eq, Clone)]
 17pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
 18
 19impl<D: Data> LWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
 20    fn n(&self) -> Degree {
 21        self.0.n()
 22    }
 23
 24    fn base2k(&self) -> Base2K {
 25        self.0.base2k()
 26    }
 27
 28    fn k(&self) -> TorusPrecision {
 29        self.0.k()
 30    }
 31    fn size(&self) -> usize {
 32        self.0.size()
 33    }
 34}
 35impl<D: Data> GLWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
 36    fn rank(&self) -> Rank {
 37        self.rank_out()
 38    }
 39}
 40
 41impl<D: Data> GGLWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
 42    fn dsize(&self) -> Dsize {
 43        self.0.dsize()
 44    }
 45
 46    fn rank_in(&self) -> Rank {
 47        self.0.rank_in()
 48    }
 49
 50    fn rank_out(&self) -> Rank {
 51        self.0.rank_out()
 52    }
 53
 54    fn dnum(&self) -> Dnum {
 55        self.0.dnum()
 56    }
 57}
 58
 59impl<D: DataRef> fmt::Debug for LWEToGLWESwitchingKeyCompressed<D> {
 60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 61        write!(f, "{self}")
 62    }
 63}
 64
 65impl<D: DataMut> FillUniform for LWEToGLWESwitchingKeyCompressed<D> {
 66    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
 67        self.0.fill_uniform(log_bound, source);
 68    }
 69}
 70
 71impl<D: DataRef> fmt::Display for LWEToGLWESwitchingKeyCompressed<D> {
 72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 73        write!(f, "(LWEToGLWESwitchingKeyCompressed) {}", self.0)
 74    }
 75}
 76
 77impl<D: DataMut> ReaderFrom for LWEToGLWESwitchingKeyCompressed<D> {
 78    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
 79        self.0.read_from(reader)
 80    }
 81}
 82
 83impl<D: DataRef> WriterTo for LWEToGLWESwitchingKeyCompressed<D> {
 84    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
 85        self.0.write_to(writer)
 86    }
 87}
 88
 89pub trait LWEToGLWESwitchingKeyCompressedAlloc
 90where
 91    Self: GLWESwitchingKeyCompressedAlloc,
 92{
 93    fn alloc_lwe_to_glwe_switching_key_compressed(
 94        &self,
 95        base2k: Base2K,
 96        k: TorusPrecision,
 97        rank_out: Rank,
 98        dnum: Dnum,
 99    ) -> LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
100        LWEToGLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
101    }
102
103    fn alloc_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed<Vec<u8>>
104    where
105        A: GGLWEInfos,
106    {
107        assert_eq!(
108            infos.dsize().0,
109            1,
110            "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
111        );
112        assert_eq!(
113            infos.rank_in().0,
114            1,
115            "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
116        );
117        self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
118    }
119
120    fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
121        self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1))
122    }
123
124    fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
125    where
126        A: GGLWEInfos,
127    {
128        assert_eq!(
129            infos.dsize().0,
130            1,
131            "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
132        );
133        assert_eq!(
134            infos.rank_in().0,
135            1,
136            "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
137        );
138        self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum())
139    }
140}
141
142impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
143    pub fn alloc<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
144    where
145        A: GGLWEInfos,
146        Module<B>: LWEToGLWESwitchingKeyCompressedAlloc,
147    {
148        module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos)
149    }
150
151    pub fn alloc_with<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self
152    where
153        Module<B>: LWEToGLWESwitchingKeyCompressedAlloc,
154    {
155        module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum)
156    }
157
158    pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
159    where
160        A: GGLWEInfos,
161        Module<B>: LWEToGLWESwitchingKeyCompressedAlloc,
162    {
163        module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos)
164    }
165
166    pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize
167    where
168        Module<B>: LWEToGLWESwitchingKeyCompressedAlloc,
169    {
170        module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum)
171    }
172}
173
174pub trait LWEToGLWESwitchingKeyDecompress
175where
176    Self: GLWESwitchingKeyDecompress,
177{
178    fn decompress_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O)
179    where
180        R: LWEToGLWESwitchingKeyToMut,
181        O: LWEToGLWESwitchingKeyCompressedToRef,
182    {
183        self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0);
184    }
185}
186
187impl<B: Backend> LWEToGLWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
188
189impl<D: DataMut> LWEToGLWESwitchingKey<D> {
190    pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O)
191    where
192        O: LWEToGLWESwitchingKeyCompressedToRef,
193        Module<B>: LWEToGLWESwitchingKeyDecompress,
194    {
195        module.decompress_lwe_to_glwe_switching_key(self, other);
196    }
197}
198
199pub trait LWEToGLWESwitchingKeyCompressedToRef {
200    fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>;
201}
202
203impl<D: DataRef> LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed<D>
204where
205    GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
206{
207    fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> {
208        LWEToGLWESwitchingKeyCompressed(self.0.to_ref())
209    }
210}
211
212pub trait LWEToGLWESwitchingKeyCompressedToMut {
213    fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>;
214}
215
216impl<D: DataMut> LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed<D>
217where
218    GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
219{
220    fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> {
221        LWEToGLWESwitchingKeyCompressed(self.0.to_mut())
222    }
223}