arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/lwe/keyswitch_layouts_exec.rs
core/src/lwe/keyswitch_layouts_exec.rsRaw
  1use backend::hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat};
  2
  3use crate::{
  4    GGLWEExecLayoutFamily, GLWESwitchingKeyExec, Infos,
  5    lwe::keyswtich_layouts::{GLWEToLWESwitchingKey, LWESwitchingKey, LWEToGLWESwitchingKey},
  6};
  7
  8#[derive(PartialEq, Eq)]
  9pub struct GLWEToLWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyExec<D, B>);
 10
 11impl<D: Data, B: Backend> Infos for GLWEToLWESwitchingKeyExec<D, B> {
 12    type Inner = VmpPMat<D, B>;
 13
 14    fn inner(&self) -> &Self::Inner {
 15        &self.0.inner()
 16    }
 17
 18    fn basek(&self) -> usize {
 19        self.0.basek()
 20    }
 21
 22    fn k(&self) -> usize {
 23        self.0.k()
 24    }
 25}
 26
 27impl<D: Data, B: Backend> GLWEToLWESwitchingKeyExec<D, B> {
 28    pub fn digits(&self) -> usize {
 29        self.0.digits()
 30    }
 31
 32    pub fn rank(&self) -> usize {
 33        self.0.rank()
 34    }
 35
 36    pub fn rank_in(&self) -> usize {
 37        self.0.rank_in()
 38    }
 39
 40    pub fn rank_out(&self) -> usize {
 41        self.0.rank_out()
 42    }
 43}
 44
 45impl<B: Backend> GLWEToLWESwitchingKeyExec<Vec<u8>, B> {
 46    pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_in: usize) -> Self
 47    where
 48        Module<B>: GGLWEExecLayoutFamily<B>,
 49    {
 50        Self(GLWESwitchingKeyExec::alloc(
 51            module, n, basek, k, rows, 1, rank_in, 1,
 52        ))
 53    }
 54
 55    pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize) -> usize
 56    where
 57        Module<B>: GGLWEExecLayoutFamily<B>,
 58    {
 59        GLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1)
 60    }
 61
 62    pub fn from<DataOther: DataRef>(
 63        module: &Module<B>,
 64        other: &GLWEToLWESwitchingKey<DataOther>,
 65        scratch: &mut Scratch<B>,
 66    ) -> Self
 67    where
 68        Module<B>: GGLWEExecLayoutFamily<B>,
 69    {
 70        let mut ksk_exec: GLWEToLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
 71            module,
 72            other.0.n(),
 73            other.0.basek(),
 74            other.0.k(),
 75            other.0.rows(),
 76            other.0.rank_in(),
 77        );
 78        ksk_exec.prepare(module, other, scratch);
 79        ksk_exec
 80    }
 81}
 82
 83impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyExec<D, B> {
 84    pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GLWEToLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
 85    where
 86        DataOther: DataRef,
 87        Module<B>: GGLWEExecLayoutFamily<B>,
 88    {
 89        self.0.prepare(module, &other.0, scratch);
 90    }
 91}
 92
 93/// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext].
 94#[derive(PartialEq, Eq)]
 95pub struct LWEToGLWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyExec<D, B>);
 96
 97impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyExec<D, B> {
 98    type Inner = VmpPMat<D, B>;
 99
100    fn inner(&self) -> &Self::Inner {
101        &self.0.inner()
102    }
103
104    fn basek(&self) -> usize {
105        self.0.basek()
106    }
107
108    fn k(&self) -> usize {
109        self.0.k()
110    }
111}
112
113impl<D: Data, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
114    pub fn digits(&self) -> usize {
115        self.0.digits()
116    }
117
118    pub fn rank(&self) -> usize {
119        self.0.rank()
120    }
121
122    pub fn rank_in(&self) -> usize {
123        self.0.rank_in()
124    }
125
126    pub fn rank_out(&self) -> usize {
127        self.0.rank_out()
128    }
129}
130
131impl<B: Backend> LWEToGLWESwitchingKeyExec<Vec<u8>, B> {
132    pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self
133    where
134        Module<B>: GGLWEExecLayoutFamily<B>,
135    {
136        Self(GLWESwitchingKeyExec::alloc(
137            module, n, basek, k, rows, 1, 1, rank_out,
138        ))
139    }
140
141    pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_out: usize) -> usize
142    where
143        Module<B>: GGLWEExecLayoutFamily<B>,
144    {
145        GLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out)
146    }
147
148    pub fn from<DataOther: DataRef>(
149        module: &Module<B>,
150        other: &LWEToGLWESwitchingKey<DataOther>,
151        scratch: &mut Scratch<B>,
152    ) -> Self
153    where
154        Module<B>: GGLWEExecLayoutFamily<B>,
155    {
156        let mut ksk_exec: LWEToGLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
157            module,
158            other.0.n(),
159            other.0.basek(),
160            other.0.k(),
161            other.0.rows(),
162            other.0.rank(),
163        );
164        ksk_exec.prepare(module, other, scratch);
165        ksk_exec
166    }
167}
168
169impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
170    pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
171    where
172        DataOther: DataRef,
173        Module<B>: GGLWEExecLayoutFamily<B>,
174    {
175        self.0.prepare(module, &other.0, scratch);
176    }
177}
178
179#[derive(PartialEq, Eq)]
180pub struct LWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyExec<D, B>);
181
182impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<D, B> {
183    type Inner = VmpPMat<D, B>;
184
185    fn inner(&self) -> &Self::Inner {
186        &self.0.inner()
187    }
188
189    fn basek(&self) -> usize {
190        self.0.basek()
191    }
192
193    fn k(&self) -> usize {
194        self.0.k()
195    }
196}
197
198impl<D: Data, B: Backend> LWESwitchingKeyExec<D, B> {
199    pub fn digits(&self) -> usize {
200        self.0.digits()
201    }
202
203    pub fn rank(&self) -> usize {
204        self.0.rank()
205    }
206
207    pub fn rank_in(&self) -> usize {
208        self.0.rank_in()
209    }
210
211    pub fn rank_out(&self) -> usize {
212        self.0.rank_out()
213    }
214}
215
216impl<B: Backend> LWESwitchingKeyExec<Vec<u8>, B> {
217    pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize) -> Self
218    where
219        Module<B>: GGLWEExecLayoutFamily<B>,
220    {
221        Self(GLWESwitchingKeyExec::alloc(
222            module, n, basek, k, rows, 1, 1, 1,
223        ))
224    }
225
226    pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize) -> usize
227    where
228        Module<B>: GGLWEExecLayoutFamily<B>,
229    {
230        GLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1)
231    }
232
233    pub fn from<DataOther: DataRef>(module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) -> Self
234    where
235        Module<B>: GGLWEExecLayoutFamily<B>,
236    {
237        let mut ksk_exec: LWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
238            module,
239            other.0.n(),
240            other.0.basek(),
241            other.0.k(),
242            other.0.rows(),
243        );
244        ksk_exec.prepare(module, other, scratch);
245        ksk_exec
246    }
247}
248
249impl<D: DataMut, B: Backend> LWESwitchingKeyExec<D, B> {
250    pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
251    where
252        DataOther: DataRef,
253        Module<B>: GGLWEExecLayoutFamily<B>,
254    {
255        self.0.prepare(module, &other.0, scratch);
256    }
257}