-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecp256k1.js
547 lines (538 loc) · 17.1 KB
/
secp256k1.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
"use strict";
/*! noble-secp256k1 - MIT License (c) Paul Miller (paulmillr.com) */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPublicKey = exports.Point = exports.CURVE = void 0;
const CURVE = {
a: 0n,
b: 7n,
P: 2n ** 256n - 2n ** 32n - 977n,
n: 2n ** 256n - 432420386565659656852420866394968145599n,
h: 1n,
Gx: 55066263022277343669578718895168534326250603453777594175500187360389116729240n,
Gy: 32670510020758816978083085130507043184471273380659243275938904335757337482424n,
beta: 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501een,
};
exports.CURVE = CURVE;
function weistrass(x) {
const { a, b } = CURVE;
return mod(x ** 3n + a * x + b);
}
const USE_ENDOMORPHISM = CURVE.a === 0n;
class JacobianPoint {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
static fromAffine(p) {
if (!(p instanceof Point)) {
throw new TypeError('JacobianPoint#fromAffine: expected Point');
}
return new JacobianPoint(p.x, p.y, 1n);
}
static toAffineBatch(points) {
const toInv = invertBatch(points.map((p) => p.z));
return points.map((p, i) => p.toAffine(toInv[i]));
}
static normalizeZ(points) {
return JacobianPoint.toAffineBatch(points).map(JacobianPoint.fromAffine);
}
equals(other) {
const a = this;
const b = other;
const az2 = mod(a.z * a.z);
const az3 = mod(a.z * az2);
const bz2 = mod(b.z * b.z);
const bz3 = mod(b.z * bz2);
return mod(a.x * bz2) === mod(az2 * b.x) && mod(a.y * bz3) === mod(az3 * b.y);
}
negate() {
return new JacobianPoint(this.x, mod(-this.y), this.z);
}
double() {
const X1 = this.x;
const Y1 = this.y;
const Z1 = this.z;
const A = X1 ** 2n;
const B = Y1 ** 2n;
const C = B ** 2n;
const D = 2n * ((X1 + B) ** 2n - A - C);
const E = 3n * A;
const F = E ** 2n;
const X3 = mod(F - 2n * D);
const Y3 = mod(E * (D - X3) - 8n * C);
const Z3 = mod(2n * Y1 * Z1);
return new JacobianPoint(X3, Y3, Z3);
}
add(other) {
if (!(other instanceof JacobianPoint)) {
throw new TypeError('JacobianPoint#add: expected JacobianPoint');
}
const X1 = this.x;
const Y1 = this.y;
const Z1 = this.z;
const X2 = other.x;
const Y2 = other.y;
const Z2 = other.z;
if (X2 === 0n || Y2 === 0n)
return this;
if (X1 === 0n || Y1 === 0n)
return other;
const Z1Z1 = Z1 ** 2n;
const Z2Z2 = Z2 ** 2n;
const U1 = X1 * Z2Z2;
const U2 = X2 * Z1Z1;
const S1 = Y1 * Z2 * Z2Z2;
const S2 = Y2 * Z1 * Z1Z1;
const H = mod(U2 - U1);
const r = mod(S2 - S1);
if (H === 0n) {
if (r === 0n) {
return this.double();
}
else {
return JacobianPoint.ZERO;
}
}
const HH = mod(H ** 2n);
const HHH = mod(H * HH);
const V = U1 * HH;
const X3 = mod(r ** 2n - HHH - 2n * V);
const Y3 = mod(r * (V - X3) - S1 * HHH);
const Z3 = mod(Z1 * Z2 * H);
return new JacobianPoint(X3, Y3, Z3);
}
subtract(other) {
return this.add(other.negate());
}
multiplyUnsafe(scalar) {
if (!isValidScalar(scalar))
throw new TypeError('Point#multiply: expected valid scalar');
let n = mod(BigInt(scalar), CURVE.n);
if (!USE_ENDOMORPHISM) {
let p = JacobianPoint.ZERO;
let d = this;
while (n > 0n) {
if (n & 1n)
p = p.add(d);
d = d.double();
n >>= 1n;
}
return p;
}
let [k1neg, k1, k2neg, k2] = splitScalarEndo(n);
let k1p = JacobianPoint.ZERO;
let k2p = JacobianPoint.ZERO;
let d = this;
while (k1 > 0n || k2 > 0n) {
if (k1 & 1n)
k1p = k1p.add(d);
if (k2 & 1n)
k2p = k2p.add(d);
d = d.double();
k1 >>= 1n;
k2 >>= 1n;
}
if (k1neg)
k1p = k1p.negate();
if (k2neg)
k2p = k2p.negate();
k2p = new JacobianPoint(mod(k2p.x * CURVE.beta), k2p.y, k2p.z);
return k1p.add(k2p);
}
precomputeWindow(W) {
const windows = USE_ENDOMORPHISM ? 128 / W + 1 : 256 / W + 1;
let points = [];
let p = this;
let base = p;
for (let window = 0; window < windows; window++) {
base = p;
points.push(base);
for (let i = 1; i < 2 ** (W - 1); i++) {
base = base.add(p);
points.push(base);
}
p = base.double();
}
return points;
}
wNAF(n, affinePoint) {
if (!affinePoint && this.equals(JacobianPoint.BASE))
affinePoint = Point.BASE;
const W = (affinePoint && affinePoint._WINDOW_SIZE) || 1;
if (256 % W) {
throw new Error('Point#wNAF: Invalid precomputation window, must be power of 2');
}
let precomputes = affinePoint && pointPrecomputes.get(affinePoint);
if (!precomputes) {
precomputes = this.precomputeWindow(W);
if (affinePoint && W !== 1) {
precomputes = JacobianPoint.normalizeZ(precomputes);
pointPrecomputes.set(affinePoint, precomputes);
}
}
let p = JacobianPoint.ZERO;
let f = JacobianPoint.ZERO;
const windows = USE_ENDOMORPHISM ? 128 / W + 1 : 256 / W + 1;
const windowSize = 2 ** (W - 1);
const mask = BigInt(2 ** W - 1);
const maxNumber = 2 ** W;
const shiftBy = BigInt(W);
for (let window = 0; window < windows; window++) {
const offset = window * windowSize;
let wbits = Number(n & mask);
n >>= shiftBy;
if (wbits > windowSize) {
wbits -= maxNumber;
n += 1n;
}
if (wbits === 0) {
f = f.add(window % 2 ? precomputes[offset].negate() : precomputes[offset]);
}
else {
const cached = precomputes[offset + Math.abs(wbits) - 1];
p = p.add(wbits < 0 ? cached.negate() : cached);
}
}
return [p, f];
}
multiply(scalar, affinePoint) {
if (!isValidScalar(scalar))
throw new TypeError('Point#multiply: expected valid scalar');
let n = mod(BigInt(scalar), CURVE.n);
let point;
let fake;
if (USE_ENDOMORPHISM) {
const [k1neg, k1, k2neg, k2] = splitScalarEndo(n);
let k1p, k2p, f1p, f2p;
[k1p, f1p] = this.wNAF(k1, affinePoint);
[k2p, f2p] = this.wNAF(k2, affinePoint);
if (k1neg)
k1p = k1p.negate();
if (k2neg)
k2p = k2p.negate();
k2p = new JacobianPoint(mod(k2p.x * CURVE.beta), k2p.y, k2p.z);
[point, fake] = [k1p.add(k2p), f1p.add(f2p)];
}
else {
[point, fake] = this.wNAF(n, affinePoint);
}
return JacobianPoint.normalizeZ([point, fake])[0];
}
toAffine(invZ = invert(this.z)) {
const invZ2 = invZ ** 2n;
const x = mod(this.x * invZ2);
const y = mod(this.y * invZ2 * invZ);
return new Point(x, y);
}
}
JacobianPoint.BASE = new JacobianPoint(CURVE.Gx, CURVE.Gy, 1n);
JacobianPoint.ZERO = new JacobianPoint(0n, 1n, 0n);
const pointPrecomputes = new WeakMap();
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
_setWindowSize(windowSize) {
this._WINDOW_SIZE = windowSize;
pointPrecomputes.delete(this);
}
static fromCompressedHex(bytes) {
const isShort = bytes.length === 32;
const x = bytesToNumber(isShort ? bytes : bytes.slice(1));
const y2 = weistrass(x);
let y = sqrtMod(y2);
const isYOdd = (y & 1n) === 1n;
if (isShort) {
if (isYOdd)
y = mod(-y);
}
else {
const isFirstByteOdd = (bytes[0] & 1) === 1;
if (isFirstByteOdd !== isYOdd)
y = mod(-y);
}
const point = new Point(x, y);
point.assertValidity();
return point;
}
static fromUncompressedHex(bytes) {
const x = bytesToNumber(bytes.slice(1, 33));
const y = bytesToNumber(bytes.slice(33));
const point = new Point(x, y);
point.assertValidity();
return point;
}
static fromHex(hex) {
const bytes = ensureBytes(hex);
const header = bytes[0];
if (bytes.length === 32 || (bytes.length === 33 && (header === 0x02 || header === 0x03))) {
return this.fromCompressedHex(bytes);
}
if (bytes.length === 65 && header === 0x04)
return this.fromUncompressedHex(bytes);
throw new Error(`Point.fromHex: received invalid point. Expected 32-33 compressed bytes or 65 uncompressed bytes, not ${bytes.length}`);
}
static fromPrivateKey(privateKey) {
return Point.BASE.multiply(normalizePrivateKey(privateKey));
}
static fromSignature(msgHash, signature, recovery) {
let h = msgHash instanceof Uint8Array ? bytesToNumber(msgHash) : hexToNumber(msgHash);
const sig = normalizeSignature(signature);
sig.assertValidity();
const { r, s } = sig;
if (recovery !== 0 && recovery !== 1) {
throw new Error('Cannot recover signature: invalid yParity bit');
}
const prefix = 2 + (recovery & 1);
const P_ = Point.fromHex(`0${prefix}${pad64(r)}`);
const sP = JacobianPoint.fromAffine(P_).multiplyUnsafe(s);
const hG = JacobianPoint.BASE.multiply(h);
const rinv = invert(r, CURVE.n);
const Q = sP.subtract(hG).multiplyUnsafe(rinv);
const point = Q.toAffine();
point.assertValidity();
return point;
}
toRawBytes(isCompressed = false) {
return hexToBytes(this.toHex(isCompressed));
}
toHex(isCompressed = false) {
const x = pad64(this.x);
if (isCompressed) {
return `${this.y & 1n ? '03' : '02'}${x}`;
}
else {
return `04${x}${pad64(this.y)}`;
}
}
toHexX() {
return this.toHex(true).slice(2);
}
toRawX() {
return this.toRawBytes(true).slice(1);
}
assertValidity() {
const msg = 'Point is not on elliptic curve';
const { P } = CURVE;
const { x, y } = this;
if (x === 0n || y === 0n || x >= P || y >= P)
throw new Error(msg);
const left = mod(y * y);
const right = weistrass(x);
if ((left - right) % P !== 0n)
throw new Error(msg);
}
equals(other) {
return this.x === other.x && this.y === other.y;
}
negate() {
return new Point(this.x, mod(-this.y));
}
double() {
return JacobianPoint.fromAffine(this).double().toAffine();
}
add(other) {
return JacobianPoint.fromAffine(this).add(JacobianPoint.fromAffine(other)).toAffine();
}
subtract(other) {
return this.add(other.negate());
}
multiply(scalar) {
return JacobianPoint.fromAffine(this).multiply(scalar, this).toAffine();
}
}
exports.Point = Point;
Point.BASE = new Point(CURVE.Gx, CURVE.Gy);
Point.ZERO = new Point(0n, 0n);
function sliceDer(s) {
return Number.parseInt(s[0], 16) >= 8 ? '00' + s : s;
}
function bytesToHex(uint8a) {
let hex = '';
for (let i = 0; i < uint8a.length; i++) {
hex += uint8a[i].toString(16).padStart(2, '0');
}
return hex;
}
function pad64(num) {
return num.toString(16).padStart(64, '0');
}
function pad32b(num) {
return hexToBytes(pad64(num));
}
function numberToHex(num) {
const hex = num.toString(16);
return hex.length & 1 ? `0${hex}` : hex;
}
function hexToNumber(hex) {
if (typeof hex !== 'string') {
throw new TypeError('hexToNumber: expected string, got ' + typeof hex);
}
return BigInt(`0x${hex}`);
}
function hexToBytes(hex) {
if (typeof hex !== 'string') {
throw new TypeError('hexToBytes: expected string, got ' + typeof hex);
}
if (hex.length % 2)
throw new Error('hexToBytes: received invalid unpadded hex');
const array = new Uint8Array(hex.length / 2);
for (let i = 0; i < array.length; i++) {
const j = i * 2;
array[i] = Number.parseInt(hex.slice(j, j + 2), 16);
}
return array;
}
function ensureBytes(hex) {
return hex instanceof Uint8Array ? hex : hexToBytes(hex);
}
function bytesToNumber(bytes) {
return hexToNumber(bytesToHex(bytes));
}
function parseByte(str) {
return Number.parseInt(str, 16) * 2;
}
function isValidScalar(num) {
if (typeof num === 'bigint' && num > 0n)
return true;
if (typeof num === 'number' && num > 0 && Number.isSafeInteger(num))
return true;
return false;
}
function mod(a, b = CURVE.P) {
const result = a % b;
return result >= 0 ? result : b + result;
}
function pow2(x, power) {
const { P } = CURVE;
let res = x;
while (power-- > 0n) {
res *= res;
res %= P;
}
return res;
}
function sqrtMod(x) {
const { P } = CURVE;
const b2 = (x * x * x) % P;
const b3 = (b2 * b2 * x) % P;
const b6 = (pow2(b3, 3n) * b3) % P;
const b9 = (pow2(b6, 3n) * b3) % P;
const b11 = (pow2(b9, 2n) * b2) % P;
const b22 = (pow2(b11, 11n) * b11) % P;
const b44 = (pow2(b22, 22n) * b22) % P;
const b88 = (pow2(b44, 44n) * b44) % P;
const b176 = (pow2(b88, 88n) * b88) % P;
const b220 = (pow2(b176, 44n) * b44) % P;
const b223 = (pow2(b220, 3n) * b3) % P;
const t1 = (pow2(b223, 23n) * b22) % P;
const t2 = (pow2(t1, 6n) * b2) % P;
return pow2(t2, 2n);
}
function invert(number, modulo = CURVE.P) {
if (number === 0n || modulo <= 0n) {
throw new Error(`invert: expected positive integers, got n=${number} mod=${modulo}`);
}
let a = mod(number, modulo);
let b = modulo;
let [x, y, u, v] = [0n, 1n, 1n, 0n];
while (a !== 0n) {
const q = b / a;
const r = b % a;
const m = x - u * q;
const n = y - v * q;
[b, a] = [a, r];
[x, y] = [u, v];
[u, v] = [m, n];
}
const gcd = b;
if (gcd !== 1n)
throw new Error('invert: does not exist');
return mod(x, modulo);
}
function invertBatch(nums, n = CURVE.P) {
const len = nums.length;
const scratch = new Array(len);
let acc = 1n;
for (let i = 0; i < len; i++) {
if (nums[i] === 0n)
continue;
scratch[i] = acc;
acc = mod(acc * nums[i], n);
}
acc = invert(acc, n);
for (let i = len - 1; i >= 0; i--) {
if (nums[i] === 0n)
continue;
const tmp = mod(acc * nums[i], n);
nums[i] = mod(acc * scratch[i], n);
acc = tmp;
}
return nums;
}
const divNearest = (a, b) => (a + b / 2n) / b;
const POW_2_128 = 2n ** 128n;
function splitScalarEndo(k) {
const { n } = CURVE;
const a1 = 0x3086d221a7d46bcde86c90e49284eb15n;
const b1 = -0xe4437ed6010e88286f547fa90abfe4c3n;
const a2 = 0x114ca50f7a8e2f3f657c1108d9d44cfd8n;
const b2 = a1;
const c1 = divNearest(b2 * k, n);
const c2 = divNearest(-b1 * k, n);
let k1 = mod(k - c1 * a1 - c2 * a2, n);
let k2 = mod(-c1 * b1 - c2 * b2, n);
const k1neg = k1 > POW_2_128;
const k2neg = k2 > POW_2_128;
if (k1neg)
k1 = n - k1;
if (k2neg)
k2 = n - k2;
if (k1 > POW_2_128 || k2 > POW_2_128)
throw new Error('splitScalarEndo: Endomorphism failed');
return [k1neg, k1, k2neg, k2];
}
function isWithinCurveOrder(num) {
return 0 < num && num < CURVE.n;
}
function normalizePrivateKey(key) {
let num;
if (typeof key === 'bigint') {
num = key;
}
else if (typeof key === 'number' && Number.isSafeInteger(key) && key > 0) {
num = BigInt(key);
}
else if (typeof key === 'string') {
if (key.length !== 64)
throw new Error('Expected 32 bytes of private key');
num = hexToNumber(key);
}
else if (key instanceof Uint8Array) {
if (key.length !== 32)
throw new Error('Expected 32 bytes of private key');
num = bytesToNumber(key);
}
else {
throw new TypeError('Expected valid private key');
}
if (!isWithinCurveOrder(num))
throw new Error('Expected private key: 0 < key < n');
return num;
}
function normalizePublicKey(publicKey) {
return publicKey instanceof Point ? publicKey : Point.fromHex(publicKey);
}
function normalizeSignature(signature) {
return signature instanceof Signature ? signature : Signature.fromHex(signature);
}
function getPublicKey(privateKey, isCompressed = false) {
const point = Point.fromPrivateKey(privateKey);
if (typeof privateKey === 'string') {
return point.toHex(isCompressed);
}
return point.toRawBytes(isCompressed);
}
exports.getPublicKey = getPublicKey;