@zoralabs/protocol-deployments 0.6.4-PRE.0 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$js.log +50 -53
- package/CHANGELOG.md +5 -2
- package/LICENSE +21 -0
- package/dist/{_esm-OJM6GREZ.js → _esm-AN6HMJZC.js} +131 -138
- package/dist/_esm-AN6HMJZC.js.map +1 -0
- package/dist/{ccip-4Y6T462U.js → ccip-3JNBGYOD.js} +2 -3
- package/dist/{chunk-KMBLRPCX.js → chunk-4DQDZNUA.js} +577 -980
- package/dist/chunk-4DQDZNUA.js.map +1 -0
- package/dist/index.cjs +180 -168
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/dist/{secp256k1-XP7IUONI.js → secp256k1-QZA5SALG.js} +746 -438
- package/dist/secp256k1-QZA5SALG.js.map +1 -0
- package/package.json +12 -12
- package/dist/_esm-OJM6GREZ.js.map +0 -1
- package/dist/chunk-BYTNVMX7.js +0 -408
- package/dist/chunk-BYTNVMX7.js.map +0 -1
- package/dist/chunk-KMBLRPCX.js.map +0 -1
- package/dist/secp256k1-XP7IUONI.js.map +0 -1
- /package/dist/{ccip-4Y6T462U.js.map → ccip-3JNBGYOD.js.map} +0 -0
|
@@ -1,41 +1,99 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
abool,
|
|
5
|
-
abytes,
|
|
6
|
-
abytes2,
|
|
7
|
-
aexists,
|
|
8
|
-
ahash,
|
|
9
|
-
anumber,
|
|
10
|
-
aoutput,
|
|
11
|
-
bitLen,
|
|
12
|
-
bitMask,
|
|
13
|
-
bytesToHex,
|
|
14
|
-
bytesToNumberBE,
|
|
15
|
-
bytesToNumberLE,
|
|
16
|
-
clean,
|
|
17
|
-
concatBytes,
|
|
18
|
-
concatBytes2,
|
|
19
|
-
createHasher,
|
|
20
|
-
createHmacDrbg,
|
|
21
|
-
createView,
|
|
22
|
-
ensureBytes,
|
|
23
|
-
hexToBytes,
|
|
24
|
-
inRange,
|
|
25
|
-
isBytes,
|
|
26
|
-
memoized,
|
|
27
|
-
numberToBytesBE,
|
|
28
|
-
numberToBytesLE,
|
|
29
|
-
numberToHexUnpadded,
|
|
30
|
-
randomBytes,
|
|
31
|
-
rotr,
|
|
32
|
-
toBytes,
|
|
33
|
-
utf8ToBytes,
|
|
34
|
-
validateObject
|
|
35
|
-
} from "./chunk-BYTNVMX7.js";
|
|
36
|
-
import "./chunk-PR4QN5HX.js";
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-PR4QN5HX.js";
|
|
37
4
|
|
|
38
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.
|
|
5
|
+
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_assert.js
|
|
6
|
+
function anumber(n) {
|
|
7
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
8
|
+
throw new Error("positive integer expected, got " + n);
|
|
9
|
+
}
|
|
10
|
+
function isBytes(a) {
|
|
11
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
12
|
+
}
|
|
13
|
+
function abytes(b, ...lengths) {
|
|
14
|
+
if (!isBytes(b))
|
|
15
|
+
throw new Error("Uint8Array expected");
|
|
16
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
17
|
+
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
18
|
+
}
|
|
19
|
+
function ahash(h) {
|
|
20
|
+
if (typeof h !== "function" || typeof h.create !== "function")
|
|
21
|
+
throw new Error("Hash should be wrapped by utils.wrapConstructor");
|
|
22
|
+
anumber(h.outputLen);
|
|
23
|
+
anumber(h.blockLen);
|
|
24
|
+
}
|
|
25
|
+
function aexists(instance, checkFinished = true) {
|
|
26
|
+
if (instance.destroyed)
|
|
27
|
+
throw new Error("Hash instance has been destroyed");
|
|
28
|
+
if (checkFinished && instance.finished)
|
|
29
|
+
throw new Error("Hash#digest() has already been called");
|
|
30
|
+
}
|
|
31
|
+
function aoutput(out, instance) {
|
|
32
|
+
abytes(out);
|
|
33
|
+
const min = instance.outputLen;
|
|
34
|
+
if (out.length < min) {
|
|
35
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/cryptoNode.js
|
|
40
|
+
import * as nc from "node:crypto";
|
|
41
|
+
var crypto = nc && typeof nc === "object" && "webcrypto" in nc ? nc.webcrypto : nc && typeof nc === "object" && "randomBytes" in nc ? nc : void 0;
|
|
42
|
+
|
|
43
|
+
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/utils.js
|
|
44
|
+
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
45
|
+
var rotr = (word, shift) => word << 32 - shift | word >>> shift;
|
|
46
|
+
function utf8ToBytes(str) {
|
|
47
|
+
if (typeof str !== "string")
|
|
48
|
+
throw new Error("utf8ToBytes expected string, got " + typeof str);
|
|
49
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
50
|
+
}
|
|
51
|
+
function toBytes(data) {
|
|
52
|
+
if (typeof data === "string")
|
|
53
|
+
data = utf8ToBytes(data);
|
|
54
|
+
abytes(data);
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
57
|
+
function concatBytes(...arrays) {
|
|
58
|
+
let sum = 0;
|
|
59
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
60
|
+
const a = arrays[i];
|
|
61
|
+
abytes(a);
|
|
62
|
+
sum += a.length;
|
|
63
|
+
}
|
|
64
|
+
const res = new Uint8Array(sum);
|
|
65
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
66
|
+
const a = arrays[i];
|
|
67
|
+
res.set(a, pad);
|
|
68
|
+
pad += a.length;
|
|
69
|
+
}
|
|
70
|
+
return res;
|
|
71
|
+
}
|
|
72
|
+
var Hash = class {
|
|
73
|
+
// Safe version that clones internal state
|
|
74
|
+
clone() {
|
|
75
|
+
return this._cloneInto();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
function wrapConstructor(hashCons) {
|
|
79
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
80
|
+
const tmp = hashCons();
|
|
81
|
+
hashC.outputLen = tmp.outputLen;
|
|
82
|
+
hashC.blockLen = tmp.blockLen;
|
|
83
|
+
hashC.create = () => hashCons();
|
|
84
|
+
return hashC;
|
|
85
|
+
}
|
|
86
|
+
function randomBytes(bytesLength = 32) {
|
|
87
|
+
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
88
|
+
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
89
|
+
}
|
|
90
|
+
if (crypto && typeof crypto.randomBytes === "function") {
|
|
91
|
+
return crypto.randomBytes(bytesLength);
|
|
92
|
+
}
|
|
93
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_md.js
|
|
39
97
|
function setBigUint64(view, byteOffset, value, isLE) {
|
|
40
98
|
if (typeof view.setBigUint64 === "function")
|
|
41
99
|
return view.setBigUint64(byteOffset, value, isLE);
|
|
@@ -48,31 +106,26 @@ function setBigUint64(view, byteOffset, value, isLE) {
|
|
|
48
106
|
view.setUint32(byteOffset + h, wh, isLE);
|
|
49
107
|
view.setUint32(byteOffset + l, wl, isLE);
|
|
50
108
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
function Maj(a, b, c) {
|
|
55
|
-
return a & b ^ a & c ^ b & c;
|
|
56
|
-
}
|
|
109
|
+
var Chi = (a, b, c) => a & b ^ ~a & c;
|
|
110
|
+
var Maj = (a, b, c) => a & b ^ a & c ^ b & c;
|
|
57
111
|
var HashMD = class extends Hash {
|
|
58
112
|
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
59
113
|
super();
|
|
60
|
-
this.finished = false;
|
|
61
|
-
this.length = 0;
|
|
62
|
-
this.pos = 0;
|
|
63
|
-
this.destroyed = false;
|
|
64
114
|
this.blockLen = blockLen;
|
|
65
115
|
this.outputLen = outputLen;
|
|
66
116
|
this.padOffset = padOffset;
|
|
67
117
|
this.isLE = isLE;
|
|
118
|
+
this.finished = false;
|
|
119
|
+
this.length = 0;
|
|
120
|
+
this.pos = 0;
|
|
121
|
+
this.destroyed = false;
|
|
68
122
|
this.buffer = new Uint8Array(blockLen);
|
|
69
123
|
this.view = createView(this.buffer);
|
|
70
124
|
}
|
|
71
125
|
update(data) {
|
|
72
126
|
aexists(this);
|
|
73
|
-
data = toBytes(data);
|
|
74
|
-
abytes(data);
|
|
75
127
|
const { view, buffer, blockLen } = this;
|
|
128
|
+
data = toBytes(data);
|
|
76
129
|
const len = data.length;
|
|
77
130
|
for (let pos = 0; pos < len; ) {
|
|
78
131
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
@@ -101,7 +154,7 @@ var HashMD = class extends Hash {
|
|
|
101
154
|
const { buffer, view, blockLen, isLE } = this;
|
|
102
155
|
let { pos } = this;
|
|
103
156
|
buffer[pos++] = 128;
|
|
104
|
-
|
|
157
|
+
this.buffer.subarray(pos).fill(0);
|
|
105
158
|
if (this.padOffset > blockLen - pos) {
|
|
106
159
|
this.process(view, 0);
|
|
107
160
|
pos = 0;
|
|
@@ -132,31 +185,18 @@ var HashMD = class extends Hash {
|
|
|
132
185
|
to || (to = new this.constructor());
|
|
133
186
|
to.set(...this.get());
|
|
134
187
|
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
135
|
-
to.destroyed = destroyed;
|
|
136
|
-
to.finished = finished;
|
|
137
188
|
to.length = length;
|
|
138
189
|
to.pos = pos;
|
|
190
|
+
to.finished = finished;
|
|
191
|
+
to.destroyed = destroyed;
|
|
139
192
|
if (length % blockLen)
|
|
140
193
|
to.buffer.set(buffer);
|
|
141
194
|
return to;
|
|
142
195
|
}
|
|
143
|
-
clone() {
|
|
144
|
-
return this._cloneInto();
|
|
145
|
-
}
|
|
146
196
|
};
|
|
147
|
-
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
148
|
-
1779033703,
|
|
149
|
-
3144134277,
|
|
150
|
-
1013904242,
|
|
151
|
-
2773480762,
|
|
152
|
-
1359893119,
|
|
153
|
-
2600822924,
|
|
154
|
-
528734635,
|
|
155
|
-
1541459225
|
|
156
|
-
]);
|
|
157
197
|
|
|
158
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.
|
|
159
|
-
var SHA256_K = /* @__PURE__ */ Uint32Array
|
|
198
|
+
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/sha256.js
|
|
199
|
+
var SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
160
200
|
1116352408,
|
|
161
201
|
1899447441,
|
|
162
202
|
3049323471,
|
|
@@ -222,10 +262,20 @@ var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
|
222
262
|
3204031479,
|
|
223
263
|
3329325298
|
|
224
264
|
]);
|
|
265
|
+
var SHA256_IV = /* @__PURE__ */ new Uint32Array([
|
|
266
|
+
1779033703,
|
|
267
|
+
3144134277,
|
|
268
|
+
1013904242,
|
|
269
|
+
2773480762,
|
|
270
|
+
1359893119,
|
|
271
|
+
2600822924,
|
|
272
|
+
528734635,
|
|
273
|
+
1541459225
|
|
274
|
+
]);
|
|
225
275
|
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
226
276
|
var SHA256 = class extends HashMD {
|
|
227
|
-
constructor(
|
|
228
|
-
super(64,
|
|
277
|
+
constructor() {
|
|
278
|
+
super(64, 32, 8, false);
|
|
229
279
|
this.A = SHA256_IV[0] | 0;
|
|
230
280
|
this.B = SHA256_IV[1] | 0;
|
|
231
281
|
this.C = SHA256_IV[2] | 0;
|
|
@@ -286,16 +336,16 @@ var SHA256 = class extends HashMD {
|
|
|
286
336
|
this.set(A, B, C, D, E, F, G, H);
|
|
287
337
|
}
|
|
288
338
|
roundClean() {
|
|
289
|
-
|
|
339
|
+
SHA256_W.fill(0);
|
|
290
340
|
}
|
|
291
341
|
destroy() {
|
|
292
342
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
293
|
-
|
|
343
|
+
this.buffer.fill(0);
|
|
294
344
|
}
|
|
295
345
|
};
|
|
296
|
-
var sha256 = /* @__PURE__ */
|
|
346
|
+
var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
297
347
|
|
|
298
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.
|
|
348
|
+
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/hmac.js
|
|
299
349
|
var HMAC = class extends Hash {
|
|
300
350
|
constructor(hash, _key) {
|
|
301
351
|
super();
|
|
@@ -318,7 +368,7 @@ var HMAC = class extends Hash {
|
|
|
318
368
|
for (let i = 0; i < pad.length; i++)
|
|
319
369
|
pad[i] ^= 54 ^ 92;
|
|
320
370
|
this.oHash.update(pad);
|
|
321
|
-
|
|
371
|
+
pad.fill(0);
|
|
322
372
|
}
|
|
323
373
|
update(buf) {
|
|
324
374
|
aexists(this);
|
|
@@ -351,9 +401,6 @@ var HMAC = class extends Hash {
|
|
|
351
401
|
to.iHash = iHash._cloneInto(to.iHash);
|
|
352
402
|
return to;
|
|
353
403
|
}
|
|
354
|
-
clone() {
|
|
355
|
-
return this._cloneInto();
|
|
356
|
-
}
|
|
357
404
|
destroy() {
|
|
358
405
|
this.destroyed = true;
|
|
359
406
|
this.oHash.destroy();
|
|
@@ -363,35 +410,321 @@ var HMAC = class extends Hash {
|
|
|
363
410
|
var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
|
364
411
|
hmac.create = (hash, key) => new HMAC(hash, key);
|
|
365
412
|
|
|
366
|
-
// ../../node_modules/.pnpm/@noble+curves@1.
|
|
367
|
-
var
|
|
368
|
-
|
|
413
|
+
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/utils.js
|
|
414
|
+
var utils_exports = {};
|
|
415
|
+
__export(utils_exports, {
|
|
416
|
+
aInRange: () => aInRange,
|
|
417
|
+
abool: () => abool,
|
|
418
|
+
abytes: () => abytes2,
|
|
419
|
+
bitGet: () => bitGet,
|
|
420
|
+
bitLen: () => bitLen,
|
|
421
|
+
bitMask: () => bitMask,
|
|
422
|
+
bitSet: () => bitSet,
|
|
423
|
+
bytesToHex: () => bytesToHex,
|
|
424
|
+
bytesToNumberBE: () => bytesToNumberBE,
|
|
425
|
+
bytesToNumberLE: () => bytesToNumberLE,
|
|
426
|
+
concatBytes: () => concatBytes2,
|
|
427
|
+
createHmacDrbg: () => createHmacDrbg,
|
|
428
|
+
ensureBytes: () => ensureBytes,
|
|
429
|
+
equalBytes: () => equalBytes,
|
|
430
|
+
hexToBytes: () => hexToBytes,
|
|
431
|
+
hexToNumber: () => hexToNumber,
|
|
432
|
+
inRange: () => inRange,
|
|
433
|
+
isBytes: () => isBytes2,
|
|
434
|
+
memoized: () => memoized,
|
|
435
|
+
notImplemented: () => notImplemented,
|
|
436
|
+
numberToBytesBE: () => numberToBytesBE,
|
|
437
|
+
numberToBytesLE: () => numberToBytesLE,
|
|
438
|
+
numberToHexUnpadded: () => numberToHexUnpadded,
|
|
439
|
+
numberToVarBytesBE: () => numberToVarBytesBE,
|
|
440
|
+
utf8ToBytes: () => utf8ToBytes2,
|
|
441
|
+
validateObject: () => validateObject
|
|
442
|
+
});
|
|
443
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
444
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
369
445
|
var _2n = /* @__PURE__ */ BigInt(2);
|
|
446
|
+
function isBytes2(a) {
|
|
447
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
448
|
+
}
|
|
449
|
+
function abytes2(item) {
|
|
450
|
+
if (!isBytes2(item))
|
|
451
|
+
throw new Error("Uint8Array expected");
|
|
452
|
+
}
|
|
453
|
+
function abool(title, value) {
|
|
454
|
+
if (typeof value !== "boolean")
|
|
455
|
+
throw new Error(title + " boolean expected, got " + value);
|
|
456
|
+
}
|
|
457
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
458
|
+
function bytesToHex(bytes) {
|
|
459
|
+
abytes2(bytes);
|
|
460
|
+
let hex = "";
|
|
461
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
462
|
+
hex += hexes[bytes[i]];
|
|
463
|
+
}
|
|
464
|
+
return hex;
|
|
465
|
+
}
|
|
466
|
+
function numberToHexUnpadded(num2) {
|
|
467
|
+
const hex = num2.toString(16);
|
|
468
|
+
return hex.length & 1 ? "0" + hex : hex;
|
|
469
|
+
}
|
|
470
|
+
function hexToNumber(hex) {
|
|
471
|
+
if (typeof hex !== "string")
|
|
472
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
473
|
+
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
474
|
+
}
|
|
475
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
476
|
+
function asciiToBase16(ch) {
|
|
477
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
478
|
+
return ch - asciis._0;
|
|
479
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
480
|
+
return ch - (asciis.A - 10);
|
|
481
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
482
|
+
return ch - (asciis.a - 10);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
function hexToBytes(hex) {
|
|
486
|
+
if (typeof hex !== "string")
|
|
487
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
488
|
+
const hl = hex.length;
|
|
489
|
+
const al = hl / 2;
|
|
490
|
+
if (hl % 2)
|
|
491
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
492
|
+
const array = new Uint8Array(al);
|
|
493
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
494
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
495
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
496
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
497
|
+
const char = hex[hi] + hex[hi + 1];
|
|
498
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
499
|
+
}
|
|
500
|
+
array[ai] = n1 * 16 + n2;
|
|
501
|
+
}
|
|
502
|
+
return array;
|
|
503
|
+
}
|
|
504
|
+
function bytesToNumberBE(bytes) {
|
|
505
|
+
return hexToNumber(bytesToHex(bytes));
|
|
506
|
+
}
|
|
507
|
+
function bytesToNumberLE(bytes) {
|
|
508
|
+
abytes2(bytes);
|
|
509
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
510
|
+
}
|
|
511
|
+
function numberToBytesBE(n, len) {
|
|
512
|
+
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
513
|
+
}
|
|
514
|
+
function numberToBytesLE(n, len) {
|
|
515
|
+
return numberToBytesBE(n, len).reverse();
|
|
516
|
+
}
|
|
517
|
+
function numberToVarBytesBE(n) {
|
|
518
|
+
return hexToBytes(numberToHexUnpadded(n));
|
|
519
|
+
}
|
|
520
|
+
function ensureBytes(title, hex, expectedLength) {
|
|
521
|
+
let res;
|
|
522
|
+
if (typeof hex === "string") {
|
|
523
|
+
try {
|
|
524
|
+
res = hexToBytes(hex);
|
|
525
|
+
} catch (e) {
|
|
526
|
+
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
527
|
+
}
|
|
528
|
+
} else if (isBytes2(hex)) {
|
|
529
|
+
res = Uint8Array.from(hex);
|
|
530
|
+
} else {
|
|
531
|
+
throw new Error(title + " must be hex string or Uint8Array");
|
|
532
|
+
}
|
|
533
|
+
const len = res.length;
|
|
534
|
+
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
535
|
+
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
536
|
+
return res;
|
|
537
|
+
}
|
|
538
|
+
function concatBytes2(...arrays) {
|
|
539
|
+
let sum = 0;
|
|
540
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
541
|
+
const a = arrays[i];
|
|
542
|
+
abytes2(a);
|
|
543
|
+
sum += a.length;
|
|
544
|
+
}
|
|
545
|
+
const res = new Uint8Array(sum);
|
|
546
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
547
|
+
const a = arrays[i];
|
|
548
|
+
res.set(a, pad);
|
|
549
|
+
pad += a.length;
|
|
550
|
+
}
|
|
551
|
+
return res;
|
|
552
|
+
}
|
|
553
|
+
function equalBytes(a, b) {
|
|
554
|
+
if (a.length !== b.length)
|
|
555
|
+
return false;
|
|
556
|
+
let diff = 0;
|
|
557
|
+
for (let i = 0; i < a.length; i++)
|
|
558
|
+
diff |= a[i] ^ b[i];
|
|
559
|
+
return diff === 0;
|
|
560
|
+
}
|
|
561
|
+
function utf8ToBytes2(str) {
|
|
562
|
+
if (typeof str !== "string")
|
|
563
|
+
throw new Error("string expected");
|
|
564
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
565
|
+
}
|
|
566
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
567
|
+
function inRange(n, min, max) {
|
|
568
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
569
|
+
}
|
|
570
|
+
function aInRange(title, n, min, max) {
|
|
571
|
+
if (!inRange(n, min, max))
|
|
572
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
573
|
+
}
|
|
574
|
+
function bitLen(n) {
|
|
575
|
+
let len;
|
|
576
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
577
|
+
;
|
|
578
|
+
return len;
|
|
579
|
+
}
|
|
580
|
+
function bitGet(n, pos) {
|
|
581
|
+
return n >> BigInt(pos) & _1n;
|
|
582
|
+
}
|
|
583
|
+
function bitSet(n, pos, value) {
|
|
584
|
+
return n | (value ? _1n : _0n) << BigInt(pos);
|
|
585
|
+
}
|
|
586
|
+
var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
|
|
587
|
+
var u8n = (data) => new Uint8Array(data);
|
|
588
|
+
var u8fr = (arr) => Uint8Array.from(arr);
|
|
589
|
+
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
590
|
+
if (typeof hashLen !== "number" || hashLen < 2)
|
|
591
|
+
throw new Error("hashLen must be a number");
|
|
592
|
+
if (typeof qByteLen !== "number" || qByteLen < 2)
|
|
593
|
+
throw new Error("qByteLen must be a number");
|
|
594
|
+
if (typeof hmacFn !== "function")
|
|
595
|
+
throw new Error("hmacFn must be a function");
|
|
596
|
+
let v = u8n(hashLen);
|
|
597
|
+
let k = u8n(hashLen);
|
|
598
|
+
let i = 0;
|
|
599
|
+
const reset = () => {
|
|
600
|
+
v.fill(1);
|
|
601
|
+
k.fill(0);
|
|
602
|
+
i = 0;
|
|
603
|
+
};
|
|
604
|
+
const h = (...b) => hmacFn(k, v, ...b);
|
|
605
|
+
const reseed = (seed = u8n()) => {
|
|
606
|
+
k = h(u8fr([0]), seed);
|
|
607
|
+
v = h();
|
|
608
|
+
if (seed.length === 0)
|
|
609
|
+
return;
|
|
610
|
+
k = h(u8fr([1]), seed);
|
|
611
|
+
v = h();
|
|
612
|
+
};
|
|
613
|
+
const gen = () => {
|
|
614
|
+
if (i++ >= 1e3)
|
|
615
|
+
throw new Error("drbg: tried 1000 values");
|
|
616
|
+
let len = 0;
|
|
617
|
+
const out = [];
|
|
618
|
+
while (len < qByteLen) {
|
|
619
|
+
v = h();
|
|
620
|
+
const sl = v.slice();
|
|
621
|
+
out.push(sl);
|
|
622
|
+
len += v.length;
|
|
623
|
+
}
|
|
624
|
+
return concatBytes2(...out);
|
|
625
|
+
};
|
|
626
|
+
const genUntil = (seed, pred) => {
|
|
627
|
+
reset();
|
|
628
|
+
reseed(seed);
|
|
629
|
+
let res = void 0;
|
|
630
|
+
while (!(res = pred(gen())))
|
|
631
|
+
reseed();
|
|
632
|
+
reset();
|
|
633
|
+
return res;
|
|
634
|
+
};
|
|
635
|
+
return genUntil;
|
|
636
|
+
}
|
|
637
|
+
var validatorFns = {
|
|
638
|
+
bigint: (val) => typeof val === "bigint",
|
|
639
|
+
function: (val) => typeof val === "function",
|
|
640
|
+
boolean: (val) => typeof val === "boolean",
|
|
641
|
+
string: (val) => typeof val === "string",
|
|
642
|
+
stringOrUint8Array: (val) => typeof val === "string" || isBytes2(val),
|
|
643
|
+
isSafeInteger: (val) => Number.isSafeInteger(val),
|
|
644
|
+
array: (val) => Array.isArray(val),
|
|
645
|
+
field: (val, object) => object.Fp.isValid(val),
|
|
646
|
+
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
|
647
|
+
};
|
|
648
|
+
function validateObject(object, validators, optValidators = {}) {
|
|
649
|
+
const checkField = (fieldName, type, isOptional) => {
|
|
650
|
+
const checkVal = validatorFns[type];
|
|
651
|
+
if (typeof checkVal !== "function")
|
|
652
|
+
throw new Error("invalid validator function");
|
|
653
|
+
const val = object[fieldName];
|
|
654
|
+
if (isOptional && val === void 0)
|
|
655
|
+
return;
|
|
656
|
+
if (!checkVal(val, object)) {
|
|
657
|
+
throw new Error("param " + String(fieldName) + " is invalid. Expected " + type + ", got " + val);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
for (const [fieldName, type] of Object.entries(validators))
|
|
661
|
+
checkField(fieldName, type, false);
|
|
662
|
+
for (const [fieldName, type] of Object.entries(optValidators))
|
|
663
|
+
checkField(fieldName, type, true);
|
|
664
|
+
return object;
|
|
665
|
+
}
|
|
666
|
+
var notImplemented = () => {
|
|
667
|
+
throw new Error("not implemented");
|
|
668
|
+
};
|
|
669
|
+
function memoized(fn) {
|
|
670
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
671
|
+
return (arg, ...args) => {
|
|
672
|
+
const val = map.get(arg);
|
|
673
|
+
if (val !== void 0)
|
|
674
|
+
return val;
|
|
675
|
+
const computed = fn(arg, ...args);
|
|
676
|
+
map.set(arg, computed);
|
|
677
|
+
return computed;
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/modular.js
|
|
682
|
+
var _0n2 = BigInt(0);
|
|
683
|
+
var _1n2 = BigInt(1);
|
|
684
|
+
var _2n2 = /* @__PURE__ */ BigInt(2);
|
|
370
685
|
var _3n = /* @__PURE__ */ BigInt(3);
|
|
371
686
|
var _4n = /* @__PURE__ */ BigInt(4);
|
|
372
687
|
var _5n = /* @__PURE__ */ BigInt(5);
|
|
373
688
|
var _8n = /* @__PURE__ */ BigInt(8);
|
|
689
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
690
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
374
691
|
function mod(a, b) {
|
|
375
692
|
const result = a % b;
|
|
376
|
-
return result >=
|
|
693
|
+
return result >= _0n2 ? result : b + result;
|
|
694
|
+
}
|
|
695
|
+
function pow(num2, power, modulo) {
|
|
696
|
+
if (power < _0n2)
|
|
697
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
698
|
+
if (modulo <= _0n2)
|
|
699
|
+
throw new Error("invalid modulus");
|
|
700
|
+
if (modulo === _1n2)
|
|
701
|
+
return _0n2;
|
|
702
|
+
let res = _1n2;
|
|
703
|
+
while (power > _0n2) {
|
|
704
|
+
if (power & _1n2)
|
|
705
|
+
res = res * num2 % modulo;
|
|
706
|
+
num2 = num2 * num2 % modulo;
|
|
707
|
+
power >>= _1n2;
|
|
708
|
+
}
|
|
709
|
+
return res;
|
|
377
710
|
}
|
|
378
711
|
function pow2(x, power, modulo) {
|
|
379
712
|
let res = x;
|
|
380
|
-
while (power-- >
|
|
713
|
+
while (power-- > _0n2) {
|
|
381
714
|
res *= res;
|
|
382
715
|
res %= modulo;
|
|
383
716
|
}
|
|
384
717
|
return res;
|
|
385
718
|
}
|
|
386
719
|
function invert(number, modulo) {
|
|
387
|
-
if (number ===
|
|
720
|
+
if (number === _0n2)
|
|
388
721
|
throw new Error("invert: expected non-zero number");
|
|
389
|
-
if (modulo <=
|
|
722
|
+
if (modulo <= _0n2)
|
|
390
723
|
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
391
724
|
let a = mod(number, modulo);
|
|
392
725
|
let b = modulo;
|
|
393
|
-
let x =
|
|
394
|
-
while (a !==
|
|
726
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
727
|
+
while (a !== _0n2) {
|
|
395
728
|
const q = b / a;
|
|
396
729
|
const r = b % a;
|
|
397
730
|
const m = x - u * q;
|
|
@@ -399,82 +732,79 @@ function invert(number, modulo) {
|
|
|
399
732
|
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
400
733
|
}
|
|
401
734
|
const gcd = b;
|
|
402
|
-
if (gcd !==
|
|
735
|
+
if (gcd !== _1n2)
|
|
403
736
|
throw new Error("invert: does not exist");
|
|
404
737
|
return mod(x, modulo);
|
|
405
738
|
}
|
|
406
|
-
function sqrt3mod4(Fp, n) {
|
|
407
|
-
const p1div4 = (Fp.ORDER + _1n) / _4n;
|
|
408
|
-
const root = Fp.pow(n, p1div4);
|
|
409
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
410
|
-
throw new Error("Cannot find square root");
|
|
411
|
-
return root;
|
|
412
|
-
}
|
|
413
|
-
function sqrt5mod8(Fp, n) {
|
|
414
|
-
const p5div8 = (Fp.ORDER - _5n) / _8n;
|
|
415
|
-
const n2 = Fp.mul(n, _2n);
|
|
416
|
-
const v = Fp.pow(n2, p5div8);
|
|
417
|
-
const nv = Fp.mul(n, v);
|
|
418
|
-
const i = Fp.mul(Fp.mul(nv, _2n), v);
|
|
419
|
-
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
420
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
421
|
-
throw new Error("Cannot find square root");
|
|
422
|
-
return root;
|
|
423
|
-
}
|
|
424
739
|
function tonelliShanks(P) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
const Q1div2 = (Q +
|
|
740
|
+
const legendreC = (P - _1n2) / _2n2;
|
|
741
|
+
let Q, S, Z;
|
|
742
|
+
for (Q = P - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
|
|
743
|
+
;
|
|
744
|
+
for (Z = _2n2; Z < P && pow(Z, legendreC, P) !== P - _1n2; Z++) {
|
|
745
|
+
if (Z > 1e3)
|
|
746
|
+
throw new Error("Cannot find square root: likely non-prime P");
|
|
747
|
+
}
|
|
748
|
+
if (S === 1) {
|
|
749
|
+
const p1div4 = (P + _1n2) / _4n;
|
|
750
|
+
return function tonelliFast(Fp, n) {
|
|
751
|
+
const root = Fp.pow(n, p1div4);
|
|
752
|
+
if (!Fp.eql(Fp.sqr(root), n))
|
|
753
|
+
throw new Error("Cannot find square root");
|
|
754
|
+
return root;
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
const Q1div2 = (Q + _1n2) / _2n2;
|
|
443
758
|
return function tonelliSlow(Fp, n) {
|
|
444
|
-
if (Fp.
|
|
445
|
-
return n;
|
|
446
|
-
if (FpLegendre(Fp, n) !== 1)
|
|
759
|
+
if (Fp.pow(n, legendreC) === Fp.neg(Fp.ONE))
|
|
447
760
|
throw new Error("Cannot find square root");
|
|
448
|
-
let
|
|
449
|
-
let
|
|
450
|
-
let
|
|
451
|
-
let
|
|
452
|
-
while (!Fp.eql(
|
|
453
|
-
if (Fp.
|
|
761
|
+
let r = S;
|
|
762
|
+
let g = Fp.pow(Fp.mul(Fp.ONE, Z), Q);
|
|
763
|
+
let x = Fp.pow(n, Q1div2);
|
|
764
|
+
let b = Fp.pow(n, Q);
|
|
765
|
+
while (!Fp.eql(b, Fp.ONE)) {
|
|
766
|
+
if (Fp.eql(b, Fp.ZERO))
|
|
454
767
|
return Fp.ZERO;
|
|
455
|
-
let
|
|
456
|
-
let
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
if (i === M)
|
|
461
|
-
throw new Error("Cannot find square root");
|
|
768
|
+
let m = 1;
|
|
769
|
+
for (let t2 = Fp.sqr(b); m < r; m++) {
|
|
770
|
+
if (Fp.eql(t2, Fp.ONE))
|
|
771
|
+
break;
|
|
772
|
+
t2 = Fp.sqr(t2);
|
|
462
773
|
}
|
|
463
|
-
const
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
return R;
|
|
774
|
+
const ge = Fp.pow(g, _1n2 << BigInt(r - m - 1));
|
|
775
|
+
g = Fp.sqr(ge);
|
|
776
|
+
x = Fp.mul(x, ge);
|
|
777
|
+
b = Fp.mul(b, g);
|
|
778
|
+
r = m;
|
|
779
|
+
}
|
|
780
|
+
return x;
|
|
471
781
|
};
|
|
472
782
|
}
|
|
473
783
|
function FpSqrt(P) {
|
|
474
|
-
if (P % _4n === _3n)
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
784
|
+
if (P % _4n === _3n) {
|
|
785
|
+
const p1div4 = (P + _1n2) / _4n;
|
|
786
|
+
return function sqrt3mod4(Fp, n) {
|
|
787
|
+
const root = Fp.pow(n, p1div4);
|
|
788
|
+
if (!Fp.eql(Fp.sqr(root), n))
|
|
789
|
+
throw new Error("Cannot find square root");
|
|
790
|
+
return root;
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
if (P % _8n === _5n) {
|
|
794
|
+
const c1 = (P - _5n) / _8n;
|
|
795
|
+
return function sqrt5mod8(Fp, n) {
|
|
796
|
+
const n2 = Fp.mul(n, _2n2);
|
|
797
|
+
const v = Fp.pow(n2, c1);
|
|
798
|
+
const nv = Fp.mul(n, v);
|
|
799
|
+
const i = Fp.mul(Fp.mul(nv, _2n2), v);
|
|
800
|
+
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
801
|
+
if (!Fp.eql(Fp.sqr(root), n))
|
|
802
|
+
throw new Error("Cannot find square root");
|
|
803
|
+
return root;
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
if (P % _16n === _9n) {
|
|
807
|
+
}
|
|
478
808
|
return tonelliShanks(P);
|
|
479
809
|
}
|
|
480
810
|
var FIELD_FIELDS = [
|
|
@@ -509,59 +839,47 @@ function validateField(field) {
|
|
|
509
839
|
}, initial);
|
|
510
840
|
return validateObject(field, opts);
|
|
511
841
|
}
|
|
512
|
-
function FpPow(
|
|
513
|
-
if (power <
|
|
842
|
+
function FpPow(f, num2, power) {
|
|
843
|
+
if (power < _0n2)
|
|
514
844
|
throw new Error("invalid exponent, negatives unsupported");
|
|
515
|
-
if (power ===
|
|
516
|
-
return
|
|
517
|
-
if (power ===
|
|
845
|
+
if (power === _0n2)
|
|
846
|
+
return f.ONE;
|
|
847
|
+
if (power === _1n2)
|
|
518
848
|
return num2;
|
|
519
|
-
let p =
|
|
849
|
+
let p = f.ONE;
|
|
520
850
|
let d = num2;
|
|
521
|
-
while (power >
|
|
522
|
-
if (power &
|
|
523
|
-
p =
|
|
524
|
-
d =
|
|
525
|
-
power >>=
|
|
851
|
+
while (power > _0n2) {
|
|
852
|
+
if (power & _1n2)
|
|
853
|
+
p = f.mul(p, d);
|
|
854
|
+
d = f.sqr(d);
|
|
855
|
+
power >>= _1n2;
|
|
526
856
|
}
|
|
527
857
|
return p;
|
|
528
858
|
}
|
|
529
|
-
function FpInvertBatch(
|
|
530
|
-
const
|
|
531
|
-
const
|
|
532
|
-
if (
|
|
859
|
+
function FpInvertBatch(f, nums) {
|
|
860
|
+
const tmp = new Array(nums.length);
|
|
861
|
+
const lastMultiplied = nums.reduce((acc, num2, i) => {
|
|
862
|
+
if (f.is0(num2))
|
|
533
863
|
return acc;
|
|
534
|
-
|
|
535
|
-
return
|
|
536
|
-
},
|
|
537
|
-
const
|
|
864
|
+
tmp[i] = acc;
|
|
865
|
+
return f.mul(acc, num2);
|
|
866
|
+
}, f.ONE);
|
|
867
|
+
const inverted = f.inv(lastMultiplied);
|
|
538
868
|
nums.reduceRight((acc, num2, i) => {
|
|
539
|
-
if (
|
|
869
|
+
if (f.is0(num2))
|
|
540
870
|
return acc;
|
|
541
|
-
|
|
542
|
-
return
|
|
543
|
-
},
|
|
544
|
-
return
|
|
545
|
-
}
|
|
546
|
-
function FpLegendre(Fp, n) {
|
|
547
|
-
const p1mod2 = (Fp.ORDER - _1n) / _2n;
|
|
548
|
-
const powered = Fp.pow(n, p1mod2);
|
|
549
|
-
const yes = Fp.eql(powered, Fp.ONE);
|
|
550
|
-
const zero = Fp.eql(powered, Fp.ZERO);
|
|
551
|
-
const no = Fp.eql(powered, Fp.neg(Fp.ONE));
|
|
552
|
-
if (!yes && !zero && !no)
|
|
553
|
-
throw new Error("invalid Legendre symbol result");
|
|
554
|
-
return yes ? 1 : zero ? 0 : -1;
|
|
871
|
+
tmp[i] = f.mul(acc, tmp[i]);
|
|
872
|
+
return f.mul(acc, num2);
|
|
873
|
+
}, inverted);
|
|
874
|
+
return tmp;
|
|
555
875
|
}
|
|
556
876
|
function nLength(n, nBitLength) {
|
|
557
|
-
if (nBitLength !== void 0)
|
|
558
|
-
anumber(nBitLength);
|
|
559
877
|
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
560
878
|
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
561
879
|
return { nBitLength: _nBitLength, nByteLength };
|
|
562
880
|
}
|
|
563
881
|
function Field(ORDER, bitLen2, isLE = false, redef = {}) {
|
|
564
|
-
if (ORDER <=
|
|
882
|
+
if (ORDER <= _0n2)
|
|
565
883
|
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
566
884
|
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2);
|
|
567
885
|
if (BYTES > 2048)
|
|
@@ -569,20 +887,19 @@ function Field(ORDER, bitLen2, isLE = false, redef = {}) {
|
|
|
569
887
|
let sqrtP;
|
|
570
888
|
const f = Object.freeze({
|
|
571
889
|
ORDER,
|
|
572
|
-
isLE,
|
|
573
890
|
BITS,
|
|
574
891
|
BYTES,
|
|
575
892
|
MASK: bitMask(BITS),
|
|
576
|
-
ZERO:
|
|
577
|
-
ONE:
|
|
893
|
+
ZERO: _0n2,
|
|
894
|
+
ONE: _1n2,
|
|
578
895
|
create: (num2) => mod(num2, ORDER),
|
|
579
896
|
isValid: (num2) => {
|
|
580
897
|
if (typeof num2 !== "bigint")
|
|
581
898
|
throw new Error("invalid field element: expected bigint, got " + typeof num2);
|
|
582
|
-
return
|
|
899
|
+
return _0n2 <= num2 && num2 < ORDER;
|
|
583
900
|
},
|
|
584
|
-
is0: (num2) => num2 ===
|
|
585
|
-
isOdd: (num2) => (num2 &
|
|
901
|
+
is0: (num2) => num2 === _0n2,
|
|
902
|
+
isOdd: (num2) => (num2 & _1n2) === _1n2,
|
|
586
903
|
neg: (num2) => mod(-num2, ORDER),
|
|
587
904
|
eql: (lhs, rhs) => lhs === rhs,
|
|
588
905
|
sqr: (num2) => mod(num2 * num2, ORDER),
|
|
@@ -602,17 +919,16 @@ function Field(ORDER, bitLen2, isLE = false, redef = {}) {
|
|
|
602
919
|
sqrtP = FpSqrt(ORDER);
|
|
603
920
|
return sqrtP(f, n);
|
|
604
921
|
}),
|
|
922
|
+
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
923
|
+
// TODO: do we really need constant cmov?
|
|
924
|
+
// We don't have const-time bigints anyway, so probably will be not very useful
|
|
925
|
+
cmov: (a, b, c) => c ? b : a,
|
|
605
926
|
toBytes: (num2) => isLE ? numberToBytesLE(num2, BYTES) : numberToBytesBE(num2, BYTES),
|
|
606
927
|
fromBytes: (bytes) => {
|
|
607
928
|
if (bytes.length !== BYTES)
|
|
608
929
|
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
609
930
|
return isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
610
|
-
}
|
|
611
|
-
// TODO: we don't need it here, move out to separate fn
|
|
612
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
613
|
-
// We can't move this out because Fp6, Fp12 implement it
|
|
614
|
-
// and it's unclear what to return in there.
|
|
615
|
-
cmov: (a, b, c) => c ? b : a
|
|
931
|
+
}
|
|
616
932
|
});
|
|
617
933
|
return Object.freeze(f);
|
|
618
934
|
}
|
|
@@ -632,14 +948,14 @@ function mapHashToField(key, fieldOrder, isLE = false) {
|
|
|
632
948
|
const minLen = getMinHashLength(fieldOrder);
|
|
633
949
|
if (len < 16 || len < minLen || len > 1024)
|
|
634
950
|
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
635
|
-
const num2 = isLE ?
|
|
636
|
-
const reduced = mod(num2, fieldOrder -
|
|
951
|
+
const num2 = isLE ? bytesToNumberBE(key) : bytesToNumberLE(key);
|
|
952
|
+
const reduced = mod(num2, fieldOrder - _1n2) + _1n2;
|
|
637
953
|
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
638
954
|
}
|
|
639
955
|
|
|
640
|
-
// ../../node_modules/.pnpm/@noble+curves@1.
|
|
641
|
-
var
|
|
642
|
-
var
|
|
956
|
+
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/curve.js
|
|
957
|
+
var _0n3 = BigInt(0);
|
|
958
|
+
var _1n3 = BigInt(1);
|
|
643
959
|
function constTimeNegate(condition, item) {
|
|
644
960
|
const neg = item.negate();
|
|
645
961
|
return condition ? neg : item;
|
|
@@ -648,30 +964,11 @@ function validateW(W, bits) {
|
|
|
648
964
|
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
649
965
|
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
650
966
|
}
|
|
651
|
-
function calcWOpts(W,
|
|
652
|
-
validateW(W,
|
|
653
|
-
const windows = Math.ceil(
|
|
967
|
+
function calcWOpts(W, bits) {
|
|
968
|
+
validateW(W, bits);
|
|
969
|
+
const windows = Math.ceil(bits / W) + 1;
|
|
654
970
|
const windowSize = 2 ** (W - 1);
|
|
655
|
-
|
|
656
|
-
const mask = bitMask(W);
|
|
657
|
-
const shiftBy = BigInt(W);
|
|
658
|
-
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
659
|
-
}
|
|
660
|
-
function calcOffsets(n, window, wOpts) {
|
|
661
|
-
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
662
|
-
let wbits = Number(n & mask);
|
|
663
|
-
let nextN = n >> shiftBy;
|
|
664
|
-
if (wbits > windowSize) {
|
|
665
|
-
wbits -= maxNumber;
|
|
666
|
-
nextN += _1n2;
|
|
667
|
-
}
|
|
668
|
-
const offsetStart = window * windowSize;
|
|
669
|
-
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
670
|
-
const isZero = wbits === 0;
|
|
671
|
-
const isNeg = wbits < 0;
|
|
672
|
-
const isNegF = window % 2 !== 0;
|
|
673
|
-
const offsetF = offsetStart;
|
|
674
|
-
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
971
|
+
return { windows, windowSize };
|
|
675
972
|
}
|
|
676
973
|
function validateMSMPoints(points, c) {
|
|
677
974
|
if (!Array.isArray(points))
|
|
@@ -703,11 +1000,11 @@ function wNAF(c, bits) {
|
|
|
703
1000
|
// non-const time multiplication ladder
|
|
704
1001
|
unsafeLadder(elm, n, p = c.ZERO) {
|
|
705
1002
|
let d = elm;
|
|
706
|
-
while (n >
|
|
707
|
-
if (n &
|
|
1003
|
+
while (n > _0n3) {
|
|
1004
|
+
if (n & _1n3)
|
|
708
1005
|
p = p.add(d);
|
|
709
1006
|
d = d.double();
|
|
710
|
-
n >>=
|
|
1007
|
+
n >>= _1n3;
|
|
711
1008
|
}
|
|
712
1009
|
return p;
|
|
713
1010
|
},
|
|
@@ -747,16 +1044,28 @@ function wNAF(c, bits) {
|
|
|
747
1044
|
* @returns real and fake (for const-time) points
|
|
748
1045
|
*/
|
|
749
1046
|
wNAF(W, precomputes, n) {
|
|
1047
|
+
const { windows, windowSize } = calcWOpts(W, bits);
|
|
750
1048
|
let p = c.ZERO;
|
|
751
1049
|
let f = c.BASE;
|
|
752
|
-
const
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
1050
|
+
const mask = BigInt(2 ** W - 1);
|
|
1051
|
+
const maxNumber = 2 ** W;
|
|
1052
|
+
const shiftBy = BigInt(W);
|
|
1053
|
+
for (let window = 0; window < windows; window++) {
|
|
1054
|
+
const offset = window * windowSize;
|
|
1055
|
+
let wbits = Number(n & mask);
|
|
1056
|
+
n >>= shiftBy;
|
|
1057
|
+
if (wbits > windowSize) {
|
|
1058
|
+
wbits -= maxNumber;
|
|
1059
|
+
n += _1n3;
|
|
1060
|
+
}
|
|
1061
|
+
const offset1 = offset;
|
|
1062
|
+
const offset2 = offset + Math.abs(wbits) - 1;
|
|
1063
|
+
const cond1 = window % 2 !== 0;
|
|
1064
|
+
const cond2 = wbits < 0;
|
|
1065
|
+
if (wbits === 0) {
|
|
1066
|
+
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
758
1067
|
} else {
|
|
759
|
-
p = p.add(constTimeNegate(
|
|
1068
|
+
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
760
1069
|
}
|
|
761
1070
|
}
|
|
762
1071
|
return { p, f };
|
|
@@ -770,18 +1079,26 @@ function wNAF(c, bits) {
|
|
|
770
1079
|
* @returns point
|
|
771
1080
|
*/
|
|
772
1081
|
wNAFUnsafe(W, precomputes, n, acc = c.ZERO) {
|
|
773
|
-
const
|
|
774
|
-
|
|
775
|
-
|
|
1082
|
+
const { windows, windowSize } = calcWOpts(W, bits);
|
|
1083
|
+
const mask = BigInt(2 ** W - 1);
|
|
1084
|
+
const maxNumber = 2 ** W;
|
|
1085
|
+
const shiftBy = BigInt(W);
|
|
1086
|
+
for (let window = 0; window < windows; window++) {
|
|
1087
|
+
const offset = window * windowSize;
|
|
1088
|
+
if (n === _0n3)
|
|
776
1089
|
break;
|
|
777
|
-
|
|
778
|
-
n
|
|
779
|
-
if (
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
const item = precomputes[offset];
|
|
783
|
-
acc = acc.add(isNeg ? item.negate() : item);
|
|
1090
|
+
let wbits = Number(n & mask);
|
|
1091
|
+
n >>= shiftBy;
|
|
1092
|
+
if (wbits > windowSize) {
|
|
1093
|
+
wbits -= maxNumber;
|
|
1094
|
+
n += _1n3;
|
|
784
1095
|
}
|
|
1096
|
+
if (wbits === 0)
|
|
1097
|
+
continue;
|
|
1098
|
+
let curr = precomputes[offset + Math.abs(wbits) - 1];
|
|
1099
|
+
if (wbits < 0)
|
|
1100
|
+
curr = curr.negate();
|
|
1101
|
+
acc = acc.add(curr);
|
|
785
1102
|
}
|
|
786
1103
|
return acc;
|
|
787
1104
|
},
|
|
@@ -817,28 +1134,20 @@ function wNAF(c, bits) {
|
|
|
817
1134
|
function pippenger(c, fieldN, points, scalars) {
|
|
818
1135
|
validateMSMPoints(points, c);
|
|
819
1136
|
validateMSMScalars(scalars, fieldN);
|
|
820
|
-
|
|
821
|
-
const slength = scalars.length;
|
|
822
|
-
if (plength !== slength)
|
|
1137
|
+
if (points.length !== scalars.length)
|
|
823
1138
|
throw new Error("arrays of points and scalars must have equal length");
|
|
824
1139
|
const zero = c.ZERO;
|
|
825
|
-
const wbits = bitLen(BigInt(
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
else if (wbits > 4)
|
|
830
|
-
windowSize = wbits - 2;
|
|
831
|
-
else if (wbits > 0)
|
|
832
|
-
windowSize = 2;
|
|
833
|
-
const MASK = bitMask(windowSize);
|
|
834
|
-
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
1140
|
+
const wbits = bitLen(BigInt(points.length));
|
|
1141
|
+
const windowSize = wbits > 12 ? wbits - 3 : wbits > 4 ? wbits - 2 : wbits ? 2 : 1;
|
|
1142
|
+
const MASK = (1 << windowSize) - 1;
|
|
1143
|
+
const buckets = new Array(MASK + 1).fill(zero);
|
|
835
1144
|
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
836
1145
|
let sum = zero;
|
|
837
1146
|
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
838
1147
|
buckets.fill(zero);
|
|
839
|
-
for (let j = 0; j <
|
|
1148
|
+
for (let j = 0; j < scalars.length; j++) {
|
|
840
1149
|
const scalar = scalars[j];
|
|
841
|
-
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
|
1150
|
+
const wbits2 = Number(scalar >> BigInt(i) & BigInt(MASK));
|
|
842
1151
|
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
843
1152
|
}
|
|
844
1153
|
let resI = zero;
|
|
@@ -871,7 +1180,7 @@ function validateBasic(curve) {
|
|
|
871
1180
|
});
|
|
872
1181
|
}
|
|
873
1182
|
|
|
874
|
-
// ../../node_modules/.pnpm/@noble+curves@1.
|
|
1183
|
+
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/weierstrass.js
|
|
875
1184
|
function validateSigVerOpts(opts) {
|
|
876
1185
|
if (opts.lowS !== void 0)
|
|
877
1186
|
abool("lowS", opts.lowS);
|
|
@@ -884,33 +1193,33 @@ function validatePointOpts(curve) {
|
|
|
884
1193
|
a: "field",
|
|
885
1194
|
b: "field"
|
|
886
1195
|
}, {
|
|
887
|
-
allowInfinityPoint: "boolean",
|
|
888
1196
|
allowedPrivateKeyLengths: "array",
|
|
1197
|
+
wrapPrivateKey: "boolean",
|
|
1198
|
+
isTorsionFree: "function",
|
|
889
1199
|
clearCofactor: "function",
|
|
1200
|
+
allowInfinityPoint: "boolean",
|
|
890
1201
|
fromBytes: "function",
|
|
891
|
-
|
|
892
|
-
toBytes: "function",
|
|
893
|
-
wrapPrivateKey: "boolean"
|
|
1202
|
+
toBytes: "function"
|
|
894
1203
|
});
|
|
895
1204
|
const { endo, Fp, a } = opts;
|
|
896
1205
|
if (endo) {
|
|
897
1206
|
if (!Fp.eql(a, Fp.ZERO)) {
|
|
898
|
-
throw new Error("invalid
|
|
1207
|
+
throw new Error("invalid endomorphism, can only be defined for Koblitz curves that have a=0");
|
|
899
1208
|
}
|
|
900
1209
|
if (typeof endo !== "object" || typeof endo.beta !== "bigint" || typeof endo.splitScalar !== "function") {
|
|
901
|
-
throw new Error(
|
|
1210
|
+
throw new Error("invalid endomorphism, expected beta: bigint and splitScalar: function");
|
|
902
1211
|
}
|
|
903
1212
|
}
|
|
904
1213
|
return Object.freeze({ ...opts });
|
|
905
1214
|
}
|
|
906
|
-
var
|
|
907
|
-
constructor(m = "") {
|
|
908
|
-
super(m);
|
|
909
|
-
}
|
|
910
|
-
};
|
|
1215
|
+
var { bytesToNumberBE: b2n, hexToBytes: h2b } = utils_exports;
|
|
911
1216
|
var DER = {
|
|
912
1217
|
// asn.1 DER encoding utils
|
|
913
|
-
Err: DERErr
|
|
1218
|
+
Err: class DERErr extends Error {
|
|
1219
|
+
constructor(m = "") {
|
|
1220
|
+
super(m);
|
|
1221
|
+
}
|
|
1222
|
+
},
|
|
914
1223
|
// Basic building block is TLV (Tag-Length-Value)
|
|
915
1224
|
_tlv: {
|
|
916
1225
|
encode: (tag, data) => {
|
|
@@ -970,7 +1279,7 @@ var DER = {
|
|
|
970
1279
|
_int: {
|
|
971
1280
|
encode(num2) {
|
|
972
1281
|
const { Err: E } = DER;
|
|
973
|
-
if (num2 <
|
|
1282
|
+
if (num2 < _0n4)
|
|
974
1283
|
throw new E("integer: negative integers are not allowed");
|
|
975
1284
|
let hex = numberToHexUnpadded(num2);
|
|
976
1285
|
if (Number.parseInt(hex[0], 16) & 8)
|
|
@@ -985,12 +1294,13 @@ var DER = {
|
|
|
985
1294
|
throw new E("invalid signature integer: negative");
|
|
986
1295
|
if (data[0] === 0 && !(data[1] & 128))
|
|
987
1296
|
throw new E("invalid signature integer: unnecessary leading zero");
|
|
988
|
-
return
|
|
1297
|
+
return b2n(data);
|
|
989
1298
|
}
|
|
990
1299
|
},
|
|
991
1300
|
toSig(hex) {
|
|
992
1301
|
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
993
|
-
const data =
|
|
1302
|
+
const data = typeof hex === "string" ? h2b(hex) : hex;
|
|
1303
|
+
abytes2(data);
|
|
994
1304
|
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
995
1305
|
if (seqLeftBytes.length)
|
|
996
1306
|
throw new E("invalid signature: left bytes after parsing");
|
|
@@ -1008,12 +1318,9 @@ var DER = {
|
|
|
1008
1318
|
return tlv.encode(48, seq);
|
|
1009
1319
|
}
|
|
1010
1320
|
};
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
var _0n3 = BigInt(0);
|
|
1015
|
-
var _1n3 = BigInt(1);
|
|
1016
|
-
var _2n2 = BigInt(2);
|
|
1321
|
+
var _0n4 = BigInt(0);
|
|
1322
|
+
var _1n4 = BigInt(1);
|
|
1323
|
+
var _2n3 = BigInt(2);
|
|
1017
1324
|
var _3n2 = BigInt(3);
|
|
1018
1325
|
var _4n2 = BigInt(4);
|
|
1019
1326
|
function weierstrassPoints(opts) {
|
|
@@ -1036,24 +1343,15 @@ function weierstrassPoints(opts) {
|
|
|
1036
1343
|
const x3 = Fp.mul(x2, x);
|
|
1037
1344
|
return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
|
|
1038
1345
|
}
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
const right = weierstrassEquation(x);
|
|
1042
|
-
return Fp.eql(left, right);
|
|
1043
|
-
}
|
|
1044
|
-
if (!isValidXY(CURVE.Gx, CURVE.Gy))
|
|
1045
|
-
throw new Error("bad curve params: generator point");
|
|
1046
|
-
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n2), _4n2);
|
|
1047
|
-
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
1048
|
-
if (Fp.is0(Fp.add(_4a3, _27b2)))
|
|
1049
|
-
throw new Error("bad curve params: a or b");
|
|
1346
|
+
if (!Fp.eql(Fp.sqr(CURVE.Gy), weierstrassEquation(CURVE.Gx)))
|
|
1347
|
+
throw new Error("bad generator point: equation left != right");
|
|
1050
1348
|
function isWithinCurveOrder(num2) {
|
|
1051
|
-
return inRange(num2,
|
|
1349
|
+
return inRange(num2, _1n4, CURVE.n);
|
|
1052
1350
|
}
|
|
1053
1351
|
function normPrivateKeyToScalar(key) {
|
|
1054
1352
|
const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n: N } = CURVE;
|
|
1055
1353
|
if (lengths && typeof key !== "bigint") {
|
|
1056
|
-
if (
|
|
1354
|
+
if (isBytes2(key))
|
|
1057
1355
|
key = bytesToHex(key);
|
|
1058
1356
|
if (typeof key !== "string" || !lengths.includes(key.length))
|
|
1059
1357
|
throw new Error("invalid private key");
|
|
@@ -1067,10 +1365,10 @@ function weierstrassPoints(opts) {
|
|
|
1067
1365
|
}
|
|
1068
1366
|
if (wrapPrivateKey)
|
|
1069
1367
|
num2 = mod(num2, N);
|
|
1070
|
-
aInRange("private key", num2,
|
|
1368
|
+
aInRange("private key", num2, _1n4, N);
|
|
1071
1369
|
return num2;
|
|
1072
1370
|
}
|
|
1073
|
-
function
|
|
1371
|
+
function assertPrjPoint(other) {
|
|
1074
1372
|
if (!(other instanceof Point2))
|
|
1075
1373
|
throw new Error("ProjectivePoint expected");
|
|
1076
1374
|
}
|
|
@@ -1099,7 +1397,9 @@ function weierstrassPoints(opts) {
|
|
|
1099
1397
|
const { x, y } = p.toAffine();
|
|
1100
1398
|
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
1101
1399
|
throw new Error("bad point: x or y not FE");
|
|
1102
|
-
|
|
1400
|
+
const left = Fp.sqr(y);
|
|
1401
|
+
const right = weierstrassEquation(x);
|
|
1402
|
+
if (!Fp.eql(left, right))
|
|
1103
1403
|
throw new Error("bad point: equation left != right");
|
|
1104
1404
|
if (!p.isTorsionFree())
|
|
1105
1405
|
throw new Error("bad point: not in prime-order subgroup");
|
|
@@ -1107,15 +1407,15 @@ function weierstrassPoints(opts) {
|
|
|
1107
1407
|
});
|
|
1108
1408
|
class Point2 {
|
|
1109
1409
|
constructor(px, py, pz) {
|
|
1410
|
+
this.px = px;
|
|
1411
|
+
this.py = py;
|
|
1412
|
+
this.pz = pz;
|
|
1110
1413
|
if (px == null || !Fp.isValid(px))
|
|
1111
1414
|
throw new Error("x required");
|
|
1112
|
-
if (py == null || !Fp.isValid(py)
|
|
1415
|
+
if (py == null || !Fp.isValid(py))
|
|
1113
1416
|
throw new Error("y required");
|
|
1114
1417
|
if (pz == null || !Fp.isValid(pz))
|
|
1115
1418
|
throw new Error("z required");
|
|
1116
|
-
this.px = px;
|
|
1117
|
-
this.py = py;
|
|
1118
|
-
this.pz = pz;
|
|
1119
1419
|
Object.freeze(this);
|
|
1120
1420
|
}
|
|
1121
1421
|
// Does not validate if the point is on-curve.
|
|
@@ -1144,7 +1444,7 @@ function weierstrassPoints(opts) {
|
|
|
1144
1444
|
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
1145
1445
|
*/
|
|
1146
1446
|
static normalizeZ(points) {
|
|
1147
|
-
const toInv =
|
|
1447
|
+
const toInv = Fp.invertBatch(points.map((p) => p.pz));
|
|
1148
1448
|
return points.map((p, i) => p.toAffine(toInv[i])).map(Point2.fromAffine);
|
|
1149
1449
|
}
|
|
1150
1450
|
/**
|
|
@@ -1182,7 +1482,7 @@ function weierstrassPoints(opts) {
|
|
|
1182
1482
|
* Compare one point to another.
|
|
1183
1483
|
*/
|
|
1184
1484
|
equals(other) {
|
|
1185
|
-
|
|
1485
|
+
assertPrjPoint(other);
|
|
1186
1486
|
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
1187
1487
|
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
1188
1488
|
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
@@ -1242,7 +1542,7 @@ function weierstrassPoints(opts) {
|
|
|
1242
1542
|
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
1243
1543
|
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
1244
1544
|
add(other) {
|
|
1245
|
-
|
|
1545
|
+
assertPrjPoint(other);
|
|
1246
1546
|
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
1247
1547
|
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
1248
1548
|
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
@@ -1305,33 +1605,33 @@ function weierstrassPoints(opts) {
|
|
|
1305
1605
|
* an exposed private key e.g. sig verification, which works over *public* keys.
|
|
1306
1606
|
*/
|
|
1307
1607
|
multiplyUnsafe(sc) {
|
|
1308
|
-
const { endo
|
|
1309
|
-
aInRange("scalar", sc,
|
|
1608
|
+
const { endo, n: N } = CURVE;
|
|
1609
|
+
aInRange("scalar", sc, _0n4, N);
|
|
1310
1610
|
const I = Point2.ZERO;
|
|
1311
|
-
if (sc ===
|
|
1611
|
+
if (sc === _0n4)
|
|
1312
1612
|
return I;
|
|
1313
|
-
if (this.is0() || sc ===
|
|
1613
|
+
if (this.is0() || sc === _1n4)
|
|
1314
1614
|
return this;
|
|
1315
|
-
if (!
|
|
1615
|
+
if (!endo || wnaf.hasPrecomputes(this))
|
|
1316
1616
|
return wnaf.wNAFCachedUnsafe(this, sc, Point2.normalizeZ);
|
|
1317
|
-
let { k1neg, k1, k2neg, k2 } =
|
|
1617
|
+
let { k1neg, k1, k2neg, k2 } = endo.splitScalar(sc);
|
|
1318
1618
|
let k1p = I;
|
|
1319
1619
|
let k2p = I;
|
|
1320
1620
|
let d = this;
|
|
1321
|
-
while (k1 >
|
|
1322
|
-
if (k1 &
|
|
1621
|
+
while (k1 > _0n4 || k2 > _0n4) {
|
|
1622
|
+
if (k1 & _1n4)
|
|
1323
1623
|
k1p = k1p.add(d);
|
|
1324
|
-
if (k2 &
|
|
1624
|
+
if (k2 & _1n4)
|
|
1325
1625
|
k2p = k2p.add(d);
|
|
1326
1626
|
d = d.double();
|
|
1327
|
-
k1 >>=
|
|
1328
|
-
k2 >>=
|
|
1627
|
+
k1 >>= _1n4;
|
|
1628
|
+
k2 >>= _1n4;
|
|
1329
1629
|
}
|
|
1330
1630
|
if (k1neg)
|
|
1331
1631
|
k1p = k1p.negate();
|
|
1332
1632
|
if (k2neg)
|
|
1333
1633
|
k2p = k2p.negate();
|
|
1334
|
-
k2p = new Point2(Fp.mul(k2p.px,
|
|
1634
|
+
k2p = new Point2(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
1335
1635
|
return k1p.add(k2p);
|
|
1336
1636
|
}
|
|
1337
1637
|
/**
|
|
@@ -1344,16 +1644,16 @@ function weierstrassPoints(opts) {
|
|
|
1344
1644
|
* @returns New point
|
|
1345
1645
|
*/
|
|
1346
1646
|
multiply(scalar) {
|
|
1347
|
-
const { endo
|
|
1348
|
-
aInRange("scalar", scalar,
|
|
1647
|
+
const { endo, n: N } = CURVE;
|
|
1648
|
+
aInRange("scalar", scalar, _1n4, N);
|
|
1349
1649
|
let point, fake;
|
|
1350
|
-
if (
|
|
1351
|
-
const { k1neg, k1, k2neg, k2 } =
|
|
1650
|
+
if (endo) {
|
|
1651
|
+
const { k1neg, k1, k2neg, k2 } = endo.splitScalar(scalar);
|
|
1352
1652
|
let { p: k1p, f: f1p } = this.wNAF(k1);
|
|
1353
1653
|
let { p: k2p, f: f2p } = this.wNAF(k2);
|
|
1354
1654
|
k1p = wnaf.constTimeNegate(k1neg, k1p);
|
|
1355
1655
|
k2p = wnaf.constTimeNegate(k2neg, k2p);
|
|
1356
|
-
k2p = new Point2(Fp.mul(k2p.px,
|
|
1656
|
+
k2p = new Point2(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
1357
1657
|
point = k1p.add(k2p);
|
|
1358
1658
|
fake = f1p.add(f2p);
|
|
1359
1659
|
} else {
|
|
@@ -1371,7 +1671,7 @@ function weierstrassPoints(opts) {
|
|
|
1371
1671
|
*/
|
|
1372
1672
|
multiplyAndAddUnsafe(Q, a, b) {
|
|
1373
1673
|
const G = Point2.BASE;
|
|
1374
|
-
const mul = (P, a2) => a2 ===
|
|
1674
|
+
const mul = (P, a2) => a2 === _0n4 || a2 === _1n4 || !P.equals(G) ? P.multiplyUnsafe(a2) : P.multiply(a2);
|
|
1375
1675
|
const sum = mul(this, a).add(mul(Q, b));
|
|
1376
1676
|
return sum.is0() ? void 0 : sum;
|
|
1377
1677
|
}
|
|
@@ -1383,7 +1683,7 @@ function weierstrassPoints(opts) {
|
|
|
1383
1683
|
}
|
|
1384
1684
|
isTorsionFree() {
|
|
1385
1685
|
const { h: cofactor, isTorsionFree } = CURVE;
|
|
1386
|
-
if (cofactor ===
|
|
1686
|
+
if (cofactor === _1n4)
|
|
1387
1687
|
return true;
|
|
1388
1688
|
if (isTorsionFree)
|
|
1389
1689
|
return isTorsionFree(Point2, this);
|
|
@@ -1391,7 +1691,7 @@ function weierstrassPoints(opts) {
|
|
|
1391
1691
|
}
|
|
1392
1692
|
clearCofactor() {
|
|
1393
1693
|
const { h: cofactor, clearCofactor } = CURVE;
|
|
1394
|
-
if (cofactor ===
|
|
1694
|
+
if (cofactor === _1n4)
|
|
1395
1695
|
return this;
|
|
1396
1696
|
if (clearCofactor)
|
|
1397
1697
|
return clearCofactor(Point2, this);
|
|
@@ -1409,8 +1709,8 @@ function weierstrassPoints(opts) {
|
|
|
1409
1709
|
}
|
|
1410
1710
|
Point2.BASE = new Point2(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
|
1411
1711
|
Point2.ZERO = new Point2(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
1412
|
-
const
|
|
1413
|
-
const wnaf = wNAF(Point2, endo ? Math.ceil(
|
|
1712
|
+
const _bits = CURVE.nBitLength;
|
|
1713
|
+
const wnaf = wNAF(Point2, CURVE.endo ? Math.ceil(_bits / 2) : _bits);
|
|
1414
1714
|
return {
|
|
1415
1715
|
CURVE,
|
|
1416
1716
|
ProjectivePoint: Point2,
|
|
@@ -1434,7 +1734,7 @@ function validateOpts(curve) {
|
|
|
1434
1734
|
}
|
|
1435
1735
|
function weierstrass(curveDef) {
|
|
1436
1736
|
const CURVE = validateOpts(curveDef);
|
|
1437
|
-
const { Fp, n: CURVE_ORDER
|
|
1737
|
+
const { Fp, n: CURVE_ORDER } = CURVE;
|
|
1438
1738
|
const compressedLen = Fp.BYTES + 1;
|
|
1439
1739
|
const uncompressedLen = 2 * Fp.BYTES + 1;
|
|
1440
1740
|
function modN2(a) {
|
|
@@ -1462,7 +1762,7 @@ function weierstrass(curveDef) {
|
|
|
1462
1762
|
const tail = bytes.subarray(1);
|
|
1463
1763
|
if (len === compressedLen && (head === 2 || head === 3)) {
|
|
1464
1764
|
const x = bytesToNumberBE(tail);
|
|
1465
|
-
if (!inRange(x,
|
|
1765
|
+
if (!inRange(x, _1n4, Fp.ORDER))
|
|
1466
1766
|
throw new Error("Point is not on curve");
|
|
1467
1767
|
const y2 = weierstrassEquation(x);
|
|
1468
1768
|
let y;
|
|
@@ -1472,7 +1772,7 @@ function weierstrass(curveDef) {
|
|
|
1472
1772
|
const suffix = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
1473
1773
|
throw new Error("Point is not on curve" + suffix);
|
|
1474
1774
|
}
|
|
1475
|
-
const isYOdd = (y &
|
|
1775
|
+
const isYOdd = (y & _1n4) === _1n4;
|
|
1476
1776
|
const isHeadOdd = (head & 1) === 1;
|
|
1477
1777
|
if (isHeadOdd !== isYOdd)
|
|
1478
1778
|
y = Fp.neg(y);
|
|
@@ -1488,8 +1788,9 @@ function weierstrass(curveDef) {
|
|
|
1488
1788
|
}
|
|
1489
1789
|
}
|
|
1490
1790
|
});
|
|
1791
|
+
const numToNByteStr = (num2) => bytesToHex(numberToBytesBE(num2, CURVE.nByteLength));
|
|
1491
1792
|
function isBiggerThanHalfOrder(number) {
|
|
1492
|
-
const HALF = CURVE_ORDER >>
|
|
1793
|
+
const HALF = CURVE_ORDER >> _1n4;
|
|
1493
1794
|
return number > HALF;
|
|
1494
1795
|
}
|
|
1495
1796
|
function normalizeS(s) {
|
|
@@ -1498,17 +1799,14 @@ function weierstrass(curveDef) {
|
|
|
1498
1799
|
const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
|
|
1499
1800
|
class Signature {
|
|
1500
1801
|
constructor(r, s, recovery) {
|
|
1501
|
-
aInRange("r", r, _1n3, CURVE_ORDER);
|
|
1502
|
-
aInRange("s", s, _1n3, CURVE_ORDER);
|
|
1503
1802
|
this.r = r;
|
|
1504
1803
|
this.s = s;
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
Object.freeze(this);
|
|
1804
|
+
this.recovery = recovery;
|
|
1805
|
+
this.assertValidity();
|
|
1508
1806
|
}
|
|
1509
1807
|
// pair (bytes of r, bytes of s)
|
|
1510
1808
|
static fromCompact(hex) {
|
|
1511
|
-
const l = nByteLength;
|
|
1809
|
+
const l = CURVE.nByteLength;
|
|
1512
1810
|
hex = ensureBytes("compactSignature", hex, l * 2);
|
|
1513
1811
|
return new Signature(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
|
|
1514
1812
|
}
|
|
@@ -1518,11 +1816,9 @@ function weierstrass(curveDef) {
|
|
|
1518
1816
|
const { r, s } = DER.toSig(ensureBytes("DER", hex));
|
|
1519
1817
|
return new Signature(r, s);
|
|
1520
1818
|
}
|
|
1521
|
-
/**
|
|
1522
|
-
* @todo remove
|
|
1523
|
-
* @deprecated
|
|
1524
|
-
*/
|
|
1525
1819
|
assertValidity() {
|
|
1820
|
+
aInRange("r", this.r, _1n4, CURVE_ORDER);
|
|
1821
|
+
aInRange("s", this.s, _1n4, CURVE_ORDER);
|
|
1526
1822
|
}
|
|
1527
1823
|
addRecoveryBit(recovery) {
|
|
1528
1824
|
return new Signature(this.r, this.s, recovery);
|
|
@@ -1536,7 +1832,7 @@ function weierstrass(curveDef) {
|
|
|
1536
1832
|
if (radj >= Fp.ORDER)
|
|
1537
1833
|
throw new Error("recovery id 2 or 3 invalid");
|
|
1538
1834
|
const prefix = (rec & 1) === 0 ? "02" : "03";
|
|
1539
|
-
const R = Point2.fromHex(prefix +
|
|
1835
|
+
const R = Point2.fromHex(prefix + numToNByteStr(radj));
|
|
1540
1836
|
const ir = invN(radj);
|
|
1541
1837
|
const u1 = modN2(-h * ir);
|
|
1542
1838
|
const u2 = modN2(s * ir);
|
|
@@ -1558,15 +1854,14 @@ function weierstrass(curveDef) {
|
|
|
1558
1854
|
return hexToBytes(this.toDERHex());
|
|
1559
1855
|
}
|
|
1560
1856
|
toDERHex() {
|
|
1561
|
-
return DER.hexFromSig(this);
|
|
1857
|
+
return DER.hexFromSig({ r: this.r, s: this.s });
|
|
1562
1858
|
}
|
|
1563
1859
|
// padded bytes of r, then padded bytes of s
|
|
1564
1860
|
toCompactRawBytes() {
|
|
1565
1861
|
return hexToBytes(this.toCompactHex());
|
|
1566
1862
|
}
|
|
1567
1863
|
toCompactHex() {
|
|
1568
|
-
|
|
1569
|
-
return numToSizedHex(this.r, l) + numToSizedHex(this.s, l);
|
|
1864
|
+
return numToNByteStr(this.r) + numToNByteStr(this.s);
|
|
1570
1865
|
}
|
|
1571
1866
|
}
|
|
1572
1867
|
const utils = {
|
|
@@ -1605,25 +1900,21 @@ function weierstrass(curveDef) {
|
|
|
1605
1900
|
return Point2.fromPrivateKey(privateKey).toRawBytes(isCompressed);
|
|
1606
1901
|
}
|
|
1607
1902
|
function isProbPub(item) {
|
|
1608
|
-
|
|
1609
|
-
|
|
1903
|
+
const arr = isBytes2(item);
|
|
1904
|
+
const str = typeof item === "string";
|
|
1905
|
+
const len = (arr || str) && item.length;
|
|
1906
|
+
if (arr)
|
|
1907
|
+
return len === compressedLen || len === uncompressedLen;
|
|
1908
|
+
if (str)
|
|
1909
|
+
return len === 2 * compressedLen || len === 2 * uncompressedLen;
|
|
1610
1910
|
if (item instanceof Point2)
|
|
1611
1911
|
return true;
|
|
1612
|
-
|
|
1613
|
-
const len = arr.length;
|
|
1614
|
-
const fpl = Fp.BYTES;
|
|
1615
|
-
const compLen = fpl + 1;
|
|
1616
|
-
const uncompLen = 2 * fpl + 1;
|
|
1617
|
-
if (CURVE.allowedPrivateKeyLengths || nByteLength === compLen) {
|
|
1618
|
-
return void 0;
|
|
1619
|
-
} else {
|
|
1620
|
-
return len === compLen || len === uncompLen;
|
|
1621
|
-
}
|
|
1912
|
+
return false;
|
|
1622
1913
|
}
|
|
1623
1914
|
function getSharedSecret(privateA, publicB, isCompressed = true) {
|
|
1624
|
-
if (isProbPub(privateA)
|
|
1915
|
+
if (isProbPub(privateA))
|
|
1625
1916
|
throw new Error("first arg must be private key");
|
|
1626
|
-
if (isProbPub(publicB)
|
|
1917
|
+
if (!isProbPub(publicB))
|
|
1627
1918
|
throw new Error("second arg must be public key");
|
|
1628
1919
|
const b = Point2.fromHex(publicB);
|
|
1629
1920
|
return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
|
|
@@ -1632,16 +1923,16 @@ function weierstrass(curveDef) {
|
|
|
1632
1923
|
if (bytes.length > 8192)
|
|
1633
1924
|
throw new Error("input is too large");
|
|
1634
1925
|
const num2 = bytesToNumberBE(bytes);
|
|
1635
|
-
const delta = bytes.length * 8 - nBitLength;
|
|
1926
|
+
const delta = bytes.length * 8 - CURVE.nBitLength;
|
|
1636
1927
|
return delta > 0 ? num2 >> BigInt(delta) : num2;
|
|
1637
1928
|
};
|
|
1638
1929
|
const bits2int_modN = CURVE.bits2int_modN || function(bytes) {
|
|
1639
1930
|
return modN2(bits2int(bytes));
|
|
1640
1931
|
};
|
|
1641
|
-
const ORDER_MASK = bitMask(nBitLength);
|
|
1932
|
+
const ORDER_MASK = bitMask(CURVE.nBitLength);
|
|
1642
1933
|
function int2octets(num2) {
|
|
1643
|
-
aInRange("num < 2^" + nBitLength, num2,
|
|
1644
|
-
return numberToBytesBE(num2, nByteLength);
|
|
1934
|
+
aInRange("num < 2^" + CURVE.nBitLength, num2, _0n4, ORDER_MASK);
|
|
1935
|
+
return numberToBytesBE(num2, CURVE.nByteLength);
|
|
1645
1936
|
}
|
|
1646
1937
|
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
|
1647
1938
|
if (["recovered", "canonical"].some((k) => k in opts))
|
|
@@ -1670,12 +1961,12 @@ function weierstrass(curveDef) {
|
|
|
1670
1961
|
const ik = invN(k);
|
|
1671
1962
|
const q = Point2.BASE.multiply(k).toAffine();
|
|
1672
1963
|
const r = modN2(q.x);
|
|
1673
|
-
if (r ===
|
|
1964
|
+
if (r === _0n4)
|
|
1674
1965
|
return;
|
|
1675
1966
|
const s = modN2(ik * modN2(m + r * d));
|
|
1676
|
-
if (s ===
|
|
1967
|
+
if (s === _0n4)
|
|
1677
1968
|
return;
|
|
1678
|
-
let recovery = (q.x === r ? 0 : 2) | Number(q.y &
|
|
1969
|
+
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
|
|
1679
1970
|
let normS = s;
|
|
1680
1971
|
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
1681
1972
|
normS = normalizeS(s);
|
|
@@ -1704,7 +1995,7 @@ function weierstrass(curveDef) {
|
|
|
1704
1995
|
throw new Error("options.strict was renamed to lowS");
|
|
1705
1996
|
if (format !== void 0 && format !== "compact" && format !== "der")
|
|
1706
1997
|
throw new Error("format must be compact or der");
|
|
1707
|
-
const isHex = typeof sg === "string" ||
|
|
1998
|
+
const isHex = typeof sg === "string" || isBytes2(sg);
|
|
1708
1999
|
const isObj = !isHex && !format && typeof sg === "object" && sg !== null && typeof sg.r === "bigint" && typeof sg.s === "bigint";
|
|
1709
2000
|
if (!isHex && !isObj)
|
|
1710
2001
|
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
@@ -1758,18 +2049,18 @@ function weierstrass(curveDef) {
|
|
|
1758
2049
|
}
|
|
1759
2050
|
function SWUFpSqrtRatio(Fp, Z) {
|
|
1760
2051
|
const q = Fp.ORDER;
|
|
1761
|
-
let l =
|
|
1762
|
-
for (let o = q -
|
|
1763
|
-
l +=
|
|
2052
|
+
let l = _0n4;
|
|
2053
|
+
for (let o = q - _1n4; o % _2n3 === _0n4; o /= _2n3)
|
|
2054
|
+
l += _1n4;
|
|
1764
2055
|
const c1 = l;
|
|
1765
|
-
const _2n_pow_c1_1 =
|
|
1766
|
-
const _2n_pow_c1 = _2n_pow_c1_1 *
|
|
1767
|
-
const c2 = (q -
|
|
1768
|
-
const c3 = (c2 -
|
|
1769
|
-
const c4 = _2n_pow_c1 -
|
|
2056
|
+
const _2n_pow_c1_1 = _2n3 << c1 - _1n4 - _1n4;
|
|
2057
|
+
const _2n_pow_c1 = _2n_pow_c1_1 * _2n3;
|
|
2058
|
+
const c2 = (q - _1n4) / _2n_pow_c1;
|
|
2059
|
+
const c3 = (c2 - _1n4) / _2n3;
|
|
2060
|
+
const c4 = _2n_pow_c1 - _1n4;
|
|
1770
2061
|
const c5 = _2n_pow_c1_1;
|
|
1771
2062
|
const c6 = Fp.pow(Z, c2);
|
|
1772
|
-
const c7 = Fp.pow(Z, (c2 +
|
|
2063
|
+
const c7 = Fp.pow(Z, (c2 + _1n4) / _2n3);
|
|
1773
2064
|
let sqrtRatio = (u, v) => {
|
|
1774
2065
|
let tv1 = c6;
|
|
1775
2066
|
let tv2 = Fp.pow(v, c4);
|
|
@@ -1787,9 +2078,9 @@ function SWUFpSqrtRatio(Fp, Z) {
|
|
|
1787
2078
|
tv5 = Fp.mul(tv4, tv1);
|
|
1788
2079
|
tv3 = Fp.cmov(tv2, tv3, isQR);
|
|
1789
2080
|
tv4 = Fp.cmov(tv5, tv4, isQR);
|
|
1790
|
-
for (let i = c1; i >
|
|
1791
|
-
let tv52 = i -
|
|
1792
|
-
tv52 =
|
|
2081
|
+
for (let i = c1; i > _1n4; i--) {
|
|
2082
|
+
let tv52 = i - _2n3;
|
|
2083
|
+
tv52 = _2n3 << tv52 - _1n4;
|
|
1793
2084
|
let tvv5 = Fp.pow(tv4, tv52);
|
|
1794
2085
|
const e1 = Fp.eql(tvv5, Fp.ONE);
|
|
1795
2086
|
tv2 = Fp.mul(tv3, tv1);
|
|
@@ -1851,13 +2142,12 @@ function mapToCurveSimpleSWU(Fp, opts) {
|
|
|
1851
2142
|
y = Fp.cmov(y, value, isValid);
|
|
1852
2143
|
const e1 = Fp.isOdd(u) === Fp.isOdd(y);
|
|
1853
2144
|
y = Fp.cmov(Fp.neg(y), y, e1);
|
|
1854
|
-
|
|
1855
|
-
x = Fp.mul(x, tv4_inv);
|
|
2145
|
+
x = Fp.div(x, tv4);
|
|
1856
2146
|
return { x, y };
|
|
1857
2147
|
};
|
|
1858
2148
|
}
|
|
1859
2149
|
|
|
1860
|
-
// ../../node_modules/.pnpm/@noble+curves@1.
|
|
2150
|
+
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/_shortw_utils.js
|
|
1861
2151
|
function getHash(hash) {
|
|
1862
2152
|
return {
|
|
1863
2153
|
hash,
|
|
@@ -1867,10 +2157,10 @@ function getHash(hash) {
|
|
|
1867
2157
|
}
|
|
1868
2158
|
function createCurve(curveDef, defHash) {
|
|
1869
2159
|
const create = (hash) => weierstrass({ ...curveDef, ...getHash(hash) });
|
|
1870
|
-
return { ...create(defHash), create };
|
|
2160
|
+
return Object.freeze({ ...create(defHash), create });
|
|
1871
2161
|
}
|
|
1872
2162
|
|
|
1873
|
-
// ../../node_modules/.pnpm/@noble+curves@1.
|
|
2163
|
+
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/hash-to-curve.js
|
|
1874
2164
|
var os2ip = bytesToNumberBE;
|
|
1875
2165
|
function i2osp(value, length) {
|
|
1876
2166
|
anum(value);
|
|
@@ -1900,7 +2190,7 @@ function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
|
1900
2190
|
abytes2(DST);
|
|
1901
2191
|
anum(lenInBytes);
|
|
1902
2192
|
if (DST.length > 255)
|
|
1903
|
-
DST = H(concatBytes2(
|
|
2193
|
+
DST = H(concatBytes2(utf8ToBytes2("H2C-OVERSIZE-DST-"), DST));
|
|
1904
2194
|
const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
|
|
1905
2195
|
const ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
1906
2196
|
if (lenInBytes > 65535 || ell > 255)
|
|
@@ -1924,7 +2214,7 @@ function expand_message_xof(msg, DST, lenInBytes, k, H) {
|
|
|
1924
2214
|
anum(lenInBytes);
|
|
1925
2215
|
if (DST.length > 255) {
|
|
1926
2216
|
const dkLen = Math.ceil(2 * k / 8);
|
|
1927
|
-
DST = H.create({ dkLen }).update(
|
|
2217
|
+
DST = H.create({ dkLen }).update(utf8ToBytes2("H2C-OVERSIZE-DST-")).update(DST).digest();
|
|
1928
2218
|
}
|
|
1929
2219
|
if (lenInBytes > 65535 || DST.length > 255)
|
|
1930
2220
|
throw new Error("expand_message_xof: invalid lenInBytes");
|
|
@@ -1941,7 +2231,7 @@ function hash_to_field(msg, count, options) {
|
|
|
1941
2231
|
const { p, k, m, hash, expand, DST: _DST } = options;
|
|
1942
2232
|
abytes2(msg);
|
|
1943
2233
|
anum(count);
|
|
1944
|
-
const DST = typeof _DST === "string" ?
|
|
2234
|
+
const DST = typeof _DST === "string" ? utf8ToBytes2(_DST) : _DST;
|
|
1945
2235
|
const log2p = p.toString(2).length;
|
|
1946
2236
|
const L = Math.ceil((log2p + k) / 8);
|
|
1947
2237
|
const len_in_bytes = count * m * L;
|
|
@@ -1968,63 +2258,56 @@ function hash_to_field(msg, count, options) {
|
|
|
1968
2258
|
return u;
|
|
1969
2259
|
}
|
|
1970
2260
|
function isogenyMap(field, map) {
|
|
1971
|
-
const
|
|
2261
|
+
const COEFF = map.map((i) => Array.from(i).reverse());
|
|
1972
2262
|
return (x, y) => {
|
|
1973
|
-
const [
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
y = field.mul(y, field.mul(yn, yd_inv));
|
|
2263
|
+
const [xNum, xDen, yNum, yDen] = COEFF.map((val) => val.reduce((acc, i) => field.add(field.mul(acc, x), i)));
|
|
2264
|
+
x = field.div(xNum, xDen);
|
|
2265
|
+
y = field.mul(y, field.div(yNum, yDen));
|
|
1977
2266
|
return { x, y };
|
|
1978
2267
|
};
|
|
1979
2268
|
}
|
|
1980
|
-
function
|
|
2269
|
+
function createHasher(Point2, mapToCurve, def) {
|
|
1981
2270
|
if (typeof mapToCurve !== "function")
|
|
1982
2271
|
throw new Error("mapToCurve() must be defined");
|
|
1983
|
-
function map(num2) {
|
|
1984
|
-
return Point2.fromAffine(mapToCurve(num2));
|
|
1985
|
-
}
|
|
1986
|
-
function clear(initial) {
|
|
1987
|
-
const P = initial.clearCofactor();
|
|
1988
|
-
if (P.equals(Point2.ZERO))
|
|
1989
|
-
return Point2.ZERO;
|
|
1990
|
-
P.assertValidity();
|
|
1991
|
-
return P;
|
|
1992
|
-
}
|
|
1993
2272
|
return {
|
|
1994
|
-
defaults,
|
|
1995
2273
|
// Encodes byte string to elliptic curve.
|
|
1996
2274
|
// hash_to_curve from https://www.rfc-editor.org/rfc/rfc9380#section-3
|
|
1997
2275
|
hashToCurve(msg, options) {
|
|
1998
|
-
const u = hash_to_field(msg, 2, { ...
|
|
1999
|
-
const u0 =
|
|
2000
|
-
const u1 =
|
|
2001
|
-
|
|
2276
|
+
const u = hash_to_field(msg, 2, { ...def, DST: def.DST, ...options });
|
|
2277
|
+
const u0 = Point2.fromAffine(mapToCurve(u[0]));
|
|
2278
|
+
const u1 = Point2.fromAffine(mapToCurve(u[1]));
|
|
2279
|
+
const P = u0.add(u1).clearCofactor();
|
|
2280
|
+
P.assertValidity();
|
|
2281
|
+
return P;
|
|
2002
2282
|
},
|
|
2003
2283
|
// Encodes byte string to elliptic curve.
|
|
2004
2284
|
// encode_to_curve from https://www.rfc-editor.org/rfc/rfc9380#section-3
|
|
2005
2285
|
encodeToCurve(msg, options) {
|
|
2006
|
-
const u = hash_to_field(msg, 1, { ...
|
|
2007
|
-
|
|
2286
|
+
const u = hash_to_field(msg, 1, { ...def, DST: def.encodeDST, ...options });
|
|
2287
|
+
const P = Point2.fromAffine(mapToCurve(u[0])).clearCofactor();
|
|
2288
|
+
P.assertValidity();
|
|
2289
|
+
return P;
|
|
2008
2290
|
},
|
|
2009
2291
|
// Same as encodeToCurve, but without hash
|
|
2010
2292
|
mapToCurve(scalars) {
|
|
2011
2293
|
if (!Array.isArray(scalars))
|
|
2012
|
-
throw new Error("expected array of bigints");
|
|
2294
|
+
throw new Error("mapToCurve: expected array of bigints");
|
|
2013
2295
|
for (const i of scalars)
|
|
2014
2296
|
if (typeof i !== "bigint")
|
|
2015
|
-
throw new Error("expected array of bigints");
|
|
2016
|
-
|
|
2297
|
+
throw new Error("mapToCurve: expected array of bigints");
|
|
2298
|
+
const P = Point2.fromAffine(mapToCurve(scalars)).clearCofactor();
|
|
2299
|
+
P.assertValidity();
|
|
2300
|
+
return P;
|
|
2017
2301
|
}
|
|
2018
2302
|
};
|
|
2019
2303
|
}
|
|
2020
2304
|
|
|
2021
|
-
// ../../node_modules/.pnpm/@noble+curves@1.
|
|
2305
|
+
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/secp256k1.js
|
|
2022
2306
|
var secp256k1P = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f");
|
|
2023
2307
|
var secp256k1N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
|
|
2024
|
-
var
|
|
2025
|
-
var
|
|
2026
|
-
var
|
|
2027
|
-
var divNearest = (a, b) => (a + b / _2n3) / b;
|
|
2308
|
+
var _1n5 = BigInt(1);
|
|
2309
|
+
var _2n4 = BigInt(2);
|
|
2310
|
+
var divNearest = (a, b) => (a + b / _2n4) / b;
|
|
2028
2311
|
function sqrtMod(y) {
|
|
2029
2312
|
const P = secp256k1P;
|
|
2030
2313
|
const _3n3 = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
@@ -2033,7 +2316,7 @@ function sqrtMod(y) {
|
|
|
2033
2316
|
const b3 = b2 * b2 * y % P;
|
|
2034
2317
|
const b6 = pow2(b3, _3n3, P) * b3 % P;
|
|
2035
2318
|
const b9 = pow2(b6, _3n3, P) * b3 % P;
|
|
2036
|
-
const b11 = pow2(b9,
|
|
2319
|
+
const b11 = pow2(b9, _2n4, P) * b2 % P;
|
|
2037
2320
|
const b22 = pow2(b11, _11n, P) * b11 % P;
|
|
2038
2321
|
const b44 = pow2(b22, _22n, P) * b22 % P;
|
|
2039
2322
|
const b88 = pow2(b44, _44n, P) * b44 % P;
|
|
@@ -2042,29 +2325,40 @@ function sqrtMod(y) {
|
|
|
2042
2325
|
const b223 = pow2(b220, _3n3, P) * b3 % P;
|
|
2043
2326
|
const t1 = pow2(b223, _23n, P) * b22 % P;
|
|
2044
2327
|
const t2 = pow2(t1, _6n, P) * b2 % P;
|
|
2045
|
-
const root = pow2(t2,
|
|
2328
|
+
const root = pow2(t2, _2n4, P);
|
|
2046
2329
|
if (!Fpk1.eql(Fpk1.sqr(root), y))
|
|
2047
2330
|
throw new Error("Cannot find square root");
|
|
2048
2331
|
return root;
|
|
2049
2332
|
}
|
|
2050
2333
|
var Fpk1 = Field(secp256k1P, void 0, void 0, { sqrt: sqrtMod });
|
|
2051
2334
|
var secp256k1 = createCurve({
|
|
2052
|
-
a:
|
|
2335
|
+
a: BigInt(0),
|
|
2336
|
+
// equation params: a, b
|
|
2053
2337
|
b: BigInt(7),
|
|
2338
|
+
// Seem to be rigid: bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975
|
|
2054
2339
|
Fp: Fpk1,
|
|
2340
|
+
// Field's prime: 2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n
|
|
2055
2341
|
n: secp256k1N,
|
|
2342
|
+
// Curve order, total count of valid points in the field
|
|
2343
|
+
// Base point (x, y) aka generator point
|
|
2056
2344
|
Gx: BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),
|
|
2057
2345
|
Gy: BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),
|
|
2058
2346
|
h: BigInt(1),
|
|
2347
|
+
// Cofactor
|
|
2059
2348
|
lowS: true,
|
|
2060
2349
|
// Allow only low-S signatures by default in sign() and verify()
|
|
2350
|
+
/**
|
|
2351
|
+
* secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
|
|
2352
|
+
* Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
|
|
2353
|
+
* For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
|
|
2354
|
+
* Explanation: https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
|
|
2355
|
+
*/
|
|
2061
2356
|
endo: {
|
|
2062
|
-
// Endomorphism, see above
|
|
2063
2357
|
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
2064
2358
|
splitScalar: (k) => {
|
|
2065
2359
|
const n = secp256k1N;
|
|
2066
2360
|
const a1 = BigInt("0x3086d221a7d46bcde86c90e49284eb15");
|
|
2067
|
-
const b1 = -
|
|
2361
|
+
const b1 = -_1n5 * BigInt("0xe4437ed6010e88286f547fa90abfe4c3");
|
|
2068
2362
|
const a2 = BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8");
|
|
2069
2363
|
const b2 = a1;
|
|
2070
2364
|
const POW_2_128 = BigInt("0x100000000000000000000000000000000");
|
|
@@ -2085,6 +2379,7 @@ var secp256k1 = createCurve({
|
|
|
2085
2379
|
}
|
|
2086
2380
|
}
|
|
2087
2381
|
}, sha256);
|
|
2382
|
+
var _0n5 = BigInt(0);
|
|
2088
2383
|
var TAGGED_HASH_PREFIXES = {};
|
|
2089
2384
|
function taggedHash(tag, ...messages) {
|
|
2090
2385
|
let tagP = TAGGED_HASH_PREFIXES[tag];
|
|
@@ -2099,7 +2394,7 @@ var pointToBytes = (point) => point.toRawBytes(true).slice(1);
|
|
|
2099
2394
|
var numTo32b = (n) => numberToBytesBE(n, 32);
|
|
2100
2395
|
var modP = (x) => mod(x, secp256k1P);
|
|
2101
2396
|
var modN = (x) => mod(x, secp256k1N);
|
|
2102
|
-
var Point =
|
|
2397
|
+
var Point = secp256k1.ProjectivePoint;
|
|
2103
2398
|
var GmulAdd = (Q, a, b) => Point.BASE.multiplyAndAddUnsafe(Q, a, b);
|
|
2104
2399
|
function schnorrGetExtPubKey(priv) {
|
|
2105
2400
|
let d_ = secp256k1.utils.normPrivateKeyToScalar(priv);
|
|
@@ -2108,13 +2403,13 @@ function schnorrGetExtPubKey(priv) {
|
|
|
2108
2403
|
return { scalar, bytes: pointToBytes(p) };
|
|
2109
2404
|
}
|
|
2110
2405
|
function lift_x(x) {
|
|
2111
|
-
aInRange("x", x,
|
|
2406
|
+
aInRange("x", x, _1n5, secp256k1P);
|
|
2112
2407
|
const xx = modP(x * x);
|
|
2113
2408
|
const c = modP(xx * x + BigInt(7));
|
|
2114
2409
|
let y = sqrtMod(c);
|
|
2115
|
-
if (y %
|
|
2410
|
+
if (y % _2n4 !== _0n5)
|
|
2116
2411
|
y = modP(-y);
|
|
2117
|
-
const p = new Point(x, y,
|
|
2412
|
+
const p = new Point(x, y, _1n5);
|
|
2118
2413
|
p.assertValidity();
|
|
2119
2414
|
return p;
|
|
2120
2415
|
}
|
|
@@ -2132,7 +2427,7 @@ function schnorrSign(message, privateKey, auxRand = randomBytes(32)) {
|
|
|
2132
2427
|
const t = numTo32b(d ^ num(taggedHash("BIP0340/aux", a)));
|
|
2133
2428
|
const rand = taggedHash("BIP0340/nonce", t, px, m);
|
|
2134
2429
|
const k_ = modN(num(rand));
|
|
2135
|
-
if (k_ ===
|
|
2430
|
+
if (k_ === _0n5)
|
|
2136
2431
|
throw new Error("sign failed: k is zero");
|
|
2137
2432
|
const { bytes: rx, scalar: k } = schnorrGetExtPubKey(k_);
|
|
2138
2433
|
const e = challenge(rx, px, m);
|
|
@@ -2150,10 +2445,10 @@ function schnorrVerify(signature, message, publicKey) {
|
|
|
2150
2445
|
try {
|
|
2151
2446
|
const P = lift_x(num(pub));
|
|
2152
2447
|
const r = num(sig.subarray(0, 32));
|
|
2153
|
-
if (!inRange(r,
|
|
2448
|
+
if (!inRange(r, _1n5, secp256k1P))
|
|
2154
2449
|
return false;
|
|
2155
2450
|
const s = num(sig.subarray(32, 64));
|
|
2156
|
-
if (!inRange(s,
|
|
2451
|
+
if (!inRange(s, _1n5, secp256k1N))
|
|
2157
2452
|
return false;
|
|
2158
2453
|
const e = challenge(numTo32b(r), pointToBytes(P), m);
|
|
2159
2454
|
const R = GmulAdd(P, s, modN(-e));
|
|
@@ -2214,7 +2509,7 @@ var mapSWU = /* @__PURE__ */ (() => mapToCurveSimpleSWU(Fpk1, {
|
|
|
2214
2509
|
B: BigInt("1771"),
|
|
2215
2510
|
Z: Fpk1.create(BigInt("-11"))
|
|
2216
2511
|
}))();
|
|
2217
|
-
var
|
|
2512
|
+
var htf = /* @__PURE__ */ (() => createHasher(secp256k1.ProjectivePoint, (scalars) => {
|
|
2218
2513
|
const { x, y } = mapSWU(Fpk1.create(scalars[0]));
|
|
2219
2514
|
return isoMap(x, y);
|
|
2220
2515
|
}, {
|
|
@@ -2226,22 +2521,35 @@ var secp256k1_hasher = /* @__PURE__ */ (() => createHasher2(secp256k1.Projective
|
|
|
2226
2521
|
expand: "xmd",
|
|
2227
2522
|
hash: sha256
|
|
2228
2523
|
}))();
|
|
2229
|
-
var hashToCurve = /* @__PURE__ */ (() =>
|
|
2230
|
-
var encodeToCurve = /* @__PURE__ */ (() =>
|
|
2524
|
+
var hashToCurve = /* @__PURE__ */ (() => htf.hashToCurve)();
|
|
2525
|
+
var encodeToCurve = /* @__PURE__ */ (() => htf.encodeToCurve)();
|
|
2231
2526
|
export {
|
|
2232
2527
|
encodeToCurve,
|
|
2233
2528
|
hashToCurve,
|
|
2234
2529
|
schnorr,
|
|
2235
|
-
secp256k1
|
|
2236
|
-
secp256k1_hasher
|
|
2530
|
+
secp256k1
|
|
2237
2531
|
};
|
|
2238
2532
|
/*! Bundled license information:
|
|
2239
2533
|
|
|
2534
|
+
@noble/hashes/esm/utils.js:
|
|
2535
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2536
|
+
|
|
2537
|
+
@noble/curves/esm/abstract/utils.js:
|
|
2538
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2539
|
+
|
|
2240
2540
|
@noble/curves/esm/abstract/modular.js:
|
|
2541
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2542
|
+
|
|
2241
2543
|
@noble/curves/esm/abstract/curve.js:
|
|
2544
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2545
|
+
|
|
2242
2546
|
@noble/curves/esm/abstract/weierstrass.js:
|
|
2547
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2548
|
+
|
|
2243
2549
|
@noble/curves/esm/_shortw_utils.js:
|
|
2550
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2551
|
+
|
|
2244
2552
|
@noble/curves/esm/secp256k1.js:
|
|
2245
2553
|
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2246
2554
|
*/
|
|
2247
|
-
//# sourceMappingURL=secp256k1-
|
|
2555
|
+
//# sourceMappingURL=secp256k1-QZA5SALG.js.map
|