poulpy_core/layouts/compressed/
gglwe_atk.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, GGLWEAutomorphismKey, GGLWELayoutInfos, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision,
9    compressed::{Decompress, GGLWESwitchingKeyCompressed},
10};
11use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
12use std::fmt;
13
14#[derive(PartialEq, Eq, Clone)]
15pub struct GGLWEAutomorphismKeyCompressed<D: Data> {
16    pub(crate) key: GGLWESwitchingKeyCompressed<D>,
17    pub(crate) p: i64,
18}
19
20impl<D: Data> LWEInfos for GGLWEAutomorphismKeyCompressed<D> {
21    fn n(&self) -> Degree {
22        self.key.n()
23    }
24
25    fn base2k(&self) -> Base2K {
26        self.key.base2k()
27    }
28
29    fn k(&self) -> TorusPrecision {
30        self.key.k()
31    }
32
33    fn size(&self) -> usize {
34        self.key.size()
35    }
36}
37impl<D: Data> GLWEInfos for GGLWEAutomorphismKeyCompressed<D> {
38    fn rank(&self) -> Rank {
39        self.rank_out()
40    }
41}
42
43impl<D: Data> GGLWELayoutInfos for GGLWEAutomorphismKeyCompressed<D> {
44    fn rank_in(&self) -> Rank {
45        self.key.rank_in()
46    }
47
48    fn rank_out(&self) -> Rank {
49        self.key.rank_out()
50    }
51
52    fn digits(&self) -> Digits {
53        self.key.digits()
54    }
55
56    fn rows(&self) -> Rows {
57        self.key.rows()
58    }
59}
60
61impl<D: DataRef> fmt::Debug for GGLWEAutomorphismKeyCompressed<D> {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        write!(f, "{self}")
64    }
65}
66
67impl<D: DataMut> FillUniform for GGLWEAutomorphismKeyCompressed<D> {
68    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
69        self.key.fill_uniform(log_bound, source);
70    }
71}
72
73impl<D: DataRef> fmt::Display for GGLWEAutomorphismKeyCompressed<D> {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key)
76    }
77}
78
79impl GGLWEAutomorphismKeyCompressed<Vec<u8>> {
80    pub fn alloc<A>(infos: &A) -> Self
81    where
82        A: GGLWELayoutInfos,
83    {
84        debug_assert_eq!(infos.rank_in(), infos.rank_out());
85        Self {
86            key: GGLWESwitchingKeyCompressed::alloc(infos),
87            p: 0,
88        }
89    }
90
91    pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank) -> Self {
92        Self {
93            key: GGLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rows, digits, rank, rank),
94            p: 0,
95        }
96    }
97
98    pub fn alloc_bytes<A>(infos: &A) -> usize
99    where
100        A: GGLWELayoutInfos,
101    {
102        debug_assert_eq!(infos.rank_in(), infos.rank_out());
103        GGLWESwitchingKeyCompressed::alloc_bytes(infos)
104    }
105
106    pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank) -> usize {
107        GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rows, digits, rank, rank)
108    }
109}
110
111impl<D: DataMut> ReaderFrom for GGLWEAutomorphismKeyCompressed<D> {
112    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
113        self.p = reader.read_u64::<LittleEndian>()? as i64;
114        self.key.read_from(reader)
115    }
116}
117
118impl<D: DataRef> WriterTo for GGLWEAutomorphismKeyCompressed<D> {
119    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
120        writer.write_u64::<LittleEndian>(self.p as u64)?;
121        self.key.write_to(writer)
122    }
123}
124
125impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWEAutomorphismKeyCompressed<DR>> for GGLWEAutomorphismKey<D>
126where
127    Module<B>: VecZnxFillUniform + VecZnxCopy,
128{
129    fn decompress(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKeyCompressed<DR>) {
130        self.key.decompress(module, &other.key);
131        self.p = other.p;
132    }
133}