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

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 (175) hide show
  1. package/dist/index100.cjs +1 -1
  2. package/dist/index100.js +5 -5
  3. package/dist/index101.cjs +2 -1
  4. package/dist/index101.js +3 -11
  5. package/dist/index102.cjs +1 -1
  6. package/dist/index102.js +16 -18
  7. package/dist/index103.cjs +1 -1
  8. package/dist/index103.js +643 -6
  9. package/dist/index104.cjs +1 -1
  10. package/dist/index104.js +173 -5
  11. package/dist/index18.cjs +1 -1
  12. package/dist/index18.js +2 -2
  13. package/dist/index19.cjs +1 -1
  14. package/dist/index19.js +9 -9
  15. package/dist/index20.cjs +1 -1
  16. package/dist/index20.js +1 -1
  17. package/dist/index21.cjs +1 -1
  18. package/dist/index21.js +3 -3
  19. package/dist/index22.cjs +1 -1
  20. package/dist/index22.js +1 -1
  21. package/dist/index23.cjs +2 -1
  22. package/dist/index23.js +49 -40
  23. package/dist/index24.cjs +1 -1
  24. package/dist/index24.js +24 -45
  25. package/dist/index25.cjs +1 -1
  26. package/dist/index25.js +41 -25
  27. package/dist/index26.cjs +1 -1
  28. package/dist/index26.js +45 -6
  29. package/dist/index27.cjs +1 -1
  30. package/dist/index27.js +24 -12
  31. package/dist/index28.cjs +1 -1
  32. package/dist/index28.js +6 -15
  33. package/dist/index29.cjs +1 -1
  34. package/dist/index29.js +13 -5
  35. package/dist/index30.cjs +1 -1
  36. package/dist/index30.js +14 -12
  37. package/dist/index31.cjs +1 -1
  38. package/dist/index31.js +5 -10
  39. package/dist/index32.cjs +1 -1
  40. package/dist/index32.js +13 -12
  41. package/dist/index33.cjs +1 -1
  42. package/dist/index33.js +10 -11
  43. package/dist/index34.cjs +1 -1
  44. package/dist/index34.js +12 -13
  45. package/dist/index35.cjs +1 -1
  46. package/dist/index35.js +11 -23
  47. package/dist/index36.cjs +1 -1
  48. package/dist/index36.js +13 -55
  49. package/dist/index37.cjs +1 -1
  50. package/dist/index37.js +23 -16
  51. package/dist/index38.cjs +1 -1
  52. package/dist/index38.js +55 -5
  53. package/dist/index39.cjs +1 -1
  54. package/dist/index39.js +16 -8
  55. package/dist/index40.cjs +1 -1
  56. package/dist/index40.js +5 -171
  57. package/dist/index41.cjs +1 -1
  58. package/dist/index41.js +8 -103
  59. package/dist/index42.cjs +1 -1
  60. package/dist/index42.js +170 -20
  61. package/dist/index43.cjs +1 -2
  62. package/dist/index43.js +97 -44
  63. package/dist/index44.cjs +1 -1
  64. package/dist/index44.js +21 -24
  65. package/dist/index45.cjs +1 -1
  66. package/dist/index45.js +10 -38
  67. package/dist/index46.cjs +1 -1
  68. package/dist/index46.js +32 -32
  69. package/dist/index47.cjs +1 -1
  70. package/dist/index47.js +6 -69
  71. package/dist/index48.cjs +2 -1
  72. package/dist/index48.js +57 -110
  73. package/dist/index49.cjs +1 -1
  74. package/dist/index49.js +2 -68
  75. package/dist/index50.cjs +1 -1
  76. package/dist/index50.js +7 -2
  77. package/dist/index51.cjs +1 -1
  78. package/dist/index51.js +4 -16
  79. package/dist/index52.cjs +1 -1
  80. package/dist/index52.js +10 -222
  81. package/dist/index53.cjs +1 -1
  82. package/dist/index53.js +7 -231
  83. package/dist/index54.cjs +5 -1
  84. package/dist/index54.js +53 -9
  85. package/dist/index55.cjs +2 -1
  86. package/dist/index55.js +30 -30
  87. package/dist/index56.cjs +1 -2
  88. package/dist/index56.js +5 -59
  89. package/dist/index57.cjs +1 -1
  90. package/dist/index57.js +2 -22
  91. package/dist/index58.cjs +1 -1
  92. package/dist/index58.js +18 -101
  93. package/dist/index59.cjs +1 -1
  94. package/dist/index59.js +8 -3
  95. package/dist/index60.cjs +1 -1
  96. package/dist/index60.js +9 -16
  97. package/dist/index61.cjs +1 -1
  98. package/dist/index61.js +11 -8
  99. package/dist/index62.cjs +1 -1
  100. package/dist/index62.js +6 -10
  101. package/dist/index63.cjs +1 -1
  102. package/dist/index63.js +13 -11
  103. package/dist/index64.cjs +1 -1
  104. package/dist/index64.js +31 -12
  105. package/dist/index65.cjs +1 -1
  106. package/dist/index65.js +14 -18
  107. package/dist/index66.cjs +1 -1
  108. package/dist/index66.js +64 -6
  109. package/dist/index67.cjs +1 -1
  110. package/dist/index67.js +25 -40
  111. package/dist/index68.cjs +1 -1
  112. package/dist/index68.js +3 -79
  113. package/dist/index69.cjs +1 -1
  114. package/dist/index69.js +3 -14
  115. package/dist/index70.cjs +1 -1
  116. package/dist/index70.js +8 -18
  117. package/dist/index71.cjs +1 -1
  118. package/dist/index71.js +191 -8
  119. package/dist/index72.cjs +1 -1
  120. package/dist/index72.js +17 -3
  121. package/dist/index73.cjs +1 -1
  122. package/dist/index73.js +41 -192
  123. package/dist/index74.cjs +1 -1
  124. package/dist/index74.js +32 -62
  125. package/dist/index75.cjs +1 -1
  126. package/dist/index75.js +70 -2
  127. package/dist/index76.cjs +1 -1
  128. package/dist/index76.js +111 -5
  129. package/dist/index77.cjs +1 -1
  130. package/dist/index77.js +67 -11
  131. package/dist/index78.cjs +1 -1
  132. package/dist/index78.js +2 -8
  133. package/dist/index79.cjs +1 -5
  134. package/dist/index79.js +15 -55
  135. package/dist/index80.cjs +1 -1
  136. package/dist/index80.js +224 -2
  137. package/dist/index81.cjs +1 -2
  138. package/dist/index81.js +228 -31
  139. package/dist/index82.cjs +1 -1
  140. package/dist/index82.js +42 -5
  141. package/dist/index83.cjs +1 -1
  142. package/dist/index83.js +104 -6
  143. package/dist/index84.cjs +1 -1
  144. package/dist/index84.js +16 -31
  145. package/dist/index85.cjs +1 -1
  146. package/dist/index85.js +75 -24
  147. package/dist/index86.cjs +1 -1
  148. package/dist/index86.js +19 -3
  149. package/dist/index87.cjs +1 -1
  150. package/dist/index87.js +8 -11
  151. package/dist/index88.cjs +1 -1
  152. package/dist/index88.js +11 -19
  153. package/dist/index89.cjs +1 -1
  154. package/dist/index89.js +19 -8
  155. package/dist/index90.cjs +1 -1
  156. package/dist/index90.js +10 -16
  157. package/dist/index91.cjs +1 -1
  158. package/dist/index91.js +33 -638
  159. package/dist/index92.cjs +1 -1
  160. package/dist/index92.js +2 -173
  161. package/dist/index93.cjs +1 -1
  162. package/dist/index93.js +5 -13
  163. package/dist/index94.cjs +1 -1
  164. package/dist/index94.js +10 -127
  165. package/dist/index95.cjs +1 -2
  166. package/dist/index95.js +128 -3
  167. package/dist/index96.cjs +1 -1
  168. package/dist/index96.js +7 -2
  169. package/dist/index97.cjs +1 -1
  170. package/dist/index97.js +2 -2
  171. package/dist/index98.cjs +1 -1
  172. package/dist/index98.js +2 -2
  173. package/dist/index99.cjs +1 -1
  174. package/dist/index99.js +17 -35
  175. package/package.json +1 -1
