poulpy/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs
poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rsRaw
1mod add;
2mod and;
3mod fheuint;
4mod ggsw_blind_rotations;
5mod glwe_blind_rotation;
6mod glwe_blind_selection;
7mod or;
8mod prepare;
9mod sll;
10mod slt;
11mod sltu;
12mod sra;
13mod srl;
14mod sub;
15mod swap;
16mod xor;
17
18pub use add::*;
19pub use and::*;
20pub use fheuint::*;
21pub use ggsw_blind_rotations::*;
22pub use glwe_blind_rotation::*;
23pub use glwe_blind_selection::*;
24pub use or::*;
25use poulpy_hal::{
26 api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
27 layouts::{Backend, Module, Scratch, ScratchOwned},
28 source::Source,
29};
30pub use prepare::*;
31pub use sll::*;
32pub use slt::*;
33pub use sltu::*;
34pub use sra::*;
35pub use srl::*;
36pub use sub::*;
37pub use swap::*;
38pub use xor::*;
39
40use poulpy_core::{
41 ScratchTakeCore,
42 layouts::{
43 Base2K, Degree, Dnum, Dsize, GGLWEToGGSWKeyLayout, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret,
44 GLWESecretPrepared, GLWESecretPreparedFactory, GLWEToLWEKeyLayout, LWESecret, Rank, TorusPrecision,
45 },
46};
47
48use crate::tfhe::{
49 bdd_arithmetic::{BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory},
50 blind_rotation::{
51 BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyLayout, BlindRotationKeyPreparedFactory,
52 },
53 circuit_bootstrapping::CircuitBootstrappingKeyLayout,
54};
55
56pub struct TestContext<BRA: BlindRotationAlgo, BE: Backend> {
57 pub module: Module<BE>,
58 pub sk_glwe: GLWESecretPrepared<Vec<u8>, BE>,
59 pub sk_lwe: LWESecret<Vec<u8>>,
60 pub bdd_key: BDDKeyPrepared<Vec<u8>, BRA, BE>,
61}
62
63impl<BRA: BlindRotationAlgo, BE: Backend> Default for TestContext<BRA, BE>
64where
65 Module<BE>: ModuleNew<BE>
66 + BDDKeyEncryptSk<BRA, BE>
67 + GLWESecretPreparedFactory<BE>
68 + BlindRotationKeyPreparedFactory<BRA, BE>
69 + BDDKeyPreparedFactory<BRA, BE>,
70 BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
71 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
72 Scratch<BE>: ScratchTakeCore<BE>,
73{
74 fn default() -> Self {
75 Self::new()
76 }
77}
78
79impl<BRA: BlindRotationAlgo, BE: Backend> TestContext<BRA, BE> {
80 pub fn glwe_infos(&self) -> GLWELayout {
81 TEST_GLWE_INFOS
82 }
83
84 pub fn ggsw_infos(&self) -> GGSWLayout {
85 TEST_GGSW_INFOS
86 }
87
88 pub fn new() -> Self
89 where
90 Module<BE>: ModuleNew<BE>
91 + BDDKeyEncryptSk<BRA, BE>
92 + GLWESecretPreparedFactory<BE>
93 + BlindRotationKeyPreparedFactory<BRA, BE>
94 + BDDKeyPreparedFactory<BRA, BE>,
95 BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
96 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
97 Scratch<BE>: ScratchTakeCore<BE>,
98 {
99 let module: Module<BE> = Module::<BE>::new(TEST_N_GLWE as u64);
100
101 let mut source_xs: Source = Source::new([1u8; 32]);
102 let mut source_xa: Source = Source::new([2u8; 32]);
103 let mut source_xe: Source = Source::new([3u8; 32]);
104
105 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
106
107 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(TEST_N_GLWE.into(), TEST_RANK.into());
108 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
109 let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(&module, TEST_RANK.into());
110 sk_glwe_prep.prepare(&module, &sk_glwe);
111
112 let n_lwe: u32 = TEST_N_LWE;
113 let block_size: u32 = TEST_BLOCK_SIZE;
114 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
115 sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
116 let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
117 let mut bdd_key: BDDKey<Vec<u8>, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos);
118 bdd_key.encrypt_sk(
119 &module,
120 &sk_lwe,
121 &sk_glwe,
122 &mut source_xa,
123 &mut source_xe,
124 scratch.borrow(),
125 );
126 let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos);
127 bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow());
128
129 TestContext {
130 bdd_key: bdd_key_prepared,
131 sk_glwe: sk_glwe_prep,
132 sk_lwe,
133 module,
134 }
135 }
136}
137
138pub(crate) const TEST_N_GLWE: u32 = 256;
139pub(crate) const TEST_N_LWE: u32 = 77;
140pub(crate) const TEST_BASE2K: u32 = 13;
141pub(crate) const TEST_K_GLWE: u32 = 26;
142pub(crate) const TEST_K_GGSW: u32 = 39;
143pub(crate) const TEST_BLOCK_SIZE: u32 = 7;
144pub(crate) const TEST_RANK: u32 = 2;
145
146pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout {
147 n: Degree(TEST_N_GLWE),
148 base2k: Base2K(TEST_BASE2K),
149 k: TorusPrecision(TEST_K_GLWE),
150 rank: Rank(TEST_RANK),
151};
152
153pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout {
154 n: Degree(TEST_N_GLWE),
155 base2k: Base2K(TEST_BASE2K),
156 k: TorusPrecision(TEST_K_GGSW),
157 rank: Rank(TEST_RANK),
158 dnum: Dnum(2),
159 dsize: Dsize(1),
160};
161
162pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
163 cbt: CircuitBootstrappingKeyLayout {
164 layout_brk: BlindRotationKeyLayout {
165 n_glwe: Degree(TEST_N_GLWE),
166 n_lwe: Degree(TEST_N_LWE),
167 base2k: Base2K(TEST_BASE2K),
168 k: TorusPrecision(52),
169 dnum: Dnum(3),
170 rank: Rank(TEST_RANK),
171 },
172 layout_atk: GLWEAutomorphismKeyLayout {
173 n: Degree(TEST_N_GLWE),
174 base2k: Base2K(TEST_BASE2K),
175 k: TorusPrecision(52),
176 rank: Rank(TEST_RANK),
177 dnum: Dnum(3),
178 dsize: Dsize(1),
179 },
180 layout_tsk: GGLWEToGGSWKeyLayout {
181 n: Degree(TEST_N_GLWE),
182 base2k: Base2K(TEST_BASE2K),
183 k: TorusPrecision(52),
184 rank: Rank(TEST_RANK),
185 dnum: Dnum(3),
186 dsize: Dsize(1),
187 },
188 },
189 ks: GLWEToLWEKeyLayout {
190 n: Degree(TEST_N_GLWE),
191 base2k: Base2K(TEST_BASE2K),
192 k: TorusPrecision(39),
193 rank_in: Rank(TEST_RANK),
194 dnum: Dnum(2),
195 },
196};