poulpy/core/src/layouts/prepared/lwe_ksk.rs
core/src/layouts/prepared/lwe_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, LWESwitchingKey, prepared::GGLWESwitchingKeyExec};
7
8#[derive(PartialEq, Eq)]
9pub struct LWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>);
10
11impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<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> LWESwitchingKeyExec<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> LWESwitchingKeyExec<Vec<u8>, B> {
46 pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize) -> Self
47 where
48 Module<B>: VmpPMatAlloc<B>,
49 {
50 Self(GGLWESwitchingKeyExec::alloc(
51 module, n, basek, k, rows, 1, 1, 1,
52 ))
53 }
54
55 pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize) -> usize
56 where
57 Module<B>: VmpPMatAllocBytes,
58 {
59 GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1)
60 }
61
62 pub fn from<DataOther: DataRef>(module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) -> Self
63 where
64 Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
65 {
66 let mut ksk_exec: LWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
67 module,
68 other.0.n(),
69 other.0.basek(),
70 other.0.k(),
71 other.0.rows(),
72 );
73 ksk_exec.prepare(module, other, scratch);
74 ksk_exec
75 }
76}
77
78impl<D: DataMut, B: Backend> LWESwitchingKeyExec<D, B> {
79 pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
80 where
81 DataOther: DataRef,
82 Module<B>: VmpPMatPrepare<B>,
83 {
84 self.0.prepare(module, &other.0, scratch);
85 }
86}