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