arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs
poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rsRaw
  1use poulpy_hal::{
  2    api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform},
  3    layouts::{Backend, Module, Scratch, ScratchOwned},
  4    source::Source,
  5};
  6
  7use crate::{
  8    GLWEEncryptSk, GLWEKeyswitch, GLWENoise, GLWENormalize, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
  9    encryption::SIGMA,
 10    layouts::{
 11        GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout,
 12        GLWESwitchingKeyPreparedFactory, LWEInfos,
 13        prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared},
 14    },
 15    noise::log2_std_noise_gglwe_product,
 16    var_noise_gglwe_product_v2,
 17};
 18
 19#[allow(clippy::too_many_arguments)]
 20pub fn test_glwe_keyswitch<BE: Backend>(module: &Module<BE>)
 21where
 22    Module<BE>: VecZnxFillUniform
 23        + GLWESwitchingKeyEncryptSk<BE>
 24        + GLWEEncryptSk<BE>
 25        + GLWEKeyswitch<BE>
 26        + GLWESecretPreparedFactory<BE>
 27        + GLWESwitchingKeyPreparedFactory<BE>
 28        + GLWENoise<BE>
 29        + GLWENormalize<BE>,
 30    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 31    Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
 32{
 33    let base2k_in: usize = 17;
 34    let base2k_key: usize = 13;
 35    let base2k_out: usize = 15;
 36    let k_in: usize = 102;
 37    let max_dsize: usize = k_in.div_ceil(base2k_key);
 38
 39    for rank_in in 1_usize..3 {
 40        for rank_out in 1_usize..3 {
 41            for dsize in 1_usize..max_dsize + 1 {
 42                let k_ksk: usize = k_in + base2k_key * dsize;
 43                let k_out: usize = k_ksk; // better capture noise
 44
 45                let n: usize = module.n();
 46                let dnum: usize = k_in.div_ceil(base2k_key * dsize);
 47
 48                let glwe_in_infos: GLWELayout = GLWELayout {
 49                    n: n.into(),
 50                    base2k: base2k_in.into(),
 51                    k: k_in.into(),
 52                    rank: rank_in.into(),
 53                };
 54
 55                let glwe_out_infos: GLWELayout = GLWELayout {
 56                    n: n.into(),
 57                    base2k: base2k_out.into(),
 58                    k: k_out.into(),
 59                    rank: rank_out.into(),
 60                };
 61
 62                let ksk: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
 63                    n: n.into(),
 64                    base2k: base2k_key.into(),
 65                    k: k_ksk.into(),
 66                    dnum: dnum.into(),
 67                    dsize: dsize.into(),
 68                    rank_in: rank_in.into(),
 69                    rank_out: rank_out.into(),
 70                };
 71
 72                let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk);
 73                let mut glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_infos);
 74                let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
 75                let mut pt_in: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos);
 76                let mut pt_out: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
 77
 78                let mut source_xs: Source = Source::new([0u8; 32]);
 79                let mut source_xe: Source = Source::new([0u8; 32]);
 80                let mut source_xa: Source = Source::new([0u8; 32]);
 81
 82                module.vec_znx_fill_uniform(pt_in.base2k().into(), &mut pt_in.data, 0, &mut source_xa);
 83
 84                let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
 85                    GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk)
 86                        | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos)
 87                        | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ksk),
 88                );
 89
 90                let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into());
 91                sk_in.fill_ternary_prob(0.5, &mut source_xs);
 92
 93                let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_in.into());
 94                sk_in_prepared.prepare(module, &sk_in);
 95
 96                let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into());
 97                sk_out.fill_ternary_prob(0.5, &mut source_xs);
 98
 99                let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into());
