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