poulpy_core/layouts/prepared/
lwe_switching_key.rs

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