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::{ScratchOwnedAlloc, ScratchOwnedBorrow},
  3    layouts::{Backend, Scratch, ScratchOwned, ZnxView},
  4    source::Source,
  5};
  6
  7use crate::tfhe::blind_rotation::{
  8    BlindRotationAlgo, BlindRotationExecute, BlindRotationKey, BlindRotationKeyEncryptSk, BlindRotationKeyFactory,
  9    BlindRotationKeyLayout, BlindRotationKeyPrepared, BlindRotationKeyPreparedFactory, LookUpTableLayout, LookupTable,
 10    LookupTableFactory, mod_switch_2n,
 11};
 12
 13use poulpy_core::{
 14    GLWEDecrypt, LWEEncryptSk, ScratchTakeCore,
 15    layouts::{
 16        GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, LWE, LWEInfos, LWELayout, LWEPlaintext,
 17        LWESecret, LWEToRef, prepared::GLWESecretPrepared,
 18    },
 19};
 20
 21pub fn test_blind_rotation<BRA: BlindRotationAlgo, M, BE: Backend>(
 22    module: &M,
 23    n_lwe: usize,
 24    block_size: usize,
 25    extension_factor: usize,
 26) where
 27    M: BlindRotationKeyEncryptSk<BE, BRA>
 28        + BlindRotationKeyPreparedFactory<BE, BRA>
 29        + BlindRotationExecute<BRA, BE>
 30        + GLWESecretPreparedFactory<BE>
 31        + BlindRotationExecute<BRA, BE>
 32        + LWEEncryptSk<BE>
 33        + LookupTableFactory
 34        + GLWEDecrypt<BE>,
 35    BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
 36    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 37    Scratch<BE>: ScratchTakeCore<BE>,
 38{
 39    let n_glwe: usize = module.n();
 40    let base2k: usize = 19;
 41    let k_lwe: usize = 24;
 42    let k_brk: usize = 3 * base2k;
 43    let rows_brk: usize = 2; // Ensures first limb is noise-free.
 44    let k_lut: usize = base2k;
 45    let k_res: usize = 2 * base2k;
 46    let rank: usize = 1;
 47
 48    let log_message_modulus: usize = 4;
 49
 50    let message_modulus: usize = 1 << log_message_modulus;
 51
 52    let mut source_xs: Source = Source::new([2u8; 32]);
 53    let mut source_xe: Source = Source::new([2u8; 32]);
 54    let mut source_xa: Source = Source::new([1u8; 32]);
 55
 56    let brk_infos: BlindRotationKeyLayout = BlindRotationKeyLayout {
 57        n_glwe: n_glwe.into(),
 58        n_lwe: n_lwe.into(),
 59        base2k: base2k.into(),
 60        k: k_brk.into(),
 61        dnum: rows_brk.into(),
 62        rank: rank.into(),
 63    };
 64
 65    let glwe_infos: GLWELayout = GLWELayout {
 66        n: n_glwe.into(),
 67        base2k: base2k.into(),
 68        k: k_res.into(),
 69        rank: rank.into(),
 70    };
 71
 72    let lwe_infos: LWELayout = LWELayout {
 73        n: n_lwe.into(),
 74        k: k_lwe.into(),
 75        base2k: base2k.into(),
 76    };
 77
 78    let mut scratch: ScratchOwned<BE> = ScratchOwned::<BE>::alloc(BlindRotationKey::encrypt_sk_tmp_bytes(module, &brk_infos));
 79
 80    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
 81    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
 82    let mut sk_glwe_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &glwe_infos);
 83    sk_glwe_dft.prepare(module, &sk_glwe);
 84
 85    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
 86    sk_lwe.fill_binary_block(block_size, &mut source_xs);
 87
 88    let mut scratch_br: ScratchOwned<BE> = ScratchOwned::<BE>::alloc(BlindRotationKeyPrepared::execute_tmp_bytes(
 89        module,
 90        block_size,
 91        extension_factor,
 92        &glwe_infos,
 93        &brk_infos,
 94    ));
 95
 96    let mut brk: BlindRotationKey<Vec<u8>, BRA> = BlindRotationKey::<Vec<u8>, BRA>::alloc(&brk_infos);
 97
 98    brk.encrypt_sk(
 99        module,
100        &sk_glwe_dft,
101        &sk_lwe,
102        &mut source_xa,
103        &mut source_xe,
104        scratch.borrow(),
105    );
106
107    let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
108
109    let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
110
111    let x: i64 = 15 % (message_modulus as i64);
112
113    pt_lwe.encode_i64(x, (log_message_modulus + 1).into());
114
115    lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
116
117    let f = |x: i64| -> i64 { 2 * x + 1 };
118
119    let mut f_vec: Vec<i64> = vec![0i64; message_modulus];
120    f_vec
121        .iter_mut()
122        .enumerate()
123        .for_each(|(i, x)| *x = f(i as i64));
124
125    let lut_infos = LookUpTableLayout {
126        n: module.n().into(),
127        extension_factor,
128        k: k_lut.into(),
129        base2k: base2k.into(),
130    };
131
132    let mut lut: LookupTable = LookupTable::alloc(&lut_infos);
133    lut.set(module, &f_vec, log_message_modulus + 1);
134
135    let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
136
137    let mut brk_prepared: BlindRotationKeyPrepared<Vec<u8>, BRA, BE> = BlindRotationKeyPrepared::alloc(module, &brk);
138    brk_prepared.prepare(module, &brk, scratch_br.borrow());
139
140    brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow());
141
142    let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
143
144    res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
145
146    let mut lwe_2n: Vec<i64> = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space
147
148    mod_switch_2n(
149        2 * lut.domain_size(),
150        &mut lwe_2n,
151        &lwe.to_ref(),
152        lut.rotation_direction(),
153    );
154
155    let pt_want: i64 = (lwe_2n[0]
156        + lwe_2n[1..]
157            .iter()
158            .zip(sk_lwe.raw())
159            .map(|(x, y)| x * y)
160            .sum::<i64>())
161        & (2 * lut.domain_size() - 1) as i64;
162
163    lut.rotate(module, pt_want);
164
165    // First limb should be exactly equal (test are parameterized such that the noise does not reach
166    // the first limb)
167    assert_eq!(pt_have.data.at(0, 0), lut.data[0].at(0, 0));
168
169    // Verify that it effectively compute f(x)
170    let mut have: i64 = pt_have.decode_coeff_i64((log_message_modulus + 1).into(), 0);
171
172    // Get positive representative and assert equality
173    have = (have + message_modulus as i64) % (message_modulus as i64);
174
175    assert_eq!(have, f(x) % (message_modulus as i64));
176}