package/dist/index91.js CHANGED
@@ -1,646 +1,41 @@
1
- import { validateBasic as mt, wNAF as gt, pippenger as Et } from "./index92.js";
2
- import { Field as bt, mod as wt, getMinHashLength as vt, mapHashToField as Bt, invert as xt } from "./index52.js";
3
- import * as St from "./index53.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 "./index53.js";
5
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- function ft(f) {
7
- f.lowS !== void 0 && tt("lowS", f.lowS), f.prehash !== void 0 && tt("prehash", f.prehash);
8
- }
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"
1
+ import { SliceOffsetOutOfBoundsError as t } from "./index90.js";
2
+ import { isHex as p } from "./index69.js";
3
+ import { size as r } from "./index51.js";
4
+ function w(e, f, i, { strict: o } = {}) {
5
+ return p(e, { strict: !1 }) ? u(e, f, i, {
6
+ strict: o
7
+ }) : m(e, f, i, {
8
+ strict: o
22
9
  });
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 });
31
10
  }
32
- const { bytesToNumberBE: Tt, hexToBytes: Zt } = St;
33
- class qt extends Error {
34
- constructor(n = "") {
35
- super(n);
36
- }
11
+ function s(e, f) {
12
+ if (typeof f == "number" && f > 0 && f > r(e) - 1)
13
+ throw new t({
14
+ offset: f,
15
+ position: "start",
16
+ size: r(e)
17
+ });
37
18
  }
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;
54
- },
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;
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
- }
222
- /**
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.
227
- */
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
- }
232
- /**
233
- * Converts hash string or Uint8Array to Point.
234
- * @param hex short/long ECDSA hex
235
- */
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
- }
262
- /**
263
- * Compare one point to another.
264
- */
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;
348
- }
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
- };
19
+ function c(e, f, i) {
20
+ if (typeof f == "number" && typeof i == "number" && r(e) !== i - f)
21
+ throw new t({
22
+ offset: i,
23
+ position: "end",
24
+ size: r(e)
25
+ });
395
26
  }
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 });
27
+ function m(e, f, i, { strict: o } = {}) {
28
+ s(e, f);
29
+ const n = e.slice(f, i);
30
+ return o && c(n, f, i), n;
407
31
  }
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));
421
- },
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
- }
517
- },
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);
526
- },
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;
537
- }
538
- };
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
- };
32
+ function u(e, f, i, { strict: o } = {}) {
33
+ s(e, f);
34
+ const n = `0x${e.replace("0x", "").slice((f ?? 0) * 2, (i ?? e.length) * 2)}`;
35
+ return o && c(n, f, i), n;
640
36
  }
