arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs
poulpy-core/src/layouts/prepared/glwe_automorphism_key.rsRaw
  1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
  2
  3use crate::layouts::{
  4    Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef,
  5    GGLWEToRef, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, TorusPrecision,
  6};
  7
  8#[derive(PartialEq, Eq)]
  9pub struct GLWEAutomorphismKeyPrepared<D: Data, B: Backend> {
 10    pub(crate) key: GGLWEPrepared<D, B>,
 11    pub(crate) p: i64,
 12}
 13
 14impl<D: Data, B: Backend> LWEInfos for GLWEAutomorphismKeyPrepared<D, B> {
 15    fn n(&self) -> Degree {
 16        self.key.n()
 17    }
 18
 19    fn base2k(&self) -> Base2K {
 20        self.key.base2k()
 21    }
 22
 23    fn k(&self) -> TorusPrecision {
 24        self.key.k()
 25    }
 26
 27    fn size(&self) -> usize {
 28        self.key.size()
 29    }
 30}
 31
 32impl<D: Data, B: Backend> GetGaloisElement for GLWEAutomorphismKeyPrepared<D, B> {
 33    fn p(&self) -> i64 {
 34        self.p
 35    }
 36}
 37
 38impl<D: Data, B: Backend> SetGaloisElement for GLWEAutomorphismKeyPrepared<D, B> {
 39    fn set_p(&mut self, p: i64) {
 40        self.p = p
 41    }
 42}
 43
 44impl<D: Data, B: Backend> GLWEInfos for GLWEAutomorphismKeyPrepared<D, B> {
 45    fn rank(&self) -> Rank {
 46        self.rank_out()
 47    }
 48}
 49
 50impl<D: Data, B: Backend> GGLWEInfos for GLWEAutomorphismKeyPrepared<D, B> {
 51    fn rank_in(&self) -> Rank {
 52        self.key.rank_in()
 53    }
 54
 55    fn rank_out(&self) -> Rank {
 56        self.key.rank_out()
 57    }
 58
 59    fn dsize(&self) -> Dsize {
 60        self.key.dsize()
 61    }
 62
 63    fn dnum(&self) -> Dnum {
 64        self.key.dnum()
 65    }
 66}
 67
 68pub trait GLWEAutomorphismKeyPreparedFactory<B: Backend>
 69where
 70    Self: GGLWEPreparedFactory<B>,
 71{
 72    fn alloc_glwe_automorphism_key_prepared(
 73        &self,
 74        base2k: Base2K,
 75        k: TorusPrecision,
 76        rank: Rank,
 77        dnum: Dnum,
 78        dsize: Dsize,
 79    ) -> GLWEAutomorphismKeyPrepared<Vec<u8>, B> {
 80        GLWEAutomorphismKeyPrepared::<Vec<u8>, B> {
 81            key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize),
 82            p: 0,
 83        }
 84    }
 85
 86    fn alloc_glwe_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> GLWEAutomorphismKeyPrepared<Vec<u8>, B>
 87    where
 88        A: GGLWEInfos,
 89    {
 90        assert_eq!(
 91            infos.rank_in(),
 92            infos.rank_out(),
 93            "rank_in != rank_out is not supported for AutomorphismKeyPrepared"
 94        );
 95        self.alloc_glwe_automorphism_key_prepared(
 96            infos.base2k(),
 97            infos.k(),
 98            infos.rank(),
 99            infos.dnum(),
100            infos.dsize(),
101        )
102    }
103
104    fn bytes_of_glwe_automorphism_key_prepared(
105        &self,
106        base2k: Base2K,
107        k: TorusPrecision,
108        rank: Rank,
109        dnum: Dnum,
110        dsize: Dsize,
111    ) -> usize {
112        self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize)
113    }
114
115    fn bytes_of_glwe_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize
116    where
117        A: GGLWEInfos,
118    {
119        assert_eq!(
120            infos.rank_in(),
121            infos.rank_out(),
122            "rank_in != rank_out is not supported for AutomorphismKeyPrepared"
123        );
124        self.bytes_of_glwe_automorphism_key_prepared(
125            infos.base2k(),
126            infos.k(),
127            infos.rank(),
128            infos.dnum(),
129            infos.dsize(),
130        )
131    }
132
133    fn prepare_glwe_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
134    where
135        A: GGLWEInfos,
136    {
137        self.prepare_gglwe_tmp_bytes(infos)
138    }
139
140    fn prepare_glwe_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
141    where
142        R: GGLWEPreparedToMut<B> + SetGaloisElement,
143        O: GGLWEToRef + GetGaloisElement,
144    {
145        self.prepare_gglwe(res, other, scratch);
146        res.set_p(other.p());
147    }
148}
149
150impl<B: Backend> GLWEAutomorphismKeyPreparedFactory<B> for Module<B> where Module<B>: GGLWEPreparedFactory<B> {}
151
152impl<B: Backend> GLWEAutomorphismKeyPrepared<Vec<u8>, B> {
153    pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
154    where
155        A: GGLWEInfos,
156        M: GLWEAutomorphismKeyPreparedFactory<B>,
157    {
158        module.alloc_glwe_automorphism_key_prepared_from_infos(infos)
159    }
160
161    pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
162    where
163        M: GLWEAutomorphismKeyPreparedFactory<B>,
164    {
165        module.alloc_glwe_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
166    }
167
168    pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
169    where
170        A: GGLWEInfos,
171        M: GLWEAutomorphismKeyPreparedFactory<B>,
172    {
173        module.bytes_of_glwe_automorphism_key_prepared_from_infos(infos)
174    }
175
176    pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
177    where
178        M: GLWEAutomorphismKeyPreparedFactory<B>,
179    {
180        module.bytes_of_glwe_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
181    }
182}
183
184impl<B: Backend> GLWEAutomorphismKeyPrepared<Vec<u8>, B> {
185    pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
186    where
187        M: GLWEAutomorphismKeyPreparedFactory<B>,
188    {
189        module.prepare_glwe_automorphism_key_tmp_bytes(self)
190    }
191}
192
193impl<D: DataMut, B: Backend> GLWEAutomorphismKeyPrepared<D, B> {
194    pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
195    where
196        O: GGLWEToRef + GetGaloisElement,
197        M: GLWEAutomorphismKeyPreparedFactory<B>,
198    {
199        module.prepare_glwe_automorphism_key(self, other, scratch);
200    }
201}
202
203impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWEAutomorphismKeyPrepared<D, B> {
204    fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
205        self.key.to_mut()
206    }
207}
208
209impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for GLWEAutomorphismKeyPrepared<D, BE> {
210    fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
211        self.key.to_ref()
212    }
213}