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