arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs
poulpy-core/src/layouts/glwe_to_lwe_ksk.rsRaw
  1use poulpy_hal::{
  2    layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
  3    source::Source,
  4};
  5
  6use crate::layouts::{
  7    Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut,
  8    GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision,
  9};
 10
 11use std::fmt;
 12
 13#[derive(PartialEq, Eq, Copy, Clone, Debug)]
 14pub struct GLWEToLWEKeyLayout {
 15    pub n: RingDegree,
 16    pub base2k: Base2K,
 17    pub k: TorusPrecision,
 18    pub rank_in: Rank,
 19    pub dnum: Dnum,
 20}
 21
 22impl LWEInfos for GLWEToLWEKeyLayout {
 23    fn n(&self) -> RingDegree {
 24        self.n
 25    }
 26
 27    fn base2k(&self) -> Base2K {
 28        self.base2k
 29    }
 30
 31    fn k(&self) -> TorusPrecision {
 32        self.k
 33    }
 34}
 35
 36impl GLWEInfos for GLWEToLWEKeyLayout {
 37    fn rank(&self) -> Rank {
 38        self.rank_out()
 39    }
 40}
 41
 42impl GGLWEInfos for GLWEToLWEKeyLayout {
 43    fn rank_in(&self) -> Rank {
 44        self.rank_in
 45    }
 46
 47    fn dsize(&self) -> Dsize {
 48        Dsize(1)
 49    }
 50
 51    fn rank_out(&self) -> Rank {
 52        Rank(1)
 53    }
 54
 55    fn dnum(&self) -> Dnum {
 56        self.dnum
 57    }
 58}
 59
 60/// A special [GLWESwitchingKey] required to for the conversion from [GLWE] to [LWE].
 61#[derive(PartialEq, Eq, Clone)]
 62pub struct GLWEToLWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
 63
 64impl<D: Data> LWEInfos for GLWEToLWESwitchingKey<D> {
 65    fn base2k(&self) -> Base2K {
 66        self.0.base2k()
 67    }
 68
 69    fn k(&self) -> TorusPrecision {
 70        self.0.k()
 71    }
 72
 73    fn n(&self) -> RingDegree {
 74        self.0.n()
 75    }
 76
 77    fn size(&self) -> usize {
 78        self.0.size()
 79    }
 80}
 81
 82impl<D: Data> GLWEInfos for GLWEToLWESwitchingKey<D> {
 83    fn rank(&self) -> Rank {
 84        self.rank_out()
 85    }
 86}
 87impl<D: Data> GGLWEInfos for GLWEToLWESwitchingKey<D> {
 88    fn rank_in(&self) -> Rank {
 89        self.0.rank_in()
 90    }
 91
 92    fn dsize(&self) -> Dsize {
 93        self.0.dsize()
 94    }
 95
 96    fn rank_out(&self) -> Rank {
 97        self.0.rank_out()
 98    }
 99
100    fn dnum(&self) -> Dnum {
101        self.0.dnum()
102    }
103}
104
105impl<D: DataRef> fmt::Debug for GLWEToLWESwitchingKey<D> {
106    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107        write!(f, "{self}")
108    }
109}
110
111impl<D: DataMut> FillUniform for GLWEToLWESwitchingKey<D> {
112    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
113        self.0.fill_uniform(log_bound, source);
114    }
115}
116
117impl<D: DataRef> fmt::Display for GLWEToLWESwitchingKey<D> {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        write!(f, "(GLWEToLWESwitchingKey) {}", self.0)
120    }
121}
122
123impl<D: DataMut> ReaderFrom for GLWEToLWESwitchingKey<D> {
124    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
125        self.0.read_from(reader)
126    }
127}
128
129impl<D: DataRef> WriterTo for GLWEToLWESwitchingKey<D> {
130    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
131        self.0.write_to(writer)
132    }
133}
134
135pub trait GLWEToLWESwitchingKeyAlloc
136where
137    Self: GLWESwitchingKeyAlloc,
138{
139    fn alloc_glwe_to_lwe_switching_key(
140        &self,
141        base2k: Base2K,
142        k: TorusPrecision,
143        rank_in: Rank,
144        dnum: Dnum,
145    ) -> GLWEToLWESwitchingKey<Vec<u8>> {
146        GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
147    }
148
149    fn alloc_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKey<Vec<u8>>
150    where
151        A: GGLWEInfos,
152    {
153        assert_eq!(
154            infos.rank_out().0,
155            1,
156            "rank_out > 1 is not supported for GLWEToLWESwitchingKey"
157        );
158        assert_eq!(
159            infos.dsize().0,
160            1,
161            "dsize > 1 is not supported for GLWEToLWESwitchingKey"
162        );
163        self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
164    }
165
166    fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
167        self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
168    }
169
170    fn bytes_of_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
171    where
172        A: GGLWEInfos,
173    {
174        assert_eq!(
175            infos.rank_out().0,
176            1,
177            "rank_out > 1 is not supported for GLWEToLWESwitchingKey"
178        );
179        assert_eq!(
180            infos.dsize().0,
181            1,
182            "dsize > 1 is not supported for GLWEToLWESwitchingKey"
183        );
184        self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
185    }
186}
187
188impl<B: Backend> GLWEToLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
189
190impl GLWEToLWESwitchingKey<Vec<u8>> {
191    pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
192    where
193        A: GGLWEInfos,
194        M: GLWEToLWESwitchingKeyAlloc,
195    {
196        module.alloc_glwe_to_lwe_switching_key_from_infos(infos)
197    }
198
199    pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self
200    where
201        M: GLWEToLWESwitchingKeyAlloc,
202    {
203        module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum)
204    }
205
206    pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
207    where
208        A: GGLWEInfos,
209        M: GLWEToLWESwitchingKeyAlloc,
210    {
211        module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos)
212    }
213
214    pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize
215    where
216        M: GLWEToLWESwitchingKeyAlloc,
217    {
218        module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum)
219    }
220}
221
222pub trait GLWEToLWESwitchingKeyToRef {
223    fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>;
224}
225
226impl<D: DataRef> GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey<D>
227where
228    GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
229{
230    fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> {
231        GLWEToLWESwitchingKey(self.0.to_ref())
232    }
233}
234
235pub trait GLWEToLWESwitchingKeyToMut {
236    fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>;
237}
238
239impl<D: DataMut> GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey<D>
240where
241    GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
242{
243    fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> {
244        GLWEToLWESwitchingKey(self.0.to_mut())
245    }
246}