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(&module, &sk_lwe, &sk_glwe, &mut source_xa, &mut source_xe, scratch.borrow());
120        let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos);
121        bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow());
122
123        TestContext {
124            bdd_key: bdd_key_prepared,
125            sk_glwe: sk_glwe_prep,
126            sk_lwe,
127            module,
128        }
129    }
130}
131
132pub(crate) const TEST_N_GLWE: u32 = 256;
133pub(crate) const TEST_N_LWE: u32 = 77;
134pub(crate) const TEST_FHEUINT_BASE2K: u32 = 13;
135pub(crate) const TEST_BRK_BASE2K: u32 = 12;
136pub(crate) const TEST_ATK_BASE2K: u32 = 11;
137pub(crate) const TEST_TSK_BASE2K: u32 = 10;
138pub(crate) const TEST_LWE_BASE2K: u32 = 4;
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_FHEUINT_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_FHEUINT_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_layout: CircuitBootstrappingKeyLayout {
162        brk_layout: BlindRotationKeyLayout {
163            n_glwe: Degree(TEST_N_GLWE),
164            n_lwe: Degree(TEST_N_LWE),
165            base2k: Base2K(TEST_BRK_BASE2K),
166            k: TorusPrecision(52),
167            dnum: Dnum(4),
168            rank: Rank(TEST_RANK),
169        },
170        atk_layout: GLWEAutomorphismKeyLayout {
171            n: Degree(TEST_N_GLWE),
172            base2k: Base2K(TEST_ATK_BASE2K),
173            k: TorusPrecision(52),
174            rank: Rank(TEST_RANK),
175            dnum: Dnum(4),
176            dsize: Dsize(1),
177        },
178        tsk_layout: GGLWEToGGSWKeyLayout {
179            n: Degree(TEST_N_GLWE),
180            base2k: Base2K(TEST_TSK_BASE2K),
181            k: TorusPrecision(52),
182            rank: Rank(TEST_RANK),
183            dnum: Dnum(4),
184            dsize: Dsize(1),
185        },
186    },
187    ks_glwe_layout: Some(GLWESwitchingKeyLayout {
188        n: Degree(TEST_N_GLWE),
189        base2k: Base2K(TEST_LWE_BASE2K),
190        k: TorusPrecision(20),
191        rank_in: Rank(TEST_RANK),
192        rank_out: Rank(1),
193        dnum: Dnum(3),
194        dsize: Dsize(1),
195    }),
196    ks_lwe_layout: GLWEToLWEKeyLayout {
197        n: Degree(TEST_N_GLWE),
198        base2k: Base2K(TEST_LWE_BASE2K),
199        k: TorusPrecision(16),
200        rank_in: Rank(1),
201        dnum: Dnum(3),
202    },
203};