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