module-develop-kit 1.3.1 → 1.4.0

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 (45) hide show
  1. package/dist/commonjs-virtual-dir/browser.js +5 -2
  2. package/dist/commonjs-virtual-dir/browser2.js +2 -5
  3. package/dist/commonjs-virtual-dir/index.js +5 -3
  4. package/dist/commonjs-virtual-dir/index2.js +3 -5
  5. package/dist/commonjs-virtual-dir/index3.js +2 -5
  6. package/dist/commonjs-virtual-dir/index4.js +4 -2
  7. package/dist/commonjs-virtual-dir/index5.js +2 -2
  8. package/dist/main.d.ts +74 -28
  9. package/dist/main.js +40 -35
  10. package/dist/src/outside-call/utils.js +1 -1
  11. package/dist/src/shield/crypto/const.js +2 -2
  12. package/dist/src/shield/crypto/sm2.js +4 -4
  13. package/dist/src/shield/crypto/util.js +11 -23
  14. package/dist/src/shield/gcm-crypto/gcm.service.js +29 -0
  15. package/dist/src/shield/gcm-crypto/hex.js +18 -0
  16. package/dist/src/shield/gcm-crypto/index.js +40 -0
  17. package/dist/src/shield/gcm-crypto/init.js +61 -0
  18. package/dist/src/shield/gcm-crypto/string.js +10 -0
  19. package/dist/src/utils/is.js +4 -0
  20. package/dist/types/axios.d.ts +16 -0
  21. package/dist/vendor/@noble/ciphers/esm/_polyval.js +116 -0
  22. package/dist/vendor/@noble/ciphers/esm/utils.js +68 -0
  23. package/dist/vendor/@noble/curves/esm/abstract/curve.js +203 -0
  24. package/dist/vendor/@noble/curves/esm/abstract/modular.js +240 -0
  25. package/dist/vendor/@noble/curves/esm/abstract/utils.js +10 -0
  26. package/dist/vendor/@noble/curves/esm/abstract/weierstrass.js +791 -0
  27. package/dist/vendor/@noble/curves/esm/utils.js +154 -0
  28. package/dist/vendor/@noble/hashes/esm/crypto.js +4 -0
  29. package/dist/vendor/@noble/hashes/esm/hmac.js +45 -0
  30. package/dist/vendor/@noble/hashes/esm/utils.js +112 -0
  31. package/dist/vendor/@tokenizer/inflate/lib/ZipHandler.js +1 -1
  32. package/dist/vendor/debug/src/browser.js +1 -1
  33. package/dist/vendor/ieee754/index.js +1 -1
  34. package/dist/vendor/sdp-transform/lib/index.js +1 -1
  35. package/dist/vendor/sm-crypto-v2/dist/index.js +1004 -0
  36. package/dist/vendor/token-types/lib/index.js +1 -1
  37. package/package.json +3 -2
  38. package/dist/commonjs-virtual-dir/index7.js +0 -4
  39. package/dist/commonjs-virtual-dir/index8.js +0 -4
  40. package/dist/src/shield/crypto/index.js +0 -53
  41. package/dist/src/shield/crypto/sm4.js +0 -15
  42. package/dist/vendor/base64-js/index.js +0 -52
  43. package/dist/vendor/gm-crypt/index.js +0 -10
  44. package/dist/vendor/gm-crypt/src/crypt.js +0 -60
  45. package/dist/vendor/gm-crypt/src/sm4.js +0 -513
