poulpy/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs
poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rsRaw
1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
2
3use crate::layouts::{
4 Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision,
5 prepared::{
6 GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
7 GLWESwitchingKeyPreparedToRef,
8 },
9};
10
11#[derive(PartialEq, Eq)]
12pub struct GLWEToLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
13
14impl<D: Data, B: Backend> LWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
15 fn base2k(&self) -> Base2K {
16 self.0.base2k()
17 }
18
19 fn k(&self) -> TorusPrecision {
20 self.0.k()
21 }
22
23 fn n(&self) -> RingDegree {
24 self.0.n()
25 }
26
27 fn size(&self) -> usize {
28 self.0.size()
29 }
30}
31
32impl<D: Data, B: Backend> GLWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
33 fn rank(&self) -> Rank {
34 self.rank_out()
35 }
36}
37
38impl<D: Data, B: Backend> GGLWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
39 fn rank_in(&self) -> Rank {
40 self.0.rank_in()
41 }
42
43 fn dsize(&self) -> Dsize {
44 self.0.dsize()
45 }
46
47 fn rank_out(&self) -> Rank {
48 self.0.rank_out()
49 }
50
51 fn dnum(&self) -> Dnum {
52 self.0.dnum()
53 }
54}
55
56pub trait GLWEToLWESwitchingKeyPreparedAlloc<B: Backend>
57where
58 Self: GLWESwitchingKeyPreparedAlloc<B>,
59{
60 fn alloc_glwe_to_lwe_switching_key_prepared(
61 &self,
62 base2k: Base2K,
63 k: TorusPrecision,
64 rank_in: Rank,
65 dnum: Dnum,
66 ) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
67 GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
68 }
69 fn alloc_glwe_to_lwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>
70 where
71 A: GGLWEInfos,
72 {
73 debug_assert_eq!(
74 infos.rank_out().0,
75 1,
76 "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
77 );
78 debug_assert_eq!(
79 infos.dsize().0,
80 1,
81 "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
82 );
83 self.alloc_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
84 }
85
86 fn bytes_of_glwe_to_lwe_switching_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
87 self.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
88 }
89
90 fn bytes_of_glwe_to_lwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> usize
91 where
92 A: GGLWEInfos,
93 {
94 debug_assert_eq!(
95 infos.rank_out().0,
96 1,
97 "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
98 );
99 debug_assert_eq!(
100 infos.dsize().0,
101 1,
102 "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
103 );
104 self.bytes_of_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
105 }
106}
107
108impl<B: Backend> GLWEToLWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GLWESwitchingKeyPreparedAlloc<B> {}
109
110impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
111 pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
112 where
113 A: GGLWEInfos,
114 M: GLWEToLWESwitchingKeyPreparedAlloc<B>,
115 {
116 module.alloc_glwe_to_lwe_switching_key_prepared_from_infos(infos)
117 }
118
119 pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self
120 where
121 M: GLWEToLWESwitchingKeyPreparedAlloc<B>,
122 {
123 module.alloc_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum)
124 }
125
126 pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
127 where
128 A: GGLWEInfos,
129 M: GLWEToLWESwitchingKeyPreparedAlloc<B>,
130 {
131 module.bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(infos)
132 }
133
134 pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize
135 where
136 M: GLWEToLWESwitchingKeyPreparedAlloc<B>,
137 {
138 module.bytes_of_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum)
139 }
140}
141
142pub trait GLWEToLWESwitchingKeyPrepare<B: Backend>
143where
144 Self: GLWESwitchingKeyPrepare<B>,
145{
146 fn prepare_glwe_to_lwe_switching_key_tmp_bytes<A>(&self, infos: &A)
147 where
148 A: GGLWEInfos,
149 {
150 self.prepare_glwe_switching_key_tmp_bytes(infos);
151 }
152
153 fn prepare_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
154 where
155 R: GLWEToLWESwitchingKeyPreparedToMut<B>,
156 O: GLWEToLWESwitchingKeyToRef,
157 {
158 self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
159 }
160}
161
162impl<B: Backend> GLWEToLWESwitchingKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
163
164impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
165 pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A)
166 where
167 A: GGLWEInfos,
168 M: GLWEToLWESwitchingKeyPrepare<B>,
169 {
170 module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos);
171 }
172}
173
174impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
175 pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
176 where
177 O: GLWEToLWESwitchingKeyToRef,
178 M: GLWEToLWESwitchingKeyPrepare<B>,
179 {
180 module.prepare_glwe_to_lwe_switching_key(self, other, scratch);
181 }
182}
183
184pub trait GLWEToLWESwitchingKeyPreparedToRef<B: Backend> {
185 fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>;
186}
187
188impl<D: DataRef, B: Backend> GLWEToLWESwitchingKeyPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
189where
190 GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
191{
192 fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> {
193 GLWEToLWESwitchingKeyPrepared(self.0.to_ref())
194 }
195}
196
197pub trait GLWEToLWESwitchingKeyPreparedToMut<B: Backend> {
198 fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>;
199}
200
201impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
202where
203 GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
204{
205 fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> {
206 GLWEToLWESwitchingKeyPrepared(self.0.to_mut())
207 }
208}