-
Notifications
You must be signed in to change notification settings - Fork 4
/
Enq.js
120 lines (103 loc) · 3.39 KB
/
Enq.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
/**
* Node Trinity source code
* See LICENCE file at the top of the source tree
*
* ******************************************
*
* Enq.js
* Enecuum crypto library wrapper
*
* ******************************************
*
* Authors: K. Zhidanov, A. Prudanov, M. Vasil'ev
*/
//let addon = require('./src/Build/Release/addon');
let addon = require('./node_modules/enecuum-crypto/addon');
var crypto = require('crypto');
module.exports = addon;
//module.exports.hashToPoint = hashToPoint;
module.exports.createPK = createPK;
module.exports.getHash = getHash;
module.exports.toPoint = toPoint;
function toPoint(hash, G, curve){
//let slice = hash.slice(0, 5);
let r = addon.BigNumber(hash);
let H = addon.mul(r, G, curve);
return H;
}
function createPK(pkey, G, curve){
//let slice = pkey.slice(0, 5);
let r = addon.BigNumber(pkey);
let Q = addon.mul(r, G, curve);
return Q;
}
function getHash(str){
return crypto.createHash('sha256').update(str).digest('hex');
}
module.exports.keySharing = function (coalition, ids, Q, msk, curve){
var q = addon.BigNumber(13);
var shares = addon.shamir(msk, ids, 3, 2, q);
// r = hash
var proj = addon.keyProj(coalition, shares, Q, curve);
return proj;
}
module.exports.sign = function (M, leadID, G, G0, secret, curve){
let H_hash = getHash(M.toString() + leadID.toString());
var H = toPoint(parseInt(H_hash.slice(0, 5), 16), G, curve);
let isInfinity = 0;
var q = addon.BigNumber(13);
do {
var r2 = addon.getRandom(q);
var s1 = addon.mul(r2, G0, curve);
// S2 = r*H + SecKey
var s2 = addon.mul(r2, H, curve);
s2 = addon.addPoints(s2, secret, curve);
isInfinity = s2.isInfinity(curve)
} while(isInfinity)
return {
r : {
x : s1.x(curve),
y : s1.y(curve)
},
s : {
x : s2.x(curve),
y : s2.y(curve)
}
};
}
module.exports.sign_tate = function (M, leadID, G0, secret, curve, ecurve){
//console.log("hash = " + getHash(M.toString() + leadID.toString()));
var h = addon.BigNumber(getHash(M.toString() + leadID.toString()));
let res = addon.signTate(h, secret, G0, curve, ecurve);
return res;
}
module.exports.verify = function (sign, M, PK_LPoS, G, G0, MPK, leadID, p, curve){
var sx = addon.BigNumber(0);
var sy = addon.BigNumber(522);
var S = addon.Point(sx, sy, curve);
let Q = toPoint(parseInt(PK_LPoS.slice(0, 5), 16), G, curve);
let H_hash = getHash(M.toString() + leadID.toString());
var H = toPoint(parseInt(H_hash.slice(0, 5), 16), G, curve);
var s1 = addon.Point(addon.BigNumber(parseInt(sign.r.x)), addon.BigNumber(parseInt(sign.r.y)), curve);
var s2 = addon.Point(addon.BigNumber(parseInt(sign.s.x)), addon.BigNumber(parseInt(sign.s.y)), curve);
var r1 = addon.weilPairing(G0, s2, S, curve);
//console.log("r1 = e(P, S):\t" + r1.value());
var b1 = addon.weilPairing(MPK, Q, S, curve);
//console.log("b1 = e(MPK, Q):\t" + b1.value());
var c1 = addon.weilPairing(s1, H, S, curve);
//console.log("c1 = e(R, H1):\t" + c1.value());
var b1c1 = addon.mmul(b1, c1, p);
//console.log("r1 = b1 * c1:\t" + b1c1.value());
if(r1.value() == b1c1.value())
return 1;
else
return 0;
}
module.exports.verify_tate = function (sign, M, PK_LPoS, G0, MPK, leadID, curve, ecurve ){
var h = addon.BigNumber(getHash(M.toString() + leadID.toString()));
PK_LPoS = addon.BigNumber(PK_LPoS);
let Q = addon.getQ(PK_LPoS, curve, ecurve);
//console.log("Qa: " + Q.xy(curve));
let res = addon.verifyTate(sign, h, Q, G0, MPK, curve, ecurve);
return res;
}