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