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