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