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