poulpy/poulpy-core/src/encryption/compressed/gglwe_ksk.rs
poulpy-core/src/encryption/compressed/gglwe_ksk.rsRaw
1use poulpy_hal::{
2 api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPrepare, VecZnxSwitchRing},
3 layouts::{Backend, DataMut, Module, ScalarZnx, Scratch},
4 source::Source,
5};
6
7use crate::{
8 ScratchTakeCore,
9 encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
10 layouts::{
11 GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
12 compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut},
13 prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
14 },
15};
16
17impl GLWESwitchingKeyCompressed<Vec<u8>> {
18 pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
19 where
20 A: GGLWEInfos,
21 M: GLWESwitchingKeyCompressedEncryptSk<BE>,
22 {
23 module.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos)
24 }
25}
26
27impl<D: DataMut> GLWESwitchingKeyCompressed<D> {
28 #[allow(clippy::too_many_arguments)]
29 pub fn encrypt_sk<M, S1, S2, BE: Backend>(
30 &mut self,
31 module: &M,
32 sk_in: &S1,
33 sk_out: &S2,
34 seed_xa: [u8; 32],
35 source_xe: &mut Source,
36 scratch: &mut Scratch<BE>,
37 ) where
38 S1: GLWESecretToRef,
39 S2: GLWESecretToRef,
40 M: GLWESwitchingKeyCompressedEncryptSk<BE>,
41 {
42 module.glwe_switching_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch);
43 }
44}
45
46pub trait GLWESwitchingKeyCompressedEncryptSk<BE: Backend> {
47 fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
48 where
49 A: GGLWEInfos;
50
51 fn glwe_switching_key_compressed_encrypt_sk<R, S1, S2>(
52 &self,
53 res: &mut R,
54 sk_in: &S1,
55 sk_out: &S2,
56 seed_xa: [u8; 32],
57 source_xe: &mut Source,
58 scratch: &mut Scratch<BE>,
59 ) where
60 R: GLWESwitchingKeyCompressedToMut,
61 S1: GLWESecretToRef,
62 S2: GLWESecretToRef;
63}
64
65impl<BE: Backend> GLWESwitchingKeyCompressedEncryptSk<BE> for Module<BE>
66where
67 Self: ModuleN + GGLWECompressedEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + VecZnxSwitchRing,
68 Scratch<BE>: ScratchTakeCore<BE>,
69{
70 fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
71 where
72 A: GGLWEInfos,
73 {
74 self.gglwe_compressed_encrypt_sk_tmp_bytes(infos)
75 .max(ScalarZnx::bytes_of(self.n(), 1))
76 + ScalarZnx::bytes_of(self.n(), infos.rank_in().into())
77 + GLWESecretPrepared::bytes_of(self, infos.rank_out())
78 }
79
80 fn glwe_switching_key_compressed_encrypt_sk<R, S1, S2>(
81 &self,
82 res: &mut R,
83 sk_in: &S1,
84 sk_out: &S2,
85 seed_xa: [u8; 32],
86 source_xe: &mut Source,
87 scratch: &mut Scratch<BE>,
88 ) where
89 R: GLWESwitchingKeyCompressedToMut,
90 S1: GLWESecretToRef,
91 S2: GLWESecretToRef,
92 {
93 let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
94 let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
95 let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
96
97 assert!(sk_in.n().0 <= self.n() as u32);
98 assert!(sk_out.n().0 <= self.n() as u32);
99 assert!(
100 scratch.available() >= self.gglwe_compressed_encrypt_sk_tmp_bytes(res),
101 "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
102 scratch.available(),
103 self.gglwe_compressed_encrypt_sk_tmp_bytes(res)
104 );
105
106 let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into());
107 for i in 0..sk_in.rank().into() {
108 self.vec_znx_switch_ring(
109 &mut sk_in_tmp.as_vec_znx_mut(),
110 i,
111 &sk_in.data.as_vec_znx(),
112 i,
113 );
114 }
115
116 let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank());
117 {
118 let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1);
119 for i in 0..sk_out.rank().into() {
120 self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
121 self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
122 }
123 }
124
125 self.gglwe_compressed_encrypt_sk(
126 &mut res.key,
127 &sk_in_tmp,
128 &sk_out_tmp,
129 seed_xa,
130 source_xe,
131 scratch_2,
132 );
133 res.sk_in_n = sk_in.n().into();
134 res.sk_out_n = sk_out.n().into();
135 }
136}