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