@gardenfi/utils 2.1.2-beta.6 → 2.1.2-beta.7

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.
Files changed (75) hide show
  1. package/dist/index102.cjs +1 -1
  2. package/dist/index102.js +6 -20
  3. package/dist/index103.cjs +1 -1
  4. package/dist/index103.js +20 -6
  5. package/dist/index23.cjs +1 -1
  6. package/dist/index23.js +3 -3
  7. package/dist/index24.cjs +1 -1
  8. package/dist/index24.js +2 -2
  9. package/dist/index35.cjs +1 -1
  10. package/dist/index35.js +2 -2
  11. package/dist/index36.cjs +1 -1
  12. package/dist/index36.js +1 -1
  13. package/dist/index41.cjs +1 -1
  14. package/dist/index41.js +2 -2
  15. package/dist/index45.cjs +1 -1
  16. package/dist/index45.js +10 -38
  17. package/dist/index46.cjs +1 -1
  18. package/dist/index46.js +32 -32
  19. package/dist/index47.cjs +1 -1
  20. package/dist/index47.js +37 -66
  21. package/dist/index48.cjs +1 -1
  22. package/dist/index48.js +34 -112
  23. package/dist/index49.cjs +1 -1
  24. package/dist/index49.js +63 -61
  25. package/dist/index50.cjs +1 -1
  26. package/dist/index50.js +113 -2
  27. package/dist/index51.cjs +1 -1
  28. package/dist/index51.js +66 -15
  29. package/dist/index52.cjs +1 -1
  30. package/dist/index52.js +2 -224
  31. package/dist/index53.cjs +1 -1
  32. package/dist/index53.js +13 -228
  33. package/dist/index54.cjs +1 -1
  34. package/dist/index54.js +220 -9
  35. package/dist/index55.cjs +1 -1
  36. package/dist/index55.js +229 -32
  37. package/dist/index58.cjs +1 -1
  38. package/dist/index58.js +3 -3
  39. package/dist/index68.cjs +1 -1
  40. package/dist/index68.js +2 -2
  41. package/dist/index71.cjs +1 -1
  42. package/dist/index71.js +1 -1
  43. package/dist/index72.cjs +1 -1
  44. package/dist/index72.js +191 -4
  45. package/dist/index73.cjs +1 -1
  46. package/dist/index73.js +58 -185
  47. package/dist/index74.cjs +1 -1
  48. package/dist/index74.js +4 -64
  49. package/dist/index79.cjs +1 -1
  50. package/dist/index79.js +1 -1
  51. package/dist/index80.cjs +2 -1
  52. package/dist/index80.js +35 -2
  53. package/dist/index81.cjs +1 -2
  54. package/dist/index81.js +5 -34
  55. package/dist/index82.cjs +1 -1
  56. package/dist/index82.js +2 -6
  57. package/dist/index83.cjs +1 -1
  58. package/dist/index83.js +1 -1
  59. package/dist/index84.cjs +1 -1
  60. package/dist/index84.js +1 -1
  61. package/dist/index85.cjs +1 -1
  62. package/dist/index85.js +1 -1
  63. package/dist/index90.cjs +1 -1
  64. package/dist/index90.js +10 -16
  65. package/dist/index91.cjs +1 -1
  66. package/dist/index91.js +16 -641
  67. package/dist/index92.cjs +1 -1
  68. package/dist/index92.js +624 -153
  69. package/dist/index93.cjs +1 -1
  70. package/dist/index93.js +170 -10
  71. package/dist/index94.cjs +1 -1
  72. package/dist/index94.js +1 -1
  73. package/dist/index99.cjs +1 -1
  74. package/dist/index99.js +2 -2
  75. package/package.json +1 -1
