arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/compressed/lwe_ct.rs
poulpy-core/src/layouts/compressed/lwe_ct.rsRaw
  1use std::fmt;
  2
  3use poulpy_hal::{
  4    api::ZnFillUniform,
  5    layouts::{
  6        Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos, ZnxView,
  7        ZnxViewMut,
  8    },
  9    source::Source,
 10};
 11
 12use crate::layouts::{Base2K, Degree, LWE, LWEInfos, LWEToMut, TorusPrecision};
 13
 14#[derive(PartialEq, Eq, Clone)]
 15pub struct LWECompressed<D: Data> {
 16    pub(crate) data: Zn<D>,
 17    pub(crate) k: TorusPrecision,
 18    pub(crate) base2k: Base2K,
 19    pub(crate) seed: [u8; 32],
 20}
 21
 22impl<D: Data> LWEInfos for LWECompressed<D> {
 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        Degree(self.data.n() as u32)
 33    }
 34
 35    fn size(&self) -> usize {
 36        self.data.size()
 37    }
 38}
 39
 40impl<D: DataRef> fmt::Debug for LWECompressed<D> {
 41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 42        write!(f, "{self}")
 43    }
 44}
 45
 46impl<D: DataRef> fmt::Display for LWECompressed<D> {
 47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 48        write!(
 49            f,
 50            "LWECompressed: base2k={} k={} seed={:?}: {}",
 51            self.base2k(),
 52            self.k(),
 53            self.seed,
 54            self.data
 55        )
 56    }
 57}
 58
 59impl<D: DataMut> FillUniform for LWECompressed<D> {
 60    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
 61        self.data.fill_uniform(log_bound, source);
 62    }
 63}
 64
 65pub trait LWECompressedAlloc {
 66    fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed<Vec<u8>> {
 67        LWECompressed {
 68            data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize),
 69            k,
 70            base2k,
 71            seed: [0u8; 32],
 72        }
 73    }
 74
 75    fn alloc_lwe_compressed_from_infos<A>(&self, infos: &A) -> LWECompressed<Vec<u8>>
 76    where
 77        A: LWEInfos,
 78    {
 79        self.alloc_lwe_compressed(infos.base2k(), infos.k())
 80    }
 81
 82    fn bytes_of_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize {
 83        Zn::bytes_of(1, 1, k.0.div_ceil(base2k.0) as usize)
 84    }
 85
 86    fn bytes_of_lwe_compressed_from_infos<A>(&self, infos: &A) -> usize
 87    where
 88        A: LWEInfos,
 89    {
 90        self.bytes_of_lwe_compressed(infos.base2k(), infos.k())
 91    }
 92}
 93
 94impl<B: Backend> LWECompressedAlloc for Module<B>{}
 95
 96impl LWECompressed<Vec<u8>> {
 97    pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
 98    where
 99        A: LWEInfos,
100        M: LWECompressedAlloc,
101    {
102        module.alloc_lwe_compressed_from_infos(infos)
103    }
104
105    pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self
106    where
107        M: LWECompressedAlloc,
108    {
109        module.alloc_lwe_compressed(base2k, k)
110    }
111
112    pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
113    where
114        A: LWEInfos,
115        M: LWECompressedAlloc,
116    {
117        module.bytes_of_lwe_compressed_from_infos(infos)
118    }
119
120    pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> usize
121    where
122        M: LWECompressedAlloc,
123    {
124        module.bytes_of_lwe_compressed(base2k, k)
125    }
126}
127
128use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
129
130impl<D: DataMut> ReaderFrom for LWECompressed<D> {
131    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
132        self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
133        self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
134        reader.read_exact(&mut self.seed)?;
135        self.data.read_from(reader)
136    }
137}
138
139impl<D: DataRef> WriterTo for LWECompressed<D> {
140    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
141        writer.write_u32::<LittleEndian>(self.k.into())?;
142        writer.write_u32::<LittleEndian>(self.base2k.into())?;
143        writer.write_all(&self.seed)?;
144        self.data.write_to(writer)
145    }
146}
147
148pub trait LWEDecompress
149where
150    Self: ZnFillUniform,
151{
152    fn decompress_lwe<R, O>(&self, res: &mut R, other: &O)
153    where
154        R: LWEToMut,
155        O: LWECompressedToRef,
156    {
157        let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
158        let other: &LWECompressed<&[u8]> = &other.to_ref();
159
160        assert_eq!(res.lwe_layout(), other.lwe_layout());
161
162        let mut source: Source = Source::new(other.seed);
163        self.zn_fill_uniform(
164            res.n().into(),
165            other.base2k().into(),
166            &mut res.data,
167            0,
168            &mut source,
169        );
170        for i in 0..res.size() {
171            res.data.at_mut(0, i)[0] = other.data.at(0, i)[0];
172        }
173    }
174}
175
176impl<B: Backend> LWEDecompress for Module<B> where Self: ZnFillUniform {}
177
178impl<D: DataMut> LWE<D> {
179    pub fn decompress<O, M>(&mut self, module: &M, other: &O)
180    where
181        O: LWECompressedToRef,
182        M: LWEDecompress,
183    {
184        module.decompress_lwe(self, other);
185    }
186}
187
188pub trait LWECompressedToRef {
189    fn to_ref(&self) -> LWECompressed<&[u8]>;
190}
191
192impl<D: DataRef> LWECompressedToRef for LWECompressed<D> {
193    fn to_ref(&self) -> LWECompressed<&[u8]> {
194        LWECompressed {
195            k: self.k,
196            base2k: self.base2k,
197            seed: self.seed,
198            data: self.data.to_ref(),
199        }
200    }
201}
202
203pub trait LWECompressedToMut {
204    fn to_mut(&mut self) -> LWECompressed<&mut [u8]>;
205}
206
207impl<D: DataMut> LWECompressedToMut for LWECompressed<D> {
208    fn to_mut(&mut self) -> LWECompressed<&mut [u8]> {
209        LWECompressed {
210            k: self.k,
211            base2k: self.base2k,
212            seed: self.seed,
213            data: self.data.to_mut(),
214        }
215    }
216}