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