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