poulpy/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs
poulpy-core/src/layouts/prepared/glwe_tensor_key.rsRaw
1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
2
3use crate::layouts::{
4 Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef,
5 GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
6};
7
8#[derive(PartialEq, Eq)]
9pub struct GLWETensorKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWEPrepared<D, B>);
10
11impl<D: Data, B: Backend> LWEInfos for GLWETensorKeyPrepared<D, B> {
12 fn n(&self) -> Degree {
13 self.0.n()
14 }
15
16 fn base2k(&self) -> Base2K {
17 self.0.base2k()
18 }
19
20 fn k(&self) -> TorusPrecision {
21 self.0.k()
22 }
23
24 fn size(&self) -> usize {
25 self.0.size()
26 }
27}
28
29impl<D: Data, B: Backend> GLWEInfos for GLWETensorKeyPrepared<D, B> {
30 fn rank(&self) -> Rank {
31 self.rank_out()
32 }
33}
34
35impl<D: Data, B: Backend> GGLWEInfos for GLWETensorKeyPrepared<D, B> {
36 fn rank_in(&self) -> Rank {
37 self.rank_out()
38 }
39
40 fn rank_out(&self) -> Rank {
41 self.0.rank_out()
42 }
43
44 fn dsize(&self) -> Dsize {
45 self.0.dsize()
46 }
47
48 fn dnum(&self) -> Dnum {
49 self.0.dnum()
50 }
51}
52
53pub trait GLWETensorKeyPreparedFactory<B: Backend>
54where
55 Self: GGLWEPreparedFactory<B>,
56{
57 fn alloc_tensor_key_prepared(
58 &self,
59 base2k: Base2K,
60 k: TorusPrecision,
61 dnum: Dnum,
62 dsize: Dsize,
63 rank: Rank,
64 ) -> GLWETensorKeyPrepared<Vec<u8>, B> {
65 let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
66 GLWETensorKeyPrepared(self.alloc_gglwe_prepared(base2k, k, Rank(pairs), rank, dnum, dsize))
67 }
68
69 fn alloc_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> GLWETensorKeyPrepared<Vec<u8>, B>
70 where
71 A: GGLWEInfos,
72 {
73 assert_eq!(
74 infos.rank_in(),
75 infos.rank_out(),
76 "rank_in != rank_out is not supported for TensorKeyPrepared"
77 );
78 self.alloc_tensor_key_prepared(
79 infos.base2k(),
80 infos.k(),
81 infos.dnum(),
82 infos.dsize(),
83 infos.rank_out(),
84 )
85 }
86
87 fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
88 let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
89 self.bytes_of_gglwe_prepared(base2k, k, Rank(pairs), rank, dnum, dsize)
90 }
91
92 fn bytes_of_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> usize
93 where
94 A: GGLWEInfos,
95 {
96 self.bytes_of_tensor_key_prepared(
97 infos.base2k(),
98 infos.k(),
99 infos.rank(),
100 infos.dnum(),
101 infos.dsize(),
102 )
103 }
104
105 fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize
106 where
107 A: GGLWEInfos,
108 {
109 self.prepare_gglwe_tmp_bytes(infos)
110 }
111
112 fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
113 where
114 R: GGLWEPreparedToMut<B>,
115 O: GGLWEToRef,
116 {
117 self.prepare_gglwe(res, other, scratch);
118 }
119}
120
121impl<B: Backend> GLWETensorKeyPreparedFactory<B> for Module<B> where Module<B>: GGLWEPreparedFactory<B> {}
122
123impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> {
124 pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
125 where
126 A: GGLWEInfos,
127 M: GLWETensorKeyPreparedFactory<B>,
128 {
129 module.alloc_tensor_key_prepared_from_infos(infos)
130 }
131
132 pub fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
133 where
134 M: GLWETensorKeyPreparedFactory<B>,
135 {
136 module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank)
137 }
138
139 pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
140 where
141 A: GGLWEInfos,
142 M: GLWETensorKeyPreparedFactory<B>,
143 {
144 module.bytes_of_tensor_key_prepared_from_infos(infos)
145 }
146
147 pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
148 where
149 M: GLWETensorKeyPreparedFactory<B>,
150 {
151 module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize)
152 }
153}
154
155impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> {
156 pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
157 where
158 A: GGLWEInfos,
159 M: GLWETensorKeyPreparedFactory<B>,
160 {
161 module.prepare_tensor_key_tmp_bytes(infos)
162 }
163}
164
165impl<D: DataMut, B: Backend> GLWETensorKeyPrepared<D, B> {
166 pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
167 where
168 O: GGLWEToRef,
169 M: GLWETensorKeyPreparedFactory<B>,
170 {
171 module.prepare_tensor_key(self, other, scratch);
172 }
173}
174
175impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWETensorKeyPrepared<D, B>
176where
177 GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
178{
179 fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
180 self.0.to_mut()
181 }
182}
183
184impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GLWETensorKeyPrepared<D, B>
185where
186 GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
187{
188 fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
189 self.0.to_ref()
190 }
191}