poulpy_core/layouts/compressed/
gglwe_ksk.rs

1use poulpy_hal::{
2    api::{VecZnxCopy, VecZnxFillUniform},
3    layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
4    source::Source,
5};
6
7use crate::layouts::{
8    Base2K, Degree, Digits, GGLWELayoutInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision,
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: Data> LWEInfos for GGLWESwitchingKeyCompressed<D> {
22    fn n(&self) -> Degree {
23        self.key.n()
24    }
25
26    fn base2k(&self) -> Base2K {
27        self.key.base2k()
28    }
29
30    fn k(&self) -> TorusPrecision {
31        self.key.k()
32    }
33
34    fn size(&self) -> usize {
35        self.key.size()
36    }
37}
38impl<D: Data> GLWEInfos for GGLWESwitchingKeyCompressed<D> {
39    fn rank(&self) -> Rank {
40        self.rank_out()
41    }
42}
43
44impl<D: Data> GGLWELayoutInfos for GGLWESwitchingKeyCompressed<D> {
45    fn rank_in(&self) -> Rank {
46        self.key.rank_in()
47    }
48
49    fn rank_out(&self) -> Rank {
50        self.key.rank_out()
51    }
52
53    fn digits(&self) -> Digits {
54        self.key.digits()
55    }
56
57    fn rows(&self) -> Rows {
58        self.key.rows()
59    }
60}
61
62impl<D: DataRef> fmt::Debug for GGLWESwitchingKeyCompressed<D> {
63    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64        write!(f, "{self}")
65    }
66}
67
68impl<D: DataMut> FillUniform for GGLWESwitchingKeyCompressed<D> {
69    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
70        self.key.fill_uniform(log_bound, source);
71    }
72}
73
74impl<D: DataRef> fmt::Display for GGLWESwitchingKeyCompressed<D> {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        write!(
77            f,
78            "(GLWESwitchingKeyCompressed: sk_in_n={} sk_out_n={}) {}",
79            self.sk_in_n, self.sk_out_n, self.key.data
80        )
81    }
82}
83
84impl GGLWESwitchingKeyCompressed<Vec<u8>> {
85    pub fn alloc<A>(infos: &A) -> Self
86    where
87        A: GGLWELayoutInfos,
88    {
89        GGLWESwitchingKeyCompressed {
90            key: GGLWECiphertextCompressed::alloc(infos),
91            sk_in_n: 0,
92            sk_out_n: 0,
93        }
94    }
95
96    pub fn alloc_with(
97        n: Degree,
98        base2k: Base2K,
99        k: TorusPrecision,
100        rows: Rows,
101        digits: Digits,
102        rank_in: Rank,
103        rank_out: Rank,
104    ) -> Self {
105        GGLWESwitchingKeyCompressed {
106            key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rows, digits, rank_in, rank_out),
107            sk_in_n: 0,
108            sk_out_n: 0,
109        }
110    }
111
112    pub fn alloc_bytes<A>(infos: &A) -> usize
113    where
114        A: GGLWELayoutInfos,
115    {
116        GGLWECiphertextCompressed::alloc_bytes(infos)
117    }
118
119    pub fn alloc_bytes_with(
120        n: Degree,
121        base2k: Base2K,
122        k: TorusPrecision,
123        rows: Rows,
124        digits: Digits,
125        rank_in: Rank,
126        rank_out: Rank,
127    ) -> usize {
128        GGLWECiphertextCompressed::alloc_bytes_with(n, base2k, k, rows, digits, rank_in, rank_out)
129    }
130}
131
132impl<D: DataMut> ReaderFrom for GGLWESwitchingKeyCompressed<D> {
133    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
134        self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize;
135        self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize;
136        self.key.read_from(reader)
137    }
138}
139
140impl<D: DataRef> WriterTo for GGLWESwitchingKeyCompressed<D> {
141    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
142        writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?;
143        writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?;
144        self.key.write_to(writer)
145    }
146}
147
148impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWESwitchingKeyCompressed<DR>> for GGLWESwitchingKey<D>
149where
150    Module<B>: VecZnxFillUniform + VecZnxCopy,
151{
152    fn decompress(&mut self, module: &Module<B>, other: &GGLWESwitchingKeyCompressed<DR>) {
153        self.key.decompress(module, &other.key);
154        self.sk_in_n = other.sk_in_n;
155        self.sk_out_n = other.sk_out_n;
156    }
157}