arnaucube
arnaucube/poulpyPublic Self-hosted repository
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, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESecretPrepared,
 42        GLWESecretPreparedFactory, GLWETensorKeyLayout, 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 new() -> Self
 79    where
 80        Module<BE>: ModuleNew<BE>
 81            + BDDKeyEncryptSk<BRA, BE>
 82            + GLWESecretPreparedFactory<BE>
 83            + BlindRotationKeyPreparedFactory<BRA, BE>
 84            + BDDKeyPreparedFactory<BRA, BE>,
 85        BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
 86        ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 87        Scratch<BE>: ScratchTakeCore<BE>,
 88    {
 89        let module: Module<BE> = Module::<BE>::new(TEST_N_GLWE as u64);
 90
 91        let mut source_xs: Source = Source::new([1u8; 32]);
 92        let mut source_xa: Source = Source::new([2u8; 32]);
 93        let mut source_xe: Source = Source::new([3u8; 32]);
 94
 95        let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
 96
 97        let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(TEST_N_GLWE.into(), TEST_RANK.into());
 98        sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
 99        let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(&module, TEST_RANK.into());
100        sk_glwe_prep.prepare(&module, &sk_glwe);
101
102        let n_lwe: u32 = TEST_N_LWE;
103        let block_size: u32 = TEST_BLOCK_SIZE;
104        let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
105        sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
106        let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
107        let mut bdd_key: BDDKey<Vec<u8>, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos);
108        bdd_key.encrypt_sk(
109            &module,
110            &sk_lwe,
111            &sk_glwe,
112            &mut source_xa,
113            &mut source_xe,
114            scratch.borrow(),
115        );
116        let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos);
117        bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow());
118
119        TestContext {
120            bdd_key: bdd_key_prepared,
121            sk_glwe: sk_glwe_prep,
122            sk_lwe,
123            module,
124        }
125    }
126}
127
128pub(crate) const TEST_N_GLWE: u32 = 256;
129pub(crate) const TEST_N_LWE: u32 = 77;
130pub(crate) const TEST_BASE2K: u32 = 13;
131pub(crate) const TEST_K_GLWE: u32 = 26;
132pub(crate) const TEST_K_GGSW: u32 = 39;
133pub(crate) const TEST_BLOCK_SIZE: u32 = 7;
134pub(crate) const TEST_RANK: u32 = 2;
135
136pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout {
137    n: Degree(TEST_N_GLWE),
138    base2k: Base2K(TEST_BASE2K),
139    k: TorusPrecision(TEST_K_GLWE),
140    rank: Rank(TEST_RANK),
141};
142
143pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout {
144    n: Degree(TEST_N_GLWE),
145    base2k: Base2K(TEST_BASE2K),
146    k: TorusPrecision(TEST_K_GGSW),
147    rank: Rank(TEST_RANK),
148    dnum: Dnum(2),
149    dsize: Dsize(1),
150};
151
152pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
153    cbt: CircuitBootstrappingKeyLayout {
154        layout_brk: BlindRotationKeyLayout {
155            n_glwe: Degree(TEST_N_GLWE),
156            n_lwe: Degree(TEST_N_LWE),
157            base2k: Base2K(TEST_BASE2K),
158            k: TorusPrecision(52),
159            dnum: Dnum(3),
160            rank: Rank(TEST_RANK),
161        },
162        layout_atk: GLWEAutomorphismKeyLayout {
163            n: Degree(TEST_N_GLWE),
164            base2k: Base2K(TEST_BASE2K),
165            k: TorusPrecision(52),
166            rank: Rank(TEST_RANK),
167            dnum: Dnum(3),
168            dsize: Dsize(1),
169        },
170        layout_tsk: GLWETensorKeyLayout {
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    },
179    ks: GLWEToLWEKeyLayout {
180        n: Degree(TEST_N_GLWE),
181        base2k: Base2K(TEST_BASE2K),
182        k: TorusPrecision(39),
183        rank_in: Rank(TEST_RANK),
184        dnum: Dnum(2),
185    },
186};