arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/ggsw/layout_compressed.rs
core/src/ggsw/layout_compressed.rsRaw
 1use backend::hal::{
 2    api::{MatZnxAlloc, MatZnxAllocBytes, VecZnxCopy, VecZnxFillUniform},
 3    layouts::{Backend, Data, DataMut, DataRef, MatZnx, Module, ReaderFrom, WriterTo},
 4};
 5
 6use crate::{GGLWECiphertextCompressed, GGSWCiphertext, Infos};
 7
 8#[derive(PartialEq, Eq)]
 9pub struct GGSWCiphertextCompressed<D: Data> {
10    pub(crate) data: GGLWECiphertextCompressed<D>,
11}
12
13impl GGSWCiphertextCompressed<Vec<u8>> {
14    pub fn alloc<B: Backend>(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
15    where
16        Module<B>: MatZnxAlloc,
17    {
18        GGSWCiphertextCompressed {
19            data: GGLWECiphertextCompressed::alloc(module, basek, k, rows, digits, rank, rank),
20        }
21    }
22
23    pub fn bytes_of<B: Backend>(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize
24    where
25        Module<B>: MatZnxAllocBytes,
26    {
27        GGLWECiphertextCompressed::bytes_of(module, basek, k, rows, digits, rank)
28    }
29}
30
31impl<D: Data> Infos for GGSWCiphertextCompressed<D> {
32    type Inner = MatZnx<D>;
33
34    fn inner(&self) -> &Self::Inner {
35        self.data.inner()
36    }
37
38    fn basek(&self) -> usize {
39        self.data.basek()
40    }
41
42    fn k(&self) -> usize {
43        self.data.k()
44    }
45}
46
47impl<D: Data> GGSWCiphertextCompressed<D> {
48    pub fn rank(&self) -> usize {
49        self.data.rank()
50    }
51
52    pub fn digits(&self) -> usize {
53        self.data.digits()
54    }
55}
56
57impl<D: DataMut> ReaderFrom for GGSWCiphertextCompressed<D> {
58    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
59        self.data.read_from(reader)
60    }
61}
62
63impl<D: DataRef> WriterTo for GGSWCiphertextCompressed<D> {
64    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
65        self.data.write_to(writer)
66    }
67}
68
69impl<D: DataMut> GGSWCiphertext<D> {
70    pub fn decompress<DataOther: DataRef, B: Backend>(&mut self, module: &Module<B>, other: &GGSWCiphertextCompressed<DataOther>)
71    where
72        Module<B>: VecZnxFillUniform + VecZnxCopy,
73    {
74        let rows = self.rows();
75        (0..rows).for_each(|row_i| {
76            self.at_mut(row_i, 0)
77                .decompress(module, &other.data.at(row_i, 0));
78        });
79    }
80}