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