poulpy_core/layouts/compressed/
gglwe_ksk.rs

1use poulpy_hal::{
2    api::{VecZnxCopy, VecZnxFillUniform},
3    layouts::{Backend, Data, DataMut, DataRef, FillUniform, MatZnx, Module, ReaderFrom, Reset, WriterTo},
4    source::Source,
5};
6
7use crate::layouts::{
8    GGLWESwitchingKey, Infos,
9    compressed::{Decompress, GGLWECiphertextCompressed},
10};
11use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
12use std::fmt;
13
14#[derive(PartialEq, Eq, Clone)]
15pub struct GGLWESwitchingKeyCompressed<D: Data> {
16    pub(crate) key: GGLWECiphertextCompressed<D>,
17    pub(crate) sk_in_n: usize,  // Degree of sk_in
18    pub(crate) sk_out_n: usize, // Degree of sk_out
19}
20
21impl<D: DataRef> fmt::Debug for GGLWESwitchingKeyCompressed<D> {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        write!(f, "{}", self)
24    }
25}
26
27impl<D: DataMut> FillUniform for GGLWESwitchingKeyCompressed<D> {
28    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
29        self.key.fill_uniform(log_bound, source);
30    }
31}
32
33impl<D: DataMut> Reset for GGLWESwitchingKeyCompressed<D>
34where
35    MatZnx<D>: Reset,
36{
37    fn reset(&mut self) {
38        self.key.reset();
39        self.sk_in_n = 0;
40        self.sk_out_n = 0;
41    }
42}
43
44impl<D: DataRef> fmt::Display for GGLWESwitchingKeyCompressed<D> {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(
47            f,
48            "(GLWESwitchingKeyCompressed: sk_in_n={} sk_out_n={}) {}",
49            self.sk_in_n, self.sk_out_n, self.key.data
50        )
51    }
52}
53
54impl<D: Data> Infos for GGLWESwitchingKeyCompressed<D> {
55    type Inner = MatZnx<D>;
56
57    fn inner(&self) -> &Self::Inner {
58        self.key.inner()
59    }
60
61    fn basek(&self) -> usize {
62        self.key.basek()
63    }
64
65    fn k(&self) -> usize {
66        self.key.k()
67    }
68}
69
70impl<D: Data> GGLWESwitchingKeyCompressed<D> {
71    pub fn rank(&self) -> usize {
72        self.key.rank()
73    }
74
75    pub fn digits(&self) -> usize {
76        self.key.digits()
77    }
78
79    pub fn rank_in(&self) -> usize {
80        self.key.rank_in()
81    }
82
83    pub fn rank_out(&self) -> usize {
84        self.key.rank_out()
85    }
86}
87
88impl GGLWESwitchingKeyCompressed<Vec<u8>> {
89    pub fn alloc(n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize) -> Self {
90        GGLWESwitchingKeyCompressed {
91            key: GGLWECiphertextCompressed::alloc(n, basek, k, rows, digits, rank_in, rank_out),
92            sk_in_n: 0,
93            sk_out_n: 0,
94        }
95    }
96
97    pub fn bytes_of(n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize) -> usize {
98        GGLWECiphertextCompressed::bytes_of(n, basek, k, rows, digits, rank_in)
99    }
100}
101
102impl<D: DataMut> ReaderFrom for GGLWESwitchingKeyCompressed<D> {
103    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
104        self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize;
105        self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize;
106        self.key.read_from(reader)
107    }
108}
109
110impl<D: DataRef> WriterTo for GGLWESwitchingKeyCompressed<D> {
111    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
112        writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?;
113        writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?;
114        self.key.write_to(writer)
115    }
116}
117
118impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWESwitchingKeyCompressed<DR>> for GGLWESwitchingKey<D>
119where
120    Module<B>: VecZnxFillUniform + VecZnxCopy,
121{
122    fn decompress(&mut self, module: &Module<B>, other: &GGLWESwitchingKeyCompressed<DR>) {
123        self.key.decompress(module, &other.key);
124        self.sk_in_n = other.sk_in_n;
125        self.sk_out_n = other.sk_out_n;
126    }
127}