-
Notifications
You must be signed in to change notification settings - Fork 2
/
eff-pre.js
560 lines (491 loc) · 13.4 KB
/
eff-pre.js
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
const mcl = require("mcl-wasm");
const crypto = require("crypto");
class PRE {
/**
* Init by L0,L1 and curve
* @param {number} L0
* @param {number} L1
* @param {PRE.CURVE} curve
* @returns {mcl.G1}
*/
static async init(L0, L1, curve = PRE.CURVE.SECP256K1) {
await mcl.init(curve);
PRE.g = mcl.getBasePointG1();
PRE.L0 = L0;
PRE.L1 = L1;
PRE.L = L0 + L1;
PRE.L_G = PRE.g.serialize().length;
PRE.L_Fr = new mcl.Fr().serialize().length;
return PRE.g
}
static get CURVE() {
return {
SECP224K1: 101,
SECP256K1: 102,
SECP384R1: 103,
NIST_P192: 105,
NIST_P224: 106,
NIST_P256: 107,
}
}
static get C1_LEN() {
return 2 * PRE.L_G + PRE.L + PRE.L_Fr
}
static get C2_LEN() {
return 2 * PRE.L_G + 2 * PRE.L
}
static get REKEY_LEN() {
return PRE.L_Fr + PRE.L_G + PRE.L
}
static get SIG_LEN() {
return 2 * PRE.L_Fr
}
static get STATUS() {
return {
VALID: 'VALID',
ERR_LENGTH: 'ERR_LENGTH',
ERR_PUB_VERIFY: 'ERR_PUB_VERIFY',
ERR_DEC1_VERIFY: 'ERR_DEC1_VERIFY',
ERR_DEC2_VERIFY: 'ERR_DEC2_VERIFY',
}
}
/**
* Hash Function H1
* @param {Buffer} m - message buffer with length l0
* @param {Buffer} w - message buffer with length l1
* @returns {Object} mcl.Fr
* @constructor
*/
static H1(m, w) {
return mcl.hashToFr(Buffer.concat([w, m]))
}
/**
* Hash Function H2
* @param {mcl.G1} point
* @returns {Uint8Array}
* @constructor
*/
static H2(point) {
return crypto.createHash('sha512').update(point.serialize()).digest().slice(0, PRE.L);
}
/**
* Hash Function H3
* @param {mcl.G1} D
* @param {mcl.G1} E
* @param {Buffer} F
* @returns {mcl.Fr}
* @constructor
*/
static H3(D, E, F) {
return mcl.hashToFr(PRE.xorArrays(F, PRE.xorArrays(PRE.toBuf(E), PRE.toBuf(D))))
}
/**
* H4(pk_2)
* @param pk2
* @returns {mcl.Fr}
*/
static h4pk2(pk2) {
return mcl.hashToFr(pk2.serialize());
}
/**
* Key Generation
* @returns {*[]}
*/
static keyGen() {
const sk1 = PRE.randomInFr();
const sk2 = PRE.randomInFr();
return [[sk1, sk2], PRE.pkFromSk([sk1, sk2])]
}
/**
* reKey Generation
* @param {mcl.Fr} ska1 A's secret key part 1
* @param {mcl.Fr} ska2 A's secret key part 2
* @param {mcl.G1} pka1 A's public key part 1
* @param {mcl.G1} pka2 A's public key part 2
* @param {mcl.G1} pkb1 B's public key part 1
* @param {mcl.G1} pkb2 B's public key part 2
* @returns {Buffer}
*/
static reKeyGen([ska1, ska2], [pka1, pka2], [pkb1, pkb2]) {
const h = crypto.randomBytes(PRE.L0);
const pi = crypto.randomBytes(PRE.L1);
const v = PRE.H1(h, pi);
const hpi = Buffer.concat([h, pi]);
const V = mcl.mul(pkb2, v); //in G
const W = PRE.xorArrays(PRE.H2(mcl.mul(PRE.g, v)), hpi);
const rk = mcl.div(mcl.hashToFr(h), mcl.add(mcl.mul(ska1, PRE.h4pk2(pka2)), ska2)); //in Fr
return Buffer.concat([PRE.toBuf(rk), PRE.toBuf(V), W])
}
/**
*
* @param M
* @param {mcl.G1} pk1
* @param {mcl.G1} pk2
* @returns {Buffer} - c1
*/
static enc1(M, [pk1, pk2]) {
let m;
if (typeof (M) === "string")
m = Buffer.from(M, 'hex');
else
m = Buffer.from(M);
const u = PRE.randomInFr();
const base = mcl.add(mcl.mul(pk1, PRE.h4pk2(pk2)), pk2);
const D = mcl.mul(base, u);// in G
const w = crypto.randomBytes(PRE.L1);
const r = PRE.H1(m, w);
const E = mcl.mul(base, r);// in G
const mw = Buffer.concat([m, w]);
const f = PRE.H2(mcl.mul(PRE.g, r));
const F = PRE.xorArrays(f, mw);// Buffer in L_G
const s = mcl.add(u, mcl.mul(r, PRE.H3(D, E, F)));//in Fr
return Buffer.concat([PRE.toBuf(D), PRE.toBuf(E), F, PRE.toBuf(s)])
}
/**
*
* @param M
* @param {mcl.G1} pk1
* @param {mcl.G1} pk2
* @returns {Buffer} - c2
*/
static enc2(M, [pk1, pk2]) {
let m;
if (typeof (M) === "string")
m = Buffer.from(M, 'hex');
else
m = Buffer.from(M);
const h = crypto.randomBytes(PRE.L0);
const pi = crypto.randomBytes(PRE.L1);
const v = PRE.H1(h, pi);
const hpi = Buffer.concat([h, pi]);
const V = mcl.mul(pk2, v); //in G
const W = PRE.xorArrays(PRE.H2(mcl.mul(PRE.g, v)), hpi);
const w = crypto.randomBytes(PRE.L1);
const r = PRE.H1(m, w);
const mw = Buffer.concat([m, w]);
const f = PRE.H2(mcl.mul(PRE.g, r));
const F = PRE.xorArrays(f, mw);// Buffer in L_G
const E1 = mcl.mul(PRE.g, mcl.mul(r, mcl.hashToFr(h)));
return Buffer.concat([PRE.toBuf(E1), F, PRE.toBuf(V), W])
}
/**
*
* @param reKey
* @param {Buffer} c1 - length C1_LEN
* @param {mcl.G1} pk1
* @param {mcl.G1} pk2
* @returns {Array}
*/
static reEnc(reKey, c1, [pk1, pk2]) {
if (reKey.length !== PRE.REKEY_LEN || c1.length !== PRE.C1_LEN)
return [PRE.STATUS.ERR_LENGTH];
const rk = PRE.bufToFr(reKey.slice(0, PRE.L_Fr));
const D = PRE.bufToG(c1.slice(0, PRE.L_G));
const E = PRE.bufToG(c1.slice(PRE.L_G, 2 * PRE.L_G));
const F = c1.slice(2 * PRE.L_G, 2 * PRE.L_G + PRE.L);
const s = PRE.bufToFr(c1.slice(2 * PRE.L_G + PRE.L));
const {status} = PRE.pubVerify([pk1, pk2], [D, E, F, s]);
if (status === PRE.STATUS.ERR_PUB_VERIFY)
return [status];
const E1 = mcl.mul(E, rk); //in G
return [status, Buffer.concat([PRE.toBuf(E1), F, reKey.slice(PRE.L_Fr)])]
}
/**
* dec1 on c1
* @param {Buffer} c1 - length C1_LEN
* @param {mcl.Fr} sk1
* @param {mcl.Fr} sk2
* @param {mcl.G1} pk1
* @param {mcl.G1} pk2
* @returns {Array}
*/
static dec1(c1, [sk1, sk2], [pk1, pk2]) {
if (c1.length !== PRE.C1_LEN)
return [PRE.STATUS.ERR_LENGTH];
const D = PRE.bufToG(c1.slice(0, PRE.L_G));
const E = PRE.bufToG(c1.slice(PRE.L_G, 2 * PRE.L_G));
const F = c1.slice(2 * PRE.L_G, 2 * PRE.L_G + PRE.L);
const s = PRE.bufToFr(c1.slice(2 * PRE.L_G + PRE.L));
const {status, h4pk2, base} = PRE.pubVerify([pk1, pk2], [D, E, F, s]);
if (status === PRE.STATUS.ERR_PUB_VERIFY)
return [status];
const index = mcl.inv(mcl.add(mcl.mul(sk1, h4pk2), sk2));
const H2EIndex = PRE.H2(mcl.mul(E, index));
const mw = PRE.xorArrays(F, H2EIndex);
const m = mw.slice(0, PRE.L0);
const w = mw.slice(PRE.L0);
return [PRE.dec1Verify(base, E, m, w), m]
}
/**
* dec 2 on c2
* @param {Buffer} c2 - length C2_LEN
* @param {mcl.Fr} sk1
* @param {mcl.Fr} sk2
* @param {mcl.G1} pk1
* @param {mcl.G1} pk2
* @returns {Array}
*/
static dec2(c2, [sk1, sk2], [pk1, pk2]) {
if (c2.length !== PRE.C2_LEN)
return [PRE.STATUS.ERR_LENGTH];
const E1 = PRE.bufToG(c2.slice(0, PRE.L_G));
const F = c2.slice(PRE.L_G, PRE.L_G + PRE.L);
const V = PRE.bufToG(c2.slice(PRE.L_G + PRE.L, 2 * PRE.L_G + PRE.L));
const W = c2.slice(2 * PRE.L_G + PRE.L);
const hpi = PRE.xorArrays(W, PRE.H2(mcl.mul(V, mcl.inv(sk2))));
const h = hpi.slice(0, PRE.L0);
const pi = hpi.slice(PRE.L0);
const mw = PRE.xorArrays(F, PRE.H2(mcl.mul(E1, mcl.inv(mcl.hashToFr(h)))));
const m = mw.slice(0, PRE.L0);
const w = mw.slice(PRE.L0);
return [PRE.dec2Verify(pk2, E1, V, h, pi, m, w), m]
}
/**
* sign message (ECDSA signature)
* @param {Buffer} m - message to be signed
* @param {mcl.Fr} sk1
* @param {mcl.Fr} sk2
* @param {mcl.G1} pk1
* @param {mcl.G1} pk2
* @returns {Buffer} signature
*/
static sign(m, [sk1, sk2], [pk1, pk2]) {
const z = mcl.hashToFr(m);
const k = PRE.randomInFr();
const r = new mcl.Fr();
r.setLittleEndianMod(mcl.mul(PRE.g, k).serialize());
const h4pk2 = PRE.h4pk2(pk2);
const base = mcl.add(mcl.mul(sk1, h4pk2), sk2);
const s = mcl.mul(mcl.inv(k), mcl.add(z, mcl.mul(r, base)));
return Buffer.concat([PRE.toBuf(r), PRE.toBuf(s)])
}
/**
* verify signature
* @param {Buffer} m - message to be signed
* @param {Buffer} sig - signature
* @param {mcl.G1} pk1
* @param {mcl.G1} pk2
* @returns {boolean}
*/
static verify(m, sig, [pk1, pk2]) {
if (sig.length !== PRE.SIG_LEN)
return false;
const r = PRE.bufToFr(sig.slice(0, PRE.L_Fr));
const s = PRE.bufToFr(sig.slice(PRE.L_Fr));
const z = mcl.hashToFr(m);
const h4pk2 = PRE.h4pk2(pk2);
const base2 = mcl.add(mcl.mul(pk1, h4pk2), pk2);
const w = mcl.inv(s);
const u1 = mcl.mul(z, w);
const u2 = mcl.mul(r, w);
const rr = new mcl.Fr();
rr.setLittleEndianMod(mcl.add(mcl.mul(PRE.g, u1), mcl.mul(base2, u2)).serialize());
return rr.isEqual(r)
}
/**
* xor 2 arrays
* @param {Uint8Array|Buffer} a1
* @param {Uint8Array|Buffer} a2
* @returns {Buffer}
*/
static xorArrays(a1, a2) {
const l = a1.length;
const r = Buffer.alloc(l);
for (let i = 0; i < l; i++)
r[i] = a1[i] ^ a2[i];
return r
}
/**
* verify can be performed by anyone on enc1 result
* @param {mcl.G1} pk1 - public key part 1
* @param {mcl.G1} pk2 - public key part 2
* @param {mcl.G1} D
* @param {mcl.G1} E
* @param {Buffer} F - length l0+l1=l
* @param {mcl.Fr} s
* @returns {{h4pk2: mcl.Fr, status: string, base: *}}
*/
static pubVerify([pk1, pk2], [D, E, F, s]) {
const h4pk2 = PRE.h4pk2(pk2);
const base = mcl.add(mcl.mul(pk1, h4pk2), pk2);
const lhs = mcl.mul(base, s);
const rhs = mcl.add(D, mcl.mul(E, PRE.H3(D, E, F)));
return {
status: lhs.isEqual(rhs) ? PRE.STATUS.VALID : PRE.STATUS.ERR_PUB_VERIFY,
h4pk2,
base
};
}
/**
* verify in dec1
* @param {mcl.G1} base - pk1^H4(pk2)*pk2
* @param {mcl.G1} E
* @param {Buffer} m - length L0
* @param {Buffer} w - length L1
* @returns {string}
*/
static dec1Verify(base, E, m, w) {
return E.isEqual(mcl.mul(base, PRE.H1(m, w))) ?
PRE.STATUS.VALID : PRE.STATUS.ERR_DEC1_VERIFY
}
/**
* verify in dec2
* @param {mcl.G1} pk2 - public key part 2
* @param {mcl.G1} E1
* @param {mcl.G1} V
* @param {Buffer} h - length L0
* @param {Buffer} pi - length L1
* @param {Buffer} m - length L0
* @param {Buffer} w - length L1
* @returns {PRE.STATUS}
*/
static dec2Verify(pk2, E1, V, h, pi, m, w) {
const isValid =
V.isEqual(mcl.mul(pk2, PRE.H1(h, pi))) &&
E1.isEqual(mcl.mul(this.g, mcl.mul(PRE.H1(m, w), mcl.hashToFr(h))));
return isValid ? PRE.STATUS.VALID : PRE.STATUS.ERR_DEC2_VERIFY
}
/**
* return random element in Fr
* @returns {mcl.Fr}
*/
static randomInFr() {
const r = new mcl.Fr();
r.setByCSPRNG();
return r
}
/**
* Buffer to G1 element
* @param {Buffer} buf
* @returns {mcl.G1}
*/
static bufToG(buf) {
const point = new mcl.G1();
point.deserialize(buf);
return point
}
/**
* Buffer to Fr element
* @param {Buffer} buf
* @returns {mcl.Fr}
*/
static bufToFr(buf) {
const point = new mcl.Fr();
point.deserialize(buf);
return point
}
/**
* point/Fr to buffer
* @param {mcl.G1|mcl.Fr} point
* @returns {Buffer}
*/
static toBuf(point) {
return Buffer.from(point.serialize())
}
/**
* return pk From sk
* @param sk1 secret key part 1
* @param sk2 secret key part 2
* @returns {mcl.G1[]}
*/
static pkFromSk([sk1, sk2]) {
const pk1 = mcl.mul(PRE.g, sk1);
const pk2 = mcl.mul(PRE.g, sk2);
return [pk1, pk2]
}
/**
* key to Buffer
* @param {mcl.G1[]|mcl.Fr[]} key
* @returns {Buffer}
*/
static keyToBuf(key) {
return Buffer.concat([
Buffer.from(key[0].serialize()),
Buffer.from(key[1].serialize())
])
}
/**
* pk to Fr
* @param {string|Buffer|Array} pk
* @returns {mcl.G1[]}
*/
static parsePk(pk) {
let buf = undefined;
if (typeof pk === "string")
buf = Buffer.from(pk, 'hex');
else if (Buffer.isBuffer(pk))
buf = pk;
if (buf === undefined)
return pk;
const pk1 = PRE.bufToG(buf.slice(0, PRE.L_G));
const pk2 = PRE.bufToG(buf.slice(PRE.L_G));
return [pk1, pk2];
}
/**
* sk to Fr
* @param {string|Buffer|Array} sk
* @returns {mcl.Fr[]}
*/
static parseSk(sk) {
let buf = undefined;
if (typeof sk === "string")
buf = Buffer.from(sk, 'hex');
else if (Buffer.isBuffer(sk))
buf = sk;
if (buf === undefined)
return sk;
const sk1 = PRE.bufToFr(buf.slice(0, PRE.L_Fr));
const sk2 = PRE.bufToFr(buf.slice(PRE.L_Fr));
return [sk1, sk2];
}
}
class PREClient {
constructor() {
this.pk = null;
this.sk = null;
}
keyGen() {
[this.sk, this.pk] = PRE.keyGen();
return [this.sk, this.pk]
}
loadKey(sk) {
this.sk = PRE.parseSk(sk);
this.pk = PRE.pkFromSk(this.sk);
}
getSk() {
return PRE.keyToBuf(this.sk)
}
getPk() {
return PRE.keyToBuf(this.pk)
}
reKeyGen(to) {
to = PRE.parsePk(to);
return PRE.reKeyGen(this.sk, this.pk, to)
}
enc(M, {to = this.pk, transformable = true} = {}) {
to = PRE.parsePk(to);
if (transformable)
return PRE.enc1(M, to);
else
return PRE.enc2(M, to);
}
dec(C) {
return C.length === PRE.C1_LEN ?
PRE.dec1(C, this.sk, this.pk) :
PRE.dec2(C, this.sk, this.pk)
}
sign(M) {
return PRE.sign(M, this.sk, this.pk)
}
static verify(M, sig, from) {
from = PRE.parsePk(from);
return PRE.verify(M, sig, from)
}
}
class PREProxy {
static reEnc(C1, reKey, owner) {
owner = PRE.parsePk(owner);
return PRE.reEnc(reKey, C1, owner)
}
}
module.exports = {PRE, PREClient, PREProxy};