poulpy/poulpy-core/src/layouts/compressed/gglwe_ksk.rs
poulpy-core/src/layouts/compressed/gglwe_ksk.rsRaw
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, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, 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> GGLWEInfos 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 dsize(&self) -> Dsize {
54 self.key.dsize()
55 }
56
57 fn dnum(&self) -> Dnum {
58 self.key.dnum()
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: GGLWEInfos,
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 rank_in: Rank,
101 rank_out: Rank,
102 dnum: Dnum,
103 dsize: Dsize,
104 ) -> Self {
105 GGLWESwitchingKeyCompressed {
106 key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize),
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: GGLWEInfos,
115 {
116 GGLWECiphertextCompressed::alloc_bytes(infos)
117 }
118
119 pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize {
120 GGLWECiphertextCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, dsize)
121 }
122}
123
124impl<D: DataMut> ReaderFrom for GGLWESwitchingKeyCompressed<D> {
125 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
126 self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize;
127 self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize;
128 self.key.read_from(reader)
129 }
130}
131
132impl<D: DataRef> WriterTo for GGLWESwitchingKeyCompressed<D> {
133 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
134 writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?;
135 writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?;
136 self.key.write_to(writer)
137 }
138}
139
140impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWESwitchingKeyCompressed<DR>> for GGLWESwitchingKey<D>
141where
142 Module<B>: VecZnxFillUniform + VecZnxCopy,
143{
144 fn decompress(&mut self, module: &Module<B>, other: &GGLWESwitchingKeyCompressed<DR>) {
145 self.key.decompress(module, &other.key);
146 self.sk_in_n = other.sk_in_n;
147 self.sk_out_n = other.sk_out_n;
148 }
149}