poulpy/poulpy-core/src/encryption/compressed/gglwe_tsk.rs
poulpy-core/src/encryption/compressed/gglwe_tsk.rsRaw
1use poulpy_hal::{
2 api::{
3 SvpApplyDftToDft, SvpPPolAllocBytes, SvpPrepare, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAllocBytes, VecZnxBigNormalize,
4 VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
5 },
6 layouts::{Backend, DataMut, DataRef, Module, Scratch},
7 source::Source,
8};
9
10use crate::{
11 TakeGLWESecret, TakeGLWESecretPrepared,
12 encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
13 layouts::{
14 GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey,
15 compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
16 },
17};
18
19impl TensorKeyCompressed<Vec<u8>> {
20 pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
21 where
22 A: GGLWEInfos,
23 Module<B>:
24 SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes,
25 {
26 TensorKey::encrypt_sk_scratch_space(module, infos)
27 }
28}
29
30pub trait GGLWETensorKeyCompressedEncryptSk<B: Backend> {
31 fn gglwe_tensor_key_encrypt_sk<R, S>(
32 &self,
33 res: &mut R,
34 sk: &S,
35 seed_xa: [u8; 32],
36 source_xe: &mut Source,
37 scratch: &mut Scratch<B>,
38 ) where
39 R: TensorKeyCompressedToMut,
40 S: GLWESecretToRef;
41}
42
43impl<B: Backend> GGLWETensorKeyCompressedEncryptSk<B> for Module<B>
44where
45 Module<B>: GGLWEKeyCompressedEncryptSk<B>
46 + VecZnxDftApply<B>
47 + SvpApplyDftToDft<B>
48 + VecZnxIdftApplyTmpA<B>
49 + VecZnxBigNormalize<B>
50 + SvpPrepare<B>,
51 Scratch<B>: TakeGLWESecretPrepared<B> + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecret,
52{
53 fn gglwe_tensor_key_encrypt_sk<R, S>(
54 &self,
55 res: &mut R,
56 sk: &S,
57 seed_xa: [u8; 32],
58 source_xe: &mut Source,
59 scratch: &mut Scratch<B>,
60 ) where
61 R: TensorKeyCompressedToMut,
62 S: GLWESecretToRef,
63 {
64 let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut();
65 let sk: &GLWESecret<&[u8]> = &sk.to_ref();
66
67 #[cfg(debug_assertions)]
68 {
69 assert_eq!(res.rank_out(), sk.rank());
70 assert_eq!(res.n(), sk.n());
71 }
72
73 let n: usize = sk.n().into();
74 let rank: usize = res.rank_out().into();
75
76 let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), res.rank_out());
77 sk_dft_prep.prepare(self, sk, scratch_1);
78
79 let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n, rank, 1);
80
81 for i in 0..rank {
82 self.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
83 }
84
85 let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n, 1, 1);
86 let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(sk.n(), Rank(1));
87 let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(n, 1, 1);
88
89 let mut source_xa: Source = Source::new(seed_xa);
90
91 for i in 0..rank {
92 for j in i..rank {
93 self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i);
94
95 self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
96 self.vec_znx_big_normalize(
97 res.base2k().into(),
98 &mut sk_ij.data.as_vec_znx_mut(),
99 0,
100 res.base2k().into(),
101 &sk_ij_big,
102 0,
103 scratch_5,
104 );
105
106 let (seed_xa_tmp, _) = source_xa.branch();
107
108 self.gglwe_key_compressed_encrypt_sk(
109 res.at_mut(i, j),
110 &sk_ij,
111 sk,
112 seed_xa_tmp,
113 source_xe,
114 scratch_5,
115 );
116 }
117 }
118 }
119}
120
121impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
122 pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
123 &mut self,
124 module: &Module<B>,
125 sk: &GLWESecret<DataSk>,
126 seed_xa: [u8; 32],
127 source_xe: &mut Source,
128 scratch: &mut Scratch<B>,
129 ) where
130 Module<B>: GGLWETensorKeyCompressedEncryptSk<B>,
131 {
132 module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch);
133 }
134}