arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/bin_fhe/bdd_arithmetic/tests/test_suite/mod.rs
poulpy-schemes/src/bin_fhe/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, GLWESwitchingKeyLayout, GLWEToLWEKeyLayout, LWESecret, Rank,
 45        TorusPrecision,
 46    },
 47};
 48
 49use crate::bin_fhe::{
 50    bdd_arithmetic::{BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory},
 51    blind_rotation::{
 52        BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyLayout, BlindRotationKeyPreparedFactory,
 53    },
 54    circuit_bootstrapping::CircuitBootstrappingKeyLayout,
 55};
 56
 57pub struct TestContext<BRA: BlindRotationAlgo, BE: Backend> {
 58    pub module: Module<BE>,
 59    pub sk_glwe: GLWESecretPrepared<Vec<u8>, BE>,
 60    pub sk_lwe: LWESecret<Vec<u8>>,
 61    pub bdd_key: BDDKeyPrepared<Vec<u8>, BRA, BE>,
 62}
 63
 64impl<BRA: BlindRotationAlgo, BE: Backend> Default for TestContext<BRA, BE>
 65where
 66    Module<BE>: ModuleNew<BE>
 67        + BDDKeyEncryptSk<BRA, BE>
 68        + GLWESecretPreparedFactory<BE>
 69        + BlindRotationKeyPreparedFactory<BRA, BE>
 70        + BDDKeyPreparedFactory<BRA, BE>,
 71    BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
 72    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 73    Scratch<BE>: ScratchTakeCore<BE>,
 74{
 75    fn default() -> Self {
 76        Self::new()
 77    }
 78}
 79
 80impl<BRA: BlindRotationAlgo, BE: Backend> TestContext<BRA, BE> {
 81    pub fn glwe_infos(&self) -> GLWELayout {
 82        TEST_GLWE_INFOS
 83    }
 84
 85    pub fn ggsw_infos(&self) -> GGSWLayout {
 86        TEST_GGSW_INFOS
 87    }
 88
 89    pub fn new() -> Self
 90    where
 91        Module<BE>: ModuleNew<BE>
 92            + BDDKeyEncryptSk<BRA, BE>
 93            + GLWESecretPreparedFactory<BE>
 94            + BlindRotationKeyPreparedFactory<BRA, BE>
 95            + BDDKeyPreparedFactory<BRA, BE>,
 96        BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
 97        ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 98        Scratch<BE>: ScratchTakeCore<BE>,
 99    {
100        let module: Module<BE> = Module::<BE>::new(TEST_N_GLWE as u64);
101
102        let mut source_xs: Source = Source::new([1u8; 32]);
103        let mut source_xa: Source = Source::new([2u8; 32]);
104        let mut source_xe: Source = Source::new([3u8; 32]);
105
106        let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
107
108        let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(TEST_N_GLWE.into(), TEST_RANK.into());
109        sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
110        let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(&module, TEST_RANK.into());
111        sk_glwe_prep.prepare(&module, &sk_glwe);
112
113        let n_lwe: u32 = TEST_N_LWE;
114        let block_size: u32 = TEST_BLOCK_SIZE;
115        let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
116        sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
117        let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
118        let mut bdd_key: BDDKey<Vec<u8>, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos);
119        bdd_key.encrypt_sk(
120            &module,
121            &sk_lwe,
122            &sk_glwe,
123            &mut source_xa,
124            &mut source_xe,
125            scratch.borrow(),
126        );
127        let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos);
128        bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow());
129
130        TestContext {
131            bdd_key: bdd_key_prepared,
132            sk_glwe: sk_glwe_prep,
133            sk_lwe,
134            module,
135        }
136    }
137}
138
139pub(crate) const TEST_N_GLWE: u32 = 256;
140pub(crate) const TEST_N_LWE: u32 = 77;
141pub(crate) const TEST_FHEUINT_BASE2K: u32 = 13;
142pub(crate) const TEST_BRK_BASE2K: u32 = 12;
143pub(crate) const TEST_ATK_BASE2K: u32 = 11;
144pub(crate) const TEST_TSK_BASE2K: u32 = 10;
145pub(crate) const TEST_LWE_BASE2K: u32 = 4;
146pub(crate) const TEST_K_GLWE: u32 = 26;
147pub(crate) const TEST_K_GGSW: u32 = 39;
148pub(crate) const TEST_BLOCK_SIZE: u32 = 7;
149pub(crate) const TEST_RANK: u32 = 2;
150
151pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout {
152    n: Degree(TEST_N_GLWE),
153    base2k: Base2K(TEST_FHEUINT_BASE2K),
154    k: TorusPrecision(TEST_K_GLWE),
155    rank: Rank(TEST_RANK),
156};
157
158pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout {
159    n: Degree(TEST_N_GLWE),
160    base2k: Base2K(TEST_FHEUINT_BASE2K),
161    k: TorusPrecision(TEST_K_GGSW),
162    rank: Rank(TEST_RANK),
163    dnum: Dnum(2),
164    dsize: Dsize(1),
165};
166
167pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
168    cbt: CircuitBootstrappingKeyLayout {
169        layout_brk: BlindRotationKeyLayout {
170            n_glwe: Degree(TEST_N_GLWE),
171            n_lwe: Degree(TEST_N_LWE),
172            base2k: Base2K(TEST_BRK_BASE2K),
173            k: TorusPrecision(52),
174            dnum: Dnum(4),
175            rank: Rank(TEST_RANK),
176        },
177        layout_atk: GLWEAutomorphismKeyLayout {
178            n: Degree(TEST_N_GLWE),
179            base2k: Base2K(TEST_ATK_BASE2K),
180            k: TorusPrecision(52),
181            rank: Rank(TEST_RANK),
182            dnum: Dnum(4),
183            dsize: Dsize(1),
184        },
185        layout_tsk: GGLWEToGGSWKeyLayout {
186            n: Degree(TEST_N_GLWE),
187            base2k: Base2K(TEST_TSK_BASE2K),
188            k: TorusPrecision(52),
189            rank: Rank(TEST_RANK),
190            dnum: Dnum(4),
191            dsize: Dsize(1),
192        },
193    },
194    ks_glwe: Some(GLWESwitchingKeyLayout {
195        n: Degree(TEST_N_GLWE),
196        base2k: Base2K(TEST_LWE_BASE2K),
197        k: TorusPrecision(20),
198        rank_in: Rank(TEST_RANK),
199        rank_out: Rank(1),
200        dnum: Dnum(3),
201        dsize: Dsize(1),
202    }),
203    ks_lwe: GLWEToLWEKeyLayout {
204        n: Degree(TEST_N_GLWE),
205        base2k: Base2K(TEST_LWE_BASE2K),
206        k: TorusPrecision(16),
207        rank_in: Rank(1),
208        dnum: Dnum(3),
209    },
210};