arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs
poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes,
  4        SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
  5        VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace,
  6        VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftSubABInplace, VecZnxDftZero, VecZnxFillUniform, VecZnxIdftApply,
  7        VecZnxIdftApplyConsume, VecZnxIdftApplyTmpBytes, VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace,
  8        VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxSub, VecZnxSubABInplace,
  9        VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal,
 10        ZnFillUniform, ZnNormalizeInplace,
 11    },
 12    layouts::{Backend, Module, ScratchOwned, ZnxView},
 13    oep::{
 14        ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl,
 15        TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
 16    },
 17    source::Source,
 18};
 19
 20use crate::tfhe::blind_rotation::{
 21    BlincRotationExecute, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyPrepared, CGGI,
 22    LookUpTable, cggi_blind_rotate_scratch_space, mod_switch_2n,
 23};
 24
 25use poulpy_core::layouts::{
 26    GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWECiphertextToRef, LWEPlaintext, LWESecret,
 27    prepared::{GLWESecretPrepared, PrepareAlloc},
 28};
 29
 30pub fn test_blind_rotation<B>(module: &Module<B>, n_lwe: usize, block_size: usize, extension_factor: usize)
 31where
 32    Module<B>: VecZnxBigAllocBytes
 33        + VecZnxDftAllocBytes
 34        + SvpPPolAllocBytes
 35        + VmpApplyDftToDftTmpBytes
 36        + VecZnxBigNormalizeTmpBytes
 37        + VecZnxIdftApplyTmpBytes
 38        + VecZnxIdftApply<B>
 39        + VecZnxDftAdd<B>
 40        + VecZnxDftAddInplace<B>
 41        + VecZnxDftApply<B>
 42        + VecZnxDftZero<B>
 43        + SvpApplyDftToDft<B>
 44        + VecZnxDftSubABInplace<B>
 45        + VecZnxBigAddSmallInplace<B>
 46        + VecZnxRotate
 47        + VecZnxAddInplace
 48        + VecZnxSubABInplace
 49        + VecZnxNormalize<B>
 50        + VecZnxNormalizeInplace<B>
 51        + VecZnxCopy
 52        + VecZnxMulXpMinusOneInplace<B>
 53        + SvpPrepare<B>
 54        + SvpPPolAlloc<B>
 55        + SvpApplyDftToDftInplace<B>
 56        + VecZnxIdftApplyConsume<B>
 57        + VecZnxBigAddInplace<B>
 58        + VecZnxBigNormalize<B>
 59        + VecZnxNormalizeTmpBytes
 60        + VecZnxFillUniform
 61        + VecZnxAddNormal
 62        + VecZnxAddScalarInplace
 63        + VecZnxRotateInplace<B>
 64        + VecZnxSwitchRing
 65        + VecZnxSub
 66        + VmpPMatAlloc<B>
 67        + VmpPrepare<B>
 68        + VmpApplyDftToDft<B>
 69        + VmpApplyDftToDftAdd<B>
 70        + ZnFillUniform
 71        + ZnAddNormal
 72        + VecZnxRotateInplaceTmpBytes
 73        + ZnNormalizeInplace<B>,
 74    B: Backend
 75        + VecZnxDftAllocBytesImpl<B>
 76        + VecZnxBigAllocBytesImpl<B>
 77        + ScratchOwnedAllocImpl<B>
 78        + ScratchOwnedBorrowImpl<B>
 79        + TakeVecZnxDftImpl<B>
 80        + TakeVecZnxBigImpl<B>
 81        + TakeVecZnxDftSliceImpl<B>
 82        + ScratchAvailableImpl<B>
 83        + TakeVecZnxImpl<B>
 84        + TakeVecZnxSliceImpl<B>,
 85{
 86    let n: usize = module.n();
 87    let basek: usize = 19;
 88    let k_lwe: usize = 24;
 89    let k_brk: usize = 3 * basek;
 90    let rows_brk: usize = 2; // Ensures first limb is noise-free.
 91    let k_lut: usize = basek;
 92    let k_res: usize = 2 * basek;
 93    let rank: usize = 1;
 94
 95    let log_message_modulus = 4;
 96
 97    let message_modulus: usize = 1 << log_message_modulus;
 98
 99    let mut source_xs: Source = Source::new([2u8; 32]);
100    let mut source_xe: Source = Source::new([2u8; 32]);
101    let mut source_xa: Source = Source::new([1u8; 32]);
102
103    let mut scratch: ScratchOwned<B> = ScratchOwned::<B>::alloc(BlindRotationKey::generate_from_sk_scratch_space(
104        module, basek, k_brk, rank,
105    ));
106
107    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
108    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
109    let sk_glwe_dft: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
110
111    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
112    sk_lwe.fill_binary_block(block_size, &mut source_xs);
113
114    let mut scratch_br: ScratchOwned<B> = ScratchOwned::<B>::alloc(cggi_blind_rotate_scratch_space(
115        module,
116        block_size,
117        extension_factor,
118        basek,
119        k_res,
120        k_brk,
121        rows_brk,
122        rank,
123    ));
124
125    let mut brk: BlindRotationKey<Vec<u8>, CGGI> =
126        BlindRotationKey::<Vec<u8>, CGGI>::alloc(n, n_lwe, basek, k_brk, rows_brk, rank);
127
128    brk.encrypt_sk(
129        module,
130        &sk_glwe_dft,
131        &sk_lwe,
132        &mut source_xa,
133        &mut source_xe,
134        scratch.borrow(),
135    );
136
137    let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
138
139    let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
140
141    let x: i64 = 15 % (message_modulus as i64);
142
143    pt_lwe.encode_i64(x, log_message_modulus + 1);
144
145    lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
146
147    let f = |x: i64| -> i64 { 2 * x + 1 };
148
149    let mut f_vec: Vec<i64> = vec![0i64; message_modulus];
150    f_vec
151        .iter_mut()
152        .enumerate()
153        .for_each(|(i, x)| *x = f(i as i64));
154
155    let mut lut: LookUpTable = LookUpTable::alloc(module, basek, k_lut, extension_factor);
156    lut.set(module, &f_vec, log_message_modulus + 1);
157
158    let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_res, rank);
159
160    let brk_prepared: BlindRotationKeyPrepared<Vec<u8>, CGGI, B> = brk.prepare_alloc(module, scratch.borrow());
161
162    brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow());
163
164    let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_res);
165
166    res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
167
168    let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
169
170    mod_switch_2n(
171        2 * lut.domain_size(),
172        &mut lwe_2n,
173        &lwe.to_ref(),
174        lut.rotation_direction(),
175    );
176
177    let pt_want: i64 = (lwe_2n[0]
178        + lwe_2n[1..]
179            .iter()
180            .zip(sk_lwe.raw())
181            .map(|(x, y)| x * y)
182            .sum::<i64>())
183        & (2 * lut.domain_size() - 1) as i64;
184
185    lut.rotate(module, pt_want);
186
187    // First limb should be exactly equal (test are parameterized such that the noise does not reach
188    // the first limb)
189    assert_eq!(pt_have.data.at(0, 0), lut.data[0].at(0, 0));
190
191    // Verify that it effectively compute f(x)
192    let mut have: i64 = pt_have.decode_coeff_i64(log_message_modulus + 1, 0);
193
194    // Get positive representative and assert equality
195    have = (have + message_modulus as i64) % (message_modulus as i64);
196
197    assert_eq!(have, f(x) % (message_modulus as i64));
198}