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