poulpy/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs
poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rsRaw
1use poulpy_hal::{
2 api::{ScratchOwnedAlloc, ScratchOwnedBorrow},
3 layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned},
4 source::Source,
5};
6
7use crate::{
8 GGSWCompressedEncryptSk, GGSWEncryptSk, GGSWNoise, ScratchTakeCore,
9 encryption::SIGMA,
10 layouts::{
11 GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPreparedApi, compressed::GGSWCompressed,
12 prepared::GLWESecretPrepared,
13 },
14};
15
16pub fn test_ggsw_encrypt_sk<BE: Backend>(module: &Module<BE>)
17where
18 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
19 Scratch<BE>: ScratchTakeCore<BE>,
20 Module<BE>: GGSWEncryptSk<BE> + GLWESecretPreparedApi<BE> + GGSWNoise<BE>,
21{
22 let base2k: usize = 12;
23 let k: usize = 54;
24 let dsize: usize = k / base2k;
25 for rank in 1_usize..3 {
26 for di in 1..dsize + 1 {
27 let n: usize = module.n();
28 let dnum: usize = (k - di * base2k) / (di * base2k);
29
30 let ggsw_infos: GGSWLayout = GGSWLayout {
31 n: n.into(),
32 base2k: base2k.into(),
33 k: k.into(),
34 dnum: dnum.into(),
35 dsize: di.into(),
36 rank: rank.into(),
37 };
38
39 let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos);
40
41 let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
42
43 let mut source_xs: Source = Source::new([0u8; 32]);
44 let mut source_xe: Source = Source::new([0u8; 32]);
45 let mut source_xa: Source = Source::new([0u8; 32]);
46
47 pt_scalar.fill_ternary_hw(0, n, &mut source_xs);
48
49 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos));
50
51 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_infos);
52 sk.fill_ternary_prob(0.5, &mut source_xs);
53
54 let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
55 sk_prepared.prepare(module, &sk);
56
57 ct.encrypt_sk(
58 module,
59 &pt_scalar,
60 &sk_prepared,
61 &mut source_xa,
62 &mut source_xe,
63 scratch.borrow(),
64 );
65
66 let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5;
67
68 ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f);
69 }
70 }
71}
72
73pub fn test_ggsw_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
74where
75 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
76 Scratch<BE>: ScratchTakeCore<BE>,
77 Module<BE>: GGSWCompressedEncryptSk<BE> + GLWESecretPreparedApi<BE> + GGSWNoise<BE> + GGSWDecompress,
78{
79 let base2k: usize = 12;
80 let k: usize = 54;
81 let dsize: usize = k / base2k;
82 for rank in 1_usize..3 {
83 for di in 1..dsize + 1 {
84 let n: usize = module.n();
85 let dnum: usize = (k - di * base2k) / (di * base2k);
86
87 let ggsw_infos: GGSWLayout = GGSWLayout {
88 n: n.into(),
89 base2k: base2k.into(),
90 k: k.into(),
91 dnum: dnum.into(),
92 dsize: di.into(),
93 rank: rank.into(),
94 };
95
96 let mut ct_compressed: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc_from_infos(&ggsw_infos);
97
98 let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
99
100 let mut source_xs: Source = Source::new([0u8; 32]);
101 let mut source_xe: Source = Source::new([0u8; 32]);
102
103 pt_scalar.fill_ternary_hw(0, n, &mut source_xs);
104
105 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes(module, &ggsw_infos));
106
107 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_infos);
108 sk.fill_ternary_prob(0.5, &mut source_xs);
109
110 let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
111 sk_prepared.prepare(module, &sk);
112
113 let seed_xa: [u8; 32] = [1u8; 32];
114
115 ct_compressed.encrypt_sk(
116 module,
117 &pt_scalar,
118 &sk_prepared,
119 seed_xa,
120 &mut source_xe,
121 scratch.borrow(),
122 );
123
124 let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5;
125
126 let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos);
127 ct.decompress(module, &ct_compressed);
128
129 ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f);
130 }
131 }
132}