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