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