poulpy/core/src/layouts/prepared/lwe_to_glwe_ksk.rs
core/src/layouts/prepared/lwe_to_glwe_ksk.rsRaw
1use backend::hal::{
2 api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPMatPrepare},
3 layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
4};
5
6use crate::layouts::{Infos, LWEToGLWESwitchingKey, prepared::GGLWESwitchingKeyExec};
7
8/// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext].
9#[derive(PartialEq, Eq)]
10pub struct LWEToGLWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>);
11
12impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyExec<D, B> {
13 type Inner = VmpPMat<D, B>;
14
15 fn inner(&self) -> &Self::Inner {
16 &self.0.inner()
17 }
18
19 fn basek(&self) -> usize {
20 self.0.basek()
21 }
22
23 fn k(&self) -> usize {
24 self.0.k()
25 }
26}
27
28impl<D: Data, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
29 pub fn digits(&self) -> usize {
30 self.0.digits()
31 }
32
33 pub fn rank(&self) -> usize {
34 self.0.rank()
35 }
36
37 pub fn rank_in(&self) -> usize {
38 self.0.rank_in()
39 }
40
41 pub fn rank_out(&self) -> usize {
42 self.0.rank_out()
43 }
44}
45
46impl<B: Backend> LWEToGLWESwitchingKeyExec<Vec<u8>, B> {
47 pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self
48 where
49 Module<B>: VmpPMatAlloc<B>,
50 {
51 Self(GGLWESwitchingKeyExec::alloc(
52 module, n, basek, k, rows, 1, 1, rank_out,
53 ))
54 }
55
56 pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_out: usize) -> usize
57 where
58 Module<B>: VmpPMatAllocBytes,
59 {
60 GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out)
61 }
62
63 pub fn from<DataOther: DataRef>(
64 module: &Module<B>,
65 other: &LWEToGLWESwitchingKey<DataOther>,
66 scratch: &mut Scratch<B>,
67 ) -> Self
68 where
69 Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
70 {
71 let mut ksk_exec: LWEToGLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
72 module,
73 other.0.n(),
74 other.0.basek(),
75 other.0.k(),
76 other.0.rows(),
77 other.0.rank(),
78 );
79 ksk_exec.prepare(module, other, scratch);
80 ksk_exec
81 }
82}
83
84impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
85 pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
86 where
87 DataOther: DataRef,
88 Module<B>: VmpPMatPrepare<B>,
89 {
90 self.0.prepare(module, &other.0, scratch);
91 }
92}