@gardenfi/utils 2.1.3-beta.1 → 2.1.3-beta.10

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