poulpy/poulpy-core/src/encryption/compressed/gglwe_ksk.rs
poulpy-core/src/encryption/compressed/gglwe_ksk.rsRaw
1use poulpy_hal::{
2 api::{
3 ScratchAvailable, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing,
4 },
5 layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
6 source::Source,
7};
8
9use crate::{
10 TakeGLWESecretPrepared,
11 encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
12 layouts::{
13 Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
14 compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut},
15 prepared::GLWESecretPrepared,
16 },
17};
18
19impl GLWESwitchingKeyCompressed<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>: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf,
24 {
25 (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
26 + ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
27 + GLWESecretPrepared::bytes_of(module, infos.rank_out())
28 }
29}
30
31impl<DataSelf: DataMut> GLWESwitchingKeyCompressed<DataSelf> {
32 #[allow(clippy::too_many_arguments)]
33 pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
34 &mut self,
35 module: &Module<B>,
36 sk_in: &GLWESecret<DataSkIn>,
37 sk_out: &GLWESecret<DataSkOut>,
38 seed_xa: [u8; 32],
39 source_xe: &mut Source,
40 scratch: &mut Scratch<B>,
41 ) where
42 Module<B>: GGLWEKeyCompressedEncryptSk<B>,
43 {
44 module.gglwe_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch);
45 }
46}
47
48pub trait GGLWEKeyCompressedEncryptSk<B: Backend> {
49 fn gglwe_key_compressed_encrypt_sk<R, SI, SO>(
50 &self,
51 res: &mut R,
52 sk_in: &SI,
53 sk_out: &SO,
54 seed_xa: [u8; 32],
55 source_xe: &mut Source,
56 scratch: &mut Scratch<B>,
57 ) where
58 R: GLWESwitchingKeyCompressedToMut,
59 SI: GLWESecretToRef,
60 SO: GLWESecretToRef;
61}
62
63impl<B: Backend> GGLWEKeyCompressedEncryptSk<B> for Module<B>
64where
65 Module<B>: GGLWECompressedEncryptSk<B>
66 + SvpPPolBytesOf
67 + VecZnxNormalizeTmpBytes
68 + VecZnxDftBytesOf
69 + VecZnxSwitchRing
70 + SvpPrepare<B>,
71 Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeGLWESecretPrepared<B>,
72{
73 fn gglwe_key_compressed_encrypt_sk<R, SI, SO>(
74 &self,
75 res: &mut R,
76 sk_in: &SI,
77 sk_out: &SO,
78 seed_xa: [u8; 32],
79 source_xe: &mut Source,
80 scratch: &mut Scratch<B>,
81 ) where
82 R: GLWESwitchingKeyCompressedToMut,
83 SI: GLWESecretToRef,
84 SO: GLWESecretToRef,
85 {
86 let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
87 let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
88 let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
89
90 #[cfg(debug_assertions)]
91 {
92 use crate::layouts::GLWESwitchingKey;
93
94 assert!(sk_in.n().0 <= self.n() as u32);
95 assert!(sk_out.n().0 <= self.n() as u32);
96 assert!(
97 scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(self, res),
98 "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
99 scratch.available(),
100 GLWESwitchingKey::encrypt_sk_scratch_space(self, res)
101 )
102 }
103
104 let n: usize = sk_in.n().max(sk_out.n()).into();
105
106 let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into());
107 (0..sk_in.rank().into()).for_each(|i| {
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(Degree(n as u32), sk_out.rank());
117 {
118 let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1);
119 (0..sk_out.rank().into()).for_each(|i| {
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}