arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/blind_rotation/test/cggi.rs
core/src/blind_rotation/test/cggi.rsRaw
  1use backend::{
  2    hal::{
  3        api::{
  4            MatZnxAlloc, ModuleNew, ScalarZnxAlloc, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddNormal,
  5            VecZnxAddScalarInplace, VecZnxAlloc, VecZnxAllocBytes, VecZnxEncodeCoeffsi64, VecZnxFillUniform, VecZnxRotateInplace,
  6            VecZnxSwithcDegree, ZnxView,
  7        },
  8        layouts::{Backend, Module, ScratchOwned},
  9        oep::{
 10            ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl,
 11            TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
 12        },
 13    },
 14    implementation::cpu_spqlios::FFT64,
 15};
 16use sampling::source::Source;
 17
 18use crate::{
 19    BlindRotationKeyCGGIExecLayoutFamily, CCGIBlindRotationFamily, GLWECiphertext, GLWEDecryptFamily, GLWEPlaintext, GLWESecret,
 20    GLWESecretExec, GLWESecretFamily, Infos, LWECiphertext, LWESecret,
 21    blind_rotation::{
 22        cggi::{cggi_blind_rotate, cggi_blind_rotate_scratch_space, negate_and_mod_switch_2n},
 23        key::{BlindRotationKeyCGGI, BlindRotationKeyCGGIExec},
 24        lut::LookUpTable,
 25    },
 26    lwe::{LWEPlaintext, ciphertext::LWECiphertextToRef},
 27};
 28
 29#[test]
 30fn standard() {
 31    let module: Module<FFT64> = Module::<FFT64>::new(512);
 32    blind_rotatio_test(&module, 224, 1, 1);
 33}
 34
 35#[test]
 36fn block_binary() {
 37    let module: Module<FFT64> = Module::<FFT64>::new(512);
 38    blind_rotatio_test(&module, 224, 7, 1);
 39}
 40
 41#[test]
 42fn block_binary_extended() {
 43    let module: Module<FFT64> = Module::<FFT64>::new(512);
 44    blind_rotatio_test(&module, 224, 7, 2);
 45}
 46
 47pub(crate) trait CGGITestModuleFamily<B: Backend> = CCGIBlindRotationFamily<B>
 48    + GLWESecretFamily<B>
 49    + GLWEDecryptFamily<B>
 50    + BlindRotationKeyCGGIExecLayoutFamily<B>
 51    + VecZnxAlloc
 52    + ScalarZnxAlloc
 53    + VecZnxFillUniform
 54    + VecZnxAddNormal
 55    + VecZnxAllocBytes
 56    + VecZnxAddScalarInplace
 57    + VecZnxEncodeCoeffsi64
 58    + VecZnxRotateInplace
 59    + VecZnxSwithcDegree
 60    + MatZnxAlloc;
 61pub(crate) trait CGGITestScratchFamily<B: Backend> = VecZnxDftAllocBytesImpl<B>
 62    + VecZnxBigAllocBytesImpl<B>
 63    + ScratchOwnedAllocImpl<B>
 64    + ScratchOwnedBorrowImpl<B>
 65    + TakeVecZnxDftImpl<B>
 66    + TakeVecZnxBigImpl<B>
 67    + TakeVecZnxDftSliceImpl<B>
 68    + ScratchAvailableImpl<B>
 69    + TakeVecZnxImpl<B>
 70    + TakeVecZnxSliceImpl<B>;
 71
 72fn blind_rotatio_test<B: Backend>(module: &Module<B>, n_lwe: usize, block_size: usize, extension_factor: usize)
 73where
 74    Module<B>: CGGITestModuleFamily<B>,
 75    B: CGGITestScratchFamily<B>,
 76{
 77    let basek: usize = 19;
 78
 79    let k_lwe: usize = 24;
 80    let k_brk: usize = 3 * basek;
 81    let rows_brk: usize = 2; // Ensures first limb is noise-free.
 82    let k_lut: usize = 1 * basek;
 83    let k_res: usize = 2 * basek;
 84    let rank: usize = 1;
 85
 86    let message_modulus: usize = 1 << 4;
 87
 88    let mut source_xs: Source = Source::new([2u8; 32]);
 89    let mut source_xe: Source = Source::new([2u8; 32]);
 90    let mut source_xa: Source = Source::new([1u8; 32]);
 91
 92    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, rank);
 93    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
 94    let sk_glwe_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_glwe);
 95
 96    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
 97    sk_lwe.fill_binary_block(block_size, &mut source_xs);
 98
 99    let mut scratch: ScratchOwned<B> = ScratchOwned::<B>::alloc(BlindRotationKeyCGGI::generate_from_sk_scratch_space(
100        module, basek, k_brk, rank,
101    ));
102
103    let mut scratch_br: ScratchOwned<B> = ScratchOwned::<B>::alloc(cggi_blind_rotate_scratch_space(
104        module,
105        block_size,
106        extension_factor,
107        basek,
108        k_res,
109        k_brk,
110        rows_brk,
111        rank,
112    ));
113
114    let mut brk: BlindRotationKeyCGGI<Vec<u8>> = BlindRotationKeyCGGI::alloc(module, n_lwe, basek, k_brk, rows_brk, rank);
115
116    brk.generate_from_sk(
117        module,
118        &sk_glwe_dft,
119        &sk_lwe,
120        &mut source_xa,
121        &mut source_xe,
122        3.2,
123        scratch.borrow(),
124    );
125
126    let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
127
128    let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
129
130    let x: i64 = 2;
131    let bits: usize = 8;
132
133    module.encode_coeff_i64(basek, &mut pt_lwe.data, 0, bits, 0, x, bits);
134
135    lwe.encrypt_sk(
136        module,
137        &pt_lwe,
138        &sk_lwe,
139        &mut source_xa,
140        &mut source_xe,
141        3.2,
142    );
143
144    let mut f: Vec<i64> = vec![0i64; message_modulus];
145    f.iter_mut()
146        .enumerate()
147        .for_each(|(i, x)| *x = 2 * (i as i64) + 1);
148
149    let mut lut: LookUpTable = LookUpTable::alloc(module, basek, k_lut, extension_factor);
150    lut.set(module, &f, message_modulus);
151
152    let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(module, basek, k_res, rank);
153
154    let brk_exec: BlindRotationKeyCGGIExec<Vec<u8>, B> = BlindRotationKeyCGGIExec::from(module, &brk, scratch_br.borrow());
155
156    cggi_blind_rotate(module, &mut res, &lwe, &lut, &brk_exec, scratch_br.borrow());
157
158    let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(module, basek, k_res);
159
160    res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
161
162    let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
163
164    negate_and_mod_switch_2n(2 * lut.domain_size(), &mut lwe_2n, &lwe.to_ref());
165
166    let pt_want: i64 = (lwe_2n[0]
167        + lwe_2n[1..]
168            .iter()
169            .zip(sk_lwe.data.at(0, 0))
170            .map(|(x, y)| x * y)
171            .sum::<i64>())
172        & (2 * lut.domain_size() - 1) as i64;
173
174    lut.rotate(module, pt_want);
175
176    // First limb should be exactly equal (test are parameterized such that the noise does not reach
177    // the first limb)
178    assert_eq!(pt_have.data.at(0, 0), lut.data[0].at(0, 0));
179}