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, GLWESecret, GLWESecretPreparedFactory, LWEInfos, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
15 FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Xor,
16 tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
17 },
18 blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
19};
20
21pub fn test_bdd_xor<BRA: BlindRotationAlgo, BE: Backend>()
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 n_glwe: usize = glwe_infos.n().into();
44
45 let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
46 let mut source: Source = Source::new([6u8; 32]);
47 let mut source_xs: Source = Source::new([1u8; 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 sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
54 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
55 let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos);
56 sk_glwe_prep.prepare(&module, &sk_glwe);
57
58 let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
59 let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
60 let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
61
62 let a: u32 = source.next_u32();
63 let b: u32 = source.next_u32();
64
65 source.fill_bytes(&mut scratch.borrow().data);
66 a_enc_prep.encrypt_sk(
67 &module,
68 a,
69 &sk_glwe_prep,
70 &mut source_xa,
71 &mut source_xe,
72 scratch.borrow(),
73 );
74 source.fill_bytes(&mut scratch.borrow().data);
75 b_enc_prep.encrypt_sk(
76 &module,
77 b,
78 &sk_glwe_prep,
79 &mut source_xa,
80 &mut source_xe,
81 scratch.borrow(),
82 );
83
84 res.xor(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
85
86 assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a ^ b);
87}