-
Notifications
You must be signed in to change notification settings - Fork 4
/
kpabe_test.cpp
210 lines (160 loc) · 5.32 KB
/
kpabe_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE kpabe_test
#include <vector>
#include <string>
#include <iostream>
#include <boost/test/unit_test.hpp>
#include <pbc.h>
#include "kpabe.hpp"
using namespace std;
struct InitPolicy {
Node root;
vector<int> attributes;
InitPolicy() : root(Node::Type::AND), attributes({1, 2, 3, 4}) {
// (one or two) and (three or four)
vector<Node> children1, children2;
for(auto it = attributes.begin(); it != attributes.begin() + attributes.size() / 2; ++it) {
children1.emplace_back(*it);
children2.emplace_back(*(it + 2));
}
Node orNodeLeft(Node::Type::OR, children1);
Node orNodeRight(Node::Type::OR, children2);
root.addChild(orNodeLeft);
root.addChild(orNodeRight);
}
};
struct InitGenerator: InitPolicy {
PrivateParams priv;
PublicParams pub;
InitGenerator() : InitPolicy() {
setup({1, 2, 3, 4}, pub, priv);
}
};
BOOST_AUTO_TEST_CASE(hashElement_test) {
element_t el;
element_init_G1(el, getPairing());
element_random(el);
uint8_t key[32];
hashElement(el, key);
//elementToKey(el, key, NULL);
element_clear(el);
}
BOOST_FIXTURE_TEST_CASE(getLeafs_test, InitPolicy) {
auto leafs = root.getLeafs();
for(auto attr: attributes) {
BOOST_CHECK(find(leafs.begin(), leafs.end(), attr) != leafs.end());
}
}
BOOST_FIXTURE_TEST_CASE(splitShares_test, InitPolicy) {
element_t rootSecret;
element_init_Zr(rootSecret, getPairing());
element_random(rootSecret);
auto shares = root.splitShares(*rootSecret);
for(auto& share: shares) {
element_add_ui(&share, &share, 1ul); // Sanity check that the shares are initialized
element_clear(&share);
}
element_clear(rootSecret);
}
BOOST_FIXTURE_TEST_CASE(getSecretShares_test, InitPolicy) {
element_t rootSecret;
element_init_Zr(rootSecret, getPairing());
element_random(rootSecret);
auto shares = root.getSecretShares(*rootSecret);
for(auto& share: shares) {
element_add_ui(&share, &share, 1ul); // Sanity check that the shares are initialized
element_clear(&share);
}
element_clear(rootSecret);
}
BOOST_FIXTURE_TEST_CASE(recoverCoefficients_test, InitPolicy) {
auto shares = root.recoverCoefficients();
for(auto& share: shares) {
element_add_ui(&share, &share, 1ul); // Sanity check that the shares are initialized
element_clear(&share);
}
}
BOOST_FIXTURE_TEST_CASE(satisfyingAttributes_test, InitPolicy) {
element_t rootCoeff;
element_init_Zr(rootCoeff, getPairing());
element_set1(rootCoeff);
vector<int> attr {1, 3};
vector<int> expected {1, 3};
auto sat = root.satisfyingAttributes(attr, *rootCoeff);
BOOST_CHECK(expected.size() == sat.size());
for(auto& s: sat) {
BOOST_CHECK(find(expected.begin(), expected.end(), s.first) != expected.end());
element_clear(&s.second);
}
element_clear(rootCoeff);
}
BOOST_FIXTURE_TEST_CASE(satisfyingAttributes_negative_test, InitPolicy) {
element_t rootCoeff;
element_init_Zr(rootCoeff, getPairing());
element_set1(rootCoeff);
vector<int> attr {1};
vector<int> expected {};
auto sat = root.satisfyingAttributes(attr, *rootCoeff);
BOOST_CHECK(expected.size() == sat.size());
// Just in case the test fails, clear all elements
for(auto& s: sat) {
const auto& a = s.first;
BOOST_CHECK(find(expected.begin(), expected.end(), a) != expected.end());
element_clear(&s.second);
}
element_clear(rootCoeff);
}
BOOST_AUTO_TEST_CASE(setupTest) {
vector<int> attributes {1, 2, 3};
PrivateParams priv;
PublicParams pub;
setup(attributes, pub, priv);
}
BOOST_FIXTURE_TEST_CASE(createSecretTest, InitPolicy) {
vector<int> attrUniverse {1, 2, 3, 4};
PrivateParams priv;
PublicParams pub;
setup(attributes, pub, priv);
auto key = keyGeneration(priv, root);
vector<int> expectedAttributes {1, 2, 3, 4};
BOOST_CHECK(expectedAttributes.size() == key.Di.size());
for(auto attr: expectedAttributes) {
auto attrDuPairIter = key.Di.find(attr);
BOOST_CHECK(attrDuPairIter != key.Di.end());
element_clear(&attrDuPairIter->second);
}
}
BOOST_FIXTURE_TEST_CASE(createSecretAndRecoverSecret, InitGenerator) {
element_s CsEnc, CsDec;
vector<int> encAttr {1, 3};
auto Cw = createSecret(pub, encAttr, CsEnc);
auto decKeyPolicy = keyGeneration(priv, root);
recoverSecret(decKeyPolicy, Cw, encAttr, CsDec);
BOOST_CHECK(!element_cmp(&CsEnc, &CsDec));
for(auto& attrCiPair: Cw) {
element_clear(&attrCiPair.second);
}
for(auto& attrDiPair: decKeyPolicy.Di) {
element_clear(&attrDiPair.second);
}
element_clear(&CsEnc);
element_clear(&CsDec);
}
BOOST_FIXTURE_TEST_CASE(encryptAndDecrypt, InitGenerator) {
const string message("Hello World!");
vector<int> attributes {1};
Cw_t Cw;
auto ciphertext = encrypt(pub, attributes, message, Cw);
Node policy(Node::Type::OR);
policy.addChild(Node(1));
policy.addChild(Node(2));
auto key = keyGeneration(priv, policy);
auto msg = decrypt(key, Cw, attributes, ciphertext);
for(auto& attrCiPair: Cw) {
element_clear(&attrCiPair.second);
}
for(auto& attrDiPair: key.Di) {
element_clear(&attrDiPair.second);
}
BOOST_CHECK(msg == message);
}