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