]> git.siccegge.de Git - software/DIPE.git/blob - tests/testDIPE.cpp
0a3e209a2be6fff68919733d9d5755f85dab1143
[software/DIPE.git] / tests / testDIPE.cpp
1 #include <gtest/gtest.h>
2 #include <pbc/pbc.h>
3
4 #include "DIPE.h"
5
6 static dipe_param_t param;
7
8 TEST(DipeTest, DipeMasterKeygen) {
9 dipe_master_publickey_t pk;
10 dipe_master_secretkey_t sk;
11
12 dipe_master_keygen(param, 12, &pk, &sk);
13
14 dipe_free_master_secretkey(sk);
15 dipe_free_master_publickey(pk);
16 }
17
18 TEST(DipeTest, DipeKeygen) {
19 dipe_master_publickey_t pk;
20 dipe_master_secretkey_t msk;
21 dipe_secretkey_t sk;
22 element_t y[5];
23 for (size_t i = 0; i < 5; ++i) {
24 element_init_Zr(y[i], *dipe_get_pairing(param));
25 element_random(y[i]);
26 }
27
28 dipe_master_keygen(param, 5, &pk, &msk);
29 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
30
31 dipe_free_master_secretkey(msk);
32 dipe_free_master_publickey(pk);
33 dipe_free_secretkey(sk);
34
35 for (size_t i = 0; i < 5; ++i) {
36 element_clear(y[i]);
37 }
38 }
39
40 TEST(DipeTest, DipeDecryptFail) {
41 dipe_ctxt_t ctxt;
42 dipe_master_publickey_t pk;
43 dipe_master_secretkey_t msk;
44 dipe_secretkey_t sk;
45
46 element_t y[5];
47 for (size_t i = 0; i < 5; ++i) {
48 element_init_Zr(y[i], *dipe_get_pairing(param));
49 element_random(y[i]);
50 }
51
52 dipe_master_keygen(param, 5, &pk, &msk);
53 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
54
55 element_t ptxt, ptxtp;
56 element_init_GT(ptxt, *dipe_get_pairing(param));
57 element_init_GT(ptxtp, *dipe_get_pairing(param));
58
59 element_random(ptxt);
60
61 dipe_encrypt(param, pk, y, ptxt, &ctxt);
62 dipe_decrypt(param, sk, "1234567890abcdef", y, ctxt, ptxtp);
63
64 EXPECT_NE(element_cmp(ptxt, ptxtp), 0);
65
66 for (size_t i = 0; i < 5; ++i) {
67 element_clear(y[i]);
68 }
69
70 element_clear(ptxt);
71 element_clear(ptxtp);
72
73 dipe_free_master_secretkey(msk);
74 dipe_free_master_publickey(pk);
75 dipe_free_secretkey(sk);
76 dipe_free_ctxt(ctxt);
77 }
78
79 TEST(DipeTest, DipeDecryptSuccess) {
80 dipe_ctxt_t ctxt;
81 dipe_master_publickey_t pk;
82 dipe_master_secretkey_t msk;
83 dipe_secretkey_t sk;
84
85 element_t y[5];
86 for (size_t i = 0; i < 5; ++i) {
87 element_init_Zr(y[i], *dipe_get_pairing(param));
88 element_set0(y[i]);
89 }
90
91 dipe_master_keygen(param, 5, &pk, &msk);
92 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
93
94 element_t ptxt, ptxtp;
95 element_init_GT(ptxt, *dipe_get_pairing(param));
96 element_init_GT(ptxtp, *dipe_get_pairing(param));
97
98 element_random(ptxt);
99
100 dipe_encrypt(param, pk, y, ptxt, &ctxt);
101 dipe_decrypt(param, sk, "1234567890abcdef", y, ctxt, ptxtp);
102 element_printf("%B\n", ptxt);
103 element_printf("%B\n", ptxtp);
104
105 EXPECT_EQ(element_cmp(ptxt, ptxtp), 0);
106
107 for (size_t i = 0; i < 5; ++i) {
108 element_clear(y[i]);
109 }
110
111 element_clear(ptxt);
112 element_clear(ptxtp);
113
114 dipe_free_master_secretkey(msk);
115 dipe_free_master_publickey(pk);
116 dipe_free_secretkey(sk);
117 dipe_free_ctxt(ctxt);
118 }
119
120
121 int main(int argc, char **argv) {
122 ::testing::InitGoogleTest(&argc, argv);
123
124 dipe_init(fopen("../tests/a.param", "r"), &param);
125
126 int result = RUN_ALL_TESTS();
127 dipe_free_param(param);
128 return result;
129 }
130