poulpy_core/layouts/prepared/
gglwe_ksk.rs1use poulpy_hal::{
2 api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare},
3 layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
4};
5
6use crate::layouts::{
7 GGLWESwitchingKey, Infos,
8 prepared::{GGLWECiphertextPrepared, Prepare, PrepareAlloc},
9};
10
11#[derive(PartialEq, Eq)]
12pub struct GGLWESwitchingKeyPrepared<D: Data, B: Backend> {
13 pub(crate) key: GGLWECiphertextPrepared<D, B>,
14 pub(crate) sk_in_n: usize, pub(crate) sk_out_n: usize, }
17
18impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
19 #[allow(clippy::too_many_arguments)]
20 pub fn alloc(
21 module: &Module<B>,
22 n: usize,
23 basek: usize,
24 k: usize,
25 rows: usize,
26 digits: usize,
27 rank_in: usize,
28 rank_out: usize,
29 ) -> Self
30 where
31 Module<B>: VmpPMatAlloc<B>,
32 {
33 GGLWESwitchingKeyPrepared::<Vec<u8>, B> {
34 key: GGLWECiphertextPrepared::alloc(module, n, basek, k, rows, digits, rank_in, rank_out),
35 sk_in_n: 0,
36 sk_out_n: 0,
37 }
38 }
39
40 #[allow(clippy::too_many_arguments)]
41 pub fn bytes_of(
42 module: &Module<B>,
43 n: usize,
44 basek: usize,
45 k: usize,
46 rows: usize,
47 digits: usize,
48 rank_in: usize,
49 rank_out: usize,
50 ) -> usize
51 where
52 Module<B>: VmpPMatAllocBytes,
53 {
54 GGLWECiphertextPrepared::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out)
55 }
56}
57
58impl<D: Data, B: Backend> Infos for GGLWESwitchingKeyPrepared<D, B> {
59 type Inner = VmpPMat<D, B>;
60
61 fn inner(&self) -> &Self::Inner {
62 self.key.inner()
63 }
64
65 fn basek(&self) -> usize {
66 self.key.basek()
67 }
68
69 fn k(&self) -> usize {
70 self.key.k()
71 }
72}
73
74impl<D: Data, B: Backend> GGLWESwitchingKeyPrepared<D, B> {
75 pub fn rank(&self) -> usize {
76 self.key.data.cols_out() - 1
77 }
78
79 pub fn rank_in(&self) -> usize {
80 self.key.data.cols_in()
81 }
82
83 pub fn rank_out(&self) -> usize {
84 self.key.data.cols_out() - 1
85 }
86
87 pub fn digits(&self) -> usize {
88 self.key.digits()
89 }
90
91 pub fn sk_degree_in(&self) -> usize {
92 self.sk_in_n
93 }
94
95 pub fn sk_degree_out(&self) -> usize {
96 self.sk_out_n
97 }
98}
99
100impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWESwitchingKey<DR>> for GGLWESwitchingKeyPrepared<D, B>
101where
102 Module<B>: VmpPrepare<B>,
103{
104 fn prepare(&mut self, module: &Module<B>, other: &GGLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
105 self.key.prepare(module, &other.key, scratch);
106 self.sk_in_n = other.sk_in_n;
107 self.sk_out_n = other.sk_out_n;
108 }
109}
110
111impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWESwitchingKeyPrepared<Vec<u8>, B>> for GGLWESwitchingKey<D>
112where
113 Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
114{
115 fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
116 let mut atk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = GGLWESwitchingKeyPrepared::alloc(
117 module,
118 self.n(),
119 self.basek(),
120 self.k(),
121 self.rows(),
122 self.digits(),
123 self.rank_in(),
124 self.rank_out(),
125 );
126 atk_prepared.prepare(module, self, scratch);
127 atk_prepared
128 }
129}