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