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