arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/prepared/gglwe_ksk.rs
poulpy-core/src/layouts/prepared/gglwe_ksk.rsRaw
  1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
  2
  3use crate::layouts::{
  4    Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef,
  5    GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision,
  6    prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef},
  7};
  8
  9#[derive(PartialEq, Eq)]
 10pub struct GLWESwitchingKeyPrepared<D: Data, B: Backend> {
 11    pub(crate) key: GGLWEPrepared<D, B>,
 12    pub(crate) sk_in_n: usize,  // Degree of sk_in
 13    pub(crate) sk_out_n: usize, // Degree of sk_out
 14}
 15
 16impl<D: DataMut, B: Backend> GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared<D, B> {
 17    fn set_sk_in_n(&mut self, sk_in_n: usize) {
 18        self.sk_in_n = sk_in_n
 19    }
 20
 21    fn set_sk_out_n(&mut self, sk_out_n: usize) {
 22        self.sk_out_n = sk_out_n
 23    }
 24}
 25
 26impl<D: DataRef, B: Backend> GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared<D, B> {
 27    fn sk_in_n(&self) -> usize {
 28        self.sk_in_n
 29    }
 30
 31    fn sk_out_n(&self) -> usize {
 32        self.sk_out_n
 33    }
 34}
 35
 36impl<D: Data, B: Backend> LWEInfos for GLWESwitchingKeyPrepared<D, B> {
 37    fn n(&self) -> Degree {
 38        self.key.n()
 39    }
 40
 41    fn base2k(&self) -> Base2K {
 42        self.key.base2k()
 43    }
 44
 45    fn k(&self) -> TorusPrecision {
 46        self.key.k()
 47    }
 48
 49    fn size(&self) -> usize {
 50        self.key.size()
 51    }
 52}
 53
 54impl<D: Data, B: Backend> GLWEInfos for GLWESwitchingKeyPrepared<D, B> {
 55    fn rank(&self) -> Rank {
 56        self.rank_out()
 57    }
 58}
 59
 60impl<D: Data, B: Backend> GGLWEInfos for GLWESwitchingKeyPrepared<D, B> {
 61    fn rank_in(&self) -> Rank {
 62        self.key.rank_in()
 63    }
 64
 65    fn rank_out(&self) -> Rank {
 66        self.key.rank_out()
 67    }
 68
 69    fn dsize(&self) -> Dsize {
 70        self.key.dsize()
 71    }
 72
 73    fn dnum(&self) -> Dnum {
 74        self.key.dnum()
 75    }
 76}
 77
 78pub trait GLWESwitchingKeyPreparedAlloc<B: Backend>
 79where
 80    Self: GGLWEPreparedAlloc<B>,
 81{
 82    fn alloc_glwe_switching_key_prepared(
 83        &self,
 84        base2k: Base2K,
 85        k: TorusPrecision,
 86        rank_in: Rank,
 87        rank_out: Rank,
 88        dnum: Dnum,
 89        dsize: Dsize,
 90    ) -> GLWESwitchingKeyPrepared<Vec<u8>, B> {
 91        GLWESwitchingKeyPrepared::<Vec<u8>, B> {
 92            key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize),
 93            sk_in_n: 0,
 94            sk_out_n: 0,
 95        }
 96    }
 97
 98    fn alloc_glwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> GLWESwitchingKeyPrepared<Vec<u8>, B>
 99    where
100        A: GGLWEInfos,
101    {
102        self.alloc_glwe_switching_key_prepared(
103            infos.base2k(),
104            infos.k(),
105            infos.rank_in(),
106            infos.rank_out(),
107            infos.dnum(),
108            infos.dsize(),
109        )
110    }
111
112    fn bytes_of_glwe_switching_key_prepared(
113        &self,
114        base2k: Base2K,
115        k: TorusPrecision,
116        rank_in: Rank,
117        rank_out: Rank,
118        dnum: Dnum,
119        dsize: Dsize,
120    ) -> usize {
121        self.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
122    }
123
124    fn bytes_of_glwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> usize
125    where
126        A: GGLWEInfos,
127    {
128        self.bytes_of_glwe_switching_key_prepared(
129            infos.base2k(),
130            infos.k(),
131            infos.rank_in(),
132            infos.rank_out(),
133            infos.dnum(),
134            infos.dsize(),
135        )
136    }
137}
138
139impl<B: Backend> GLWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GGLWEPreparedAlloc<B> {}
140
141impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
142    pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
143    where
144        A: GGLWEInfos,
145        M: GLWESwitchingKeyPreparedAlloc<B>,
146    {
147        module.alloc_glwe_switching_key_prepared_from_infos(infos)
148    }
149
150    pub fn alloc<M>(
151        module: &M,
152        base2k: Base2K,
153        k: TorusPrecision,
154        rank_in: Rank,
155        rank_out: Rank,
156        dnum: Dnum,
157        dsize: Dsize,
158    ) -> Self
159    where
160        M: GLWESwitchingKeyPreparedAlloc<B>,
161    {
162        module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
163    }
164
165    pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
166    where
167        A: GGLWEInfos,
168        M: GLWESwitchingKeyPreparedAlloc<B>,
169    {
170        module.bytes_of_glwe_switching_key_prepared_from_infos(infos)
171    }
172
173    pub fn bytes_of<M>(
174        module: &M,
175        base2k: Base2K,
176        k: TorusPrecision,
177        rank_in: Rank,
178        rank_out: Rank,
179        dnum: Dnum,
180        dsize: Dsize,
181    ) -> usize
182    where
183        M: GLWESwitchingKeyPreparedAlloc<B>,
184    {
185        module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
186    }
187}
188
189pub trait GLWESwitchingKeyPrepare<B: Backend>
190where
191    Self: GGLWEPrepare<B>,
192{
193    fn prepare_glwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize
194    where
195        A: GGLWEInfos,
196    {
197        self.prepare_gglwe_tmp_bytes(infos)
198    }
199
200    fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
201    where
202        R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeySetMetaData,
203        O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
204    {
205        self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch);
206        res.set_sk_in_n(other.sk_in_n());
207        res.set_sk_out_n(other.sk_out_n());
208    }
209}
210
211impl<B: Backend> GLWESwitchingKeyPrepare<B> for Module<B> where Self: GGLWEPrepare<B> {}
212
213impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
214    pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
215    where
216        O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
217        M: GLWESwitchingKeyPrepare<B>,
218    {
219        module.prepare_glwe_switching(self, other, scratch);
220    }
221}
222
223impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
224    pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
225    where
226        M: GLWESwitchingKeyPrepare<B>,
227    {
228        module.prepare_glwe_switching_key_tmp_bytes(self)
229    }
230}
231
232pub trait GLWESwitchingKeyPreparedToMut<B: Backend> {
233    fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>;
234}
235
236impl<D: DataMut, B: Backend> GLWESwitchingKeyPreparedToMut<B> for GLWESwitchingKeyPrepared<D, B> {
237    fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> {
238        GLWESwitchingKeyPrepared {
239            sk_in_n: self.sk_in_n,
240            sk_out_n: self.sk_out_n,
241            key: self.key.to_mut(),
242        }
243    }
244}
245
246pub trait GLWESwitchingKeyPreparedToRef<B: Backend> {
247    fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>;
248}
249
250impl<D: DataRef, B: Backend> GLWESwitchingKeyPreparedToRef<B> for GLWESwitchingKeyPrepared<D, B> {
251    fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> {
252        GLWESwitchingKeyPrepared {
253            sk_in_n: self.sk_in_n,
254            sk_out_n: self.sk_out_n,
255            key: self.key.to_ref(),
256        }
257    }
258}