arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs
poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rsRaw
 1use poulpy_core::{
 2    GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
 3    layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
 4};
 5use poulpy_hal::{
 6    api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
 7    layouts::{Backend, Module, Scratch, ScratchOwned},
 8    source::Source,
 9};
10use rand::RngCore;
11
12use crate::tfhe::{
13    bdd_arithmetic::{
14        BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
15        FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Xor,
16        tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
17    },
18    blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
19};
20
21pub fn test_bdd_xor<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
22where
23    Module<BE>: ModuleNew<BE>
24        + GLWESecretPreparedFactory<BE>
25        + GLWEDecrypt<BE>
26        + GLWENoise<BE>
27        + FheUintBlocksPreparedFactory<u32, BE>
28        + FheUintBlocksPreparedEncryptSk<u32, BE>
29        + FheUintBlockDebugPrepare<BRA, u32, BE>
30        + BDDKeyEncryptSk<BRA, BE>
31        + BDDKeyPreparedFactory<BRA, BE>
32        + GGSWNoise<BE>
33        + FheUintBlocksPrepare<BRA, u32, BE>
34        + ExecuteBDDCircuit2WTo1W<u32, BE>
35        + GLWEEncryptSk<BE>,
36    BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
37    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
38    Scratch<BE>: ScratchTakeCore<BE>,
39{
40    let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
41    let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
42
43    let module: &Module<BE> = &test_context.module;
44    let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
45    let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
46
47    let mut source: Source = Source::new([6u8; 32]);
48    let mut source_xa: Source = Source::new([2u8; 32]);
49    let mut source_xe: Source = Source::new([3u8; 32]);
50
51    let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
52
53    let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
54    let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc_from_infos(module, &ggsw_infos);
55    let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc_from_infos(module, &ggsw_infos);
56
57    let a: u32 = source.next_u32();
58    let b: u32 = source.next_u32();
59
60    source.fill_bytes(&mut scratch.borrow().data);
61    a_enc_prep.encrypt_sk(
62        module,
63        a,
64        sk_glwe_prep,
65        &mut source_xa,
66        &mut source_xe,
67        scratch.borrow(),
68    );
69    source.fill_bytes(&mut scratch.borrow().data);
70    b_enc_prep.encrypt_sk(
71        module,
72        b,
73        sk_glwe_prep,
74        &mut source_xa,
75        &mut source_xe,
76        scratch.borrow(),
77    );
78
79    res.xor(
80        module,
81        &a_enc_prep,
82        &b_enc_prep,
83        bdd_key_prepared,
84        scratch.borrow(),
85    );
86
87    assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a ^ b);
88}