641
37
  export {
642
- U as DER,
643
- qt as DERErr,
644
- Yt as weierstrass,
645
- zt as weierstrassPoints
38
+ w as slice,
39
+ m as sliceBytes,
40
+ u as sliceHex
646
41
  };
package/dist/index92.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A=require("./index52.cjs"),y=require("./index53.cjs");/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const I=BigInt(0),p=BigInt(1);function m(e,r){const t=r.negate();return e?t:r}function E(e,r){if(!Number.isSafeInteger(e)||e<=0||e>r)throw new Error("invalid window size, expected [1.."+r+"], got W="+e)}function b(e,r){E(e,r);const t=Math.ceil(r/e)+1,n=2**(e-1);return{windows:t,windowSize:n}}function O(e,r){if(!Array.isArray(e))throw new Error("array expected");e.forEach((t,n)=>{if(!(t instanceof r))throw new Error("invalid point at index "+n)})}function x(e,r){if(!Array.isArray(e))throw new Error("array of scalars expected");e.forEach((t,n)=>{if(!r.isValid(t))throw new Error("invalid scalar at index "+n)})}const B=new WeakMap,N=new WeakMap;function S(e){return N.get(e)||1}function L(e,r){return{constTimeNegate:m,hasPrecomputes(t){return S(t)!==1},unsafeLadder(t,n,o=e.ZERO){let i=t;for(;n>I;)n&p&&(o=o.add(i)),i=i.double(),n>>=p;return o},precomputeWindow(t,n){const{windows:o,windowSize:i}=b(n,r),a=[];let c=t,d=c;for(let g=0;g<o;g++){d=c,a.push(d);for(let l=1;l<i;l++)d=d.add(c),a.push(d);c=d.double()}return a},wNAF(t,n,o){const{windows:i,windowSize:a}=b(t,r);let c=e.ZERO,d=e.BASE;const g=BigInt(2**t-1),l=2**t,w=BigInt(t);for(let u=0;u<i;u++){const s=u*a;let f=Number(o&g);o>>=w,f>a&&(f-=l,o+=p);const h=s,M=s+Math.abs(f)-1,F=u%2!==0,z=f<0;f===0?d=d.add(m(F,n[h])):c=c.add(m(z,n[M]))}return{p:c,f:d}},wNAFUnsafe(t,n,o,i=e.ZERO){const{windows:a,windowSize:c}=b(t,r),d=BigInt(2**t-1),g=2**t,l=BigInt(t);for(let w=0;w<a;w++){const u=w*c;if(o===I)break;let s=Number(o&d);if(o>>=l,s>c&&(s-=g,o+=p),s===0)continue;let f=n[u+Math.abs(s)-1];s<0&&(f=f.negate()),i=i.add(f)}return i},getPrecomputes(t,n,o){let i=B.get(n);return i||(i=this.precomputeWindow(n,t),t!==1&&B.set(n,o(i))),i},wNAFCached(t,n,o){const i=S(t);return this.wNAF(i,this.getPrecomputes(i,t,o),n)},wNAFCachedUnsafe(t,n,o,i){const a=S(t);return a===1?this.unsafeLadder(t,n,i):this.wNAFUnsafe(a,this.getPrecomputes(a,t,o),n,i)},setWindowSize(t,n){E(n,r),N.set(t,n),B.delete(t)}}}function j(e,r,t,n){if(O(t,e),x(n,r),t.length!==n.length)throw new Error("arrays of points and scalars must have equal length");const o=e.ZERO,i=y.bitLen(BigInt(t.length)),a=i>12?i-3:i>4?i-2:i?2:1,c=(1<<a)-1,d=new Array(c+1).fill(o),g=Math.floor((r.BITS-1)/a)*a;let l=o;for(let w=g;w>=0;w-=a){d.fill(o);for(let s=0;s<n.length;s++){const f=n[s],h=Number(f>>BigInt(w)&BigInt(c));d[h]=d[h].add(t[s])}let u=o;for(let s=d.length-1,f=o;s>0;s--)f=f.add(d[s]),u=u.add(f);if(l=l.add(u),w!==0)for(let s=0;s<a;s++)l=l.double()}return l}function k(e){return A.validateField(e.Fp),y.validateObject(e,{n:"bigint",h:"bigint",Gx:"field",Gy:"field"},{nBitLength:"isSafeInteger",nByteLength:"isSafeInteger"}),Object.freeze({...A.nLength(e.n,e.nBitLength),...e,p:e.Fp.ORDER})}exports.pippenger=j;exports.validateBasic=k;exports.wNAF=L;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=2n**256n-1n;exports.maxUint256=t;