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