@@ -0,0 +1,116 @@
1
+ import { toBytes as y, u32 as x, abytes as W, createView as G, aexists as U, clean as k, aoutput as H, copyBytes as M } from "./utils.js";
2
+ const f = 16, v = /* @__PURE__ */ new Uint8Array(16), l = x(v), R = 225, V = (o, t, s, e) => {
3
+ const n = e & 1;
4
+ return {
5
+ s3: s << 31 | e >>> 1,
6
+ s2: t << 31 | s >>> 1,
7
+ s1: o << 31 | t >>> 1,
8
+ s0: o >>> 1 ^ R << 24 & -(n & 1)
9
+ // reduce % poly
10
+ };
11
+ }, c = (o) => (o >>> 0 & 255) << 24 | (o >>> 8 & 255) << 16 | (o >>> 16 & 255) << 8 | o >>> 24 & 255 | 0;
12
+ function j(o) {
13
+ o.reverse();
14
+ const t = o[15] & 1;
15
+ let s = 0;
16
+ for (let e = 0; e < o.length; e++) {
17
+ const n = o[e];
18
+ o[e] = n >>> 1 | s, s = (n & 1) << 7;
19
+ }
20
+ return o[0] ^= -t & 225, o;
21
+ }
22
+ const C = (o) => o > 64 * 1024 ? 8 : o > 1024 ? 4 : 2;
23
+ class I {
24
+ // We select bits per window adaptively based on expectedLength
25
+ constructor(t, s) {
26
+ this.blockLen = f, this.outputLen = f, this.s0 = 0, this.s1 = 0, this.s2 = 0, this.s3 = 0, this.finished = !1, t = y(t), W(t, 16);
27
+ const e = G(t);
28
+ let n = e.getUint32(0, !1), i = e.getUint32(4, !1), r = e.getUint32(8, !1), b = e.getUint32(12, !1);
29
+ const g = [];
30
+ for (let u = 0; u < 128; u++)
31
+ g.push({ s0: c(n), s1: c(i), s2: c(r), s3: c(b) }), { s0: n, s1: i, s2: r, s3: b } = V(n, i, r, b);
32
+ const h = C(s || 1024);
33
+ if (![1, 2, 4, 8].includes(h))
34
+ throw new Error("ghash: invalid window size, expected 2, 4 or 8");
35
+ this.W = h;
36
+ const B = 128 / h, A = this.windowSize = 2 ** h, a = [];
37
+ for (let u = 0; u < B; u++)
38
+ for (let w = 0; w < A; w++) {
39
+ let L = 0, d = 0, m = 0, S = 0;
40
+ for (let p = 0; p < h; p++) {
41
+ if (!(w >>> h - p - 1 & 1))
42
+ continue;
43
+ const { s0: E, s1: O, s2: P, s3: Z } = g[h * u + p];
44
+ L ^= E, d ^= O, m ^= P, S ^= Z;
45
+ }
46
+ a.push({ s0: L, s1: d, s2: m, s3: S });
47
+ }
48
+ this.t = a;
49
+ }
50
+ _updateBlock(t, s, e, n) {
51
+ t ^= this.s0, s ^= this.s1, e ^= this.s2, n ^= this.s3;
52
+ const { W: i, t: r, windowSize: b } = this;
53
+ let g = 0, h = 0, _ = 0, B = 0;
54
+ const A = (1 << i) - 1;
55
+ let a = 0;
56
+ for (const u of [t, s, e, n])
57
+ for (let w = 0; w < 4; w++) {
58
+ const L = u >>> 8 * w & 255;
59
+ for (let d = 8 / i - 1; d >= 0; d--) {
60
+ const m = L >>> i * d & A, { s0: S, s1: p, s2: z, s3: E } = r[a * b + m];
61
+ g ^= S, h ^= p, _ ^= z, B ^= E, a += 1;
62
+ }
63
+ }
64
+ this.s0 = g, this.s1 = h, this.s2 = _, this.s3 = B;
65
+ }
66
+ update(t) {
67
+ U(this), t = y(t), W(t);
68
+ const s = x(t), e = Math.floor(t.length / f), n = t.length % f;
69
+ for (let i = 0; i < e; i++)
70
+ this._updateBlock(s[i * 4 + 0], s[i * 4 + 1], s[i * 4 + 2], s[i * 4 + 3]);
71
+ return n && (v.set(t.subarray(e * f)), this._updateBlock(l[0], l[1], l[2], l[3]), k(l)), this;
72
+ }
73
+ destroy() {
74
+ const { t } = this;
75
+ for (const s of t)
76
+ s.s0 = 0, s.s1 = 0, s.s2 = 0, s.s3 = 0;
77
+ }
78
+ digestInto(t) {
79
+ U(this), H(t, this), this.finished = !0;
80
+ const { s0: s, s1: e, s2: n, s3: i } = this, r = x(t);
81
+ return r[0] = s, r[1] = e, r[2] = n, r[3] = i, t;
82
+ }
83
+ digest() {
84
+ const t = new Uint8Array(f);
85
+ return this.digestInto(t), this.destroy(), t;
86
+ }
87
+ }
88
+ class Y extends I {
89
+ constructor(t, s) {
90
+ t = y(t), W(t);
91
+ const e = j(M(t));
92
+ super(e, s), k(e);
93
+ }
94
+ update(t) {
95
+ t = y(t), U(this);
96
+ const s = x(t), e = t.length % f, n = Math.floor(t.length / f);
97
+ for (let i = 0; i < n; i++)
98
+ this._updateBlock(c(s[i * 4 + 3]), c(s[i * 4 + 2]), c(s[i * 4 + 1]), c(s[i * 4 + 0]));
99
+ return e && (v.set(t.subarray(n * f)), this._updateBlock(c(l[3]), c(l[2]), c(l[1]), c(l[0])), k(l)), this;
100
+ }
101
+ digestInto(t) {
102
+ U(this), H(t, this), this.finished = !0;
103
+ const { s0: s, s1: e, s2: n, s3: i } = this, r = x(t);
104
+ return r[0] = s, r[1] = e, r[2] = n, r[3] = i, t.reverse();
105
+ }
106
+ }
107
+ function K(o) {
108
+ const t = (e, n) => o(n, e.length).update(y(e)).digest(), s = o(new Uint8Array(16), 0);
109
+ return t.outputLen = s.outputLen, t.blockLen = s.blockLen, t.create = (e, n) => o(e, n), t;
110
+ }
111
+ const D = K((o, t) => new I(o, t));
112
+ K((o, t) => new Y(o, t));
113
+ export {
114
+ j as _toGHASHKey,
115
+ D as ghash
116
+ };
@@ -0,0 +1,68 @@
1
+ /*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) */
2
+ function i(t) {
3
+ return t instanceof Uint8Array || ArrayBuffer.isView(t) && t.constructor.name === "Uint8Array";
4
+ }
5
+ function y(t, ...e) {
6
+ if (!i(t))
7
+ throw new Error("Uint8Array expected");
8
+ if (e.length > 0 && !e.includes(t.length))
9
+ throw new Error("Uint8Array expected of length " + e + ", got length=" + t.length);
10
+ }
11
+ function l(t, e = !0) {
12
+ if (t.destroyed)
13
+ throw new Error("Hash instance has been destroyed");
14
+ if (e && t.finished)
15
+ throw new Error("Hash#digest() has already been called");
16
+ }
17
+ function p(t, e) {
18
+ y(t);
19
+ const n = e.outputLen;
20
+ if (t.length < n)
21
+ throw new Error("digestInto() expects output buffer of length at least " + n);
22
+ }
23
+ function U(t) {
24
+ return new Uint32Array(t.buffer, t.byteOffset, Math.floor(t.byteLength / 4));
25
+ }
26
+ function x(...t) {
27
+ for (let e = 0; e < t.length; e++)
28
+ t[e].fill(0);
29
+ }
30
+ function B(t) {
31
+ return new DataView(t.buffer, t.byteOffset, t.byteLength);
32
+ }
33
+ function g(t) {
34
+ if (typeof t != "string")
35
+ throw new Error("string expected");
36
+ return new Uint8Array(new TextEncoder().encode(t));
37
+ }
38
+ function d(t) {
39
+ if (typeof t == "string")
40
+ t = g(t);
41
+ else if (i(t))
42
+ t = w(t);
43
+ else
44
+ throw new Error("Uint8Array expected, got " + typeof t);
45
+ return t;
46
+ }
47
+ function A(t, e, n, r) {
48
+ if (typeof t.setBigUint64 == "function")
49
+ return t.setBigUint64(e, n, r);
50
+ const f = BigInt(32), o = BigInt(4294967295), c = Number(n >> f & o), s = Number(n & o), u = 0, h = 4;
51
+ t.setUint32(e + u, c, r), t.setUint32(e + h, s, r);
52
+ }
53
+ function w(t) {
54
+ return Uint8Array.from(t);
55
+ }
56
+ export {
57
+ y as abytes,
58
+ l as aexists,
59
+ p as aoutput,
60
+ x as clean,
61
+ w as copyBytes,
62
+ B as createView,
63
+ i as isBytes,
64
+ A as setBigUint64,
65
+ d as toBytes,
66
+ U as u32,
67
+ g as utf8ToBytes
68
+ };
@@ -0,0 +1,203 @@
1
+ import { bitLen as M, bitMask as y } from "../utils.js";
2
+ import { validateField as I, Field as z, FpInvertBatch as R } from "./modular.js";
3
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
4
+ const m = BigInt(0), b = BigInt(1);
5
+ function v(r, t) {
6
+ const e = t.negate();
7
+ return r ? e : t;
8
+ }
9
+ function D(r, t) {
10
+ const e = R(r.Fp, t.map((n) => n.Z));
11
+ return t.map((n, i) => r.fromAffine(n.toAffine(e[i])));
12
+ }
13
+ function O(r, t) {
14
+ if (!Number.isSafeInteger(r) || r <= 0 || r > t)
15
+ throw new Error("invalid window size, expected [1.." + t + "], got W=" + r);
16
+ }
17
+ function F(r, t) {
18
+ O(r, t);
19
+ const e = Math.ceil(t / r) + 1, n = 2 ** (r - 1), i = 2 ** r, o = y(r), s = BigInt(r);
20
+ return { windows: e, windowSize: n, mask: o, maxNumber: i, shiftBy: s };
21
+ }
22
+ function S(r, t, e) {
23
+ const { windowSize: n, mask: i, maxNumber: o, shiftBy: s } = e;
24
+ let a = Number(r & i), f = r >> s;
25
+ a > n && (a -= o, f += b);
26
+ const c = t * n, d = c + Math.abs(a) - 1, u = a === 0, w = a < 0, h = t % 2 !== 0;
27
+ return { nextN: f, offset: d, isZero: u, isNeg: w, isNegF: h, offsetF: c };
28
+ }
29
+ function j(r, t) {
30
+ if (!Array.isArray(r))
31
+ throw new Error("array expected");
32
+ r.forEach((e, n) => {
33
+ if (!(e instanceof t))
34
+ throw new Error("invalid point at index " + n);
35
+ });
36
+ }
37
+ function _(r, t) {
38
+ if (!Array.isArray(r))
39
+ throw new Error("array of scalars expected");
40
+ r.forEach((e, n) => {
41
+ if (!t.isValid(e))
42
+ throw new Error("invalid scalar at index " + n);
43
+ });
44
+ }
45
+ const E = /* @__PURE__ */ new WeakMap(), B = /* @__PURE__ */ new WeakMap();
46
+ function N(r) {
47
+ return B.get(r) || 1;
48
+ }
49
+ function Z(r) {
50
+ if (r !== m)
51
+ throw new Error("invalid wNAF");
52
+ }
53
+ class G {
54
+ // Parametrized with a given Point class (not individual point)
55
+ constructor(t, e) {
56
+ this.BASE = t.BASE, this.ZERO = t.ZERO, this.Fn = t.Fn, this.bits = e;
57
+ }
58
+ // non-const time multiplication ladder
59
+ _unsafeLadder(t, e, n = this.ZERO) {
60
+ let i = t;
61
+ for (; e > m; )
62
+ e & b && (n = n.add(i)), i = i.double(), e >>= b;
63
+ return n;
64
+ }
65
+ /**
66
+ * Creates a wNAF precomputation window. Used for caching.
67
+ * Default window size is set by `utils.precompute()` and is equal to 8.
68
+ * Number of precomputed points depends on the curve size:
69
+ * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
70
+ * - 𝑊 is the window size
71
+ * - 𝑛 is the bitlength of the curve order.
72
+ * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
73
+ * @param point Point instance
74
+ * @param W window size
75
+ * @returns precomputed point tables flattened to a single array
76
+ */
77
+ precomputeWindow(t, e) {
78
+ const { windows: n, windowSize: i } = F(e, this.bits), o = [];
79
+ let s = t, a = s;
80
+ for (let f = 0; f < n; f++) {
81
+ a = s, o.push(a);
82
+ for (let c = 1; c < i; c++)
83
+ a = a.add(s), o.push(a);
84
+ s = a.double();
85
+ }
86
+ return o;
87
+ }
88
+ /**
89
+ * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
90
+ * More compact implementation:
91
+ * https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
92
+ * @returns real and fake (for const-time) points
93
+ */
94
+ wNAF(t, e, n) {
95
+ if (!this.Fn.isValid(n))
96
+ throw new Error("invalid scalar");
97
+ let i = this.ZERO, o = this.BASE;
98
+ const s = F(t, this.bits);
99
+ for (let a = 0; a < s.windows; a++) {
100
+ const { nextN: f, offset: c, isZero: d, isNeg: u, isNegF: w, offsetF: h } = S(n, a, s);
101
+ n = f, d ? o = o.add(v(w, e[h])) : i = i.add(v(u, e[c]));
102
+ }
103
+ return Z(n), { p: i, f: o };
104
+ }
105
+ /**
106
+ * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
107
+ * @param acc accumulator point to add result of multiplication
108
+ * @returns point
109
+ */
110
+ wNAFUnsafe(t, e, n, i = this.ZERO) {
111
+ const o = F(t, this.bits);
112
+ for (let s = 0; s < o.windows && n !== m; s++) {
113
+ const { nextN: a, offset: f, isZero: c, isNeg: d } = S(n, s, o);
114
+ if (n = a, !c) {
115
+ const u = e[f];
116
+ i = i.add(d ? u.negate() : u);
117
+ }
118
+ }
119
+ return Z(n), i;
120
+ }
121
+ getPrecomputes(t, e, n) {
122
+ let i = E.get(e);
123
+ return i || (i = this.precomputeWindow(e, t), t !== 1 && (typeof n == "function" && (i = n(i)), E.set(e, i))), i;
124
+ }
125
+ cached(t, e, n) {
126
+ const i = N(t);
127
+ return this.wNAF(i, this.getPrecomputes(i, t, n), e);
128
+ }
129
+ unsafe(t, e, n, i) {
130
+ const o = N(t);
131
+ return o === 1 ? this._unsafeLadder(t, e, i) : this.wNAFUnsafe(o, this.getPrecomputes(o, t, n), e, i);
132
+ }
133
+ // We calculate precomputes for elliptic curve point multiplication
134
+ // using windowed method. This specifies window size and
135
+ // stores precomputed values. Usually only base point would be precomputed.
136
+ createCache(t, e) {
137
+ O(e, this.bits), B.set(t, e), E.delete(t);
138
+ }
139
+ hasCache(t) {
140
+ return N(t) !== 1;
141
+ }
142
+ }
143
+ function q(r, t, e, n) {
144
+ let i = t, o = r.ZERO, s = r.ZERO;
145
+ for (; e > m || n > m; )
146
+ e & b && (o = o.add(i)), n & b && (s = s.add(i)), i = i.double(), e >>= b, n >>= b;
147
+ return { p1: o, p2: s };
148
+ }
149
+ function K(r, t, e, n) {
150
+ j(e, r), _(n, t);
151
+ const i = e.length, o = n.length;
152
+ if (i !== o)
153
+ throw new Error("arrays of points and scalars must have equal length");
154
+ const s = r.ZERO, a = M(BigInt(i));
155
+ let f = 1;
156
+ a > 12 ? f = a - 3 : a > 4 ? f = a - 2 : a > 0 && (f = 2);
157
+ const c = y(f), d = new Array(Number(c) + 1).fill(s), u = Math.floor((t.BITS - 1) / f) * f;
158
+ let w = s;
159
+ for (let h = u; h >= 0; h -= f) {
160
+ d.fill(s);
161
+ for (let l = 0; l < o; l++) {
162
+ const p = n[l], A = Number(p >> BigInt(h) & c);
163
+ d[A] = d[A].add(e[l]);
164
+ }
165
+ let g = s;
166
+ for (let l = d.length - 1, p = s; l > 0; l--)
167
+ p = p.add(d[l]), g = g.add(p);
168
+ if (w = w.add(g), h !== 0)
169
+ for (let l = 0; l < f; l++)
170
+ w = w.double();
171
+ }
172
+ return w;
173
+ }
174
+ function x(r, t, e) {
175
+ if (t) {
176
+ if (t.ORDER !== r)
177
+ throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
178
+ return I(t), t;
179
+ } else
180
+ return z(r, { isLE: e });
181
+ }
182
+ function P(r, t, e = {}, n) {
183
+ if (n === void 0 && (n = r === "edwards"), !t || typeof t != "object")
184
+ throw new Error(`expected valid ${r} CURVE object`);
185
+ for (const f of ["p", "n", "h"]) {
186
+ const c = t[f];
187
+ if (!(typeof c == "bigint" && c > m))
188
+ throw new Error(`CURVE.${f} must be positive bigint`);
189
+ }
190
+ const i = x(t.p, e.Fp, n), o = x(t.n, e.Fn, n), a = ["Gx", "Gy", "a", "b"];
191
+ for (const f of a)
192
+ if (!i.isValid(t[f]))
193
+ throw new Error(`CURVE.${f} must be valid field element of CURVE.Fp`);
194
+ return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: i, Fn: o };
195
+ }
196
+ export {
197
+ P as _createCurveFields,
198
+ q as mulEndoUnsafe,
199
+ v as negateCt,
200
+ D as normalizeZ,
201
+ K as pippenger,
202
+ G as wNAF
203
+ };
@@ -0,0 +1,240 @@
1
+ import { numberToBytesLE as _, numberToBytesBE as S, bitMask as K, bytesToNumberLE as x, bytesToNumberBE as T, _validateObject as Q } from "../utils.js";
2
+ import { anumber as Y } from "../../../hashes/esm/utils.js";
3
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
4
+ const m = BigInt(0), g = BigInt(1), B = /* @__PURE__ */ BigInt(2), L = /* @__PURE__ */ BigInt(3), M = /* @__PURE__ */ BigInt(4), A = /* @__PURE__ */ BigInt(5), U = /* @__PURE__ */ BigInt(7), V = /* @__PURE__ */ BigInt(8), k = /* @__PURE__ */ BigInt(9), Z = /* @__PURE__ */ BigInt(16);
5
+ function q(t, n) {
6
+ const r = t % n;
7
+ return r >= m ? r : n + r;
8
+ }
9
+ function y(t, n) {
10
+ if (t === m)
11
+ throw new Error("invert: expected non-zero number");
12
+ if (n <= m)
13
+ throw new Error("invert: expected positive modulus, got " + n);
14
+ let r = q(t, n), o = n, l = m, u = g;
15
+ for (; r !== m; ) {
16
+ const i = o / r, s = o % r, f = l - u * i;
17
+ o = r, r = s, l = u, u = f;
18
+ }
19
+ if (o !== g)
20
+ throw new Error("invert: does not exist");
21
+ return q(l, n);
22
+ }
23
+ function b(t, n, r) {
24
+ if (!t.eql(t.sqr(n), r))
25
+ throw new Error("Cannot find square root");
26
+ }
27
+ function j(t, n) {
28
+ const r = (t.ORDER + g) / M, o = t.pow(n, r);
29
+ return b(t, o, n), o;
30
+ }
31
+ function G(t, n) {
32
+ const r = (t.ORDER - A) / V, o = t.mul(n, B), l = t.pow(o, r), u = t.mul(n, l), d = t.mul(t.mul(u, B), l), i = t.mul(u, t.sub(d, t.ONE));
33
+ return b(t, i, n), i;
34
+ }
35
+ function J(t) {
36
+ const n = C(t), r = z(t), o = r(n, n.neg(n.ONE)), l = r(n, o), u = r(n, n.neg(o)), d = (t + U) / Z;
37
+ return (i, s) => {
38
+ let f = i.pow(s, d), w = i.mul(f, o);
39
+ const a = i.mul(f, l), e = i.mul(f, u), c = i.eql(i.sqr(w), s), h = i.eql(i.sqr(a), s);
40
+ f = i.cmov(f, w, c), w = i.cmov(e, a, h);
41
+ const v = i.eql(i.sqr(w), s), E = i.cmov(f, w, v);
42
+ return b(i, E, s), E;
43
+ };
44
+ }
45
+ function z(t) {
46
+ if (t < L)
47
+ throw new Error("sqrt is not defined for small field");
48
+ let n = t - g, r = 0;
49
+ for (; n % B === m; )
50
+ n /= B, r++;
51
+ let o = B;
52
+ const l = C(t);
53
+ for (; I(l, o) === 1; )
54
+ if (o++ > 1e3)
55
+ throw new Error("Cannot find square root: probably non-prime P");
56
+ if (r === 1)
57
+ return j;
58
+ let u = l.pow(o, n);
59
+ const d = (n + g) / B;
60
+ return function(s, f) {
61
+ if (s.is0(f))
62
+ return f;
63
+ if (I(s, f) !== 1)
64
+ throw new Error("Cannot find square root");
65
+ let w = r, a = s.mul(s.ONE, u), e = s.pow(f, n), c = s.pow(f, d);
66
+ for (; !s.eql(e, s.ONE); ) {
67
+ if (s.is0(e))
68
+ return s.ZERO;
69
+ let h = 1, v = s.sqr(e);
70
+ for (; !s.eql(v, s.ONE); )
71
+ if (h++, v = s.sqr(v), h === w)
72
+ throw new Error("Cannot find square root");
73
+ const E = g << BigInt(w - h - 1), N = s.pow(a, E);
74
+ w = h, a = s.sqr(N), e = s.mul(e, a), c = s.mul(c, N);
75
+ }
76
+ return c;
77
+ };
78
+ }
79
+ function O(t) {
80
+ return t % M === L ? j : t % V === A ? G : t % Z === k ? J(t) : z(t);
81
+ }
82
+ const W = [
83
+ "create",
84
+ "isValid",
85
+ "is0",
86
+ "neg",
87
+ "inv",
88
+ "sqrt",
89
+ "sqr",
90
+ "eql",
91
+ "add",
92
+ "sub",
93
+ "mul",
94
+ "pow",
95
+ "div",
96
+ "addN",
97
+ "subN",
98
+ "mulN",
99
+ "sqrN"
100
+ ];
101
+ function F(t) {
102
+ const n = {
103
+ ORDER: "bigint",
104
+ MASK: "bigint",
105
+ BYTES: "number",
106
+ BITS: "number"
107
+ }, r = W.reduce((o, l) => (o[l] = "function", o), n);
108
+ return Q(t, r), t;
109
+ }
110
+ function X(t, n, r) {
111
+ if (r < m)
112
+ throw new Error("invalid exponent, negatives unsupported");
113
+ if (r === m)
114
+ return t.ONE;
115
+ if (r === g)
116
+ return n;
117
+ let o = t.ONE, l = n;
118
+ for (; r > m; )
119
+ r & g && (o = t.mul(o, l)), l = t.sqr(l), r >>= g;
120
+ return o;
121
+ }
122
+ function $(t, n, r = !1) {
123
+ const o = new Array(n.length).fill(r ? t.ZERO : void 0), l = n.reduce((d, i, s) => t.is0(i) ? d : (o[s] = d, t.mul(d, i)), t.ONE), u = t.inv(l);
124
+ return n.reduceRight((d, i, s) => t.is0(i) ? d : (o[s] = t.mul(d, o[s]), t.mul(d, i)), u), o;
125
+ }
126
+ function I(t, n) {
127
+ const r = (t.ORDER - g) / B, o = t.pow(n, r), l = t.eql(o, t.ONE), u = t.eql(o, t.ZERO), d = t.eql(o, t.neg(t.ONE));
128
+ if (!l && !u && !d)
129
+ throw new Error("invalid Legendre symbol result");
130
+ return l ? 1 : u ? 0 : -1;
131
+ }
132
+ function P(t, n) {
133
+ n !== void 0 && Y(n);
134
+ const r = n !== void 0 ? n : t.toString(2).length, o = Math.ceil(r / 8);
135
+ return { nBitLength: r, nByteLength: o };
136
+ }
137
+ function C(t, n, r = !1, o = {}) {
138
+ if (t <= m)
139
+ throw new Error("invalid field: expected ORDER > 0, got " + t);
140
+ let l, u, d = !1, i;
141
+ if (typeof n == "object" && n != null) {
142
+ if (o.sqrt || r)
143
+ throw new Error("cannot specify opts in two arguments");
144
+ const e = n;
145
+ e.BITS && (l = e.BITS), e.sqrt && (u = e.sqrt), typeof e.isLE == "boolean" && (r = e.isLE), typeof e.modFromBytes == "boolean" && (d = e.modFromBytes), i = e.allowedLengths;
146
+ } else
147
+ typeof n == "number" && (l = n), o.sqrt && (u = o.sqrt);
148
+ const { nBitLength: s, nByteLength: f } = P(t, l);
149
+ if (f > 2048)
150
+ throw new Error("invalid field: expected ORDER of <= 2048 bytes");
151
+ let w;
152
+ const a = Object.freeze({
153
+ ORDER: t,
154
+ isLE: r,
155
+ BITS: s,
156
+ BYTES: f,
157
+ MASK: K(s),
158
+ ZERO: m,
159
+ ONE: g,
160
+ allowedLengths: i,
161
+ create: (e) => q(e, t),
162
+ isValid: (e) => {
163
+ if (typeof e != "bigint")
164
+ throw new Error("invalid field element: expected bigint, got " + typeof e);
165
+ return m <= e && e < t;
166
+ },
167
+ is0: (e) => e === m,
168
+ // is valid and invertible
169
+ isValidNot0: (e) => !a.is0(e) && a.isValid(e),
170
+ isOdd: (e) => (e & g) === g,
171
+ neg: (e) => q(-e, t),
172
+ eql: (e, c) => e === c,
173
+ sqr: (e) => q(e * e, t),
174
+ add: (e, c) => q(e + c, t),
175
+ sub: (e, c) => q(e - c, t),
176
+ mul: (e, c) => q(e * c, t),
177
+ pow: (e, c) => X(a, e, c),
178
+ div: (e, c) => q(e * y(c, t), t),
179
+ // Same as above, but doesn't normalize
180
+ sqrN: (e) => e * e,
181
+ addN: (e, c) => e + c,
182
+ subN: (e, c) => e - c,
183
+ mulN: (e, c) => e * c,
184
+ inv: (e) => y(e, t),
185
+ sqrt: u || ((e) => (w || (w = O(t)), w(a, e))),
186
+ toBytes: (e) => r ? _(e, f) : S(e, f),
187
+ fromBytes: (e, c = !0) => {
188
+ if (i) {
189
+ if (!i.includes(e.length) || e.length > f)
190
+ throw new Error("Field.fromBytes: expected " + i + " bytes, got " + e.length);
191
+ const v = new Uint8Array(f);
192
+ v.set(e, r ? 0 : v.length - e.length), e = v;
193
+ }
194
+ if (e.length !== f)
195
+ throw new Error("Field.fromBytes: expected " + f + " bytes, got " + e.length);
196
+ let h = r ? x(e) : T(e);
197
+ if (d && (h = q(h, t)), !c && !a.isValid(h))
198
+ throw new Error("invalid field element: outside of range 0..ORDER");
199
+ return h;
200
+ },
201
+ // TODO: we don't need it here, move out to separate fn
202
+ invertBatch: (e) => $(a, e),
203
+ // We can't move this out because Fp6, Fp12 implement it
204
+ // and it's unclear what to return in there.
205
+ cmov: (e, c, h) => h ? c : e
206
+ });
207
+ return Object.freeze(a);
208
+ }
209
+ function H(t) {
210
+ if (typeof t != "bigint")
211
+ throw new Error("field order must be bigint");
212
+ const n = t.toString(2).length;
213
+ return Math.ceil(n / 8);
214
+ }
215
+ function D(t) {
216
+ const n = H(t);
217
+ return n + Math.ceil(n / 2);
218
+ }
219
+ function tt(t, n, r = !1) {
220
+ const o = t.length, l = H(n), u = D(n);
221
+ if (o < 16 || o < u || o > 1024)
222
+ throw new Error("expected " + u + "-1024 bytes of input, got " + o);
223
+ const d = r ? x(t) : T(t), i = q(d, n - g) + g;
224
+ return r ? _(i, l) : S(i, l);
225
+ }
226
+ export {
227
+ C as Field,
228
+ $ as FpInvertBatch,
229
+ I as FpLegendre,
230
+ X as FpPow,
231
+ O as FpSqrt,
232
+ H as getFieldBytesLength,
233
+ D as getMinHashLength,
234
+ y as invert,
235
+ tt as mapHashToField,
236
+ q as mod,
237
+ P as nLength,
238
+ z as tonelliShanks,
239
+ F as validateField
240
+ };
@@ -0,0 +1,10 @@
1
+ import { hexToNumber as e, numberToBytesBE as o, numberToHexUnpadded as t } from "../utils.js";
2
+ import { concatBytes as s, bytesToHex as n } from "../../../hashes/esm/utils.js";
3
+ const b = n, T = s, a = t, c = e, x = o;
4
+ export {
5
+ b as bytesToHex,
6
+ T as concatBytes,
7
+ c as hexToNumber,
8
+ x as numberToBytesBE,
9
+ a as numberToHexUnpadded
10
+ };