-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECDSA.js
226 lines (198 loc) · 7.93 KB
/
ECDSA.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
/* ECDSA Cryptography */
/* All elements of array must be in value 0-255 */
/**
* Requirements:
* ECMAScript 2015
*/
let string = require('./util/string')
let utils = require('./util/utils')
let keccak = require('./Keccak')
let ec = require('./EllipticCurve')
const byteReduction = 0n // Biar ga lama-lama saat membuat key
const binReduction = 1n
module.exports = {
ECDSA: class {
/**
*
* @param {BigInt} a
* @param {BigInt} b
* @param {BigInt} p
* @param {Array} basePointNum
* @param {BigInt} n
*/
constructor(a, b, p, basePointNum, n) {
// DEFAULT NIST-192
if (a === undefined || b === undefined || p === undefined) {
let p = BigInt(6277101735386680763835789423207666416083908700390324961279n)
let n = BigInt(6277101735386680763835789423176059013767194773182842284081n)
let a = BigInt(-3n)
let b = BigInt(0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1)
let g = [BigInt(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012),
BigInt(0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811)]
this.curve = new ec.EllipticCurve(a, b, p, g)
this.curve.setOrderExplicit(n)
}
// BASE POINT NUM EXPLICIT
else if (isNaN(basePointNum)) {
this.curve = new ec.EllipticCurve(a, b, p, basePointNum)
if (n === undefined) {
this.curve.setOrder();
} else {
this.curve.setOrderExplicit(n)
}
}
// ONLY A, B, P, and NUMBER G
else {
this.curve = new ec.EllipticCurve(a, b, p)
this.curve.setBasePointNumber(basePointNum)
}
}
setKeyRandom() {
let binCount = this.curve.binSize - binReduction
this.privateKey = utils.getRandomIntRange(binCount, 2n ** binCount + 2n, (this.curve.n - 2n))
this.privateKeyHex = this.privateKey.toString(16)
this.publicKey = this.curve.multiplyGraphPoint(this.curve.base, this.privateKey)
this.publicKeyHex = this.publicKey[0].toString(16) + ' ' + this.publicKey[1].toString(16)
}
/**
*
* @param {BigInt} privateKey
*/
setPrivateKey(privateKey) {
this.privateKey = privateKey
this.privateKeyHex = this.privateKey.toString(16)
}
/**
*
* @param {Array} publicKey
*/
setPublicKey(publicKey) {
this.publicKey = publicKey
this.publicKeyHex = this.publicKey[0].toString(16) + ' ' + this.publicKey[1].toString(16)
}
/**
*
* @param {String} privateKeyHex
*/
setPrivatekeyHex(privateKeyHex) {
this.privateKeyHex = privateKeyHex
this.privateKey = BigInt('0x' + privateKeyHex)
}
/**
*
* @param {String} publicKeyHex
*/
setPublicKeyHex(publicKeyHex) {
this.publicKeyHex = publicKeyHex
let splittedPublic = publicKeyHex.split(" ")
this.publicKey = [BigInt('0x' + splittedPublic[0]), BigInt('0x' + splittedPublic[1])]
}
setKeyHex(privateKeyHex) {
this.privateKeyHex = privateKeyHex
this.privateKey = BigInt('0x' + privateKeyHex)
this.publicKey = this.curve.multiplyGraphPoint(this.curve.base, this.privateKey)
this.publicKeyHex = this.publicKey[0].toString(16) + ' ' + this.publicKey[1].toString(16)
}
initiateK(privateKey) {
let binCount = this.curve.binSize - binReduction
let k = utils.getRandomIntRange(binCount, 2n ** binCount, privateKey)
while (k >= privateKey) {
k = utils.getRandomIntRange(binCount, 2n ** binCount, privateKey)
}
let qa = this.curve.multiplyGraphPoint(this.curve.base, k)
return [k, qa]
}
validatePublicKey(publicKey) {
if (publicKey[0] === -1 && publicKey[1] === -1) {
return false;
}
let mulPub = this.curve.multiplyGraphPoint(publicKey, this.curve.N)
if (mulPub[0] === -1 && mulPub[1] === -1) {
return false;
}
return true;
}
sign(message, privateKey, hexedKey = false, hexedOutput = false) {
let s = 0n
let r = 0n
if (hexedKey) {
privateKey = BigInt('0x' + privateKey)
}
while (s === 0n) {
// STEP 1 & 2: count k and qa
let initiate = this.initiateK(privateKey)
let k = initiate[0]
let qa = initiate[1]
// STEP 3: count r, if 0, repeat
r = utils.mod(qa[0], this.curve.n)
while (r === 0n) {
initiate = this.initiateK()
k = initiate[0]
qa = initiate[1]
r = utils.mod(qa[0], this.curve.n)
}
// STEP 4: count k inverse mod n
let invK = utils.modInverse(k, this.curve.n)
if (!isNaN(Number(invK))) {
// STEP 5: hash and count hash value
let hashed = keccak.hash(message, Number(this.curve.binSize / 8n))
let e = BigInt(utils.strToHex(hashed))
let sumEDR = utils.mod(e + privateKey * r, this.curve.n)
// STEP 6: count s, if 0, repeat
s = utils.mod((invK * sumEDR), this.curve.n)
if (isNaN(Number(utils.modInverse(s, this.curve.n)))) {
s = 0n
} else {
// console.log('k =', k, '; qa =', qa, 'e =', e, 'sumEDR =', sumEDR, 'invK =', invK)
}
}
}
if (!hexedOutput) {
return [r, s]
} else {
return (r.toString(16) + '|' + s.toString(16))
}
}
verify(message, signature, publicKey, hexedKey = false, hexedSign = false) {
let r = 0
let s = 0
if (hexedKey) {
let splitted = publicKey.split(" ")
publicKey = [BigInt('0x' + splitted[0]), BigInt('0x' + splitted[1])]
}
if (!hexedSign) {
r = signature[0]
s = signature[1]
} else {
let splitted = signature.split("|")
console.log('Signature:', signature)
console.log('Splitted signature public key:', splitted)
r = BigInt('0x' + splitted[0])
s = BigInt('0x' + splitted[1])
}
// STEP 1: VERIFY range of r and s
if (r >= this.curve.n || r <= 0 || s >= this.curve.n || s <= 0) {
return false
}
// STEP 2: hash and count hash value
let hashed = keccak.hash(message, Number(this.curve.binSize / 8n))
let e = BigInt(utils.strToHex(hashed))
// STEP 3: calculate w
let w = utils.modInverse(s, this.curve.n)
// STEP 4: find u1 and u2
let u1 = utils.mod((e * w), this.curve.n)
let u2 = utils.mod((r * w), this.curve.n)
// STEP 5: calculate X
let u1g = this.curve.multiplyGraphPoint(this.curve.base, u1)
let u2q = this.curve.multiplyGraphPoint(publicKey, u2)
let x = this.curve.sumGraphPoint(u1g, u2q)
// STEP 6: Validate X not infinite
if (x[0] === -1 || x[1] === -1) {
return false
}
let x1 = x[0]
let v = utils.mod(x1, this.curve.n)
return (v === r)
}
}
}