package/dist/index92.js CHANGED
@@ -1,175 +1,646 @@
1
- import { validateField as z, nLength as F } from "./index52.js";
2
- import { validateObject as x, bitLen as O } from "./index53.js";
1
+ import { validateBasic as mt, wNAF as gt, pippenger as Et } from "./index93.js";
2
+ import { Field as bt, mod as wt, getMinHashLength as vt, mapHashToField as Bt, invert as xt } from "./index54.js";
3
+ import * as St from "./index55.js";
4
+ import { bytesToNumberBE as nt, bitMask as Rt, validateObject as yt, concatBytes as st, ensureBytes as Y, aInRange as Q, bytesToHex as ct, hexToBytes as lt, isBytes as at, createHmacDrbg as Ot, memoized as ut, abool as tt, inRange as pt, abytes as At, numberToHexUnpadded as et, numberToBytesBE as dt } from "./index55.js";
3
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
4
- const I = BigInt(0), p = BigInt(1);
5
- function m(e, r) {
6
- const t = r.negate();
7
- return e ? t : r;
6
+ function ft(f) {
7
+ f.lowS !== void 0 && tt("lowS", f.lowS), f.prehash !== void 0 && tt("prehash", f.prehash);
8
8
  }
9
- function A(e, r) {
10
- if (!Number.isSafeInteger(e) || e <= 0 || e > r)
11
- throw new Error("invalid window size, expected [1.." + r + "], got W=" + e);
12
- }
13
- function b(e, r) {
14
- A(e, r);
15
- const t = Math.ceil(r / e) + 1, n = 2 ** (e - 1);
16
- return { windows: t, windowSize: n };
17
- }
18
- function L(e, r) {
19
- if (!Array.isArray(e))
20
- throw new Error("array expected");
21
- e.forEach((t, n) => {
22
- if (!(t instanceof r))
23
- throw new Error("invalid point at index " + n);
24
- });
25
- }
26
- function k(e, r) {
27
- if (!Array.isArray(e))
28
- throw new Error("array of scalars expected");
29
- e.forEach((t, n) => {
30
- if (!r.isValid(t))
31
- throw new Error("invalid scalar at index " + n);
9
+ function Nt(f) {
10
+ const n = mt(f);
11
+ yt(n, {
12
+ a: "field",
13
+ b: "field"
14
+ }, {
15
+ allowedPrivateKeyLengths: "array",
16
+ wrapPrivateKey: "boolean",
17
+ isTorsionFree: "function",
18
+ clearCofactor: "function",
19
+ allowInfinityPoint: "boolean",
20
+ fromBytes: "function",
21
+ toBytes: "function"
32
22
  });
23
+ const { endo: t, Fp: E, a: R } = n;
24
+ if (t) {
25
+ if (!E.eql(R, E.ZERO))
26
+ throw new Error("invalid endomorphism, can only be defined for Koblitz curves that have a=0");
27
+ if (typeof t != "object" || typeof t.beta != "bigint" || typeof t.splitScalar != "function")
28
+ throw new Error("invalid endomorphism, expected beta: bigint and splitScalar: function");
29
+ }
30
+ return Object.freeze({ ...n });
33
31
  }
34
- const B = /* @__PURE__ */ new WeakMap(), E = /* @__PURE__ */ new WeakMap();
35
- function S(e) {
36
- return E.get(e) || 1;
32
+ const { bytesToNumberBE: Tt, hexToBytes: Zt } = St;
33
+ class qt extends Error {
34
+ constructor(n = "") {
35
+ super(n);
36
+ }
37
37
  }
38
- function v(e, r) {
39
- return {
40
- constTimeNegate: m,
41
- hasPrecomputes(t) {
42
- return S(t) !== 1;
38
+ const U = {
39
+ // asn.1 DER encoding utils
40
+ Err: qt,
41
+ // Basic building block is TLV (Tag-Length-Value)
42
+ _tlv: {
43
+ encode: (f, n) => {
44
+ const { Err: t } = U;
45
+ if (f < 0 || f > 256)
46
+ throw new t("tlv.encode: wrong tag");
47
+ if (n.length & 1)
48
+ throw new t("tlv.encode: unpadded data");
49
+ const E = n.length / 2, R = et(E);
50
+ if (R.length / 2 & 128)
51
+ throw new t("tlv.encode: long form length too big");
52
+ const Z = E > 127 ? et(R.length / 2 | 128) : "";
53
+ return et(f) + Z + R + n;
43
54
  },
44
- // non-const time multiplication ladder
45
- unsafeLadder(t, n, i = e.ZERO) {
46
- let o = t;
47
- for (; n > I; )
48
- n & p && (i = i.add(o)), o = o.double(), n >>= p;
49
- return i;
55
+ // v - value, l - left bytes (unparsed)
56
+ decode(f, n) {
57
+ const { Err: t } = U;
58
+ let E = 0;
59
+ if (f < 0 || f > 256)
60
+ throw new t("tlv.encode: wrong tag");
61
+ if (n.length < 2 || n[E++] !== f)
62
+ throw new t("tlv.decode: wrong tlv");
63
+ const R = n[E++], Z = !!(R & 128);
64
+ let B = 0;
65
+ if (!Z)
66
+ B = R;
67
+ else {
68
+ const S = R & 127;
69
+ if (!S)
70
+ throw new t("tlv.decode(long): indefinite length not supported");
71
+ if (S > 4)
72
+ throw new t("tlv.decode(long): byte length is too big");
73
+ const L = n.subarray(E, E + S);
74
+ if (L.length !== S)
75
+ throw new t("tlv.decode: length bytes not complete");
76
+ if (L[0] === 0)
77
+ throw new t("tlv.decode(long): zero leftmost byte");
78
+ for (const P of L)
79
+ B = B << 8 | P;
80
+ if (E += S, B < 128)
81
+ throw new t("tlv.decode(long): not minimal encoding");
82
+ }
83
+ const H = n.subarray(E, E + B);
84
+ if (H.length !== B)
85
+ throw new t("tlv.decode: wrong value length");
86
+ return { v: H, l: n.subarray(E + B) };
87
+ }
88
+ },
89
+ // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
90
+ // since we always use positive integers here. It must always be empty:
91
+ // - add zero byte if exists
92
+ // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
93
+ _int: {
94
+ encode(f) {
95
+ const { Err: n } = U;
96
+ if (f < j)
97
+ throw new n("integer: negative integers are not allowed");
98
+ let t = et(f);
99
+ if (Number.parseInt(t[0], 16) & 8 && (t = "00" + t), t.length & 1)
100
+ throw new n("unexpected DER parsing assertion: unpadded hex");
101
+ return t;
50
102
  },
103
+ decode(f) {
104
+ const { Err: n } = U;
105
+ if (f[0] & 128)
106
+ throw new n("invalid signature integer: negative");
107
+ if (f[0] === 0 && !(f[1] & 128))
108
+ throw new n("invalid signature integer: unnecessary leading zero");
109
+ return Tt(f);
110
+ }
111
+ },
112
+ toSig(f) {
113
+ const { Err: n, _int: t, _tlv: E } = U, R = typeof f == "string" ? Zt(f) : f;
114
+ At(R);
115
+ const { v: Z, l: B } = E.decode(48, R);
116
+ if (B.length)
117
+ throw new n("invalid signature: left bytes after parsing");
118
+ const { v: H, l: S } = E.decode(2, Z), { v: L, l: P } = E.decode(2, S);
119
+ if (P.length)
120
+ throw new n("invalid signature: left bytes after parsing");
121
+ return { r: t.decode(H), s: t.decode(L) };
122
+ },
123
+ hexFromSig(f) {
124
+ const { _tlv: n, _int: t } = U, E = n.encode(2, t.encode(f.r)), R = n.encode(2, t.encode(f.s)), Z = E + R;
125
+ return n.encode(48, Z);
126
+ }
127
+ }, j = BigInt(0), A = BigInt(1);
128
+ BigInt(2);
129
+ const ht = BigInt(3);
130
+ BigInt(4);
131
+ function zt(f) {
132
+ const n = Nt(f), { Fp: t } = n, E = bt(n.n, n.nBitLength), R = n.toBytes || ((m, e, i) => {
133
+ const c = e.toAffine();
134
+ return st(Uint8Array.from([4]), t.toBytes(c.x), t.toBytes(c.y));
135
+ }), Z = n.fromBytes || ((m) => {
136
+ const e = m.subarray(1), i = t.fromBytes(e.subarray(0, t.BYTES)), c = t.fromBytes(e.subarray(t.BYTES, 2 * t.BYTES));
137
+ return { x: i, y: c };
138
+ });
139
+ function B(m) {
140
+ const { a: e, b: i } = n, c = t.sqr(m), u = t.mul(c, m);
141
+ return t.add(t.add(u, t.mul(m, e)), i);
142
+ }
143
+ if (!t.eql(t.sqr(n.Gy), B(n.Gx)))
144
+ throw new Error("bad generator point: equation left != right");
145
+ function H(m) {
146
+ return pt(m, A, n.n);
147
+ }
148
+ function S(m) {
149
+ const { allowedPrivateKeyLengths: e, nByteLength: i, wrapPrivateKey: c, n: u } = n;
150
+ if (e && typeof m != "bigint") {
151
+ if (at(m) && (m = ct(m)), typeof m != "string" || !e.includes(m.length))
152
+ throw new Error("invalid private key");
153
+ m = m.padStart(i * 2, "0");
154
+ }
155
+ let y;
156
+ try {
157
+ y = typeof m == "bigint" ? m : nt(Y("private key", m, i));
158
+ } catch {
159
+ throw new Error("invalid private key, expected hex or " + i + " bytes, got " + typeof m);
160
+ }
161
+ return c && (y = wt(y, u)), Q("private key", y, A, u), y;
162
+ }
163
+ function L(m) {
164
+ if (!(m instanceof w))
165
+ throw new Error("ProjectivePoint expected");
166
+ }
167
+ const P = ut((m, e) => {
168
+ const { px: i, py: c, pz: u } = m;
169
+ if (t.eql(u, t.ONE))
170
+ return { x: i, y: c };
171
+ const y = m.is0();
172
+ e == null && (e = y ? t.ONE : t.inv(u));
173
+ const v = t.mul(i, e), p = t.mul(c, e), a = t.mul(u, e);
174
+ if (y)
175
+ return { x: t.ZERO, y: t.ZERO };
176
+ if (!t.eql(a, t.ONE))
177
+ throw new Error("invZ was invalid");
178
+ return { x: v, y: p };
179
+ }), rt = ut((m) => {
180
+ if (m.is0()) {
181
+ if (n.allowInfinityPoint && !t.is0(m.py))
182
+ return;
183
+ throw new Error("bad point: ZERO");
184
+ }
185
+ const { x: e, y: i } = m.toAffine();
186
+ if (!t.isValid(e) || !t.isValid(i))
187
+ throw new Error("bad point: x or y not FE");
188
+ const c = t.sqr(i), u = B(e);
189
+ if (!t.eql(c, u))
190
+ throw new Error("bad point: equation left != right");
191
+ if (!m.isTorsionFree())
192
+ throw new Error("bad point: not in prime-order subgroup");
193
+ return !0;
194
+ });
195
+ class w {
196
+ constructor(e, i, c) {
197
+ if (this.px = e, this.py = i, this.pz = c, e == null || !t.isValid(e))
198
+ throw new Error("x required");
199
+ if (i == null || !t.isValid(i))
200
+ throw new Error("y required");
201
+ if (c == null || !t.isValid(c))
202
+ throw new Error("z required");
203
+ Object.freeze(this);
204
+ }
205
+ // Does not validate if the point is on-curve.
206
+ // Use fromHex instead, or call assertValidity() later.
207
+ static fromAffine(e) {
208
+ const { x: i, y: c } = e || {};
209
+ if (!e || !t.isValid(i) || !t.isValid(c))
210
+ throw new Error("invalid affine point");
211
+ if (e instanceof w)
212
+ throw new Error("projective point not allowed");
213
+ const u = (y) => t.eql(y, t.ZERO);
214
+ return u(i) && u(c) ? w.ZERO : new w(i, c, t.ONE);
215
+ }
216
+ get x() {
217
+ return this.toAffine().x;
218
+ }
219
+ get y() {
220
+ return this.toAffine().y;
221
+ }
51
222
  /**
52
- * Creates a wNAF precomputation window. Used for caching.
53
- * Default window size is set by `utils.precompute()` and is equal to 8.
54
- * Number of precomputed points depends on the curve size:
55
- * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
56
- * - 𝑊 is the window size
57
- * - 𝑛 is the bitlength of the curve order.
58
- * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
59
- * @param elm Point instance
60
- * @param W window size
61
- * @returns precomputed point tables flattened to a single array
223
+ * Takes a bunch of Projective Points but executes only one
224
+ * inversion on all of them. Inversion is very slow operation,
225
+ * so this improves performance massively.
226
+ * Optimization: converts a list of projective points to a list of identical points with Z=1.
62
227
  */
63
- precomputeWindow(t, n) {
64
- const { windows: i, windowSize: o } = b(n, r), a = [];
65
- let c = t, d = c;
66
- for (let h = 0; h < i; h++) {
67
- d = c, a.push(d);
68
- for (let w = 1; w < o; w++)
69
- d = d.add(c), a.push(d);
70
- c = d.double();
71
- }
72
- return a;
73
- },
228
+ static normalizeZ(e) {
229
+ const i = t.invertBatch(e.map((c) => c.pz));
230
+ return e.map((c, u) => c.toAffine(i[u])).map(w.fromAffine);
231
+ }
74
232
  /**
75
- * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
76
- * @param W window size
77
- * @param precomputes precomputed tables
78
- * @param n scalar (we don't check here, but should be less than curve order)
79
- * @returns real and fake (for const-time) points
233
+ * Converts hash string or Uint8Array to Point.
234
+ * @param hex short/long ECDSA hex
80
235
  */
81
- wNAF(t, n, i) {
82
- const { windows: o, windowSize: a } = b(t, r);
83
- let c = e.ZERO, d = e.BASE;
84
- const h = BigInt(2 ** t - 1), w = 2 ** t, l = BigInt(t);
85
- for (let u = 0; u < o; u++) {
86
- const s = u * a;
87
- let f = Number(i & h);
88
- i >>= l, f > a && (f -= w, i += p);
89
- const g = s, N = s + Math.abs(f) - 1, y = u % 2 !== 0, M = f < 0;
90
- f === 0 ? d = d.add(m(y, n[g])) : c = c.add(m(M, n[N]));
91
- }
92
- return { p: c, f: d };
93
- },
236
+ static fromHex(e) {
237
+ const i = w.fromAffine(Z(Y("pointHex", e)));
238
+ return i.assertValidity(), i;
239
+ }
240
+ // Multiplies generator point by privateKey.
241
+ static fromPrivateKey(e) {
242
+ return w.BASE.multiply(S(e));
243
+ }
244
+ // Multiscalar Multiplication
245
+ static msm(e, i) {
246
+ return Et(w, E, e, i);
247
+ }
248
+ // "Private method", don't use it directly
249
+ _setWindowSize(e) {
250
+ D.setWindowSize(this, e);
251
+ }
252
+ // A point on curve is valid if it conforms to equation.
253
+ assertValidity() {
254
+ rt(this);
255
+ }
256
+ hasEvenY() {
257
+ const { y: e } = this.toAffine();
258
+ if (t.isOdd)
259
+ return !t.isOdd(e);
260
+ throw new Error("Field doesn't support isOdd");
261
+ }
94
262
  /**
95
- * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
96
- * @param W window size
97
- * @param precomputes precomputed tables
98
- * @param n scalar (we don't check here, but should be less than curve order)
99
- * @param acc accumulator point to add result of multiplication
100
- * @returns point
263
+ * Compare one point to another.
101
264
  */
102
- wNAFUnsafe(t, n, i, o = e.ZERO) {
103
- const { windows: a, windowSize: c } = b(t, r), d = BigInt(2 ** t - 1), h = 2 ** t, w = BigInt(t);
104
- for (let l = 0; l < a; l++) {
105
- const u = l * c;
106
- if (i === I)
107
- break;
108
- let s = Number(i & d);
109
- if (i >>= w, s > c && (s -= h, i += p), s === 0)
110
- continue;
111
- let f = n[u + Math.abs(s) - 1];
112
- s < 0 && (f = f.negate()), o = o.add(f);
265
+ equals(e) {
266
+ L(e);
267
+ const { px: i, py: c, pz: u } = this, { px: y, py: v, pz: p } = e, a = t.eql(t.mul(i, p), t.mul(y, u)), h = t.eql(t.mul(c, p), t.mul(v, u));
268
+ return a && h;
269
+ }
270
+ /**
271
+ * Flips point to one corresponding to (x, -y) in Affine coordinates.
272
+ */
273
+ negate() {
274
+ return new w(this.px, t.neg(this.py), this.pz);
275
+ }
276
+ // Renes-Costello-Batina exception-free doubling formula.
277
+ // There is 30% faster Jacobian formula, but it is not complete.
278
+ // https://eprint.iacr.org/2015/1060, algorithm 3
279
+ // Cost: 8M + 3S + 3*a + 2*b3 + 15add.
280
+ double() {
281
+ const { a: e, b: i } = n, c = t.mul(i, ht), { px: u, py: y, pz: v } = this;
282
+ let p = t.ZERO, a = t.ZERO, h = t.ZERO, l = t.mul(u, u), N = t.mul(y, y), x = t.mul(v, v), b = t.mul(u, y);
283
+ return b = t.add(b, b), h = t.mul(u, v), h = t.add(h, h), p = t.mul(e, h), a = t.mul(c, x), a = t.add(p, a), p = t.sub(N, a), a = t.add(N, a), a = t.mul(p, a), p = t.mul(b, p), h = t.mul(c, h), x = t.mul(e, x), b = t.sub(l, x), b = t.mul(e, b), b = t.add(b, h), h = t.add(l, l), l = t.add(h, l), l = t.add(l, x), l = t.mul(l, b), a = t.add(a, l), x = t.mul(y, v), x = t.add(x, x), l = t.mul(x, b), p = t.sub(p, l), h = t.mul(x, N), h = t.add(h, h), h = t.add(h, h), new w(p, a, h);
284
+ }
285
+ // Renes-Costello-Batina exception-free addition formula.
286
+ // There is 30% faster Jacobian formula, but it is not complete.
287
+ // https://eprint.iacr.org/2015/1060, algorithm 1
288
+ // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
289
+ add(e) {
290
+ L(e);
291
+ const { px: i, py: c, pz: u } = this, { px: y, py: v, pz: p } = e;
292
+ let a = t.ZERO, h = t.ZERO, l = t.ZERO;
293
+ const N = n.a, x = t.mul(n.b, ht);
294
+ let b = t.mul(i, y), F = t.mul(c, v), r = t.mul(u, p), o = t.add(i, c), s = t.add(y, v);
295
+ o = t.mul(o, s), s = t.add(b, F), o = t.sub(o, s), s = t.add(i, u);
296
+ let d = t.add(y, p);
297
+ return s = t.mul(s, d), d = t.add(b, r), s = t.sub(s, d), d = t.add(c, u), a = t.add(v, p), d = t.mul(d, a), a = t.add(F, r), d = t.sub(d, a), l = t.mul(N, s), a = t.mul(x, r), l = t.add(a, l), a = t.sub(F, l), l = t.add(F, l), h = t.mul(a, l), F = t.add(b, b), F = t.add(F, b), r = t.mul(N, r), s = t.mul(x, s), F = t.add(F, r), r = t.sub(b, r), r = t.mul(N, r), s = t.add(s, r), b = t.mul(F, s), h = t.add(h, b), b = t.mul(d, s), a = t.mul(o, a), a = t.sub(a, b), b = t.mul(o, F), l = t.mul(d, l), l = t.add(l, b), new w(a, h, l);
298
+ }
299
+ subtract(e) {
300
+ return this.add(e.negate());
301
+ }
302
+ is0() {
303
+ return this.equals(w.ZERO);
304
+ }
305
+ wNAF(e) {
306
+ return D.wNAFCached(this, e, w.normalizeZ);
307
+ }
308
+ /**
309
+ * Non-constant-time multiplication. Uses double-and-add algorithm.
310
+ * It's faster, but should only be used when you don't care about
311
+ * an exposed private key e.g. sig verification, which works over *public* keys.
312
+ */
313
+ multiplyUnsafe(e) {
314
+ const { endo: i, n: c } = n;
315
+ Q("scalar", e, j, c);
316
+ const u = w.ZERO;
317
+ if (e === j)
318
+ return u;
319
+ if (this.is0() || e === A)
320
+ return this;
321
+ if (!i || D.hasPrecomputes(this))
322
+ return D.wNAFCachedUnsafe(this, e, w.normalizeZ);
323
+ let { k1neg: y, k1: v, k2neg: p, k2: a } = i.splitScalar(e), h = u, l = u, N = this;
324
+ for (; v > j || a > j; )
325
+ v & A && (h = h.add(N)), a & A && (l = l.add(N)), N = N.double(), v >>= A, a >>= A;
326
+ return y && (h = h.negate()), p && (l = l.negate()), l = new w(t.mul(l.px, i.beta), l.py, l.pz), h.add(l);
327
+ }
328
+ /**
329
+ * Constant time multiplication.
330
+ * Uses wNAF method. Windowed method may be 10% faster,
331
+ * but takes 2x longer to generate and consumes 2x memory.
332
+ * Uses precomputes when available.
333
+ * Uses endomorphism for Koblitz curves.
334
+ * @param scalar by which the point would be multiplied
335
+ * @returns New point
336
+ */
337
+ multiply(e) {
338
+ const { endo: i, n: c } = n;
339
+ Q("scalar", e, A, c);
340
+ let u, y;
341
+ if (i) {
342
+ const { k1neg: v, k1: p, k2neg: a, k2: h } = i.splitScalar(e);
343
+ let { p: l, f: N } = this.wNAF(p), { p: x, f: b } = this.wNAF(h);
344
+ l = D.constTimeNegate(v, l), x = D.constTimeNegate(a, x), x = new w(t.mul(x.px, i.beta), x.py, x.pz), u = l.add(x), y = N.add(b);
345
+ } else {
346
+ const { p: v, f: p } = this.wNAF(e);
347
+ u = v, y = p;
113
348
  }
114
- return o;
115
- },
116
- getPrecomputes(t, n, i) {
117
- let o = B.get(n);
118
- return o || (o = this.precomputeWindow(n, t), t !== 1 && B.set(n, i(o))), o;
349
+ return w.normalizeZ([u, y])[0];
350
+ }
351
+ /**
352
+ * Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
353
+ * Not using Strauss-Shamir trick: precomputation tables are faster.
354
+ * The trick could be useful if both P and Q are not G (not in our case).
355
+ * @returns non-zero affine point
356
+ */
357
+ multiplyAndAddUnsafe(e, i, c) {
358
+ const u = w.BASE, y = (p, a) => a === j || a === A || !p.equals(u) ? p.multiplyUnsafe(a) : p.multiply(a), v = y(this, i).add(y(e, c));
359
+ return v.is0() ? void 0 : v;
360
+ }
361
+ // Converts Projective point to affine (x, y) coordinates.
362
+ // Can accept precomputed Z^-1 - for example, from invertBatch.
363
+ // (x, y, z) ∋ (x=x/z, y=y/z)
364
+ toAffine(e) {
365
+ return P(this, e);
366
+ }
367
+ isTorsionFree() {
368
+ const { h: e, isTorsionFree: i } = n;
369
+ if (e === A)
370
+ return !0;
371
+ if (i)
372
+ return i(w, this);
373
+ throw new Error("isTorsionFree() has not been declared for the elliptic curve");
374
+ }
375
+ clearCofactor() {
376
+ const { h: e, clearCofactor: i } = n;
377
+ return e === A ? this : i ? i(w, this) : this.multiplyUnsafe(n.h);
378
+ }
379
+ toRawBytes(e = !0) {
380
+ return tt("isCompressed", e), this.assertValidity(), R(w, this, e);
381
+ }
382
+ toHex(e = !0) {
383
+ return tt("isCompressed", e), ct(this.toRawBytes(e));
384
+ }
385
+ }
386
+ w.BASE = new w(n.Gx, n.Gy, t.ONE), w.ZERO = new w(t.ZERO, t.ONE, t.ZERO);
387
+ const X = n.nBitLength, D = gt(w, n.endo ? Math.ceil(X / 2) : X);
388
+ return {
389
+ CURVE: n,
390
+ ProjectivePoint: w,
391
+ normPrivateKeyToScalar: S,
392
+ weierstrassEquation: B,
393
+ isWithinCurveOrder: H
394
+ };
395
+ }
396
+ function Lt(f) {
397
+ const n = mt(f);
398
+ return yt(n, {
399
+ hash: "hash",
400
+ hmac: "function",
401
+ randomBytes: "function"
402
+ }, {
403
+ bits2int: "function",
404
+ bits2int_modN: "function",
405
+ lowS: "boolean"
406
+ }), Object.freeze({ lowS: !0, ...n });
407
+ }
408
+ function Yt(f) {
409
+ const n = Lt(f), { Fp: t, n: E } = n, R = t.BYTES + 1, Z = 2 * t.BYTES + 1;
410
+ function B(r) {
411
+ return wt(r, E);
412
+ }
413
+ function H(r) {
414
+ return xt(r, E);
415
+ }
416
+ const { ProjectivePoint: S, normPrivateKeyToScalar: L, weierstrassEquation: P, isWithinCurveOrder: rt } = zt({
417
+ ...n,
418
+ toBytes(r, o, s) {
419
+ const d = o.toAffine(), g = t.toBytes(d.x), O = st;
420
+ return tt("isCompressed", s), s ? O(Uint8Array.from([o.hasEvenY() ? 2 : 3]), g) : O(Uint8Array.from([4]), g, t.toBytes(d.y));
119
421
  },
120
- wNAFCached(t, n, i) {
121
- const o = S(t);
122
- return this.wNAF(o, this.getPrecomputes(o, t, i), n);
422
+ fromBytes(r) {
423
+ const o = r.length, s = r[0], d = r.subarray(1);
424
+ if (o === R && (s === 2 || s === 3)) {
425
+ const g = nt(d);
426
+ if (!pt(g, A, t.ORDER))
427
+ throw new Error("Point is not on curve");
428
+ const O = P(g);
429
+ let q;
430
+ try {
431
+ q = t.sqrt(O);
432
+ } catch (V) {
433
+ const z = V instanceof Error ? ": " + V.message : "";
434
+ throw new Error("Point is not on curve" + z);
435
+ }
436
+ const T = (q & A) === A;
437
+ return (s & 1) === 1 !== T && (q = t.neg(q)), { x: g, y: q };
438
+ } else if (o === Z && s === 4) {
439
+ const g = t.fromBytes(d.subarray(0, t.BYTES)), O = t.fromBytes(d.subarray(t.BYTES, 2 * t.BYTES));
440
+ return { x: g, y: O };
441
+ } else {
442
+ const g = R, O = Z;
443
+ throw new Error("invalid Point, expected length of " + g + ", or uncompressed " + O + ", got " + o);
444
+ }
445
+ }
446
+ }), w = (r) => ct(dt(r, n.nByteLength));
447
+ function X(r) {
448
+ const o = E >> A;
449
+ return r > o;
450
+ }
451
+ function D(r) {
452
+ return X(r) ? B(-r) : r;
453
+ }
454
+ const m = (r, o, s) => nt(r.slice(o, s));
455
+ class e {
456
+ constructor(o, s, d) {
457
+ this.r = o, this.s = s, this.recovery = d, this.assertValidity();
458
+ }
459
+ // pair (bytes of r, bytes of s)
460
+ static fromCompact(o) {
461
+ const s = n.nByteLength;
462
+ return o = Y("compactSignature", o, s * 2), new e(m(o, 0, s), m(o, s, 2 * s));
463
+ }
464
+ // DER encoded ECDSA signature
465
+ // https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
466
+ static fromDER(o) {
467
+ const { r: s, s: d } = U.toSig(Y("DER", o));
468
+ return new e(s, d);
469
+ }
470
+ assertValidity() {
471
+ Q("r", this.r, A, E), Q("s", this.s, A, E);
472
+ }
473
+ addRecoveryBit(o) {
474
+ return new e(this.r, this.s, o);
475
+ }
476
+ recoverPublicKey(o) {
477
+ const { r: s, s: d, recovery: g } = this, O = p(Y("msgHash", o));
478
+ if (g == null || ![0, 1, 2, 3].includes(g))
479
+ throw new Error("recovery id invalid");
480
+ const q = g === 2 || g === 3 ? s + n.n : s;
481
+ if (q >= t.ORDER)
482
+ throw new Error("recovery id 2 or 3 invalid");
483
+ const T = g & 1 ? "03" : "02", C = S.fromHex(T + w(q)), V = H(q), z = B(-O * V), M = B(d * V), K = S.BASE.multiplyAndAddUnsafe(C, z, M);
484
+ if (!K)
485
+ throw new Error("point at infinify");
486
+ return K.assertValidity(), K;
487
+ }
488
+ // Signatures should be low-s, to prevent malleability.
489
+ hasHighS() {
490
+ return X(this.s);
491
+ }
492
+ normalizeS() {
493
+ return this.hasHighS() ? new e(this.r, B(-this.s), this.recovery) : this;
494
+ }
495
+ // DER-encoded
496
+ toDERRawBytes() {
497
+ return lt(this.toDERHex());
498
+ }
499
+ toDERHex() {
500
+ return U.hexFromSig({ r: this.r, s: this.s });
501
+ }
502
+ // padded bytes of r, then padded bytes of s
503
+ toCompactRawBytes() {
504
+ return lt(this.toCompactHex());
505
+ }
506
+ toCompactHex() {
507
+ return w(this.r) + w(this.s);
508
+ }
509
+ }
510
+ const i = {
511
+ isValidPrivateKey(r) {
512
+ try {
513
+ return L(r), !0;
514
+ } catch {
515
+ return !1;
516
+ }
123
517
  },
124
- wNAFCachedUnsafe(t, n, i, o) {
125
- const a = S(t);
126
- return a === 1 ? this.unsafeLadder(t, n, o) : this.wNAFUnsafe(a, this.getPrecomputes(a, t, i), n, o);
518
+ normPrivateKeyToScalar: L,
519
+ /**
520
+ * Produces cryptographically secure private key from random of size
521
+ * (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
522
+ */
523
+ randomPrivateKey: () => {
524
+ const r = vt(n.n);
525
+ return Bt(n.randomBytes(r), n.n);
127
526
  },
128
- // We calculate precomputes for elliptic curve point multiplication
129
- // using windowed method. This specifies window size and
130
- // stores precomputed values. Usually only base point would be precomputed.
131
- setWindowSize(t, n) {
132
- A(n, r), E.set(t, n), B.delete(t);
527
+ /**
528
+ * Creates precompute table for an arbitrary EC point. Makes point "cached".
529
+ * Allows to massively speed-up `point.multiply(scalar)`.
530
+ * @returns cached point
531
+ * @example
532
+ * const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
533
+ * fast.multiply(privKey); // much faster ECDH now
534
+ */
535
+ precompute(r = 8, o = S.BASE) {
536
+ return o._setWindowSize(r), o.multiply(BigInt(3)), o;
133
537
  }
134
538
  };
135
- }
136
- function Z(e, r, t, n) {
137
- if (L(t, e), k(n, r), t.length !== n.length)
138
- throw new Error("arrays of points and scalars must have equal length");
139
- const i = e.ZERO, o = O(BigInt(t.length)), a = o > 12 ? o - 3 : o > 4 ? o - 2 : o ? 2 : 1, c = (1 << a) - 1, d = new Array(c + 1).fill(i), h = Math.floor((r.BITS - 1) / a) * a;
140
- let w = i;
141
- for (let l = h; l >= 0; l -= a) {
142
- d.fill(i);
143
- for (let s = 0; s < n.length; s++) {
144
- const f = n[s], g = Number(f >> BigInt(l) & BigInt(c));
145
- d[g] = d[g].add(t[s]);
146
- }
147
- let u = i;
148
- for (let s = d.length - 1, f = i; s > 0; s--)
149
- f = f.add(d[s]), u = u.add(f);
150
- if (w = w.add(u), l !== 0)
151
- for (let s = 0; s < a; s++)
152
- w = w.double();
153
- }
154
- return w;
155
- }
156
- function U(e) {
157
- return z(e.Fp), x(e, {
158
- n: "bigint",
159
- h: "bigint",
160
- Gx: "field",
161
- Gy: "field"
162
- }, {
163
- nBitLength: "isSafeInteger",
164
- nByteLength: "isSafeInteger"
165
- }), Object.freeze({
166
- ...F(e.n, e.nBitLength),
167
- ...e,
168
- p: e.Fp.ORDER
169
- });
539
+ function c(r, o = !0) {
540
+ return S.fromPrivateKey(r).toRawBytes(o);
541
+ }
542
+ function u(r) {
543
+ const o = at(r), s = typeof r == "string", d = (o || s) && r.length;
544
+ return o ? d === R || d === Z : s ? d === 2 * R || d === 2 * Z : r instanceof S;
545
+ }
546
+ function y(r, o, s = !0) {
547
+ if (u(r))
548
+ throw new Error("first arg must be private key");
549
+ if (!u(o))
550
+ throw new Error("second arg must be public key");
551
+ return S.fromHex(o).multiply(L(r)).toRawBytes(s);
552
+ }
553
+ const v = n.bits2int || function(r) {
554
+ if (r.length > 8192)
555
+ throw new Error("input is too large");
556
+ const o = nt(r), s = r.length * 8 - n.nBitLength;
557
+ return s > 0 ? o >> BigInt(s) : o;
558
+ }, p = n.bits2int_modN || function(r) {
559
+ return B(v(r));
560
+ }, a = Rt(n.nBitLength);
561
+ function h(r) {
562
+ return Q("num < 2^" + n.nBitLength, r, j, a), dt(r, n.nByteLength);
563
+ }
564
+ function l(r, o, s = N) {
565
+ if (["recovered", "canonical"].some((k) => k in s))
566
+ throw new Error("sign() legacy options not supported");
567
+ const { hash: d, randomBytes: g } = n;
568
+ let { lowS: O, prehash: q, extraEntropy: T } = s;
569
+ O == null && (O = !0), r = Y("msgHash", r), ft(s), q && (r = Y("prehashed msgHash", d(r)));
570
+ const C = p(r), V = L(o), z = [h(V), h(C)];
571
+ if (T != null && T !== !1) {
572
+ const k = T === !0 ? g(t.BYTES) : T;
573
+ z.push(Y("extraEntropy", k));
574
+ }
575
+ const M = st(...z), K = C;
576
+ function ot(k) {
577
+ const W = v(k);
578
+ if (!rt(W))
579
+ return;
580
+ const it = H(W), $ = S.BASE.multiply(W).toAffine(), I = B($.x);
581
+ if (I === j)
582
+ return;
583
+ const J = B(it * B(K + I * V));
584
+ if (J === j)
585
+ return;
586
+ let _ = ($.x === I ? 0 : 2) | Number($.y & A), G = J;
587
+ return O && X(J) && (G = D(J), _ ^= 1), new e(I, G, _);
588
+ }
589
+ return { seed: M, k2sig: ot };
590
+ }
591
+ const N = { lowS: n.lowS, prehash: !1 }, x = { lowS: n.lowS, prehash: !1 };
592
+ function b(r, o, s = N) {
593
+ const { seed: d, k2sig: g } = l(r, o, s), O = n;
594
+ return Ot(O.hash.outputLen, O.nByteLength, O.hmac)(d, g);
595
+ }
596
+ S.BASE._setWindowSize(8);
597
+ function F(r, o, s, d = x) {
598
+ var _;
599
+ const g = r;
600
+ o = Y("msgHash", o), s = Y("publicKey", s);
601
+ const { lowS: O, prehash: q, format: T } = d;
602
+ if (ft(d), "strict" in d)
603
+ throw new Error("options.strict was renamed to lowS");
604
+ if (T !== void 0 && T !== "compact" && T !== "der")
605
+ throw new Error("format must be compact or der");
606
+ const C = typeof g == "string" || at(g), V = !C && !T && typeof g == "object" && g !== null && typeof g.r == "bigint" && typeof g.s == "bigint";
607
+ if (!C && !V)
608
+ throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
609
+ let z, M;
610
+ try {
611
+ if (V && (z = new e(g.r, g.s)), C) {
612
+ try {
613
+ T !== "compact" && (z = e.fromDER(g));
614
+ } catch (G) {
615
+ if (!(G instanceof U.Err))
616
+ throw G;
617
+ }
618
+ !z && T !== "der" && (z = e.fromCompact(g));
619
+ }
620
+ M = S.fromHex(s);
621
+ } catch {
622
+ return !1;
623
+ }
624
+ if (!z || O && z.hasHighS())
625
+ return !1;
626
+ q && (o = n.hash(o));
627
+ const { r: K, s: ot } = z, k = p(o), W = H(ot), it = B(k * W), $ = B(K * W), I = (_ = S.BASE.multiplyAndAddUnsafe(M, it, $)) == null ? void 0 : _.toAffine();
628
+ return I ? B(I.x) === K : !1;
629
+ }
630
+ return {
631
+ CURVE: n,
632
+ getPublicKey: c,
633
+ getSharedSecret: y,
634
+ sign: b,
635
+ verify: F,
636
+ ProjectivePoint: S,
637
+ Signature: e,
638
+ utils: i
639
+ };
170
640
  }
171
641
  export {
172
- Z as pippenger,
173
- U as validateBasic,
174
- v as wNAF
642
+ U as DER,
643
+ qt as DERErr,
644
+ Yt as weierstrass,
645
+ zt as weierstrassPoints
175
646
  };