@gardenfi/core 2.1.13 → 2.1.14

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.
@@ -1,1287 +0,0 @@
1
- import { u as se, v as ce, w as fe, x as Ut, y as ae, z as ue, C as le, D as de } from "./index-CBKJdpVQ.js";
2
- class Pt extends se {
3
- constructor(n, t) {
4
- super(), this.finished = !1, this.destroyed = !1, ce(n);
5
- const r = fe(t);
6
- if (this.iHash = n.create(), typeof this.iHash.update != "function")
7
- throw new Error("Expected instance of class which extends utils.Hash");
8
- this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
9
- const s = this.blockLen, i = new Uint8Array(s);
10
- i.set(r.length > s ? n.create().update(r).digest() : r);
11
- for (let c = 0; c < i.length; c++)
12
- i[c] ^= 54;
13
- this.iHash.update(i), this.oHash = n.create();
14
- for (let c = 0; c < i.length; c++)
15
- i[c] ^= 106;
16
- this.oHash.update(i), i.fill(0);
17
- }
18
- update(n) {
19
- return Ut(this), this.iHash.update(n), this;
20
- }
21
- digestInto(n) {
22
- Ut(this), ae(n, this.outputLen), this.finished = !0, this.iHash.digestInto(n), this.oHash.update(n), this.oHash.digestInto(n), this.destroy();
23
- }
24
- digest() {
25
- const n = new Uint8Array(this.oHash.outputLen);
26
- return this.digestInto(n), n;
27
- }
28
- _cloneInto(n) {
29
- n || (n = Object.create(Object.getPrototypeOf(this), {}));
30
- const { oHash: t, iHash: r, finished: s, destroyed: i, blockLen: c, outputLen: f } = this;
31
- return n = n, n.finished = s, n.destroyed = i, n.blockLen = c, n.outputLen = f, n.oHash = t._cloneInto(n.oHash), n.iHash = r._cloneInto(n.iHash), n;
32
- }
33
- destroy() {
34
- this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
35
- }
36
- }
37
- const $t = (e, n, t) => new Pt(e, n).update(t).digest();
38
- $t.create = (e, n) => new Pt(e, n);
39
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
40
- const Lt = /* @__PURE__ */ BigInt(0), At = /* @__PURE__ */ BigInt(1);
41
- function it(e) {
42
- return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array";
43
- }
44
- function zt(e) {
45
- if (!it(e))
46
- throw new Error("Uint8Array expected");
47
- }
48
- function at(e, n) {
49
- if (typeof n != "boolean")
50
- throw new Error(e + " boolean expected, got " + n);
51
- }
52
- function dt(e) {
53
- const n = e.toString(16);
54
- return n.length & 1 ? "0" + n : n;
55
- }
56
- function Wt(e) {
57
- if (typeof e != "string")
58
- throw new Error("hex string expected, got " + typeof e);
59
- return e === "" ? Lt : BigInt("0x" + e);
60
- }
61
- const Gt = (
62
- // @ts-ignore
63
- typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function"
64
- ), he = /* @__PURE__ */ Array.from({ length: 256 }, (e, n) => n.toString(16).padStart(2, "0"));
65
- function ut(e) {
66
- if (zt(e), Gt)
67
- return e.toHex();
68
- let n = "";
69
- for (let t = 0; t < e.length; t++)
70
- n += he[e[t]];
71
- return n;
72
- }
73
- const $ = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
74
- function kt(e) {
75
- if (e >= $._0 && e <= $._9)
76
- return e - $._0;
77
- if (e >= $.A && e <= $.F)
78
- return e - ($.A - 10);
79
- if (e >= $.a && e <= $.f)
80
- return e - ($.a - 10);
81
- }
82
- function ht(e) {
83
- if (typeof e != "string")
84
- throw new Error("hex string expected, got " + typeof e);
85
- if (Gt)
86
- return Uint8Array.fromHex(e);
87
- const n = e.length, t = n / 2;
88
- if (n % 2)
89
- throw new Error("hex string expected, got unpadded hex of length " + n);
90
- const r = new Uint8Array(t);
91
- for (let s = 0, i = 0; s < t; s++, i += 2) {
92
- const c = kt(e.charCodeAt(i)), f = kt(e.charCodeAt(i + 1));
93
- if (c === void 0 || f === void 0) {
94
- const o = e[i] + e[i + 1];
95
- throw new Error('hex string expected, got non-hex character "' + o + '" at index ' + i);
96
- }
97
- r[s] = c * 16 + f;
98
- }
99
- return r;
100
- }
101
- function tt(e) {
102
- return Wt(ut(e));
103
- }
104
- function Dt(e) {
105
- return zt(e), Wt(ut(Uint8Array.from(e).reverse()));
106
- }
107
- function lt(e, n) {
108
- return ht(e.toString(16).padStart(n * 2, "0"));
109
- }
110
- function Xt(e, n) {
111
- return lt(e, n).reverse();
112
- }
113
- function K(e, n, t) {
114
- let r;
115
- if (typeof n == "string")
116
- try {
117
- r = ht(n);
118
- } catch (i) {
119
- throw new Error(e + " must be hex string or Uint8Array, cause: " + i);
120
- }
121
- else if (it(n))
122
- r = Uint8Array.from(n);
123
- else
124
- throw new Error(e + " must be hex string or Uint8Array");
125
- const s = r.length;
126
- if (typeof t == "number" && s !== t)
127
- throw new Error(e + " of length " + t + " expected, got " + s);
128
- return r;
129
- }
130
- function wt(...e) {
131
- let n = 0;
132
- for (let r = 0; r < e.length; r++) {
133
- const s = e[r];
134
- zt(s), n += s.length;
135
- }
136
- const t = new Uint8Array(n);
137
- for (let r = 0, s = 0; r < e.length; r++) {
138
- const i = e[r];
139
- t.set(i, s), s += i.length;
140
- }
141
- return t;
142
- }
143
- const bt = (e) => typeof e == "bigint" && Lt <= e;
144
- function Zt(e, n, t) {
145
- return bt(e) && bt(n) && bt(t) && n <= e && e < t;
146
- }
147
- function ot(e, n, t, r) {
148
- if (!Zt(n, t, r))
149
- throw new Error("expected valid " + e + ": " + t + " <= n < " + r + ", got " + n);
150
- }
151
- function we(e) {
152
- let n;
153
- for (n = 0; e > Lt; e >>= At, n += 1)
154
- ;
155
- return n;
156
- }
157
- const gt = (e) => (At << BigInt(e)) - At, Et = (e) => new Uint8Array(e), Tt = (e) => Uint8Array.from(e);
158
- function ge(e, n, t) {
159
- if (typeof e != "number" || e < 2)
160
- throw new Error("hashLen must be a number");
161
- if (typeof n != "number" || n < 2)
162
- throw new Error("qByteLen must be a number");
163
- if (typeof t != "function")
164
- throw new Error("hmacFn must be a function");
165
- let r = Et(e), s = Et(e), i = 0;
166
- const c = () => {
167
- r.fill(1), s.fill(0), i = 0;
168
- }, f = (...I) => t(s, r, ...I), o = (I = Et(0)) => {
169
- s = f(Tt([0]), I), r = f(), I.length !== 0 && (s = f(Tt([1]), I), r = f());
170
- }, u = () => {
171
- if (i++ >= 1e3)
172
- throw new Error("drbg: tried 1000 values");
173
- let I = 0;
174
- const d = [];
175
- for (; I < n; ) {
176
- r = f();
177
- const S = r.slice();
178
- d.push(S), I += r.length;
179
- }
180
- return wt(...d);
181
- };
182
- return (I, d) => {
183
- c(), o(I);
184
- let S;
185
- for (; !(S = d(u())); )
186
- o();
187
- return c(), S;
188
- };
189
- }
190
- const pe = {
191
- bigint: (e) => typeof e == "bigint",
192
- function: (e) => typeof e == "function",
193
- boolean: (e) => typeof e == "boolean",
194
- string: (e) => typeof e == "string",
195
- stringOrUint8Array: (e) => typeof e == "string" || it(e),
196
- isSafeInteger: (e) => Number.isSafeInteger(e),
197
- array: (e) => Array.isArray(e),
198
- field: (e, n) => n.Fp.isValid(e),
199
- hash: (e) => typeof e == "function" && Number.isSafeInteger(e.outputLen)
200
- };
201
- function pt(e, n, t = {}) {
202
- const r = (s, i, c) => {
203
- const f = pe[i];
204
- if (typeof f != "function")
205
- throw new Error("invalid validator function");
206
- const o = e[s];
207
- if (!(c && o === void 0) && !f(o, e))
208
- throw new Error("param " + String(s) + " is invalid. Expected " + i + ", got " + o);
209
- };
210
- for (const [s, i] of Object.entries(n))
211
- r(s, i, !1);
212
- for (const [s, i] of Object.entries(t))
213
- r(s, i, !0);
214
- return e;
215
- }
216
- function Rt(e) {
217
- const n = /* @__PURE__ */ new WeakMap();
218
- return (t, ...r) => {
219
- const s = n.get(t);
220
- if (s !== void 0)
221
- return s;
222
- const i = e(t, ...r);
223
- return n.set(t, i), i;
224
- };
225
- }
226
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
227
- const T = BigInt(0), Z = BigInt(1), J = /* @__PURE__ */ BigInt(2), ye = /* @__PURE__ */ BigInt(3), It = /* @__PURE__ */ BigInt(4), Ct = /* @__PURE__ */ BigInt(5), _t = /* @__PURE__ */ BigInt(8);
228
- function V(e, n) {
229
- const t = e % n;
230
- return t >= T ? t : n + t;
231
- }
232
- function me(e, n, t) {
233
- if (n < T)
234
- throw new Error("invalid exponent, negatives unsupported");
235
- if (t <= T)
236
- throw new Error("invalid modulus");
237
- if (t === Z)
238
- return T;
239
- let r = Z;
240
- for (; n > T; )
241
- n & Z && (r = r * e % t), e = e * e % t, n >>= Z;
242
- return r;
243
- }
244
- function Y(e, n, t) {
245
- let r = e;
246
- for (; n-- > T; )
247
- r *= r, r %= t;
248
- return r;
249
- }
250
- function Nt(e, n) {
251
- if (e === T)
252
- throw new Error("invert: expected non-zero number");
253
- if (n <= T)
254
- throw new Error("invert: expected positive modulus, got " + n);
255
- let t = V(e, n), r = n, s = T, i = Z;
256
- for (; t !== T; ) {
257
- const f = r / t, o = r % t, u = s - i * f;
258
- r = t, t = o, s = i, i = u;
259
- }
260
- if (r !== Z)
261
- throw new Error("invert: does not exist");
262
- return V(s, n);
263
- }
264
- function be(e) {
265
- const n = (e - Z) / J;
266
- let t, r, s;
267
- for (t = e - Z, r = 0; t % J === T; t /= J, r++)
268
- ;
269
- for (s = J; s < e && me(s, n, e) !== e - Z; s++)
270
- if (s > 1e3)
271
- throw new Error("Cannot find square root: likely non-prime P");
272
- if (r === 1) {
273
- const c = (e + Z) / It;
274
- return function(o, u) {
275
- const m = o.pow(u, c);
276
- if (!o.eql(o.sqr(m), u))
277
- throw new Error("Cannot find square root");
278
- return m;
279
- };
280
- }
281
- const i = (t + Z) / J;
282
- return function(f, o) {
283
- if (f.pow(o, n) === f.neg(f.ONE))
284
- throw new Error("Cannot find square root");
285
- let u = r, m = f.pow(f.mul(f.ONE, s), t), I = f.pow(o, i), d = f.pow(o, t);
286
- for (; !f.eql(d, f.ONE); ) {
287
- if (f.eql(d, f.ZERO))
288
- return f.ZERO;
289
- let S = 1;
290
- for (let p = f.sqr(d); S < u && !f.eql(p, f.ONE); S++)
291
- p = f.sqr(p);
292
- const L = f.pow(m, Z << BigInt(u - S - 1));
293
- m = f.sqr(L), I = f.mul(I, L), d = f.mul(d, m), u = S;
294
- }
295
- return I;
296
- };
297
- }
298
- function Ee(e) {
299
- if (e % It === ye) {
300
- const n = (e + Z) / It;
301
- return function(r, s) {
302
- const i = r.pow(s, n);
303
- if (!r.eql(r.sqr(i), s))
304
- throw new Error("Cannot find square root");
305
- return i;
306
- };
307
- }
308
- if (e % _t === Ct) {
309
- const n = (e - Ct) / _t;
310
- return function(r, s) {
311
- const i = r.mul(s, J), c = r.pow(i, n), f = r.mul(s, c), o = r.mul(r.mul(f, J), c), u = r.mul(f, r.sub(o, r.ONE));
312
- if (!r.eql(r.sqr(u), s))
313
- throw new Error("Cannot find square root");
314
- return u;
315
- };
316
- }
317
- return be(e);
318
- }
319
- const Be = [
320
- "create",
321
- "isValid",
322
- "is0",
323
- "neg",
324
- "inv",
325
- "sqrt",
326
- "sqr",
327
- "eql",
328
- "add",
329
- "sub",
330
- "mul",
331
- "pow",
332
- "div",
333
- "addN",
334
- "subN",
335
- "mulN",
336
- "sqrN"
337
- ];
338
- function ve(e) {
339
- const n = {
340
- ORDER: "bigint",
341
- MASK: "bigint",
342
- BYTES: "isSafeInteger",
343
- BITS: "isSafeInteger"
344
- }, t = Be.reduce((r, s) => (r[s] = "function", r), n);
345
- return pt(e, t);
346
- }
347
- function xe(e, n, t) {
348
- if (t < T)
349
- throw new Error("invalid exponent, negatives unsupported");
350
- if (t === T)
351
- return e.ONE;
352
- if (t === Z)
353
- return n;
354
- let r = e.ONE, s = n;
355
- for (; t > T; )
356
- t & Z && (r = e.mul(r, s)), s = e.sqr(s), t >>= Z;
357
- return r;
358
- }
359
- function Se(e, n) {
360
- const t = new Array(n.length), r = n.reduce((i, c, f) => e.is0(c) ? i : (t[f] = i, e.mul(i, c)), e.ONE), s = e.inv(r);
361
- return n.reduceRight((i, c, f) => e.is0(c) ? i : (t[f] = e.mul(i, t[f]), e.mul(i, c)), s), t;
362
- }
363
- function Qt(e, n) {
364
- const t = n !== void 0 ? n : e.toString(2).length, r = Math.ceil(t / 8);
365
- return { nBitLength: t, nByteLength: r };
366
- }
367
- function Jt(e, n, t = !1, r = {}) {
368
- if (e <= T)
369
- throw new Error("invalid field: expected ORDER > 0, got " + e);
370
- const { nBitLength: s, nByteLength: i } = Qt(e, n);
371
- if (i > 2048)
372
- throw new Error("invalid field: expected ORDER of <= 2048 bytes");
373
- let c;
374
- const f = Object.freeze({
375
- ORDER: e,
376
- isLE: t,
377
- BITS: s,
378
- BYTES: i,
379
- MASK: gt(s),
380
- ZERO: T,
381
- ONE: Z,
382
- create: (o) => V(o, e),
383
- isValid: (o) => {
384
- if (typeof o != "bigint")
385
- throw new Error("invalid field element: expected bigint, got " + typeof o);
386
- return T <= o && o < e;
387
- },
388
- is0: (o) => o === T,
389
- isOdd: (o) => (o & Z) === Z,
390
- neg: (o) => V(-o, e),
391
- eql: (o, u) => o === u,
392
- sqr: (o) => V(o * o, e),
393
- add: (o, u) => V(o + u, e),
394
- sub: (o, u) => V(o - u, e),
395
- mul: (o, u) => V(o * u, e),
396
- pow: (o, u) => xe(f, o, u),
397
- div: (o, u) => V(o * Nt(u, e), e),
398
- // Same as above, but doesn't normalize
399
- sqrN: (o) => o * o,
400
- addN: (o, u) => o + u,
401
- subN: (o, u) => o - u,
402
- mulN: (o, u) => o * u,
403
- inv: (o) => Nt(o, e),
404
- sqrt: r.sqrt || ((o) => (c || (c = Ee(e)), c(f, o))),
405
- invertBatch: (o) => Se(f, o),
406
- // TODO: do we really need constant cmov?
407
- // We don't have const-time bigints anyway, so probably will be not very useful
408
- cmov: (o, u, m) => m ? u : o,
409
- toBytes: (o) => t ? Xt(o, i) : lt(o, i),
410
- fromBytes: (o) => {
411
- if (o.length !== i)
412
- throw new Error("Field.fromBytes: expected " + i + " bytes, got " + o.length);
413
- return t ? Dt(o) : tt(o);
414
- }
415
- });
416
- return Object.freeze(f);
417
- }
418
- function te(e) {
419
- if (typeof e != "bigint")
420
- throw new Error("field order must be bigint");
421
- const n = e.toString(2).length;
422
- return Math.ceil(n / 8);
423
- }
424
- function ee(e) {
425
- const n = te(e);
426
- return n + Math.ceil(n / 2);
427
- }
428
- function Ae(e, n, t = !1) {
429
- const r = e.length, s = te(n), i = ee(n);
430
- if (r < 16 || r < i || r > 1024)
431
- throw new Error("expected " + i + "-1024 bytes of input, got " + r);
432
- const c = t ? Dt(e) : tt(e), f = V(c, n - Z) + Z;
433
- return t ? Xt(f, s) : lt(f, s);
434
- }
435
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
436
- const Mt = BigInt(0), qt = BigInt(1);
437
- function Bt(e, n) {
438
- const t = n.negate();
439
- return e ? t : n;
440
- }
441
- function ne(e, n) {
442
- if (!Number.isSafeInteger(e) || e <= 0 || e > n)
443
- throw new Error("invalid window size, expected [1.." + n + "], got W=" + e);
444
- }
445
- function vt(e, n) {
446
- ne(e, n);
447
- const t = Math.ceil(n / e) + 1, r = 2 ** (e - 1), s = 2 ** e, i = gt(e), c = BigInt(e);
448
- return { windows: t, windowSize: r, mask: i, maxNumber: s, shiftBy: c };
449
- }
450
- function Vt(e, n, t) {
451
- const { windowSize: r, mask: s, maxNumber: i, shiftBy: c } = t;
452
- let f = Number(e & s), o = e >> c;
453
- f > r && (f -= i, o += qt);
454
- const u = n * r, m = u + Math.abs(f) - 1, I = f === 0, d = f < 0, S = n % 2 !== 0;
455
- return { nextN: o, offset: m, isZero: I, isNeg: d, isNegF: S, offsetF: u };
456
- }
457
- function Ie(e, n) {
458
- if (!Array.isArray(e))
459
- throw new Error("array expected");
460
- e.forEach((t, r) => {
461
- if (!(t instanceof n))
462
- throw new Error("invalid point at index " + r);
463
- });
464
- }
465
- function Ne(e, n) {
466
- if (!Array.isArray(e))
467
- throw new Error("array of scalars expected");
468
- e.forEach((t, r) => {
469
- if (!n.isValid(t))
470
- throw new Error("invalid scalar at index " + r);
471
- });
472
- }
473
- const xt = /* @__PURE__ */ new WeakMap(), re = /* @__PURE__ */ new WeakMap();
474
- function St(e) {
475
- return re.get(e) || 1;
476
- }
477
- function qe(e, n) {
478
- return {
479
- constTimeNegate: Bt,
480
- hasPrecomputes(t) {
481
- return St(t) !== 1;
482
- },
483
- // non-const time multiplication ladder
484
- unsafeLadder(t, r, s = e.ZERO) {
485
- let i = t;
486
- for (; r > Mt; )
487
- r & qt && (s = s.add(i)), i = i.double(), r >>= qt;
488
- return s;
489
- },
490
- /**
491
- * Creates a wNAF precomputation window. Used for caching.
492
- * Default window size is set by `utils.precompute()` and is equal to 8.
493
- * Number of precomputed points depends on the curve size:
494
- * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
495
- * - 𝑊 is the window size
496
- * - 𝑛 is the bitlength of the curve order.
497
- * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
498
- * @param elm Point instance
499
- * @param W window size
500
- * @returns precomputed point tables flattened to a single array
501
- */
502
- precomputeWindow(t, r) {
503
- const { windows: s, windowSize: i } = vt(r, n), c = [];
504
- let f = t, o = f;
505
- for (let u = 0; u < s; u++) {
506
- o = f, c.push(o);
507
- for (let m = 1; m < i; m++)
508
- o = o.add(f), c.push(o);
509
- f = o.double();
510
- }
511
- return c;
512
- },
513
- /**
514
- * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
515
- * @param W window size
516
- * @param precomputes precomputed tables
517
- * @param n scalar (we don't check here, but should be less than curve order)
518
- * @returns real and fake (for const-time) points
519
- */
520
- wNAF(t, r, s) {
521
- let i = e.ZERO, c = e.BASE;
522
- const f = vt(t, n);
523
- for (let o = 0; o < f.windows; o++) {
524
- const { nextN: u, offset: m, isZero: I, isNeg: d, isNegF: S, offsetF: L } = Vt(s, o, f);
525
- s = u, I ? c = c.add(Bt(S, r[L])) : i = i.add(Bt(d, r[m]));
526
- }
527
- return { p: i, f: c };
528
- },
529
- /**
530
- * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
531
- * @param W window size
532
- * @param precomputes precomputed tables
533
- * @param n scalar (we don't check here, but should be less than curve order)
534
- * @param acc accumulator point to add result of multiplication
535
- * @returns point
536
- */
537
- wNAFUnsafe(t, r, s, i = e.ZERO) {
538
- const c = vt(t, n);
539
- for (let f = 0; f < c.windows && s !== Mt; f++) {
540
- const { nextN: o, offset: u, isZero: m, isNeg: I } = Vt(s, f, c);
541
- if (s = o, !m) {
542
- const d = r[u];
543
- i = i.add(I ? d.negate() : d);
544
- }
545
- }
546
- return i;
547
- },
548
- getPrecomputes(t, r, s) {
549
- let i = xt.get(r);
550
- return i || (i = this.precomputeWindow(r, t), t !== 1 && xt.set(r, s(i))), i;
551
- },
552
- wNAFCached(t, r, s) {
553
- const i = St(t);
554
- return this.wNAF(i, this.getPrecomputes(i, t, s), r);
555
- },
556
- wNAFCachedUnsafe(t, r, s, i) {
557
- const c = St(t);
558
- return c === 1 ? this.unsafeLadder(t, r, i) : this.wNAFUnsafe(c, this.getPrecomputes(c, t, s), r, i);
559
- },
560
- // We calculate precomputes for elliptic curve point multiplication
561
- // using windowed method. This specifies window size and
562
- // stores precomputed values. Usually only base point would be precomputed.
563
- setWindowSize(t, r) {
564
- ne(r, n), re.set(t, r), xt.delete(t);
565
- }
566
- };
567
- }
568
- function Oe(e, n, t, r) {
569
- if (Ie(t, e), Ne(r, n), t.length !== r.length)
570
- throw new Error("arrays of points and scalars must have equal length");
571
- const s = e.ZERO, i = we(BigInt(t.length)), c = i > 12 ? i - 3 : i > 4 ? i - 2 : i ? 2 : 1, f = gt(c), o = new Array(Number(f) + 1).fill(s), u = Math.floor((n.BITS - 1) / c) * c;
572
- let m = s;
573
- for (let I = u; I >= 0; I -= c) {
574
- o.fill(s);
575
- for (let S = 0; S < r.length; S++) {
576
- const L = r[S], p = Number(L >> BigInt(I) & f);
577
- o[p] = o[p].add(t[S]);
578
- }
579
- let d = s;
580
- for (let S = o.length - 1, L = s; S > 0; S--)
581
- L = L.add(o[S]), d = d.add(L);
582
- if (m = m.add(d), I !== 0)
583
- for (let S = 0; S < c; S++)
584
- m = m.double();
585
- }
586
- return m;
587
- }
588
- function oe(e) {
589
- return ve(e.Fp), pt(e, {
590
- n: "bigint",
591
- h: "bigint",
592
- Gx: "field",
593
- Gy: "field"
594
- }, {
595
- nBitLength: "isSafeInteger",
596
- nByteLength: "isSafeInteger"
597
- }), Object.freeze({
598
- ...Qt(e.n, e.nBitLength),
599
- ...e,
600
- p: e.Fp.ORDER
601
- });
602
- }
603
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
604
- function jt(e) {
605
- e.lowS !== void 0 && at("lowS", e.lowS), e.prehash !== void 0 && at("prehash", e.prehash);
606
- }
607
- function He(e) {
608
- const n = oe(e);
609
- pt(n, {
610
- a: "field",
611
- b: "field"
612
- }, {
613
- allowedPrivateKeyLengths: "array",
614
- wrapPrivateKey: "boolean",
615
- isTorsionFree: "function",
616
- clearCofactor: "function",
617
- allowInfinityPoint: "boolean",
618
- fromBytes: "function",
619
- toBytes: "function"
620
- });
621
- const { endo: t, Fp: r, a: s } = n;
622
- if (t) {
623
- if (!r.eql(s, r.ZERO))
624
- throw new Error("invalid endomorphism, can only be defined for Koblitz curves that have a=0");
625
- if (typeof t != "object" || typeof t.beta != "bigint" || typeof t.splitScalar != "function")
626
- throw new Error("invalid endomorphism, expected beta: bigint and splitScalar: function");
627
- }
628
- return Object.freeze({ ...n });
629
- }
630
- class Le extends Error {
631
- constructor(n = "") {
632
- super(n);
633
- }
634
- }
635
- const W = {
636
- // asn.1 DER encoding utils
637
- Err: Le,
638
- // Basic building block is TLV (Tag-Length-Value)
639
- _tlv: {
640
- encode: (e, n) => {
641
- const { Err: t } = W;
642
- if (e < 0 || e > 256)
643
- throw new t("tlv.encode: wrong tag");
644
- if (n.length & 1)
645
- throw new t("tlv.encode: unpadded data");
646
- const r = n.length / 2, s = dt(r);
647
- if (s.length / 2 & 128)
648
- throw new t("tlv.encode: long form length too big");
649
- const i = r > 127 ? dt(s.length / 2 | 128) : "";
650
- return dt(e) + i + s + n;
651
- },
652
- // v - value, l - left bytes (unparsed)
653
- decode(e, n) {
654
- const { Err: t } = W;
655
- let r = 0;
656
- if (e < 0 || e > 256)
657
- throw new t("tlv.encode: wrong tag");
658
- if (n.length < 2 || n[r++] !== e)
659
- throw new t("tlv.decode: wrong tlv");
660
- const s = n[r++], i = !!(s & 128);
661
- let c = 0;
662
- if (!i)
663
- c = s;
664
- else {
665
- const o = s & 127;
666
- if (!o)
667
- throw new t("tlv.decode(long): indefinite length not supported");
668
- if (o > 4)
669
- throw new t("tlv.decode(long): byte length is too big");
670
- const u = n.subarray(r, r + o);
671
- if (u.length !== o)
672
- throw new t("tlv.decode: length bytes not complete");
673
- if (u[0] === 0)
674
- throw new t("tlv.decode(long): zero leftmost byte");
675
- for (const m of u)
676
- c = c << 8 | m;
677
- if (r += o, c < 128)
678
- throw new t("tlv.decode(long): not minimal encoding");
679
- }
680
- const f = n.subarray(r, r + c);
681
- if (f.length !== c)
682
- throw new t("tlv.decode: wrong value length");
683
- return { v: f, l: n.subarray(r + c) };
684
- }
685
- },
686
- // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
687
- // since we always use positive integers here. It must always be empty:
688
- // - add zero byte if exists
689
- // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
690
- _int: {
691
- encode(e) {
692
- const { Err: n } = W;
693
- if (e < G)
694
- throw new n("integer: negative integers are not allowed");
695
- let t = dt(e);
696
- if (Number.parseInt(t[0], 16) & 8 && (t = "00" + t), t.length & 1)
697
- throw new n("unexpected DER parsing assertion: unpadded hex");
698
- return t;
699
- },
700
- decode(e) {
701
- const { Err: n } = W;
702
- if (e[0] & 128)
703
- throw new n("invalid signature integer: negative");
704
- if (e[0] === 0 && !(e[1] & 128))
705
- throw new n("invalid signature integer: unnecessary leading zero");
706
- return tt(e);
707
- }
708
- },
709
- toSig(e) {
710
- const { Err: n, _int: t, _tlv: r } = W, s = K("signature", e), { v: i, l: c } = r.decode(48, s);
711
- if (c.length)
712
- throw new n("invalid signature: left bytes after parsing");
713
- const { v: f, l: o } = r.decode(2, i), { v: u, l: m } = r.decode(2, o);
714
- if (m.length)
715
- throw new n("invalid signature: left bytes after parsing");
716
- return { r: t.decode(f), s: t.decode(u) };
717
- },
718
- hexFromSig(e) {
719
- const { _tlv: n, _int: t } = W, r = n.encode(2, t.encode(e.r)), s = n.encode(2, t.encode(e.s)), i = r + s;
720
- return n.encode(48, i);
721
- }
722
- }, G = BigInt(0), k = BigInt(1);
723
- BigInt(2);
724
- const Yt = BigInt(3);
725
- BigInt(4);
726
- function ze(e) {
727
- const n = He(e), { Fp: t } = n, r = Jt(n.n, n.nBitLength), s = n.toBytes || ((p, a, h) => {
728
- const y = a.toAffine();
729
- return wt(Uint8Array.from([4]), t.toBytes(y.x), t.toBytes(y.y));
730
- }), i = n.fromBytes || ((p) => {
731
- const a = p.subarray(1), h = t.fromBytes(a.subarray(0, t.BYTES)), y = t.fromBytes(a.subarray(t.BYTES, 2 * t.BYTES));
732
- return { x: h, y };
733
- });
734
- function c(p) {
735
- const { a, b: h } = n, y = t.sqr(p), b = t.mul(y, p);
736
- return t.add(t.add(b, t.mul(p, a)), h);
737
- }
738
- if (!t.eql(t.sqr(n.Gy), c(n.Gx)))
739
- throw new Error("bad generator point: equation left != right");
740
- function f(p) {
741
- return Zt(p, k, n.n);
742
- }
743
- function o(p) {
744
- const { allowedPrivateKeyLengths: a, nByteLength: h, wrapPrivateKey: y, n: b } = n;
745
- if (a && typeof p != "bigint") {
746
- if (it(p) && (p = ut(p)), typeof p != "string" || !a.includes(p.length))
747
- throw new Error("invalid private key");
748
- p = p.padStart(h * 2, "0");
749
- }
750
- let N;
751
- try {
752
- N = typeof p == "bigint" ? p : tt(K("private key", p, h));
753
- } catch {
754
- throw new Error("invalid private key, expected hex or " + h + " bytes, got " + typeof p);
755
- }
756
- return y && (N = V(N, b)), ot("private key", N, k, b), N;
757
- }
758
- function u(p) {
759
- if (!(p instanceof d))
760
- throw new Error("ProjectivePoint expected");
761
- }
762
- const m = Rt((p, a) => {
763
- const { px: h, py: y, pz: b } = p;
764
- if (t.eql(b, t.ONE))
765
- return { x: h, y };
766
- const N = p.is0();
767
- a == null && (a = N ? t.ONE : t.inv(b));
768
- const O = t.mul(h, a), A = t.mul(y, a), E = t.mul(b, a);
769
- if (N)
770
- return { x: t.ZERO, y: t.ZERO };
771
- if (!t.eql(E, t.ONE))
772
- throw new Error("invZ was invalid");
773
- return { x: O, y: A };
774
- }), I = Rt((p) => {
775
- if (p.is0()) {
776
- if (n.allowInfinityPoint && !t.is0(p.py))
777
- return;
778
- throw new Error("bad point: ZERO");
779
- }
780
- const { x: a, y: h } = p.toAffine();
781
- if (!t.isValid(a) || !t.isValid(h))
782
- throw new Error("bad point: x or y not FE");
783
- const y = t.sqr(h), b = c(a);
784
- if (!t.eql(y, b))
785
- throw new Error("bad point: equation left != right");
786
- if (!p.isTorsionFree())
787
- throw new Error("bad point: not in prime-order subgroup");
788
- return !0;
789
- });
790
- class d {
791
- constructor(a, h, y) {
792
- if (a == null || !t.isValid(a))
793
- throw new Error("x required");
794
- if (h == null || !t.isValid(h))
795
- throw new Error("y required");
796
- if (y == null || !t.isValid(y))
797
- throw new Error("z required");
798
- this.px = a, this.py = h, this.pz = y, Object.freeze(this);
799
- }
800
- // Does not validate if the point is on-curve.
801
- // Use fromHex instead, or call assertValidity() later.
802
- static fromAffine(a) {
803
- const { x: h, y } = a || {};
804
- if (!a || !t.isValid(h) || !t.isValid(y))
805
- throw new Error("invalid affine point");
806
- if (a instanceof d)
807
- throw new Error("projective point not allowed");
808
- const b = (N) => t.eql(N, t.ZERO);
809
- return b(h) && b(y) ? d.ZERO : new d(h, y, t.ONE);
810
- }
811
- get x() {
812
- return this.toAffine().x;
813
- }
814
- get y() {
815
- return this.toAffine().y;
816
- }
817
- /**
818
- * Takes a bunch of Projective Points but executes only one
819
- * inversion on all of them. Inversion is very slow operation,
820
- * so this improves performance massively.
821
- * Optimization: converts a list of projective points to a list of identical points with Z=1.
822
- */
823
- static normalizeZ(a) {
824
- const h = t.invertBatch(a.map((y) => y.pz));
825
- return a.map((y, b) => y.toAffine(h[b])).map(d.fromAffine);
826
- }
827
- /**
828
- * Converts hash string or Uint8Array to Point.
829
- * @param hex short/long ECDSA hex
830
- */
831
- static fromHex(a) {
832
- const h = d.fromAffine(i(K("pointHex", a)));
833
- return h.assertValidity(), h;
834
- }
835
- // Multiplies generator point by privateKey.
836
- static fromPrivateKey(a) {
837
- return d.BASE.multiply(o(a));
838
- }
839
- // Multiscalar Multiplication
840
- static msm(a, h) {
841
- return Oe(d, r, a, h);
842
- }
843
- // "Private method", don't use it directly
844
- _setWindowSize(a) {
845
- L.setWindowSize(this, a);
846
- }
847
- // A point on curve is valid if it conforms to equation.
848
- assertValidity() {
849
- I(this);
850
- }
851
- hasEvenY() {
852
- const { y: a } = this.toAffine();
853
- if (t.isOdd)
854
- return !t.isOdd(a);
855
- throw new Error("Field doesn't support isOdd");
856
- }
857
- /**
858
- * Compare one point to another.
859
- */
860
- equals(a) {
861
- u(a);
862
- const { px: h, py: y, pz: b } = this, { px: N, py: O, pz: A } = a, E = t.eql(t.mul(h, A), t.mul(N, b)), x = t.eql(t.mul(y, A), t.mul(O, b));
863
- return E && x;
864
- }
865
- /**
866
- * Flips point to one corresponding to (x, -y) in Affine coordinates.
867
- */
868
- negate() {
869
- return new d(this.px, t.neg(this.py), this.pz);
870
- }
871
- // Renes-Costello-Batina exception-free doubling formula.
872
- // There is 30% faster Jacobian formula, but it is not complete.
873
- // https://eprint.iacr.org/2015/1060, algorithm 3
874
- // Cost: 8M + 3S + 3*a + 2*b3 + 15add.
875
- double() {
876
- const { a, b: h } = n, y = t.mul(h, Yt), { px: b, py: N, pz: O } = this;
877
- let A = t.ZERO, E = t.ZERO, x = t.ZERO, B = t.mul(b, b), R = t.mul(N, N), z = t.mul(O, O), H = t.mul(b, N);
878
- return H = t.add(H, H), x = t.mul(b, O), x = t.add(x, x), A = t.mul(a, x), E = t.mul(y, z), E = t.add(A, E), A = t.sub(R, E), E = t.add(R, E), E = t.mul(A, E), A = t.mul(H, A), x = t.mul(y, x), z = t.mul(a, z), H = t.sub(B, z), H = t.mul(a, H), H = t.add(H, x), x = t.add(B, B), B = t.add(x, B), B = t.add(B, z), B = t.mul(B, H), E = t.add(E, B), z = t.mul(N, O), z = t.add(z, z), B = t.mul(z, H), A = t.sub(A, B), x = t.mul(z, R), x = t.add(x, x), x = t.add(x, x), new d(A, E, x);
879
- }
880
- // Renes-Costello-Batina exception-free addition formula.
881
- // There is 30% faster Jacobian formula, but it is not complete.
882
- // https://eprint.iacr.org/2015/1060, algorithm 1
883
- // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
884
- add(a) {
885
- u(a);
886
- const { px: h, py: y, pz: b } = this, { px: N, py: O, pz: A } = a;
887
- let E = t.ZERO, x = t.ZERO, B = t.ZERO;
888
- const R = n.a, z = t.mul(n.b, Yt);
889
- let H = t.mul(h, N), j = t.mul(y, O), l = t.mul(b, A), w = t.add(h, y), g = t.add(N, O);
890
- w = t.mul(w, g), g = t.add(H, j), w = t.sub(w, g), g = t.add(h, b);
891
- let v = t.add(N, A);
892
- return g = t.mul(g, v), v = t.add(H, l), g = t.sub(g, v), v = t.add(y, b), E = t.add(O, A), v = t.mul(v, E), E = t.add(j, l), v = t.sub(v, E), B = t.mul(R, g), E = t.mul(z, l), B = t.add(E, B), E = t.sub(j, B), B = t.add(j, B), x = t.mul(E, B), j = t.add(H, H), j = t.add(j, H), l = t.mul(R, l), g = t.mul(z, g), j = t.add(j, l), l = t.sub(H, l), l = t.mul(R, l), g = t.add(g, l), H = t.mul(j, g), x = t.add(x, H), H = t.mul(v, g), E = t.mul(w, E), E = t.sub(E, H), H = t.mul(w, j), B = t.mul(v, B), B = t.add(B, H), new d(E, x, B);
893
- }
894
- subtract(a) {
895
- return this.add(a.negate());
896
- }
897
- is0() {
898
- return this.equals(d.ZERO);
899
- }
900
- wNAF(a) {
901
- return L.wNAFCached(this, a, d.normalizeZ);
902
- }
903
- /**
904
- * Non-constant-time multiplication. Uses double-and-add algorithm.
905
- * It's faster, but should only be used when you don't care about
906
- * an exposed private key e.g. sig verification, which works over *public* keys.
907
- */
908
- multiplyUnsafe(a) {
909
- const { endo: h, n: y } = n;
910
- ot("scalar", a, G, y);
911
- const b = d.ZERO;
912
- if (a === G)
913
- return b;
914
- if (this.is0() || a === k)
915
- return this;
916
- if (!h || L.hasPrecomputes(this))
917
- return L.wNAFCachedUnsafe(this, a, d.normalizeZ);
918
- let { k1neg: N, k1: O, k2neg: A, k2: E } = h.splitScalar(a), x = b, B = b, R = this;
919
- for (; O > G || E > G; )
920
- O & k && (x = x.add(R)), E & k && (B = B.add(R)), R = R.double(), O >>= k, E >>= k;
921
- return N && (x = x.negate()), A && (B = B.negate()), B = new d(t.mul(B.px, h.beta), B.py, B.pz), x.add(B);
922
- }
923
- /**
924
- * Constant time multiplication.
925
- * Uses wNAF method. Windowed method may be 10% faster,
926
- * but takes 2x longer to generate and consumes 2x memory.
927
- * Uses precomputes when available.
928
- * Uses endomorphism for Koblitz curves.
929
- * @param scalar by which the point would be multiplied
930
- * @returns New point
931
- */
932
- multiply(a) {
933
- const { endo: h, n: y } = n;
934
- ot("scalar", a, k, y);
935
- let b, N;
936
- if (h) {
937
- const { k1neg: O, k1: A, k2neg: E, k2: x } = h.splitScalar(a);
938
- let { p: B, f: R } = this.wNAF(A), { p: z, f: H } = this.wNAF(x);
939
- B = L.constTimeNegate(O, B), z = L.constTimeNegate(E, z), z = new d(t.mul(z.px, h.beta), z.py, z.pz), b = B.add(z), N = R.add(H);
940
- } else {
941
- const { p: O, f: A } = this.wNAF(a);
942
- b = O, N = A;
943
- }
944
- return d.normalizeZ([b, N])[0];
945
- }
946
- /**
947
- * Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
948
- * Not using Strauss-Shamir trick: precomputation tables are faster.
949
- * The trick could be useful if both P and Q are not G (not in our case).
950
- * @returns non-zero affine point
951
- */
952
- multiplyAndAddUnsafe(a, h, y) {
953
- const b = d.BASE, N = (A, E) => E === G || E === k || !A.equals(b) ? A.multiplyUnsafe(E) : A.multiply(E), O = N(this, h).add(N(a, y));
954
- return O.is0() ? void 0 : O;
955
- }
956
- // Converts Projective point to affine (x, y) coordinates.
957
- // Can accept precomputed Z^-1 - for example, from invertBatch.
958
- // (x, y, z) ∋ (x=x/z, y=y/z)
959
- toAffine(a) {
960
- return m(this, a);
961
- }
962
- isTorsionFree() {
963
- const { h: a, isTorsionFree: h } = n;
964
- if (a === k)
965
- return !0;
966
- if (h)
967
- return h(d, this);
968
- throw new Error("isTorsionFree() has not been declared for the elliptic curve");
969
- }
970
- clearCofactor() {
971
- const { h: a, clearCofactor: h } = n;
972
- return a === k ? this : h ? h(d, this) : this.multiplyUnsafe(n.h);
973
- }
974
- toRawBytes(a = !0) {
975
- return at("isCompressed", a), this.assertValidity(), s(d, this, a);
976
- }
977
- toHex(a = !0) {
978
- return at("isCompressed", a), ut(this.toRawBytes(a));
979
- }
980
- }
981
- d.BASE = new d(n.Gx, n.Gy, t.ONE), d.ZERO = new d(t.ZERO, t.ONE, t.ZERO);
982
- const S = n.nBitLength, L = qe(d, n.endo ? Math.ceil(S / 2) : S);
983
- return {
984
- CURVE: n,
985
- ProjectivePoint: d,
986
- normPrivateKeyToScalar: o,
987
- weierstrassEquation: c,
988
- isWithinCurveOrder: f
989
- };
990
- }
991
- function Ze(e) {
992
- const n = oe(e);
993
- return pt(n, {
994
- hash: "hash",
995
- hmac: "function",
996
- randomBytes: "function"
997
- }, {
998
- bits2int: "function",
999
- bits2int_modN: "function",
1000
- lowS: "boolean"
1001
- }), Object.freeze({ lowS: !0, ...n });
1002
- }
1003
- function Ue(e) {
1004
- const n = Ze(e), { Fp: t, n: r } = n, s = t.BYTES + 1, i = 2 * t.BYTES + 1;
1005
- function c(l) {
1006
- return V(l, r);
1007
- }
1008
- function f(l) {
1009
- return Nt(l, r);
1010
- }
1011
- const { ProjectivePoint: o, normPrivateKeyToScalar: u, weierstrassEquation: m, isWithinCurveOrder: I } = ze({
1012
- ...n,
1013
- toBytes(l, w, g) {
1014
- const v = w.toAffine(), q = t.toBytes(v.x), U = wt;
1015
- return at("isCompressed", g), g ? U(Uint8Array.from([w.hasEvenY() ? 2 : 3]), q) : U(Uint8Array.from([4]), q, t.toBytes(v.y));
1016
- },
1017
- fromBytes(l) {
1018
- const w = l.length, g = l[0], v = l.subarray(1);
1019
- if (w === s && (g === 2 || g === 3)) {
1020
- const q = tt(v);
1021
- if (!Zt(q, k, t.ORDER))
1022
- throw new Error("Point is not on curve");
1023
- const U = m(q);
1024
- let _;
1025
- try {
1026
- _ = t.sqrt(U);
1027
- } catch (F) {
1028
- const M = F instanceof Error ? ": " + F.message : "";
1029
- throw new Error("Point is not on curve" + M);
1030
- }
1031
- const C = (_ & k) === k;
1032
- return (g & 1) === 1 !== C && (_ = t.neg(_)), { x: q, y: _ };
1033
- } else if (w === i && g === 4) {
1034
- const q = t.fromBytes(v.subarray(0, t.BYTES)), U = t.fromBytes(v.subarray(t.BYTES, 2 * t.BYTES));
1035
- return { x: q, y: U };
1036
- } else {
1037
- const q = s, U = i;
1038
- throw new Error("invalid Point, expected length of " + q + ", or uncompressed " + U + ", got " + w);
1039
- }
1040
- }
1041
- }), d = (l) => ut(lt(l, n.nByteLength));
1042
- function S(l) {
1043
- const w = r >> k;
1044
- return l > w;
1045
- }
1046
- function L(l) {
1047
- return S(l) ? c(-l) : l;
1048
- }
1049
- const p = (l, w, g) => tt(l.slice(w, g));
1050
- class a {
1051
- constructor(w, g, v) {
1052
- ot("r", w, k, r), ot("s", g, k, r), this.r = w, this.s = g, v != null && (this.recovery = v), Object.freeze(this);
1053
- }
1054
- // pair (bytes of r, bytes of s)
1055
- static fromCompact(w) {
1056
- const g = n.nByteLength;
1057
- return w = K("compactSignature", w, g * 2), new a(p(w, 0, g), p(w, g, 2 * g));
1058
- }
1059
- // DER encoded ECDSA signature
1060
- // https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
1061
- static fromDER(w) {
1062
- const { r: g, s: v } = W.toSig(K("DER", w));
1063
- return new a(g, v);
1064
- }
1065
- /**
1066
- * @todo remove
1067
- * @deprecated
1068
- */
1069
- assertValidity() {
1070
- }
1071
- addRecoveryBit(w) {
1072
- return new a(this.r, this.s, w);
1073
- }
1074
- recoverPublicKey(w) {
1075
- const { r: g, s: v, recovery: q } = this, U = A(K("msgHash", w));
1076
- if (q == null || ![0, 1, 2, 3].includes(q))
1077
- throw new Error("recovery id invalid");
1078
- const _ = q === 2 || q === 3 ? g + n.n : g;
1079
- if (_ >= t.ORDER)
1080
- throw new Error("recovery id 2 or 3 invalid");
1081
- const C = q & 1 ? "03" : "02", P = o.fromHex(C + d(_)), F = f(_), M = c(-U * F), et = c(v * F), D = o.BASE.multiplyAndAddUnsafe(P, M, et);
1082
- if (!D)
1083
- throw new Error("point at infinify");
1084
- return D.assertValidity(), D;
1085
- }
1086
- // Signatures should be low-s, to prevent malleability.
1087
- hasHighS() {
1088
- return S(this.s);
1089
- }
1090
- normalizeS() {
1091
- return this.hasHighS() ? new a(this.r, c(-this.s), this.recovery) : this;
1092
- }
1093
- // DER-encoded
1094
- toDERRawBytes() {
1095
- return ht(this.toDERHex());
1096
- }
1097
- toDERHex() {
1098
- return W.hexFromSig({ r: this.r, s: this.s });
1099
- }
1100
- // padded bytes of r, then padded bytes of s
1101
- toCompactRawBytes() {
1102
- return ht(this.toCompactHex());
1103
- }
1104
- toCompactHex() {
1105
- return d(this.r) + d(this.s);
1106
- }
1107
- }
1108
- const h = {
1109
- isValidPrivateKey(l) {
1110
- try {
1111
- return u(l), !0;
1112
- } catch {
1113
- return !1;
1114
- }
1115
- },
1116
- normPrivateKeyToScalar: u,
1117
- /**
1118
- * Produces cryptographically secure private key from random of size
1119
- * (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
1120
- */
1121
- randomPrivateKey: () => {
1122
- const l = ee(n.n);
1123
- return Ae(n.randomBytes(l), n.n);
1124
- },
1125
- /**
1126
- * Creates precompute table for an arbitrary EC point. Makes point "cached".
1127
- * Allows to massively speed-up `point.multiply(scalar)`.
1128
- * @returns cached point
1129
- * @example
1130
- * const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
1131
- * fast.multiply(privKey); // much faster ECDH now
1132
- */
1133
- precompute(l = 8, w = o.BASE) {
1134
- return w._setWindowSize(l), w.multiply(BigInt(3)), w;
1135
- }
1136
- };
1137
- function y(l, w = !0) {
1138
- return o.fromPrivateKey(l).toRawBytes(w);
1139
- }
1140
- function b(l) {
1141
- const w = it(l), g = typeof l == "string", v = (w || g) && l.length;
1142
- return w ? v === s || v === i : g ? v === 2 * s || v === 2 * i : l instanceof o;
1143
- }
1144
- function N(l, w, g = !0) {
1145
- if (b(l))
1146
- throw new Error("first arg must be private key");
1147
- if (!b(w))
1148
- throw new Error("second arg must be public key");
1149
- return o.fromHex(w).multiply(u(l)).toRawBytes(g);
1150
- }
1151
- const O = n.bits2int || function(l) {
1152
- if (l.length > 8192)
1153
- throw new Error("input is too large");
1154
- const w = tt(l), g = l.length * 8 - n.nBitLength;
1155
- return g > 0 ? w >> BigInt(g) : w;
1156
- }, A = n.bits2int_modN || function(l) {
1157
- return c(O(l));
1158
- }, E = gt(n.nBitLength);
1159
- function x(l) {
1160
- return ot("num < 2^" + n.nBitLength, l, G, E), lt(l, n.nByteLength);
1161
- }
1162
- function B(l, w, g = R) {
1163
- if (["recovered", "canonical"].some((X) => X in g))
1164
- throw new Error("sign() legacy options not supported");
1165
- const { hash: v, randomBytes: q } = n;
1166
- let { lowS: U, prehash: _, extraEntropy: C } = g;
1167
- U == null && (U = !0), l = K("msgHash", l), jt(g), _ && (l = K("prehashed msgHash", v(l)));
1168
- const P = A(l), F = u(w), M = [x(F), x(P)];
1169
- if (C != null && C !== !1) {
1170
- const X = C === !0 ? q(t.BYTES) : C;
1171
- M.push(K("extraEntropy", X));
1172
- }
1173
- const et = wt(...M), D = P;
1174
- function yt(X) {
1175
- const nt = O(X);
1176
- if (!I(nt))
1177
- return;
1178
- const mt = f(nt), st = o.BASE.multiply(nt).toAffine(), Q = c(st.x);
1179
- if (Q === G)
1180
- return;
1181
- const ct = c(mt * c(D + Q * F));
1182
- if (ct === G)
1183
- return;
1184
- let ft = (st.x === Q ? 0 : 2) | Number(st.y & k), rt = ct;
1185
- return U && S(ct) && (rt = L(ct), ft ^= 1), new a(Q, rt, ft);
1186
- }
1187
- return { seed: et, k2sig: yt };
1188
- }
1189
- const R = { lowS: n.lowS, prehash: !1 }, z = { lowS: n.lowS, prehash: !1 };
1190
- function H(l, w, g = R) {
1191
- const { seed: v, k2sig: q } = B(l, w, g), U = n;
1192
- return ge(U.hash.outputLen, U.nByteLength, U.hmac)(v, q);
1193
- }
1194
- o.BASE._setWindowSize(8);
1195
- function j(l, w, g, v = z) {
1196
- var ft;
1197
- const q = l;
1198
- w = K("msgHash", w), g = K("publicKey", g);
1199
- const { lowS: U, prehash: _, format: C } = v;
1200
- if (jt(v), "strict" in v)
1201
- throw new Error("options.strict was renamed to lowS");
1202
- if (C !== void 0 && C !== "compact" && C !== "der")
1203
- throw new Error("format must be compact or der");
1204
- const P = typeof q == "string" || it(q), F = !P && !C && typeof q == "object" && q !== null && typeof q.r == "bigint" && typeof q.s == "bigint";
1205
- if (!P && !F)
1206
- throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
1207
- let M, et;
1208
- try {
1209
- if (F && (M = new a(q.r, q.s)), P) {
1210
- try {
1211
- C !== "compact" && (M = a.fromDER(q));
1212
- } catch (rt) {
1213
- if (!(rt instanceof W.Err))
1214
- throw rt;
1215
- }
1216
- !M && C !== "der" && (M = a.fromCompact(q));
1217
- }
1218
- et = o.fromHex(g);
1219
- } catch {
1220
- return !1;
1221
- }
1222
- if (!M || U && M.hasHighS())
1223
- return !1;
1224
- _ && (w = n.hash(w));
1225
- const { r: D, s: yt } = M, X = A(w), nt = f(yt), mt = c(X * nt), st = c(D * nt), Q = (ft = o.BASE.multiplyAndAddUnsafe(et, mt, st)) == null ? void 0 : ft.toAffine();
1226
- return Q ? c(Q.x) === D : !1;
1227
- }
1228
- return {
1229
- CURVE: n,
1230
- getPublicKey: y,
1231
- getSharedSecret: N,
1232
- sign: H,
1233
- verify: j,
1234
- ProjectivePoint: o,
1235
- Signature: a,
1236
- utils: h
1237
- };
1238
- }
1239
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1240
- function ke(e) {
1241
- return {
1242
- hash: e,
1243
- hmac: (n, ...t) => $t(e, n, le(...t)),
1244
- randomBytes: ue
1245
- };
1246
- }
1247
- function Te(e, n) {
1248
- const t = (r) => Ue({ ...e, ...ke(r) });
1249
- return { ...t(n), create: t };
1250
- }
1251
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1252
- const ie = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"), Ft = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"), Re = BigInt(1), Ot = BigInt(2), Kt = (e, n) => (e + n / Ot) / n;
1253
- function Ce(e) {
1254
- const n = ie, t = BigInt(3), r = BigInt(6), s = BigInt(11), i = BigInt(22), c = BigInt(23), f = BigInt(44), o = BigInt(88), u = e * e * e % n, m = u * u * e % n, I = Y(m, t, n) * m % n, d = Y(I, t, n) * m % n, S = Y(d, Ot, n) * u % n, L = Y(S, s, n) * S % n, p = Y(L, i, n) * L % n, a = Y(p, f, n) * p % n, h = Y(a, o, n) * a % n, y = Y(h, f, n) * p % n, b = Y(y, t, n) * m % n, N = Y(b, c, n) * L % n, O = Y(N, r, n) * u % n, A = Y(O, Ot, n);
1255
- if (!Ht.eql(Ht.sqr(A), e))
1256
- throw new Error("Cannot find square root");
1257
- return A;
1258
- }
1259
- const Ht = Jt(ie, void 0, void 0, { sqrt: Ce }), _e = Te({
1260
- a: BigInt(0),
1261
- b: BigInt(7),
1262
- Fp: Ht,
1263
- n: Ft,
1264
- Gx: BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),
1265
- Gy: BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),
1266
- h: BigInt(1),
1267
- // Cofactor
1268
- lowS: !0,
1269
- // Allow only low-S signatures by default in sign() and verify()
1270
- endo: {
1271
- // Endomorphism, see above
1272
- beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
1273
- splitScalar: (e) => {
1274
- const n = Ft, t = BigInt("0x3086d221a7d46bcde86c90e49284eb15"), r = -Re * BigInt("0xe4437ed6010e88286f547fa90abfe4c3"), s = BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), i = t, c = BigInt("0x100000000000000000000000000000000"), f = Kt(i * e, n), o = Kt(-r * e, n);
1275
- let u = V(e - f * t - o * s, n), m = V(-f * r - o * i, n);
1276
- const I = u > c, d = m > c;
1277
- if (I && (u = n - u), d && (m = n - m), u > c || m > c)
1278
- throw new Error("splitScalar: Endomorphism failed, k=" + e);
1279
- return { k1neg: I, k1: u, k2neg: d, k2: m };
1280
- }
1281
- }
1282
- }, de);
1283
- BigInt(0);
1284
- _e.ProjectivePoint;
1285
- export {
1286
- _e as secp256k1
1287
- };