poulpy/poulpy-core/src/layouts/prepared/gglwe_atk.rs
poulpy-core/src/layouts/prepared/gglwe_atk.rsRaw
1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
2
3use crate::layouts::{
4 Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
5 GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
6};
7
8#[derive(PartialEq, Eq)]
9pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
10 pub(crate) key: GGLWEPrepared<D, B>,
11 pub(crate) p: i64,
12}
13
14impl<D: Data, B: Backend> LWEInfos for AutomorphismKeyPrepared<D, B> {
15 fn n(&self) -> Degree {
16 self.key.n()
17 }
18
19 fn base2k(&self) -> Base2K {
20 self.key.base2k()
21 }
22
23 fn k(&self) -> TorusPrecision {
24 self.key.k()
25 }
26
27 fn size(&self) -> usize {
28 self.key.size()
29 }
30}
31
32pub trait GetAutomorphismGaloisElement {
33 fn p(&self) -> i64;
34}
35
36impl<D: Data, B: Backend> GetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
37 fn p(&self) -> i64 {
38 self.p
39 }
40}
41
42pub trait SetAutomorphismGaloisElement {
43 fn set_p(&mut self, p: i64);
44}
45
46impl<D: Data, B: Backend> SetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
47 fn set_p(&mut self, p: i64) {
48 self.p = p
49 }
50}
51
52impl<D: Data, B: Backend> GLWEInfos for AutomorphismKeyPrepared<D, B> {
53 fn rank(&self) -> Rank {
54 self.rank_out()
55 }
56}
57
58impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
59 fn rank_in(&self) -> Rank {
60 self.key.rank_in()
61 }
62
63 fn rank_out(&self) -> Rank {
64 self.key.rank_out()
65 }
66
67 fn dsize(&self) -> Dsize {
68 self.key.dsize()
69 }
70
71 fn dnum(&self) -> Dnum {
72 self.key.dnum()
73 }
74}
75
76pub trait AutomorphismKeyPreparedAlloc<B: Backend>
77where
78 Self: GGLWEPreparedAlloc<B>,
79{
80 fn alloc_automorphism_key_prepared(
81 &self,
82 base2k: Base2K,
83 k: TorusPrecision,
84 rank: Rank,
85 dnum: Dnum,
86 dsize: Dsize,
87 ) -> AutomorphismKeyPrepared<Vec<u8>, B> {
88 AutomorphismKeyPrepared::<Vec<u8>, B> {
89 key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize),
90 p: 0,
91 }
92 }
93
94 fn alloc_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
95 where
96 A: GGLWEInfos,
97 {
98 assert_eq!(
99 infos.rank_in(),
100 infos.rank_out(),
101 "rank_in != rank_out is not supported for AutomorphismKeyPrepared"
102 );
103 self.alloc_automorphism_key_prepared(
104 infos.base2k(),
105 infos.k(),
106 infos.rank(),
107 infos.dnum(),
108 infos.dsize(),
109 )
110 }
111
112 fn bytes_of_automorphism_key_prepared(
113 &self,
114 base2k: Base2K,
115 k: TorusPrecision,
116 rank: Rank,
117 dnum: Dnum,
118 dsize: Dsize,
119 ) -> usize {
120 self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize)
121 }
122
123 fn bytes_of_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize
124 where
125 A: GGLWEInfos,
126 {
127 assert_eq!(
128 infos.rank_in(),
129 infos.rank_out(),
130 "rank_in != rank_out is not supported for AutomorphismKeyPrepared"
131 );
132 self.bytes_of_automorphism_key_prepared(
133 infos.base2k(),
134 infos.k(),
135 infos.rank(),
136 infos.dnum(),
137 infos.dsize(),
138 )
139 }
140}
141
142impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
143
144impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
145 pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
146 where
147 A: GGLWEInfos,
148 M: AutomorphismKeyPreparedAlloc<B>,
149 {
150 module.alloc_automorphism_key_prepared_from_infos(infos)
151 }
152
153 pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
154 where
155 M: AutomorphismKeyPreparedAlloc<B>,
156 {
157 module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
158 }
159
160 pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
161 where
162 A: GGLWEInfos,
163 M: AutomorphismKeyPreparedAlloc<B>,
164 {
165 module.bytes_of_automorphism_key_prepared_from_infos(infos)
166 }
167
168 pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
169 where
170 M: AutomorphismKeyPreparedAlloc<B>,
171 {
172 module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
173 }
174}
175
176pub trait PrepareAutomorphismKey<B: Backend>
177where
178 Self: GGLWEPrepare<B>,
179{
180 fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
181 where
182 A: GGLWEInfos,
183 {
184 self.prepare_gglwe_tmp_bytes(infos)
185 }
186
187 fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
188 where
189 R: GGLWEPreparedToMut<B> + SetAutomorphismGaloisElement,
190 O: GGLWEToRef + GetAutomorphismGaloisElement,
191 {
192 self.prepare_gglwe(res, other, scratch);
193 res.set_p(other.p());
194 }
195}
196
197impl<B: Backend> PrepareAutomorphismKey<B> for Module<B> where Module<B>: GGLWEPrepare<B> {}
198
199impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
200 pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
201 where
202 M: PrepareAutomorphismKey<B>,
203 {
204 module.prepare_automorphism_key_tmp_bytes(self)
205 }
206}
207
208impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
209 pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
210 where
211 O: GGLWEToRef + GetAutomorphismGaloisElement,
212 M: PrepareAutomorphismKey<B>,
213 {
214 module.prepare_automorphism_key(self, other, scratch);
215 }
216}
217
218impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
219 fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
220 self.key.to_mut()
221 }
222}
223
224impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for AutomorphismKeyPrepared<D, BE> {
225 fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
226 self.key.to_ref()
227 }
228}