@btc-vision/transaction 1.7.25 → 1.7.26
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/browser/_version.d.ts +1 -1
- package/browser/btc-vision-bitcoin.js +12 -13
- package/browser/index.js +456 -443
- package/browser/noble-curves.js +1045 -1016
- package/browser/noble-hashes.js +918 -728
- package/browser/vendors.js +11418 -15799
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/_version.ts +1 -1
- package/src/crypto/crypto-browser.js +9 -16
- package/vite.config.browser.ts +30 -29
- package/browser/bip39.js +0 -204
- package/browser/bitcoin-utils.js +0 -3172
- package/browser/btc-vision-bip32.js +0 -805
- package/browser/btc-vision-logger.js +0 -86
- package/browser/btc-vision-post-quantum.js +0 -542
- package/browser/polyfills.js +0 -4952
- package/browser/scure-base.js +0 -410
- package/webpack.config.js +0 -220
package/browser/noble-curves.js
CHANGED
|
@@ -1,9 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { b as We, c as ln, d as dn, e as Me, i as yt, h as Qe, f as hn, g as Ae, j as yn, k as mn, l as kt, m as bn, n as wn } from "./noble-hashes.js";
|
|
2
|
+
function er(e) {
|
|
3
|
+
return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
|
|
4
|
+
}
|
|
5
|
+
function tr(e) {
|
|
6
|
+
if (Object.prototype.hasOwnProperty.call(e, "__esModule")) return e;
|
|
7
|
+
var n = e.default;
|
|
8
|
+
if (typeof n == "function") {
|
|
9
|
+
var c = function t() {
|
|
10
|
+
var u = !1;
|
|
11
|
+
try {
|
|
12
|
+
u = this instanceof t;
|
|
13
|
+
} catch {
|
|
14
|
+
}
|
|
15
|
+
return u ? Reflect.construct(n, arguments, this.constructor) : n.apply(this, arguments);
|
|
16
|
+
};
|
|
17
|
+
c.prototype = n.prototype;
|
|
18
|
+
} else c = {};
|
|
19
|
+
return Object.defineProperty(c, "__esModule", { value: !0 }), Object.keys(e).forEach(function(t) {
|
|
20
|
+
var u = Object.getOwnPropertyDescriptor(e, t);
|
|
21
|
+
Object.defineProperty(c, t, u.get ? u : {
|
|
22
|
+
enumerable: !0,
|
|
23
|
+
get: function() {
|
|
24
|
+
return e[t];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}), c;
|
|
28
|
+
}
|
|
2
29
|
var tt = {}, Ke = {}, nt = {}, rt = {}, _t;
|
|
3
30
|
function Ce() {
|
|
4
31
|
return _t || (_t = 1, (function(e) {
|
|
5
|
-
Object.defineProperty(e, "__esModule", { value: !0 }), e.notImplemented = e.bitMask = e.utf8ToBytes = e.randomBytes = e.isBytes = e.hexToBytes = e.concatBytes = e.bytesToUtf8 = e.bytesToHex = e.anumber = e.abytes = void 0, e.abool = m, e._abool2 = g, e._abytes2 = p, e.numberToHexUnpadded = v, e.hexToNumber = U, e.bytesToNumberBE = H, e.bytesToNumberLE = V, e.numberToBytesBE = E, e.numberToBytesLE = b, e.numberToVarBytesBE = S, e.ensureBytes = A, e.equalBytes = M, e.copyBytes = Q, e.asciiToBytes = F, e.inRange = se, e.aInRange = ie, e.bitLen = j, e.bitGet =
|
|
6
|
-
const
|
|
32
|
+
Object.defineProperty(e, "__esModule", { value: !0 }), e.notImplemented = e.bitMask = e.utf8ToBytes = e.randomBytes = e.isBytes = e.hexToBytes = e.concatBytes = e.bytesToUtf8 = e.bytesToHex = e.anumber = e.abytes = void 0, e.abool = m, e._abool2 = g, e._abytes2 = p, e.numberToHexUnpadded = v, e.hexToNumber = U, e.bytesToNumberBE = H, e.bytesToNumberLE = V, e.numberToBytesBE = E, e.numberToBytesLE = b, e.numberToVarBytesBE = S, e.ensureBytes = A, e.equalBytes = M, e.copyBytes = Q, e.asciiToBytes = F, e.inRange = se, e.aInRange = ie, e.bitLen = j, e.bitGet = R, e.bitSet = L, e.createHmacDrbg = K, e.validateObject = D, e.isHash = d, e._validateObject = l, e.memoized = a;
|
|
33
|
+
const n = /* @__PURE__ */ We();
|
|
7
34
|
var c = /* @__PURE__ */ We();
|
|
8
35
|
Object.defineProperty(e, "abytes", { enumerable: !0, get: function() {
|
|
9
36
|
return c.abytes;
|
|
@@ -24,7 +51,7 @@ function Ce() {
|
|
|
24
51
|
} }), Object.defineProperty(e, "utf8ToBytes", { enumerable: !0, get: function() {
|
|
25
52
|
return c.utf8ToBytes;
|
|
26
53
|
} });
|
|
27
|
-
const t = /* @__PURE__ */ BigInt(0),
|
|
54
|
+
const t = /* @__PURE__ */ BigInt(0), u = /* @__PURE__ */ BigInt(1);
|
|
28
55
|
function m(i, o) {
|
|
29
56
|
if (typeof o != "boolean")
|
|
30
57
|
throw new Error(i + " boolean expected, got " + o);
|
|
@@ -37,10 +64,10 @@ function Ce() {
|
|
|
37
64
|
return i;
|
|
38
65
|
}
|
|
39
66
|
function p(i, o, h = "") {
|
|
40
|
-
const w = (0,
|
|
67
|
+
const w = (0, n.isBytes)(i), s = i?.length, f = o !== void 0;
|
|
41
68
|
if (!w || f && s !== o) {
|
|
42
|
-
const y = h && `"${h}" `, B = f ? ` of length ${o}` : "",
|
|
43
|
-
throw new Error(y + "expected Uint8Array" + B + ", got " +
|
|
69
|
+
const y = h && `"${h}" `, B = f ? ` of length ${o}` : "", O = w ? `length=${s}` : `type=${typeof i}`;
|
|
70
|
+
throw new Error(y + "expected Uint8Array" + B + ", got " + O);
|
|
44
71
|
}
|
|
45
72
|
return i;
|
|
46
73
|
}
|
|
@@ -54,29 +81,29 @@ function Ce() {
|
|
|
54
81
|
return i === "" ? t : BigInt("0x" + i);
|
|
55
82
|
}
|
|
56
83
|
function H(i) {
|
|
57
|
-
return U((0,
|
|
84
|
+
return U((0, n.bytesToHex)(i));
|
|
58
85
|
}
|
|
59
86
|
function V(i) {
|
|
60
|
-
return (0,
|
|
87
|
+
return (0, n.abytes)(i), U((0, n.bytesToHex)(Uint8Array.from(i).reverse()));
|
|
61
88
|
}
|
|
62
89
|
function E(i, o) {
|
|
63
|
-
return (0,
|
|
90
|
+
return (0, n.hexToBytes)(i.toString(16).padStart(o * 2, "0"));
|
|
64
91
|
}
|
|
65
92
|
function b(i, o) {
|
|
66
93
|
return E(i, o).reverse();
|
|
67
94
|
}
|
|
68
95
|
function S(i) {
|
|
69
|
-
return (0,
|
|
96
|
+
return (0, n.hexToBytes)(v(i));
|
|
70
97
|
}
|
|
71
98
|
function A(i, o, h) {
|
|
72
99
|
let w;
|
|
73
100
|
if (typeof o == "string")
|
|
74
101
|
try {
|
|
75
|
-
w = (0,
|
|
102
|
+
w = (0, n.hexToBytes)(o);
|
|
76
103
|
} catch (f) {
|
|
77
104
|
throw new Error(i + " must be hex string or Uint8Array, cause: " + f);
|
|
78
105
|
}
|
|
79
|
-
else if ((0,
|
|
106
|
+
else if ((0, n.isBytes)(o))
|
|
80
107
|
w = Uint8Array.from(o);
|
|
81
108
|
else
|
|
82
109
|
throw new Error(i + " must be hex string or Uint8Array");
|
|
@@ -114,17 +141,17 @@ function Ce() {
|
|
|
114
141
|
}
|
|
115
142
|
function j(i) {
|
|
116
143
|
let o;
|
|
117
|
-
for (o = 0; i > t; i >>=
|
|
144
|
+
for (o = 0; i > t; i >>= u, o += 1)
|
|
118
145
|
;
|
|
119
146
|
return o;
|
|
120
147
|
}
|
|
121
|
-
function
|
|
122
|
-
return i >> BigInt(o) &
|
|
148
|
+
function R(i, o) {
|
|
149
|
+
return i >> BigInt(o) & u;
|
|
123
150
|
}
|
|
124
151
|
function L(i, o, h) {
|
|
125
|
-
return i | (h ?
|
|
152
|
+
return i | (h ? u : t) << BigInt(o);
|
|
126
153
|
}
|
|
127
|
-
const _ = (i) => (
|
|
154
|
+
const _ = (i) => (u << BigInt(i)) - u;
|
|
128
155
|
e.bitMask = _;
|
|
129
156
|
function K(i, o, h) {
|
|
130
157
|
if (typeof i != "number" || i < 2)
|
|
@@ -135,7 +162,7 @@ function Ce() {
|
|
|
135
162
|
throw new Error("hmacFn must be a function");
|
|
136
163
|
const w = (C) => new Uint8Array(C), s = (C) => Uint8Array.of(C);
|
|
137
164
|
let f = w(i), y = w(i), B = 0;
|
|
138
|
-
const
|
|
165
|
+
const O = () => {
|
|
139
166
|
f.fill(1), y.fill(0), B = 0;
|
|
140
167
|
}, k = (...C) => h(y, f, ...C), Y = (C = w(0)) => {
|
|
141
168
|
y = k(s(0), C), f = k(), C.length !== 0 && (y = k(s(1), C), f = k());
|
|
@@ -149,14 +176,14 @@ function Ce() {
|
|
|
149
176
|
const $ = f.slice();
|
|
150
177
|
te.push($), C += f.length;
|
|
151
178
|
}
|
|
152
|
-
return (0,
|
|
179
|
+
return (0, n.concatBytes)(...te);
|
|
153
180
|
};
|
|
154
181
|
return (C, te) => {
|
|
155
|
-
|
|
182
|
+
O(), Y(C);
|
|
156
183
|
let $;
|
|
157
184
|
for (; !($ = te(I())); )
|
|
158
185
|
Y();
|
|
159
|
-
return
|
|
186
|
+
return O(), $;
|
|
160
187
|
};
|
|
161
188
|
}
|
|
162
189
|
const W = {
|
|
@@ -164,20 +191,20 @@ function Ce() {
|
|
|
164
191
|
function: (i) => typeof i == "function",
|
|
165
192
|
boolean: (i) => typeof i == "boolean",
|
|
166
193
|
string: (i) => typeof i == "string",
|
|
167
|
-
stringOrUint8Array: (i) => typeof i == "string" || (0,
|
|
194
|
+
stringOrUint8Array: (i) => typeof i == "string" || (0, n.isBytes)(i),
|
|
168
195
|
isSafeInteger: (i) => Number.isSafeInteger(i),
|
|
169
196
|
array: (i) => Array.isArray(i),
|
|
170
197
|
field: (i, o) => o.Fp.isValid(i),
|
|
171
198
|
hash: (i) => typeof i == "function" && Number.isSafeInteger(i.outputLen)
|
|
172
199
|
};
|
|
173
|
-
function
|
|
200
|
+
function D(i, o, h = {}) {
|
|
174
201
|
const w = (s, f, y) => {
|
|
175
202
|
const B = W[f];
|
|
176
203
|
if (typeof B != "function")
|
|
177
204
|
throw new Error("invalid validator function");
|
|
178
|
-
const
|
|
179
|
-
if (!(y &&
|
|
180
|
-
throw new Error("param " + String(s) + " is invalid. Expected " + f + ", got " +
|
|
205
|
+
const O = i[s];
|
|
206
|
+
if (!(y && O === void 0) && !B(O, i))
|
|
207
|
+
throw new Error("param " + String(s) + " is invalid. Expected " + f + ", got " + O);
|
|
181
208
|
};
|
|
182
209
|
for (const [s, f] of Object.entries(o))
|
|
183
210
|
w(s, f, !1);
|
|
@@ -188,23 +215,23 @@ function Ce() {
|
|
|
188
215
|
function d(i) {
|
|
189
216
|
return typeof i == "function" && Number.isSafeInteger(i.outputLen);
|
|
190
217
|
}
|
|
191
|
-
function
|
|
218
|
+
function l(i, o, h = {}) {
|
|
192
219
|
if (!i || typeof i != "object")
|
|
193
220
|
throw new Error("expected valid options object");
|
|
194
221
|
function w(s, f, y) {
|
|
195
222
|
const B = i[s];
|
|
196
223
|
if (y && B === void 0)
|
|
197
224
|
return;
|
|
198
|
-
const
|
|
199
|
-
if (
|
|
200
|
-
throw new Error(`param "${s}" is invalid: expected ${f}, got ${
|
|
225
|
+
const O = typeof B;
|
|
226
|
+
if (O !== f || B === null)
|
|
227
|
+
throw new Error(`param "${s}" is invalid: expected ${f}, got ${O}`);
|
|
201
228
|
}
|
|
202
229
|
Object.entries(o).forEach(([s, f]) => w(s, f, !1)), Object.entries(h).forEach(([s, f]) => w(s, f, !0));
|
|
203
230
|
}
|
|
204
|
-
const
|
|
231
|
+
const r = () => {
|
|
205
232
|
throw new Error("not implemented");
|
|
206
233
|
};
|
|
207
|
-
e.notImplemented =
|
|
234
|
+
e.notImplemented = r;
|
|
208
235
|
function a(i) {
|
|
209
236
|
const o = /* @__PURE__ */ new WeakMap();
|
|
210
237
|
return (h, ...w) => {
|
|
@@ -217,37 +244,37 @@ function Ce() {
|
|
|
217
244
|
}
|
|
218
245
|
})(rt)), rt;
|
|
219
246
|
}
|
|
220
|
-
var
|
|
247
|
+
var Re = {}, he = {}, St;
|
|
221
248
|
function $e() {
|
|
222
249
|
if (St) return he;
|
|
223
|
-
St = 1, Object.defineProperty(he, "__esModule", { value: !0 }), he.isNegativeLE = void 0, he.mod = V, he.pow = E, he.pow2 = b, he.invert = S, he.tonelliShanks = G, he.FpSqrt = se, he.validateField =
|
|
224
|
-
const e = /* @__PURE__ */ Ce(),
|
|
250
|
+
St = 1, Object.defineProperty(he, "__esModule", { value: !0 }), he.isNegativeLE = void 0, he.mod = V, he.pow = E, he.pow2 = b, he.invert = S, he.tonelliShanks = G, he.FpSqrt = se, he.validateField = R, he.FpPow = L, he.FpInvertBatch = _, he.FpDiv = K, he.FpLegendre = W, he.FpIsSquare = D, he.nLength = d, he.Field = l, he.FpSqrtOdd = r, he.FpSqrtEven = a, he.hashToPrivateScalar = i, he.getFieldBytesLength = o, he.getMinHashLength = h, he.mapHashToField = w;
|
|
251
|
+
const e = /* @__PURE__ */ Ce(), n = BigInt(0), c = BigInt(1), t = /* @__PURE__ */ BigInt(2), u = /* @__PURE__ */ BigInt(3), m = /* @__PURE__ */ BigInt(4), g = /* @__PURE__ */ BigInt(5), p = /* @__PURE__ */ BigInt(7), v = /* @__PURE__ */ BigInt(8), U = /* @__PURE__ */ BigInt(9), H = /* @__PURE__ */ BigInt(16);
|
|
225
252
|
function V(s, f) {
|
|
226
253
|
const y = s % f;
|
|
227
|
-
return y >=
|
|
254
|
+
return y >= n ? y : f + y;
|
|
228
255
|
}
|
|
229
256
|
function E(s, f, y) {
|
|
230
|
-
return L(
|
|
257
|
+
return L(l(y), s, f);
|
|
231
258
|
}
|
|
232
259
|
function b(s, f, y) {
|
|
233
260
|
let B = s;
|
|
234
|
-
for (; f-- >
|
|
261
|
+
for (; f-- > n; )
|
|
235
262
|
B *= B, B %= y;
|
|
236
263
|
return B;
|
|
237
264
|
}
|
|
238
265
|
function S(s, f) {
|
|
239
|
-
if (s ===
|
|
266
|
+
if (s === n)
|
|
240
267
|
throw new Error("invert: expected non-zero number");
|
|
241
|
-
if (f <=
|
|
268
|
+
if (f <= n)
|
|
242
269
|
throw new Error("invert: expected positive modulus, got " + f);
|
|
243
|
-
let y = V(s, f), B = f,
|
|
244
|
-
for (; y !==
|
|
245
|
-
const I = B / y, q = B % y, C =
|
|
246
|
-
B = y, y = q,
|
|
270
|
+
let y = V(s, f), B = f, O = n, k = c;
|
|
271
|
+
for (; y !== n; ) {
|
|
272
|
+
const I = B / y, q = B % y, C = O - k * I;
|
|
273
|
+
B = y, y = q, O = k, k = C;
|
|
247
274
|
}
|
|
248
275
|
if (B !== c)
|
|
249
276
|
throw new Error("invert: does not exist");
|
|
250
|
-
return V(
|
|
277
|
+
return V(O, f);
|
|
251
278
|
}
|
|
252
279
|
function A(s, f, y) {
|
|
253
280
|
if (!s.eql(s.sqr(f), y))
|
|
@@ -258,55 +285,55 @@ function $e() {
|
|
|
258
285
|
return A(s, B, f), B;
|
|
259
286
|
}
|
|
260
287
|
function Q(s, f) {
|
|
261
|
-
const y = (s.ORDER - g) / v, B = s.mul(f, t),
|
|
288
|
+
const y = (s.ORDER - g) / v, B = s.mul(f, t), O = s.pow(B, y), k = s.mul(f, O), Y = s.mul(s.mul(k, t), O), I = s.mul(k, s.sub(Y, s.ONE));
|
|
262
289
|
return A(s, I, f), I;
|
|
263
290
|
}
|
|
264
291
|
function F(s) {
|
|
265
|
-
const f =
|
|
292
|
+
const f = l(s), y = G(s), B = y(f, f.neg(f.ONE)), O = y(f, B), k = y(f, f.neg(B)), Y = (s + p) / H;
|
|
266
293
|
return (I, q) => {
|
|
267
294
|
let C = I.pow(q, Y), te = I.mul(C, B);
|
|
268
|
-
const $ = I.mul(C,
|
|
269
|
-
C = I.cmov(C, te, X), te = I.cmov(
|
|
295
|
+
const $ = I.mul(C, O), T = I.mul(C, k), X = I.eql(I.sqr(te), q), ae = I.eql(I.sqr($), q);
|
|
296
|
+
C = I.cmov(C, te, X), te = I.cmov(T, $, ae);
|
|
270
297
|
const ye = I.eql(I.sqr(te), q), pe = I.cmov(C, te, ye);
|
|
271
298
|
return A(I, pe, q), pe;
|
|
272
299
|
};
|
|
273
300
|
}
|
|
274
301
|
function G(s) {
|
|
275
|
-
if (s <
|
|
302
|
+
if (s < u)
|
|
276
303
|
throw new Error("sqrt is not defined for small field");
|
|
277
304
|
let f = s - c, y = 0;
|
|
278
|
-
for (; f % t ===
|
|
305
|
+
for (; f % t === n; )
|
|
279
306
|
f /= t, y++;
|
|
280
307
|
let B = t;
|
|
281
|
-
const
|
|
282
|
-
for (; W(
|
|
308
|
+
const O = l(s);
|
|
309
|
+
for (; W(O, B) === 1; )
|
|
283
310
|
if (B++ > 1e3)
|
|
284
311
|
throw new Error("Cannot find square root: probably non-prime P");
|
|
285
312
|
if (y === 1)
|
|
286
313
|
return M;
|
|
287
|
-
let k =
|
|
314
|
+
let k = O.pow(B, f);
|
|
288
315
|
const Y = (f + c) / t;
|
|
289
316
|
return function(q, C) {
|
|
290
317
|
if (q.is0(C))
|
|
291
318
|
return C;
|
|
292
319
|
if (W(q, C) !== 1)
|
|
293
320
|
throw new Error("Cannot find square root");
|
|
294
|
-
let te = y, $ = q.mul(q.ONE, k),
|
|
295
|
-
for (; !q.eql(
|
|
296
|
-
if (q.is0(
|
|
321
|
+
let te = y, $ = q.mul(q.ONE, k), T = q.pow(C, f), X = q.pow(C, Y);
|
|
322
|
+
for (; !q.eql(T, q.ONE); ) {
|
|
323
|
+
if (q.is0(T))
|
|
297
324
|
return q.ZERO;
|
|
298
|
-
let ae = 1, ye = q.sqr(
|
|
325
|
+
let ae = 1, ye = q.sqr(T);
|
|
299
326
|
for (; !q.eql(ye, q.ONE); )
|
|
300
327
|
if (ae++, ye = q.sqr(ye), ae === te)
|
|
301
328
|
throw new Error("Cannot find square root");
|
|
302
329
|
const pe = c << BigInt(te - ae - 1), ve = q.pow($, pe);
|
|
303
|
-
te = ae, $ = q.sqr(ve),
|
|
330
|
+
te = ae, $ = q.sqr(ve), T = q.mul(T, $), X = q.mul(X, ve);
|
|
304
331
|
}
|
|
305
332
|
return X;
|
|
306
333
|
};
|
|
307
334
|
}
|
|
308
335
|
function se(s) {
|
|
309
|
-
return s % m ===
|
|
336
|
+
return s % m === u ? M : s % v === g ? Q : s % H === U ? F(s) : G(s);
|
|
310
337
|
}
|
|
311
338
|
const ie = (s, f) => (V(s, f) & c) === c;
|
|
312
339
|
he.isNegativeLE = ie;
|
|
@@ -329,41 +356,41 @@ function $e() {
|
|
|
329
356
|
"mulN",
|
|
330
357
|
"sqrN"
|
|
331
358
|
];
|
|
332
|
-
function
|
|
359
|
+
function R(s) {
|
|
333
360
|
const f = {
|
|
334
361
|
ORDER: "bigint",
|
|
335
362
|
MASK: "bigint",
|
|
336
363
|
BYTES: "number",
|
|
337
364
|
BITS: "number"
|
|
338
|
-
}, y = j.reduce((B,
|
|
365
|
+
}, y = j.reduce((B, O) => (B[O] = "function", B), f);
|
|
339
366
|
return (0, e._validateObject)(s, y), s;
|
|
340
367
|
}
|
|
341
368
|
function L(s, f, y) {
|
|
342
|
-
if (y <
|
|
369
|
+
if (y < n)
|
|
343
370
|
throw new Error("invalid exponent, negatives unsupported");
|
|
344
|
-
if (y ===
|
|
371
|
+
if (y === n)
|
|
345
372
|
return s.ONE;
|
|
346
373
|
if (y === c)
|
|
347
374
|
return f;
|
|
348
|
-
let B = s.ONE,
|
|
349
|
-
for (; y >
|
|
350
|
-
y & c && (B = s.mul(B,
|
|
375
|
+
let B = s.ONE, O = f;
|
|
376
|
+
for (; y > n; )
|
|
377
|
+
y & c && (B = s.mul(B, O)), O = s.sqr(O), y >>= c;
|
|
351
378
|
return B;
|
|
352
379
|
}
|
|
353
380
|
function _(s, f, y = !1) {
|
|
354
|
-
const B = new Array(f.length).fill(y ? s.ZERO : void 0),
|
|
381
|
+
const B = new Array(f.length).fill(y ? s.ZERO : void 0), O = f.reduce((Y, I, q) => s.is0(I) ? Y : (B[q] = Y, s.mul(Y, I)), s.ONE), k = s.inv(O);
|
|
355
382
|
return f.reduceRight((Y, I, q) => s.is0(I) ? Y : (B[q] = s.mul(Y, B[q]), s.mul(Y, I)), k), B;
|
|
356
383
|
}
|
|
357
384
|
function K(s, f, y) {
|
|
358
385
|
return s.mul(f, typeof y == "bigint" ? S(y, s.ORDER) : s.inv(y));
|
|
359
386
|
}
|
|
360
387
|
function W(s, f) {
|
|
361
|
-
const y = (s.ORDER - c) / t, B = s.pow(f, y),
|
|
362
|
-
if (!
|
|
388
|
+
const y = (s.ORDER - c) / t, B = s.pow(f, y), O = s.eql(B, s.ONE), k = s.eql(B, s.ZERO), Y = s.eql(B, s.neg(s.ONE));
|
|
389
|
+
if (!O && !k && !Y)
|
|
363
390
|
throw new Error("invalid Legendre symbol result");
|
|
364
|
-
return
|
|
391
|
+
return O ? 1 : k ? 0 : -1;
|
|
365
392
|
}
|
|
366
|
-
function
|
|
393
|
+
function D(s, f) {
|
|
367
394
|
return W(s, f) === 1;
|
|
368
395
|
}
|
|
369
396
|
function d(s, f) {
|
|
@@ -371,18 +398,18 @@ function $e() {
|
|
|
371
398
|
const y = f !== void 0 ? f : s.toString(2).length, B = Math.ceil(y / 8);
|
|
372
399
|
return { nBitLength: y, nByteLength: B };
|
|
373
400
|
}
|
|
374
|
-
function
|
|
375
|
-
if (s <=
|
|
401
|
+
function l(s, f, y = !1, B = {}) {
|
|
402
|
+
if (s <= n)
|
|
376
403
|
throw new Error("invalid field: expected ORDER > 0, got " + s);
|
|
377
|
-
let
|
|
404
|
+
let O, k, Y = !1, I;
|
|
378
405
|
if (typeof f == "object" && f != null) {
|
|
379
406
|
if (B.sqrt || y)
|
|
380
407
|
throw new Error("cannot specify opts in two arguments");
|
|
381
|
-
const
|
|
382
|
-
|
|
408
|
+
const T = f;
|
|
409
|
+
T.BITS && (O = T.BITS), T.sqrt && (k = T.sqrt), typeof T.isLE == "boolean" && (y = T.isLE), typeof T.modFromBytes == "boolean" && (Y = T.modFromBytes), I = T.allowedLengths;
|
|
383
410
|
} else
|
|
384
|
-
typeof f == "number" && (
|
|
385
|
-
const { nBitLength: q, nByteLength: C } = d(s,
|
|
411
|
+
typeof f == "number" && (O = f), B.sqrt && (k = B.sqrt);
|
|
412
|
+
const { nBitLength: q, nByteLength: C } = d(s, O);
|
|
386
413
|
if (C > 2048)
|
|
387
414
|
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
388
415
|
let te;
|
|
@@ -392,58 +419,58 @@ function $e() {
|
|
|
392
419
|
BITS: q,
|
|
393
420
|
BYTES: C,
|
|
394
421
|
MASK: (0, e.bitMask)(q),
|
|
395
|
-
ZERO:
|
|
422
|
+
ZERO: n,
|
|
396
423
|
ONE: c,
|
|
397
424
|
allowedLengths: I,
|
|
398
|
-
create: (
|
|
399
|
-
isValid: (
|
|
400
|
-
if (typeof
|
|
401
|
-
throw new Error("invalid field element: expected bigint, got " + typeof
|
|
402
|
-
return
|
|
425
|
+
create: (T) => V(T, s),
|
|
426
|
+
isValid: (T) => {
|
|
427
|
+
if (typeof T != "bigint")
|
|
428
|
+
throw new Error("invalid field element: expected bigint, got " + typeof T);
|
|
429
|
+
return n <= T && T < s;
|
|
403
430
|
},
|
|
404
|
-
is0: (
|
|
431
|
+
is0: (T) => T === n,
|
|
405
432
|
// is valid and invertible
|
|
406
|
-
isValidNot0: (
|
|
407
|
-
isOdd: (
|
|
408
|
-
neg: (
|
|
409
|
-
eql: (
|
|
410
|
-
sqr: (
|
|
411
|
-
add: (
|
|
412
|
-
sub: (
|
|
413
|
-
mul: (
|
|
414
|
-
pow: (
|
|
415
|
-
div: (
|
|
433
|
+
isValidNot0: (T) => !$.is0(T) && $.isValid(T),
|
|
434
|
+
isOdd: (T) => (T & c) === c,
|
|
435
|
+
neg: (T) => V(-T, s),
|
|
436
|
+
eql: (T, X) => T === X,
|
|
437
|
+
sqr: (T) => V(T * T, s),
|
|
438
|
+
add: (T, X) => V(T + X, s),
|
|
439
|
+
sub: (T, X) => V(T - X, s),
|
|
440
|
+
mul: (T, X) => V(T * X, s),
|
|
441
|
+
pow: (T, X) => L($, T, X),
|
|
442
|
+
div: (T, X) => V(T * S(X, s), s),
|
|
416
443
|
// Same as above, but doesn't normalize
|
|
417
|
-
sqrN: (
|
|
418
|
-
addN: (
|
|
419
|
-
subN: (
|
|
420
|
-
mulN: (
|
|
421
|
-
inv: (
|
|
422
|
-
sqrt: k || ((
|
|
423
|
-
toBytes: (
|
|
424
|
-
fromBytes: (
|
|
444
|
+
sqrN: (T) => T * T,
|
|
445
|
+
addN: (T, X) => T + X,
|
|
446
|
+
subN: (T, X) => T - X,
|
|
447
|
+
mulN: (T, X) => T * X,
|
|
448
|
+
inv: (T) => S(T, s),
|
|
449
|
+
sqrt: k || ((T) => (te || (te = se(s)), te($, T))),
|
|
450
|
+
toBytes: (T) => y ? (0, e.numberToBytesLE)(T, C) : (0, e.numberToBytesBE)(T, C),
|
|
451
|
+
fromBytes: (T, X = !0) => {
|
|
425
452
|
if (I) {
|
|
426
|
-
if (!I.includes(
|
|
427
|
-
throw new Error("Field.fromBytes: expected " + I + " bytes, got " +
|
|
453
|
+
if (!I.includes(T.length) || T.length > C)
|
|
454
|
+
throw new Error("Field.fromBytes: expected " + I + " bytes, got " + T.length);
|
|
428
455
|
const ye = new Uint8Array(C);
|
|
429
|
-
ye.set(
|
|
456
|
+
ye.set(T, y ? 0 : ye.length - T.length), T = ye;
|
|
430
457
|
}
|
|
431
|
-
if (
|
|
432
|
-
throw new Error("Field.fromBytes: expected " + C + " bytes, got " +
|
|
433
|
-
let ae = y ? (0, e.bytesToNumberLE)(
|
|
458
|
+
if (T.length !== C)
|
|
459
|
+
throw new Error("Field.fromBytes: expected " + C + " bytes, got " + T.length);
|
|
460
|
+
let ae = y ? (0, e.bytesToNumberLE)(T) : (0, e.bytesToNumberBE)(T);
|
|
434
461
|
if (Y && (ae = V(ae, s)), !X && !$.isValid(ae))
|
|
435
462
|
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
436
463
|
return ae;
|
|
437
464
|
},
|
|
438
465
|
// TODO: we don't need it here, move out to separate fn
|
|
439
|
-
invertBatch: (
|
|
466
|
+
invertBatch: (T) => _($, T),
|
|
440
467
|
// We can't move this out because Fp6, Fp12 implement it
|
|
441
468
|
// and it's unclear what to return in there.
|
|
442
|
-
cmov: (
|
|
469
|
+
cmov: (T, X, ae) => ae ? X : T
|
|
443
470
|
});
|
|
444
471
|
return Object.freeze($);
|
|
445
472
|
}
|
|
446
|
-
function
|
|
473
|
+
function r(s, f) {
|
|
447
474
|
if (!s.isOdd)
|
|
448
475
|
throw new Error("Field doesn't have isOdd");
|
|
449
476
|
const y = s.sqrt(f);
|
|
@@ -457,9 +484,9 @@ function $e() {
|
|
|
457
484
|
}
|
|
458
485
|
function i(s, f, y = !1) {
|
|
459
486
|
s = (0, e.ensureBytes)("privateHash", s);
|
|
460
|
-
const B = s.length,
|
|
461
|
-
if (
|
|
462
|
-
throw new Error("hashToPrivateScalar: expected " +
|
|
487
|
+
const B = s.length, O = d(f).nByteLength + 8;
|
|
488
|
+
if (O < 24 || B < O || B > 1024)
|
|
489
|
+
throw new Error("hashToPrivateScalar: expected " + O + "-1024 bytes of input, got " + B);
|
|
463
490
|
const k = y ? (0, e.bytesToNumberLE)(s) : (0, e.bytesToNumberBE)(s);
|
|
464
491
|
return V(k, f - c) + c;
|
|
465
492
|
}
|
|
@@ -474,56 +501,56 @@ function $e() {
|
|
|
474
501
|
return f + Math.ceil(f / 2);
|
|
475
502
|
}
|
|
476
503
|
function w(s, f, y = !1) {
|
|
477
|
-
const B = s.length,
|
|
504
|
+
const B = s.length, O = o(f), k = h(f);
|
|
478
505
|
if (B < 16 || B < k || B > 1024)
|
|
479
506
|
throw new Error("expected " + k + "-1024 bytes of input, got " + B);
|
|
480
507
|
const Y = y ? (0, e.bytesToNumberLE)(s) : (0, e.bytesToNumberBE)(s), I = V(Y, f - c) + c;
|
|
481
|
-
return y ? (0, e.numberToBytesLE)(I,
|
|
508
|
+
return y ? (0, e.numberToBytesLE)(I, O) : (0, e.numberToBytesBE)(I, O);
|
|
482
509
|
}
|
|
483
510
|
return he;
|
|
484
511
|
}
|
|
485
|
-
var
|
|
512
|
+
var Ot;
|
|
486
513
|
function gn() {
|
|
487
|
-
if (
|
|
488
|
-
|
|
489
|
-
const e = /* @__PURE__ */ Ce(),
|
|
490
|
-
function
|
|
491
|
-
const L =
|
|
492
|
-
return j ? L :
|
|
493
|
-
}
|
|
494
|
-
function m(j,
|
|
495
|
-
const L = (0,
|
|
496
|
-
return
|
|
497
|
-
}
|
|
498
|
-
function g(j,
|
|
499
|
-
if (!Number.isSafeInteger(j) || j <= 0 || j >
|
|
500
|
-
throw new Error("invalid window size, expected [1.." +
|
|
501
|
-
}
|
|
502
|
-
function p(j,
|
|
503
|
-
g(j,
|
|
504
|
-
const L = Math.ceil(
|
|
505
|
-
return { windows: L, windowSize: _, mask: W, maxNumber: K, shiftBy:
|
|
506
|
-
}
|
|
507
|
-
function v(j,
|
|
508
|
-
const { windowSize: _, mask: K, maxNumber: W, shiftBy:
|
|
509
|
-
let d = Number(j & K),
|
|
510
|
-
d > _ && (d -= W,
|
|
511
|
-
const
|
|
512
|
-
return { nextN:
|
|
513
|
-
}
|
|
514
|
-
function U(j,
|
|
514
|
+
if (Ot) return Re;
|
|
515
|
+
Ot = 1, Object.defineProperty(Re, "__esModule", { value: !0 }), Re.wNAF = void 0, Re.negateCt = u, Re.normalizeZ = m, Re.mulEndoUnsafe = M, Re.pippenger = Q, Re.precomputeMSMUnsafe = F, Re.validateBasic = G, Re._createCurveFields = ie;
|
|
516
|
+
const e = /* @__PURE__ */ Ce(), n = /* @__PURE__ */ $e(), c = BigInt(0), t = BigInt(1);
|
|
517
|
+
function u(j, R) {
|
|
518
|
+
const L = R.negate();
|
|
519
|
+
return j ? L : R;
|
|
520
|
+
}
|
|
521
|
+
function m(j, R) {
|
|
522
|
+
const L = (0, n.FpInvertBatch)(j.Fp, R.map((_) => _.Z));
|
|
523
|
+
return R.map((_, K) => j.fromAffine(_.toAffine(L[K])));
|
|
524
|
+
}
|
|
525
|
+
function g(j, R) {
|
|
526
|
+
if (!Number.isSafeInteger(j) || j <= 0 || j > R)
|
|
527
|
+
throw new Error("invalid window size, expected [1.." + R + "], got W=" + j);
|
|
528
|
+
}
|
|
529
|
+
function p(j, R) {
|
|
530
|
+
g(j, R);
|
|
531
|
+
const L = Math.ceil(R / j) + 1, _ = 2 ** (j - 1), K = 2 ** j, W = (0, e.bitMask)(j), D = BigInt(j);
|
|
532
|
+
return { windows: L, windowSize: _, mask: W, maxNumber: K, shiftBy: D };
|
|
533
|
+
}
|
|
534
|
+
function v(j, R, L) {
|
|
535
|
+
const { windowSize: _, mask: K, maxNumber: W, shiftBy: D } = L;
|
|
536
|
+
let d = Number(j & K), l = j >> D;
|
|
537
|
+
d > _ && (d -= W, l += t);
|
|
538
|
+
const r = R * _, a = r + Math.abs(d) - 1, i = d === 0, o = d < 0, h = R % 2 !== 0;
|
|
539
|
+
return { nextN: l, offset: a, isZero: i, isNeg: o, isNegF: h, offsetF: r };
|
|
540
|
+
}
|
|
541
|
+
function U(j, R) {
|
|
515
542
|
if (!Array.isArray(j))
|
|
516
543
|
throw new Error("array expected");
|
|
517
544
|
j.forEach((L, _) => {
|
|
518
|
-
if (!(L instanceof
|
|
545
|
+
if (!(L instanceof R))
|
|
519
546
|
throw new Error("invalid point at index " + _);
|
|
520
547
|
});
|
|
521
548
|
}
|
|
522
|
-
function H(j,
|
|
549
|
+
function H(j, R) {
|
|
523
550
|
if (!Array.isArray(j))
|
|
524
551
|
throw new Error("array of scalars expected");
|
|
525
552
|
j.forEach((L, _) => {
|
|
526
|
-
if (!
|
|
553
|
+
if (!R.isValid(L))
|
|
527
554
|
throw new Error("invalid scalar at index " + _);
|
|
528
555
|
});
|
|
529
556
|
}
|
|
@@ -537,12 +564,12 @@ function gn() {
|
|
|
537
564
|
}
|
|
538
565
|
class A {
|
|
539
566
|
// Parametrized with a given Point class (not individual point)
|
|
540
|
-
constructor(
|
|
541
|
-
this.BASE =
|
|
567
|
+
constructor(R, L) {
|
|
568
|
+
this.BASE = R.BASE, this.ZERO = R.ZERO, this.Fn = R.Fn, this.bits = L;
|
|
542
569
|
}
|
|
543
570
|
// non-const time multiplication ladder
|
|
544
|
-
_unsafeLadder(
|
|
545
|
-
let K =
|
|
571
|
+
_unsafeLadder(R, L, _ = this.ZERO) {
|
|
572
|
+
let K = R;
|
|
546
573
|
for (; L > c; )
|
|
547
574
|
L & t && (_ = _.add(K)), K = K.double(), L >>= t;
|
|
548
575
|
return _;
|
|
@@ -559,14 +586,14 @@ function gn() {
|
|
|
559
586
|
* @param W window size
|
|
560
587
|
* @returns precomputed point tables flattened to a single array
|
|
561
588
|
*/
|
|
562
|
-
precomputeWindow(
|
|
589
|
+
precomputeWindow(R, L) {
|
|
563
590
|
const { windows: _, windowSize: K } = p(L, this.bits), W = [];
|
|
564
|
-
let
|
|
565
|
-
for (let
|
|
566
|
-
d =
|
|
567
|
-
for (let
|
|
568
|
-
d = d.add(
|
|
569
|
-
|
|
591
|
+
let D = R, d = D;
|
|
592
|
+
for (let l = 0; l < _; l++) {
|
|
593
|
+
d = D, W.push(d);
|
|
594
|
+
for (let r = 1; r < K; r++)
|
|
595
|
+
d = d.add(D), W.push(d);
|
|
596
|
+
D = d.double();
|
|
570
597
|
}
|
|
571
598
|
return W;
|
|
572
599
|
}
|
|
@@ -576,14 +603,14 @@ function gn() {
|
|
|
576
603
|
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
577
604
|
* @returns real and fake (for const-time) points
|
|
578
605
|
*/
|
|
579
|
-
wNAF(
|
|
606
|
+
wNAF(R, L, _) {
|
|
580
607
|
if (!this.Fn.isValid(_))
|
|
581
608
|
throw new Error("invalid scalar");
|
|
582
609
|
let K = this.ZERO, W = this.BASE;
|
|
583
|
-
const
|
|
584
|
-
for (let d = 0; d <
|
|
585
|
-
const { nextN:
|
|
586
|
-
_ =
|
|
610
|
+
const D = p(R, this.bits);
|
|
611
|
+
for (let d = 0; d < D.windows; d++) {
|
|
612
|
+
const { nextN: l, offset: r, isZero: a, isNeg: i, isNegF: o, offsetF: h } = v(_, d, D);
|
|
613
|
+
_ = l, a ? W = W.add(u(o, L[h])) : K = K.add(u(i, L[r]));
|
|
587
614
|
}
|
|
588
615
|
return S(_), { p: K, f: W };
|
|
589
616
|
}
|
|
@@ -592,98 +619,98 @@ function gn() {
|
|
|
592
619
|
* @param acc accumulator point to add result of multiplication
|
|
593
620
|
* @returns point
|
|
594
621
|
*/
|
|
595
|
-
wNAFUnsafe(
|
|
596
|
-
const W = p(
|
|
597
|
-
for (let
|
|
598
|
-
const { nextN: d, offset:
|
|
599
|
-
if (_ = d, !
|
|
600
|
-
const i = L[
|
|
622
|
+
wNAFUnsafe(R, L, _, K = this.ZERO) {
|
|
623
|
+
const W = p(R, this.bits);
|
|
624
|
+
for (let D = 0; D < W.windows && _ !== c; D++) {
|
|
625
|
+
const { nextN: d, offset: l, isZero: r, isNeg: a } = v(_, D, W);
|
|
626
|
+
if (_ = d, !r) {
|
|
627
|
+
const i = L[l];
|
|
601
628
|
K = K.add(a ? i.negate() : i);
|
|
602
629
|
}
|
|
603
630
|
}
|
|
604
631
|
return S(_), K;
|
|
605
632
|
}
|
|
606
|
-
getPrecomputes(
|
|
633
|
+
getPrecomputes(R, L, _) {
|
|
607
634
|
let K = V.get(L);
|
|
608
|
-
return K || (K = this.precomputeWindow(L,
|
|
635
|
+
return K || (K = this.precomputeWindow(L, R), R !== 1 && (typeof _ == "function" && (K = _(K)), V.set(L, K))), K;
|
|
609
636
|
}
|
|
610
|
-
cached(
|
|
611
|
-
const K = b(
|
|
612
|
-
return this.wNAF(K, this.getPrecomputes(K,
|
|
637
|
+
cached(R, L, _) {
|
|
638
|
+
const K = b(R);
|
|
639
|
+
return this.wNAF(K, this.getPrecomputes(K, R, _), L);
|
|
613
640
|
}
|
|
614
|
-
unsafe(
|
|
615
|
-
const W = b(
|
|
616
|
-
return W === 1 ? this._unsafeLadder(
|
|
641
|
+
unsafe(R, L, _, K) {
|
|
642
|
+
const W = b(R);
|
|
643
|
+
return W === 1 ? this._unsafeLadder(R, L, K) : this.wNAFUnsafe(W, this.getPrecomputes(W, R, _), L, K);
|
|
617
644
|
}
|
|
618
645
|
// We calculate precomputes for elliptic curve point multiplication
|
|
619
646
|
// using windowed method. This specifies window size and
|
|
620
647
|
// stores precomputed values. Usually only base point would be precomputed.
|
|
621
|
-
createCache(
|
|
622
|
-
g(L, this.bits), E.set(
|
|
648
|
+
createCache(R, L) {
|
|
649
|
+
g(L, this.bits), E.set(R, L), V.delete(R);
|
|
623
650
|
}
|
|
624
|
-
hasCache(
|
|
625
|
-
return b(
|
|
651
|
+
hasCache(R) {
|
|
652
|
+
return b(R) !== 1;
|
|
626
653
|
}
|
|
627
654
|
}
|
|
628
|
-
|
|
629
|
-
function M(j,
|
|
630
|
-
let K =
|
|
655
|
+
Re.wNAF = A;
|
|
656
|
+
function M(j, R, L, _) {
|
|
657
|
+
let K = R, W = j.ZERO, D = j.ZERO;
|
|
631
658
|
for (; L > c || _ > c; )
|
|
632
|
-
L & t && (W = W.add(K)), _ & t && (
|
|
633
|
-
return { p1: W, p2:
|
|
659
|
+
L & t && (W = W.add(K)), _ & t && (D = D.add(K)), K = K.double(), L >>= t, _ >>= t;
|
|
660
|
+
return { p1: W, p2: D };
|
|
634
661
|
}
|
|
635
|
-
function Q(j,
|
|
636
|
-
U(L, j), H(_,
|
|
662
|
+
function Q(j, R, L, _) {
|
|
663
|
+
U(L, j), H(_, R);
|
|
637
664
|
const K = L.length, W = _.length;
|
|
638
665
|
if (K !== W)
|
|
639
666
|
throw new Error("arrays of points and scalars must have equal length");
|
|
640
|
-
const
|
|
641
|
-
let
|
|
642
|
-
d > 12 ?
|
|
643
|
-
const
|
|
644
|
-
let o =
|
|
645
|
-
for (let h = i; h >= 0; h -=
|
|
646
|
-
a.fill(
|
|
667
|
+
const D = j.ZERO, d = (0, e.bitLen)(BigInt(K));
|
|
668
|
+
let l = 1;
|
|
669
|
+
d > 12 ? l = d - 3 : d > 4 ? l = d - 2 : d > 0 && (l = 2);
|
|
670
|
+
const r = (0, e.bitMask)(l), a = new Array(Number(r) + 1).fill(D), i = Math.floor((R.BITS - 1) / l) * l;
|
|
671
|
+
let o = D;
|
|
672
|
+
for (let h = i; h >= 0; h -= l) {
|
|
673
|
+
a.fill(D);
|
|
647
674
|
for (let s = 0; s < W; s++) {
|
|
648
|
-
const f = _[s], y = Number(f >> BigInt(h) &
|
|
675
|
+
const f = _[s], y = Number(f >> BigInt(h) & r);
|
|
649
676
|
a[y] = a[y].add(L[s]);
|
|
650
677
|
}
|
|
651
|
-
let w =
|
|
652
|
-
for (let s = a.length - 1, f =
|
|
678
|
+
let w = D;
|
|
679
|
+
for (let s = a.length - 1, f = D; s > 0; s--)
|
|
653
680
|
f = f.add(a[s]), w = w.add(f);
|
|
654
681
|
if (o = o.add(w), h !== 0)
|
|
655
|
-
for (let s = 0; s <
|
|
682
|
+
for (let s = 0; s < l; s++)
|
|
656
683
|
o = o.double();
|
|
657
684
|
}
|
|
658
685
|
return o;
|
|
659
686
|
}
|
|
660
|
-
function F(j,
|
|
661
|
-
g(_,
|
|
662
|
-
const K = j.ZERO, W = 2 ** _ - 1,
|
|
687
|
+
function F(j, R, L, _) {
|
|
688
|
+
g(_, R.BITS), U(L, j);
|
|
689
|
+
const K = j.ZERO, W = 2 ** _ - 1, D = Math.ceil(R.BITS / _), d = (0, e.bitMask)(_), l = L.map((r) => {
|
|
663
690
|
const a = [];
|
|
664
|
-
for (let i = 0, o =
|
|
665
|
-
a.push(o), o = o.add(
|
|
691
|
+
for (let i = 0, o = r; i < W; i++)
|
|
692
|
+
a.push(o), o = o.add(r);
|
|
666
693
|
return a;
|
|
667
694
|
});
|
|
668
|
-
return (
|
|
669
|
-
if (H(
|
|
695
|
+
return (r) => {
|
|
696
|
+
if (H(r, R), r.length > L.length)
|
|
670
697
|
throw new Error("array of scalars must be smaller than array of points");
|
|
671
698
|
let a = K;
|
|
672
|
-
for (let i = 0; i <
|
|
699
|
+
for (let i = 0; i < D; i++) {
|
|
673
700
|
if (a !== K)
|
|
674
701
|
for (let h = 0; h < _; h++)
|
|
675
702
|
a = a.double();
|
|
676
|
-
const o = BigInt(
|
|
677
|
-
for (let h = 0; h <
|
|
678
|
-
const w =
|
|
679
|
-
s && (a = a.add(
|
|
703
|
+
const o = BigInt(D * _ - (i + 1) * _);
|
|
704
|
+
for (let h = 0; h < r.length; h++) {
|
|
705
|
+
const w = r[h], s = Number(w >> o & d);
|
|
706
|
+
s && (a = a.add(l[h][s - 1]));
|
|
680
707
|
}
|
|
681
708
|
}
|
|
682
709
|
return a;
|
|
683
710
|
};
|
|
684
711
|
}
|
|
685
712
|
function G(j) {
|
|
686
|
-
return (0,
|
|
713
|
+
return (0, n.validateField)(j.Fp), (0, e.validateObject)(j, {
|
|
687
714
|
n: "bigint",
|
|
688
715
|
h: "bigint",
|
|
689
716
|
Gx: "field",
|
|
@@ -692,59 +719,59 @@ function gn() {
|
|
|
692
719
|
nBitLength: "isSafeInteger",
|
|
693
720
|
nByteLength: "isSafeInteger"
|
|
694
721
|
}), Object.freeze({
|
|
695
|
-
...(0,
|
|
722
|
+
...(0, n.nLength)(j.n, j.nBitLength),
|
|
696
723
|
...j,
|
|
697
724
|
p: j.Fp.ORDER
|
|
698
725
|
});
|
|
699
726
|
}
|
|
700
|
-
function se(j,
|
|
701
|
-
if (
|
|
702
|
-
if (
|
|
727
|
+
function se(j, R, L) {
|
|
728
|
+
if (R) {
|
|
729
|
+
if (R.ORDER !== j)
|
|
703
730
|
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
704
|
-
return (0,
|
|
731
|
+
return (0, n.validateField)(R), R;
|
|
705
732
|
} else
|
|
706
|
-
return (0,
|
|
733
|
+
return (0, n.Field)(j, { isLE: L });
|
|
707
734
|
}
|
|
708
|
-
function ie(j,
|
|
709
|
-
if (_ === void 0 && (_ = j === "edwards"), !
|
|
735
|
+
function ie(j, R, L = {}, _) {
|
|
736
|
+
if (_ === void 0 && (_ = j === "edwards"), !R || typeof R != "object")
|
|
710
737
|
throw new Error(`expected valid ${j} CURVE object`);
|
|
711
|
-
for (const
|
|
712
|
-
const
|
|
713
|
-
if (!(typeof
|
|
714
|
-
throw new Error(`CURVE.${
|
|
738
|
+
for (const l of ["p", "n", "h"]) {
|
|
739
|
+
const r = R[l];
|
|
740
|
+
if (!(typeof r == "bigint" && r > c))
|
|
741
|
+
throw new Error(`CURVE.${l} must be positive bigint`);
|
|
715
742
|
}
|
|
716
|
-
const K = se(
|
|
717
|
-
for (const
|
|
718
|
-
if (!K.isValid(
|
|
719
|
-
throw new Error(`CURVE.${
|
|
720
|
-
return
|
|
743
|
+
const K = se(R.p, L.Fp, _), W = se(R.n, L.Fn, _), d = ["Gx", "Gy", "a", j === "weierstrass" ? "b" : "d"];
|
|
744
|
+
for (const l of d)
|
|
745
|
+
if (!K.isValid(R[l]))
|
|
746
|
+
throw new Error(`CURVE.${l} must be valid field element of CURVE.Fp`);
|
|
747
|
+
return R = Object.freeze(Object.assign({}, R)), { CURVE: R, Fp: K, Fn: W };
|
|
721
748
|
}
|
|
722
|
-
return
|
|
749
|
+
return Re;
|
|
723
750
|
}
|
|
724
|
-
var
|
|
751
|
+
var Tt;
|
|
725
752
|
function Ct() {
|
|
726
|
-
return
|
|
727
|
-
Object.defineProperty(e, "__esModule", { value: !0 }), e.DER = e.DERErr = void 0, e._splitEndoScalar = p, e._normFnElement = M, e.weierstrassN = Q, e.SWUFpSqrtRatio = G, e.mapToCurveSimpleSWU = se, e.ecdh = j, e.ecdsa =
|
|
728
|
-
const
|
|
729
|
-
function p(
|
|
730
|
-
const [[o, h], [w, s]] = a, f = g(s *
|
|
731
|
-
let B =
|
|
732
|
-
const k = B < V, Y =
|
|
733
|
-
k && (B = -B), Y && (
|
|
753
|
+
return Tt || (Tt = 1, (function(e) {
|
|
754
|
+
Object.defineProperty(e, "__esModule", { value: !0 }), e.DER = e.DERErr = void 0, e._splitEndoScalar = p, e._normFnElement = M, e.weierstrassN = Q, e.SWUFpSqrtRatio = G, e.mapToCurveSimpleSWU = se, e.ecdh = j, e.ecdsa = R, e.weierstrassPoints = L, e._legacyHelperEquat = W, e.weierstrass = l;
|
|
755
|
+
const n = /* @__PURE__ */ ln(), c = /* @__PURE__ */ We(), t = /* @__PURE__ */ Ce(), u = /* @__PURE__ */ gn(), m = /* @__PURE__ */ $e(), g = (r, a) => (r + (r >= 0 ? a : -a) / b) / a;
|
|
756
|
+
function p(r, a, i) {
|
|
757
|
+
const [[o, h], [w, s]] = a, f = g(s * r, i), y = g(-h * r, i);
|
|
758
|
+
let B = r - f * o - y * w, O = -f * h - y * s;
|
|
759
|
+
const k = B < V, Y = O < V;
|
|
760
|
+
k && (B = -B), Y && (O = -O);
|
|
734
761
|
const I = (0, t.bitMask)(Math.ceil((0, t.bitLen)(i) / 2)) + E;
|
|
735
|
-
if (B < V || B >= I ||
|
|
736
|
-
throw new Error("splitScalar (endomorphism): failed, k=" +
|
|
737
|
-
return { k1neg: k, k1: B, k2neg: Y, k2:
|
|
762
|
+
if (B < V || B >= I || O < V || O >= I)
|
|
763
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + r);
|
|
764
|
+
return { k1neg: k, k1: B, k2neg: Y, k2: O };
|
|
738
765
|
}
|
|
739
|
-
function v(
|
|
740
|
-
if (!["compact", "recovered", "der"].includes(
|
|
766
|
+
function v(r) {
|
|
767
|
+
if (!["compact", "recovered", "der"].includes(r))
|
|
741
768
|
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
742
|
-
return
|
|
769
|
+
return r;
|
|
743
770
|
}
|
|
744
|
-
function U(
|
|
771
|
+
function U(r, a) {
|
|
745
772
|
const i = {};
|
|
746
773
|
for (let o of Object.keys(a))
|
|
747
|
-
i[o] =
|
|
774
|
+
i[o] = r[o] === void 0 ? a[o] : r[o];
|
|
748
775
|
return (0, t._abool2)(i.lowS, "lowS"), (0, t._abool2)(i.prehash, "prehash"), i.format !== void 0 && v(i.format), i;
|
|
749
776
|
}
|
|
750
777
|
class H extends Error {
|
|
@@ -757,9 +784,9 @@ function Ct() {
|
|
|
757
784
|
Err: H,
|
|
758
785
|
// Basic building block is TLV (Tag-Length-Value)
|
|
759
786
|
_tlv: {
|
|
760
|
-
encode: (
|
|
787
|
+
encode: (r, a) => {
|
|
761
788
|
const { Err: i } = e.DER;
|
|
762
|
-
if (
|
|
789
|
+
if (r < 0 || r > 256)
|
|
763
790
|
throw new i("tlv.encode: wrong tag");
|
|
764
791
|
if (a.length & 1)
|
|
765
792
|
throw new i("tlv.encode: unpadded data");
|
|
@@ -767,15 +794,15 @@ function Ct() {
|
|
|
767
794
|
if (h.length / 2 & 128)
|
|
768
795
|
throw new i("tlv.encode: long form length too big");
|
|
769
796
|
const w = o > 127 ? (0, t.numberToHexUnpadded)(h.length / 2 | 128) : "";
|
|
770
|
-
return (0, t.numberToHexUnpadded)(
|
|
797
|
+
return (0, t.numberToHexUnpadded)(r) + w + h + a;
|
|
771
798
|
},
|
|
772
799
|
// v - value, l - left bytes (unparsed)
|
|
773
|
-
decode(
|
|
800
|
+
decode(r, a) {
|
|
774
801
|
const { Err: i } = e.DER;
|
|
775
802
|
let o = 0;
|
|
776
|
-
if (
|
|
803
|
+
if (r < 0 || r > 256)
|
|
777
804
|
throw new i("tlv.encode: wrong tag");
|
|
778
|
-
if (a.length < 2 || a[o++] !==
|
|
805
|
+
if (a.length < 2 || a[o++] !== r)
|
|
779
806
|
throw new i("tlv.decode: wrong tlv");
|
|
780
807
|
const h = a[o++], w = !!(h & 128);
|
|
781
808
|
let s = 0;
|
|
@@ -792,8 +819,8 @@ function Ct() {
|
|
|
792
819
|
throw new i("tlv.decode: length bytes not complete");
|
|
793
820
|
if (B[0] === 0)
|
|
794
821
|
throw new i("tlv.decode(long): zero leftmost byte");
|
|
795
|
-
for (const
|
|
796
|
-
s = s << 8 |
|
|
822
|
+
for (const O of B)
|
|
823
|
+
s = s << 8 | O;
|
|
797
824
|
if (o += y, s < 128)
|
|
798
825
|
throw new i("tlv.decode(long): not minimal encoding");
|
|
799
826
|
}
|
|
@@ -808,58 +835,58 @@ function Ct() {
|
|
|
808
835
|
// - add zero byte if exists
|
|
809
836
|
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
810
837
|
_int: {
|
|
811
|
-
encode(
|
|
838
|
+
encode(r) {
|
|
812
839
|
const { Err: a } = e.DER;
|
|
813
|
-
if (
|
|
840
|
+
if (r < V)
|
|
814
841
|
throw new a("integer: negative integers are not allowed");
|
|
815
|
-
let i = (0, t.numberToHexUnpadded)(
|
|
842
|
+
let i = (0, t.numberToHexUnpadded)(r);
|
|
816
843
|
if (Number.parseInt(i[0], 16) & 8 && (i = "00" + i), i.length & 1)
|
|
817
844
|
throw new a("unexpected DER parsing assertion: unpadded hex");
|
|
818
845
|
return i;
|
|
819
846
|
},
|
|
820
|
-
decode(
|
|
847
|
+
decode(r) {
|
|
821
848
|
const { Err: a } = e.DER;
|
|
822
|
-
if (
|
|
849
|
+
if (r[0] & 128)
|
|
823
850
|
throw new a("invalid signature integer: negative");
|
|
824
|
-
if (
|
|
851
|
+
if (r[0] === 0 && !(r[1] & 128))
|
|
825
852
|
throw new a("invalid signature integer: unnecessary leading zero");
|
|
826
|
-
return (0, t.bytesToNumberBE)(
|
|
853
|
+
return (0, t.bytesToNumberBE)(r);
|
|
827
854
|
}
|
|
828
855
|
},
|
|
829
|
-
toSig(
|
|
830
|
-
const { Err: a, _int: i, _tlv: o } = e.DER, h = (0, t.ensureBytes)("signature",
|
|
856
|
+
toSig(r) {
|
|
857
|
+
const { Err: a, _int: i, _tlv: o } = e.DER, h = (0, t.ensureBytes)("signature", r), { v: w, l: s } = o.decode(48, h);
|
|
831
858
|
if (s.length)
|
|
832
859
|
throw new a("invalid signature: left bytes after parsing");
|
|
833
|
-
const { v: f, l: y } = o.decode(2, w), { v: B, l:
|
|
834
|
-
if (
|
|
860
|
+
const { v: f, l: y } = o.decode(2, w), { v: B, l: O } = o.decode(2, y);
|
|
861
|
+
if (O.length)
|
|
835
862
|
throw new a("invalid signature: left bytes after parsing");
|
|
836
863
|
return { r: i.decode(f), s: i.decode(B) };
|
|
837
864
|
},
|
|
838
|
-
hexFromSig(
|
|
839
|
-
const { _tlv: a, _int: i } = e.DER, o = a.encode(2, i.encode(
|
|
865
|
+
hexFromSig(r) {
|
|
866
|
+
const { _tlv: a, _int: i } = e.DER, o = a.encode(2, i.encode(r.r)), h = a.encode(2, i.encode(r.s)), w = o + h;
|
|
840
867
|
return a.encode(48, w);
|
|
841
868
|
}
|
|
842
869
|
};
|
|
843
870
|
const V = BigInt(0), E = BigInt(1), b = BigInt(2), S = BigInt(3), A = BigInt(4);
|
|
844
|
-
function M(
|
|
845
|
-
const { BYTES: i } =
|
|
871
|
+
function M(r, a) {
|
|
872
|
+
const { BYTES: i } = r;
|
|
846
873
|
let o;
|
|
847
874
|
if (typeof a == "bigint")
|
|
848
875
|
o = a;
|
|
849
876
|
else {
|
|
850
877
|
let h = (0, t.ensureBytes)("private key", a);
|
|
851
878
|
try {
|
|
852
|
-
o =
|
|
879
|
+
o = r.fromBytes(h);
|
|
853
880
|
} catch {
|
|
854
881
|
throw new Error(`invalid private key: expected ui8a of size ${i}, got ${typeof a}`);
|
|
855
882
|
}
|
|
856
883
|
}
|
|
857
|
-
if (!
|
|
884
|
+
if (!r.isValidNot0(o))
|
|
858
885
|
throw new Error("invalid private key: out of range [1..N-1]");
|
|
859
886
|
return o;
|
|
860
887
|
}
|
|
861
|
-
function Q(
|
|
862
|
-
const i = (0,
|
|
888
|
+
function Q(r, a = {}) {
|
|
889
|
+
const i = (0, u._createCurveFields)("weierstrass", r, a), { Fp: o, Fn: h } = i;
|
|
863
890
|
let w = i.CURVE;
|
|
864
891
|
const { h: s, n: f } = w;
|
|
865
892
|
(0, t._validateObject)(a, {}, {
|
|
@@ -875,23 +902,23 @@ function Ct() {
|
|
|
875
902
|
if (y && (!o.is0(w.a) || typeof y.beta != "bigint" || !Array.isArray(y.basises)))
|
|
876
903
|
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
877
904
|
const B = ie(o, h);
|
|
878
|
-
function
|
|
905
|
+
function O() {
|
|
879
906
|
if (!o.isOdd)
|
|
880
907
|
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
881
908
|
}
|
|
882
909
|
function k(le, Z, N) {
|
|
883
|
-
const { x
|
|
910
|
+
const { x, y: z } = Z.toAffine(), J = o.toBytes(x);
|
|
884
911
|
if ((0, t._abool2)(N, "isCompressed"), N) {
|
|
885
|
-
|
|
886
|
-
const re = !o.isOdd(
|
|
912
|
+
O();
|
|
913
|
+
const re = !o.isOdd(z);
|
|
887
914
|
return (0, t.concatBytes)(F(re), J);
|
|
888
915
|
} else
|
|
889
|
-
return (0, t.concatBytes)(Uint8Array.of(4), J, o.toBytes(
|
|
916
|
+
return (0, t.concatBytes)(Uint8Array.of(4), J, o.toBytes(z));
|
|
890
917
|
}
|
|
891
918
|
function Y(le) {
|
|
892
919
|
(0, t._abytes2)(le, void 0, "Point");
|
|
893
|
-
const { publicKey: Z, publicKeyUncompressed: N } = B,
|
|
894
|
-
if (
|
|
920
|
+
const { publicKey: Z, publicKeyUncompressed: N } = B, x = le.length, z = le[0], J = le.subarray(1);
|
|
921
|
+
if (x === Z && (z === 2 || z === 3)) {
|
|
895
922
|
const re = o.fromBytes(J);
|
|
896
923
|
if (!o.isValid(re))
|
|
897
924
|
throw new Error("bad point: is not on curve, wrong x");
|
|
@@ -903,16 +930,16 @@ function Ct() {
|
|
|
903
930
|
const de = we instanceof Error ? ": " + we.message : "";
|
|
904
931
|
throw new Error("bad point: is not on curve, sqrt error" + de);
|
|
905
932
|
}
|
|
906
|
-
|
|
933
|
+
O();
|
|
907
934
|
const oe = o.isOdd(ee);
|
|
908
|
-
return (
|
|
909
|
-
} else if (
|
|
935
|
+
return (z & 1) === 1 !== oe && (ee = o.neg(ee)), { x: re, y: ee };
|
|
936
|
+
} else if (x === N && z === 4) {
|
|
910
937
|
const re = o.BYTES, ne = o.fromBytes(J.subarray(0, re)), ee = o.fromBytes(J.subarray(re, re * 2));
|
|
911
938
|
if (!te(ne, ee))
|
|
912
939
|
throw new Error("bad point: is not on curve");
|
|
913
940
|
return { x: ne, y: ee };
|
|
914
941
|
} else
|
|
915
|
-
throw new Error(`bad point: got length ${
|
|
942
|
+
throw new Error(`bad point: got length ${x}, expected compressed=${Z} or uncompressed=${N}`);
|
|
916
943
|
}
|
|
917
944
|
const I = a.toBytes || k, q = a.fromBytes || Y;
|
|
918
945
|
function C(le) {
|
|
@@ -920,13 +947,13 @@ function Ct() {
|
|
|
920
947
|
return o.add(o.add(N, o.mul(le, w.a)), w.b);
|
|
921
948
|
}
|
|
922
949
|
function te(le, Z) {
|
|
923
|
-
const N = o.sqr(Z),
|
|
924
|
-
return o.eql(N,
|
|
950
|
+
const N = o.sqr(Z), x = C(le);
|
|
951
|
+
return o.eql(N, x);
|
|
925
952
|
}
|
|
926
953
|
if (!te(w.Gx, w.Gy))
|
|
927
954
|
throw new Error("bad curve params: generator point");
|
|
928
|
-
const $ = o.mul(o.pow(w.a, S), A),
|
|
929
|
-
if (o.is0(o.add($,
|
|
955
|
+
const $ = o.mul(o.pow(w.a, S), A), T = o.mul(o.sqr(w.b), BigInt(27));
|
|
956
|
+
if (o.is0(o.add($, T)))
|
|
930
957
|
throw new Error("bad curve params: a or b");
|
|
931
958
|
function X(le, Z, N = !1) {
|
|
932
959
|
if (!o.isValid(Z) || N && o.is0(Z))
|
|
@@ -943,12 +970,12 @@ function Ct() {
|
|
|
943
970
|
return p(le, y.basises, h.ORDER);
|
|
944
971
|
}
|
|
945
972
|
const pe = (0, t.memoized)((le, Z) => {
|
|
946
|
-
const { X: N, Y:
|
|
947
|
-
if (o.eql(
|
|
948
|
-
return { x: N, y:
|
|
973
|
+
const { X: N, Y: x, Z: z } = le;
|
|
974
|
+
if (o.eql(z, o.ONE))
|
|
975
|
+
return { x: N, y: x };
|
|
949
976
|
const J = le.is0();
|
|
950
|
-
Z == null && (Z = J ? o.ONE : o.inv(
|
|
951
|
-
const re = o.mul(N, Z), ne = o.mul(
|
|
977
|
+
Z == null && (Z = J ? o.ONE : o.inv(z));
|
|
978
|
+
const re = o.mul(N, Z), ne = o.mul(x, Z), ee = o.mul(z, Z);
|
|
952
979
|
if (J)
|
|
953
980
|
return { x: o.ZERO, y: o.ZERO };
|
|
954
981
|
if (!o.eql(ee, o.ONE))
|
|
@@ -969,25 +996,25 @@ function Ct() {
|
|
|
969
996
|
throw new Error("bad point: not in prime-order subgroup");
|
|
970
997
|
return !0;
|
|
971
998
|
});
|
|
972
|
-
function
|
|
973
|
-
return N = new fe(o.mul(N.X, le), N.Y, N.Z), Z = (0,
|
|
999
|
+
function Te(le, Z, N, x, z) {
|
|
1000
|
+
return N = new fe(o.mul(N.X, le), N.Y, N.Z), Z = (0, u.negateCt)(x, Z), N = (0, u.negateCt)(z, N), Z.add(N);
|
|
974
1001
|
}
|
|
975
1002
|
class fe {
|
|
976
1003
|
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
977
|
-
constructor(Z, N,
|
|
978
|
-
this.X = X("x", Z), this.Y = X("y", N, !0), this.Z = X("z",
|
|
1004
|
+
constructor(Z, N, x) {
|
|
1005
|
+
this.X = X("x", Z), this.Y = X("y", N, !0), this.Z = X("z", x), Object.freeze(this);
|
|
979
1006
|
}
|
|
980
1007
|
static CURVE() {
|
|
981
1008
|
return w;
|
|
982
1009
|
}
|
|
983
1010
|
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
984
1011
|
static fromAffine(Z) {
|
|
985
|
-
const { x: N, y:
|
|
986
|
-
if (!Z || !o.isValid(N) || !o.isValid(
|
|
1012
|
+
const { x: N, y: x } = Z || {};
|
|
1013
|
+
if (!Z || !o.isValid(N) || !o.isValid(x))
|
|
987
1014
|
throw new Error("invalid affine point");
|
|
988
1015
|
if (Z instanceof fe)
|
|
989
1016
|
throw new Error("projective point not allowed");
|
|
990
|
-
return o.is0(N) && o.is0(
|
|
1017
|
+
return o.is0(N) && o.is0(x) ? fe.ZERO : new fe(N, x, o.ONE);
|
|
991
1018
|
}
|
|
992
1019
|
static fromBytes(Z) {
|
|
993
1020
|
const N = fe.fromAffine(q((0, t._abytes2)(Z, void 0, "point")));
|
|
@@ -1025,7 +1052,7 @@ function Ct() {
|
|
|
1025
1052
|
/** Compare one point to another. */
|
|
1026
1053
|
equals(Z) {
|
|
1027
1054
|
ae(Z);
|
|
1028
|
-
const { X: N, Y:
|
|
1055
|
+
const { X: N, Y: x, Z: z } = this, { X: J, Y: re, Z: ne } = Z, ee = o.eql(o.mul(N, ne), o.mul(J, z)), oe = o.eql(o.mul(x, ne), o.mul(re, z));
|
|
1029
1056
|
return ee && oe;
|
|
1030
1057
|
}
|
|
1031
1058
|
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
@@ -1037,9 +1064,9 @@ function Ct() {
|
|
|
1037
1064
|
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
1038
1065
|
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
1039
1066
|
double() {
|
|
1040
|
-
const { a: Z, b: N } = w,
|
|
1041
|
-
let ne = o.ZERO, ee = o.ZERO, oe = o.ZERO, ce = o.mul(
|
|
1042
|
-
return ue = o.add(ue, ue), oe = o.mul(
|
|
1067
|
+
const { a: Z, b: N } = w, x = o.mul(N, S), { X: z, Y: J, Z: re } = this;
|
|
1068
|
+
let ne = o.ZERO, ee = o.ZERO, oe = o.ZERO, ce = o.mul(z, z), we = o.mul(J, J), de = o.mul(re, re), ue = o.mul(z, J);
|
|
1069
|
+
return ue = o.add(ue, ue), oe = o.mul(z, re), oe = o.add(oe, oe), ne = o.mul(Z, oe), ee = o.mul(x, de), ee = o.add(ne, ee), ne = o.sub(we, ee), ee = o.add(we, ee), ee = o.mul(ne, ee), ne = o.mul(ue, ne), oe = o.mul(x, oe), de = o.mul(Z, de), ue = o.sub(ce, de), ue = o.mul(Z, ue), ue = o.add(ue, oe), oe = o.add(ce, ce), ce = o.add(oe, ce), ce = o.add(ce, de), ce = o.mul(ce, ue), ee = o.add(ee, ce), de = o.mul(J, re), de = o.add(de, de), ce = o.mul(de, ue), ne = o.sub(ne, ce), oe = o.mul(de, we), oe = o.add(oe, oe), oe = o.add(oe, oe), new fe(ne, ee, oe);
|
|
1043
1070
|
}
|
|
1044
1071
|
// Renes-Costello-Batina exception-free addition formula.
|
|
1045
1072
|
// There is 30% faster Jacobian formula, but it is not complete.
|
|
@@ -1047,13 +1074,13 @@ function Ct() {
|
|
|
1047
1074
|
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
1048
1075
|
add(Z) {
|
|
1049
1076
|
ae(Z);
|
|
1050
|
-
const { X: N, Y:
|
|
1077
|
+
const { X: N, Y: x, Z: z } = this, { X: J, Y: re, Z: ne } = Z;
|
|
1051
1078
|
let ee = o.ZERO, oe = o.ZERO, ce = o.ZERO;
|
|
1052
1079
|
const we = w.a, de = o.mul(w.b, S);
|
|
1053
|
-
let ue = o.mul(N, J), me = o.mul(
|
|
1054
|
-
_e = o.mul(_e, be), be = o.add(ue, me), _e = o.sub(_e, be), be = o.add(N,
|
|
1080
|
+
let ue = o.mul(N, J), me = o.mul(x, re), ge = o.mul(z, ne), _e = o.add(N, x), be = o.add(J, re);
|
|
1081
|
+
_e = o.mul(_e, be), be = o.add(ue, me), _e = o.sub(_e, be), be = o.add(N, z);
|
|
1055
1082
|
let Ee = o.add(J, ne);
|
|
1056
|
-
return be = o.mul(be, Ee), Ee = o.add(ue, ge), be = o.sub(be, Ee), Ee = o.add(
|
|
1083
|
+
return be = o.mul(be, Ee), Ee = o.add(ue, ge), be = o.sub(be, Ee), Ee = o.add(x, z), ee = o.add(re, ne), Ee = o.mul(Ee, ee), ee = o.add(me, ge), Ee = o.sub(Ee, ee), ce = o.mul(we, be), ee = o.mul(de, ge), ce = o.add(ee, ce), ee = o.sub(me, ce), ce = o.add(me, ce), oe = o.mul(ee, ce), me = o.add(ue, ue), me = o.add(me, ue), ge = o.mul(we, ge), be = o.mul(de, be), me = o.add(me, ge), ge = o.sub(ue, ge), ge = o.mul(we, ge), be = o.add(be, ge), ue = o.mul(me, be), oe = o.add(oe, ue), ue = o.mul(Ee, be), ee = o.mul(_e, ee), ee = o.sub(ee, ue), ue = o.mul(_e, me), ce = o.mul(Ee, ce), ce = o.add(ce, ue), new fe(ee, oe, ce);
|
|
1057
1084
|
}
|
|
1058
1085
|
subtract(Z) {
|
|
1059
1086
|
return this.add(Z.negate());
|
|
@@ -1074,16 +1101,16 @@ function Ct() {
|
|
|
1074
1101
|
const { endo: N } = a;
|
|
1075
1102
|
if (!h.isValidNot0(Z))
|
|
1076
1103
|
throw new Error("invalid scalar: out of range");
|
|
1077
|
-
let
|
|
1078
|
-
const J = (re) => Ue.cached(this, re, (ne) => (0,
|
|
1104
|
+
let x, z;
|
|
1105
|
+
const J = (re) => Ue.cached(this, re, (ne) => (0, u.normalizeZ)(fe, ne));
|
|
1079
1106
|
if (N) {
|
|
1080
1107
|
const { k1neg: re, k1: ne, k2neg: ee, k2: oe } = ye(Z), { p: ce, f: we } = J(ne), { p: de, f: ue } = J(oe);
|
|
1081
|
-
|
|
1108
|
+
z = we.add(ue), x = Te(N.beta, ce, de, re, ee);
|
|
1082
1109
|
} else {
|
|
1083
1110
|
const { p: re, f: ne } = J(Z);
|
|
1084
|
-
|
|
1111
|
+
x = re, z = ne;
|
|
1085
1112
|
}
|
|
1086
|
-
return (0,
|
|
1113
|
+
return (0, u.normalizeZ)(fe, [x, z])[0];
|
|
1087
1114
|
}
|
|
1088
1115
|
/**
|
|
1089
1116
|
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
@@ -1091,24 +1118,24 @@ function Ct() {
|
|
|
1091
1118
|
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
1092
1119
|
*/
|
|
1093
1120
|
multiplyUnsafe(Z) {
|
|
1094
|
-
const { endo: N } = a,
|
|
1121
|
+
const { endo: N } = a, x = this;
|
|
1095
1122
|
if (!h.isValid(Z))
|
|
1096
1123
|
throw new Error("invalid scalar: out of range");
|
|
1097
|
-
if (Z === V ||
|
|
1124
|
+
if (Z === V || x.is0())
|
|
1098
1125
|
return fe.ZERO;
|
|
1099
1126
|
if (Z === E)
|
|
1100
|
-
return
|
|
1127
|
+
return x;
|
|
1101
1128
|
if (Ue.hasCache(this))
|
|
1102
1129
|
return this.multiply(Z);
|
|
1103
1130
|
if (N) {
|
|
1104
|
-
const { k1neg:
|
|
1105
|
-
return
|
|
1131
|
+
const { k1neg: z, k1: J, k2neg: re, k2: ne } = ye(Z), { p1: ee, p2: oe } = (0, u.mulEndoUnsafe)(fe, x, J, ne);
|
|
1132
|
+
return Te(N.beta, ee, oe, z, re);
|
|
1106
1133
|
} else
|
|
1107
|
-
return Ue.unsafe(
|
|
1134
|
+
return Ue.unsafe(x, Z);
|
|
1108
1135
|
}
|
|
1109
|
-
multiplyAndAddUnsafe(Z, N,
|
|
1110
|
-
const
|
|
1111
|
-
return
|
|
1136
|
+
multiplyAndAddUnsafe(Z, N, x) {
|
|
1137
|
+
const z = this.multiplyUnsafe(N).add(Z.multiplyUnsafe(x));
|
|
1138
|
+
return z.is0() ? void 0 : z;
|
|
1112
1139
|
}
|
|
1113
1140
|
/**
|
|
1114
1141
|
* Converts Projective point to affine (x, y) coordinates.
|
|
@@ -1158,91 +1185,91 @@ function Ct() {
|
|
|
1158
1185
|
this.precompute(Z);
|
|
1159
1186
|
}
|
|
1160
1187
|
static normalizeZ(Z) {
|
|
1161
|
-
return (0,
|
|
1188
|
+
return (0, u.normalizeZ)(fe, Z);
|
|
1162
1189
|
}
|
|
1163
1190
|
static msm(Z, N) {
|
|
1164
|
-
return (0,
|
|
1191
|
+
return (0, u.pippenger)(fe, h, Z, N);
|
|
1165
1192
|
}
|
|
1166
1193
|
static fromPrivateKey(Z) {
|
|
1167
1194
|
return fe.BASE.multiply(M(h, Z));
|
|
1168
1195
|
}
|
|
1169
1196
|
}
|
|
1170
1197
|
fe.BASE = new fe(w.Gx, w.Gy, o.ONE), fe.ZERO = new fe(o.ZERO, o.ONE, o.ZERO), fe.Fp = o, fe.Fn = h;
|
|
1171
|
-
const Ye = h.BITS, Ue = new
|
|
1198
|
+
const Ye = h.BITS, Ue = new u.wNAF(fe, a.endo ? Math.ceil(Ye / 2) : Ye);
|
|
1172
1199
|
return fe.BASE.precompute(8), fe;
|
|
1173
1200
|
}
|
|
1174
|
-
function F(
|
|
1175
|
-
return Uint8Array.of(
|
|
1201
|
+
function F(r) {
|
|
1202
|
+
return Uint8Array.of(r ? 2 : 3);
|
|
1176
1203
|
}
|
|
1177
|
-
function G(
|
|
1178
|
-
const i =
|
|
1204
|
+
function G(r, a) {
|
|
1205
|
+
const i = r.ORDER;
|
|
1179
1206
|
let o = V;
|
|
1180
1207
|
for (let q = i - E; q % b === V; q /= b)
|
|
1181
1208
|
o += E;
|
|
1182
|
-
const h = o, w = b << h - E - E, s = w * b, f = (i - E) / s, y = (f - E) / b, B = s - E,
|
|
1209
|
+
const h = o, w = b << h - E - E, s = w * b, f = (i - E) / s, y = (f - E) / b, B = s - E, O = w, k = r.pow(a, f), Y = r.pow(a, (f + E) / b);
|
|
1183
1210
|
let I = (q, C) => {
|
|
1184
|
-
let te = k, $ =
|
|
1185
|
-
|
|
1186
|
-
let X =
|
|
1187
|
-
X =
|
|
1188
|
-
let ae =
|
|
1189
|
-
X =
|
|
1190
|
-
let ye =
|
|
1191
|
-
$ =
|
|
1211
|
+
let te = k, $ = r.pow(C, B), T = r.sqr($);
|
|
1212
|
+
T = r.mul(T, C);
|
|
1213
|
+
let X = r.mul(q, T);
|
|
1214
|
+
X = r.pow(X, y), X = r.mul(X, $), $ = r.mul(X, C), T = r.mul(X, q);
|
|
1215
|
+
let ae = r.mul(T, $);
|
|
1216
|
+
X = r.pow(ae, O);
|
|
1217
|
+
let ye = r.eql(X, r.ONE);
|
|
1218
|
+
$ = r.mul(T, Y), X = r.mul(ae, te), T = r.cmov($, T, ye), ae = r.cmov(X, ae, ye);
|
|
1192
1219
|
for (let pe = h; pe > E; pe--) {
|
|
1193
1220
|
let ve = pe - b;
|
|
1194
1221
|
ve = b << ve - E;
|
|
1195
|
-
let
|
|
1196
|
-
const fe =
|
|
1197
|
-
$ =
|
|
1222
|
+
let Te = r.pow(ae, ve);
|
|
1223
|
+
const fe = r.eql(Te, r.ONE);
|
|
1224
|
+
$ = r.mul(T, te), te = r.mul(te, te), Te = r.mul(ae, te), T = r.cmov($, T, fe), ae = r.cmov(Te, ae, fe);
|
|
1198
1225
|
}
|
|
1199
|
-
return { isValid: ye, value:
|
|
1226
|
+
return { isValid: ye, value: T };
|
|
1200
1227
|
};
|
|
1201
|
-
if (
|
|
1202
|
-
const q = (
|
|
1228
|
+
if (r.ORDER % A === S) {
|
|
1229
|
+
const q = (r.ORDER - S) / A, C = r.sqrt(r.neg(a));
|
|
1203
1230
|
I = (te, $) => {
|
|
1204
|
-
let
|
|
1205
|
-
const X =
|
|
1206
|
-
|
|
1207
|
-
let ae =
|
|
1208
|
-
ae =
|
|
1209
|
-
const ye =
|
|
1210
|
-
let
|
|
1211
|
-
return { isValid: ve, value:
|
|
1231
|
+
let T = r.sqr($);
|
|
1232
|
+
const X = r.mul(te, $);
|
|
1233
|
+
T = r.mul(T, X);
|
|
1234
|
+
let ae = r.pow(T, q);
|
|
1235
|
+
ae = r.mul(ae, X);
|
|
1236
|
+
const ye = r.mul(ae, C), pe = r.mul(r.sqr(ae), $), ve = r.eql(pe, te);
|
|
1237
|
+
let Te = r.cmov(ye, ae, ve);
|
|
1238
|
+
return { isValid: ve, value: Te };
|
|
1212
1239
|
};
|
|
1213
1240
|
}
|
|
1214
1241
|
return I;
|
|
1215
1242
|
}
|
|
1216
|
-
function se(
|
|
1217
|
-
(0, m.validateField)(
|
|
1243
|
+
function se(r, a) {
|
|
1244
|
+
(0, m.validateField)(r);
|
|
1218
1245
|
const { A: i, B: o, Z: h } = a;
|
|
1219
|
-
if (!
|
|
1246
|
+
if (!r.isValid(i) || !r.isValid(o) || !r.isValid(h))
|
|
1220
1247
|
throw new Error("mapToCurveSimpleSWU: invalid opts");
|
|
1221
|
-
const w = G(
|
|
1222
|
-
if (!
|
|
1248
|
+
const w = G(r, h);
|
|
1249
|
+
if (!r.isOdd)
|
|
1223
1250
|
throw new Error("Field does not have .isOdd()");
|
|
1224
1251
|
return (s) => {
|
|
1225
|
-
let f, y, B,
|
|
1226
|
-
f =
|
|
1252
|
+
let f, y, B, O, k, Y, I, q;
|
|
1253
|
+
f = r.sqr(s), f = r.mul(f, h), y = r.sqr(f), y = r.add(y, f), B = r.add(y, r.ONE), B = r.mul(B, o), O = r.cmov(h, r.neg(y), !r.eql(y, r.ZERO)), O = r.mul(O, i), y = r.sqr(B), Y = r.sqr(O), k = r.mul(Y, i), y = r.add(y, k), y = r.mul(y, B), Y = r.mul(Y, O), k = r.mul(Y, o), y = r.add(y, k), I = r.mul(f, B);
|
|
1227
1254
|
const { isValid: C, value: te } = w(y, Y);
|
|
1228
|
-
q =
|
|
1229
|
-
const $ =
|
|
1230
|
-
q =
|
|
1231
|
-
const
|
|
1232
|
-
return I =
|
|
1255
|
+
q = r.mul(f, s), q = r.mul(q, te), I = r.cmov(I, B, C), q = r.cmov(q, te, C);
|
|
1256
|
+
const $ = r.isOdd(s) === r.isOdd(q);
|
|
1257
|
+
q = r.cmov(r.neg(q), q, $);
|
|
1258
|
+
const T = (0, m.FpInvertBatch)(r, [O], !0)[0];
|
|
1259
|
+
return I = r.mul(I, T), { x: I, y: q };
|
|
1233
1260
|
};
|
|
1234
1261
|
}
|
|
1235
|
-
function ie(
|
|
1262
|
+
function ie(r, a) {
|
|
1236
1263
|
return {
|
|
1237
1264
|
secretKey: a.BYTES,
|
|
1238
|
-
publicKey: 1 +
|
|
1239
|
-
publicKeyUncompressed: 1 + 2 *
|
|
1265
|
+
publicKey: 1 + r.BYTES,
|
|
1266
|
+
publicKeyUncompressed: 1 + 2 * r.BYTES,
|
|
1240
1267
|
publicKeyHasPrefix: !0,
|
|
1241
1268
|
signature: 2 * a.BYTES
|
|
1242
1269
|
};
|
|
1243
1270
|
}
|
|
1244
|
-
function j(
|
|
1245
|
-
const { Fn: i } =
|
|
1271
|
+
function j(r, a = {}) {
|
|
1272
|
+
const { Fn: i } = r, o = a.randomBytes || t.randomBytes, h = Object.assign(ie(r.Fp, i), { seed: (0, m.getMinHashLength)(i.ORDER) });
|
|
1246
1273
|
function w(I) {
|
|
1247
1274
|
try {
|
|
1248
1275
|
return !!M(i, I);
|
|
@@ -1254,7 +1281,7 @@ function Ct() {
|
|
|
1254
1281
|
const { publicKey: C, publicKeyUncompressed: te } = h;
|
|
1255
1282
|
try {
|
|
1256
1283
|
const $ = I.length;
|
|
1257
|
-
return q === !0 && $ !== C || q === !1 && $ !== te ? !1 : !!
|
|
1284
|
+
return q === !0 && $ !== C || q === !1 && $ !== te ? !1 : !!r.fromBytes(I);
|
|
1258
1285
|
} catch {
|
|
1259
1286
|
return !1;
|
|
1260
1287
|
}
|
|
@@ -1263,16 +1290,16 @@ function Ct() {
|
|
|
1263
1290
|
return (0, m.mapHashToField)((0, t._abytes2)(I, h.seed, "seed"), i.ORDER);
|
|
1264
1291
|
}
|
|
1265
1292
|
function y(I, q = !0) {
|
|
1266
|
-
return
|
|
1293
|
+
return r.BASE.multiply(M(i, I)).toBytes(q);
|
|
1267
1294
|
}
|
|
1268
1295
|
function B(I) {
|
|
1269
1296
|
const q = f(I);
|
|
1270
1297
|
return { secretKey: q, publicKey: y(q) };
|
|
1271
1298
|
}
|
|
1272
|
-
function
|
|
1299
|
+
function O(I) {
|
|
1273
1300
|
if (typeof I == "bigint")
|
|
1274
1301
|
return !1;
|
|
1275
|
-
if (I instanceof
|
|
1302
|
+
if (I instanceof r)
|
|
1276
1303
|
return !0;
|
|
1277
1304
|
const { secretKey: q, publicKey: C, publicKeyUncompressed: te } = h;
|
|
1278
1305
|
if (i.allowedLengths || q === C)
|
|
@@ -1281,14 +1308,14 @@ function Ct() {
|
|
|
1281
1308
|
return $ === C || $ === te;
|
|
1282
1309
|
}
|
|
1283
1310
|
function k(I, q, C = !0) {
|
|
1284
|
-
if (
|
|
1311
|
+
if (O(I) === !0)
|
|
1285
1312
|
throw new Error("first arg must be private key");
|
|
1286
|
-
if (
|
|
1313
|
+
if (O(q) === !1)
|
|
1287
1314
|
throw new Error("second arg must be public key");
|
|
1288
1315
|
const te = M(i, I);
|
|
1289
|
-
return
|
|
1316
|
+
return r.fromHex(q).multiply(te).toBytes(C);
|
|
1290
1317
|
}
|
|
1291
|
-
return Object.freeze({ getPublicKey: y, getSharedSecret: k, keygen: B, Point:
|
|
1318
|
+
return Object.freeze({ getPublicKey: y, getSharedSecret: k, keygen: B, Point: r, utils: {
|
|
1292
1319
|
isValidSecretKey: w,
|
|
1293
1320
|
isValidPublicKey: s,
|
|
1294
1321
|
randomSecretKey: f,
|
|
@@ -1296,12 +1323,12 @@ function Ct() {
|
|
|
1296
1323
|
isValidPrivateKey: w,
|
|
1297
1324
|
randomPrivateKey: f,
|
|
1298
1325
|
normPrivateKeyToScalar: (I) => M(i, I),
|
|
1299
|
-
precompute(I = 8, q =
|
|
1326
|
+
precompute(I = 8, q = r.BASE) {
|
|
1300
1327
|
return q.precompute(I, !1);
|
|
1301
1328
|
}
|
|
1302
1329
|
}, lengths: h });
|
|
1303
1330
|
}
|
|
1304
|
-
function
|
|
1331
|
+
function R(r, a, i = {}) {
|
|
1305
1332
|
(0, c.ahash)(a), (0, t._validateObject)(i, {}, {
|
|
1306
1333
|
hmac: "function",
|
|
1307
1334
|
lowS: "boolean",
|
|
@@ -1309,7 +1336,7 @@ function Ct() {
|
|
|
1309
1336
|
bits2int: "function",
|
|
1310
1337
|
bits2int_modN: "function"
|
|
1311
1338
|
});
|
|
1312
|
-
const o = i.randomBytes || t.randomBytes, h = i.hmac || ((N, ...
|
|
1339
|
+
const o = i.randomBytes || t.randomBytes, h = i.hmac || ((N, ...x) => (0, n.hmac)(a, N, (0, t.concatBytes)(...x))), { Fp: w, Fn: s } = r, { ORDER: f, BITS: y } = s, { keygen: B, getPublicKey: O, getSharedSecret: k, utils: Y, lengths: I } = j(r, i), q = {
|
|
1313
1340
|
prehash: !1,
|
|
1314
1341
|
lowS: typeof i.lowS == "boolean" ? i.lowS : !1,
|
|
1315
1342
|
format: void 0,
|
|
@@ -1317,50 +1344,50 @@ function Ct() {
|
|
|
1317
1344
|
extraEntropy: !1
|
|
1318
1345
|
}, C = "compact";
|
|
1319
1346
|
function te(N) {
|
|
1320
|
-
const
|
|
1321
|
-
return N >
|
|
1347
|
+
const x = f >> E;
|
|
1348
|
+
return N > x;
|
|
1322
1349
|
}
|
|
1323
|
-
function $(N,
|
|
1324
|
-
if (!s.isValidNot0(
|
|
1350
|
+
function $(N, x) {
|
|
1351
|
+
if (!s.isValidNot0(x))
|
|
1325
1352
|
throw new Error(`invalid signature ${N}: out of range 1..Point.Fn.ORDER`);
|
|
1326
|
-
return
|
|
1353
|
+
return x;
|
|
1327
1354
|
}
|
|
1328
|
-
function
|
|
1329
|
-
v(
|
|
1330
|
-
const
|
|
1331
|
-
return (0, t._abytes2)(N, J, `${
|
|
1355
|
+
function T(N, x) {
|
|
1356
|
+
v(x);
|
|
1357
|
+
const z = I.signature, J = x === "compact" ? z : x === "recovered" ? z + 1 : void 0;
|
|
1358
|
+
return (0, t._abytes2)(N, J, `${x} signature`);
|
|
1332
1359
|
}
|
|
1333
1360
|
class X {
|
|
1334
|
-
constructor(
|
|
1335
|
-
this.r = $("r",
|
|
1361
|
+
constructor(x, z, J) {
|
|
1362
|
+
this.r = $("r", x), this.s = $("s", z), J != null && (this.recovery = J), Object.freeze(this);
|
|
1336
1363
|
}
|
|
1337
|
-
static fromBytes(
|
|
1338
|
-
x
|
|
1364
|
+
static fromBytes(x, z = C) {
|
|
1365
|
+
T(x, z);
|
|
1339
1366
|
let J;
|
|
1340
|
-
if (
|
|
1341
|
-
const { r: oe, s: ce } = e.DER.toSig((0, t._abytes2)(
|
|
1367
|
+
if (z === "der") {
|
|
1368
|
+
const { r: oe, s: ce } = e.DER.toSig((0, t._abytes2)(x));
|
|
1342
1369
|
return new X(oe, ce);
|
|
1343
1370
|
}
|
|
1344
|
-
|
|
1345
|
-
const re = s.BYTES, ne =
|
|
1371
|
+
z === "recovered" && (J = x[0], z = "compact", x = x.subarray(1));
|
|
1372
|
+
const re = s.BYTES, ne = x.subarray(0, re), ee = x.subarray(re, re * 2);
|
|
1346
1373
|
return new X(s.fromBytes(ne), s.fromBytes(ee), J);
|
|
1347
1374
|
}
|
|
1348
|
-
static fromHex(
|
|
1349
|
-
return this.fromBytes((0, t.hexToBytes)(
|
|
1375
|
+
static fromHex(x, z) {
|
|
1376
|
+
return this.fromBytes((0, t.hexToBytes)(x), z);
|
|
1350
1377
|
}
|
|
1351
|
-
addRecoveryBit(
|
|
1352
|
-
return new X(this.r, this.s,
|
|
1378
|
+
addRecoveryBit(x) {
|
|
1379
|
+
return new X(this.r, this.s, x);
|
|
1353
1380
|
}
|
|
1354
|
-
recoverPublicKey(
|
|
1355
|
-
const
|
|
1381
|
+
recoverPublicKey(x) {
|
|
1382
|
+
const z = w.ORDER, { r: J, s: re, recovery: ne } = this;
|
|
1356
1383
|
if (ne == null || ![0, 1, 2, 3].includes(ne))
|
|
1357
1384
|
throw new Error("recovery id invalid");
|
|
1358
|
-
if (f * b <
|
|
1385
|
+
if (f * b < z && ne > 1)
|
|
1359
1386
|
throw new Error("recovery id is ambiguous for h>1 curve");
|
|
1360
1387
|
const oe = ne === 2 || ne === 3 ? J + f : J;
|
|
1361
1388
|
if (!w.isValid(oe))
|
|
1362
1389
|
throw new Error("recovery id 2 or 3 invalid");
|
|
1363
|
-
const ce = w.toBytes(oe), we =
|
|
1390
|
+
const ce = w.toBytes(oe), we = r.fromBytes((0, t.concatBytes)(F((ne & 1) === 0), ce)), de = s.inv(oe), ue = ye((0, t.ensureBytes)("msgHash", x)), me = s.create(-ue * de), ge = s.create(re * de), _e = r.BASE.multiplyUnsafe(me).add(we.multiplyUnsafe(ge));
|
|
1364
1391
|
if (_e.is0())
|
|
1365
1392
|
throw new Error("point at infinify");
|
|
1366
1393
|
return _e.assertValidity(), _e;
|
|
@@ -1369,28 +1396,28 @@ function Ct() {
|
|
|
1369
1396
|
hasHighS() {
|
|
1370
1397
|
return te(this.s);
|
|
1371
1398
|
}
|
|
1372
|
-
toBytes(
|
|
1373
|
-
if (v(
|
|
1399
|
+
toBytes(x = C) {
|
|
1400
|
+
if (v(x), x === "der")
|
|
1374
1401
|
return (0, t.hexToBytes)(e.DER.hexFromSig(this));
|
|
1375
|
-
const
|
|
1376
|
-
if (
|
|
1402
|
+
const z = s.toBytes(this.r), J = s.toBytes(this.s);
|
|
1403
|
+
if (x === "recovered") {
|
|
1377
1404
|
if (this.recovery == null)
|
|
1378
1405
|
throw new Error("recovery bit must be present");
|
|
1379
|
-
return (0, t.concatBytes)(Uint8Array.of(this.recovery),
|
|
1406
|
+
return (0, t.concatBytes)(Uint8Array.of(this.recovery), z, J);
|
|
1380
1407
|
}
|
|
1381
|
-
return (0, t.concatBytes)(
|
|
1408
|
+
return (0, t.concatBytes)(z, J);
|
|
1382
1409
|
}
|
|
1383
|
-
toHex(
|
|
1384
|
-
return (0, t.bytesToHex)(this.toBytes(
|
|
1410
|
+
toHex(x) {
|
|
1411
|
+
return (0, t.bytesToHex)(this.toBytes(x));
|
|
1385
1412
|
}
|
|
1386
1413
|
// TODO: remove
|
|
1387
1414
|
assertValidity() {
|
|
1388
1415
|
}
|
|
1389
|
-
static fromCompact(
|
|
1390
|
-
return X.fromBytes((0, t.ensureBytes)("sig",
|
|
1416
|
+
static fromCompact(x) {
|
|
1417
|
+
return X.fromBytes((0, t.ensureBytes)("sig", x), "compact");
|
|
1391
1418
|
}
|
|
1392
|
-
static fromDER(
|
|
1393
|
-
return X.fromBytes((0, t.ensureBytes)("sig",
|
|
1419
|
+
static fromDER(x) {
|
|
1420
|
+
return X.fromBytes((0, t.ensureBytes)("sig", x), "der");
|
|
1394
1421
|
}
|
|
1395
1422
|
normalizeS() {
|
|
1396
1423
|
return this.hasHighS() ? new X(this.r, s.neg(this.s), this.recovery) : this;
|
|
@@ -1408,26 +1435,26 @@ function Ct() {
|
|
|
1408
1435
|
return (0, t.bytesToHex)(this.toBytes("compact"));
|
|
1409
1436
|
}
|
|
1410
1437
|
}
|
|
1411
|
-
const ae = i.bits2int || function(
|
|
1412
|
-
if (
|
|
1438
|
+
const ae = i.bits2int || function(x) {
|
|
1439
|
+
if (x.length > 8192)
|
|
1413
1440
|
throw new Error("input is too large");
|
|
1414
|
-
const
|
|
1415
|
-
return J > 0 ?
|
|
1416
|
-
}, ye = i.bits2int_modN || function(
|
|
1417
|
-
return s.create(ae(
|
|
1441
|
+
const z = (0, t.bytesToNumberBE)(x), J = x.length * 8 - y;
|
|
1442
|
+
return J > 0 ? z >> BigInt(J) : z;
|
|
1443
|
+
}, ye = i.bits2int_modN || function(x) {
|
|
1444
|
+
return s.create(ae(x));
|
|
1418
1445
|
}, pe = (0, t.bitMask)(y);
|
|
1419
1446
|
function ve(N) {
|
|
1420
1447
|
return (0, t.aInRange)("num < 2^" + y, N, V, pe), s.toBytes(N);
|
|
1421
1448
|
}
|
|
1422
|
-
function
|
|
1423
|
-
return (0, t._abytes2)(N, void 0, "message"),
|
|
1449
|
+
function Te(N, x) {
|
|
1450
|
+
return (0, t._abytes2)(N, void 0, "message"), x ? (0, t._abytes2)(a(N), void 0, "prehashed message") : N;
|
|
1424
1451
|
}
|
|
1425
|
-
function fe(N,
|
|
1426
|
-
if (["recovered", "canonical"].some((me) => me in
|
|
1452
|
+
function fe(N, x, z) {
|
|
1453
|
+
if (["recovered", "canonical"].some((me) => me in z))
|
|
1427
1454
|
throw new Error("sign() legacy options not supported");
|
|
1428
|
-
const { lowS: J, prehash: re, extraEntropy: ne } = U(
|
|
1429
|
-
N =
|
|
1430
|
-
const ee = ye(N), oe = M(s,
|
|
1455
|
+
const { lowS: J, prehash: re, extraEntropy: ne } = U(z, q);
|
|
1456
|
+
N = Te(N, re);
|
|
1457
|
+
const ee = ye(N), oe = M(s, x), ce = [ve(oe), ve(ee)];
|
|
1431
1458
|
if (ne != null && ne !== !1) {
|
|
1432
1459
|
const me = ne === !0 ? o(I.secretKey) : ne;
|
|
1433
1460
|
ce.push((0, t.ensureBytes)("extraEntropy", me));
|
|
@@ -1437,7 +1464,7 @@ function Ct() {
|
|
|
1437
1464
|
const ge = ae(me);
|
|
1438
1465
|
if (!s.isValidNot0(ge))
|
|
1439
1466
|
return;
|
|
1440
|
-
const _e = s.inv(ge), be =
|
|
1467
|
+
const _e = s.inv(ge), be = r.BASE.multiply(ge).toAffine(), Ee = s.create(be.x);
|
|
1441
1468
|
if (Ee === V)
|
|
1442
1469
|
return;
|
|
1443
1470
|
const Pe = s.create(_e * s.create(de + Ee * oe));
|
|
@@ -1448,62 +1475,62 @@ function Ct() {
|
|
|
1448
1475
|
}
|
|
1449
1476
|
return { seed: we, k2sig: ue };
|
|
1450
1477
|
}
|
|
1451
|
-
function Ye(N,
|
|
1478
|
+
function Ye(N, x, z = {}) {
|
|
1452
1479
|
N = (0, t.ensureBytes)("message", N);
|
|
1453
|
-
const { seed: J, k2sig: re } = fe(N,
|
|
1480
|
+
const { seed: J, k2sig: re } = fe(N, x, z);
|
|
1454
1481
|
return (0, t.createHmacDrbg)(a.outputLen, s.BYTES, h)(J, re);
|
|
1455
1482
|
}
|
|
1456
1483
|
function Ue(N) {
|
|
1457
|
-
let
|
|
1458
|
-
const
|
|
1459
|
-
if (!
|
|
1484
|
+
let x;
|
|
1485
|
+
const z = typeof N == "string" || (0, t.isBytes)(N), J = !z && N !== null && typeof N == "object" && typeof N.r == "bigint" && typeof N.s == "bigint";
|
|
1486
|
+
if (!z && !J)
|
|
1460
1487
|
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
1461
1488
|
if (J)
|
|
1462
|
-
|
|
1463
|
-
else if (
|
|
1489
|
+
x = new X(N.r, N.s);
|
|
1490
|
+
else if (z) {
|
|
1464
1491
|
try {
|
|
1465
|
-
|
|
1492
|
+
x = X.fromBytes((0, t.ensureBytes)("sig", N), "der");
|
|
1466
1493
|
} catch (re) {
|
|
1467
1494
|
if (!(re instanceof e.DER.Err))
|
|
1468
1495
|
throw re;
|
|
1469
1496
|
}
|
|
1470
|
-
if (!
|
|
1497
|
+
if (!x)
|
|
1471
1498
|
try {
|
|
1472
|
-
|
|
1499
|
+
x = X.fromBytes((0, t.ensureBytes)("sig", N), "compact");
|
|
1473
1500
|
} catch {
|
|
1474
1501
|
return !1;
|
|
1475
1502
|
}
|
|
1476
1503
|
}
|
|
1477
|
-
return
|
|
1504
|
+
return x || !1;
|
|
1478
1505
|
}
|
|
1479
|
-
function le(N,
|
|
1506
|
+
function le(N, x, z, J = {}) {
|
|
1480
1507
|
const { lowS: re, prehash: ne, format: ee } = U(J, q);
|
|
1481
|
-
if (
|
|
1508
|
+
if (z = (0, t.ensureBytes)("publicKey", z), x = Te((0, t.ensureBytes)("message", x), ne), "strict" in J)
|
|
1482
1509
|
throw new Error("options.strict was renamed to lowS");
|
|
1483
1510
|
const oe = ee === void 0 ? Ue(N) : X.fromBytes((0, t.ensureBytes)("sig", N), ee);
|
|
1484
1511
|
if (oe === !1)
|
|
1485
1512
|
return !1;
|
|
1486
1513
|
try {
|
|
1487
|
-
const ce =
|
|
1514
|
+
const ce = r.fromBytes(z);
|
|
1488
1515
|
if (re && oe.hasHighS())
|
|
1489
1516
|
return !1;
|
|
1490
|
-
const { r: we, s: de } = oe, ue = ye(
|
|
1517
|
+
const { r: we, s: de } = oe, ue = ye(x), me = s.inv(de), ge = s.create(ue * me), _e = s.create(we * me), be = r.BASE.multiplyUnsafe(ge).add(ce.multiplyUnsafe(_e));
|
|
1491
1518
|
return be.is0() ? !1 : s.create(be.x) === we;
|
|
1492
1519
|
} catch {
|
|
1493
1520
|
return !1;
|
|
1494
1521
|
}
|
|
1495
1522
|
}
|
|
1496
|
-
function Z(N,
|
|
1497
|
-
const { prehash: J } = U(
|
|
1498
|
-
return
|
|
1523
|
+
function Z(N, x, z = {}) {
|
|
1524
|
+
const { prehash: J } = U(z, q);
|
|
1525
|
+
return x = Te(x, J), X.fromBytes(N, "recovered").recoverPublicKey(x).toBytes();
|
|
1499
1526
|
}
|
|
1500
1527
|
return Object.freeze({
|
|
1501
1528
|
keygen: B,
|
|
1502
|
-
getPublicKey:
|
|
1529
|
+
getPublicKey: O,
|
|
1503
1530
|
getSharedSecret: k,
|
|
1504
1531
|
utils: Y,
|
|
1505
1532
|
lengths: I,
|
|
1506
|
-
Point:
|
|
1533
|
+
Point: r,
|
|
1507
1534
|
sign: Ye,
|
|
1508
1535
|
verify: le,
|
|
1509
1536
|
recoverPublicKey: Z,
|
|
@@ -1511,62 +1538,62 @@ function Ct() {
|
|
|
1511
1538
|
hash: a
|
|
1512
1539
|
});
|
|
1513
1540
|
}
|
|
1514
|
-
function L(
|
|
1515
|
-
const { CURVE: a, curveOpts: i } = _(
|
|
1516
|
-
return
|
|
1541
|
+
function L(r) {
|
|
1542
|
+
const { CURVE: a, curveOpts: i } = _(r), o = Q(a, i);
|
|
1543
|
+
return D(r, o);
|
|
1517
1544
|
}
|
|
1518
|
-
function _(
|
|
1545
|
+
function _(r) {
|
|
1519
1546
|
const a = {
|
|
1520
|
-
a:
|
|
1521
|
-
b:
|
|
1522
|
-
p:
|
|
1523
|
-
n:
|
|
1524
|
-
h:
|
|
1525
|
-
Gx:
|
|
1526
|
-
Gy:
|
|
1527
|
-
}, i =
|
|
1528
|
-
let o =
|
|
1547
|
+
a: r.a,
|
|
1548
|
+
b: r.b,
|
|
1549
|
+
p: r.Fp.ORDER,
|
|
1550
|
+
n: r.n,
|
|
1551
|
+
h: r.h,
|
|
1552
|
+
Gx: r.Gx,
|
|
1553
|
+
Gy: r.Gy
|
|
1554
|
+
}, i = r.Fp;
|
|
1555
|
+
let o = r.allowedPrivateKeyLengths ? Array.from(new Set(r.allowedPrivateKeyLengths.map((s) => Math.ceil(s / 2)))) : void 0;
|
|
1529
1556
|
const h = (0, m.Field)(a.n, {
|
|
1530
|
-
BITS:
|
|
1557
|
+
BITS: r.nBitLength,
|
|
1531
1558
|
allowedLengths: o,
|
|
1532
|
-
modFromBytes:
|
|
1559
|
+
modFromBytes: r.wrapPrivateKey
|
|
1533
1560
|
}), w = {
|
|
1534
1561
|
Fp: i,
|
|
1535
1562
|
Fn: h,
|
|
1536
|
-
allowInfinityPoint:
|
|
1537
|
-
endo:
|
|
1538
|
-
isTorsionFree:
|
|
1539
|
-
clearCofactor:
|
|
1540
|
-
fromBytes:
|
|
1541
|
-
toBytes:
|
|
1563
|
+
allowInfinityPoint: r.allowInfinityPoint,
|
|
1564
|
+
endo: r.endo,
|
|
1565
|
+
isTorsionFree: r.isTorsionFree,
|
|
1566
|
+
clearCofactor: r.clearCofactor,
|
|
1567
|
+
fromBytes: r.fromBytes,
|
|
1568
|
+
toBytes: r.toBytes
|
|
1542
1569
|
};
|
|
1543
1570
|
return { CURVE: a, curveOpts: w };
|
|
1544
1571
|
}
|
|
1545
|
-
function K(
|
|
1546
|
-
const { CURVE: a, curveOpts: i } = _(
|
|
1547
|
-
hmac:
|
|
1548
|
-
randomBytes:
|
|
1549
|
-
lowS:
|
|
1550
|
-
bits2int:
|
|
1551
|
-
bits2int_modN:
|
|
1572
|
+
function K(r) {
|
|
1573
|
+
const { CURVE: a, curveOpts: i } = _(r), o = {
|
|
1574
|
+
hmac: r.hmac,
|
|
1575
|
+
randomBytes: r.randomBytes,
|
|
1576
|
+
lowS: r.lowS,
|
|
1577
|
+
bits2int: r.bits2int,
|
|
1578
|
+
bits2int_modN: r.bits2int_modN
|
|
1552
1579
|
};
|
|
1553
|
-
return { CURVE: a, curveOpts: i, hash:
|
|
1580
|
+
return { CURVE: a, curveOpts: i, hash: r.hash, ecdsaOpts: o };
|
|
1554
1581
|
}
|
|
1555
|
-
function W(
|
|
1582
|
+
function W(r, a, i) {
|
|
1556
1583
|
function o(h) {
|
|
1557
|
-
const w =
|
|
1558
|
-
return
|
|
1584
|
+
const w = r.sqr(h), s = r.mul(w, h);
|
|
1585
|
+
return r.add(r.add(s, r.mul(h, a)), i);
|
|
1559
1586
|
}
|
|
1560
1587
|
return o;
|
|
1561
1588
|
}
|
|
1562
|
-
function
|
|
1589
|
+
function D(r, a) {
|
|
1563
1590
|
const { Fp: i, Fn: o } = a;
|
|
1564
1591
|
function h(s) {
|
|
1565
1592
|
return (0, t.inRange)(s, E, o.ORDER);
|
|
1566
1593
|
}
|
|
1567
|
-
const w = W(i,
|
|
1594
|
+
const w = W(i, r.a, r.b);
|
|
1568
1595
|
return Object.assign({}, {
|
|
1569
|
-
CURVE:
|
|
1596
|
+
CURVE: r,
|
|
1570
1597
|
Point: a,
|
|
1571
1598
|
ProjectivePoint: a,
|
|
1572
1599
|
normPrivateKeyToScalar: (s) => M(o, s),
|
|
@@ -1574,39 +1601,39 @@ function Ct() {
|
|
|
1574
1601
|
isWithinCurveOrder: h
|
|
1575
1602
|
});
|
|
1576
1603
|
}
|
|
1577
|
-
function d(
|
|
1604
|
+
function d(r, a) {
|
|
1578
1605
|
const i = a.Point;
|
|
1579
1606
|
return Object.assign({}, a, {
|
|
1580
1607
|
ProjectivePoint: i,
|
|
1581
|
-
CURVE: Object.assign({},
|
|
1608
|
+
CURVE: Object.assign({}, r, (0, m.nLength)(i.Fn.ORDER, i.Fn.BITS))
|
|
1582
1609
|
});
|
|
1583
1610
|
}
|
|
1584
|
-
function
|
|
1585
|
-
const { CURVE: a, curveOpts: i, hash: o, ecdsaOpts: h } = K(
|
|
1586
|
-
return d(
|
|
1611
|
+
function l(r) {
|
|
1612
|
+
const { CURVE: a, curveOpts: i, hash: o, ecdsaOpts: h } = K(r), w = Q(a, i), s = R(w, o, h);
|
|
1613
|
+
return d(r, s);
|
|
1587
1614
|
}
|
|
1588
1615
|
})(nt)), nt;
|
|
1589
1616
|
}
|
|
1590
|
-
var
|
|
1617
|
+
var Rt;
|
|
1591
1618
|
function En() {
|
|
1592
|
-
if (
|
|
1593
|
-
|
|
1619
|
+
if (Rt) return Ke;
|
|
1620
|
+
Rt = 1, Object.defineProperty(Ke, "__esModule", { value: !0 }), Ke.getHash = n, Ke.createCurve = c;
|
|
1594
1621
|
const e = /* @__PURE__ */ Ct();
|
|
1595
|
-
function
|
|
1622
|
+
function n(t) {
|
|
1596
1623
|
return { hash: t };
|
|
1597
1624
|
}
|
|
1598
|
-
function c(t,
|
|
1625
|
+
function c(t, u) {
|
|
1599
1626
|
const m = (g) => (0, e.weierstrass)({ ...t, hash: g });
|
|
1600
|
-
return { ...m(
|
|
1627
|
+
return { ...m(u), create: m };
|
|
1601
1628
|
}
|
|
1602
1629
|
return Ke;
|
|
1603
1630
|
}
|
|
1604
|
-
var ot = {},
|
|
1631
|
+
var ot = {}, xt;
|
|
1605
1632
|
function Bn() {
|
|
1606
|
-
return
|
|
1633
|
+
return xt || (xt = 1, (function(e) {
|
|
1607
1634
|
Object.defineProperty(e, "__esModule", { value: !0 }), e._DST_scalar = void 0, e.expand_message_xmd = v, e.expand_message_xof = U, e.hash_to_field = H, e.isogenyMap = V, e.createHasher = E;
|
|
1608
|
-
const
|
|
1609
|
-
function
|
|
1635
|
+
const n = /* @__PURE__ */ Ce(), c = /* @__PURE__ */ $e(), t = n.bytesToNumberBE;
|
|
1636
|
+
function u(b, S) {
|
|
1610
1637
|
if (g(b), g(S), b < 0 || b >= 1 << 8 * S)
|
|
1611
1638
|
throw new Error("invalid I2OSP input: " + b);
|
|
1612
1639
|
const A = Array.from({ length: S }).fill(0);
|
|
@@ -1625,44 +1652,44 @@ function Bn() {
|
|
|
1625
1652
|
throw new Error("number expected");
|
|
1626
1653
|
}
|
|
1627
1654
|
function p(b) {
|
|
1628
|
-
if (!(0,
|
|
1655
|
+
if (!(0, n.isBytes)(b) && typeof b != "string")
|
|
1629
1656
|
throw new Error("DST must be Uint8Array or string");
|
|
1630
|
-
return typeof b == "string" ? (0,
|
|
1657
|
+
return typeof b == "string" ? (0, n.utf8ToBytes)(b) : b;
|
|
1631
1658
|
}
|
|
1632
1659
|
function v(b, S, A, M) {
|
|
1633
|
-
(0,
|
|
1660
|
+
(0, n.abytes)(b), g(A), S = p(S), S.length > 255 && (S = M((0, n.concatBytes)((0, n.utf8ToBytes)("H2C-OVERSIZE-DST-"), S)));
|
|
1634
1661
|
const { outputLen: Q, blockLen: F } = M, G = Math.ceil(A / Q);
|
|
1635
1662
|
if (A > 65535 || G > 255)
|
|
1636
1663
|
throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
1637
|
-
const se = (0,
|
|
1638
|
-
|
|
1664
|
+
const se = (0, n.concatBytes)(S, u(S.length, 1)), ie = u(0, F), j = u(A, 2), R = new Array(G), L = M((0, n.concatBytes)(ie, b, j, u(0, 1), se));
|
|
1665
|
+
R[0] = M((0, n.concatBytes)(L, u(1, 1), se));
|
|
1639
1666
|
for (let K = 1; K <= G; K++) {
|
|
1640
|
-
const W = [m(L,
|
|
1641
|
-
|
|
1667
|
+
const W = [m(L, R[K - 1]), u(K + 1, 1), se];
|
|
1668
|
+
R[K] = M((0, n.concatBytes)(...W));
|
|
1642
1669
|
}
|
|
1643
|
-
return (0,
|
|
1670
|
+
return (0, n.concatBytes)(...R).slice(0, A);
|
|
1644
1671
|
}
|
|
1645
1672
|
function U(b, S, A, M, Q) {
|
|
1646
|
-
if ((0,
|
|
1673
|
+
if ((0, n.abytes)(b), g(A), S = p(S), S.length > 255) {
|
|
1647
1674
|
const F = Math.ceil(2 * M / 8);
|
|
1648
|
-
S = Q.create({ dkLen: F }).update((0,
|
|
1675
|
+
S = Q.create({ dkLen: F }).update((0, n.utf8ToBytes)("H2C-OVERSIZE-DST-")).update(S).digest();
|
|
1649
1676
|
}
|
|
1650
1677
|
if (A > 65535 || S.length > 255)
|
|
1651
1678
|
throw new Error("expand_message_xof: invalid lenInBytes");
|
|
1652
|
-
return Q.create({ dkLen: A }).update(b).update(
|
|
1679
|
+
return Q.create({ dkLen: A }).update(b).update(u(A, 2)).update(S).update(u(S.length, 1)).digest();
|
|
1653
1680
|
}
|
|
1654
1681
|
function H(b, S, A) {
|
|
1655
|
-
(0,
|
|
1682
|
+
(0, n._validateObject)(A, {
|
|
1656
1683
|
p: "bigint",
|
|
1657
1684
|
m: "number",
|
|
1658
1685
|
k: "number",
|
|
1659
1686
|
hash: "function"
|
|
1660
1687
|
});
|
|
1661
1688
|
const { p: M, k: Q, m: F, hash: G, expand: se, DST: ie } = A;
|
|
1662
|
-
if (!(0,
|
|
1689
|
+
if (!(0, n.isHash)(A.hash))
|
|
1663
1690
|
throw new Error("expected valid hash");
|
|
1664
|
-
(0,
|
|
1665
|
-
const j = M.toString(2).length,
|
|
1691
|
+
(0, n.abytes)(b), g(S);
|
|
1692
|
+
const j = M.toString(2).length, R = Math.ceil((j + Q) / 8), L = S * F * R;
|
|
1666
1693
|
let _;
|
|
1667
1694
|
if (se === "xmd")
|
|
1668
1695
|
_ = v(b, ie, L, G);
|
|
@@ -1674,23 +1701,23 @@ function Bn() {
|
|
|
1674
1701
|
throw new Error('expand must be "xmd" or "xof"');
|
|
1675
1702
|
const K = new Array(S);
|
|
1676
1703
|
for (let W = 0; W < S; W++) {
|
|
1677
|
-
const
|
|
1704
|
+
const D = new Array(F);
|
|
1678
1705
|
for (let d = 0; d < F; d++) {
|
|
1679
|
-
const
|
|
1680
|
-
|
|
1706
|
+
const l = R * (d + W * F), r = _.subarray(l, l + R);
|
|
1707
|
+
D[d] = (0, c.mod)(t(r), M);
|
|
1681
1708
|
}
|
|
1682
|
-
K[W] =
|
|
1709
|
+
K[W] = D;
|
|
1683
1710
|
}
|
|
1684
1711
|
return K;
|
|
1685
1712
|
}
|
|
1686
1713
|
function V(b, S) {
|
|
1687
1714
|
const A = S.map((M) => Array.from(M).reverse());
|
|
1688
1715
|
return (M, Q) => {
|
|
1689
|
-
const [F, G, se, ie] = A.map((L) => L.reduce((_, K) => b.add(b.mul(_, M), K))), [j,
|
|
1690
|
-
return M = b.mul(F, j), Q = b.mul(Q, b.mul(se,
|
|
1716
|
+
const [F, G, se, ie] = A.map((L) => L.reduce((_, K) => b.add(b.mul(_, M), K))), [j, R] = (0, c.FpInvertBatch)(b, [G, ie], !0);
|
|
1717
|
+
return M = b.mul(F, j), Q = b.mul(Q, b.mul(se, R)), { x: M, y: Q };
|
|
1691
1718
|
};
|
|
1692
1719
|
}
|
|
1693
|
-
e._DST_scalar = (0,
|
|
1720
|
+
e._DST_scalar = (0, n.utf8ToBytes)("HashToScalar-");
|
|
1694
1721
|
function E(b, S, A) {
|
|
1695
1722
|
if (typeof S != "function")
|
|
1696
1723
|
throw new Error("mapToCurve() must be defined");
|
|
@@ -1704,12 +1731,12 @@ function Bn() {
|
|
|
1704
1731
|
return {
|
|
1705
1732
|
defaults: A,
|
|
1706
1733
|
hashToCurve(F, G) {
|
|
1707
|
-
const se = Object.assign({}, A, G), ie = H(F, 2, se), j = M(ie[0]),
|
|
1708
|
-
return Q(j.add(
|
|
1734
|
+
const se = Object.assign({}, A, G), ie = H(F, 2, se), j = M(ie[0]), R = M(ie[1]);
|
|
1735
|
+
return Q(j.add(R));
|
|
1709
1736
|
},
|
|
1710
1737
|
encodeToCurve(F, G) {
|
|
1711
|
-
const se = A.encodeDST ? { DST: A.encodeDST } : {}, ie = Object.assign({}, A, se, G), j = H(F, 1, ie),
|
|
1712
|
-
return Q(
|
|
1738
|
+
const se = A.encodeDST ? { DST: A.encodeDST } : {}, ie = Object.assign({}, A, se, G), j = H(F, 1, ie), R = M(j[0]);
|
|
1739
|
+
return Q(R);
|
|
1713
1740
|
},
|
|
1714
1741
|
/** See {@link H2CHasher} */
|
|
1715
1742
|
mapToCurve(F) {
|
|
@@ -1731,10 +1758,10 @@ function Bn() {
|
|
|
1731
1758
|
})(ot)), ot;
|
|
1732
1759
|
}
|
|
1733
1760
|
var qt;
|
|
1734
|
-
function
|
|
1761
|
+
function nr() {
|
|
1735
1762
|
return qt || (qt = 1, (function(e) {
|
|
1736
1763
|
Object.defineProperty(e, "__esModule", { value: !0 }), e.encodeToCurve = e.hashToCurve = e.secp256k1_hasher = e.schnorr = e.secp256k1 = void 0;
|
|
1737
|
-
const
|
|
1764
|
+
const n = /* @__PURE__ */ dn(), c = /* @__PURE__ */ We(), t = /* @__PURE__ */ En(), u = /* @__PURE__ */ Bn(), m = /* @__PURE__ */ $e(), g = /* @__PURE__ */ Ct(), p = /* @__PURE__ */ Ce(), v = {
|
|
1738
1765
|
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
1739
1766
|
n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
1740
1767
|
h: BigInt(1),
|
|
@@ -1750,52 +1777,52 @@ function er() {
|
|
|
1750
1777
|
]
|
|
1751
1778
|
}, H = /* @__PURE__ */ BigInt(0), V = /* @__PURE__ */ BigInt(1), E = /* @__PURE__ */ BigInt(2);
|
|
1752
1779
|
function b(d) {
|
|
1753
|
-
const
|
|
1780
|
+
const l = v.p, r = BigInt(3), a = BigInt(6), i = BigInt(11), o = BigInt(22), h = BigInt(23), w = BigInt(44), s = BigInt(88), f = d * d * d % l, y = f * f * d % l, B = (0, m.pow2)(y, r, l) * y % l, O = (0, m.pow2)(B, r, l) * y % l, k = (0, m.pow2)(O, E, l) * f % l, Y = (0, m.pow2)(k, i, l) * k % l, I = (0, m.pow2)(Y, o, l) * Y % l, q = (0, m.pow2)(I, w, l) * I % l, C = (0, m.pow2)(q, s, l) * q % l, te = (0, m.pow2)(C, w, l) * I % l, $ = (0, m.pow2)(te, r, l) * y % l, T = (0, m.pow2)($, h, l) * Y % l, X = (0, m.pow2)(T, a, l) * f % l, ae = (0, m.pow2)(X, E, l);
|
|
1754
1781
|
if (!S.eql(S.sqr(ae), d))
|
|
1755
1782
|
throw new Error("Cannot find square root");
|
|
1756
1783
|
return ae;
|
|
1757
1784
|
}
|
|
1758
1785
|
const S = (0, m.Field)(v.p, { sqrt: b });
|
|
1759
|
-
e.secp256k1 = (0, t.createCurve)({ ...v, Fp: S, lowS: !0, endo: U },
|
|
1786
|
+
e.secp256k1 = (0, t.createCurve)({ ...v, Fp: S, lowS: !0, endo: U }, n.sha256);
|
|
1760
1787
|
const A = {};
|
|
1761
|
-
function M(d, ...
|
|
1762
|
-
let
|
|
1763
|
-
if (
|
|
1764
|
-
const a = (0,
|
|
1765
|
-
|
|
1788
|
+
function M(d, ...l) {
|
|
1789
|
+
let r = A[d];
|
|
1790
|
+
if (r === void 0) {
|
|
1791
|
+
const a = (0, n.sha256)((0, p.utf8ToBytes)(d));
|
|
1792
|
+
r = (0, p.concatBytes)(a, a), A[d] = r;
|
|
1766
1793
|
}
|
|
1767
|
-
return (0,
|
|
1794
|
+
return (0, n.sha256)((0, p.concatBytes)(r, ...l));
|
|
1768
1795
|
}
|
|
1769
1796
|
const Q = (d) => d.toBytes(!0).slice(1), F = e.secp256k1.Point, G = (d) => d % E === H;
|
|
1770
1797
|
function se(d) {
|
|
1771
|
-
const { Fn:
|
|
1772
|
-
return { scalar: G(i.y) ? a :
|
|
1798
|
+
const { Fn: l, BASE: r } = F, a = (0, g._normFnElement)(l, d), i = r.multiply(a);
|
|
1799
|
+
return { scalar: G(i.y) ? a : l.neg(a), bytes: Q(i) };
|
|
1773
1800
|
}
|
|
1774
1801
|
function ie(d) {
|
|
1775
|
-
const
|
|
1776
|
-
if (!
|
|
1802
|
+
const l = S;
|
|
1803
|
+
if (!l.isValidNot0(d))
|
|
1777
1804
|
throw new Error("invalid x: Fail if x ≥ p");
|
|
1778
|
-
const
|
|
1779
|
-
let i =
|
|
1780
|
-
G(i) || (i =
|
|
1805
|
+
const r = l.create(d * d), a = l.create(r * d + BigInt(7));
|
|
1806
|
+
let i = l.sqrt(a);
|
|
1807
|
+
G(i) || (i = l.neg(i));
|
|
1781
1808
|
const o = F.fromAffine({ x: d, y: i });
|
|
1782
1809
|
return o.assertValidity(), o;
|
|
1783
1810
|
}
|
|
1784
1811
|
const j = p.bytesToNumberBE;
|
|
1785
|
-
function
|
|
1812
|
+
function R(...d) {
|
|
1786
1813
|
return F.Fn.create(j(M("BIP0340/challenge", ...d)));
|
|
1787
1814
|
}
|
|
1788
1815
|
function L(d) {
|
|
1789
1816
|
return se(d).bytes;
|
|
1790
1817
|
}
|
|
1791
|
-
function _(d,
|
|
1792
|
-
const { Fn: a } = F, i = (0, p.ensureBytes)("message", d), { bytes: o, scalar: h } = se(
|
|
1793
|
-
if (k.set(y, 0), k.set(a.toBytes(a.create(B +
|
|
1818
|
+
function _(d, l, r = (0, c.randomBytes)(32)) {
|
|
1819
|
+
const { Fn: a } = F, i = (0, p.ensureBytes)("message", d), { bytes: o, scalar: h } = se(l), w = (0, p.ensureBytes)("auxRand", r, 32), s = a.toBytes(h ^ j(M("BIP0340/aux", w))), f = M("BIP0340/nonce", s, o, i), { bytes: y, scalar: B } = se(f), O = R(y, o, i), k = new Uint8Array(64);
|
|
1820
|
+
if (k.set(y, 0), k.set(a.toBytes(a.create(B + O * h)), 32), !K(k, i, o))
|
|
1794
1821
|
throw new Error("sign: Invalid signature produced");
|
|
1795
1822
|
return k;
|
|
1796
1823
|
}
|
|
1797
|
-
function K(d,
|
|
1798
|
-
const { Fn: a, BASE: i } = F, o = (0, p.ensureBytes)("signature", d, 64), h = (0, p.ensureBytes)("message",
|
|
1824
|
+
function K(d, l, r) {
|
|
1825
|
+
const { Fn: a, BASE: i } = F, o = (0, p.ensureBytes)("signature", d, 64), h = (0, p.ensureBytes)("message", l), w = (0, p.ensureBytes)("publicKey", r, 32);
|
|
1799
1826
|
try {
|
|
1800
1827
|
const s = ie(j(w)), f = j(o.subarray(0, 32));
|
|
1801
1828
|
if (!(0, p.inRange)(f, V, v.p))
|
|
@@ -1803,17 +1830,17 @@ function er() {
|
|
|
1803
1830
|
const y = j(o.subarray(32, 64));
|
|
1804
1831
|
if (!(0, p.inRange)(y, V, v.n))
|
|
1805
1832
|
return !1;
|
|
1806
|
-
const B =
|
|
1807
|
-
return !(
|
|
1833
|
+
const B = R(a.toBytes(f), Q(s), h), O = i.multiplyUnsafe(y).add(s.multiplyUnsafe(a.neg(B))), { x: k, y: Y } = O.toAffine();
|
|
1834
|
+
return !(O.is0() || !G(Y) || k !== f);
|
|
1808
1835
|
} catch {
|
|
1809
1836
|
return !1;
|
|
1810
1837
|
}
|
|
1811
1838
|
}
|
|
1812
1839
|
e.schnorr = (() => {
|
|
1813
|
-
const
|
|
1840
|
+
const r = (i = (0, c.randomBytes)(48)) => (0, m.mapHashToField)(i, v.n);
|
|
1814
1841
|
e.secp256k1.utils.randomSecretKey;
|
|
1815
1842
|
function a(i) {
|
|
1816
|
-
const o =
|
|
1843
|
+
const o = r(i);
|
|
1817
1844
|
return { secretKey: o, publicKey: L(o) };
|
|
1818
1845
|
}
|
|
1819
1846
|
return {
|
|
@@ -1823,8 +1850,8 @@ function er() {
|
|
|
1823
1850
|
verify: K,
|
|
1824
1851
|
Point: F,
|
|
1825
1852
|
utils: {
|
|
1826
|
-
randomSecretKey:
|
|
1827
|
-
randomPrivateKey:
|
|
1853
|
+
randomSecretKey: r,
|
|
1854
|
+
randomPrivateKey: r,
|
|
1828
1855
|
taggedHash: M,
|
|
1829
1856
|
// TODO: remove
|
|
1830
1857
|
lift_x: ie,
|
|
@@ -1842,7 +1869,7 @@ function er() {
|
|
|
1842
1869
|
}
|
|
1843
1870
|
};
|
|
1844
1871
|
})();
|
|
1845
|
-
const W = (0,
|
|
1872
|
+
const W = (0, u.isogenyMap)(S, [
|
|
1846
1873
|
// xNum
|
|
1847
1874
|
[
|
|
1848
1875
|
"0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7",
|
|
@@ -1872,14 +1899,14 @@ function er() {
|
|
|
1872
1899
|
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
1873
1900
|
// LAST 1
|
|
1874
1901
|
]
|
|
1875
|
-
].map((d) => d.map((
|
|
1902
|
+
].map((d) => d.map((l) => BigInt(l)))), D = (0, g.mapToCurveSimpleSWU)(S, {
|
|
1876
1903
|
A: BigInt("0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533"),
|
|
1877
1904
|
B: BigInt("1771"),
|
|
1878
1905
|
Z: S.create(BigInt("-11"))
|
|
1879
1906
|
});
|
|
1880
|
-
e.secp256k1_hasher = (0,
|
|
1881
|
-
const { x:
|
|
1882
|
-
return W(
|
|
1907
|
+
e.secp256k1_hasher = (0, u.createHasher)(e.secp256k1.Point, (d) => {
|
|
1908
|
+
const { x: l, y: r } = D(S.create(d[0]));
|
|
1909
|
+
return W(l, r);
|
|
1883
1910
|
}, {
|
|
1884
1911
|
DST: "secp256k1_XMD:SHA-256_SSWU_RO_",
|
|
1885
1912
|
encodeDST: "secp256k1_XMD:SHA-256_SSWU_NU_",
|
|
@@ -1887,20 +1914,20 @@ function er() {
|
|
|
1887
1914
|
m: 1,
|
|
1888
1915
|
k: 128,
|
|
1889
1916
|
expand: "xmd",
|
|
1890
|
-
hash:
|
|
1917
|
+
hash: n.sha256
|
|
1891
1918
|
}), e.hashToCurve = e.secp256k1_hasher.hashToCurve, e.encodeToCurve = e.secp256k1_hasher.encodeToCurve;
|
|
1892
1919
|
})(tt)), tt;
|
|
1893
1920
|
}
|
|
1894
1921
|
var P = {}, It;
|
|
1895
|
-
function
|
|
1922
|
+
function rr() {
|
|
1896
1923
|
if (It) return P;
|
|
1897
1924
|
It = 1, Object.defineProperty(P, "__esModule", { value: !0 }), P.isHash = P.validateObject = P.memoized = P.notImplemented = P.createHmacDrbg = P.bitMask = P.bitSet = P.bitGet = P.bitLen = P.aInRange = P.inRange = P.asciiToBytes = P.copyBytes = P.equalBytes = P.ensureBytes = P.numberToVarBytesBE = P.numberToBytesLE = P.numberToBytesBE = P.bytesToNumberLE = P.bytesToNumberBE = P.hexToNumber = P.numberToHexUnpadded = P.abool = P.utf8ToBytes = P.randomBytes = P.isBytes = P.hexToBytes = P.concatBytes = P.bytesToUtf8 = P.bytesToHex = P.anumber = P.abytes = void 0;
|
|
1898
1925
|
const e = /* @__PURE__ */ Ce();
|
|
1899
1926
|
return P.abytes = e.abytes, P.anumber = e.anumber, P.bytesToHex = e.bytesToHex, P.bytesToUtf8 = e.bytesToUtf8, P.concatBytes = e.concatBytes, P.hexToBytes = e.hexToBytes, P.isBytes = e.isBytes, P.randomBytes = e.randomBytes, P.utf8ToBytes = e.utf8ToBytes, P.abool = e.abool, P.numberToHexUnpadded = e.numberToHexUnpadded, P.hexToNumber = e.hexToNumber, P.bytesToNumberBE = e.bytesToNumberBE, P.bytesToNumberLE = e.bytesToNumberLE, P.numberToBytesBE = e.numberToBytesBE, P.numberToBytesLE = e.numberToBytesLE, P.numberToVarBytesBE = e.numberToVarBytesBE, P.ensureBytes = e.ensureBytes, P.equalBytes = e.equalBytes, P.copyBytes = e.copyBytes, P.asciiToBytes = e.asciiToBytes, P.inRange = e.inRange, P.aInRange = e.aInRange, P.bitLen = e.bitLen, P.bitGet = e.bitGet, P.bitSet = e.bitSet, P.bitMask = e.bitMask, P.createHmacDrbg = e.createHmacDrbg, P.notImplemented = e.notImplemented, P.memoized = e.memoized, P.validateObject = e.validateObject, P.isHash = e.isHash, P;
|
|
1900
1927
|
}
|
|
1901
|
-
function
|
|
1928
|
+
function or(e, n = "") {
|
|
1902
1929
|
if (typeof e != "boolean") {
|
|
1903
|
-
const c =
|
|
1930
|
+
const c = n && `"${n}" `;
|
|
1904
1931
|
throw new Error(c + "expected boolean, got type=" + typeof e);
|
|
1905
1932
|
}
|
|
1906
1933
|
return e;
|
|
@@ -1913,138 +1940,138 @@ function mt(e) {
|
|
|
1913
1940
|
function Kt(e) {
|
|
1914
1941
|
return mt(e), (e & e - 1) === 0 && e !== 0;
|
|
1915
1942
|
}
|
|
1916
|
-
function pn(e,
|
|
1943
|
+
function pn(e, n) {
|
|
1917
1944
|
mt(e);
|
|
1918
1945
|
let c = 0;
|
|
1919
|
-
for (let t = 0; t <
|
|
1946
|
+
for (let t = 0; t < n; t++, e >>>= 1)
|
|
1920
1947
|
c = c << 1 | e & 1;
|
|
1921
1948
|
return c;
|
|
1922
1949
|
}
|
|
1923
|
-
function
|
|
1950
|
+
function Dt(e) {
|
|
1924
1951
|
return mt(e), 31 - Math.clz32(e);
|
|
1925
1952
|
}
|
|
1926
1953
|
function Nt(e) {
|
|
1927
|
-
const
|
|
1928
|
-
if (
|
|
1929
|
-
throw new Error("n must be a power of 2 and greater than 1. Got " +
|
|
1930
|
-
const c =
|
|
1931
|
-
for (let t = 0; t <
|
|
1932
|
-
const
|
|
1933
|
-
if (t <
|
|
1954
|
+
const n = e.length;
|
|
1955
|
+
if (n < 2 || !Kt(n))
|
|
1956
|
+
throw new Error("n must be a power of 2 and greater than 1. Got " + n);
|
|
1957
|
+
const c = Dt(n);
|
|
1958
|
+
for (let t = 0; t < n; t++) {
|
|
1959
|
+
const u = pn(t, c);
|
|
1960
|
+
if (t < u) {
|
|
1934
1961
|
const m = e[t];
|
|
1935
|
-
e[t] = e[
|
|
1962
|
+
e[t] = e[u], e[u] = m;
|
|
1936
1963
|
}
|
|
1937
1964
|
}
|
|
1938
1965
|
return e;
|
|
1939
1966
|
}
|
|
1940
|
-
const
|
|
1941
|
-
const { N: c, roots: t, dit:
|
|
1967
|
+
const sr = (e, n) => {
|
|
1968
|
+
const { N: c, roots: t, dit: u, invertButterflies: m = !1, skipStages: g = 0, brp: p = !0 } = n, v = Dt(c);
|
|
1942
1969
|
if (!Kt(c))
|
|
1943
1970
|
throw new Error("FFT: Polynomial size should be power of two");
|
|
1944
|
-
const U =
|
|
1971
|
+
const U = u !== m;
|
|
1945
1972
|
return (H) => {
|
|
1946
1973
|
if (H.length !== c)
|
|
1947
1974
|
throw new Error("FFT: wrong Polynomial length");
|
|
1948
|
-
|
|
1975
|
+
u && p && Nt(H);
|
|
1949
1976
|
for (let V = 0, E = 1; V < v - g; V++) {
|
|
1950
|
-
const b =
|
|
1977
|
+
const b = u ? V + 1 + g : v - V, S = 1 << b, A = S >> 1, M = c >> b;
|
|
1951
1978
|
for (let Q = 0; Q < c; Q += S)
|
|
1952
1979
|
for (let F = 0, G = E++; F < A; F++) {
|
|
1953
|
-
const se = m ?
|
|
1980
|
+
const se = m ? u ? c - G : G : F * M, ie = Q + F, j = Q + F + A, R = t[se], L = H[j], _ = H[ie];
|
|
1954
1981
|
if (U) {
|
|
1955
|
-
const K = e.mul(L,
|
|
1982
|
+
const K = e.mul(L, R);
|
|
1956
1983
|
H[ie] = e.add(_, K), H[j] = e.sub(_, K);
|
|
1957
|
-
} else m ? (H[ie] = e.add(L, _), H[j] = e.mul(e.sub(L, _),
|
|
1984
|
+
} else m ? (H[ie] = e.add(L, _), H[j] = e.mul(e.sub(L, _), R)) : (H[ie] = e.add(_, L), H[j] = e.mul(e.sub(_, L), R));
|
|
1958
1985
|
}
|
|
1959
1986
|
}
|
|
1960
|
-
return !
|
|
1987
|
+
return !u && p && Nt(H), H;
|
|
1961
1988
|
};
|
|
1962
1989
|
};
|
|
1963
1990
|
const bt = /* @__PURE__ */ BigInt(0), lt = /* @__PURE__ */ BigInt(1);
|
|
1964
|
-
function Je(e,
|
|
1991
|
+
function Je(e, n = "") {
|
|
1965
1992
|
if (typeof e != "boolean") {
|
|
1966
|
-
const c =
|
|
1993
|
+
const c = n && `"${n}"`;
|
|
1967
1994
|
throw new Error(c + "expected boolean, got type=" + typeof e);
|
|
1968
1995
|
}
|
|
1969
1996
|
return e;
|
|
1970
1997
|
}
|
|
1971
|
-
function Ze(e,
|
|
1972
|
-
const t = yt(e),
|
|
1973
|
-
if (!t || m &&
|
|
1974
|
-
const g = c && `"${c}" `, p = m ? ` of length ${
|
|
1998
|
+
function Ze(e, n, c = "") {
|
|
1999
|
+
const t = yt(e), u = e?.length, m = n !== void 0;
|
|
2000
|
+
if (!t || m && u !== n) {
|
|
2001
|
+
const g = c && `"${c}" `, p = m ? ` of length ${n}` : "", v = t ? `length=${u}` : `type=${typeof e}`;
|
|
1975
2002
|
throw new Error(g + "expected Uint8Array" + p + ", got " + v);
|
|
1976
2003
|
}
|
|
1977
2004
|
return e;
|
|
1978
2005
|
}
|
|
1979
2006
|
function Xe(e) {
|
|
1980
|
-
const
|
|
1981
|
-
return
|
|
2007
|
+
const n = e.toString(16);
|
|
2008
|
+
return n.length & 1 ? "0" + n : n;
|
|
1982
2009
|
}
|
|
1983
|
-
function
|
|
2010
|
+
function zt(e) {
|
|
1984
2011
|
if (typeof e != "string")
|
|
1985
2012
|
throw new Error("hex string expected, got " + typeof e);
|
|
1986
2013
|
return e === "" ? bt : BigInt("0x" + e);
|
|
1987
2014
|
}
|
|
1988
2015
|
function et(e) {
|
|
1989
|
-
return
|
|
2016
|
+
return zt(Me(e));
|
|
1990
2017
|
}
|
|
1991
2018
|
function Yt(e) {
|
|
1992
|
-
return hn(e),
|
|
2019
|
+
return hn(e), zt(Me(Uint8Array.from(e).reverse()));
|
|
1993
2020
|
}
|
|
1994
|
-
function wt(e,
|
|
1995
|
-
return Qe(e.toString(16).padStart(
|
|
2021
|
+
function wt(e, n) {
|
|
2022
|
+
return Qe(e.toString(16).padStart(n * 2, "0"));
|
|
1996
2023
|
}
|
|
1997
|
-
function Pt(e,
|
|
1998
|
-
return wt(e,
|
|
2024
|
+
function Pt(e, n) {
|
|
2025
|
+
return wt(e, n).reverse();
|
|
1999
2026
|
}
|
|
2000
|
-
function Se(e,
|
|
2027
|
+
function Se(e, n, c) {
|
|
2001
2028
|
let t;
|
|
2002
|
-
if (typeof
|
|
2029
|
+
if (typeof n == "string")
|
|
2003
2030
|
try {
|
|
2004
|
-
t = Qe(
|
|
2005
|
-
} catch (
|
|
2006
|
-
throw new Error(e + " must be hex string or Uint8Array, cause: " +
|
|
2031
|
+
t = Qe(n);
|
|
2032
|
+
} catch (u) {
|
|
2033
|
+
throw new Error(e + " must be hex string or Uint8Array, cause: " + u);
|
|
2007
2034
|
}
|
|
2008
|
-
else if (yt(
|
|
2009
|
-
t = Uint8Array.from(
|
|
2035
|
+
else if (yt(n))
|
|
2036
|
+
t = Uint8Array.from(n);
|
|
2010
2037
|
else
|
|
2011
2038
|
throw new Error(e + " must be hex string or Uint8Array");
|
|
2012
2039
|
return t.length, t;
|
|
2013
2040
|
}
|
|
2014
2041
|
const st = (e) => typeof e == "bigint" && bt <= e;
|
|
2015
|
-
function vn(e,
|
|
2016
|
-
return st(e) && st(
|
|
2042
|
+
function vn(e, n, c) {
|
|
2043
|
+
return st(e) && st(n) && st(c) && n <= e && e < c;
|
|
2017
2044
|
}
|
|
2018
|
-
function _n(e,
|
|
2019
|
-
if (!vn(
|
|
2020
|
-
throw new Error("expected valid " + e + ": " + c + " <= n < " + t + ", got " +
|
|
2045
|
+
function _n(e, n, c, t) {
|
|
2046
|
+
if (!vn(n, c, t))
|
|
2047
|
+
throw new Error("expected valid " + e + ": " + c + " <= n < " + t + ", got " + n);
|
|
2021
2048
|
}
|
|
2022
2049
|
function Xt(e) {
|
|
2023
|
-
let
|
|
2024
|
-
for (
|
|
2050
|
+
let n;
|
|
2051
|
+
for (n = 0; e > bt; e >>= lt, n += 1)
|
|
2025
2052
|
;
|
|
2026
|
-
return
|
|
2053
|
+
return n;
|
|
2027
2054
|
}
|
|
2028
|
-
const
|
|
2029
|
-
function Sn(e,
|
|
2055
|
+
const De = (e) => (lt << BigInt(e)) - lt;
|
|
2056
|
+
function Sn(e, n, c) {
|
|
2030
2057
|
if (typeof e != "number" || e < 2)
|
|
2031
2058
|
throw new Error("hashLen must be a number");
|
|
2032
|
-
if (typeof
|
|
2059
|
+
if (typeof n != "number" || n < 2)
|
|
2033
2060
|
throw new Error("qByteLen must be a number");
|
|
2034
2061
|
if (typeof c != "function")
|
|
2035
2062
|
throw new Error("hmacFn must be a function");
|
|
2036
|
-
const t = (b) => new Uint8Array(b),
|
|
2063
|
+
const t = (b) => new Uint8Array(b), u = (b) => Uint8Array.of(b);
|
|
2037
2064
|
let m = t(e), g = t(e), p = 0;
|
|
2038
2065
|
const v = () => {
|
|
2039
2066
|
m.fill(1), g.fill(0), p = 0;
|
|
2040
2067
|
}, U = (...b) => c(g, m, ...b), H = (b = t(0)) => {
|
|
2041
|
-
g = U(
|
|
2068
|
+
g = U(u(0), b), m = U(), b.length !== 0 && (g = U(u(1), b), m = U());
|
|
2042
2069
|
}, V = () => {
|
|
2043
2070
|
if (p++ >= 1e3)
|
|
2044
2071
|
throw new Error("drbg: tried 1000 values");
|
|
2045
2072
|
let b = 0;
|
|
2046
2073
|
const S = [];
|
|
2047
|
-
for (; b <
|
|
2074
|
+
for (; b < n; ) {
|
|
2048
2075
|
m = U();
|
|
2049
2076
|
const A = m.slice();
|
|
2050
2077
|
S.push(A), b += m.length;
|
|
@@ -2059,71 +2086,71 @@ function Sn(e, r, c) {
|
|
|
2059
2086
|
return v(), A;
|
|
2060
2087
|
};
|
|
2061
2088
|
}
|
|
2062
|
-
function gt(e,
|
|
2089
|
+
function gt(e, n, c = {}) {
|
|
2063
2090
|
if (!e || typeof e != "object")
|
|
2064
2091
|
throw new Error("expected valid options object");
|
|
2065
|
-
function t(
|
|
2066
|
-
const p = e[
|
|
2092
|
+
function t(u, m, g) {
|
|
2093
|
+
const p = e[u];
|
|
2067
2094
|
if (g && p === void 0)
|
|
2068
2095
|
return;
|
|
2069
2096
|
const v = typeof p;
|
|
2070
2097
|
if (v !== m || p === null)
|
|
2071
|
-
throw new Error(`param "${
|
|
2098
|
+
throw new Error(`param "${u}" is invalid: expected ${m}, got ${v}`);
|
|
2072
2099
|
}
|
|
2073
|
-
Object.entries(
|
|
2100
|
+
Object.entries(n).forEach(([u, m]) => t(u, m, !1)), Object.entries(c).forEach(([u, m]) => t(u, m, !0));
|
|
2074
2101
|
}
|
|
2075
2102
|
function At(e) {
|
|
2076
|
-
const
|
|
2103
|
+
const n = /* @__PURE__ */ new WeakMap();
|
|
2077
2104
|
return (c, ...t) => {
|
|
2078
|
-
const
|
|
2079
|
-
if (
|
|
2080
|
-
return
|
|
2105
|
+
const u = n.get(c);
|
|
2106
|
+
if (u !== void 0)
|
|
2107
|
+
return u;
|
|
2081
2108
|
const m = e(c, ...t);
|
|
2082
|
-
return
|
|
2109
|
+
return n.set(c, m), m;
|
|
2083
2110
|
};
|
|
2084
2111
|
}
|
|
2085
|
-
const
|
|
2086
|
-
function qe(e,
|
|
2087
|
-
const c = e %
|
|
2088
|
-
return c >=
|
|
2112
|
+
const Oe = BigInt(0), Be = BigInt(1), je = /* @__PURE__ */ BigInt(2), Gt = /* @__PURE__ */ BigInt(3), Wt = /* @__PURE__ */ BigInt(4), Qt = /* @__PURE__ */ BigInt(5), On = /* @__PURE__ */ BigInt(7), Jt = /* @__PURE__ */ BigInt(8), Tn = /* @__PURE__ */ BigInt(9), Ft = /* @__PURE__ */ BigInt(16);
|
|
2113
|
+
function qe(e, n) {
|
|
2114
|
+
const c = e % n;
|
|
2115
|
+
return c >= Oe ? c : n + c;
|
|
2089
2116
|
}
|
|
2090
|
-
function
|
|
2117
|
+
function xe(e, n, c) {
|
|
2091
2118
|
let t = e;
|
|
2092
|
-
for (;
|
|
2119
|
+
for (; n-- > Oe; )
|
|
2093
2120
|
t *= t, t %= c;
|
|
2094
2121
|
return t;
|
|
2095
2122
|
}
|
|
2096
|
-
function Ut(e,
|
|
2097
|
-
if (e ===
|
|
2123
|
+
function Ut(e, n) {
|
|
2124
|
+
if (e === Oe)
|
|
2098
2125
|
throw new Error("invert: expected non-zero number");
|
|
2099
|
-
if (
|
|
2100
|
-
throw new Error("invert: expected positive modulus, got " +
|
|
2101
|
-
let c = qe(e,
|
|
2102
|
-
for (; c !==
|
|
2103
|
-
const p = t / c, v = t % c, U =
|
|
2104
|
-
t = c, c = v,
|
|
2126
|
+
if (n <= Oe)
|
|
2127
|
+
throw new Error("invert: expected positive modulus, got " + n);
|
|
2128
|
+
let c = qe(e, n), t = n, u = Oe, m = Be;
|
|
2129
|
+
for (; c !== Oe; ) {
|
|
2130
|
+
const p = t / c, v = t % c, U = u - m * p;
|
|
2131
|
+
t = c, c = v, u = m, m = U;
|
|
2105
2132
|
}
|
|
2106
2133
|
if (t !== Be)
|
|
2107
2134
|
throw new Error("invert: does not exist");
|
|
2108
|
-
return qe(
|
|
2135
|
+
return qe(u, n);
|
|
2109
2136
|
}
|
|
2110
|
-
function Et(e,
|
|
2111
|
-
if (!e.eql(e.sqr(
|
|
2137
|
+
function Et(e, n, c) {
|
|
2138
|
+
if (!e.eql(e.sqr(n), c))
|
|
2112
2139
|
throw new Error("Cannot find square root");
|
|
2113
2140
|
}
|
|
2114
|
-
function $t(e,
|
|
2115
|
-
const c = (e.ORDER + Be) / Wt, t = e.pow(
|
|
2116
|
-
return Et(e, t,
|
|
2141
|
+
function $t(e, n) {
|
|
2142
|
+
const c = (e.ORDER + Be) / Wt, t = e.pow(n, c);
|
|
2143
|
+
return Et(e, t, n), t;
|
|
2117
2144
|
}
|
|
2118
|
-
function
|
|
2119
|
-
const c = (e.ORDER - Qt) / Jt, t = e.mul(
|
|
2120
|
-
return Et(e, p,
|
|
2145
|
+
function Rn(e, n) {
|
|
2146
|
+
const c = (e.ORDER - Qt) / Jt, t = e.mul(n, je), u = e.pow(t, c), m = e.mul(n, u), g = e.mul(e.mul(m, je), u), p = e.mul(m, e.sub(g, e.ONE));
|
|
2147
|
+
return Et(e, p, n), p;
|
|
2121
2148
|
}
|
|
2122
|
-
function
|
|
2123
|
-
const
|
|
2149
|
+
function xn(e) {
|
|
2150
|
+
const n = ze(e), c = en(e), t = c(n, n.neg(n.ONE)), u = c(n, t), m = c(n, n.neg(t)), g = (e + On) / Ft;
|
|
2124
2151
|
return (p, v) => {
|
|
2125
2152
|
let U = p.pow(v, g), H = p.mul(U, t);
|
|
2126
|
-
const V = p.mul(U,
|
|
2153
|
+
const V = p.mul(U, u), E = p.mul(U, m), b = p.eql(p.sqr(H), v), S = p.eql(p.sqr(V), v);
|
|
2127
2154
|
U = p.cmov(U, H, b), H = p.cmov(E, V, S);
|
|
2128
2155
|
const A = p.eql(p.sqr(H), v), M = p.cmov(U, H, A);
|
|
2129
2156
|
return Et(p, M, v), M;
|
|
@@ -2132,24 +2159,24 @@ function Rn(e) {
|
|
|
2132
2159
|
function en(e) {
|
|
2133
2160
|
if (e < Gt)
|
|
2134
2161
|
throw new Error("sqrt is not defined for small field");
|
|
2135
|
-
let
|
|
2136
|
-
for (;
|
|
2137
|
-
|
|
2162
|
+
let n = e - Be, c = 0;
|
|
2163
|
+
for (; n % je === Oe; )
|
|
2164
|
+
n /= je, c++;
|
|
2138
2165
|
let t = je;
|
|
2139
|
-
const
|
|
2140
|
-
for (; Zt(
|
|
2166
|
+
const u = ze(e);
|
|
2167
|
+
for (; Zt(u, t) === 1; )
|
|
2141
2168
|
if (t++ > 1e3)
|
|
2142
2169
|
throw new Error("Cannot find square root: probably non-prime P");
|
|
2143
2170
|
if (c === 1)
|
|
2144
2171
|
return $t;
|
|
2145
|
-
let m =
|
|
2146
|
-
const g = (
|
|
2172
|
+
let m = u.pow(t, n);
|
|
2173
|
+
const g = (n + Be) / je;
|
|
2147
2174
|
return function(v, U) {
|
|
2148
2175
|
if (v.is0(U))
|
|
2149
2176
|
return U;
|
|
2150
2177
|
if (Zt(v, U) !== 1)
|
|
2151
2178
|
throw new Error("Cannot find square root");
|
|
2152
|
-
let H = c, V = v.mul(v.ONE, m), E = v.pow(U,
|
|
2179
|
+
let H = c, V = v.mul(v.ONE, m), E = v.pow(U, n), b = v.pow(U, g);
|
|
2153
2180
|
for (; !v.eql(E, v.ONE); ) {
|
|
2154
2181
|
if (v.is0(E))
|
|
2155
2182
|
return v.ZERO;
|
|
@@ -2164,7 +2191,7 @@ function en(e) {
|
|
|
2164
2191
|
};
|
|
2165
2192
|
}
|
|
2166
2193
|
function qn(e) {
|
|
2167
|
-
return e % Wt === Gt ? $t : e % Jt === Qt ?
|
|
2194
|
+
return e % Wt === Gt ? $t : e % Jt === Qt ? Rn : e % Ft === Tn ? xn(e) : en(e);
|
|
2168
2195
|
}
|
|
2169
2196
|
const In = [
|
|
2170
2197
|
"create",
|
|
@@ -2186,53 +2213,53 @@ const In = [
|
|
|
2186
2213
|
"sqrN"
|
|
2187
2214
|
];
|
|
2188
2215
|
function Nn(e) {
|
|
2189
|
-
const
|
|
2216
|
+
const n = {
|
|
2190
2217
|
ORDER: "bigint",
|
|
2191
2218
|
MASK: "bigint",
|
|
2192
2219
|
BYTES: "number",
|
|
2193
2220
|
BITS: "number"
|
|
2194
|
-
}, c = In.reduce((t,
|
|
2221
|
+
}, c = In.reduce((t, u) => (t[u] = "function", t), n);
|
|
2195
2222
|
return gt(e, c), e;
|
|
2196
2223
|
}
|
|
2197
|
-
function An(e,
|
|
2198
|
-
if (c <
|
|
2224
|
+
function An(e, n, c) {
|
|
2225
|
+
if (c < Oe)
|
|
2199
2226
|
throw new Error("invalid exponent, negatives unsupported");
|
|
2200
|
-
if (c ===
|
|
2227
|
+
if (c === Oe)
|
|
2201
2228
|
return e.ONE;
|
|
2202
2229
|
if (c === Be)
|
|
2203
|
-
return
|
|
2204
|
-
let t = e.ONE,
|
|
2205
|
-
for (; c >
|
|
2206
|
-
c & Be && (t = e.mul(t,
|
|
2230
|
+
return n;
|
|
2231
|
+
let t = e.ONE, u = n;
|
|
2232
|
+
for (; c > Oe; )
|
|
2233
|
+
c & Be && (t = e.mul(t, u)), u = e.sqr(u), c >>= Be;
|
|
2207
2234
|
return t;
|
|
2208
2235
|
}
|
|
2209
|
-
function tn(e,
|
|
2210
|
-
const t = new Array(
|
|
2211
|
-
return
|
|
2236
|
+
function tn(e, n, c = !1) {
|
|
2237
|
+
const t = new Array(n.length).fill(c ? e.ZERO : void 0), u = n.reduce((g, p, v) => e.is0(p) ? g : (t[v] = g, e.mul(g, p)), e.ONE), m = e.inv(u);
|
|
2238
|
+
return n.reduceRight((g, p, v) => e.is0(p) ? g : (t[v] = e.mul(g, t[v]), e.mul(g, p)), m), t;
|
|
2212
2239
|
}
|
|
2213
|
-
function Zt(e,
|
|
2214
|
-
const c = (e.ORDER - Be) / je, t = e.pow(
|
|
2215
|
-
if (!
|
|
2240
|
+
function Zt(e, n) {
|
|
2241
|
+
const c = (e.ORDER - Be) / je, t = e.pow(n, c), u = e.eql(t, e.ONE), m = e.eql(t, e.ZERO), g = e.eql(t, e.neg(e.ONE));
|
|
2242
|
+
if (!u && !m && !g)
|
|
2216
2243
|
throw new Error("invalid Legendre symbol result");
|
|
2217
|
-
return
|
|
2244
|
+
return u ? 1 : m ? 0 : -1;
|
|
2218
2245
|
}
|
|
2219
|
-
function nn(e,
|
|
2220
|
-
|
|
2221
|
-
const c =
|
|
2246
|
+
function nn(e, n) {
|
|
2247
|
+
n !== void 0 && yn(n);
|
|
2248
|
+
const c = n !== void 0 ? n : e.toString(2).length, t = Math.ceil(c / 8);
|
|
2222
2249
|
return { nBitLength: c, nByteLength: t };
|
|
2223
2250
|
}
|
|
2224
|
-
function
|
|
2225
|
-
if (e <=
|
|
2251
|
+
function ze(e, n, c = !1, t = {}) {
|
|
2252
|
+
if (e <= Oe)
|
|
2226
2253
|
throw new Error("invalid field: expected ORDER > 0, got " + e);
|
|
2227
|
-
let
|
|
2228
|
-
if (typeof
|
|
2254
|
+
let u, m, g = !1, p;
|
|
2255
|
+
if (typeof n == "object" && n != null) {
|
|
2229
2256
|
if (t.sqrt || c)
|
|
2230
2257
|
throw new Error("cannot specify opts in two arguments");
|
|
2231
|
-
const E =
|
|
2232
|
-
E.BITS && (
|
|
2258
|
+
const E = n;
|
|
2259
|
+
E.BITS && (u = E.BITS), E.sqrt && (m = E.sqrt), typeof E.isLE == "boolean" && (c = E.isLE), typeof E.modFromBytes == "boolean" && (g = E.modFromBytes), p = E.allowedLengths;
|
|
2233
2260
|
} else
|
|
2234
|
-
typeof
|
|
2235
|
-
const { nBitLength: v, nByteLength: U } = nn(e,
|
|
2261
|
+
typeof n == "number" && (u = n), t.sqrt && (m = t.sqrt);
|
|
2262
|
+
const { nBitLength: v, nByteLength: U } = nn(e, u);
|
|
2236
2263
|
if (U > 2048)
|
|
2237
2264
|
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
2238
2265
|
let H;
|
|
@@ -2241,17 +2268,17 @@ function De(e, r, c = !1, t = {}) {
|
|
|
2241
2268
|
isLE: c,
|
|
2242
2269
|
BITS: v,
|
|
2243
2270
|
BYTES: U,
|
|
2244
|
-
MASK:
|
|
2245
|
-
ZERO:
|
|
2271
|
+
MASK: De(v),
|
|
2272
|
+
ZERO: Oe,
|
|
2246
2273
|
ONE: Be,
|
|
2247
2274
|
allowedLengths: p,
|
|
2248
2275
|
create: (E) => qe(E, e),
|
|
2249
2276
|
isValid: (E) => {
|
|
2250
2277
|
if (typeof E != "bigint")
|
|
2251
2278
|
throw new Error("invalid field element: expected bigint, got " + typeof E);
|
|
2252
|
-
return
|
|
2279
|
+
return Oe <= E && E < e;
|
|
2253
2280
|
},
|
|
2254
|
-
is0: (E) => E ===
|
|
2281
|
+
is0: (E) => E === Oe,
|
|
2255
2282
|
// is valid and invertible
|
|
2256
2283
|
isValidNot0: (E) => !V.is0(E) && V.isValid(E),
|
|
2257
2284
|
isOdd: (E) => (E & Be) === Be,
|
|
@@ -2296,58 +2323,58 @@ function De(e, r, c = !1, t = {}) {
|
|
|
2296
2323
|
function rn(e) {
|
|
2297
2324
|
if (typeof e != "bigint")
|
|
2298
2325
|
throw new Error("field order must be bigint");
|
|
2299
|
-
const
|
|
2300
|
-
return Math.ceil(
|
|
2326
|
+
const n = e.toString(2).length;
|
|
2327
|
+
return Math.ceil(n / 8);
|
|
2301
2328
|
}
|
|
2302
2329
|
function on(e) {
|
|
2303
|
-
const
|
|
2304
|
-
return
|
|
2330
|
+
const n = rn(e);
|
|
2331
|
+
return n + Math.ceil(n / 2);
|
|
2305
2332
|
}
|
|
2306
|
-
function Un(e,
|
|
2307
|
-
const t = e.length,
|
|
2333
|
+
function Un(e, n, c = !1) {
|
|
2334
|
+
const t = e.length, u = rn(n), m = on(n);
|
|
2308
2335
|
if (t < 16 || t < m || t > 1024)
|
|
2309
2336
|
throw new Error("expected " + m + "-1024 bytes of input, got " + t);
|
|
2310
|
-
const g = c ? Yt(e) : et(e), p = qe(g,
|
|
2311
|
-
return c ? Pt(p,
|
|
2337
|
+
const g = c ? Yt(e) : et(e), p = qe(g, n - Be) + Be;
|
|
2338
|
+
return c ? Pt(p, u) : wt(p, u);
|
|
2312
2339
|
}
|
|
2313
2340
|
const ke = BigInt(0), Le = BigInt(1);
|
|
2314
|
-
function Fe(e,
|
|
2315
|
-
const c =
|
|
2316
|
-
return e ? c :
|
|
2317
|
-
}
|
|
2318
|
-
function it(e,
|
|
2319
|
-
const c = tn(e.Fp,
|
|
2320
|
-
return
|
|
2321
|
-
}
|
|
2322
|
-
function sn(e,
|
|
2323
|
-
if (!Number.isSafeInteger(e) || e <= 0 || e >
|
|
2324
|
-
throw new Error("invalid window size, expected [1.." +
|
|
2325
|
-
}
|
|
2326
|
-
function ct(e,
|
|
2327
|
-
sn(e,
|
|
2328
|
-
const c = Math.ceil(
|
|
2329
|
-
return { windows: c, windowSize: t, mask: m, maxNumber:
|
|
2330
|
-
}
|
|
2331
|
-
function jt(e,
|
|
2332
|
-
const { windowSize: t, mask:
|
|
2333
|
-
let p = Number(e &
|
|
2341
|
+
function Fe(e, n) {
|
|
2342
|
+
const c = n.negate();
|
|
2343
|
+
return e ? c : n;
|
|
2344
|
+
}
|
|
2345
|
+
function it(e, n) {
|
|
2346
|
+
const c = tn(e.Fp, n.map((t) => t.Z));
|
|
2347
|
+
return n.map((t, u) => e.fromAffine(t.toAffine(c[u])));
|
|
2348
|
+
}
|
|
2349
|
+
function sn(e, n) {
|
|
2350
|
+
if (!Number.isSafeInteger(e) || e <= 0 || e > n)
|
|
2351
|
+
throw new Error("invalid window size, expected [1.." + n + "], got W=" + e);
|
|
2352
|
+
}
|
|
2353
|
+
function ct(e, n) {
|
|
2354
|
+
sn(e, n);
|
|
2355
|
+
const c = Math.ceil(n / e) + 1, t = 2 ** (e - 1), u = 2 ** e, m = De(e), g = BigInt(e);
|
|
2356
|
+
return { windows: c, windowSize: t, mask: m, maxNumber: u, shiftBy: g };
|
|
2357
|
+
}
|
|
2358
|
+
function jt(e, n, c) {
|
|
2359
|
+
const { windowSize: t, mask: u, maxNumber: m, shiftBy: g } = c;
|
|
2360
|
+
let p = Number(e & u), v = e >> g;
|
|
2334
2361
|
p > t && (p -= m, v += Le);
|
|
2335
|
-
const U =
|
|
2362
|
+
const U = n * t, H = U + Math.abs(p) - 1, V = p === 0, E = p < 0, b = n % 2 !== 0;
|
|
2336
2363
|
return { nextN: v, offset: H, isZero: V, isNeg: E, isNegF: b, offsetF: U };
|
|
2337
2364
|
}
|
|
2338
|
-
function Zn(e,
|
|
2365
|
+
function Zn(e, n) {
|
|
2339
2366
|
if (!Array.isArray(e))
|
|
2340
2367
|
throw new Error("array expected");
|
|
2341
2368
|
e.forEach((c, t) => {
|
|
2342
|
-
if (!(c instanceof
|
|
2369
|
+
if (!(c instanceof n))
|
|
2343
2370
|
throw new Error("invalid point at index " + t);
|
|
2344
2371
|
});
|
|
2345
2372
|
}
|
|
2346
|
-
function jn(e,
|
|
2373
|
+
function jn(e, n) {
|
|
2347
2374
|
if (!Array.isArray(e))
|
|
2348
2375
|
throw new Error("array of scalars expected");
|
|
2349
2376
|
e.forEach((c, t) => {
|
|
2350
|
-
if (!
|
|
2377
|
+
if (!n.isValid(c))
|
|
2351
2378
|
throw new Error("invalid scalar at index " + t);
|
|
2352
2379
|
});
|
|
2353
2380
|
}
|
|
@@ -2361,14 +2388,14 @@ function Lt(e) {
|
|
|
2361
2388
|
}
|
|
2362
2389
|
class Ln {
|
|
2363
2390
|
// Parametrized with a given Point class (not individual point)
|
|
2364
|
-
constructor(
|
|
2365
|
-
this.BASE =
|
|
2391
|
+
constructor(n, c) {
|
|
2392
|
+
this.BASE = n.BASE, this.ZERO = n.ZERO, this.Fn = n.Fn, this.bits = c;
|
|
2366
2393
|
}
|
|
2367
2394
|
// non-const time multiplication ladder
|
|
2368
|
-
_unsafeLadder(
|
|
2369
|
-
let
|
|
2395
|
+
_unsafeLadder(n, c, t = this.ZERO) {
|
|
2396
|
+
let u = n;
|
|
2370
2397
|
for (; c > ke; )
|
|
2371
|
-
c & Le && (t = t.add(
|
|
2398
|
+
c & Le && (t = t.add(u)), u = u.double(), c >>= Le;
|
|
2372
2399
|
return t;
|
|
2373
2400
|
}
|
|
2374
2401
|
/**
|
|
@@ -2383,12 +2410,12 @@ class Ln {
|
|
|
2383
2410
|
* @param W window size
|
|
2384
2411
|
* @returns precomputed point tables flattened to a single array
|
|
2385
2412
|
*/
|
|
2386
|
-
precomputeWindow(
|
|
2387
|
-
const { windows: t, windowSize:
|
|
2388
|
-
let g =
|
|
2413
|
+
precomputeWindow(n, c) {
|
|
2414
|
+
const { windows: t, windowSize: u } = ct(c, this.bits), m = [];
|
|
2415
|
+
let g = n, p = g;
|
|
2389
2416
|
for (let v = 0; v < t; v++) {
|
|
2390
2417
|
p = g, m.push(p);
|
|
2391
|
-
for (let U = 1; U <
|
|
2418
|
+
for (let U = 1; U < u; U++)
|
|
2392
2419
|
p = p.add(g), m.push(p);
|
|
2393
2420
|
g = p.double();
|
|
2394
2421
|
}
|
|
@@ -2400,70 +2427,70 @@ class Ln {
|
|
|
2400
2427
|
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
2401
2428
|
* @returns real and fake (for const-time) points
|
|
2402
2429
|
*/
|
|
2403
|
-
wNAF(
|
|
2430
|
+
wNAF(n, c, t) {
|
|
2404
2431
|
if (!this.Fn.isValid(t))
|
|
2405
2432
|
throw new Error("invalid scalar");
|
|
2406
|
-
let
|
|
2407
|
-
const g = ct(
|
|
2433
|
+
let u = this.ZERO, m = this.BASE;
|
|
2434
|
+
const g = ct(n, this.bits);
|
|
2408
2435
|
for (let p = 0; p < g.windows; p++) {
|
|
2409
2436
|
const { nextN: v, offset: U, isZero: H, isNeg: V, isNegF: E, offsetF: b } = jt(t, p, g);
|
|
2410
|
-
t = v, H ? m = m.add(Fe(E, c[b])) :
|
|
2437
|
+
t = v, H ? m = m.add(Fe(E, c[b])) : u = u.add(Fe(V, c[U]));
|
|
2411
2438
|
}
|
|
2412
|
-
return Lt(t), { p:
|
|
2439
|
+
return Lt(t), { p: u, f: m };
|
|
2413
2440
|
}
|
|
2414
2441
|
/**
|
|
2415
2442
|
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2416
2443
|
* @param acc accumulator point to add result of multiplication
|
|
2417
2444
|
* @returns point
|
|
2418
2445
|
*/
|
|
2419
|
-
wNAFUnsafe(
|
|
2420
|
-
const m = ct(
|
|
2446
|
+
wNAFUnsafe(n, c, t, u = this.ZERO) {
|
|
2447
|
+
const m = ct(n, this.bits);
|
|
2421
2448
|
for (let g = 0; g < m.windows && t !== ke; g++) {
|
|
2422
2449
|
const { nextN: p, offset: v, isZero: U, isNeg: H } = jt(t, g, m);
|
|
2423
2450
|
if (t = p, !U) {
|
|
2424
2451
|
const V = c[v];
|
|
2425
|
-
|
|
2452
|
+
u = u.add(H ? V.negate() : V);
|
|
2426
2453
|
}
|
|
2427
2454
|
}
|
|
2428
|
-
return Lt(t),
|
|
2455
|
+
return Lt(t), u;
|
|
2429
2456
|
}
|
|
2430
|
-
getPrecomputes(
|
|
2431
|
-
let
|
|
2432
|
-
return
|
|
2457
|
+
getPrecomputes(n, c, t) {
|
|
2458
|
+
let u = at.get(c);
|
|
2459
|
+
return u || (u = this.precomputeWindow(c, n), n !== 1 && (typeof t == "function" && (u = t(u)), at.set(c, u))), u;
|
|
2433
2460
|
}
|
|
2434
|
-
cached(
|
|
2435
|
-
const
|
|
2436
|
-
return this.wNAF(
|
|
2461
|
+
cached(n, c, t) {
|
|
2462
|
+
const u = ft(n);
|
|
2463
|
+
return this.wNAF(u, this.getPrecomputes(u, n, t), c);
|
|
2437
2464
|
}
|
|
2438
|
-
unsafe(
|
|
2439
|
-
const m = ft(
|
|
2440
|
-
return m === 1 ? this._unsafeLadder(
|
|
2465
|
+
unsafe(n, c, t, u) {
|
|
2466
|
+
const m = ft(n);
|
|
2467
|
+
return m === 1 ? this._unsafeLadder(n, c, u) : this.wNAFUnsafe(m, this.getPrecomputes(m, n, t), c, u);
|
|
2441
2468
|
}
|
|
2442
2469
|
// We calculate precomputes for elliptic curve point multiplication
|
|
2443
2470
|
// using windowed method. This specifies window size and
|
|
2444
2471
|
// stores precomputed values. Usually only base point would be precomputed.
|
|
2445
|
-
createCache(
|
|
2446
|
-
sn(c, this.bits), cn.set(
|
|
2472
|
+
createCache(n, c) {
|
|
2473
|
+
sn(c, this.bits), cn.set(n, c), at.delete(n);
|
|
2447
2474
|
}
|
|
2448
|
-
hasCache(
|
|
2449
|
-
return ft(
|
|
2475
|
+
hasCache(n) {
|
|
2476
|
+
return ft(n) !== 1;
|
|
2450
2477
|
}
|
|
2451
2478
|
}
|
|
2452
|
-
function Hn(e,
|
|
2453
|
-
let
|
|
2479
|
+
function Hn(e, n, c, t) {
|
|
2480
|
+
let u = n, m = e.ZERO, g = e.ZERO;
|
|
2454
2481
|
for (; c > ke || t > ke; )
|
|
2455
|
-
c & Le && (m = m.add(
|
|
2482
|
+
c & Le && (m = m.add(u)), t & Le && (g = g.add(u)), u = u.double(), c >>= Le, t >>= Le;
|
|
2456
2483
|
return { p1: m, p2: g };
|
|
2457
2484
|
}
|
|
2458
|
-
function Mn(e,
|
|
2459
|
-
Zn(c, e), jn(t,
|
|
2460
|
-
const
|
|
2461
|
-
if (
|
|
2485
|
+
function Mn(e, n, c, t) {
|
|
2486
|
+
Zn(c, e), jn(t, n);
|
|
2487
|
+
const u = c.length, m = t.length;
|
|
2488
|
+
if (u !== m)
|
|
2462
2489
|
throw new Error("arrays of points and scalars must have equal length");
|
|
2463
|
-
const g = e.ZERO, p = Xt(BigInt(
|
|
2490
|
+
const g = e.ZERO, p = Xt(BigInt(u));
|
|
2464
2491
|
let v = 1;
|
|
2465
2492
|
p > 12 ? v = p - 3 : p > 4 ? v = p - 2 : p > 0 && (v = 2);
|
|
2466
|
-
const U =
|
|
2493
|
+
const U = De(v), H = new Array(Number(U) + 1).fill(g), V = Math.floor((n.BITS - 1) / v) * v;
|
|
2467
2494
|
let E = g;
|
|
2468
2495
|
for (let b = V; b >= 0; b -= v) {
|
|
2469
2496
|
H.fill(g);
|
|
@@ -2480,35 +2507,35 @@ function Mn(e, r, c, t) {
|
|
|
2480
2507
|
}
|
|
2481
2508
|
return E;
|
|
2482
2509
|
}
|
|
2483
|
-
function Ht(e,
|
|
2484
|
-
if (
|
|
2485
|
-
if (
|
|
2510
|
+
function Ht(e, n, c) {
|
|
2511
|
+
if (n) {
|
|
2512
|
+
if (n.ORDER !== e)
|
|
2486
2513
|
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
2487
|
-
return Nn(
|
|
2514
|
+
return Nn(n), n;
|
|
2488
2515
|
} else
|
|
2489
|
-
return
|
|
2516
|
+
return ze(e, { isLE: c });
|
|
2490
2517
|
}
|
|
2491
|
-
function Vn(e,
|
|
2492
|
-
if (t === void 0 && (t = e === "edwards"), !
|
|
2518
|
+
function Vn(e, n, c = {}, t) {
|
|
2519
|
+
if (t === void 0 && (t = e === "edwards"), !n || typeof n != "object")
|
|
2493
2520
|
throw new Error(`expected valid ${e} CURVE object`);
|
|
2494
2521
|
for (const v of ["p", "n", "h"]) {
|
|
2495
|
-
const U =
|
|
2522
|
+
const U = n[v];
|
|
2496
2523
|
if (!(typeof U == "bigint" && U > ke))
|
|
2497
2524
|
throw new Error(`CURVE.${v} must be positive bigint`);
|
|
2498
2525
|
}
|
|
2499
|
-
const
|
|
2526
|
+
const u = Ht(n.p, c.Fp, t), m = Ht(n.n, c.Fn, t), p = ["Gx", "Gy", "a", "b"];
|
|
2500
2527
|
for (const v of p)
|
|
2501
|
-
if (!
|
|
2528
|
+
if (!u.isValid(n[v]))
|
|
2502
2529
|
throw new Error(`CURVE.${v} must be valid field element of CURVE.Fp`);
|
|
2503
|
-
return
|
|
2530
|
+
return n = Object.freeze(Object.assign({}, n)), { CURVE: n, Fp: u, Fn: m };
|
|
2504
2531
|
}
|
|
2505
|
-
const Mt = (e,
|
|
2506
|
-
function kn(e,
|
|
2507
|
-
const [[t,
|
|
2508
|
-
let U = e - p * t - v * m, H = -p *
|
|
2532
|
+
const Mt = (e, n) => (e + (e >= 0 ? n : -n) / an) / n;
|
|
2533
|
+
function kn(e, n, c) {
|
|
2534
|
+
const [[t, u], [m, g]] = n, p = Mt(g * e, c), v = Mt(-u * e, c);
|
|
2535
|
+
let U = e - p * t - v * m, H = -p * u - v * g;
|
|
2509
2536
|
const V = U < Ne, E = H < Ne;
|
|
2510
2537
|
V && (U = -U), E && (H = -H);
|
|
2511
|
-
const b =
|
|
2538
|
+
const b = De(Math.ceil(Xt(c) / 2)) + Ve;
|
|
2512
2539
|
if (U < Ne || U >= b || H < Ne || H >= b)
|
|
2513
2540
|
throw new Error("splitScalar (endomorphism): failed, k=" + e);
|
|
2514
2541
|
return { k1neg: V, k1: U, k2neg: E, k2: H };
|
|
@@ -2518,15 +2545,15 @@ function dt(e) {
|
|
|
2518
2545
|
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
2519
2546
|
return e;
|
|
2520
2547
|
}
|
|
2521
|
-
function ut(e,
|
|
2548
|
+
function ut(e, n) {
|
|
2522
2549
|
const c = {};
|
|
2523
|
-
for (let t of Object.keys(
|
|
2524
|
-
c[t] = e[t] === void 0 ?
|
|
2550
|
+
for (let t of Object.keys(n))
|
|
2551
|
+
c[t] = e[t] === void 0 ? n[t] : e[t];
|
|
2525
2552
|
return Je(c.lowS, "lowS"), Je(c.prehash, "prehash"), c.format !== void 0 && dt(c.format), c;
|
|
2526
2553
|
}
|
|
2527
2554
|
class Cn extends Error {
|
|
2528
|
-
constructor(
|
|
2529
|
-
super(
|
|
2555
|
+
constructor(n = "") {
|
|
2556
|
+
super(n);
|
|
2530
2557
|
}
|
|
2531
2558
|
}
|
|
2532
2559
|
const Ie = {
|
|
@@ -2534,37 +2561,37 @@ const Ie = {
|
|
|
2534
2561
|
Err: Cn,
|
|
2535
2562
|
// Basic building block is TLV (Tag-Length-Value)
|
|
2536
2563
|
_tlv: {
|
|
2537
|
-
encode: (e,
|
|
2564
|
+
encode: (e, n) => {
|
|
2538
2565
|
const { Err: c } = Ie;
|
|
2539
2566
|
if (e < 0 || e > 256)
|
|
2540
2567
|
throw new c("tlv.encode: wrong tag");
|
|
2541
|
-
if (
|
|
2568
|
+
if (n.length & 1)
|
|
2542
2569
|
throw new c("tlv.encode: unpadded data");
|
|
2543
|
-
const t =
|
|
2544
|
-
if (
|
|
2570
|
+
const t = n.length / 2, u = Xe(t);
|
|
2571
|
+
if (u.length / 2 & 128)
|
|
2545
2572
|
throw new c("tlv.encode: long form length too big");
|
|
2546
|
-
const m = t > 127 ? Xe(
|
|
2547
|
-
return Xe(e) + m +
|
|
2573
|
+
const m = t > 127 ? Xe(u.length / 2 | 128) : "";
|
|
2574
|
+
return Xe(e) + m + u + n;
|
|
2548
2575
|
},
|
|
2549
2576
|
// v - value, l - left bytes (unparsed)
|
|
2550
|
-
decode(e,
|
|
2577
|
+
decode(e, n) {
|
|
2551
2578
|
const { Err: c } = Ie;
|
|
2552
2579
|
let t = 0;
|
|
2553
2580
|
if (e < 0 || e > 256)
|
|
2554
2581
|
throw new c("tlv.encode: wrong tag");
|
|
2555
|
-
if (
|
|
2582
|
+
if (n.length < 2 || n[t++] !== e)
|
|
2556
2583
|
throw new c("tlv.decode: wrong tlv");
|
|
2557
|
-
const
|
|
2584
|
+
const u = n[t++], m = !!(u & 128);
|
|
2558
2585
|
let g = 0;
|
|
2559
2586
|
if (!m)
|
|
2560
|
-
g =
|
|
2587
|
+
g = u;
|
|
2561
2588
|
else {
|
|
2562
|
-
const v =
|
|
2589
|
+
const v = u & 127;
|
|
2563
2590
|
if (!v)
|
|
2564
2591
|
throw new c("tlv.decode(long): indefinite length not supported");
|
|
2565
2592
|
if (v > 4)
|
|
2566
2593
|
throw new c("tlv.decode(long): byte length is too big");
|
|
2567
|
-
const U =
|
|
2594
|
+
const U = n.subarray(t, t + v);
|
|
2568
2595
|
if (U.length !== v)
|
|
2569
2596
|
throw new c("tlv.decode: length bytes not complete");
|
|
2570
2597
|
if (U[0] === 0)
|
|
@@ -2574,10 +2601,10 @@ const Ie = {
|
|
|
2574
2601
|
if (t += v, g < 128)
|
|
2575
2602
|
throw new c("tlv.decode(long): not minimal encoding");
|
|
2576
2603
|
}
|
|
2577
|
-
const p =
|
|
2604
|
+
const p = n.subarray(t, t + g);
|
|
2578
2605
|
if (p.length !== g)
|
|
2579
2606
|
throw new c("tlv.decode: wrong value length");
|
|
2580
|
-
return { v: p, l:
|
|
2607
|
+
return { v: p, l: n.subarray(t + g) };
|
|
2581
2608
|
}
|
|
2582
2609
|
},
|
|
2583
2610
|
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
@@ -2586,59 +2613,59 @@ const Ie = {
|
|
|
2586
2613
|
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
2587
2614
|
_int: {
|
|
2588
2615
|
encode(e) {
|
|
2589
|
-
const { Err:
|
|
2616
|
+
const { Err: n } = Ie;
|
|
2590
2617
|
if (e < Ne)
|
|
2591
|
-
throw new
|
|
2618
|
+
throw new n("integer: negative integers are not allowed");
|
|
2592
2619
|
let c = Xe(e);
|
|
2593
2620
|
if (Number.parseInt(c[0], 16) & 8 && (c = "00" + c), c.length & 1)
|
|
2594
|
-
throw new
|
|
2621
|
+
throw new n("unexpected DER parsing assertion: unpadded hex");
|
|
2595
2622
|
return c;
|
|
2596
2623
|
},
|
|
2597
2624
|
decode(e) {
|
|
2598
|
-
const { Err:
|
|
2625
|
+
const { Err: n } = Ie;
|
|
2599
2626
|
if (e[0] & 128)
|
|
2600
|
-
throw new
|
|
2627
|
+
throw new n("invalid signature integer: negative");
|
|
2601
2628
|
if (e[0] === 0 && !(e[1] & 128))
|
|
2602
|
-
throw new
|
|
2629
|
+
throw new n("invalid signature integer: unnecessary leading zero");
|
|
2603
2630
|
return et(e);
|
|
2604
2631
|
}
|
|
2605
2632
|
},
|
|
2606
2633
|
toSig(e) {
|
|
2607
|
-
const { Err:
|
|
2634
|
+
const { Err: n, _int: c, _tlv: t } = Ie, u = Se("signature", e), { v: m, l: g } = t.decode(48, u);
|
|
2608
2635
|
if (g.length)
|
|
2609
|
-
throw new
|
|
2636
|
+
throw new n("invalid signature: left bytes after parsing");
|
|
2610
2637
|
const { v: p, l: v } = t.decode(2, m), { v: U, l: H } = t.decode(2, v);
|
|
2611
2638
|
if (H.length)
|
|
2612
|
-
throw new
|
|
2639
|
+
throw new n("invalid signature: left bytes after parsing");
|
|
2613
2640
|
return { r: c.decode(p), s: c.decode(U) };
|
|
2614
2641
|
},
|
|
2615
2642
|
hexFromSig(e) {
|
|
2616
|
-
const { _tlv:
|
|
2617
|
-
return
|
|
2643
|
+
const { _tlv: n, _int: c } = Ie, t = n.encode(2, c.encode(e.r)), u = n.encode(2, c.encode(e.s)), m = t + u;
|
|
2644
|
+
return n.encode(48, m);
|
|
2618
2645
|
}
|
|
2619
2646
|
}, Ne = BigInt(0), Ve = BigInt(1), an = BigInt(2), Ge = BigInt(3), Kn = BigInt(4);
|
|
2620
|
-
function He(e,
|
|
2647
|
+
function He(e, n) {
|
|
2621
2648
|
const { BYTES: c } = e;
|
|
2622
2649
|
let t;
|
|
2623
|
-
if (typeof
|
|
2624
|
-
t =
|
|
2650
|
+
if (typeof n == "bigint")
|
|
2651
|
+
t = n;
|
|
2625
2652
|
else {
|
|
2626
|
-
let
|
|
2653
|
+
let u = Se("private key", n);
|
|
2627
2654
|
try {
|
|
2628
|
-
t = e.fromBytes(
|
|
2655
|
+
t = e.fromBytes(u);
|
|
2629
2656
|
} catch {
|
|
2630
|
-
throw new Error(`invalid private key: expected ui8a of size ${c}, got ${typeof
|
|
2657
|
+
throw new Error(`invalid private key: expected ui8a of size ${c}, got ${typeof n}`);
|
|
2631
2658
|
}
|
|
2632
2659
|
}
|
|
2633
2660
|
if (!e.isValidNot0(t))
|
|
2634
2661
|
throw new Error("invalid private key: out of range [1..N-1]");
|
|
2635
2662
|
return t;
|
|
2636
2663
|
}
|
|
2637
|
-
function
|
|
2638
|
-
const c = Vn("weierstrass", e,
|
|
2664
|
+
function Dn(e, n = {}) {
|
|
2665
|
+
const c = Vn("weierstrass", e, n), { Fp: t, Fn: u } = c;
|
|
2639
2666
|
let m = c.CURVE;
|
|
2640
2667
|
const { h: g, n: p } = m;
|
|
2641
|
-
gt(
|
|
2668
|
+
gt(n, {}, {
|
|
2642
2669
|
allowInfinityPoint: "boolean",
|
|
2643
2670
|
clearCofactor: "function",
|
|
2644
2671
|
isTorsionFree: "function",
|
|
@@ -2647,27 +2674,27 @@ function zn(e, r = {}) {
|
|
|
2647
2674
|
endo: "object",
|
|
2648
2675
|
wrapPrivateKey: "boolean"
|
|
2649
2676
|
});
|
|
2650
|
-
const { endo: v } =
|
|
2677
|
+
const { endo: v } = n;
|
|
2651
2678
|
if (v && (!t.is0(m.a) || typeof v.beta != "bigint" || !Array.isArray(v.basises)))
|
|
2652
2679
|
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
2653
|
-
const U = un(t,
|
|
2680
|
+
const U = un(t, u);
|
|
2654
2681
|
function H() {
|
|
2655
2682
|
if (!t.isOdd)
|
|
2656
2683
|
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
2657
2684
|
}
|
|
2658
|
-
function V(
|
|
2659
|
-
const { x:
|
|
2660
|
-
if (Je(
|
|
2685
|
+
function V(D, d, l) {
|
|
2686
|
+
const { x: r, y: a } = d.toAffine(), i = t.toBytes(r);
|
|
2687
|
+
if (Je(l, "isCompressed"), l) {
|
|
2661
2688
|
H();
|
|
2662
2689
|
const o = !t.isOdd(a);
|
|
2663
2690
|
return Ae(fn(o), i);
|
|
2664
2691
|
} else
|
|
2665
2692
|
return Ae(Uint8Array.of(4), i, t.toBytes(a));
|
|
2666
2693
|
}
|
|
2667
|
-
function E(
|
|
2668
|
-
Ze(
|
|
2669
|
-
const { publicKey: d, publicKeyUncompressed:
|
|
2670
|
-
if (
|
|
2694
|
+
function E(D) {
|
|
2695
|
+
Ze(D, void 0, "Point");
|
|
2696
|
+
const { publicKey: d, publicKeyUncompressed: l } = U, r = D.length, a = D[0], i = D.subarray(1);
|
|
2697
|
+
if (r === d && (a === 2 || a === 3)) {
|
|
2671
2698
|
const o = t.fromBytes(i);
|
|
2672
2699
|
if (!t.isValid(o))
|
|
2673
2700
|
throw new Error("bad point: is not on curve, wrong x");
|
|
@@ -2682,92 +2709,92 @@ function zn(e, r = {}) {
|
|
|
2682
2709
|
H();
|
|
2683
2710
|
const s = t.isOdd(w);
|
|
2684
2711
|
return (a & 1) === 1 !== s && (w = t.neg(w)), { x: o, y: w };
|
|
2685
|
-
} else if (
|
|
2712
|
+
} else if (r === l && a === 4) {
|
|
2686
2713
|
const o = t.BYTES, h = t.fromBytes(i.subarray(0, o)), w = t.fromBytes(i.subarray(o, o * 2));
|
|
2687
2714
|
if (!M(h, w))
|
|
2688
2715
|
throw new Error("bad point: is not on curve");
|
|
2689
2716
|
return { x: h, y: w };
|
|
2690
2717
|
} else
|
|
2691
|
-
throw new Error(`bad point: got length ${
|
|
2718
|
+
throw new Error(`bad point: got length ${r}, expected compressed=${d} or uncompressed=${l}`);
|
|
2692
2719
|
}
|
|
2693
|
-
const b =
|
|
2694
|
-
function A(
|
|
2695
|
-
const d = t.sqr(
|
|
2696
|
-
return t.add(t.add(
|
|
2720
|
+
const b = n.toBytes || V, S = n.fromBytes || E;
|
|
2721
|
+
function A(D) {
|
|
2722
|
+
const d = t.sqr(D), l = t.mul(d, D);
|
|
2723
|
+
return t.add(t.add(l, t.mul(D, m.a)), m.b);
|
|
2697
2724
|
}
|
|
2698
|
-
function M(
|
|
2699
|
-
const
|
|
2700
|
-
return t.eql(
|
|
2725
|
+
function M(D, d) {
|
|
2726
|
+
const l = t.sqr(d), r = A(D);
|
|
2727
|
+
return t.eql(l, r);
|
|
2701
2728
|
}
|
|
2702
2729
|
if (!M(m.Gx, m.Gy))
|
|
2703
2730
|
throw new Error("bad curve params: generator point");
|
|
2704
2731
|
const Q = t.mul(t.pow(m.a, Ge), Kn), F = t.mul(t.sqr(m.b), BigInt(27));
|
|
2705
2732
|
if (t.is0(t.add(Q, F)))
|
|
2706
2733
|
throw new Error("bad curve params: a or b");
|
|
2707
|
-
function G(
|
|
2708
|
-
if (!t.isValid(d) ||
|
|
2709
|
-
throw new Error(`bad point coordinate ${
|
|
2734
|
+
function G(D, d, l = !1) {
|
|
2735
|
+
if (!t.isValid(d) || l && t.is0(d))
|
|
2736
|
+
throw new Error(`bad point coordinate ${D}`);
|
|
2710
2737
|
return d;
|
|
2711
2738
|
}
|
|
2712
|
-
function se(
|
|
2713
|
-
if (!(
|
|
2739
|
+
function se(D) {
|
|
2740
|
+
if (!(D instanceof _))
|
|
2714
2741
|
throw new Error("ProjectivePoint expected");
|
|
2715
2742
|
}
|
|
2716
|
-
function ie(
|
|
2743
|
+
function ie(D) {
|
|
2717
2744
|
if (!v || !v.basises)
|
|
2718
2745
|
throw new Error("no endo");
|
|
2719
|
-
return kn(
|
|
2746
|
+
return kn(D, v.basises, u.ORDER);
|
|
2720
2747
|
}
|
|
2721
|
-
const j = At((
|
|
2722
|
-
const { X:
|
|
2748
|
+
const j = At((D, d) => {
|
|
2749
|
+
const { X: l, Y: r, Z: a } = D;
|
|
2723
2750
|
if (t.eql(a, t.ONE))
|
|
2724
|
-
return { x:
|
|
2725
|
-
const i =
|
|
2751
|
+
return { x: l, y: r };
|
|
2752
|
+
const i = D.is0();
|
|
2726
2753
|
d == null && (d = i ? t.ONE : t.inv(a));
|
|
2727
|
-
const o = t.mul(
|
|
2754
|
+
const o = t.mul(l, d), h = t.mul(r, d), w = t.mul(a, d);
|
|
2728
2755
|
if (i)
|
|
2729
2756
|
return { x: t.ZERO, y: t.ZERO };
|
|
2730
2757
|
if (!t.eql(w, t.ONE))
|
|
2731
2758
|
throw new Error("invZ was invalid");
|
|
2732
2759
|
return { x: o, y: h };
|
|
2733
|
-
}),
|
|
2734
|
-
if (
|
|
2735
|
-
if (
|
|
2760
|
+
}), R = At((D) => {
|
|
2761
|
+
if (D.is0()) {
|
|
2762
|
+
if (n.allowInfinityPoint && !t.is0(D.Y))
|
|
2736
2763
|
return;
|
|
2737
2764
|
throw new Error("bad point: ZERO");
|
|
2738
2765
|
}
|
|
2739
|
-
const { x: d, y:
|
|
2740
|
-
if (!t.isValid(d) || !t.isValid(
|
|
2766
|
+
const { x: d, y: l } = D.toAffine();
|
|
2767
|
+
if (!t.isValid(d) || !t.isValid(l))
|
|
2741
2768
|
throw new Error("bad point: x or y not field elements");
|
|
2742
|
-
if (!M(d,
|
|
2769
|
+
if (!M(d, l))
|
|
2743
2770
|
throw new Error("bad point: equation left != right");
|
|
2744
|
-
if (!
|
|
2771
|
+
if (!D.isTorsionFree())
|
|
2745
2772
|
throw new Error("bad point: not in prime-order subgroup");
|
|
2746
2773
|
return !0;
|
|
2747
2774
|
});
|
|
2748
|
-
function L(
|
|
2749
|
-
return
|
|
2775
|
+
function L(D, d, l, r, a) {
|
|
2776
|
+
return l = new _(t.mul(l.X, D), l.Y, l.Z), d = Fe(r, d), l = Fe(a, l), d.add(l);
|
|
2750
2777
|
}
|
|
2751
2778
|
class _ {
|
|
2752
2779
|
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2753
|
-
constructor(d,
|
|
2754
|
-
this.X = G("x", d), this.Y = G("y",
|
|
2780
|
+
constructor(d, l, r) {
|
|
2781
|
+
this.X = G("x", d), this.Y = G("y", l, !0), this.Z = G("z", r), Object.freeze(this);
|
|
2755
2782
|
}
|
|
2756
2783
|
static CURVE() {
|
|
2757
2784
|
return m;
|
|
2758
2785
|
}
|
|
2759
2786
|
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2760
2787
|
static fromAffine(d) {
|
|
2761
|
-
const { x:
|
|
2762
|
-
if (!d || !t.isValid(
|
|
2788
|
+
const { x: l, y: r } = d || {};
|
|
2789
|
+
if (!d || !t.isValid(l) || !t.isValid(r))
|
|
2763
2790
|
throw new Error("invalid affine point");
|
|
2764
2791
|
if (d instanceof _)
|
|
2765
2792
|
throw new Error("projective point not allowed");
|
|
2766
|
-
return t.is0(
|
|
2793
|
+
return t.is0(l) && t.is0(r) ? _.ZERO : new _(l, r, t.ONE);
|
|
2767
2794
|
}
|
|
2768
2795
|
static fromBytes(d) {
|
|
2769
|
-
const
|
|
2770
|
-
return
|
|
2796
|
+
const l = _.fromAffine(S(Ze(d, void 0, "point")));
|
|
2797
|
+
return l.assertValidity(), l;
|
|
2771
2798
|
}
|
|
2772
2799
|
static fromHex(d) {
|
|
2773
2800
|
return _.fromBytes(Se("pointHex", d));
|
|
@@ -2784,13 +2811,13 @@ function zn(e, r = {}) {
|
|
|
2784
2811
|
* @param isLazy true will defer table computation until the first multiplication
|
|
2785
2812
|
* @returns
|
|
2786
2813
|
*/
|
|
2787
|
-
precompute(d = 8,
|
|
2788
|
-
return W.createCache(this, d),
|
|
2814
|
+
precompute(d = 8, l = !0) {
|
|
2815
|
+
return W.createCache(this, d), l || this.multiply(Ge), this;
|
|
2789
2816
|
}
|
|
2790
2817
|
// TODO: return `this`
|
|
2791
2818
|
/** A point on curve is valid if it conforms to equation. */
|
|
2792
2819
|
assertValidity() {
|
|
2793
|
-
|
|
2820
|
+
R(this);
|
|
2794
2821
|
}
|
|
2795
2822
|
hasEvenY() {
|
|
2796
2823
|
const { y: d } = this.toAffine();
|
|
@@ -2801,7 +2828,7 @@ function zn(e, r = {}) {
|
|
|
2801
2828
|
/** Compare one point to another. */
|
|
2802
2829
|
equals(d) {
|
|
2803
2830
|
se(d);
|
|
2804
|
-
const { X:
|
|
2831
|
+
const { X: l, Y: r, Z: a } = this, { X: i, Y: o, Z: h } = d, w = t.eql(t.mul(l, h), t.mul(i, a)), s = t.eql(t.mul(r, h), t.mul(o, a));
|
|
2805
2832
|
return w && s;
|
|
2806
2833
|
}
|
|
2807
2834
|
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
@@ -2813,9 +2840,9 @@ function zn(e, r = {}) {
|
|
|
2813
2840
|
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
2814
2841
|
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
2815
2842
|
double() {
|
|
2816
|
-
const { a: d, b:
|
|
2817
|
-
let h = t.ZERO, w = t.ZERO, s = t.ZERO, f = t.mul(a, a), y = t.mul(i, i), B = t.mul(o, o),
|
|
2818
|
-
return
|
|
2843
|
+
const { a: d, b: l } = m, r = t.mul(l, Ge), { X: a, Y: i, Z: o } = this;
|
|
2844
|
+
let h = t.ZERO, w = t.ZERO, s = t.ZERO, f = t.mul(a, a), y = t.mul(i, i), B = t.mul(o, o), O = t.mul(a, i);
|
|
2845
|
+
return O = t.add(O, O), s = t.mul(a, o), s = t.add(s, s), h = t.mul(d, s), w = t.mul(r, B), w = t.add(h, w), h = t.sub(y, w), w = t.add(y, w), w = t.mul(h, w), h = t.mul(O, h), s = t.mul(r, s), B = t.mul(d, B), O = t.sub(f, B), O = t.mul(d, O), O = t.add(O, s), s = t.add(f, f), f = t.add(s, f), f = t.add(f, B), f = t.mul(f, O), w = t.add(w, f), B = t.mul(i, o), B = t.add(B, B), f = t.mul(B, O), h = t.sub(h, f), s = t.mul(B, y), s = t.add(s, s), s = t.add(s, s), new _(h, w, s);
|
|
2819
2846
|
}
|
|
2820
2847
|
// Renes-Costello-Batina exception-free addition formula.
|
|
2821
2848
|
// There is 30% faster Jacobian formula, but it is not complete.
|
|
@@ -2823,13 +2850,13 @@ function zn(e, r = {}) {
|
|
|
2823
2850
|
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
2824
2851
|
add(d) {
|
|
2825
2852
|
se(d);
|
|
2826
|
-
const { X:
|
|
2853
|
+
const { X: l, Y: r, Z: a } = this, { X: i, Y: o, Z: h } = d;
|
|
2827
2854
|
let w = t.ZERO, s = t.ZERO, f = t.ZERO;
|
|
2828
2855
|
const y = m.a, B = t.mul(m.b, Ge);
|
|
2829
|
-
let
|
|
2830
|
-
I = t.mul(I, q), q = t.add(
|
|
2856
|
+
let O = t.mul(l, i), k = t.mul(r, o), Y = t.mul(a, h), I = t.add(l, r), q = t.add(i, o);
|
|
2857
|
+
I = t.mul(I, q), q = t.add(O, k), I = t.sub(I, q), q = t.add(l, a);
|
|
2831
2858
|
let C = t.add(i, h);
|
|
2832
|
-
return q = t.mul(q, C), C = t.add(
|
|
2859
|
+
return q = t.mul(q, C), C = t.add(O, Y), q = t.sub(q, C), C = t.add(r, a), w = t.add(o, h), C = t.mul(C, w), w = t.add(k, Y), C = t.sub(C, w), f = t.mul(y, q), w = t.mul(B, Y), f = t.add(w, f), w = t.sub(k, f), f = t.add(k, f), s = t.mul(w, f), k = t.add(O, O), k = t.add(k, O), Y = t.mul(y, Y), q = t.mul(B, q), k = t.add(k, Y), Y = t.sub(O, Y), Y = t.mul(y, Y), q = t.add(q, Y), O = t.mul(k, q), s = t.add(s, O), O = t.mul(C, q), w = t.mul(I, w), w = t.sub(w, O), O = t.mul(I, k), f = t.mul(C, f), f = t.add(f, O), new _(w, s, f);
|
|
2833
2860
|
}
|
|
2834
2861
|
subtract(d) {
|
|
2835
2862
|
return this.add(d.negate());
|
|
@@ -2847,19 +2874,19 @@ function zn(e, r = {}) {
|
|
|
2847
2874
|
* @returns New point
|
|
2848
2875
|
*/
|
|
2849
2876
|
multiply(d) {
|
|
2850
|
-
const { endo:
|
|
2851
|
-
if (!
|
|
2877
|
+
const { endo: l } = n;
|
|
2878
|
+
if (!u.isValidNot0(d))
|
|
2852
2879
|
throw new Error("invalid scalar: out of range");
|
|
2853
|
-
let
|
|
2880
|
+
let r, a;
|
|
2854
2881
|
const i = (o) => W.cached(this, o, (h) => it(_, h));
|
|
2855
|
-
if (
|
|
2856
|
-
const { k1neg: o, k1: h, k2neg: w, k2: s } = ie(d), { p: f, f: y } = i(h), { p: B, f:
|
|
2857
|
-
a = y.add(
|
|
2882
|
+
if (l) {
|
|
2883
|
+
const { k1neg: o, k1: h, k2neg: w, k2: s } = ie(d), { p: f, f: y } = i(h), { p: B, f: O } = i(s);
|
|
2884
|
+
a = y.add(O), r = L(l.beta, f, B, o, w);
|
|
2858
2885
|
} else {
|
|
2859
2886
|
const { p: o, f: h } = i(d);
|
|
2860
|
-
|
|
2887
|
+
r = o, a = h;
|
|
2861
2888
|
}
|
|
2862
|
-
return it(_, [
|
|
2889
|
+
return it(_, [r, a])[0];
|
|
2863
2890
|
}
|
|
2864
2891
|
/**
|
|
2865
2892
|
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
@@ -2867,23 +2894,23 @@ function zn(e, r = {}) {
|
|
|
2867
2894
|
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
2868
2895
|
*/
|
|
2869
2896
|
multiplyUnsafe(d) {
|
|
2870
|
-
const { endo:
|
|
2871
|
-
if (!
|
|
2897
|
+
const { endo: l } = n, r = this;
|
|
2898
|
+
if (!u.isValid(d))
|
|
2872
2899
|
throw new Error("invalid scalar: out of range");
|
|
2873
|
-
if (d === Ne ||
|
|
2900
|
+
if (d === Ne || r.is0())
|
|
2874
2901
|
return _.ZERO;
|
|
2875
2902
|
if (d === Ve)
|
|
2876
|
-
return
|
|
2903
|
+
return r;
|
|
2877
2904
|
if (W.hasCache(this))
|
|
2878
2905
|
return this.multiply(d);
|
|
2879
|
-
if (
|
|
2880
|
-
const { k1neg: a, k1: i, k2neg: o, k2: h } = ie(d), { p1: w, p2: s } = Hn(_,
|
|
2881
|
-
return L(
|
|
2906
|
+
if (l) {
|
|
2907
|
+
const { k1neg: a, k1: i, k2neg: o, k2: h } = ie(d), { p1: w, p2: s } = Hn(_, r, i, h);
|
|
2908
|
+
return L(l.beta, w, s, a, o);
|
|
2882
2909
|
} else
|
|
2883
|
-
return W.unsafe(
|
|
2910
|
+
return W.unsafe(r, d);
|
|
2884
2911
|
}
|
|
2885
|
-
multiplyAndAddUnsafe(d,
|
|
2886
|
-
const a = this.multiplyUnsafe(
|
|
2912
|
+
multiplyAndAddUnsafe(d, l, r) {
|
|
2913
|
+
const a = this.multiplyUnsafe(l).add(d.multiplyUnsafe(r));
|
|
2887
2914
|
return a.is0() ? void 0 : a;
|
|
2888
2915
|
}
|
|
2889
2916
|
/**
|
|
@@ -2898,11 +2925,11 @@ function zn(e, r = {}) {
|
|
|
2898
2925
|
* Always torsion-free for cofactor=1 curves.
|
|
2899
2926
|
*/
|
|
2900
2927
|
isTorsionFree() {
|
|
2901
|
-
const { isTorsionFree: d } =
|
|
2928
|
+
const { isTorsionFree: d } = n;
|
|
2902
2929
|
return g === Ve ? !0 : d ? d(_, this) : W.unsafe(this, p).is0();
|
|
2903
2930
|
}
|
|
2904
2931
|
clearCofactor() {
|
|
2905
|
-
const { clearCofactor: d } =
|
|
2932
|
+
const { clearCofactor: d } = n;
|
|
2906
2933
|
return g === Ve ? this : d ? d(_, this) : this.multiplyUnsafe(g);
|
|
2907
2934
|
}
|
|
2908
2935
|
isSmallOrder() {
|
|
@@ -2936,31 +2963,31 @@ function zn(e, r = {}) {
|
|
|
2936
2963
|
static normalizeZ(d) {
|
|
2937
2964
|
return it(_, d);
|
|
2938
2965
|
}
|
|
2939
|
-
static msm(d,
|
|
2940
|
-
return Mn(_,
|
|
2966
|
+
static msm(d, l) {
|
|
2967
|
+
return Mn(_, u, d, l);
|
|
2941
2968
|
}
|
|
2942
2969
|
static fromPrivateKey(d) {
|
|
2943
|
-
return _.BASE.multiply(He(
|
|
2970
|
+
return _.BASE.multiply(He(u, d));
|
|
2944
2971
|
}
|
|
2945
2972
|
}
|
|
2946
|
-
_.BASE = new _(m.Gx, m.Gy, t.ONE), _.ZERO = new _(t.ZERO, t.ONE, t.ZERO), _.Fp = t, _.Fn =
|
|
2947
|
-
const K =
|
|
2973
|
+
_.BASE = new _(m.Gx, m.Gy, t.ONE), _.ZERO = new _(t.ZERO, t.ONE, t.ZERO), _.Fp = t, _.Fn = u;
|
|
2974
|
+
const K = u.BITS, W = new Ln(_, n.endo ? Math.ceil(K / 2) : K);
|
|
2948
2975
|
return _.BASE.precompute(8), _;
|
|
2949
2976
|
}
|
|
2950
2977
|
function fn(e) {
|
|
2951
2978
|
return Uint8Array.of(e ? 2 : 3);
|
|
2952
2979
|
}
|
|
2953
|
-
function un(e,
|
|
2980
|
+
function un(e, n) {
|
|
2954
2981
|
return {
|
|
2955
|
-
secretKey:
|
|
2982
|
+
secretKey: n.BYTES,
|
|
2956
2983
|
publicKey: 1 + e.BYTES,
|
|
2957
2984
|
publicKeyUncompressed: 1 + 2 * e.BYTES,
|
|
2958
2985
|
publicKeyHasPrefix: !0,
|
|
2959
|
-
signature: 2 *
|
|
2986
|
+
signature: 2 * n.BYTES
|
|
2960
2987
|
};
|
|
2961
2988
|
}
|
|
2962
|
-
function
|
|
2963
|
-
const { Fn: c } = e, t =
|
|
2989
|
+
function zn(e, n = {}) {
|
|
2990
|
+
const { Fn: c } = e, t = n.randomBytes || kt, u = Object.assign(un(e.Fp, c), { seed: on(c.ORDER) });
|
|
2964
2991
|
function m(b) {
|
|
2965
2992
|
try {
|
|
2966
2993
|
return !!He(c, b);
|
|
@@ -2969,7 +2996,7 @@ function Dn(e, r = {}) {
|
|
|
2969
2996
|
}
|
|
2970
2997
|
}
|
|
2971
2998
|
function g(b, S) {
|
|
2972
|
-
const { publicKey: A, publicKeyUncompressed: M } =
|
|
2999
|
+
const { publicKey: A, publicKeyUncompressed: M } = u;
|
|
2973
3000
|
try {
|
|
2974
3001
|
const Q = b.length;
|
|
2975
3002
|
return S === !0 && Q !== A || S === !1 && Q !== M ? !1 : !!e.fromBytes(b);
|
|
@@ -2977,8 +3004,8 @@ function Dn(e, r = {}) {
|
|
|
2977
3004
|
return !1;
|
|
2978
3005
|
}
|
|
2979
3006
|
}
|
|
2980
|
-
function p(b = t(
|
|
2981
|
-
return Un(Ze(b,
|
|
3007
|
+
function p(b = t(u.seed)) {
|
|
3008
|
+
return Un(Ze(b, u.seed, "seed"), c.ORDER);
|
|
2982
3009
|
}
|
|
2983
3010
|
function v(b, S = !0) {
|
|
2984
3011
|
return e.BASE.multiply(He(c, b)).toBytes(S);
|
|
@@ -2992,7 +3019,7 @@ function Dn(e, r = {}) {
|
|
|
2992
3019
|
return !1;
|
|
2993
3020
|
if (b instanceof e)
|
|
2994
3021
|
return !0;
|
|
2995
|
-
const { secretKey: S, publicKey: A, publicKeyUncompressed: M } =
|
|
3022
|
+
const { secretKey: S, publicKey: A, publicKeyUncompressed: M } = u;
|
|
2996
3023
|
if (c.allowedLengths || S === A)
|
|
2997
3024
|
return;
|
|
2998
3025
|
const Q = Se("key", b).length;
|
|
@@ -3017,59 +3044,59 @@ function Dn(e, r = {}) {
|
|
|
3017
3044
|
precompute(b = 8, S = e.BASE) {
|
|
3018
3045
|
return S.precompute(b, !1);
|
|
3019
3046
|
}
|
|
3020
|
-
}, lengths:
|
|
3047
|
+
}, lengths: u });
|
|
3021
3048
|
}
|
|
3022
|
-
function Yn(e,
|
|
3023
|
-
mn(
|
|
3049
|
+
function Yn(e, n, c = {}) {
|
|
3050
|
+
mn(n), gt(c, {}, {
|
|
3024
3051
|
hmac: "function",
|
|
3025
3052
|
lowS: "boolean",
|
|
3026
3053
|
randomBytes: "function",
|
|
3027
3054
|
bits2int: "function",
|
|
3028
3055
|
bits2int_modN: "function"
|
|
3029
3056
|
});
|
|
3030
|
-
const t = c.randomBytes || kt,
|
|
3057
|
+
const t = c.randomBytes || kt, u = c.hmac || ((l, ...r) => bn(n, l, Ae(...r))), { Fp: m, Fn: g } = e, { ORDER: p, BITS: v } = g, { keygen: U, getPublicKey: H, getSharedSecret: V, utils: E, lengths: b } = zn(e, c), S = {
|
|
3031
3058
|
prehash: !1,
|
|
3032
3059
|
lowS: typeof c.lowS == "boolean" ? c.lowS : !1,
|
|
3033
3060
|
format: void 0,
|
|
3034
3061
|
//'compact' as ECDSASigFormat,
|
|
3035
3062
|
extraEntropy: !1
|
|
3036
3063
|
}, A = "compact";
|
|
3037
|
-
function M(
|
|
3038
|
-
const
|
|
3039
|
-
return
|
|
3064
|
+
function M(l) {
|
|
3065
|
+
const r = p >> Ve;
|
|
3066
|
+
return l > r;
|
|
3040
3067
|
}
|
|
3041
|
-
function Q(
|
|
3042
|
-
if (!g.isValidNot0(
|
|
3043
|
-
throw new Error(`invalid signature ${
|
|
3044
|
-
return
|
|
3068
|
+
function Q(l, r) {
|
|
3069
|
+
if (!g.isValidNot0(r))
|
|
3070
|
+
throw new Error(`invalid signature ${l}: out of range 1..Point.Fn.ORDER`);
|
|
3071
|
+
return r;
|
|
3045
3072
|
}
|
|
3046
|
-
function F(
|
|
3047
|
-
dt(
|
|
3048
|
-
const a = b.signature, i =
|
|
3049
|
-
return Ze(
|
|
3073
|
+
function F(l, r) {
|
|
3074
|
+
dt(r);
|
|
3075
|
+
const a = b.signature, i = r === "compact" ? a : r === "recovered" ? a + 1 : void 0;
|
|
3076
|
+
return Ze(l, i, `${r} signature`);
|
|
3050
3077
|
}
|
|
3051
3078
|
class G {
|
|
3052
|
-
constructor(
|
|
3053
|
-
this.r = Q("r",
|
|
3079
|
+
constructor(r, a, i) {
|
|
3080
|
+
this.r = Q("r", r), this.s = Q("s", a), i != null && (this.recovery = i), Object.freeze(this);
|
|
3054
3081
|
}
|
|
3055
|
-
static fromBytes(
|
|
3056
|
-
F(
|
|
3082
|
+
static fromBytes(r, a = A) {
|
|
3083
|
+
F(r, a);
|
|
3057
3084
|
let i;
|
|
3058
3085
|
if (a === "der") {
|
|
3059
|
-
const { r: s, s: f } = Ie.toSig(Ze(
|
|
3086
|
+
const { r: s, s: f } = Ie.toSig(Ze(r));
|
|
3060
3087
|
return new G(s, f);
|
|
3061
3088
|
}
|
|
3062
|
-
a === "recovered" && (i =
|
|
3063
|
-
const o = g.BYTES, h =
|
|
3089
|
+
a === "recovered" && (i = r[0], a = "compact", r = r.subarray(1));
|
|
3090
|
+
const o = g.BYTES, h = r.subarray(0, o), w = r.subarray(o, o * 2);
|
|
3064
3091
|
return new G(g.fromBytes(h), g.fromBytes(w), i);
|
|
3065
3092
|
}
|
|
3066
|
-
static fromHex(
|
|
3067
|
-
return this.fromBytes(Qe(
|
|
3093
|
+
static fromHex(r, a) {
|
|
3094
|
+
return this.fromBytes(Qe(r), a);
|
|
3068
3095
|
}
|
|
3069
|
-
addRecoveryBit(
|
|
3070
|
-
return new G(this.r, this.s,
|
|
3096
|
+
addRecoveryBit(r) {
|
|
3097
|
+
return new G(this.r, this.s, r);
|
|
3071
3098
|
}
|
|
3072
|
-
recoverPublicKey(
|
|
3099
|
+
recoverPublicKey(r) {
|
|
3073
3100
|
const a = m.ORDER, { r: i, s: o, recovery: h } = this;
|
|
3074
3101
|
if (h == null || ![0, 1, 2, 3].includes(h))
|
|
3075
3102
|
throw new Error("recovery id invalid");
|
|
@@ -3078,7 +3105,7 @@ function Yn(e, r, c = {}) {
|
|
|
3078
3105
|
const s = h === 2 || h === 3 ? i + p : i;
|
|
3079
3106
|
if (!m.isValid(s))
|
|
3080
3107
|
throw new Error("recovery id 2 or 3 invalid");
|
|
3081
|
-
const f = m.toBytes(s), y = e.fromBytes(Ae(fn((h & 1) === 0), f)), B = g.inv(s),
|
|
3108
|
+
const f = m.toBytes(s), y = e.fromBytes(Ae(fn((h & 1) === 0), f)), B = g.inv(s), O = ie(Se("msgHash", r)), k = g.create(-O * B), Y = g.create(o * B), I = e.BASE.multiplyUnsafe(k).add(y.multiplyUnsafe(Y));
|
|
3082
3109
|
if (I.is0())
|
|
3083
3110
|
throw new Error("point at infinify");
|
|
3084
3111
|
return I.assertValidity(), I;
|
|
@@ -3087,28 +3114,28 @@ function Yn(e, r, c = {}) {
|
|
|
3087
3114
|
hasHighS() {
|
|
3088
3115
|
return M(this.s);
|
|
3089
3116
|
}
|
|
3090
|
-
toBytes(
|
|
3091
|
-
if (dt(
|
|
3117
|
+
toBytes(r = A) {
|
|
3118
|
+
if (dt(r), r === "der")
|
|
3092
3119
|
return Qe(Ie.hexFromSig(this));
|
|
3093
3120
|
const a = g.toBytes(this.r), i = g.toBytes(this.s);
|
|
3094
|
-
if (
|
|
3121
|
+
if (r === "recovered") {
|
|
3095
3122
|
if (this.recovery == null)
|
|
3096
3123
|
throw new Error("recovery bit must be present");
|
|
3097
3124
|
return Ae(Uint8Array.of(this.recovery), a, i);
|
|
3098
3125
|
}
|
|
3099
3126
|
return Ae(a, i);
|
|
3100
3127
|
}
|
|
3101
|
-
toHex(
|
|
3102
|
-
return Me(this.toBytes(
|
|
3128
|
+
toHex(r) {
|
|
3129
|
+
return Me(this.toBytes(r));
|
|
3103
3130
|
}
|
|
3104
3131
|
// TODO: remove
|
|
3105
3132
|
assertValidity() {
|
|
3106
3133
|
}
|
|
3107
|
-
static fromCompact(
|
|
3108
|
-
return G.fromBytes(Se("sig",
|
|
3134
|
+
static fromCompact(r) {
|
|
3135
|
+
return G.fromBytes(Se("sig", r), "compact");
|
|
3109
3136
|
}
|
|
3110
|
-
static fromDER(
|
|
3111
|
-
return G.fromBytes(Se("sig",
|
|
3137
|
+
static fromDER(r) {
|
|
3138
|
+
return G.fromBytes(Se("sig", r), "der");
|
|
3112
3139
|
}
|
|
3113
3140
|
normalizeS() {
|
|
3114
3141
|
return this.hasHighS() ? new G(this.r, g.neg(this.s), this.recovery) : this;
|
|
@@ -3126,32 +3153,32 @@ function Yn(e, r, c = {}) {
|
|
|
3126
3153
|
return Me(this.toBytes("compact"));
|
|
3127
3154
|
}
|
|
3128
3155
|
}
|
|
3129
|
-
const se = c.bits2int || function(
|
|
3130
|
-
if (
|
|
3156
|
+
const se = c.bits2int || function(r) {
|
|
3157
|
+
if (r.length > 8192)
|
|
3131
3158
|
throw new Error("input is too large");
|
|
3132
|
-
const a = et(
|
|
3159
|
+
const a = et(r), i = r.length * 8 - v;
|
|
3133
3160
|
return i > 0 ? a >> BigInt(i) : a;
|
|
3134
|
-
}, ie = c.bits2int_modN || function(
|
|
3135
|
-
return g.create(se(
|
|
3136
|
-
}, j =
|
|
3137
|
-
function
|
|
3138
|
-
return _n("num < 2^" + v,
|
|
3161
|
+
}, ie = c.bits2int_modN || function(r) {
|
|
3162
|
+
return g.create(se(r));
|
|
3163
|
+
}, j = De(v);
|
|
3164
|
+
function R(l) {
|
|
3165
|
+
return _n("num < 2^" + v, l, Ne, j), g.toBytes(l);
|
|
3139
3166
|
}
|
|
3140
|
-
function L(
|
|
3141
|
-
return Ze(
|
|
3167
|
+
function L(l, r) {
|
|
3168
|
+
return Ze(l, void 0, "message"), r ? Ze(n(l), void 0, "prehashed message") : l;
|
|
3142
3169
|
}
|
|
3143
|
-
function _(
|
|
3170
|
+
function _(l, r, a) {
|
|
3144
3171
|
if (["recovered", "canonical"].some((k) => k in a))
|
|
3145
3172
|
throw new Error("sign() legacy options not supported");
|
|
3146
3173
|
const { lowS: i, prehash: o, extraEntropy: h } = ut(a, S);
|
|
3147
|
-
|
|
3148
|
-
const w = ie(
|
|
3174
|
+
l = L(l, o);
|
|
3175
|
+
const w = ie(l), s = He(g, r), f = [R(s), R(w)];
|
|
3149
3176
|
if (h != null && h !== !1) {
|
|
3150
3177
|
const k = h === !0 ? t(b.secretKey) : h;
|
|
3151
3178
|
f.push(Se("extraEntropy", k));
|
|
3152
3179
|
}
|
|
3153
3180
|
const y = Ae(...f), B = w;
|
|
3154
|
-
function
|
|
3181
|
+
function O(k) {
|
|
3155
3182
|
const Y = se(k);
|
|
3156
3183
|
if (!g.isValidNot0(Y))
|
|
3157
3184
|
return;
|
|
@@ -3161,59 +3188,59 @@ function Yn(e, r, c = {}) {
|
|
|
3161
3188
|
const te = g.create(I * g.create(B + C * s));
|
|
3162
3189
|
if (te === Ne)
|
|
3163
3190
|
return;
|
|
3164
|
-
let $ = (q.x === C ? 0 : 2) | Number(q.y & Ve),
|
|
3165
|
-
return i && M(te) && (
|
|
3191
|
+
let $ = (q.x === C ? 0 : 2) | Number(q.y & Ve), T = te;
|
|
3192
|
+
return i && M(te) && (T = g.neg(te), $ ^= 1), new G(C, T, $);
|
|
3166
3193
|
}
|
|
3167
|
-
return { seed: y, k2sig:
|
|
3194
|
+
return { seed: y, k2sig: O };
|
|
3168
3195
|
}
|
|
3169
|
-
function K(
|
|
3170
|
-
|
|
3171
|
-
const { seed: i, k2sig: o } = _(
|
|
3172
|
-
return Sn(
|
|
3196
|
+
function K(l, r, a = {}) {
|
|
3197
|
+
l = Se("message", l);
|
|
3198
|
+
const { seed: i, k2sig: o } = _(l, r, a);
|
|
3199
|
+
return Sn(n.outputLen, g.BYTES, u)(i, o);
|
|
3173
3200
|
}
|
|
3174
|
-
function W(
|
|
3175
|
-
let
|
|
3176
|
-
const a = typeof
|
|
3201
|
+
function W(l) {
|
|
3202
|
+
let r;
|
|
3203
|
+
const a = typeof l == "string" || yt(l), i = !a && l !== null && typeof l == "object" && typeof l.r == "bigint" && typeof l.s == "bigint";
|
|
3177
3204
|
if (!a && !i)
|
|
3178
3205
|
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
3179
3206
|
if (i)
|
|
3180
|
-
|
|
3207
|
+
r = new G(l.r, l.s);
|
|
3181
3208
|
else if (a) {
|
|
3182
3209
|
try {
|
|
3183
|
-
|
|
3210
|
+
r = G.fromBytes(Se("sig", l), "der");
|
|
3184
3211
|
} catch (o) {
|
|
3185
3212
|
if (!(o instanceof Ie.Err))
|
|
3186
3213
|
throw o;
|
|
3187
3214
|
}
|
|
3188
|
-
if (!
|
|
3215
|
+
if (!r)
|
|
3189
3216
|
try {
|
|
3190
|
-
|
|
3217
|
+
r = G.fromBytes(Se("sig", l), "compact");
|
|
3191
3218
|
} catch {
|
|
3192
3219
|
return !1;
|
|
3193
3220
|
}
|
|
3194
3221
|
}
|
|
3195
|
-
return
|
|
3222
|
+
return r || !1;
|
|
3196
3223
|
}
|
|
3197
|
-
function
|
|
3224
|
+
function D(l, r, a, i = {}) {
|
|
3198
3225
|
const { lowS: o, prehash: h, format: w } = ut(i, S);
|
|
3199
|
-
if (a = Se("publicKey", a),
|
|
3226
|
+
if (a = Se("publicKey", a), r = L(Se("message", r), h), "strict" in i)
|
|
3200
3227
|
throw new Error("options.strict was renamed to lowS");
|
|
3201
|
-
const s = w === void 0 ? W(
|
|
3228
|
+
const s = w === void 0 ? W(l) : G.fromBytes(Se("sig", l), w);
|
|
3202
3229
|
if (s === !1)
|
|
3203
3230
|
return !1;
|
|
3204
3231
|
try {
|
|
3205
3232
|
const f = e.fromBytes(a);
|
|
3206
3233
|
if (o && s.hasHighS())
|
|
3207
3234
|
return !1;
|
|
3208
|
-
const { r: y, s: B } = s,
|
|
3235
|
+
const { r: y, s: B } = s, O = ie(r), k = g.inv(B), Y = g.create(O * k), I = g.create(y * k), q = e.BASE.multiplyUnsafe(Y).add(f.multiplyUnsafe(I));
|
|
3209
3236
|
return q.is0() ? !1 : g.create(q.x) === y;
|
|
3210
3237
|
} catch {
|
|
3211
3238
|
return !1;
|
|
3212
3239
|
}
|
|
3213
3240
|
}
|
|
3214
|
-
function d(
|
|
3241
|
+
function d(l, r, a = {}) {
|
|
3215
3242
|
const { prehash: i } = ut(a, S);
|
|
3216
|
-
return
|
|
3243
|
+
return r = L(r, i), G.fromBytes(l, "recovered").recoverPublicKey(r).toBytes();
|
|
3217
3244
|
}
|
|
3218
3245
|
return Object.freeze({
|
|
3219
3246
|
keygen: U,
|
|
@@ -3223,14 +3250,14 @@ function Yn(e, r, c = {}) {
|
|
|
3223
3250
|
lengths: b,
|
|
3224
3251
|
Point: e,
|
|
3225
3252
|
sign: K,
|
|
3226
|
-
verify:
|
|
3253
|
+
verify: D,
|
|
3227
3254
|
recoverPublicKey: d,
|
|
3228
3255
|
Signature: G,
|
|
3229
|
-
hash:
|
|
3256
|
+
hash: n
|
|
3230
3257
|
});
|
|
3231
3258
|
}
|
|
3232
3259
|
function Pn(e) {
|
|
3233
|
-
const
|
|
3260
|
+
const n = {
|
|
3234
3261
|
a: e.a,
|
|
3235
3262
|
b: e.b,
|
|
3236
3263
|
p: e.Fp.ORDER,
|
|
@@ -3240,13 +3267,13 @@ function Pn(e) {
|
|
|
3240
3267
|
Gy: e.Gy
|
|
3241
3268
|
}, c = e.Fp;
|
|
3242
3269
|
let t = e.allowedPrivateKeyLengths ? Array.from(new Set(e.allowedPrivateKeyLengths.map((g) => Math.ceil(g / 2)))) : void 0;
|
|
3243
|
-
const
|
|
3270
|
+
const u = ze(n.n, {
|
|
3244
3271
|
BITS: e.nBitLength,
|
|
3245
3272
|
allowedLengths: t,
|
|
3246
3273
|
modFromBytes: e.wrapPrivateKey
|
|
3247
3274
|
}), m = {
|
|
3248
3275
|
Fp: c,
|
|
3249
|
-
Fn:
|
|
3276
|
+
Fn: u,
|
|
3250
3277
|
allowInfinityPoint: e.allowInfinityPoint,
|
|
3251
3278
|
endo: e.endo,
|
|
3252
3279
|
isTorsionFree: e.isTorsionFree,
|
|
@@ -3254,32 +3281,32 @@ function Pn(e) {
|
|
|
3254
3281
|
fromBytes: e.fromBytes,
|
|
3255
3282
|
toBytes: e.toBytes
|
|
3256
3283
|
};
|
|
3257
|
-
return { CURVE:
|
|
3284
|
+
return { CURVE: n, curveOpts: m };
|
|
3258
3285
|
}
|
|
3259
3286
|
function Xn(e) {
|
|
3260
|
-
const { CURVE:
|
|
3287
|
+
const { CURVE: n, curveOpts: c } = Pn(e), t = {
|
|
3261
3288
|
hmac: e.hmac,
|
|
3262
3289
|
randomBytes: e.randomBytes,
|
|
3263
3290
|
lowS: e.lowS,
|
|
3264
3291
|
bits2int: e.bits2int,
|
|
3265
3292
|
bits2int_modN: e.bits2int_modN
|
|
3266
3293
|
};
|
|
3267
|
-
return { CURVE:
|
|
3294
|
+
return { CURVE: n, curveOpts: c, hash: e.hash, ecdsaOpts: t };
|
|
3268
3295
|
}
|
|
3269
|
-
function Gn(e,
|
|
3270
|
-
const c =
|
|
3271
|
-
return Object.assign({},
|
|
3296
|
+
function Gn(e, n) {
|
|
3297
|
+
const c = n.Point;
|
|
3298
|
+
return Object.assign({}, n, {
|
|
3272
3299
|
ProjectivePoint: c,
|
|
3273
3300
|
CURVE: Object.assign({}, e, nn(c.Fn.ORDER, c.Fn.BITS))
|
|
3274
3301
|
});
|
|
3275
3302
|
}
|
|
3276
3303
|
function Wn(e) {
|
|
3277
|
-
const { CURVE:
|
|
3304
|
+
const { CURVE: n, curveOpts: c, hash: t, ecdsaOpts: u } = Xn(e), m = Dn(n, c), g = Yn(m, t, u);
|
|
3278
3305
|
return Gn(e, g);
|
|
3279
3306
|
}
|
|
3280
|
-
function Qn(e,
|
|
3307
|
+
function Qn(e, n) {
|
|
3281
3308
|
const c = (t) => Wn({ ...e, hash: t });
|
|
3282
|
-
return { ...c(
|
|
3309
|
+
return { ...c(n), create: c };
|
|
3283
3310
|
}
|
|
3284
3311
|
const Bt = {
|
|
3285
3312
|
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
@@ -3297,20 +3324,22 @@ const Bt = {
|
|
|
3297
3324
|
]
|
|
3298
3325
|
}, Vt = /* @__PURE__ */ BigInt(2);
|
|
3299
3326
|
function Fn(e) {
|
|
3300
|
-
const
|
|
3327
|
+
const n = Bt.p, c = BigInt(3), t = BigInt(6), u = BigInt(11), m = BigInt(22), g = BigInt(23), p = BigInt(44), v = BigInt(88), U = e * e * e % n, H = U * U * e % n, V = xe(H, c, n) * H % n, E = xe(V, c, n) * H % n, b = xe(E, Vt, n) * U % n, S = xe(b, u, n) * b % n, A = xe(S, m, n) * S % n, M = xe(A, p, n) * A % n, Q = xe(M, v, n) * M % n, F = xe(Q, p, n) * A % n, G = xe(F, c, n) * H % n, se = xe(G, g, n) * S % n, ie = xe(se, t, n) * U % n, j = xe(ie, Vt, n);
|
|
3301
3328
|
if (!ht.eql(ht.sqr(j), e))
|
|
3302
3329
|
throw new Error("Cannot find square root");
|
|
3303
3330
|
return j;
|
|
3304
3331
|
}
|
|
3305
|
-
const ht =
|
|
3332
|
+
const ht = ze(Bt.p, { sqrt: Fn }), ir = Qn({ ...Bt, Fp: ht, lowS: !0, endo: Jn }, wn);
|
|
3306
3333
|
export {
|
|
3307
|
-
|
|
3308
|
-
|
|
3334
|
+
sr as F,
|
|
3335
|
+
rr as a,
|
|
3309
3336
|
$e as b,
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3337
|
+
tr as c,
|
|
3338
|
+
pn as d,
|
|
3339
|
+
or as e,
|
|
3340
|
+
et as f,
|
|
3341
|
+
er as g,
|
|
3313
3342
|
qe as m,
|
|
3314
|
-
|
|
3315
|
-
|
|
3343
|
+
nr as r,
|
|
3344
|
+
ir as s
|
|
3316
3345
|
};
|