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