100                sk_out_prepared.prepare(module, &sk_out);
101
102                ksk.encrypt_sk(
103                    module,
104                    &sk_in,
105                    &sk_out,
106                    &mut source_xa,
107                    &mut source_xe,
108                    scratch.borrow(),
109                );
110
111                glwe_in.encrypt_sk(
112                    module,
113                    &pt_in,
114                    &sk_in_prepared,
115                    &mut source_xa,
116                    &mut source_xe,
117                    scratch.borrow(),
118                );
119
120                let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
121                    GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
122                ksk_prepared.prepare(module, &ksk, scratch.borrow());
123
124                glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow());
125
126                let max_noise: f64 = var_noise_gglwe_product_v2(
127                    module.n() as f64,
128                    k_ksk,
129                    dnum,
130                    dsize,
131                    base2k_key,
132                    0.5,
133                    0.5,
134                    0f64,
135                    SIGMA * SIGMA,
136                    0f64,
137                    rank_in as f64,
138                )
139                .sqrt()
140                .log2();
141
142                module.glwe_normalize(&mut pt_out, &pt_in, scratch.borrow());
143
144                glwe_out.assert_noise(module, &sk_out_prepared, &pt_out, max_noise + 0.5);
145            }
146        }
147    }
148}
149
150pub fn test_glwe_keyswitch_inplace<BE: Backend>(module: &Module<BE>)
151where
152    Module<BE>: VecZnxFillUniform
153        + GLWESwitchingKeyEncryptSk<BE>
154        + GLWEEncryptSk<BE>
155        + GLWEKeyswitch<BE>
156        + GLWESecretPreparedFactory<BE>
157        + GLWESwitchingKeyPreparedFactory<BE>
158        + GLWENoise<BE>,
159    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
160    Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
161{
162    let base2k_out: usize = 17;
163    let base2k_key: usize = 13;
164
165    let k_out: usize = 102;
166    let max_dsize: usize = k_out.div_ceil(base2k_key);
167
168    for rank in 1_usize..3 {
169        for dsize in 1..max_dsize + 1 {
170            let k_ksk: usize = k_out + base2k_key * dsize;
171
172            let n: usize = module.n();
173            let dnum: usize = k_out.div_ceil(base2k_key * dsize);
174            let glwe_out_infos: GLWELayout = GLWELayout {
175                n: n.into(),
176                base2k: base2k_out.into(),
177                k: k_out.into(),
178                rank: rank.into(),
179            };
180
181            let ksk_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
182                n: n.into(),
183                base2k: base2k_key.into(),
184                k: k_ksk.into(),
185                dnum: dnum.into(),
186                dsize: dsize.into(),
187                rank_in: rank.into(),
188                rank_out: rank.into(),
189            };
190
191            let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_infos);
192            let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
193            let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
194
195            let mut source_xs: Source = Source::new([0u8; 32]);
196            let mut source_xe: Source = Source::new([0u8; 32]);
197            let mut source_xa: Source = Source::new([0u8; 32]);
198
199            module.vec_znx_fill_uniform(
200                pt_want.base2k().into(),
201                &mut pt_want.data,
202                0,
203                &mut source_xa,
204            );
205
206            let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
207                GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_infos)
208                    | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
209                    | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ksk_infos),
210            );
211
212            let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
213            sk_in.fill_ternary_prob(0.5, &mut source_xs);
214
215            let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
216            sk_in_prepared.prepare(module, &sk_in);
217
218            let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
219            sk_out.fill_ternary_prob(0.5, &mut source_xs);
220
221            let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
222            sk_out_prepared.prepare(module, &sk_out);
223
224            ksk.encrypt_sk(
225                module,
226                &sk_in,
227                &sk_out,
228                &mut source_xa,
229                &mut source_xe,
230                scratch.borrow(),
231            );
232
233            glwe_out.encrypt_sk(
234                module,
235                &pt_want,
236                &sk_in_prepared,
237                &mut source_xa,
238                &mut source_xe,
239                scratch.borrow(),
240            );
241
242            let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
243                GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
244            ksk_prepared.prepare(module, &ksk, scratch.borrow());
245
246            glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow());
247
248            let max_noise: f64 = var_noise_gglwe_product_v2(
249                module.n() as f64,
250                k_ksk,
251                dnum,
252                dsize,
253                base2k_key,
254                0.5,
255                0.5,
256                0f64,
257                SIGMA * SIGMA,
258                0f64,
259                rank as f64,
260            )
261            .sqrt()
262            .log2();
263
264            glwe_out.assert_noise(module, &sk_out_prepared, &pt_want, max_noise + 0.5);
265        }
266    }
267}