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