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, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
  7        GLWESwitchingKeyPreparedToRef,
  8    },
  9};
 10
 11#[derive(PartialEq, Eq)]
 12pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
 13    pub(crate) key: GLWESwitchingKeyPrepared<D, B>,
 14    pub(crate) p: i64,
 15}
 16
 17impl<D: Data, B: Backend> LWEInfos for AutomorphismKeyPrepared<D, B> {
 18    fn n(&self) -> Degree {
 19        self.key.n()
 20    }
 21
 22    fn base2k(&self) -> Base2K {
 23        self.key.base2k()
 24    }
 25
 26    fn k(&self) -> TorusPrecision {
 27        self.key.k()
 28    }
 29
 30    fn size(&self) -> usize {
 31        self.key.size()
 32    }
 33}
 34
 35pub trait GetAutomorphismGaloisElement {
 36    fn p(&self) -> i64;
 37}
 38
 39impl<D: Data, B: Backend> GetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
 40    fn p(&self) -> i64 {
 41        self.p
 42    }
 43}
 44
 45pub trait SetAutomorphismGaloisElement {
 46    fn set_p(&mut self, p: i64);
 47}
 48
 49impl<D: Data, B: Backend> SetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
 50    fn set_p(&mut self, p: i64) {
 51        self.p = p
 52    }
 53}
 54
 55impl<D: Data, B: Backend> GLWEInfos for AutomorphismKeyPrepared<D, B> {
 56    fn rank(&self) -> Rank {
 57        self.rank_out()
 58    }
 59}
 60
 61impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
 62    fn rank_in(&self) -> Rank {
 63        self.key.rank_in()
 64    }
 65
 66    fn rank_out(&self) -> Rank {
 67        self.key.rank_out()
 68    }
 69
 70    fn dsize(&self) -> Dsize {
 71        self.key.dsize()
 72    }
 73
 74    fn dnum(&self) -> Dnum {
 75        self.key.dnum()
 76    }
 77}
 78
 79pub trait AutomorphismKeyPreparedAlloc<B: Backend>
 80where
 81    Self: GLWESwitchingKeyPreparedAlloc<B>,
 82{
 83    fn alloc_automorphism_key_prepared(
 84        &self,
 85        base2k: Base2K,
 86        k: TorusPrecision,
 87        rank: Rank,
 88        dnum: Dnum,
 89        dsize: Dsize,
 90    ) -> AutomorphismKeyPrepared<Vec<u8>, B> {
 91        AutomorphismKeyPrepared::<Vec<u8>, B> {
 92            key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize),
 93            p: 0,
 94        }
 95    }
 96
 97    fn alloc_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
 98    where
 99        A: GGLWEInfos,
100    {
101        assert_eq!(
102            infos.rank_in(),
103            infos.rank_out(),
104            "rank_in != rank_out is not supported for AutomorphismKeyPrepared"
105        );
106        self.alloc_automorphism_key_prepared(
107            infos.base2k(),
108            infos.k(),
109            infos.rank(),
110            infos.dnum(),
111            infos.dsize(),
112        )
113    }
114
115    fn bytes_of_automorphism_key_prepared(
116        &self,
117        base2k: Base2K,
118        k: TorusPrecision,
119        rank: Rank,
120        dnum: Dnum,
121        dsize: Dsize,
122    ) -> usize {
123        self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize)
124    }
125
126    fn bytes_of_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize
127    where
128        A: GGLWEInfos,
129    {
130        assert_eq!(
131            infos.rank_in(),
132            infos.rank_out(),
133            "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
134        );
135        self.bytes_of_automorphism_key_prepared(
136            infos.base2k(),
137            infos.k(),
138            infos.rank(),
139            infos.dnum(),
140            infos.dsize(),
141        )
142    }
143}
144
145impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {}
146
147impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
148    pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
149    where
150        A: GGLWEInfos,
151        M: AutomorphismKeyPreparedAlloc<B>,
152    {
153        module.alloc_automorphism_key_prepared_from_infos(infos)
154    }
155
156    pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
157    where
158        M: AutomorphismKeyPreparedAlloc<B>,
159    {
160        module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
161    }
162
163    pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
164    where
165        A: GGLWEInfos,
166        M: AutomorphismKeyPreparedAlloc<B>,
167    {
168        module.bytes_of_automorphism_key_prepared_from_infos(infos)
169    }
170
171    pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
172    where
173        M: AutomorphismKeyPreparedAlloc<B>,
174    {
175        module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
176    }
177}
178
179pub trait PrepareAutomorphismKey<B: Backend>
180where
181    Self: GLWESwitchingKeyPrepare<B>,
182{
183    fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
184    where
185        A: GGLWEInfos,
186    {
187        self.prepare_glwe_switching_key_tmp_bytes(infos)
188    }
189
190    fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
191    where
192        R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
193        O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
194    {
195        self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch);
196        res.set_p(other.p());
197    }
198}
199
200impl<B: Backend> PrepareAutomorphismKey<B> for Module<B> where Module<B>: GLWESwitchingKeyPrepare<B> {}
201
202impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
203    pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
204    where
205        M: PrepareAutomorphismKey<B>,
206    {
207        module.prepare_automorphism_key_tmp_bytes(self)
208    }
209}
210
211impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
212    pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
213    where
214        O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
215        M: PrepareAutomorphismKey<B>,
216    {
217        module.prepare_automorphism_key(self, other, scratch);
218    }
219}
220
221pub trait AutomorphismKeyPreparedToMut<B: Backend> {
222    fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>;
223}
224
225impl<D: DataMut, B: Backend> AutomorphismKeyPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
226    fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> {
227        AutomorphismKeyPrepared {
228            p: self.p,
229            key: self.key.to_mut(),
230        }
231    }
232}
233
234pub trait AutomorphismKeyPreparedToRef<B: Backend> {
235    fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>;
236}
237
238impl<D: DataRef, B: Backend> AutomorphismKeyPreparedToRef<B> for AutomorphismKeyPrepared<D, B> {
239    fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> {
240        AutomorphismKeyPrepared {
241            p: self.p,
242            key: self.key.to_ref(),
243        }
244    }
245}