-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
330 lines (298 loc) · 9.75 KB
/
main.cc
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <iostream>
#include <nan.h>
#include <sstream>
#include <iomanip>
#include <vector>
#include "openssl/objects.h"
#include <openssl/sha.h>
#include "./pbcwrapper/PBC.h"
using namespace std;
using namespace v8;
#include <fstream>
const char *paramFileName = "pairing.param";
FILE *sysParamFile = fopen(paramFileName, "r");
Pairing e(sysParamFile);
Zr ZERO(e, (long int)0);
Zr ONE(e, (long int)1);
G1 g1;
Zr polynomialEvaluation(long int x, long int size, Zr a[]) {
Zr y(e, (long int) 0);
Zr xx(e, (long int) 1);
for (int coeff = 0; coeff < size; coeff++) {
Zr x_val(e, x);
y += a[coeff] * xx;
xx *= x_val;
}
return y;
}
string sha256(const string str) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str.c_str(), str.size());
SHA256_Final(hash, &sha256);
stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
OPENSSL_cleanse(&sha256, sizeof(sha256));
return ss.str();
}
static inline unsigned int value(char c) {
if (c >= '0' && c <= '9') { return c - '0'; }
if (c >= 'a' && c <= 'f') { return c - 'a' + 10; }
if (c >= 'A' && c <= 'F') { return c - 'A' + 10; }
return -1;
}
char nibble(char c) {
if (c >= '0' && c <= '9') { return c - '0'; }
if (c >= 'a' && c <= 'f') { return c - 'a' + 10; }
return c - 'A' + 10;
}
string str_xor(string const &s1, string const &s2) {
assert(s1.length() == s2.length());
static char const alphabet[] = "0123456789abcdef";
string result;
result.reserve(s1.length());
for (size_t i = 0; i != s1.length(); ++i) {
unsigned int v = value(s1[i]) ^ value(s2[i]);
assert(v < sizeof alphabet);
result.push_back(alphabet[v]);
}
return result;
}
string stringToHex(string s) {
string o;
for (size_t i=0 ; i<s.size() ; i+=2){
char c = (nibble(s[i]) << 4) | nibble(s[i+1]);
o.push_back(c);
}
return o;
}
Zr lagrange(long int j, int arr[], int l) {
Zr num(e, (long int)1);
Zr den(e, (long int)1);
Zr jj(e, (long int)j);
for (int i = 0; i < l; i++) {
Zr ii(e, (long int)arr[i]);
if (!(ii == jj)) {
num *= (ZERO - ii - ONE);
den *= (jj - ii);
}
}
return num/den;
}
NAN_METHOD(CombineShares) {
if (!info[0]->IsObject() && !info[1]->IsArray() && !info[2]->IsArray()) {
Nan::ThrowTypeError("first argument must be an object, second must be an array!");
return;
}
// Prepare isolate
Isolate* isolate = info.GetIsolate();
// grab the parameters:
Local<Object> c(info[0]->ToObject());
String::Utf8Value paramV(c->Get(String::NewFromUtf8(isolate, "V")));
Local<Array> paramK = Local<Array>::Cast(info[1]);
Local<Array> paramS = Local<Array>::Cast(info[2]);
int length = paramS->Length();
int keys[length];
for (int i = 0; i < length; i++) {
keys[i] = paramK->Get(i)->NumberValue();
}
String::Utf8Value shareI(paramS->Get(0)->ToString());
string sS = stringToHex(*shareI);
G1 g(e, (const unsigned char *)sS.c_str(), sS.length());
G1 res = g^lagrange(keys[0], keys, length);
for (int i = 1; i < length; i++) {
String::Utf8Value shareI(paramS->Get(i)->ToString());
string sS = stringToHex(*shareI);
g = G1(e, (const unsigned char *)sS.c_str(), sS.length());
res *= g^lagrange(keys[i], keys, length);
}
string hashG = sha256(stringToHex(res.toHexString(true)));
string r = str_xor(hashG, *paramV);
info.GetReturnValue().Set(String::NewFromUtf8(isolate, r.c_str()));
}
NAN_METHOD(VerifyShare) {
if (!info[0]->IsObject() && !info[1]->IsString() && !info[2]->IsString()) {
Nan::ThrowTypeError("first argument must be an object, second must be a string!");
return;
}
// Prepare isolate
Isolate* isolate = info.GetIsolate();
// grab the parameters:
Local<Object> c(info[0]->ToObject());
String::Utf8Value paramU(c->Get(String::NewFromUtf8(isolate, "U")));
String::Utf8Value paramUi(info[1]->ToString());
String::Utf8Value paramVKsi(info[2]->ToString());
// U
string sU = stringToHex(*paramU);
G1 U(e, (const unsigned char *)sU.c_str(), sU.length());
// Ui
string sUi = stringToHex(*paramUi);
G2 Ui(e, (const unsigned char *)sUi.c_str(), sUi.length());
// VKsi
string sVKsi = stringToHex(*paramVKsi);
G2 VKsi(e, (const unsigned char *)sVKsi.c_str(), sVKsi.length());
if (e(g1, Ui) == e(U, VKsi))
info.GetReturnValue().Set(Nan::True());
else
info.GetReturnValue().Set(Nan::False());
}
NAN_METHOD(DecryptShare) {
// Check correctness of ciphertext
if (!info[0]->IsObject() && !info[1]->IsString()) {
Nan::ThrowTypeError("first argument must be an object, second must be a string!");
return;
}
// Prepare isolate
Isolate* isolate = info.GetIsolate();
// grab the parameters:
Local<Object> c(info[0]->ToObject());
String::Utf8Value paramU(c->Get(String::NewFromUtf8(isolate,"U")));
String::Utf8Value paramSK(info[1]->ToString());
// setup U
string sU = stringToHex(*paramU);
G1 U(e, (const unsigned char *)sU.c_str(), sU.length());
// setup SK
string sSK = stringToHex(*paramSK);
Zr SK(e, (const unsigned char *)sSK.c_str(), sSK.length());
G1 Ui = U^SK;
info.GetReturnValue().Set(String::NewFromUtf8(isolate, Ui.toHexString(false).c_str()));
}
NAN_METHOD(VerifyCipherText) {
// Check correctness of ciphertext
if (!info[0]->IsObject()) {
Nan::ThrowTypeError("arguments must be strings!");
return;
}
// Prepare isolate
Isolate* isolate = info.GetIsolate();
// grab the parameters:
Local<Object> c(info[0]->ToObject());
String::Utf8Value paramU(c->Get(String::NewFromUtf8(isolate,"U")));
String::Utf8Value paramV(c->Get(String::NewFromUtf8(isolate,"V")));
String::Utf8Value paramW(c->Get(String::NewFromUtf8(isolate,"W")));
// setup U
string sU = stringToHex(*paramU);
G1 U(e, (const unsigned char *)sU.c_str(), sU.length());
// setup V
string UV = U.toHexString(true);
string sH = stringToHex(UV.substr(0, UV.size()-2));
G2 H(e, sH, sH.length());
// setup W
string sW = stringToHex(*paramW);
G2 W(e, (const unsigned char *)sW.c_str(), sW.length());
if (e(g1, W) == e(U, H))
info.GetReturnValue().Set(Nan::True());
else
info.GetReturnValue().Set(Nan::False());
}
NAN_METHOD(Encrypt) {
if (!info[0]->IsString() && !info[1]->IsString()) {
Nan::ThrowTypeError("arguments must be strings!");
return;
}
// Prepare isolate
Isolate* isolate = info.GetIsolate();
// get the message and primary verification key
String::Utf8Value paramM(info[0]->ToString());
String::Utf8Value paramVK(info[1]->ToString());
string m;
if (paramM.length() == 64)
m = (string) *paramM;
else
m = sha256((string) *paramM);
// setup random value
Zr r(e, true);
// U
G1 U(e);
U = G1(g1^r);
// V
string sVK = stringToHex(*paramVK);
G1 VK(e, (const unsigned char *)sVK.c_str(), sVK.length());
VK ^= r;
string hashG = sha256(stringToHex(VK.toHexString(true)));
string V = str_xor(m, hashG);
// Use U (compressed) to create W
string sU = U.toHexString(true);
string sW = stringToHex(sU.substr(0, sU.size()-2));
G2 W(e, sW, sW.length());
// W must be raised to r:
W ^= r;
// Prepare an object and send the ciphertext
Local<Object> obj = Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate, "G1"),
String::NewFromUtf8(isolate, g1.toHexString(false).c_str()));
obj->Set(String::NewFromUtf8(isolate, "U"),
String::NewFromUtf8(isolate, U.toHexString(false).c_str()));
obj->Set(String::NewFromUtf8(isolate, "V"),
String::NewFromUtf8(isolate, V.c_str()));
obj->Set(String::NewFromUtf8(isolate, "W"),
String::NewFromUtf8(isolate, W.toHexString(false).c_str()));
info.GetReturnValue().Set(obj);
}
NAN_METHOD(Dealer) {
if (!info[0]->IsNumber() && !info[1]->IsNumber()) {
Nan::ThrowTypeError("arguments must be numbers!");
return;
}
// Prepare isolate
Isolate* isolate = info.GetIsolate();
// grab player count and # of shards needed to successfully decrypt (k)
int players = (int) info[0]->NumberValue();
int k = (int) info[1]->NumberValue();
g1 = G1(e, false);
// prepare all polynomial secrets. Master ksecret is secrets[0]
Zr secrets[k];
for (int i = 0; i < k; i++) {
secrets[i] = Zr(e, true);
}
// get all shared secrets of master key (secrets[0])
Zr SKs[players];
Local<Array> lSKs = Array::New(isolate);
for (long int i = 0; i < players; i++) {
SKs[i] = polynomialEvaluation(i + 1, k, secrets);
lSKs->Set(i, String::NewFromUtf8(isolate, SKs[i].toHexString().c_str()));
}
// Create verifications keys:
G1 VK(e);
VK = g1^secrets[0];
G1 VKs[players];
Local<Array> lVKs = Array::New(isolate);
for (int i = 0; i < players; i++) {
VKs[i] = g1^SKs[i];
lVKs->Set(i, String::NewFromUtf8(isolate, VKs[i].toHexString(false).c_str()));
}
// Let's create the v8 object and pass it over to node
Local<Object> obj = Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate, "secret"),
String::NewFromUtf8(isolate, secrets[0].toHexString().c_str()));
obj->Set(String::NewFromUtf8(isolate, "SKs"),
lSKs);
obj->Set(String::NewFromUtf8(isolate, "VK"),
String::NewFromUtf8(isolate, VK.toHexString(false).c_str()));
obj->Set(String::NewFromUtf8(isolate, "VKs"),
lVKs);
info.GetReturnValue().Set(obj);
}
NAN_METHOD(SetG) {
if (!info[0]->IsString()) {
Nan::ThrowTypeError("argument must be string!");
return;
}
// get G1 value
String::Utf8Value paramG(info[0]->ToString());
string sG = stringToHex(*paramG);
g1 = G1(e, (const unsigned char *)sG.c_str(), sG.length());
}
NAN_MODULE_INIT(Initialize) {
NAN_EXPORT(target, SetG);
NAN_EXPORT(target, Dealer);
NAN_EXPORT(target, Encrypt);
NAN_EXPORT(target, VerifyCipherText);
NAN_EXPORT(target, DecryptShare);
NAN_EXPORT(target, VerifyShare);
NAN_EXPORT(target, CombineShares);
}
NODE_MODULE(addon, Initialize);