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