arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/prepared/gglwe_atk.rs
poulpy-core/src/layouts/prepared/gglwe_atk.rsRaw
  1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
  2
  3use crate::layouts::{
  4    AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
  5    prepared::{
  6        GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc,
  7        GLWESwitchingKeyPreparedAllocBytes, GLWESwitchingKeyPreparedAllocBytesFromInfos, GLWESwitchingKeyPreparedAllocFromInfos,
  8        GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef,
  9    },
 10};
 11
 12#[derive(PartialEq, Eq)]
 13pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
 14    pub(crate) key: GLWESwitchingKeyPrepared<D, B>,
 15    pub(crate) p: i64,
 16}
 17
 18impl<D: Data, B: Backend> LWEInfos for AutomorphismKeyPrepared<D, B> {
 19    fn n(&self) -> Degree {
 20        self.key.n()
 21    }
 22
 23    fn base2k(&self) -> Base2K {
 24        self.key.base2k()
 25    }
 26
 27    fn k(&self) -> TorusPrecision {
 28        self.key.k()
 29    }
 30
 31    fn size(&self) -> usize {
 32        self.key.size()
 33    }
 34}
 35
 36pub trait GetAutomorphismGaloisElement {
 37    fn p(&self) -> i64;
 38}
 39
 40impl<D: Data, B: Backend> GetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
 41    fn p(&self) -> i64 {
 42        self.p
 43    }
 44}
 45
 46pub trait SetAutomorphismGaloisElement {
 47    fn set_p(&mut self, p: i64);
 48}
 49
 50impl<D: Data, B: Backend> SetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
 51    fn set_p(&mut self, p: i64) {
 52        self.p = p
 53    }
 54}
 55
 56impl<D: Data, B: Backend> GLWEInfos for AutomorphismKeyPrepared<D, B> {
 57    fn rank(&self) -> Rank {
 58        self.rank_out()
 59    }
 60}
 61
 62impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
 63    fn rank_in(&self) -> Rank {
 64        self.key.rank_in()
 65    }
 66
 67    fn rank_out(&self) -> Rank {
 68        self.key.rank_out()
 69    }
 70
 71    fn dsize(&self) -> Dsize {
 72        self.key.dsize()
 73    }
 74
 75    fn dnum(&self) -> Dnum {
 76        self.key.dnum()
 77    }
 78}
 79
 80pub trait AutomorphismKeyPreparedAlloc<B: Backend> {
 81    fn automorphism_key_prepared_alloc(
 82        &self,
 83        base2k: Base2K,
 84        k: TorusPrecision,
 85        rank: Rank,
 86        dnum: Dnum,
 87        dsize: Dsize,
 88    ) -> AutomorphismKeyPrepared<Vec<u8>, B>;
 89}
 90
 91impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B>
 92where
 93    Module<B>: GLWESwitchingKeyPreparedAlloc<B>,
 94{
 95    fn automorphism_key_prepared_alloc(
 96        &self,
 97        base2k: Base2K,
 98        k: TorusPrecision,
 99        rank: Rank,
100        dnum: Dnum,
101        dsize: Dsize,
102    ) -> AutomorphismKeyPrepared<Vec<u8>, B> {
103        AutomorphismKeyPrepared::<Vec<u8>, B> {
104            key: self.glwe_switching_key_prepared_alloc(base2k, k, rank, rank, dnum, dsize),
105            p: 0,
106        }
107    }
108}
109
110pub trait AutomorphismKeyPreparedAllocFromInfos<B: Backend> {
111    fn automorphism_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
112    where
113        A: GGLWEInfos;
114}
115
116impl<B: Backend> AutomorphismKeyPreparedAllocFromInfos<B> for Module<B>
117where
118    Module<B>: GLWESwitchingKeyPreparedAllocFromInfos<B>,
119{
120    fn automorphism_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
121    where
122        A: GGLWEInfos,
123    {
124        assert_eq!(
125            infos.rank_in(),
126            infos.rank_out(),
127            "rank_in != rank_out is not supported for AutomorphismKeyPrepared"
128        );
129        AutomorphismKeyPrepared {
130            key: self.glwe_switching_key_prepared_alloc_from_infos(infos),
131            p: 0,
132        }
133    }
134}
135
136pub trait AutomorphismKeyPreparedAllocBytes<B: Backend> {
137    fn automorphism_key_prepared_alloc_bytes(
138        &self,
139        base2k: Base2K,
140        k: TorusPrecision,
141        rank: Rank,
142        dnum: Dnum,
143        dsize: Dsize,
144    ) -> usize;
145}
146
147impl<B: Backend> AutomorphismKeyPreparedAllocBytes<B> for Module<B>
148where
149    Module<B>: GLWESwitchingKeyPreparedAllocBytes<B>,
150{
151    fn automorphism_key_prepared_alloc_bytes(
152        &self,
153        base2k: Base2K,
154        k: TorusPrecision,
155        rank: Rank,
156        dnum: Dnum,
157        dsize: Dsize,
158    ) -> usize {
159        self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank, rank, dnum, dsize)
160    }
161}
162
163pub trait AutomorphismKeyPreparedAllocBytesFromInfos<B: Backend> {
164    fn automorphism_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize
165    where
166        A: GGLWEInfos;
167}
168
169impl<B: Backend> AutomorphismKeyPreparedAllocBytesFromInfos<B> for Module<B>
170where
171    Module<B>: GLWESwitchingKeyPreparedAllocBytesFromInfos<B>,
172{
173    fn automorphism_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize
174    where
175        A: GGLWEInfos,
176    {
177        assert_eq!(
178            infos.rank_in(),
179            infos.rank_out(),
180            "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
181        );
182        self.glwe_switching_key_prepared_alloc_bytes_from_infos(infos)
183    }
184}
185
186impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
187    pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self
188    where
189        A: GGLWEInfos,
190        Module<B>: AutomorphismKeyPreparedAllocFromInfos<B>,
191    {
192        module.automorphism_key_prepared_alloc_from_infos(infos)
193    }
194
195    pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
196    where
197        Module<B>: AutomorphismKeyPreparedAlloc<B>,
198    {
199        module.automorphism_key_prepared_alloc(base2k, k, rank, dnum, dsize)
200    }
201
202    pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize
203    where
204        A: GGLWEInfos,
205        Module<B>: AutomorphismKeyPreparedAllocBytesFromInfos<B>,
206    {
207        module.automorphism_key_prepared_alloc_bytes_from_infos(infos)
208    }
209
210    pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
211    where
212        Module<B>: AutomorphismKeyPreparedAllocBytes<B>,
213    {
214        module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize)
215    }
216}
217
218pub trait AutomorphismKeyPrepareTmpBytes {
219    fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
220    where
221        A: GGLWEInfos;
222}
223
224impl<B: Backend> AutomorphismKeyPrepareTmpBytes for Module<B>
225where
226    Module<B>: GLWESwitchingKeyPrepareTmpBytes,
227{
228    fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
229    where
230        A: GGLWEInfos,
231    {
232        self.glwe_switching_key_prepare_tmp_bytes(infos)
233    }
234}
235
236impl<D: DataRef, B: Backend> AutomorphismKeyPrepared<D, B> {
237    pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize
238    where
239        Module<B>: AutomorphismKeyPrepareTmpBytes,
240    {
241        module.automorphism_key_prepare_tmp_bytes(self)
242    }
243}
244
245pub trait AutomorphismKeyPrepare<B: Backend> {
246    fn automorphism_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
247    where
248        R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
249        O: AutomorphismKeyToRef + GetAutomorphismGaloisElement;
250}
251
252impl<B: Backend> AutomorphismKeyPrepare<B> for Module<B>
253where
254    Module<B>: GLWESwitchingKeyPrepare<B>,
255{
256    fn automorphism_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
257    where
258        R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
259        O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
260    {
261        self.glwe_switching_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch);
262        res.set_p(other.p());
263    }
264}
265
266pub trait AutomorphismKeyPreparedToMut<B: Backend> {
267    fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>;
268}
269
270impl<D: DataMut, B: Backend> AutomorphismKeyPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
271    fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> {
272        AutomorphismKeyPrepared {
273            p: self.p,
274            key: self.key.to_mut(),
275        }
276    }
277}
278
279pub trait AutomorphismKeyPreparedToRef<B: Backend> {
280    fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>;
281}
282
283impl<D: DataRef, B: Backend> AutomorphismKeyPreparedToRef<B> for AutomorphismKeyPrepared<D, B> {
284    fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> {
285        AutomorphismKeyPrepared {
286            p: self.p,
287            key: self.key.to_ref(),
288        }
289    }
290}