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