@btc-vision/transaction 1.7.22 → 1.7.24
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/.babelrc +10 -1
- package/README.md +1 -1
- package/browser/_version.d.ts +1 -1
- package/browser/bip39.js +204 -0
- package/browser/bitcoin-utils.js +3172 -0
- package/browser/btc-vision-bip32.js +805 -0
- package/browser/btc-vision-bitcoin.js +4179 -0
- package/browser/btc-vision-logger.js +273 -0
- package/browser/btc-vision-post-quantum.js +542 -0
- package/browser/chain/ChainData.d.ts +1 -1
- package/browser/crypto/crypto.d.ts +1 -1
- package/browser/generators/AddressGenerator.d.ts +1 -1
- package/browser/generators/Generator.d.ts +1 -1
- package/browser/generators/MLDSAData.d.ts +1 -1
- package/browser/generators/builders/CalldataGenerator.d.ts +1 -1
- package/browser/generators/builders/CustomGenerator.d.ts +1 -1
- package/browser/generators/builders/DeploymentGenerator.d.ts +1 -1
- package/browser/generators/builders/HashCommitmentGenerator.d.ts +1 -1
- package/browser/generators/builders/LegacyCalldataGenerator.d.ts +1 -1
- package/browser/generators/builders/P2WDAGenerator.d.ts +1 -1
- package/browser/index.js +10780 -2
- package/browser/keypair/Address.d.ts +2 -2
- package/browser/keypair/AddressVerificator.d.ts +2 -2
- package/browser/keypair/EcKeyPair.d.ts +2 -2
- package/browser/keypair/MessageSigner.d.ts +2 -2
- package/browser/keypair/Wallet.d.ts +2 -2
- package/browser/metadata/ContractBaseMetadata.d.ts +1 -1
- package/browser/mnemonic/Mnemonic.d.ts +2 -2
- package/browser/noble-curves.js +3316 -0
- package/browser/noble-hashes.js +1608 -0
- package/browser/opnet.d.ts +10 -2
- package/browser/p2wda/P2WDADetector.d.ts +2 -2
- package/browser/polyfills.js +4590 -0
- package/browser/scure-base.js +410 -0
- package/browser/signer/AddressRotation.d.ts +1 -1
- package/browser/signer/SignerUtils.d.ts +1 -1
- package/browser/signer/TweakedSigner.d.ts +1 -1
- package/browser/transaction/TransactionFactory.d.ts +1 -1
- package/browser/transaction/browser/BrowserSignerBase.d.ts +1 -1
- package/browser/transaction/browser/Web3Provider.d.ts +1 -1
- package/browser/transaction/browser/extensions/UnisatSigner.d.ts +1 -1
- package/browser/transaction/browser/extensions/XverseSigner.d.ts +1 -1
- package/browser/transaction/builders/CancelTransaction.d.ts +1 -1
- package/browser/transaction/builders/ConsolidatedInteractionTransaction.d.ts +4 -4
- package/browser/transaction/builders/CustomScriptTransaction.d.ts +1 -1
- package/browser/transaction/builders/DeploymentTransaction.d.ts +1 -1
- package/browser/transaction/builders/FundingTransaction.d.ts +1 -1
- package/browser/transaction/builders/InteractionTransaction.d.ts +1 -1
- package/browser/transaction/builders/InteractionTransactionP2WDA.d.ts +2 -2
- package/browser/transaction/builders/MultiSignTransaction.d.ts +1 -1
- package/browser/transaction/builders/SharedInteractionTransaction.d.ts +1 -1
- package/browser/transaction/builders/TransactionBuilder.d.ts +1 -1
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +1 -1
- package/browser/transaction/interfaces/Tap.d.ts +1 -1
- package/browser/transaction/mineable/TimelockGenerator.d.ts +1 -1
- package/browser/transaction/offline/OfflineTransactionManager.d.ts +1 -1
- package/browser/transaction/offline/TransactionReconstructor.d.ts +2 -2
- package/browser/transaction/processor/PsbtTransaction.d.ts +1 -1
- package/browser/transaction/shared/P2TR_MS.d.ts +1 -1
- package/browser/transaction/shared/TweakedTransaction.d.ts +3 -3
- package/browser/utxo/OPNetLimitedProvider.d.ts +1 -1
- package/browser/valibot.js +4948 -0
- package/browser/vendors.js +12913 -0
- package/browser/verification/TapscriptVerificator.d.ts +1 -1
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/keypair/Address.js +4 -4
- package/build/opnet.d.ts +9 -1
- package/build/opnet.js +6 -1
- package/build/transaction/builders/ConsolidatedInteractionTransaction.d.ts +3 -3
- package/build/transaction/builders/ConsolidatedInteractionTransaction.js +16 -16
- package/documentation/README.md +0 -2
- package/package.json +62 -4
- package/src/_version.ts +1 -1
- package/src/keypair/Address.ts +7 -4
- package/src/opnet.ts +9 -1
- package/src/transaction/TransactionFactory.ts +1 -4
- package/src/transaction/builders/ConsolidatedInteractionTransaction.ts +40 -47
- package/vite.config.browser.ts +92 -0
- package/webpack.config.js +143 -2
- package/browser/crypto/crypto-browser.d.ts +0 -11
- package/browser/index.js.LICENSE.txt +0 -29
- package/browser/transaction/offline/index.d.ts +0 -5
- package/browser/transaction/offline/interfaces/index.d.ts +0 -2
- package/build/transaction/offline/index.d.ts +0 -5
- package/build/transaction/offline/index.js +0 -5
- package/build/transaction/offline/interfaces/index.d.ts +0 -2
- package/build/transaction/offline/interfaces/index.js +0 -2
- package/src/transaction/offline/index.ts +0 -8
- package/src/transaction/offline/interfaces/index.ts +0 -2
|
@@ -0,0 +1,3316 @@
|
|
|
1
|
+
import { c as We, e as ln, n as dn, o as Me, p as yt, q as Qe, t as hn, u as Ae, v as yn, w as mn, x as kt, y as bn, z as wn } from "./noble-hashes.js";
|
|
2
|
+
var tt = {}, Ke = {}, nt = {}, rt = {}, _t;
|
|
3
|
+
function Ce() {
|
|
4
|
+
return _t || (_t = 1, (function(e) {
|
|
5
|
+
Object.defineProperty(e, "__esModule", { value: !0 }), e.notImplemented = e.bitMask = e.utf8ToBytes = e.randomBytes = e.isBytes = e.hexToBytes = e.concatBytes = e.bytesToUtf8 = e.bytesToHex = e.anumber = e.abytes = void 0, e.abool = m, e._abool2 = g, e._abytes2 = p, e.numberToHexUnpadded = v, e.hexToNumber = U, e.bytesToNumberBE = H, e.bytesToNumberLE = V, e.numberToBytesBE = E, e.numberToBytesLE = b, e.numberToVarBytesBE = S, e.ensureBytes = A, e.equalBytes = M, e.copyBytes = Q, e.asciiToBytes = F, e.inRange = se, e.aInRange = ie, e.bitLen = j, e.bitGet = O, e.bitSet = L, e.createHmacDrbg = K, e.validateObject = z, e.isHash = d, e._validateObject = u, e.memoized = a;
|
|
6
|
+
const r = /* @__PURE__ */ We();
|
|
7
|
+
var c = /* @__PURE__ */ We();
|
|
8
|
+
Object.defineProperty(e, "abytes", { enumerable: !0, get: function() {
|
|
9
|
+
return c.abytes;
|
|
10
|
+
} }), Object.defineProperty(e, "anumber", { enumerable: !0, get: function() {
|
|
11
|
+
return c.anumber;
|
|
12
|
+
} }), Object.defineProperty(e, "bytesToHex", { enumerable: !0, get: function() {
|
|
13
|
+
return c.bytesToHex;
|
|
14
|
+
} }), Object.defineProperty(e, "bytesToUtf8", { enumerable: !0, get: function() {
|
|
15
|
+
return c.bytesToUtf8;
|
|
16
|
+
} }), Object.defineProperty(e, "concatBytes", { enumerable: !0, get: function() {
|
|
17
|
+
return c.concatBytes;
|
|
18
|
+
} }), Object.defineProperty(e, "hexToBytes", { enumerable: !0, get: function() {
|
|
19
|
+
return c.hexToBytes;
|
|
20
|
+
} }), Object.defineProperty(e, "isBytes", { enumerable: !0, get: function() {
|
|
21
|
+
return c.isBytes;
|
|
22
|
+
} }), Object.defineProperty(e, "randomBytes", { enumerable: !0, get: function() {
|
|
23
|
+
return c.randomBytes;
|
|
24
|
+
} }), Object.defineProperty(e, "utf8ToBytes", { enumerable: !0, get: function() {
|
|
25
|
+
return c.utf8ToBytes;
|
|
26
|
+
} });
|
|
27
|
+
const t = /* @__PURE__ */ BigInt(0), l = /* @__PURE__ */ BigInt(1);
|
|
28
|
+
function m(i, o) {
|
|
29
|
+
if (typeof o != "boolean")
|
|
30
|
+
throw new Error(i + " boolean expected, got " + o);
|
|
31
|
+
}
|
|
32
|
+
function g(i, o = "") {
|
|
33
|
+
if (typeof i != "boolean") {
|
|
34
|
+
const h = o && `"${o}"`;
|
|
35
|
+
throw new Error(h + "expected boolean, got type=" + typeof i);
|
|
36
|
+
}
|
|
37
|
+
return i;
|
|
38
|
+
}
|
|
39
|
+
function p(i, o, h = "") {
|
|
40
|
+
const w = (0, r.isBytes)(i), s = i?.length, f = o !== void 0;
|
|
41
|
+
if (!w || f && s !== o) {
|
|
42
|
+
const y = h && `"${h}" `, B = f ? ` of length ${o}` : "", T = w ? `length=${s}` : `type=${typeof i}`;
|
|
43
|
+
throw new Error(y + "expected Uint8Array" + B + ", got " + T);
|
|
44
|
+
}
|
|
45
|
+
return i;
|
|
46
|
+
}
|
|
47
|
+
function v(i) {
|
|
48
|
+
const o = i.toString(16);
|
|
49
|
+
return o.length & 1 ? "0" + o : o;
|
|
50
|
+
}
|
|
51
|
+
function U(i) {
|
|
52
|
+
if (typeof i != "string")
|
|
53
|
+
throw new Error("hex string expected, got " + typeof i);
|
|
54
|
+
return i === "" ? t : BigInt("0x" + i);
|
|
55
|
+
}
|
|
56
|
+
function H(i) {
|
|
57
|
+
return U((0, r.bytesToHex)(i));
|
|
58
|
+
}
|
|
59
|
+
function V(i) {
|
|
60
|
+
return (0, r.abytes)(i), U((0, r.bytesToHex)(Uint8Array.from(i).reverse()));
|
|
61
|
+
}
|
|
62
|
+
function E(i, o) {
|
|
63
|
+
return (0, r.hexToBytes)(i.toString(16).padStart(o * 2, "0"));
|
|
64
|
+
}
|
|
65
|
+
function b(i, o) {
|
|
66
|
+
return E(i, o).reverse();
|
|
67
|
+
}
|
|
68
|
+
function S(i) {
|
|
69
|
+
return (0, r.hexToBytes)(v(i));
|
|
70
|
+
}
|
|
71
|
+
function A(i, o, h) {
|
|
72
|
+
let w;
|
|
73
|
+
if (typeof o == "string")
|
|
74
|
+
try {
|
|
75
|
+
w = (0, r.hexToBytes)(o);
|
|
76
|
+
} catch (f) {
|
|
77
|
+
throw new Error(i + " must be hex string or Uint8Array, cause: " + f);
|
|
78
|
+
}
|
|
79
|
+
else if ((0, r.isBytes)(o))
|
|
80
|
+
w = Uint8Array.from(o);
|
|
81
|
+
else
|
|
82
|
+
throw new Error(i + " must be hex string or Uint8Array");
|
|
83
|
+
const s = w.length;
|
|
84
|
+
if (typeof h == "number" && s !== h)
|
|
85
|
+
throw new Error(i + " of length " + h + " expected, got " + s);
|
|
86
|
+
return w;
|
|
87
|
+
}
|
|
88
|
+
function M(i, o) {
|
|
89
|
+
if (i.length !== o.length)
|
|
90
|
+
return !1;
|
|
91
|
+
let h = 0;
|
|
92
|
+
for (let w = 0; w < i.length; w++)
|
|
93
|
+
h |= i[w] ^ o[w];
|
|
94
|
+
return h === 0;
|
|
95
|
+
}
|
|
96
|
+
function Q(i) {
|
|
97
|
+
return Uint8Array.from(i);
|
|
98
|
+
}
|
|
99
|
+
function F(i) {
|
|
100
|
+
return Uint8Array.from(i, (o, h) => {
|
|
101
|
+
const w = o.charCodeAt(0);
|
|
102
|
+
if (o.length !== 1 || w > 127)
|
|
103
|
+
throw new Error(`string contains non-ASCII character "${i[h]}" with code ${w} at position ${h}`);
|
|
104
|
+
return w;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const G = (i) => typeof i == "bigint" && t <= i;
|
|
108
|
+
function se(i, o, h) {
|
|
109
|
+
return G(i) && G(o) && G(h) && o <= i && i < h;
|
|
110
|
+
}
|
|
111
|
+
function ie(i, o, h, w) {
|
|
112
|
+
if (!se(o, h, w))
|
|
113
|
+
throw new Error("expected valid " + i + ": " + h + " <= n < " + w + ", got " + o);
|
|
114
|
+
}
|
|
115
|
+
function j(i) {
|
|
116
|
+
let o;
|
|
117
|
+
for (o = 0; i > t; i >>= l, o += 1)
|
|
118
|
+
;
|
|
119
|
+
return o;
|
|
120
|
+
}
|
|
121
|
+
function O(i, o) {
|
|
122
|
+
return i >> BigInt(o) & l;
|
|
123
|
+
}
|
|
124
|
+
function L(i, o, h) {
|
|
125
|
+
return i | (h ? l : t) << BigInt(o);
|
|
126
|
+
}
|
|
127
|
+
const _ = (i) => (l << BigInt(i)) - l;
|
|
128
|
+
e.bitMask = _;
|
|
129
|
+
function K(i, o, h) {
|
|
130
|
+
if (typeof i != "number" || i < 2)
|
|
131
|
+
throw new Error("hashLen must be a number");
|
|
132
|
+
if (typeof o != "number" || o < 2)
|
|
133
|
+
throw new Error("qByteLen must be a number");
|
|
134
|
+
if (typeof h != "function")
|
|
135
|
+
throw new Error("hmacFn must be a function");
|
|
136
|
+
const w = (C) => new Uint8Array(C), s = (C) => Uint8Array.of(C);
|
|
137
|
+
let f = w(i), y = w(i), B = 0;
|
|
138
|
+
const T = () => {
|
|
139
|
+
f.fill(1), y.fill(0), B = 0;
|
|
140
|
+
}, k = (...C) => h(y, f, ...C), Y = (C = w(0)) => {
|
|
141
|
+
y = k(s(0), C), f = k(), C.length !== 0 && (y = k(s(1), C), f = k());
|
|
142
|
+
}, I = () => {
|
|
143
|
+
if (B++ >= 1e3)
|
|
144
|
+
throw new Error("drbg: tried 1000 values");
|
|
145
|
+
let C = 0;
|
|
146
|
+
const te = [];
|
|
147
|
+
for (; C < o; ) {
|
|
148
|
+
f = k();
|
|
149
|
+
const $ = f.slice();
|
|
150
|
+
te.push($), C += f.length;
|
|
151
|
+
}
|
|
152
|
+
return (0, r.concatBytes)(...te);
|
|
153
|
+
};
|
|
154
|
+
return (C, te) => {
|
|
155
|
+
T(), Y(C);
|
|
156
|
+
let $;
|
|
157
|
+
for (; !($ = te(I())); )
|
|
158
|
+
Y();
|
|
159
|
+
return T(), $;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const W = {
|
|
163
|
+
bigint: (i) => typeof i == "bigint",
|
|
164
|
+
function: (i) => typeof i == "function",
|
|
165
|
+
boolean: (i) => typeof i == "boolean",
|
|
166
|
+
string: (i) => typeof i == "string",
|
|
167
|
+
stringOrUint8Array: (i) => typeof i == "string" || (0, r.isBytes)(i),
|
|
168
|
+
isSafeInteger: (i) => Number.isSafeInteger(i),
|
|
169
|
+
array: (i) => Array.isArray(i),
|
|
170
|
+
field: (i, o) => o.Fp.isValid(i),
|
|
171
|
+
hash: (i) => typeof i == "function" && Number.isSafeInteger(i.outputLen)
|
|
172
|
+
};
|
|
173
|
+
function z(i, o, h = {}) {
|
|
174
|
+
const w = (s, f, y) => {
|
|
175
|
+
const B = W[f];
|
|
176
|
+
if (typeof B != "function")
|
|
177
|
+
throw new Error("invalid validator function");
|
|
178
|
+
const T = i[s];
|
|
179
|
+
if (!(y && T === void 0) && !B(T, i))
|
|
180
|
+
throw new Error("param " + String(s) + " is invalid. Expected " + f + ", got " + T);
|
|
181
|
+
};
|
|
182
|
+
for (const [s, f] of Object.entries(o))
|
|
183
|
+
w(s, f, !1);
|
|
184
|
+
for (const [s, f] of Object.entries(h))
|
|
185
|
+
w(s, f, !0);
|
|
186
|
+
return i;
|
|
187
|
+
}
|
|
188
|
+
function d(i) {
|
|
189
|
+
return typeof i == "function" && Number.isSafeInteger(i.outputLen);
|
|
190
|
+
}
|
|
191
|
+
function u(i, o, h = {}) {
|
|
192
|
+
if (!i || typeof i != "object")
|
|
193
|
+
throw new Error("expected valid options object");
|
|
194
|
+
function w(s, f, y) {
|
|
195
|
+
const B = i[s];
|
|
196
|
+
if (y && B === void 0)
|
|
197
|
+
return;
|
|
198
|
+
const T = typeof B;
|
|
199
|
+
if (T !== f || B === null)
|
|
200
|
+
throw new Error(`param "${s}" is invalid: expected ${f}, got ${T}`);
|
|
201
|
+
}
|
|
202
|
+
Object.entries(o).forEach(([s, f]) => w(s, f, !1)), Object.entries(h).forEach(([s, f]) => w(s, f, !0));
|
|
203
|
+
}
|
|
204
|
+
const n = () => {
|
|
205
|
+
throw new Error("not implemented");
|
|
206
|
+
};
|
|
207
|
+
e.notImplemented = n;
|
|
208
|
+
function a(i) {
|
|
209
|
+
const o = /* @__PURE__ */ new WeakMap();
|
|
210
|
+
return (h, ...w) => {
|
|
211
|
+
const s = o.get(h);
|
|
212
|
+
if (s !== void 0)
|
|
213
|
+
return s;
|
|
214
|
+
const f = i(h, ...w);
|
|
215
|
+
return o.set(h, f), f;
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
})(rt)), rt;
|
|
219
|
+
}
|
|
220
|
+
var Oe = {}, he = {}, St;
|
|
221
|
+
function $e() {
|
|
222
|
+
if (St) return he;
|
|
223
|
+
St = 1, Object.defineProperty(he, "__esModule", { value: !0 }), he.isNegativeLE = void 0, he.mod = V, he.pow = E, he.pow2 = b, he.invert = S, he.tonelliShanks = G, he.FpSqrt = se, he.validateField = O, he.FpPow = L, he.FpInvertBatch = _, he.FpDiv = K, he.FpLegendre = W, he.FpIsSquare = z, he.nLength = d, he.Field = u, he.FpSqrtOdd = n, he.FpSqrtEven = a, he.hashToPrivateScalar = i, he.getFieldBytesLength = o, he.getMinHashLength = h, he.mapHashToField = w;
|
|
224
|
+
const e = /* @__PURE__ */ Ce(), r = BigInt(0), c = BigInt(1), t = /* @__PURE__ */ BigInt(2), l = /* @__PURE__ */ BigInt(3), m = /* @__PURE__ */ BigInt(4), g = /* @__PURE__ */ BigInt(5), p = /* @__PURE__ */ BigInt(7), v = /* @__PURE__ */ BigInt(8), U = /* @__PURE__ */ BigInt(9), H = /* @__PURE__ */ BigInt(16);
|
|
225
|
+
function V(s, f) {
|
|
226
|
+
const y = s % f;
|
|
227
|
+
return y >= r ? y : f + y;
|
|
228
|
+
}
|
|
229
|
+
function E(s, f, y) {
|
|
230
|
+
return L(u(y), s, f);
|
|
231
|
+
}
|
|
232
|
+
function b(s, f, y) {
|
|
233
|
+
let B = s;
|
|
234
|
+
for (; f-- > r; )
|
|
235
|
+
B *= B, B %= y;
|
|
236
|
+
return B;
|
|
237
|
+
}
|
|
238
|
+
function S(s, f) {
|
|
239
|
+
if (s === r)
|
|
240
|
+
throw new Error("invert: expected non-zero number");
|
|
241
|
+
if (f <= r)
|
|
242
|
+
throw new Error("invert: expected positive modulus, got " + f);
|
|
243
|
+
let y = V(s, f), B = f, T = r, k = c;
|
|
244
|
+
for (; y !== r; ) {
|
|
245
|
+
const I = B / y, q = B % y, C = T - k * I;
|
|
246
|
+
B = y, y = q, T = k, k = C;
|
|
247
|
+
}
|
|
248
|
+
if (B !== c)
|
|
249
|
+
throw new Error("invert: does not exist");
|
|
250
|
+
return V(T, f);
|
|
251
|
+
}
|
|
252
|
+
function A(s, f, y) {
|
|
253
|
+
if (!s.eql(s.sqr(f), y))
|
|
254
|
+
throw new Error("Cannot find square root");
|
|
255
|
+
}
|
|
256
|
+
function M(s, f) {
|
|
257
|
+
const y = (s.ORDER + c) / m, B = s.pow(f, y);
|
|
258
|
+
return A(s, B, f), B;
|
|
259
|
+
}
|
|
260
|
+
function Q(s, f) {
|
|
261
|
+
const y = (s.ORDER - g) / v, B = s.mul(f, t), T = s.pow(B, y), k = s.mul(f, T), Y = s.mul(s.mul(k, t), T), I = s.mul(k, s.sub(Y, s.ONE));
|
|
262
|
+
return A(s, I, f), I;
|
|
263
|
+
}
|
|
264
|
+
function F(s) {
|
|
265
|
+
const f = u(s), y = G(s), B = y(f, f.neg(f.ONE)), T = y(f, B), k = y(f, f.neg(B)), Y = (s + p) / H;
|
|
266
|
+
return (I, q) => {
|
|
267
|
+
let C = I.pow(q, Y), te = I.mul(C, B);
|
|
268
|
+
const $ = I.mul(C, T), x = I.mul(C, k), X = I.eql(I.sqr(te), q), ae = I.eql(I.sqr($), q);
|
|
269
|
+
C = I.cmov(C, te, X), te = I.cmov(x, $, ae);
|
|
270
|
+
const ye = I.eql(I.sqr(te), q), pe = I.cmov(C, te, ye);
|
|
271
|
+
return A(I, pe, q), pe;
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function G(s) {
|
|
275
|
+
if (s < l)
|
|
276
|
+
throw new Error("sqrt is not defined for small field");
|
|
277
|
+
let f = s - c, y = 0;
|
|
278
|
+
for (; f % t === r; )
|
|
279
|
+
f /= t, y++;
|
|
280
|
+
let B = t;
|
|
281
|
+
const T = u(s);
|
|
282
|
+
for (; W(T, B) === 1; )
|
|
283
|
+
if (B++ > 1e3)
|
|
284
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
285
|
+
if (y === 1)
|
|
286
|
+
return M;
|
|
287
|
+
let k = T.pow(B, f);
|
|
288
|
+
const Y = (f + c) / t;
|
|
289
|
+
return function(q, C) {
|
|
290
|
+
if (q.is0(C))
|
|
291
|
+
return C;
|
|
292
|
+
if (W(q, C) !== 1)
|
|
293
|
+
throw new Error("Cannot find square root");
|
|
294
|
+
let te = y, $ = q.mul(q.ONE, k), x = q.pow(C, f), X = q.pow(C, Y);
|
|
295
|
+
for (; !q.eql(x, q.ONE); ) {
|
|
296
|
+
if (q.is0(x))
|
|
297
|
+
return q.ZERO;
|
|
298
|
+
let ae = 1, ye = q.sqr(x);
|
|
299
|
+
for (; !q.eql(ye, q.ONE); )
|
|
300
|
+
if (ae++, ye = q.sqr(ye), ae === te)
|
|
301
|
+
throw new Error("Cannot find square root");
|
|
302
|
+
const pe = c << BigInt(te - ae - 1), ve = q.pow($, pe);
|
|
303
|
+
te = ae, $ = q.sqr(ve), x = q.mul(x, $), X = q.mul(X, ve);
|
|
304
|
+
}
|
|
305
|
+
return X;
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
function se(s) {
|
|
309
|
+
return s % m === l ? M : s % v === g ? Q : s % H === U ? F(s) : G(s);
|
|
310
|
+
}
|
|
311
|
+
const ie = (s, f) => (V(s, f) & c) === c;
|
|
312
|
+
he.isNegativeLE = ie;
|
|
313
|
+
const j = [
|
|
314
|
+
"create",
|
|
315
|
+
"isValid",
|
|
316
|
+
"is0",
|
|
317
|
+
"neg",
|
|
318
|
+
"inv",
|
|
319
|
+
"sqrt",
|
|
320
|
+
"sqr",
|
|
321
|
+
"eql",
|
|
322
|
+
"add",
|
|
323
|
+
"sub",
|
|
324
|
+
"mul",
|
|
325
|
+
"pow",
|
|
326
|
+
"div",
|
|
327
|
+
"addN",
|
|
328
|
+
"subN",
|
|
329
|
+
"mulN",
|
|
330
|
+
"sqrN"
|
|
331
|
+
];
|
|
332
|
+
function O(s) {
|
|
333
|
+
const f = {
|
|
334
|
+
ORDER: "bigint",
|
|
335
|
+
MASK: "bigint",
|
|
336
|
+
BYTES: "number",
|
|
337
|
+
BITS: "number"
|
|
338
|
+
}, y = j.reduce((B, T) => (B[T] = "function", B), f);
|
|
339
|
+
return (0, e._validateObject)(s, y), s;
|
|
340
|
+
}
|
|
341
|
+
function L(s, f, y) {
|
|
342
|
+
if (y < r)
|
|
343
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
344
|
+
if (y === r)
|
|
345
|
+
return s.ONE;
|
|
346
|
+
if (y === c)
|
|
347
|
+
return f;
|
|
348
|
+
let B = s.ONE, T = f;
|
|
349
|
+
for (; y > r; )
|
|
350
|
+
y & c && (B = s.mul(B, T)), T = s.sqr(T), y >>= c;
|
|
351
|
+
return B;
|
|
352
|
+
}
|
|
353
|
+
function _(s, f, y = !1) {
|
|
354
|
+
const B = new Array(f.length).fill(y ? s.ZERO : void 0), T = f.reduce((Y, I, q) => s.is0(I) ? Y : (B[q] = Y, s.mul(Y, I)), s.ONE), k = s.inv(T);
|
|
355
|
+
return f.reduceRight((Y, I, q) => s.is0(I) ? Y : (B[q] = s.mul(Y, B[q]), s.mul(Y, I)), k), B;
|
|
356
|
+
}
|
|
357
|
+
function K(s, f, y) {
|
|
358
|
+
return s.mul(f, typeof y == "bigint" ? S(y, s.ORDER) : s.inv(y));
|
|
359
|
+
}
|
|
360
|
+
function W(s, f) {
|
|
361
|
+
const y = (s.ORDER - c) / t, B = s.pow(f, y), T = s.eql(B, s.ONE), k = s.eql(B, s.ZERO), Y = s.eql(B, s.neg(s.ONE));
|
|
362
|
+
if (!T && !k && !Y)
|
|
363
|
+
throw new Error("invalid Legendre symbol result");
|
|
364
|
+
return T ? 1 : k ? 0 : -1;
|
|
365
|
+
}
|
|
366
|
+
function z(s, f) {
|
|
367
|
+
return W(s, f) === 1;
|
|
368
|
+
}
|
|
369
|
+
function d(s, f) {
|
|
370
|
+
f !== void 0 && (0, e.anumber)(f);
|
|
371
|
+
const y = f !== void 0 ? f : s.toString(2).length, B = Math.ceil(y / 8);
|
|
372
|
+
return { nBitLength: y, nByteLength: B };
|
|
373
|
+
}
|
|
374
|
+
function u(s, f, y = !1, B = {}) {
|
|
375
|
+
if (s <= r)
|
|
376
|
+
throw new Error("invalid field: expected ORDER > 0, got " + s);
|
|
377
|
+
let T, k, Y = !1, I;
|
|
378
|
+
if (typeof f == "object" && f != null) {
|
|
379
|
+
if (B.sqrt || y)
|
|
380
|
+
throw new Error("cannot specify opts in two arguments");
|
|
381
|
+
const x = f;
|
|
382
|
+
x.BITS && (T = x.BITS), x.sqrt && (k = x.sqrt), typeof x.isLE == "boolean" && (y = x.isLE), typeof x.modFromBytes == "boolean" && (Y = x.modFromBytes), I = x.allowedLengths;
|
|
383
|
+
} else
|
|
384
|
+
typeof f == "number" && (T = f), B.sqrt && (k = B.sqrt);
|
|
385
|
+
const { nBitLength: q, nByteLength: C } = d(s, T);
|
|
386
|
+
if (C > 2048)
|
|
387
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
388
|
+
let te;
|
|
389
|
+
const $ = Object.freeze({
|
|
390
|
+
ORDER: s,
|
|
391
|
+
isLE: y,
|
|
392
|
+
BITS: q,
|
|
393
|
+
BYTES: C,
|
|
394
|
+
MASK: (0, e.bitMask)(q),
|
|
395
|
+
ZERO: r,
|
|
396
|
+
ONE: c,
|
|
397
|
+
allowedLengths: I,
|
|
398
|
+
create: (x) => V(x, s),
|
|
399
|
+
isValid: (x) => {
|
|
400
|
+
if (typeof x != "bigint")
|
|
401
|
+
throw new Error("invalid field element: expected bigint, got " + typeof x);
|
|
402
|
+
return r <= x && x < s;
|
|
403
|
+
},
|
|
404
|
+
is0: (x) => x === r,
|
|
405
|
+
// is valid and invertible
|
|
406
|
+
isValidNot0: (x) => !$.is0(x) && $.isValid(x),
|
|
407
|
+
isOdd: (x) => (x & c) === c,
|
|
408
|
+
neg: (x) => V(-x, s),
|
|
409
|
+
eql: (x, X) => x === X,
|
|
410
|
+
sqr: (x) => V(x * x, s),
|
|
411
|
+
add: (x, X) => V(x + X, s),
|
|
412
|
+
sub: (x, X) => V(x - X, s),
|
|
413
|
+
mul: (x, X) => V(x * X, s),
|
|
414
|
+
pow: (x, X) => L($, x, X),
|
|
415
|
+
div: (x, X) => V(x * S(X, s), s),
|
|
416
|
+
// Same as above, but doesn't normalize
|
|
417
|
+
sqrN: (x) => x * x,
|
|
418
|
+
addN: (x, X) => x + X,
|
|
419
|
+
subN: (x, X) => x - X,
|
|
420
|
+
mulN: (x, X) => x * X,
|
|
421
|
+
inv: (x) => S(x, s),
|
|
422
|
+
sqrt: k || ((x) => (te || (te = se(s)), te($, x))),
|
|
423
|
+
toBytes: (x) => y ? (0, e.numberToBytesLE)(x, C) : (0, e.numberToBytesBE)(x, C),
|
|
424
|
+
fromBytes: (x, X = !0) => {
|
|
425
|
+
if (I) {
|
|
426
|
+
if (!I.includes(x.length) || x.length > C)
|
|
427
|
+
throw new Error("Field.fromBytes: expected " + I + " bytes, got " + x.length);
|
|
428
|
+
const ye = new Uint8Array(C);
|
|
429
|
+
ye.set(x, y ? 0 : ye.length - x.length), x = ye;
|
|
430
|
+
}
|
|
431
|
+
if (x.length !== C)
|
|
432
|
+
throw new Error("Field.fromBytes: expected " + C + " bytes, got " + x.length);
|
|
433
|
+
let ae = y ? (0, e.bytesToNumberLE)(x) : (0, e.bytesToNumberBE)(x);
|
|
434
|
+
if (Y && (ae = V(ae, s)), !X && !$.isValid(ae))
|
|
435
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
436
|
+
return ae;
|
|
437
|
+
},
|
|
438
|
+
// TODO: we don't need it here, move out to separate fn
|
|
439
|
+
invertBatch: (x) => _($, x),
|
|
440
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
441
|
+
// and it's unclear what to return in there.
|
|
442
|
+
cmov: (x, X, ae) => ae ? X : x
|
|
443
|
+
});
|
|
444
|
+
return Object.freeze($);
|
|
445
|
+
}
|
|
446
|
+
function n(s, f) {
|
|
447
|
+
if (!s.isOdd)
|
|
448
|
+
throw new Error("Field doesn't have isOdd");
|
|
449
|
+
const y = s.sqrt(f);
|
|
450
|
+
return s.isOdd(y) ? y : s.neg(y);
|
|
451
|
+
}
|
|
452
|
+
function a(s, f) {
|
|
453
|
+
if (!s.isOdd)
|
|
454
|
+
throw new Error("Field doesn't have isOdd");
|
|
455
|
+
const y = s.sqrt(f);
|
|
456
|
+
return s.isOdd(y) ? s.neg(y) : y;
|
|
457
|
+
}
|
|
458
|
+
function i(s, f, y = !1) {
|
|
459
|
+
s = (0, e.ensureBytes)("privateHash", s);
|
|
460
|
+
const B = s.length, T = d(f).nByteLength + 8;
|
|
461
|
+
if (T < 24 || B < T || B > 1024)
|
|
462
|
+
throw new Error("hashToPrivateScalar: expected " + T + "-1024 bytes of input, got " + B);
|
|
463
|
+
const k = y ? (0, e.bytesToNumberLE)(s) : (0, e.bytesToNumberBE)(s);
|
|
464
|
+
return V(k, f - c) + c;
|
|
465
|
+
}
|
|
466
|
+
function o(s) {
|
|
467
|
+
if (typeof s != "bigint")
|
|
468
|
+
throw new Error("field order must be bigint");
|
|
469
|
+
const f = s.toString(2).length;
|
|
470
|
+
return Math.ceil(f / 8);
|
|
471
|
+
}
|
|
472
|
+
function h(s) {
|
|
473
|
+
const f = o(s);
|
|
474
|
+
return f + Math.ceil(f / 2);
|
|
475
|
+
}
|
|
476
|
+
function w(s, f, y = !1) {
|
|
477
|
+
const B = s.length, T = o(f), k = h(f);
|
|
478
|
+
if (B < 16 || B < k || B > 1024)
|
|
479
|
+
throw new Error("expected " + k + "-1024 bytes of input, got " + B);
|
|
480
|
+
const Y = y ? (0, e.bytesToNumberLE)(s) : (0, e.bytesToNumberBE)(s), I = V(Y, f - c) + c;
|
|
481
|
+
return y ? (0, e.numberToBytesLE)(I, T) : (0, e.numberToBytesBE)(I, T);
|
|
482
|
+
}
|
|
483
|
+
return he;
|
|
484
|
+
}
|
|
485
|
+
var Tt;
|
|
486
|
+
function gn() {
|
|
487
|
+
if (Tt) return Oe;
|
|
488
|
+
Tt = 1, Object.defineProperty(Oe, "__esModule", { value: !0 }), Oe.wNAF = void 0, Oe.negateCt = l, Oe.normalizeZ = m, Oe.mulEndoUnsafe = M, Oe.pippenger = Q, Oe.precomputeMSMUnsafe = F, Oe.validateBasic = G, Oe._createCurveFields = ie;
|
|
489
|
+
const e = /* @__PURE__ */ Ce(), r = /* @__PURE__ */ $e(), c = BigInt(0), t = BigInt(1);
|
|
490
|
+
function l(j, O) {
|
|
491
|
+
const L = O.negate();
|
|
492
|
+
return j ? L : O;
|
|
493
|
+
}
|
|
494
|
+
function m(j, O) {
|
|
495
|
+
const L = (0, r.FpInvertBatch)(j.Fp, O.map((_) => _.Z));
|
|
496
|
+
return O.map((_, K) => j.fromAffine(_.toAffine(L[K])));
|
|
497
|
+
}
|
|
498
|
+
function g(j, O) {
|
|
499
|
+
if (!Number.isSafeInteger(j) || j <= 0 || j > O)
|
|
500
|
+
throw new Error("invalid window size, expected [1.." + O + "], got W=" + j);
|
|
501
|
+
}
|
|
502
|
+
function p(j, O) {
|
|
503
|
+
g(j, O);
|
|
504
|
+
const L = Math.ceil(O / j) + 1, _ = 2 ** (j - 1), K = 2 ** j, W = (0, e.bitMask)(j), z = BigInt(j);
|
|
505
|
+
return { windows: L, windowSize: _, mask: W, maxNumber: K, shiftBy: z };
|
|
506
|
+
}
|
|
507
|
+
function v(j, O, L) {
|
|
508
|
+
const { windowSize: _, mask: K, maxNumber: W, shiftBy: z } = L;
|
|
509
|
+
let d = Number(j & K), u = j >> z;
|
|
510
|
+
d > _ && (d -= W, u += t);
|
|
511
|
+
const n = O * _, a = n + Math.abs(d) - 1, i = d === 0, o = d < 0, h = O % 2 !== 0;
|
|
512
|
+
return { nextN: u, offset: a, isZero: i, isNeg: o, isNegF: h, offsetF: n };
|
|
513
|
+
}
|
|
514
|
+
function U(j, O) {
|
|
515
|
+
if (!Array.isArray(j))
|
|
516
|
+
throw new Error("array expected");
|
|
517
|
+
j.forEach((L, _) => {
|
|
518
|
+
if (!(L instanceof O))
|
|
519
|
+
throw new Error("invalid point at index " + _);
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
function H(j, O) {
|
|
523
|
+
if (!Array.isArray(j))
|
|
524
|
+
throw new Error("array of scalars expected");
|
|
525
|
+
j.forEach((L, _) => {
|
|
526
|
+
if (!O.isValid(L))
|
|
527
|
+
throw new Error("invalid scalar at index " + _);
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
const V = /* @__PURE__ */ new WeakMap(), E = /* @__PURE__ */ new WeakMap();
|
|
531
|
+
function b(j) {
|
|
532
|
+
return E.get(j) || 1;
|
|
533
|
+
}
|
|
534
|
+
function S(j) {
|
|
535
|
+
if (j !== c)
|
|
536
|
+
throw new Error("invalid wNAF");
|
|
537
|
+
}
|
|
538
|
+
class A {
|
|
539
|
+
// Parametrized with a given Point class (not individual point)
|
|
540
|
+
constructor(O, L) {
|
|
541
|
+
this.BASE = O.BASE, this.ZERO = O.ZERO, this.Fn = O.Fn, this.bits = L;
|
|
542
|
+
}
|
|
543
|
+
// non-const time multiplication ladder
|
|
544
|
+
_unsafeLadder(O, L, _ = this.ZERO) {
|
|
545
|
+
let K = O;
|
|
546
|
+
for (; L > c; )
|
|
547
|
+
L & t && (_ = _.add(K)), K = K.double(), L >>= t;
|
|
548
|
+
return _;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
552
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
553
|
+
* Number of precomputed points depends on the curve size:
|
|
554
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
555
|
+
* - 𝑊 is the window size
|
|
556
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
557
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
558
|
+
* @param point Point instance
|
|
559
|
+
* @param W window size
|
|
560
|
+
* @returns precomputed point tables flattened to a single array
|
|
561
|
+
*/
|
|
562
|
+
precomputeWindow(O, L) {
|
|
563
|
+
const { windows: _, windowSize: K } = p(L, this.bits), W = [];
|
|
564
|
+
let z = O, d = z;
|
|
565
|
+
for (let u = 0; u < _; u++) {
|
|
566
|
+
d = z, W.push(d);
|
|
567
|
+
for (let n = 1; n < K; n++)
|
|
568
|
+
d = d.add(z), W.push(d);
|
|
569
|
+
z = d.double();
|
|
570
|
+
}
|
|
571
|
+
return W;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
575
|
+
* More compact implementation:
|
|
576
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
577
|
+
* @returns real and fake (for const-time) points
|
|
578
|
+
*/
|
|
579
|
+
wNAF(O, L, _) {
|
|
580
|
+
if (!this.Fn.isValid(_))
|
|
581
|
+
throw new Error("invalid scalar");
|
|
582
|
+
let K = this.ZERO, W = this.BASE;
|
|
583
|
+
const z = p(O, this.bits);
|
|
584
|
+
for (let d = 0; d < z.windows; d++) {
|
|
585
|
+
const { nextN: u, offset: n, isZero: a, isNeg: i, isNegF: o, offsetF: h } = v(_, d, z);
|
|
586
|
+
_ = u, a ? W = W.add(l(o, L[h])) : K = K.add(l(i, L[n]));
|
|
587
|
+
}
|
|
588
|
+
return S(_), { p: K, f: W };
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
592
|
+
* @param acc accumulator point to add result of multiplication
|
|
593
|
+
* @returns point
|
|
594
|
+
*/
|
|
595
|
+
wNAFUnsafe(O, L, _, K = this.ZERO) {
|
|
596
|
+
const W = p(O, this.bits);
|
|
597
|
+
for (let z = 0; z < W.windows && _ !== c; z++) {
|
|
598
|
+
const { nextN: d, offset: u, isZero: n, isNeg: a } = v(_, z, W);
|
|
599
|
+
if (_ = d, !n) {
|
|
600
|
+
const i = L[u];
|
|
601
|
+
K = K.add(a ? i.negate() : i);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return S(_), K;
|
|
605
|
+
}
|
|
606
|
+
getPrecomputes(O, L, _) {
|
|
607
|
+
let K = V.get(L);
|
|
608
|
+
return K || (K = this.precomputeWindow(L, O), O !== 1 && (typeof _ == "function" && (K = _(K)), V.set(L, K))), K;
|
|
609
|
+
}
|
|
610
|
+
cached(O, L, _) {
|
|
611
|
+
const K = b(O);
|
|
612
|
+
return this.wNAF(K, this.getPrecomputes(K, O, _), L);
|
|
613
|
+
}
|
|
614
|
+
unsafe(O, L, _, K) {
|
|
615
|
+
const W = b(O);
|
|
616
|
+
return W === 1 ? this._unsafeLadder(O, L, K) : this.wNAFUnsafe(W, this.getPrecomputes(W, O, _), L, K);
|
|
617
|
+
}
|
|
618
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
619
|
+
// using windowed method. This specifies window size and
|
|
620
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
621
|
+
createCache(O, L) {
|
|
622
|
+
g(L, this.bits), E.set(O, L), V.delete(O);
|
|
623
|
+
}
|
|
624
|
+
hasCache(O) {
|
|
625
|
+
return b(O) !== 1;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
Oe.wNAF = A;
|
|
629
|
+
function M(j, O, L, _) {
|
|
630
|
+
let K = O, W = j.ZERO, z = j.ZERO;
|
|
631
|
+
for (; L > c || _ > c; )
|
|
632
|
+
L & t && (W = W.add(K)), _ & t && (z = z.add(K)), K = K.double(), L >>= t, _ >>= t;
|
|
633
|
+
return { p1: W, p2: z };
|
|
634
|
+
}
|
|
635
|
+
function Q(j, O, L, _) {
|
|
636
|
+
U(L, j), H(_, O);
|
|
637
|
+
const K = L.length, W = _.length;
|
|
638
|
+
if (K !== W)
|
|
639
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
640
|
+
const z = j.ZERO, d = (0, e.bitLen)(BigInt(K));
|
|
641
|
+
let u = 1;
|
|
642
|
+
d > 12 ? u = d - 3 : d > 4 ? u = d - 2 : d > 0 && (u = 2);
|
|
643
|
+
const n = (0, e.bitMask)(u), a = new Array(Number(n) + 1).fill(z), i = Math.floor((O.BITS - 1) / u) * u;
|
|
644
|
+
let o = z;
|
|
645
|
+
for (let h = i; h >= 0; h -= u) {
|
|
646
|
+
a.fill(z);
|
|
647
|
+
for (let s = 0; s < W; s++) {
|
|
648
|
+
const f = _[s], y = Number(f >> BigInt(h) & n);
|
|
649
|
+
a[y] = a[y].add(L[s]);
|
|
650
|
+
}
|
|
651
|
+
let w = z;
|
|
652
|
+
for (let s = a.length - 1, f = z; s > 0; s--)
|
|
653
|
+
f = f.add(a[s]), w = w.add(f);
|
|
654
|
+
if (o = o.add(w), h !== 0)
|
|
655
|
+
for (let s = 0; s < u; s++)
|
|
656
|
+
o = o.double();
|
|
657
|
+
}
|
|
658
|
+
return o;
|
|
659
|
+
}
|
|
660
|
+
function F(j, O, L, _) {
|
|
661
|
+
g(_, O.BITS), U(L, j);
|
|
662
|
+
const K = j.ZERO, W = 2 ** _ - 1, z = Math.ceil(O.BITS / _), d = (0, e.bitMask)(_), u = L.map((n) => {
|
|
663
|
+
const a = [];
|
|
664
|
+
for (let i = 0, o = n; i < W; i++)
|
|
665
|
+
a.push(o), o = o.add(n);
|
|
666
|
+
return a;
|
|
667
|
+
});
|
|
668
|
+
return (n) => {
|
|
669
|
+
if (H(n, O), n.length > L.length)
|
|
670
|
+
throw new Error("array of scalars must be smaller than array of points");
|
|
671
|
+
let a = K;
|
|
672
|
+
for (let i = 0; i < z; i++) {
|
|
673
|
+
if (a !== K)
|
|
674
|
+
for (let h = 0; h < _; h++)
|
|
675
|
+
a = a.double();
|
|
676
|
+
const o = BigInt(z * _ - (i + 1) * _);
|
|
677
|
+
for (let h = 0; h < n.length; h++) {
|
|
678
|
+
const w = n[h], s = Number(w >> o & d);
|
|
679
|
+
s && (a = a.add(u[h][s - 1]));
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
return a;
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
function G(j) {
|
|
686
|
+
return (0, r.validateField)(j.Fp), (0, e.validateObject)(j, {
|
|
687
|
+
n: "bigint",
|
|
688
|
+
h: "bigint",
|
|
689
|
+
Gx: "field",
|
|
690
|
+
Gy: "field"
|
|
691
|
+
}, {
|
|
692
|
+
nBitLength: "isSafeInteger",
|
|
693
|
+
nByteLength: "isSafeInteger"
|
|
694
|
+
}), Object.freeze({
|
|
695
|
+
...(0, r.nLength)(j.n, j.nBitLength),
|
|
696
|
+
...j,
|
|
697
|
+
p: j.Fp.ORDER
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
function se(j, O, L) {
|
|
701
|
+
if (O) {
|
|
702
|
+
if (O.ORDER !== j)
|
|
703
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
704
|
+
return (0, r.validateField)(O), O;
|
|
705
|
+
} else
|
|
706
|
+
return (0, r.Field)(j, { isLE: L });
|
|
707
|
+
}
|
|
708
|
+
function ie(j, O, L = {}, _) {
|
|
709
|
+
if (_ === void 0 && (_ = j === "edwards"), !O || typeof O != "object")
|
|
710
|
+
throw new Error(`expected valid ${j} CURVE object`);
|
|
711
|
+
for (const u of ["p", "n", "h"]) {
|
|
712
|
+
const n = O[u];
|
|
713
|
+
if (!(typeof n == "bigint" && n > c))
|
|
714
|
+
throw new Error(`CURVE.${u} must be positive bigint`);
|
|
715
|
+
}
|
|
716
|
+
const K = se(O.p, L.Fp, _), W = se(O.n, L.Fn, _), d = ["Gx", "Gy", "a", j === "weierstrass" ? "b" : "d"];
|
|
717
|
+
for (const u of d)
|
|
718
|
+
if (!K.isValid(O[u]))
|
|
719
|
+
throw new Error(`CURVE.${u} must be valid field element of CURVE.Fp`);
|
|
720
|
+
return O = Object.freeze(Object.assign({}, O)), { CURVE: O, Fp: K, Fn: W };
|
|
721
|
+
}
|
|
722
|
+
return Oe;
|
|
723
|
+
}
|
|
724
|
+
var xt;
|
|
725
|
+
function Ct() {
|
|
726
|
+
return xt || (xt = 1, (function(e) {
|
|
727
|
+
Object.defineProperty(e, "__esModule", { value: !0 }), e.DER = e.DERErr = void 0, e._splitEndoScalar = p, e._normFnElement = M, e.weierstrassN = Q, e.SWUFpSqrtRatio = G, e.mapToCurveSimpleSWU = se, e.ecdh = j, e.ecdsa = O, e.weierstrassPoints = L, e._legacyHelperEquat = W, e.weierstrass = u;
|
|
728
|
+
const r = /* @__PURE__ */ ln(), c = /* @__PURE__ */ We(), t = /* @__PURE__ */ Ce(), l = /* @__PURE__ */ gn(), m = /* @__PURE__ */ $e(), g = (n, a) => (n + (n >= 0 ? a : -a) / b) / a;
|
|
729
|
+
function p(n, a, i) {
|
|
730
|
+
const [[o, h], [w, s]] = a, f = g(s * n, i), y = g(-h * n, i);
|
|
731
|
+
let B = n - f * o - y * w, T = -f * h - y * s;
|
|
732
|
+
const k = B < V, Y = T < V;
|
|
733
|
+
k && (B = -B), Y && (T = -T);
|
|
734
|
+
const I = (0, t.bitMask)(Math.ceil((0, t.bitLen)(i) / 2)) + E;
|
|
735
|
+
if (B < V || B >= I || T < V || T >= I)
|
|
736
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + n);
|
|
737
|
+
return { k1neg: k, k1: B, k2neg: Y, k2: T };
|
|
738
|
+
}
|
|
739
|
+
function v(n) {
|
|
740
|
+
if (!["compact", "recovered", "der"].includes(n))
|
|
741
|
+
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
742
|
+
return n;
|
|
743
|
+
}
|
|
744
|
+
function U(n, a) {
|
|
745
|
+
const i = {};
|
|
746
|
+
for (let o of Object.keys(a))
|
|
747
|
+
i[o] = n[o] === void 0 ? a[o] : n[o];
|
|
748
|
+
return (0, t._abool2)(i.lowS, "lowS"), (0, t._abool2)(i.prehash, "prehash"), i.format !== void 0 && v(i.format), i;
|
|
749
|
+
}
|
|
750
|
+
class H extends Error {
|
|
751
|
+
constructor(a = "") {
|
|
752
|
+
super(a);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
e.DERErr = H, e.DER = {
|
|
756
|
+
// asn.1 DER encoding utils
|
|
757
|
+
Err: H,
|
|
758
|
+
// Basic building block is TLV (Tag-Length-Value)
|
|
759
|
+
_tlv: {
|
|
760
|
+
encode: (n, a) => {
|
|
761
|
+
const { Err: i } = e.DER;
|
|
762
|
+
if (n < 0 || n > 256)
|
|
763
|
+
throw new i("tlv.encode: wrong tag");
|
|
764
|
+
if (a.length & 1)
|
|
765
|
+
throw new i("tlv.encode: unpadded data");
|
|
766
|
+
const o = a.length / 2, h = (0, t.numberToHexUnpadded)(o);
|
|
767
|
+
if (h.length / 2 & 128)
|
|
768
|
+
throw new i("tlv.encode: long form length too big");
|
|
769
|
+
const w = o > 127 ? (0, t.numberToHexUnpadded)(h.length / 2 | 128) : "";
|
|
770
|
+
return (0, t.numberToHexUnpadded)(n) + w + h + a;
|
|
771
|
+
},
|
|
772
|
+
// v - value, l - left bytes (unparsed)
|
|
773
|
+
decode(n, a) {
|
|
774
|
+
const { Err: i } = e.DER;
|
|
775
|
+
let o = 0;
|
|
776
|
+
if (n < 0 || n > 256)
|
|
777
|
+
throw new i("tlv.encode: wrong tag");
|
|
778
|
+
if (a.length < 2 || a[o++] !== n)
|
|
779
|
+
throw new i("tlv.decode: wrong tlv");
|
|
780
|
+
const h = a[o++], w = !!(h & 128);
|
|
781
|
+
let s = 0;
|
|
782
|
+
if (!w)
|
|
783
|
+
s = h;
|
|
784
|
+
else {
|
|
785
|
+
const y = h & 127;
|
|
786
|
+
if (!y)
|
|
787
|
+
throw new i("tlv.decode(long): indefinite length not supported");
|
|
788
|
+
if (y > 4)
|
|
789
|
+
throw new i("tlv.decode(long): byte length is too big");
|
|
790
|
+
const B = a.subarray(o, o + y);
|
|
791
|
+
if (B.length !== y)
|
|
792
|
+
throw new i("tlv.decode: length bytes not complete");
|
|
793
|
+
if (B[0] === 0)
|
|
794
|
+
throw new i("tlv.decode(long): zero leftmost byte");
|
|
795
|
+
for (const T of B)
|
|
796
|
+
s = s << 8 | T;
|
|
797
|
+
if (o += y, s < 128)
|
|
798
|
+
throw new i("tlv.decode(long): not minimal encoding");
|
|
799
|
+
}
|
|
800
|
+
const f = a.subarray(o, o + s);
|
|
801
|
+
if (f.length !== s)
|
|
802
|
+
throw new i("tlv.decode: wrong value length");
|
|
803
|
+
return { v: f, l: a.subarray(o + s) };
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
807
|
+
// since we always use positive integers here. It must always be empty:
|
|
808
|
+
// - add zero byte if exists
|
|
809
|
+
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
810
|
+
_int: {
|
|
811
|
+
encode(n) {
|
|
812
|
+
const { Err: a } = e.DER;
|
|
813
|
+
if (n < V)
|
|
814
|
+
throw new a("integer: negative integers are not allowed");
|
|
815
|
+
let i = (0, t.numberToHexUnpadded)(n);
|
|
816
|
+
if (Number.parseInt(i[0], 16) & 8 && (i = "00" + i), i.length & 1)
|
|
817
|
+
throw new a("unexpected DER parsing assertion: unpadded hex");
|
|
818
|
+
return i;
|
|
819
|
+
},
|
|
820
|
+
decode(n) {
|
|
821
|
+
const { Err: a } = e.DER;
|
|
822
|
+
if (n[0] & 128)
|
|
823
|
+
throw new a("invalid signature integer: negative");
|
|
824
|
+
if (n[0] === 0 && !(n[1] & 128))
|
|
825
|
+
throw new a("invalid signature integer: unnecessary leading zero");
|
|
826
|
+
return (0, t.bytesToNumberBE)(n);
|
|
827
|
+
}
|
|
828
|
+
},
|
|
829
|
+
toSig(n) {
|
|
830
|
+
const { Err: a, _int: i, _tlv: o } = e.DER, h = (0, t.ensureBytes)("signature", n), { v: w, l: s } = o.decode(48, h);
|
|
831
|
+
if (s.length)
|
|
832
|
+
throw new a("invalid signature: left bytes after parsing");
|
|
833
|
+
const { v: f, l: y } = o.decode(2, w), { v: B, l: T } = o.decode(2, y);
|
|
834
|
+
if (T.length)
|
|
835
|
+
throw new a("invalid signature: left bytes after parsing");
|
|
836
|
+
return { r: i.decode(f), s: i.decode(B) };
|
|
837
|
+
},
|
|
838
|
+
hexFromSig(n) {
|
|
839
|
+
const { _tlv: a, _int: i } = e.DER, o = a.encode(2, i.encode(n.r)), h = a.encode(2, i.encode(n.s)), w = o + h;
|
|
840
|
+
return a.encode(48, w);
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
const V = BigInt(0), E = BigInt(1), b = BigInt(2), S = BigInt(3), A = BigInt(4);
|
|
844
|
+
function M(n, a) {
|
|
845
|
+
const { BYTES: i } = n;
|
|
846
|
+
let o;
|
|
847
|
+
if (typeof a == "bigint")
|
|
848
|
+
o = a;
|
|
849
|
+
else {
|
|
850
|
+
let h = (0, t.ensureBytes)("private key", a);
|
|
851
|
+
try {
|
|
852
|
+
o = n.fromBytes(h);
|
|
853
|
+
} catch {
|
|
854
|
+
throw new Error(`invalid private key: expected ui8a of size ${i}, got ${typeof a}`);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if (!n.isValidNot0(o))
|
|
858
|
+
throw new Error("invalid private key: out of range [1..N-1]");
|
|
859
|
+
return o;
|
|
860
|
+
}
|
|
861
|
+
function Q(n, a = {}) {
|
|
862
|
+
const i = (0, l._createCurveFields)("weierstrass", n, a), { Fp: o, Fn: h } = i;
|
|
863
|
+
let w = i.CURVE;
|
|
864
|
+
const { h: s, n: f } = w;
|
|
865
|
+
(0, t._validateObject)(a, {}, {
|
|
866
|
+
allowInfinityPoint: "boolean",
|
|
867
|
+
clearCofactor: "function",
|
|
868
|
+
isTorsionFree: "function",
|
|
869
|
+
fromBytes: "function",
|
|
870
|
+
toBytes: "function",
|
|
871
|
+
endo: "object",
|
|
872
|
+
wrapPrivateKey: "boolean"
|
|
873
|
+
});
|
|
874
|
+
const { endo: y } = a;
|
|
875
|
+
if (y && (!o.is0(w.a) || typeof y.beta != "bigint" || !Array.isArray(y.basises)))
|
|
876
|
+
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
877
|
+
const B = ie(o, h);
|
|
878
|
+
function T() {
|
|
879
|
+
if (!o.isOdd)
|
|
880
|
+
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
881
|
+
}
|
|
882
|
+
function k(le, Z, N) {
|
|
883
|
+
const { x: R, y: D } = Z.toAffine(), J = o.toBytes(R);
|
|
884
|
+
if ((0, t._abool2)(N, "isCompressed"), N) {
|
|
885
|
+
T();
|
|
886
|
+
const re = !o.isOdd(D);
|
|
887
|
+
return (0, t.concatBytes)(F(re), J);
|
|
888
|
+
} else
|
|
889
|
+
return (0, t.concatBytes)(Uint8Array.of(4), J, o.toBytes(D));
|
|
890
|
+
}
|
|
891
|
+
function Y(le) {
|
|
892
|
+
(0, t._abytes2)(le, void 0, "Point");
|
|
893
|
+
const { publicKey: Z, publicKeyUncompressed: N } = B, R = le.length, D = le[0], J = le.subarray(1);
|
|
894
|
+
if (R === Z && (D === 2 || D === 3)) {
|
|
895
|
+
const re = o.fromBytes(J);
|
|
896
|
+
if (!o.isValid(re))
|
|
897
|
+
throw new Error("bad point: is not on curve, wrong x");
|
|
898
|
+
const ne = C(re);
|
|
899
|
+
let ee;
|
|
900
|
+
try {
|
|
901
|
+
ee = o.sqrt(ne);
|
|
902
|
+
} catch (we) {
|
|
903
|
+
const de = we instanceof Error ? ": " + we.message : "";
|
|
904
|
+
throw new Error("bad point: is not on curve, sqrt error" + de);
|
|
905
|
+
}
|
|
906
|
+
T();
|
|
907
|
+
const oe = o.isOdd(ee);
|
|
908
|
+
return (D & 1) === 1 !== oe && (ee = o.neg(ee)), { x: re, y: ee };
|
|
909
|
+
} else if (R === N && D === 4) {
|
|
910
|
+
const re = o.BYTES, ne = o.fromBytes(J.subarray(0, re)), ee = o.fromBytes(J.subarray(re, re * 2));
|
|
911
|
+
if (!te(ne, ee))
|
|
912
|
+
throw new Error("bad point: is not on curve");
|
|
913
|
+
return { x: ne, y: ee };
|
|
914
|
+
} else
|
|
915
|
+
throw new Error(`bad point: got length ${R}, expected compressed=${Z} or uncompressed=${N}`);
|
|
916
|
+
}
|
|
917
|
+
const I = a.toBytes || k, q = a.fromBytes || Y;
|
|
918
|
+
function C(le) {
|
|
919
|
+
const Z = o.sqr(le), N = o.mul(Z, le);
|
|
920
|
+
return o.add(o.add(N, o.mul(le, w.a)), w.b);
|
|
921
|
+
}
|
|
922
|
+
function te(le, Z) {
|
|
923
|
+
const N = o.sqr(Z), R = C(le);
|
|
924
|
+
return o.eql(N, R);
|
|
925
|
+
}
|
|
926
|
+
if (!te(w.Gx, w.Gy))
|
|
927
|
+
throw new Error("bad curve params: generator point");
|
|
928
|
+
const $ = o.mul(o.pow(w.a, S), A), x = o.mul(o.sqr(w.b), BigInt(27));
|
|
929
|
+
if (o.is0(o.add($, x)))
|
|
930
|
+
throw new Error("bad curve params: a or b");
|
|
931
|
+
function X(le, Z, N = !1) {
|
|
932
|
+
if (!o.isValid(Z) || N && o.is0(Z))
|
|
933
|
+
throw new Error(`bad point coordinate ${le}`);
|
|
934
|
+
return Z;
|
|
935
|
+
}
|
|
936
|
+
function ae(le) {
|
|
937
|
+
if (!(le instanceof fe))
|
|
938
|
+
throw new Error("ProjectivePoint expected");
|
|
939
|
+
}
|
|
940
|
+
function ye(le) {
|
|
941
|
+
if (!y || !y.basises)
|
|
942
|
+
throw new Error("no endo");
|
|
943
|
+
return p(le, y.basises, h.ORDER);
|
|
944
|
+
}
|
|
945
|
+
const pe = (0, t.memoized)((le, Z) => {
|
|
946
|
+
const { X: N, Y: R, Z: D } = le;
|
|
947
|
+
if (o.eql(D, o.ONE))
|
|
948
|
+
return { x: N, y: R };
|
|
949
|
+
const J = le.is0();
|
|
950
|
+
Z == null && (Z = J ? o.ONE : o.inv(D));
|
|
951
|
+
const re = o.mul(N, Z), ne = o.mul(R, Z), ee = o.mul(D, Z);
|
|
952
|
+
if (J)
|
|
953
|
+
return { x: o.ZERO, y: o.ZERO };
|
|
954
|
+
if (!o.eql(ee, o.ONE))
|
|
955
|
+
throw new Error("invZ was invalid");
|
|
956
|
+
return { x: re, y: ne };
|
|
957
|
+
}), ve = (0, t.memoized)((le) => {
|
|
958
|
+
if (le.is0()) {
|
|
959
|
+
if (a.allowInfinityPoint && !o.is0(le.Y))
|
|
960
|
+
return;
|
|
961
|
+
throw new Error("bad point: ZERO");
|
|
962
|
+
}
|
|
963
|
+
const { x: Z, y: N } = le.toAffine();
|
|
964
|
+
if (!o.isValid(Z) || !o.isValid(N))
|
|
965
|
+
throw new Error("bad point: x or y not field elements");
|
|
966
|
+
if (!te(Z, N))
|
|
967
|
+
throw new Error("bad point: equation left != right");
|
|
968
|
+
if (!le.isTorsionFree())
|
|
969
|
+
throw new Error("bad point: not in prime-order subgroup");
|
|
970
|
+
return !0;
|
|
971
|
+
});
|
|
972
|
+
function xe(le, Z, N, R, D) {
|
|
973
|
+
return N = new fe(o.mul(N.X, le), N.Y, N.Z), Z = (0, l.negateCt)(R, Z), N = (0, l.negateCt)(D, N), Z.add(N);
|
|
974
|
+
}
|
|
975
|
+
class fe {
|
|
976
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
977
|
+
constructor(Z, N, R) {
|
|
978
|
+
this.X = X("x", Z), this.Y = X("y", N, !0), this.Z = X("z", R), Object.freeze(this);
|
|
979
|
+
}
|
|
980
|
+
static CURVE() {
|
|
981
|
+
return w;
|
|
982
|
+
}
|
|
983
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
984
|
+
static fromAffine(Z) {
|
|
985
|
+
const { x: N, y: R } = Z || {};
|
|
986
|
+
if (!Z || !o.isValid(N) || !o.isValid(R))
|
|
987
|
+
throw new Error("invalid affine point");
|
|
988
|
+
if (Z instanceof fe)
|
|
989
|
+
throw new Error("projective point not allowed");
|
|
990
|
+
return o.is0(N) && o.is0(R) ? fe.ZERO : new fe(N, R, o.ONE);
|
|
991
|
+
}
|
|
992
|
+
static fromBytes(Z) {
|
|
993
|
+
const N = fe.fromAffine(q((0, t._abytes2)(Z, void 0, "point")));
|
|
994
|
+
return N.assertValidity(), N;
|
|
995
|
+
}
|
|
996
|
+
static fromHex(Z) {
|
|
997
|
+
return fe.fromBytes((0, t.ensureBytes)("pointHex", Z));
|
|
998
|
+
}
|
|
999
|
+
get x() {
|
|
1000
|
+
return this.toAffine().x;
|
|
1001
|
+
}
|
|
1002
|
+
get y() {
|
|
1003
|
+
return this.toAffine().y;
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
*
|
|
1007
|
+
* @param windowSize
|
|
1008
|
+
* @param isLazy true will defer table computation until the first multiplication
|
|
1009
|
+
* @returns
|
|
1010
|
+
*/
|
|
1011
|
+
precompute(Z = 8, N = !0) {
|
|
1012
|
+
return Ue.createCache(this, Z), N || this.multiply(S), this;
|
|
1013
|
+
}
|
|
1014
|
+
// TODO: return `this`
|
|
1015
|
+
/** A point on curve is valid if it conforms to equation. */
|
|
1016
|
+
assertValidity() {
|
|
1017
|
+
ve(this);
|
|
1018
|
+
}
|
|
1019
|
+
hasEvenY() {
|
|
1020
|
+
const { y: Z } = this.toAffine();
|
|
1021
|
+
if (!o.isOdd)
|
|
1022
|
+
throw new Error("Field doesn't support isOdd");
|
|
1023
|
+
return !o.isOdd(Z);
|
|
1024
|
+
}
|
|
1025
|
+
/** Compare one point to another. */
|
|
1026
|
+
equals(Z) {
|
|
1027
|
+
ae(Z);
|
|
1028
|
+
const { X: N, Y: R, Z: D } = this, { X: J, Y: re, Z: ne } = Z, ee = o.eql(o.mul(N, ne), o.mul(J, D)), oe = o.eql(o.mul(R, ne), o.mul(re, D));
|
|
1029
|
+
return ee && oe;
|
|
1030
|
+
}
|
|
1031
|
+
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
1032
|
+
negate() {
|
|
1033
|
+
return new fe(this.X, o.neg(this.Y), this.Z);
|
|
1034
|
+
}
|
|
1035
|
+
// Renes-Costello-Batina exception-free doubling formula.
|
|
1036
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
1037
|
+
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
1038
|
+
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
1039
|
+
double() {
|
|
1040
|
+
const { a: Z, b: N } = w, R = o.mul(N, S), { X: D, Y: J, Z: re } = this;
|
|
1041
|
+
let ne = o.ZERO, ee = o.ZERO, oe = o.ZERO, ce = o.mul(D, D), we = o.mul(J, J), de = o.mul(re, re), ue = o.mul(D, J);
|
|
1042
|
+
return ue = o.add(ue, ue), oe = o.mul(D, re), oe = o.add(oe, oe), ne = o.mul(Z, oe), ee = o.mul(R, de), ee = o.add(ne, ee), ne = o.sub(we, ee), ee = o.add(we, ee), ee = o.mul(ne, ee), ne = o.mul(ue, ne), oe = o.mul(R, oe), de = o.mul(Z, de), ue = o.sub(ce, de), ue = o.mul(Z, ue), ue = o.add(ue, oe), oe = o.add(ce, ce), ce = o.add(oe, ce), ce = o.add(ce, de), ce = o.mul(ce, ue), ee = o.add(ee, ce), de = o.mul(J, re), de = o.add(de, de), ce = o.mul(de, ue), ne = o.sub(ne, ce), oe = o.mul(de, we), oe = o.add(oe, oe), oe = o.add(oe, oe), new fe(ne, ee, oe);
|
|
1043
|
+
}
|
|
1044
|
+
// Renes-Costello-Batina exception-free addition formula.
|
|
1045
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
1046
|
+
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
1047
|
+
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
1048
|
+
add(Z) {
|
|
1049
|
+
ae(Z);
|
|
1050
|
+
const { X: N, Y: R, Z: D } = this, { X: J, Y: re, Z: ne } = Z;
|
|
1051
|
+
let ee = o.ZERO, oe = o.ZERO, ce = o.ZERO;
|
|
1052
|
+
const we = w.a, de = o.mul(w.b, S);
|
|
1053
|
+
let ue = o.mul(N, J), me = o.mul(R, re), ge = o.mul(D, ne), _e = o.add(N, R), be = o.add(J, re);
|
|
1054
|
+
_e = o.mul(_e, be), be = o.add(ue, me), _e = o.sub(_e, be), be = o.add(N, D);
|
|
1055
|
+
let Ee = o.add(J, ne);
|
|
1056
|
+
return be = o.mul(be, Ee), Ee = o.add(ue, ge), be = o.sub(be, Ee), Ee = o.add(R, D), ee = o.add(re, ne), Ee = o.mul(Ee, ee), ee = o.add(me, ge), Ee = o.sub(Ee, ee), ce = o.mul(we, be), ee = o.mul(de, ge), ce = o.add(ee, ce), ee = o.sub(me, ce), ce = o.add(me, ce), oe = o.mul(ee, ce), me = o.add(ue, ue), me = o.add(me, ue), ge = o.mul(we, ge), be = o.mul(de, be), me = o.add(me, ge), ge = o.sub(ue, ge), ge = o.mul(we, ge), be = o.add(be, ge), ue = o.mul(me, be), oe = o.add(oe, ue), ue = o.mul(Ee, be), ee = o.mul(_e, ee), ee = o.sub(ee, ue), ue = o.mul(_e, me), ce = o.mul(Ee, ce), ce = o.add(ce, ue), new fe(ee, oe, ce);
|
|
1057
|
+
}
|
|
1058
|
+
subtract(Z) {
|
|
1059
|
+
return this.add(Z.negate());
|
|
1060
|
+
}
|
|
1061
|
+
is0() {
|
|
1062
|
+
return this.equals(fe.ZERO);
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Constant time multiplication.
|
|
1066
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
1067
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
1068
|
+
* Uses precomputes when available.
|
|
1069
|
+
* Uses endomorphism for Koblitz curves.
|
|
1070
|
+
* @param scalar by which the point would be multiplied
|
|
1071
|
+
* @returns New point
|
|
1072
|
+
*/
|
|
1073
|
+
multiply(Z) {
|
|
1074
|
+
const { endo: N } = a;
|
|
1075
|
+
if (!h.isValidNot0(Z))
|
|
1076
|
+
throw new Error("invalid scalar: out of range");
|
|
1077
|
+
let R, D;
|
|
1078
|
+
const J = (re) => Ue.cached(this, re, (ne) => (0, l.normalizeZ)(fe, ne));
|
|
1079
|
+
if (N) {
|
|
1080
|
+
const { k1neg: re, k1: ne, k2neg: ee, k2: oe } = ye(Z), { p: ce, f: we } = J(ne), { p: de, f: ue } = J(oe);
|
|
1081
|
+
D = we.add(ue), R = xe(N.beta, ce, de, re, ee);
|
|
1082
|
+
} else {
|
|
1083
|
+
const { p: re, f: ne } = J(Z);
|
|
1084
|
+
R = re, D = ne;
|
|
1085
|
+
}
|
|
1086
|
+
return (0, l.normalizeZ)(fe, [R, D])[0];
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1090
|
+
* It's faster, but should only be used when you don't care about
|
|
1091
|
+
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
1092
|
+
*/
|
|
1093
|
+
multiplyUnsafe(Z) {
|
|
1094
|
+
const { endo: N } = a, R = this;
|
|
1095
|
+
if (!h.isValid(Z))
|
|
1096
|
+
throw new Error("invalid scalar: out of range");
|
|
1097
|
+
if (Z === V || R.is0())
|
|
1098
|
+
return fe.ZERO;
|
|
1099
|
+
if (Z === E)
|
|
1100
|
+
return R;
|
|
1101
|
+
if (Ue.hasCache(this))
|
|
1102
|
+
return this.multiply(Z);
|
|
1103
|
+
if (N) {
|
|
1104
|
+
const { k1neg: D, k1: J, k2neg: re, k2: ne } = ye(Z), { p1: ee, p2: oe } = (0, l.mulEndoUnsafe)(fe, R, J, ne);
|
|
1105
|
+
return xe(N.beta, ee, oe, D, re);
|
|
1106
|
+
} else
|
|
1107
|
+
return Ue.unsafe(R, Z);
|
|
1108
|
+
}
|
|
1109
|
+
multiplyAndAddUnsafe(Z, N, R) {
|
|
1110
|
+
const D = this.multiplyUnsafe(N).add(Z.multiplyUnsafe(R));
|
|
1111
|
+
return D.is0() ? void 0 : D;
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* Converts Projective point to affine (x, y) coordinates.
|
|
1115
|
+
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
1116
|
+
*/
|
|
1117
|
+
toAffine(Z) {
|
|
1118
|
+
return pe(this, Z);
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
1122
|
+
* Always torsion-free for cofactor=1 curves.
|
|
1123
|
+
*/
|
|
1124
|
+
isTorsionFree() {
|
|
1125
|
+
const { isTorsionFree: Z } = a;
|
|
1126
|
+
return s === E ? !0 : Z ? Z(fe, this) : Ue.unsafe(this, f).is0();
|
|
1127
|
+
}
|
|
1128
|
+
clearCofactor() {
|
|
1129
|
+
const { clearCofactor: Z } = a;
|
|
1130
|
+
return s === E ? this : Z ? Z(fe, this) : this.multiplyUnsafe(s);
|
|
1131
|
+
}
|
|
1132
|
+
isSmallOrder() {
|
|
1133
|
+
return this.multiplyUnsafe(s).is0();
|
|
1134
|
+
}
|
|
1135
|
+
toBytes(Z = !0) {
|
|
1136
|
+
return (0, t._abool2)(Z, "isCompressed"), this.assertValidity(), I(fe, this, Z);
|
|
1137
|
+
}
|
|
1138
|
+
toHex(Z = !0) {
|
|
1139
|
+
return (0, t.bytesToHex)(this.toBytes(Z));
|
|
1140
|
+
}
|
|
1141
|
+
toString() {
|
|
1142
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1143
|
+
}
|
|
1144
|
+
// TODO: remove
|
|
1145
|
+
get px() {
|
|
1146
|
+
return this.X;
|
|
1147
|
+
}
|
|
1148
|
+
get py() {
|
|
1149
|
+
return this.X;
|
|
1150
|
+
}
|
|
1151
|
+
get pz() {
|
|
1152
|
+
return this.Z;
|
|
1153
|
+
}
|
|
1154
|
+
toRawBytes(Z = !0) {
|
|
1155
|
+
return this.toBytes(Z);
|
|
1156
|
+
}
|
|
1157
|
+
_setWindowSize(Z) {
|
|
1158
|
+
this.precompute(Z);
|
|
1159
|
+
}
|
|
1160
|
+
static normalizeZ(Z) {
|
|
1161
|
+
return (0, l.normalizeZ)(fe, Z);
|
|
1162
|
+
}
|
|
1163
|
+
static msm(Z, N) {
|
|
1164
|
+
return (0, l.pippenger)(fe, h, Z, N);
|
|
1165
|
+
}
|
|
1166
|
+
static fromPrivateKey(Z) {
|
|
1167
|
+
return fe.BASE.multiply(M(h, Z));
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
fe.BASE = new fe(w.Gx, w.Gy, o.ONE), fe.ZERO = new fe(o.ZERO, o.ONE, o.ZERO), fe.Fp = o, fe.Fn = h;
|
|
1171
|
+
const Ye = h.BITS, Ue = new l.wNAF(fe, a.endo ? Math.ceil(Ye / 2) : Ye);
|
|
1172
|
+
return fe.BASE.precompute(8), fe;
|
|
1173
|
+
}
|
|
1174
|
+
function F(n) {
|
|
1175
|
+
return Uint8Array.of(n ? 2 : 3);
|
|
1176
|
+
}
|
|
1177
|
+
function G(n, a) {
|
|
1178
|
+
const i = n.ORDER;
|
|
1179
|
+
let o = V;
|
|
1180
|
+
for (let q = i - E; q % b === V; q /= b)
|
|
1181
|
+
o += E;
|
|
1182
|
+
const h = o, w = b << h - E - E, s = w * b, f = (i - E) / s, y = (f - E) / b, B = s - E, T = w, k = n.pow(a, f), Y = n.pow(a, (f + E) / b);
|
|
1183
|
+
let I = (q, C) => {
|
|
1184
|
+
let te = k, $ = n.pow(C, B), x = n.sqr($);
|
|
1185
|
+
x = n.mul(x, C);
|
|
1186
|
+
let X = n.mul(q, x);
|
|
1187
|
+
X = n.pow(X, y), X = n.mul(X, $), $ = n.mul(X, C), x = n.mul(X, q);
|
|
1188
|
+
let ae = n.mul(x, $);
|
|
1189
|
+
X = n.pow(ae, T);
|
|
1190
|
+
let ye = n.eql(X, n.ONE);
|
|
1191
|
+
$ = n.mul(x, Y), X = n.mul(ae, te), x = n.cmov($, x, ye), ae = n.cmov(X, ae, ye);
|
|
1192
|
+
for (let pe = h; pe > E; pe--) {
|
|
1193
|
+
let ve = pe - b;
|
|
1194
|
+
ve = b << ve - E;
|
|
1195
|
+
let xe = n.pow(ae, ve);
|
|
1196
|
+
const fe = n.eql(xe, n.ONE);
|
|
1197
|
+
$ = n.mul(x, te), te = n.mul(te, te), xe = n.mul(ae, te), x = n.cmov($, x, fe), ae = n.cmov(xe, ae, fe);
|
|
1198
|
+
}
|
|
1199
|
+
return { isValid: ye, value: x };
|
|
1200
|
+
};
|
|
1201
|
+
if (n.ORDER % A === S) {
|
|
1202
|
+
const q = (n.ORDER - S) / A, C = n.sqrt(n.neg(a));
|
|
1203
|
+
I = (te, $) => {
|
|
1204
|
+
let x = n.sqr($);
|
|
1205
|
+
const X = n.mul(te, $);
|
|
1206
|
+
x = n.mul(x, X);
|
|
1207
|
+
let ae = n.pow(x, q);
|
|
1208
|
+
ae = n.mul(ae, X);
|
|
1209
|
+
const ye = n.mul(ae, C), pe = n.mul(n.sqr(ae), $), ve = n.eql(pe, te);
|
|
1210
|
+
let xe = n.cmov(ye, ae, ve);
|
|
1211
|
+
return { isValid: ve, value: xe };
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
return I;
|
|
1215
|
+
}
|
|
1216
|
+
function se(n, a) {
|
|
1217
|
+
(0, m.validateField)(n);
|
|
1218
|
+
const { A: i, B: o, Z: h } = a;
|
|
1219
|
+
if (!n.isValid(i) || !n.isValid(o) || !n.isValid(h))
|
|
1220
|
+
throw new Error("mapToCurveSimpleSWU: invalid opts");
|
|
1221
|
+
const w = G(n, h);
|
|
1222
|
+
if (!n.isOdd)
|
|
1223
|
+
throw new Error("Field does not have .isOdd()");
|
|
1224
|
+
return (s) => {
|
|
1225
|
+
let f, y, B, T, k, Y, I, q;
|
|
1226
|
+
f = n.sqr(s), f = n.mul(f, h), y = n.sqr(f), y = n.add(y, f), B = n.add(y, n.ONE), B = n.mul(B, o), T = n.cmov(h, n.neg(y), !n.eql(y, n.ZERO)), T = n.mul(T, i), y = n.sqr(B), Y = n.sqr(T), k = n.mul(Y, i), y = n.add(y, k), y = n.mul(y, B), Y = n.mul(Y, T), k = n.mul(Y, o), y = n.add(y, k), I = n.mul(f, B);
|
|
1227
|
+
const { isValid: C, value: te } = w(y, Y);
|
|
1228
|
+
q = n.mul(f, s), q = n.mul(q, te), I = n.cmov(I, B, C), q = n.cmov(q, te, C);
|
|
1229
|
+
const $ = n.isOdd(s) === n.isOdd(q);
|
|
1230
|
+
q = n.cmov(n.neg(q), q, $);
|
|
1231
|
+
const x = (0, m.FpInvertBatch)(n, [T], !0)[0];
|
|
1232
|
+
return I = n.mul(I, x), { x: I, y: q };
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
function ie(n, a) {
|
|
1236
|
+
return {
|
|
1237
|
+
secretKey: a.BYTES,
|
|
1238
|
+
publicKey: 1 + n.BYTES,
|
|
1239
|
+
publicKeyUncompressed: 1 + 2 * n.BYTES,
|
|
1240
|
+
publicKeyHasPrefix: !0,
|
|
1241
|
+
signature: 2 * a.BYTES
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
function j(n, a = {}) {
|
|
1245
|
+
const { Fn: i } = n, o = a.randomBytes || t.randomBytes, h = Object.assign(ie(n.Fp, i), { seed: (0, m.getMinHashLength)(i.ORDER) });
|
|
1246
|
+
function w(I) {
|
|
1247
|
+
try {
|
|
1248
|
+
return !!M(i, I);
|
|
1249
|
+
} catch {
|
|
1250
|
+
return !1;
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
function s(I, q) {
|
|
1254
|
+
const { publicKey: C, publicKeyUncompressed: te } = h;
|
|
1255
|
+
try {
|
|
1256
|
+
const $ = I.length;
|
|
1257
|
+
return q === !0 && $ !== C || q === !1 && $ !== te ? !1 : !!n.fromBytes(I);
|
|
1258
|
+
} catch {
|
|
1259
|
+
return !1;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
function f(I = o(h.seed)) {
|
|
1263
|
+
return (0, m.mapHashToField)((0, t._abytes2)(I, h.seed, "seed"), i.ORDER);
|
|
1264
|
+
}
|
|
1265
|
+
function y(I, q = !0) {
|
|
1266
|
+
return n.BASE.multiply(M(i, I)).toBytes(q);
|
|
1267
|
+
}
|
|
1268
|
+
function B(I) {
|
|
1269
|
+
const q = f(I);
|
|
1270
|
+
return { secretKey: q, publicKey: y(q) };
|
|
1271
|
+
}
|
|
1272
|
+
function T(I) {
|
|
1273
|
+
if (typeof I == "bigint")
|
|
1274
|
+
return !1;
|
|
1275
|
+
if (I instanceof n)
|
|
1276
|
+
return !0;
|
|
1277
|
+
const { secretKey: q, publicKey: C, publicKeyUncompressed: te } = h;
|
|
1278
|
+
if (i.allowedLengths || q === C)
|
|
1279
|
+
return;
|
|
1280
|
+
const $ = (0, t.ensureBytes)("key", I).length;
|
|
1281
|
+
return $ === C || $ === te;
|
|
1282
|
+
}
|
|
1283
|
+
function k(I, q, C = !0) {
|
|
1284
|
+
if (T(I) === !0)
|
|
1285
|
+
throw new Error("first arg must be private key");
|
|
1286
|
+
if (T(q) === !1)
|
|
1287
|
+
throw new Error("second arg must be public key");
|
|
1288
|
+
const te = M(i, I);
|
|
1289
|
+
return n.fromHex(q).multiply(te).toBytes(C);
|
|
1290
|
+
}
|
|
1291
|
+
return Object.freeze({ getPublicKey: y, getSharedSecret: k, keygen: B, Point: n, utils: {
|
|
1292
|
+
isValidSecretKey: w,
|
|
1293
|
+
isValidPublicKey: s,
|
|
1294
|
+
randomSecretKey: f,
|
|
1295
|
+
// TODO: remove
|
|
1296
|
+
isValidPrivateKey: w,
|
|
1297
|
+
randomPrivateKey: f,
|
|
1298
|
+
normPrivateKeyToScalar: (I) => M(i, I),
|
|
1299
|
+
precompute(I = 8, q = n.BASE) {
|
|
1300
|
+
return q.precompute(I, !1);
|
|
1301
|
+
}
|
|
1302
|
+
}, lengths: h });
|
|
1303
|
+
}
|
|
1304
|
+
function O(n, a, i = {}) {
|
|
1305
|
+
(0, c.ahash)(a), (0, t._validateObject)(i, {}, {
|
|
1306
|
+
hmac: "function",
|
|
1307
|
+
lowS: "boolean",
|
|
1308
|
+
randomBytes: "function",
|
|
1309
|
+
bits2int: "function",
|
|
1310
|
+
bits2int_modN: "function"
|
|
1311
|
+
});
|
|
1312
|
+
const o = i.randomBytes || t.randomBytes, h = i.hmac || ((N, ...R) => (0, r.hmac)(a, N, (0, t.concatBytes)(...R))), { Fp: w, Fn: s } = n, { ORDER: f, BITS: y } = s, { keygen: B, getPublicKey: T, getSharedSecret: k, utils: Y, lengths: I } = j(n, i), q = {
|
|
1313
|
+
prehash: !1,
|
|
1314
|
+
lowS: typeof i.lowS == "boolean" ? i.lowS : !1,
|
|
1315
|
+
format: void 0,
|
|
1316
|
+
//'compact' as ECDSASigFormat,
|
|
1317
|
+
extraEntropy: !1
|
|
1318
|
+
}, C = "compact";
|
|
1319
|
+
function te(N) {
|
|
1320
|
+
const R = f >> E;
|
|
1321
|
+
return N > R;
|
|
1322
|
+
}
|
|
1323
|
+
function $(N, R) {
|
|
1324
|
+
if (!s.isValidNot0(R))
|
|
1325
|
+
throw new Error(`invalid signature ${N}: out of range 1..Point.Fn.ORDER`);
|
|
1326
|
+
return R;
|
|
1327
|
+
}
|
|
1328
|
+
function x(N, R) {
|
|
1329
|
+
v(R);
|
|
1330
|
+
const D = I.signature, J = R === "compact" ? D : R === "recovered" ? D + 1 : void 0;
|
|
1331
|
+
return (0, t._abytes2)(N, J, `${R} signature`);
|
|
1332
|
+
}
|
|
1333
|
+
class X {
|
|
1334
|
+
constructor(R, D, J) {
|
|
1335
|
+
this.r = $("r", R), this.s = $("s", D), J != null && (this.recovery = J), Object.freeze(this);
|
|
1336
|
+
}
|
|
1337
|
+
static fromBytes(R, D = C) {
|
|
1338
|
+
x(R, D);
|
|
1339
|
+
let J;
|
|
1340
|
+
if (D === "der") {
|
|
1341
|
+
const { r: oe, s: ce } = e.DER.toSig((0, t._abytes2)(R));
|
|
1342
|
+
return new X(oe, ce);
|
|
1343
|
+
}
|
|
1344
|
+
D === "recovered" && (J = R[0], D = "compact", R = R.subarray(1));
|
|
1345
|
+
const re = s.BYTES, ne = R.subarray(0, re), ee = R.subarray(re, re * 2);
|
|
1346
|
+
return new X(s.fromBytes(ne), s.fromBytes(ee), J);
|
|
1347
|
+
}
|
|
1348
|
+
static fromHex(R, D) {
|
|
1349
|
+
return this.fromBytes((0, t.hexToBytes)(R), D);
|
|
1350
|
+
}
|
|
1351
|
+
addRecoveryBit(R) {
|
|
1352
|
+
return new X(this.r, this.s, R);
|
|
1353
|
+
}
|
|
1354
|
+
recoverPublicKey(R) {
|
|
1355
|
+
const D = w.ORDER, { r: J, s: re, recovery: ne } = this;
|
|
1356
|
+
if (ne == null || ![0, 1, 2, 3].includes(ne))
|
|
1357
|
+
throw new Error("recovery id invalid");
|
|
1358
|
+
if (f * b < D && ne > 1)
|
|
1359
|
+
throw new Error("recovery id is ambiguous for h>1 curve");
|
|
1360
|
+
const oe = ne === 2 || ne === 3 ? J + f : J;
|
|
1361
|
+
if (!w.isValid(oe))
|
|
1362
|
+
throw new Error("recovery id 2 or 3 invalid");
|
|
1363
|
+
const ce = w.toBytes(oe), we = n.fromBytes((0, t.concatBytes)(F((ne & 1) === 0), ce)), de = s.inv(oe), ue = ye((0, t.ensureBytes)("msgHash", R)), me = s.create(-ue * de), ge = s.create(re * de), _e = n.BASE.multiplyUnsafe(me).add(we.multiplyUnsafe(ge));
|
|
1364
|
+
if (_e.is0())
|
|
1365
|
+
throw new Error("point at infinify");
|
|
1366
|
+
return _e.assertValidity(), _e;
|
|
1367
|
+
}
|
|
1368
|
+
// Signatures should be low-s, to prevent malleability.
|
|
1369
|
+
hasHighS() {
|
|
1370
|
+
return te(this.s);
|
|
1371
|
+
}
|
|
1372
|
+
toBytes(R = C) {
|
|
1373
|
+
if (v(R), R === "der")
|
|
1374
|
+
return (0, t.hexToBytes)(e.DER.hexFromSig(this));
|
|
1375
|
+
const D = s.toBytes(this.r), J = s.toBytes(this.s);
|
|
1376
|
+
if (R === "recovered") {
|
|
1377
|
+
if (this.recovery == null)
|
|
1378
|
+
throw new Error("recovery bit must be present");
|
|
1379
|
+
return (0, t.concatBytes)(Uint8Array.of(this.recovery), D, J);
|
|
1380
|
+
}
|
|
1381
|
+
return (0, t.concatBytes)(D, J);
|
|
1382
|
+
}
|
|
1383
|
+
toHex(R) {
|
|
1384
|
+
return (0, t.bytesToHex)(this.toBytes(R));
|
|
1385
|
+
}
|
|
1386
|
+
// TODO: remove
|
|
1387
|
+
assertValidity() {
|
|
1388
|
+
}
|
|
1389
|
+
static fromCompact(R) {
|
|
1390
|
+
return X.fromBytes((0, t.ensureBytes)("sig", R), "compact");
|
|
1391
|
+
}
|
|
1392
|
+
static fromDER(R) {
|
|
1393
|
+
return X.fromBytes((0, t.ensureBytes)("sig", R), "der");
|
|
1394
|
+
}
|
|
1395
|
+
normalizeS() {
|
|
1396
|
+
return this.hasHighS() ? new X(this.r, s.neg(this.s), this.recovery) : this;
|
|
1397
|
+
}
|
|
1398
|
+
toDERRawBytes() {
|
|
1399
|
+
return this.toBytes("der");
|
|
1400
|
+
}
|
|
1401
|
+
toDERHex() {
|
|
1402
|
+
return (0, t.bytesToHex)(this.toBytes("der"));
|
|
1403
|
+
}
|
|
1404
|
+
toCompactRawBytes() {
|
|
1405
|
+
return this.toBytes("compact");
|
|
1406
|
+
}
|
|
1407
|
+
toCompactHex() {
|
|
1408
|
+
return (0, t.bytesToHex)(this.toBytes("compact"));
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
const ae = i.bits2int || function(R) {
|
|
1412
|
+
if (R.length > 8192)
|
|
1413
|
+
throw new Error("input is too large");
|
|
1414
|
+
const D = (0, t.bytesToNumberBE)(R), J = R.length * 8 - y;
|
|
1415
|
+
return J > 0 ? D >> BigInt(J) : D;
|
|
1416
|
+
}, ye = i.bits2int_modN || function(R) {
|
|
1417
|
+
return s.create(ae(R));
|
|
1418
|
+
}, pe = (0, t.bitMask)(y);
|
|
1419
|
+
function ve(N) {
|
|
1420
|
+
return (0, t.aInRange)("num < 2^" + y, N, V, pe), s.toBytes(N);
|
|
1421
|
+
}
|
|
1422
|
+
function xe(N, R) {
|
|
1423
|
+
return (0, t._abytes2)(N, void 0, "message"), R ? (0, t._abytes2)(a(N), void 0, "prehashed message") : N;
|
|
1424
|
+
}
|
|
1425
|
+
function fe(N, R, D) {
|
|
1426
|
+
if (["recovered", "canonical"].some((me) => me in D))
|
|
1427
|
+
throw new Error("sign() legacy options not supported");
|
|
1428
|
+
const { lowS: J, prehash: re, extraEntropy: ne } = U(D, q);
|
|
1429
|
+
N = xe(N, re);
|
|
1430
|
+
const ee = ye(N), oe = M(s, R), ce = [ve(oe), ve(ee)];
|
|
1431
|
+
if (ne != null && ne !== !1) {
|
|
1432
|
+
const me = ne === !0 ? o(I.secretKey) : ne;
|
|
1433
|
+
ce.push((0, t.ensureBytes)("extraEntropy", me));
|
|
1434
|
+
}
|
|
1435
|
+
const we = (0, t.concatBytes)(...ce), de = ee;
|
|
1436
|
+
function ue(me) {
|
|
1437
|
+
const ge = ae(me);
|
|
1438
|
+
if (!s.isValidNot0(ge))
|
|
1439
|
+
return;
|
|
1440
|
+
const _e = s.inv(ge), be = n.BASE.multiply(ge).toAffine(), Ee = s.create(be.x);
|
|
1441
|
+
if (Ee === V)
|
|
1442
|
+
return;
|
|
1443
|
+
const Pe = s.create(_e * s.create(de + Ee * oe));
|
|
1444
|
+
if (Pe === V)
|
|
1445
|
+
return;
|
|
1446
|
+
let pt = (be.x === Ee ? 0 : 2) | Number(be.y & E), vt = Pe;
|
|
1447
|
+
return J && te(Pe) && (vt = s.neg(Pe), pt ^= 1), new X(Ee, vt, pt);
|
|
1448
|
+
}
|
|
1449
|
+
return { seed: we, k2sig: ue };
|
|
1450
|
+
}
|
|
1451
|
+
function Ye(N, R, D = {}) {
|
|
1452
|
+
N = (0, t.ensureBytes)("message", N);
|
|
1453
|
+
const { seed: J, k2sig: re } = fe(N, R, D);
|
|
1454
|
+
return (0, t.createHmacDrbg)(a.outputLen, s.BYTES, h)(J, re);
|
|
1455
|
+
}
|
|
1456
|
+
function Ue(N) {
|
|
1457
|
+
let R;
|
|
1458
|
+
const D = typeof N == "string" || (0, t.isBytes)(N), J = !D && N !== null && typeof N == "object" && typeof N.r == "bigint" && typeof N.s == "bigint";
|
|
1459
|
+
if (!D && !J)
|
|
1460
|
+
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
1461
|
+
if (J)
|
|
1462
|
+
R = new X(N.r, N.s);
|
|
1463
|
+
else if (D) {
|
|
1464
|
+
try {
|
|
1465
|
+
R = X.fromBytes((0, t.ensureBytes)("sig", N), "der");
|
|
1466
|
+
} catch (re) {
|
|
1467
|
+
if (!(re instanceof e.DER.Err))
|
|
1468
|
+
throw re;
|
|
1469
|
+
}
|
|
1470
|
+
if (!R)
|
|
1471
|
+
try {
|
|
1472
|
+
R = X.fromBytes((0, t.ensureBytes)("sig", N), "compact");
|
|
1473
|
+
} catch {
|
|
1474
|
+
return !1;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
return R || !1;
|
|
1478
|
+
}
|
|
1479
|
+
function le(N, R, D, J = {}) {
|
|
1480
|
+
const { lowS: re, prehash: ne, format: ee } = U(J, q);
|
|
1481
|
+
if (D = (0, t.ensureBytes)("publicKey", D), R = xe((0, t.ensureBytes)("message", R), ne), "strict" in J)
|
|
1482
|
+
throw new Error("options.strict was renamed to lowS");
|
|
1483
|
+
const oe = ee === void 0 ? Ue(N) : X.fromBytes((0, t.ensureBytes)("sig", N), ee);
|
|
1484
|
+
if (oe === !1)
|
|
1485
|
+
return !1;
|
|
1486
|
+
try {
|
|
1487
|
+
const ce = n.fromBytes(D);
|
|
1488
|
+
if (re && oe.hasHighS())
|
|
1489
|
+
return !1;
|
|
1490
|
+
const { r: we, s: de } = oe, ue = ye(R), me = s.inv(de), ge = s.create(ue * me), _e = s.create(we * me), be = n.BASE.multiplyUnsafe(ge).add(ce.multiplyUnsafe(_e));
|
|
1491
|
+
return be.is0() ? !1 : s.create(be.x) === we;
|
|
1492
|
+
} catch {
|
|
1493
|
+
return !1;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
function Z(N, R, D = {}) {
|
|
1497
|
+
const { prehash: J } = U(D, q);
|
|
1498
|
+
return R = xe(R, J), X.fromBytes(N, "recovered").recoverPublicKey(R).toBytes();
|
|
1499
|
+
}
|
|
1500
|
+
return Object.freeze({
|
|
1501
|
+
keygen: B,
|
|
1502
|
+
getPublicKey: T,
|
|
1503
|
+
getSharedSecret: k,
|
|
1504
|
+
utils: Y,
|
|
1505
|
+
lengths: I,
|
|
1506
|
+
Point: n,
|
|
1507
|
+
sign: Ye,
|
|
1508
|
+
verify: le,
|
|
1509
|
+
recoverPublicKey: Z,
|
|
1510
|
+
Signature: X,
|
|
1511
|
+
hash: a
|
|
1512
|
+
});
|
|
1513
|
+
}
|
|
1514
|
+
function L(n) {
|
|
1515
|
+
const { CURVE: a, curveOpts: i } = _(n), o = Q(a, i);
|
|
1516
|
+
return z(n, o);
|
|
1517
|
+
}
|
|
1518
|
+
function _(n) {
|
|
1519
|
+
const a = {
|
|
1520
|
+
a: n.a,
|
|
1521
|
+
b: n.b,
|
|
1522
|
+
p: n.Fp.ORDER,
|
|
1523
|
+
n: n.n,
|
|
1524
|
+
h: n.h,
|
|
1525
|
+
Gx: n.Gx,
|
|
1526
|
+
Gy: n.Gy
|
|
1527
|
+
}, i = n.Fp;
|
|
1528
|
+
let o = n.allowedPrivateKeyLengths ? Array.from(new Set(n.allowedPrivateKeyLengths.map((s) => Math.ceil(s / 2)))) : void 0;
|
|
1529
|
+
const h = (0, m.Field)(a.n, {
|
|
1530
|
+
BITS: n.nBitLength,
|
|
1531
|
+
allowedLengths: o,
|
|
1532
|
+
modFromBytes: n.wrapPrivateKey
|
|
1533
|
+
}), w = {
|
|
1534
|
+
Fp: i,
|
|
1535
|
+
Fn: h,
|
|
1536
|
+
allowInfinityPoint: n.allowInfinityPoint,
|
|
1537
|
+
endo: n.endo,
|
|
1538
|
+
isTorsionFree: n.isTorsionFree,
|
|
1539
|
+
clearCofactor: n.clearCofactor,
|
|
1540
|
+
fromBytes: n.fromBytes,
|
|
1541
|
+
toBytes: n.toBytes
|
|
1542
|
+
};
|
|
1543
|
+
return { CURVE: a, curveOpts: w };
|
|
1544
|
+
}
|
|
1545
|
+
function K(n) {
|
|
1546
|
+
const { CURVE: a, curveOpts: i } = _(n), o = {
|
|
1547
|
+
hmac: n.hmac,
|
|
1548
|
+
randomBytes: n.randomBytes,
|
|
1549
|
+
lowS: n.lowS,
|
|
1550
|
+
bits2int: n.bits2int,
|
|
1551
|
+
bits2int_modN: n.bits2int_modN
|
|
1552
|
+
};
|
|
1553
|
+
return { CURVE: a, curveOpts: i, hash: n.hash, ecdsaOpts: o };
|
|
1554
|
+
}
|
|
1555
|
+
function W(n, a, i) {
|
|
1556
|
+
function o(h) {
|
|
1557
|
+
const w = n.sqr(h), s = n.mul(w, h);
|
|
1558
|
+
return n.add(n.add(s, n.mul(h, a)), i);
|
|
1559
|
+
}
|
|
1560
|
+
return o;
|
|
1561
|
+
}
|
|
1562
|
+
function z(n, a) {
|
|
1563
|
+
const { Fp: i, Fn: o } = a;
|
|
1564
|
+
function h(s) {
|
|
1565
|
+
return (0, t.inRange)(s, E, o.ORDER);
|
|
1566
|
+
}
|
|
1567
|
+
const w = W(i, n.a, n.b);
|
|
1568
|
+
return Object.assign({}, {
|
|
1569
|
+
CURVE: n,
|
|
1570
|
+
Point: a,
|
|
1571
|
+
ProjectivePoint: a,
|
|
1572
|
+
normPrivateKeyToScalar: (s) => M(o, s),
|
|
1573
|
+
weierstrassEquation: w,
|
|
1574
|
+
isWithinCurveOrder: h
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
function d(n, a) {
|
|
1578
|
+
const i = a.Point;
|
|
1579
|
+
return Object.assign({}, a, {
|
|
1580
|
+
ProjectivePoint: i,
|
|
1581
|
+
CURVE: Object.assign({}, n, (0, m.nLength)(i.Fn.ORDER, i.Fn.BITS))
|
|
1582
|
+
});
|
|
1583
|
+
}
|
|
1584
|
+
function u(n) {
|
|
1585
|
+
const { CURVE: a, curveOpts: i, hash: o, ecdsaOpts: h } = K(n), w = Q(a, i), s = O(w, o, h);
|
|
1586
|
+
return d(n, s);
|
|
1587
|
+
}
|
|
1588
|
+
})(nt)), nt;
|
|
1589
|
+
}
|
|
1590
|
+
var Ot;
|
|
1591
|
+
function En() {
|
|
1592
|
+
if (Ot) return Ke;
|
|
1593
|
+
Ot = 1, Object.defineProperty(Ke, "__esModule", { value: !0 }), Ke.getHash = r, Ke.createCurve = c;
|
|
1594
|
+
const e = /* @__PURE__ */ Ct();
|
|
1595
|
+
function r(t) {
|
|
1596
|
+
return { hash: t };
|
|
1597
|
+
}
|
|
1598
|
+
function c(t, l) {
|
|
1599
|
+
const m = (g) => (0, e.weierstrass)({ ...t, hash: g });
|
|
1600
|
+
return { ...m(l), create: m };
|
|
1601
|
+
}
|
|
1602
|
+
return Ke;
|
|
1603
|
+
}
|
|
1604
|
+
var ot = {}, Rt;
|
|
1605
|
+
function Bn() {
|
|
1606
|
+
return Rt || (Rt = 1, (function(e) {
|
|
1607
|
+
Object.defineProperty(e, "__esModule", { value: !0 }), e._DST_scalar = void 0, e.expand_message_xmd = v, e.expand_message_xof = U, e.hash_to_field = H, e.isogenyMap = V, e.createHasher = E;
|
|
1608
|
+
const r = /* @__PURE__ */ Ce(), c = /* @__PURE__ */ $e(), t = r.bytesToNumberBE;
|
|
1609
|
+
function l(b, S) {
|
|
1610
|
+
if (g(b), g(S), b < 0 || b >= 1 << 8 * S)
|
|
1611
|
+
throw new Error("invalid I2OSP input: " + b);
|
|
1612
|
+
const A = Array.from({ length: S }).fill(0);
|
|
1613
|
+
for (let M = S - 1; M >= 0; M--)
|
|
1614
|
+
A[M] = b & 255, b >>>= 8;
|
|
1615
|
+
return new Uint8Array(A);
|
|
1616
|
+
}
|
|
1617
|
+
function m(b, S) {
|
|
1618
|
+
const A = new Uint8Array(b.length);
|
|
1619
|
+
for (let M = 0; M < b.length; M++)
|
|
1620
|
+
A[M] = b[M] ^ S[M];
|
|
1621
|
+
return A;
|
|
1622
|
+
}
|
|
1623
|
+
function g(b) {
|
|
1624
|
+
if (!Number.isSafeInteger(b))
|
|
1625
|
+
throw new Error("number expected");
|
|
1626
|
+
}
|
|
1627
|
+
function p(b) {
|
|
1628
|
+
if (!(0, r.isBytes)(b) && typeof b != "string")
|
|
1629
|
+
throw new Error("DST must be Uint8Array or string");
|
|
1630
|
+
return typeof b == "string" ? (0, r.utf8ToBytes)(b) : b;
|
|
1631
|
+
}
|
|
1632
|
+
function v(b, S, A, M) {
|
|
1633
|
+
(0, r.abytes)(b), g(A), S = p(S), S.length > 255 && (S = M((0, r.concatBytes)((0, r.utf8ToBytes)("H2C-OVERSIZE-DST-"), S)));
|
|
1634
|
+
const { outputLen: Q, blockLen: F } = M, G = Math.ceil(A / Q);
|
|
1635
|
+
if (A > 65535 || G > 255)
|
|
1636
|
+
throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
1637
|
+
const se = (0, r.concatBytes)(S, l(S.length, 1)), ie = l(0, F), j = l(A, 2), O = new Array(G), L = M((0, r.concatBytes)(ie, b, j, l(0, 1), se));
|
|
1638
|
+
O[0] = M((0, r.concatBytes)(L, l(1, 1), se));
|
|
1639
|
+
for (let K = 1; K <= G; K++) {
|
|
1640
|
+
const W = [m(L, O[K - 1]), l(K + 1, 1), se];
|
|
1641
|
+
O[K] = M((0, r.concatBytes)(...W));
|
|
1642
|
+
}
|
|
1643
|
+
return (0, r.concatBytes)(...O).slice(0, A);
|
|
1644
|
+
}
|
|
1645
|
+
function U(b, S, A, M, Q) {
|
|
1646
|
+
if ((0, r.abytes)(b), g(A), S = p(S), S.length > 255) {
|
|
1647
|
+
const F = Math.ceil(2 * M / 8);
|
|
1648
|
+
S = Q.create({ dkLen: F }).update((0, r.utf8ToBytes)("H2C-OVERSIZE-DST-")).update(S).digest();
|
|
1649
|
+
}
|
|
1650
|
+
if (A > 65535 || S.length > 255)
|
|
1651
|
+
throw new Error("expand_message_xof: invalid lenInBytes");
|
|
1652
|
+
return Q.create({ dkLen: A }).update(b).update(l(A, 2)).update(S).update(l(S.length, 1)).digest();
|
|
1653
|
+
}
|
|
1654
|
+
function H(b, S, A) {
|
|
1655
|
+
(0, r._validateObject)(A, {
|
|
1656
|
+
p: "bigint",
|
|
1657
|
+
m: "number",
|
|
1658
|
+
k: "number",
|
|
1659
|
+
hash: "function"
|
|
1660
|
+
});
|
|
1661
|
+
const { p: M, k: Q, m: F, hash: G, expand: se, DST: ie } = A;
|
|
1662
|
+
if (!(0, r.isHash)(A.hash))
|
|
1663
|
+
throw new Error("expected valid hash");
|
|
1664
|
+
(0, r.abytes)(b), g(S);
|
|
1665
|
+
const j = M.toString(2).length, O = Math.ceil((j + Q) / 8), L = S * F * O;
|
|
1666
|
+
let _;
|
|
1667
|
+
if (se === "xmd")
|
|
1668
|
+
_ = v(b, ie, L, G);
|
|
1669
|
+
else if (se === "xof")
|
|
1670
|
+
_ = U(b, ie, L, Q, G);
|
|
1671
|
+
else if (se === "_internal_pass")
|
|
1672
|
+
_ = b;
|
|
1673
|
+
else
|
|
1674
|
+
throw new Error('expand must be "xmd" or "xof"');
|
|
1675
|
+
const K = new Array(S);
|
|
1676
|
+
for (let W = 0; W < S; W++) {
|
|
1677
|
+
const z = new Array(F);
|
|
1678
|
+
for (let d = 0; d < F; d++) {
|
|
1679
|
+
const u = O * (d + W * F), n = _.subarray(u, u + O);
|
|
1680
|
+
z[d] = (0, c.mod)(t(n), M);
|
|
1681
|
+
}
|
|
1682
|
+
K[W] = z;
|
|
1683
|
+
}
|
|
1684
|
+
return K;
|
|
1685
|
+
}
|
|
1686
|
+
function V(b, S) {
|
|
1687
|
+
const A = S.map((M) => Array.from(M).reverse());
|
|
1688
|
+
return (M, Q) => {
|
|
1689
|
+
const [F, G, se, ie] = A.map((L) => L.reduce((_, K) => b.add(b.mul(_, M), K))), [j, O] = (0, c.FpInvertBatch)(b, [G, ie], !0);
|
|
1690
|
+
return M = b.mul(F, j), Q = b.mul(Q, b.mul(se, O)), { x: M, y: Q };
|
|
1691
|
+
};
|
|
1692
|
+
}
|
|
1693
|
+
e._DST_scalar = (0, r.utf8ToBytes)("HashToScalar-");
|
|
1694
|
+
function E(b, S, A) {
|
|
1695
|
+
if (typeof S != "function")
|
|
1696
|
+
throw new Error("mapToCurve() must be defined");
|
|
1697
|
+
function M(F) {
|
|
1698
|
+
return b.fromAffine(S(F));
|
|
1699
|
+
}
|
|
1700
|
+
function Q(F) {
|
|
1701
|
+
const G = F.clearCofactor();
|
|
1702
|
+
return G.equals(b.ZERO) ? b.ZERO : (G.assertValidity(), G);
|
|
1703
|
+
}
|
|
1704
|
+
return {
|
|
1705
|
+
defaults: A,
|
|
1706
|
+
hashToCurve(F, G) {
|
|
1707
|
+
const se = Object.assign({}, A, G), ie = H(F, 2, se), j = M(ie[0]), O = M(ie[1]);
|
|
1708
|
+
return Q(j.add(O));
|
|
1709
|
+
},
|
|
1710
|
+
encodeToCurve(F, G) {
|
|
1711
|
+
const se = A.encodeDST ? { DST: A.encodeDST } : {}, ie = Object.assign({}, A, se, G), j = H(F, 1, ie), O = M(j[0]);
|
|
1712
|
+
return Q(O);
|
|
1713
|
+
},
|
|
1714
|
+
/** See {@link H2CHasher} */
|
|
1715
|
+
mapToCurve(F) {
|
|
1716
|
+
if (!Array.isArray(F))
|
|
1717
|
+
throw new Error("expected array of bigints");
|
|
1718
|
+
for (const G of F)
|
|
1719
|
+
if (typeof G != "bigint")
|
|
1720
|
+
throw new Error("expected array of bigints");
|
|
1721
|
+
return Q(M(F));
|
|
1722
|
+
},
|
|
1723
|
+
// hash_to_scalar can produce 0: https://www.rfc-editor.org/errata/eid8393
|
|
1724
|
+
// RFC 9380, draft-irtf-cfrg-bbs-signatures-08
|
|
1725
|
+
hashToScalar(F, G) {
|
|
1726
|
+
const se = b.Fn.ORDER, ie = Object.assign({}, A, { p: se, m: 1, DST: e._DST_scalar }, G);
|
|
1727
|
+
return H(F, 1, ie)[0][0];
|
|
1728
|
+
}
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
})(ot)), ot;
|
|
1732
|
+
}
|
|
1733
|
+
var qt;
|
|
1734
|
+
function er() {
|
|
1735
|
+
return qt || (qt = 1, (function(e) {
|
|
1736
|
+
Object.defineProperty(e, "__esModule", { value: !0 }), e.encodeToCurve = e.hashToCurve = e.secp256k1_hasher = e.schnorr = e.secp256k1 = void 0;
|
|
1737
|
+
const r = /* @__PURE__ */ dn(), c = /* @__PURE__ */ We(), t = /* @__PURE__ */ En(), l = /* @__PURE__ */ Bn(), m = /* @__PURE__ */ $e(), g = /* @__PURE__ */ Ct(), p = /* @__PURE__ */ Ce(), v = {
|
|
1738
|
+
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
1739
|
+
n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
1740
|
+
h: BigInt(1),
|
|
1741
|
+
a: BigInt(0),
|
|
1742
|
+
b: BigInt(7),
|
|
1743
|
+
Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
|
|
1744
|
+
Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
|
|
1745
|
+
}, U = {
|
|
1746
|
+
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
1747
|
+
basises: [
|
|
1748
|
+
[BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
|
|
1749
|
+
[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
|
|
1750
|
+
]
|
|
1751
|
+
}, H = /* @__PURE__ */ BigInt(0), V = /* @__PURE__ */ BigInt(1), E = /* @__PURE__ */ BigInt(2);
|
|
1752
|
+
function b(d) {
|
|
1753
|
+
const u = v.p, n = BigInt(3), a = BigInt(6), i = BigInt(11), o = BigInt(22), h = BigInt(23), w = BigInt(44), s = BigInt(88), f = d * d * d % u, y = f * f * d % u, B = (0, m.pow2)(y, n, u) * y % u, T = (0, m.pow2)(B, n, u) * y % u, k = (0, m.pow2)(T, E, u) * f % u, Y = (0, m.pow2)(k, i, u) * k % u, I = (0, m.pow2)(Y, o, u) * Y % u, q = (0, m.pow2)(I, w, u) * I % u, C = (0, m.pow2)(q, s, u) * q % u, te = (0, m.pow2)(C, w, u) * I % u, $ = (0, m.pow2)(te, n, u) * y % u, x = (0, m.pow2)($, h, u) * Y % u, X = (0, m.pow2)(x, a, u) * f % u, ae = (0, m.pow2)(X, E, u);
|
|
1754
|
+
if (!S.eql(S.sqr(ae), d))
|
|
1755
|
+
throw new Error("Cannot find square root");
|
|
1756
|
+
return ae;
|
|
1757
|
+
}
|
|
1758
|
+
const S = (0, m.Field)(v.p, { sqrt: b });
|
|
1759
|
+
e.secp256k1 = (0, t.createCurve)({ ...v, Fp: S, lowS: !0, endo: U }, r.sha256);
|
|
1760
|
+
const A = {};
|
|
1761
|
+
function M(d, ...u) {
|
|
1762
|
+
let n = A[d];
|
|
1763
|
+
if (n === void 0) {
|
|
1764
|
+
const a = (0, r.sha256)((0, p.utf8ToBytes)(d));
|
|
1765
|
+
n = (0, p.concatBytes)(a, a), A[d] = n;
|
|
1766
|
+
}
|
|
1767
|
+
return (0, r.sha256)((0, p.concatBytes)(n, ...u));
|
|
1768
|
+
}
|
|
1769
|
+
const Q = (d) => d.toBytes(!0).slice(1), F = e.secp256k1.Point, G = (d) => d % E === H;
|
|
1770
|
+
function se(d) {
|
|
1771
|
+
const { Fn: u, BASE: n } = F, a = (0, g._normFnElement)(u, d), i = n.multiply(a);
|
|
1772
|
+
return { scalar: G(i.y) ? a : u.neg(a), bytes: Q(i) };
|
|
1773
|
+
}
|
|
1774
|
+
function ie(d) {
|
|
1775
|
+
const u = S;
|
|
1776
|
+
if (!u.isValidNot0(d))
|
|
1777
|
+
throw new Error("invalid x: Fail if x ≥ p");
|
|
1778
|
+
const n = u.create(d * d), a = u.create(n * d + BigInt(7));
|
|
1779
|
+
let i = u.sqrt(a);
|
|
1780
|
+
G(i) || (i = u.neg(i));
|
|
1781
|
+
const o = F.fromAffine({ x: d, y: i });
|
|
1782
|
+
return o.assertValidity(), o;
|
|
1783
|
+
}
|
|
1784
|
+
const j = p.bytesToNumberBE;
|
|
1785
|
+
function O(...d) {
|
|
1786
|
+
return F.Fn.create(j(M("BIP0340/challenge", ...d)));
|
|
1787
|
+
}
|
|
1788
|
+
function L(d) {
|
|
1789
|
+
return se(d).bytes;
|
|
1790
|
+
}
|
|
1791
|
+
function _(d, u, n = (0, c.randomBytes)(32)) {
|
|
1792
|
+
const { Fn: a } = F, i = (0, p.ensureBytes)("message", d), { bytes: o, scalar: h } = se(u), w = (0, p.ensureBytes)("auxRand", n, 32), s = a.toBytes(h ^ j(M("BIP0340/aux", w))), f = M("BIP0340/nonce", s, o, i), { bytes: y, scalar: B } = se(f), T = O(y, o, i), k = new Uint8Array(64);
|
|
1793
|
+
if (k.set(y, 0), k.set(a.toBytes(a.create(B + T * h)), 32), !K(k, i, o))
|
|
1794
|
+
throw new Error("sign: Invalid signature produced");
|
|
1795
|
+
return k;
|
|
1796
|
+
}
|
|
1797
|
+
function K(d, u, n) {
|
|
1798
|
+
const { Fn: a, BASE: i } = F, o = (0, p.ensureBytes)("signature", d, 64), h = (0, p.ensureBytes)("message", u), w = (0, p.ensureBytes)("publicKey", n, 32);
|
|
1799
|
+
try {
|
|
1800
|
+
const s = ie(j(w)), f = j(o.subarray(0, 32));
|
|
1801
|
+
if (!(0, p.inRange)(f, V, v.p))
|
|
1802
|
+
return !1;
|
|
1803
|
+
const y = j(o.subarray(32, 64));
|
|
1804
|
+
if (!(0, p.inRange)(y, V, v.n))
|
|
1805
|
+
return !1;
|
|
1806
|
+
const B = O(a.toBytes(f), Q(s), h), T = i.multiplyUnsafe(y).add(s.multiplyUnsafe(a.neg(B))), { x: k, y: Y } = T.toAffine();
|
|
1807
|
+
return !(T.is0() || !G(Y) || k !== f);
|
|
1808
|
+
} catch {
|
|
1809
|
+
return !1;
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
e.schnorr = (() => {
|
|
1813
|
+
const n = (i = (0, c.randomBytes)(48)) => (0, m.mapHashToField)(i, v.n);
|
|
1814
|
+
e.secp256k1.utils.randomSecretKey;
|
|
1815
|
+
function a(i) {
|
|
1816
|
+
const o = n(i);
|
|
1817
|
+
return { secretKey: o, publicKey: L(o) };
|
|
1818
|
+
}
|
|
1819
|
+
return {
|
|
1820
|
+
keygen: a,
|
|
1821
|
+
getPublicKey: L,
|
|
1822
|
+
sign: _,
|
|
1823
|
+
verify: K,
|
|
1824
|
+
Point: F,
|
|
1825
|
+
utils: {
|
|
1826
|
+
randomSecretKey: n,
|
|
1827
|
+
randomPrivateKey: n,
|
|
1828
|
+
taggedHash: M,
|
|
1829
|
+
// TODO: remove
|
|
1830
|
+
lift_x: ie,
|
|
1831
|
+
pointToBytes: Q,
|
|
1832
|
+
numberToBytesBE: p.numberToBytesBE,
|
|
1833
|
+
bytesToNumberBE: p.bytesToNumberBE,
|
|
1834
|
+
mod: m.mod
|
|
1835
|
+
},
|
|
1836
|
+
lengths: {
|
|
1837
|
+
secretKey: 32,
|
|
1838
|
+
publicKey: 32,
|
|
1839
|
+
publicKeyHasPrefix: !1,
|
|
1840
|
+
signature: 64,
|
|
1841
|
+
seed: 48
|
|
1842
|
+
}
|
|
1843
|
+
};
|
|
1844
|
+
})();
|
|
1845
|
+
const W = (0, l.isogenyMap)(S, [
|
|
1846
|
+
// xNum
|
|
1847
|
+
[
|
|
1848
|
+
"0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7",
|
|
1849
|
+
"0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581",
|
|
1850
|
+
"0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262",
|
|
1851
|
+
"0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c"
|
|
1852
|
+
],
|
|
1853
|
+
// xDen
|
|
1854
|
+
[
|
|
1855
|
+
"0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b",
|
|
1856
|
+
"0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14",
|
|
1857
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
1858
|
+
// LAST 1
|
|
1859
|
+
],
|
|
1860
|
+
// yNum
|
|
1861
|
+
[
|
|
1862
|
+
"0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c",
|
|
1863
|
+
"0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3",
|
|
1864
|
+
"0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931",
|
|
1865
|
+
"0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84"
|
|
1866
|
+
],
|
|
1867
|
+
// yDen
|
|
1868
|
+
[
|
|
1869
|
+
"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b",
|
|
1870
|
+
"0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573",
|
|
1871
|
+
"0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f",
|
|
1872
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
1873
|
+
// LAST 1
|
|
1874
|
+
]
|
|
1875
|
+
].map((d) => d.map((u) => BigInt(u)))), z = (0, g.mapToCurveSimpleSWU)(S, {
|
|
1876
|
+
A: BigInt("0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533"),
|
|
1877
|
+
B: BigInt("1771"),
|
|
1878
|
+
Z: S.create(BigInt("-11"))
|
|
1879
|
+
});
|
|
1880
|
+
e.secp256k1_hasher = (0, l.createHasher)(e.secp256k1.Point, (d) => {
|
|
1881
|
+
const { x: u, y: n } = z(S.create(d[0]));
|
|
1882
|
+
return W(u, n);
|
|
1883
|
+
}, {
|
|
1884
|
+
DST: "secp256k1_XMD:SHA-256_SSWU_RO_",
|
|
1885
|
+
encodeDST: "secp256k1_XMD:SHA-256_SSWU_NU_",
|
|
1886
|
+
p: S.ORDER,
|
|
1887
|
+
m: 1,
|
|
1888
|
+
k: 128,
|
|
1889
|
+
expand: "xmd",
|
|
1890
|
+
hash: r.sha256
|
|
1891
|
+
}), e.hashToCurve = e.secp256k1_hasher.hashToCurve, e.encodeToCurve = e.secp256k1_hasher.encodeToCurve;
|
|
1892
|
+
})(tt)), tt;
|
|
1893
|
+
}
|
|
1894
|
+
var P = {}, It;
|
|
1895
|
+
function tr() {
|
|
1896
|
+
if (It) return P;
|
|
1897
|
+
It = 1, Object.defineProperty(P, "__esModule", { value: !0 }), P.isHash = P.validateObject = P.memoized = P.notImplemented = P.createHmacDrbg = P.bitMask = P.bitSet = P.bitGet = P.bitLen = P.aInRange = P.inRange = P.asciiToBytes = P.copyBytes = P.equalBytes = P.ensureBytes = P.numberToVarBytesBE = P.numberToBytesLE = P.numberToBytesBE = P.bytesToNumberLE = P.bytesToNumberBE = P.hexToNumber = P.numberToHexUnpadded = P.abool = P.utf8ToBytes = P.randomBytes = P.isBytes = P.hexToBytes = P.concatBytes = P.bytesToUtf8 = P.bytesToHex = P.anumber = P.abytes = void 0;
|
|
1898
|
+
const e = /* @__PURE__ */ Ce();
|
|
1899
|
+
return P.abytes = e.abytes, P.anumber = e.anumber, P.bytesToHex = e.bytesToHex, P.bytesToUtf8 = e.bytesToUtf8, P.concatBytes = e.concatBytes, P.hexToBytes = e.hexToBytes, P.isBytes = e.isBytes, P.randomBytes = e.randomBytes, P.utf8ToBytes = e.utf8ToBytes, P.abool = e.abool, P.numberToHexUnpadded = e.numberToHexUnpadded, P.hexToNumber = e.hexToNumber, P.bytesToNumberBE = e.bytesToNumberBE, P.bytesToNumberLE = e.bytesToNumberLE, P.numberToBytesBE = e.numberToBytesBE, P.numberToBytesLE = e.numberToBytesLE, P.numberToVarBytesBE = e.numberToVarBytesBE, P.ensureBytes = e.ensureBytes, P.equalBytes = e.equalBytes, P.copyBytes = e.copyBytes, P.asciiToBytes = e.asciiToBytes, P.inRange = e.inRange, P.aInRange = e.aInRange, P.bitLen = e.bitLen, P.bitGet = e.bitGet, P.bitSet = e.bitSet, P.bitMask = e.bitMask, P.createHmacDrbg = e.createHmacDrbg, P.notImplemented = e.notImplemented, P.memoized = e.memoized, P.validateObject = e.validateObject, P.isHash = e.isHash, P;
|
|
1900
|
+
}
|
|
1901
|
+
function nr(e, r = "") {
|
|
1902
|
+
if (typeof e != "boolean") {
|
|
1903
|
+
const c = r && `"${r}" `;
|
|
1904
|
+
throw new Error(c + "expected boolean, got type=" + typeof e);
|
|
1905
|
+
}
|
|
1906
|
+
return e;
|
|
1907
|
+
}
|
|
1908
|
+
function mt(e) {
|
|
1909
|
+
if (!Number.isSafeInteger(e) || e < 0 || e > 4294967295)
|
|
1910
|
+
throw new Error("wrong u32 integer:" + e);
|
|
1911
|
+
return e;
|
|
1912
|
+
}
|
|
1913
|
+
function Kt(e) {
|
|
1914
|
+
return mt(e), (e & e - 1) === 0 && e !== 0;
|
|
1915
|
+
}
|
|
1916
|
+
function pn(e, r) {
|
|
1917
|
+
mt(e);
|
|
1918
|
+
let c = 0;
|
|
1919
|
+
for (let t = 0; t < r; t++, e >>>= 1)
|
|
1920
|
+
c = c << 1 | e & 1;
|
|
1921
|
+
return c;
|
|
1922
|
+
}
|
|
1923
|
+
function zt(e) {
|
|
1924
|
+
return mt(e), 31 - Math.clz32(e);
|
|
1925
|
+
}
|
|
1926
|
+
function Nt(e) {
|
|
1927
|
+
const r = e.length;
|
|
1928
|
+
if (r < 2 || !Kt(r))
|
|
1929
|
+
throw new Error("n must be a power of 2 and greater than 1. Got " + r);
|
|
1930
|
+
const c = zt(r);
|
|
1931
|
+
for (let t = 0; t < r; t++) {
|
|
1932
|
+
const l = pn(t, c);
|
|
1933
|
+
if (t < l) {
|
|
1934
|
+
const m = e[t];
|
|
1935
|
+
e[t] = e[l], e[l] = m;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
return e;
|
|
1939
|
+
}
|
|
1940
|
+
const rr = (e, r) => {
|
|
1941
|
+
const { N: c, roots: t, dit: l, invertButterflies: m = !1, skipStages: g = 0, brp: p = !0 } = r, v = zt(c);
|
|
1942
|
+
if (!Kt(c))
|
|
1943
|
+
throw new Error("FFT: Polynomial size should be power of two");
|
|
1944
|
+
const U = l !== m;
|
|
1945
|
+
return (H) => {
|
|
1946
|
+
if (H.length !== c)
|
|
1947
|
+
throw new Error("FFT: wrong Polynomial length");
|
|
1948
|
+
l && p && Nt(H);
|
|
1949
|
+
for (let V = 0, E = 1; V < v - g; V++) {
|
|
1950
|
+
const b = l ? V + 1 + g : v - V, S = 1 << b, A = S >> 1, M = c >> b;
|
|
1951
|
+
for (let Q = 0; Q < c; Q += S)
|
|
1952
|
+
for (let F = 0, G = E++; F < A; F++) {
|
|
1953
|
+
const se = m ? l ? c - G : G : F * M, ie = Q + F, j = Q + F + A, O = t[se], L = H[j], _ = H[ie];
|
|
1954
|
+
if (U) {
|
|
1955
|
+
const K = e.mul(L, O);
|
|
1956
|
+
H[ie] = e.add(_, K), H[j] = e.sub(_, K);
|
|
1957
|
+
} else m ? (H[ie] = e.add(L, _), H[j] = e.mul(e.sub(L, _), O)) : (H[ie] = e.add(_, L), H[j] = e.mul(e.sub(_, L), O));
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
return !l && p && Nt(H), H;
|
|
1961
|
+
};
|
|
1962
|
+
};
|
|
1963
|
+
const bt = /* @__PURE__ */ BigInt(0), lt = /* @__PURE__ */ BigInt(1);
|
|
1964
|
+
function Je(e, r = "") {
|
|
1965
|
+
if (typeof e != "boolean") {
|
|
1966
|
+
const c = r && `"${r}"`;
|
|
1967
|
+
throw new Error(c + "expected boolean, got type=" + typeof e);
|
|
1968
|
+
}
|
|
1969
|
+
return e;
|
|
1970
|
+
}
|
|
1971
|
+
function Ze(e, r, c = "") {
|
|
1972
|
+
const t = yt(e), l = e?.length, m = r !== void 0;
|
|
1973
|
+
if (!t || m && l !== r) {
|
|
1974
|
+
const g = c && `"${c}" `, p = m ? ` of length ${r}` : "", v = t ? `length=${l}` : `type=${typeof e}`;
|
|
1975
|
+
throw new Error(g + "expected Uint8Array" + p + ", got " + v);
|
|
1976
|
+
}
|
|
1977
|
+
return e;
|
|
1978
|
+
}
|
|
1979
|
+
function Xe(e) {
|
|
1980
|
+
const r = e.toString(16);
|
|
1981
|
+
return r.length & 1 ? "0" + r : r;
|
|
1982
|
+
}
|
|
1983
|
+
function Dt(e) {
|
|
1984
|
+
if (typeof e != "string")
|
|
1985
|
+
throw new Error("hex string expected, got " + typeof e);
|
|
1986
|
+
return e === "" ? bt : BigInt("0x" + e);
|
|
1987
|
+
}
|
|
1988
|
+
function et(e) {
|
|
1989
|
+
return Dt(Me(e));
|
|
1990
|
+
}
|
|
1991
|
+
function Yt(e) {
|
|
1992
|
+
return hn(e), Dt(Me(Uint8Array.from(e).reverse()));
|
|
1993
|
+
}
|
|
1994
|
+
function wt(e, r) {
|
|
1995
|
+
return Qe(e.toString(16).padStart(r * 2, "0"));
|
|
1996
|
+
}
|
|
1997
|
+
function Pt(e, r) {
|
|
1998
|
+
return wt(e, r).reverse();
|
|
1999
|
+
}
|
|
2000
|
+
function Se(e, r, c) {
|
|
2001
|
+
let t;
|
|
2002
|
+
if (typeof r == "string")
|
|
2003
|
+
try {
|
|
2004
|
+
t = Qe(r);
|
|
2005
|
+
} catch (l) {
|
|
2006
|
+
throw new Error(e + " must be hex string or Uint8Array, cause: " + l);
|
|
2007
|
+
}
|
|
2008
|
+
else if (yt(r))
|
|
2009
|
+
t = Uint8Array.from(r);
|
|
2010
|
+
else
|
|
2011
|
+
throw new Error(e + " must be hex string or Uint8Array");
|
|
2012
|
+
return t.length, t;
|
|
2013
|
+
}
|
|
2014
|
+
const st = (e) => typeof e == "bigint" && bt <= e;
|
|
2015
|
+
function vn(e, r, c) {
|
|
2016
|
+
return st(e) && st(r) && st(c) && r <= e && e < c;
|
|
2017
|
+
}
|
|
2018
|
+
function _n(e, r, c, t) {
|
|
2019
|
+
if (!vn(r, c, t))
|
|
2020
|
+
throw new Error("expected valid " + e + ": " + c + " <= n < " + t + ", got " + r);
|
|
2021
|
+
}
|
|
2022
|
+
function Xt(e) {
|
|
2023
|
+
let r;
|
|
2024
|
+
for (r = 0; e > bt; e >>= lt, r += 1)
|
|
2025
|
+
;
|
|
2026
|
+
return r;
|
|
2027
|
+
}
|
|
2028
|
+
const ze = (e) => (lt << BigInt(e)) - lt;
|
|
2029
|
+
function Sn(e, r, c) {
|
|
2030
|
+
if (typeof e != "number" || e < 2)
|
|
2031
|
+
throw new Error("hashLen must be a number");
|
|
2032
|
+
if (typeof r != "number" || r < 2)
|
|
2033
|
+
throw new Error("qByteLen must be a number");
|
|
2034
|
+
if (typeof c != "function")
|
|
2035
|
+
throw new Error("hmacFn must be a function");
|
|
2036
|
+
const t = (b) => new Uint8Array(b), l = (b) => Uint8Array.of(b);
|
|
2037
|
+
let m = t(e), g = t(e), p = 0;
|
|
2038
|
+
const v = () => {
|
|
2039
|
+
m.fill(1), g.fill(0), p = 0;
|
|
2040
|
+
}, U = (...b) => c(g, m, ...b), H = (b = t(0)) => {
|
|
2041
|
+
g = U(l(0), b), m = U(), b.length !== 0 && (g = U(l(1), b), m = U());
|
|
2042
|
+
}, V = () => {
|
|
2043
|
+
if (p++ >= 1e3)
|
|
2044
|
+
throw new Error("drbg: tried 1000 values");
|
|
2045
|
+
let b = 0;
|
|
2046
|
+
const S = [];
|
|
2047
|
+
for (; b < r; ) {
|
|
2048
|
+
m = U();
|
|
2049
|
+
const A = m.slice();
|
|
2050
|
+
S.push(A), b += m.length;
|
|
2051
|
+
}
|
|
2052
|
+
return Ae(...S);
|
|
2053
|
+
};
|
|
2054
|
+
return (b, S) => {
|
|
2055
|
+
v(), H(b);
|
|
2056
|
+
let A;
|
|
2057
|
+
for (; !(A = S(V())); )
|
|
2058
|
+
H();
|
|
2059
|
+
return v(), A;
|
|
2060
|
+
};
|
|
2061
|
+
}
|
|
2062
|
+
function gt(e, r, c = {}) {
|
|
2063
|
+
if (!e || typeof e != "object")
|
|
2064
|
+
throw new Error("expected valid options object");
|
|
2065
|
+
function t(l, m, g) {
|
|
2066
|
+
const p = e[l];
|
|
2067
|
+
if (g && p === void 0)
|
|
2068
|
+
return;
|
|
2069
|
+
const v = typeof p;
|
|
2070
|
+
if (v !== m || p === null)
|
|
2071
|
+
throw new Error(`param "${l}" is invalid: expected ${m}, got ${v}`);
|
|
2072
|
+
}
|
|
2073
|
+
Object.entries(r).forEach(([l, m]) => t(l, m, !1)), Object.entries(c).forEach(([l, m]) => t(l, m, !0));
|
|
2074
|
+
}
|
|
2075
|
+
function At(e) {
|
|
2076
|
+
const r = /* @__PURE__ */ new WeakMap();
|
|
2077
|
+
return (c, ...t) => {
|
|
2078
|
+
const l = r.get(c);
|
|
2079
|
+
if (l !== void 0)
|
|
2080
|
+
return l;
|
|
2081
|
+
const m = e(c, ...t);
|
|
2082
|
+
return r.set(c, m), m;
|
|
2083
|
+
};
|
|
2084
|
+
}
|
|
2085
|
+
const Te = BigInt(0), Be = BigInt(1), je = /* @__PURE__ */ BigInt(2), Gt = /* @__PURE__ */ BigInt(3), Wt = /* @__PURE__ */ BigInt(4), Qt = /* @__PURE__ */ BigInt(5), Tn = /* @__PURE__ */ BigInt(7), Jt = /* @__PURE__ */ BigInt(8), xn = /* @__PURE__ */ BigInt(9), Ft = /* @__PURE__ */ BigInt(16);
|
|
2086
|
+
function qe(e, r) {
|
|
2087
|
+
const c = e % r;
|
|
2088
|
+
return c >= Te ? c : r + c;
|
|
2089
|
+
}
|
|
2090
|
+
function Re(e, r, c) {
|
|
2091
|
+
let t = e;
|
|
2092
|
+
for (; r-- > Te; )
|
|
2093
|
+
t *= t, t %= c;
|
|
2094
|
+
return t;
|
|
2095
|
+
}
|
|
2096
|
+
function Ut(e, r) {
|
|
2097
|
+
if (e === Te)
|
|
2098
|
+
throw new Error("invert: expected non-zero number");
|
|
2099
|
+
if (r <= Te)
|
|
2100
|
+
throw new Error("invert: expected positive modulus, got " + r);
|
|
2101
|
+
let c = qe(e, r), t = r, l = Te, m = Be;
|
|
2102
|
+
for (; c !== Te; ) {
|
|
2103
|
+
const p = t / c, v = t % c, U = l - m * p;
|
|
2104
|
+
t = c, c = v, l = m, m = U;
|
|
2105
|
+
}
|
|
2106
|
+
if (t !== Be)
|
|
2107
|
+
throw new Error("invert: does not exist");
|
|
2108
|
+
return qe(l, r);
|
|
2109
|
+
}
|
|
2110
|
+
function Et(e, r, c) {
|
|
2111
|
+
if (!e.eql(e.sqr(r), c))
|
|
2112
|
+
throw new Error("Cannot find square root");
|
|
2113
|
+
}
|
|
2114
|
+
function $t(e, r) {
|
|
2115
|
+
const c = (e.ORDER + Be) / Wt, t = e.pow(r, c);
|
|
2116
|
+
return Et(e, t, r), t;
|
|
2117
|
+
}
|
|
2118
|
+
function On(e, r) {
|
|
2119
|
+
const c = (e.ORDER - Qt) / Jt, t = e.mul(r, je), l = e.pow(t, c), m = e.mul(r, l), g = e.mul(e.mul(m, je), l), p = e.mul(m, e.sub(g, e.ONE));
|
|
2120
|
+
return Et(e, p, r), p;
|
|
2121
|
+
}
|
|
2122
|
+
function Rn(e) {
|
|
2123
|
+
const r = De(e), c = en(e), t = c(r, r.neg(r.ONE)), l = c(r, t), m = c(r, r.neg(t)), g = (e + Tn) / Ft;
|
|
2124
|
+
return (p, v) => {
|
|
2125
|
+
let U = p.pow(v, g), H = p.mul(U, t);
|
|
2126
|
+
const V = p.mul(U, l), E = p.mul(U, m), b = p.eql(p.sqr(H), v), S = p.eql(p.sqr(V), v);
|
|
2127
|
+
U = p.cmov(U, H, b), H = p.cmov(E, V, S);
|
|
2128
|
+
const A = p.eql(p.sqr(H), v), M = p.cmov(U, H, A);
|
|
2129
|
+
return Et(p, M, v), M;
|
|
2130
|
+
};
|
|
2131
|
+
}
|
|
2132
|
+
function en(e) {
|
|
2133
|
+
if (e < Gt)
|
|
2134
|
+
throw new Error("sqrt is not defined for small field");
|
|
2135
|
+
let r = e - Be, c = 0;
|
|
2136
|
+
for (; r % je === Te; )
|
|
2137
|
+
r /= je, c++;
|
|
2138
|
+
let t = je;
|
|
2139
|
+
const l = De(e);
|
|
2140
|
+
for (; Zt(l, t) === 1; )
|
|
2141
|
+
if (t++ > 1e3)
|
|
2142
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
2143
|
+
if (c === 1)
|
|
2144
|
+
return $t;
|
|
2145
|
+
let m = l.pow(t, r);
|
|
2146
|
+
const g = (r + Be) / je;
|
|
2147
|
+
return function(v, U) {
|
|
2148
|
+
if (v.is0(U))
|
|
2149
|
+
return U;
|
|
2150
|
+
if (Zt(v, U) !== 1)
|
|
2151
|
+
throw new Error("Cannot find square root");
|
|
2152
|
+
let H = c, V = v.mul(v.ONE, m), E = v.pow(U, r), b = v.pow(U, g);
|
|
2153
|
+
for (; !v.eql(E, v.ONE); ) {
|
|
2154
|
+
if (v.is0(E))
|
|
2155
|
+
return v.ZERO;
|
|
2156
|
+
let S = 1, A = v.sqr(E);
|
|
2157
|
+
for (; !v.eql(A, v.ONE); )
|
|
2158
|
+
if (S++, A = v.sqr(A), S === H)
|
|
2159
|
+
throw new Error("Cannot find square root");
|
|
2160
|
+
const M = Be << BigInt(H - S - 1), Q = v.pow(V, M);
|
|
2161
|
+
H = S, V = v.sqr(Q), E = v.mul(E, V), b = v.mul(b, Q);
|
|
2162
|
+
}
|
|
2163
|
+
return b;
|
|
2164
|
+
};
|
|
2165
|
+
}
|
|
2166
|
+
function qn(e) {
|
|
2167
|
+
return e % Wt === Gt ? $t : e % Jt === Qt ? On : e % Ft === xn ? Rn(e) : en(e);
|
|
2168
|
+
}
|
|
2169
|
+
const In = [
|
|
2170
|
+
"create",
|
|
2171
|
+
"isValid",
|
|
2172
|
+
"is0",
|
|
2173
|
+
"neg",
|
|
2174
|
+
"inv",
|
|
2175
|
+
"sqrt",
|
|
2176
|
+
"sqr",
|
|
2177
|
+
"eql",
|
|
2178
|
+
"add",
|
|
2179
|
+
"sub",
|
|
2180
|
+
"mul",
|
|
2181
|
+
"pow",
|
|
2182
|
+
"div",
|
|
2183
|
+
"addN",
|
|
2184
|
+
"subN",
|
|
2185
|
+
"mulN",
|
|
2186
|
+
"sqrN"
|
|
2187
|
+
];
|
|
2188
|
+
function Nn(e) {
|
|
2189
|
+
const r = {
|
|
2190
|
+
ORDER: "bigint",
|
|
2191
|
+
MASK: "bigint",
|
|
2192
|
+
BYTES: "number",
|
|
2193
|
+
BITS: "number"
|
|
2194
|
+
}, c = In.reduce((t, l) => (t[l] = "function", t), r);
|
|
2195
|
+
return gt(e, c), e;
|
|
2196
|
+
}
|
|
2197
|
+
function An(e, r, c) {
|
|
2198
|
+
if (c < Te)
|
|
2199
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
2200
|
+
if (c === Te)
|
|
2201
|
+
return e.ONE;
|
|
2202
|
+
if (c === Be)
|
|
2203
|
+
return r;
|
|
2204
|
+
let t = e.ONE, l = r;
|
|
2205
|
+
for (; c > Te; )
|
|
2206
|
+
c & Be && (t = e.mul(t, l)), l = e.sqr(l), c >>= Be;
|
|
2207
|
+
return t;
|
|
2208
|
+
}
|
|
2209
|
+
function tn(e, r, c = !1) {
|
|
2210
|
+
const t = new Array(r.length).fill(c ? e.ZERO : void 0), l = r.reduce((g, p, v) => e.is0(p) ? g : (t[v] = g, e.mul(g, p)), e.ONE), m = e.inv(l);
|
|
2211
|
+
return r.reduceRight((g, p, v) => e.is0(p) ? g : (t[v] = e.mul(g, t[v]), e.mul(g, p)), m), t;
|
|
2212
|
+
}
|
|
2213
|
+
function Zt(e, r) {
|
|
2214
|
+
const c = (e.ORDER - Be) / je, t = e.pow(r, c), l = e.eql(t, e.ONE), m = e.eql(t, e.ZERO), g = e.eql(t, e.neg(e.ONE));
|
|
2215
|
+
if (!l && !m && !g)
|
|
2216
|
+
throw new Error("invalid Legendre symbol result");
|
|
2217
|
+
return l ? 1 : m ? 0 : -1;
|
|
2218
|
+
}
|
|
2219
|
+
function nn(e, r) {
|
|
2220
|
+
r !== void 0 && yn(r);
|
|
2221
|
+
const c = r !== void 0 ? r : e.toString(2).length, t = Math.ceil(c / 8);
|
|
2222
|
+
return { nBitLength: c, nByteLength: t };
|
|
2223
|
+
}
|
|
2224
|
+
function De(e, r, c = !1, t = {}) {
|
|
2225
|
+
if (e <= Te)
|
|
2226
|
+
throw new Error("invalid field: expected ORDER > 0, got " + e);
|
|
2227
|
+
let l, m, g = !1, p;
|
|
2228
|
+
if (typeof r == "object" && r != null) {
|
|
2229
|
+
if (t.sqrt || c)
|
|
2230
|
+
throw new Error("cannot specify opts in two arguments");
|
|
2231
|
+
const E = r;
|
|
2232
|
+
E.BITS && (l = E.BITS), E.sqrt && (m = E.sqrt), typeof E.isLE == "boolean" && (c = E.isLE), typeof E.modFromBytes == "boolean" && (g = E.modFromBytes), p = E.allowedLengths;
|
|
2233
|
+
} else
|
|
2234
|
+
typeof r == "number" && (l = r), t.sqrt && (m = t.sqrt);
|
|
2235
|
+
const { nBitLength: v, nByteLength: U } = nn(e, l);
|
|
2236
|
+
if (U > 2048)
|
|
2237
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
2238
|
+
let H;
|
|
2239
|
+
const V = Object.freeze({
|
|
2240
|
+
ORDER: e,
|
|
2241
|
+
isLE: c,
|
|
2242
|
+
BITS: v,
|
|
2243
|
+
BYTES: U,
|
|
2244
|
+
MASK: ze(v),
|
|
2245
|
+
ZERO: Te,
|
|
2246
|
+
ONE: Be,
|
|
2247
|
+
allowedLengths: p,
|
|
2248
|
+
create: (E) => qe(E, e),
|
|
2249
|
+
isValid: (E) => {
|
|
2250
|
+
if (typeof E != "bigint")
|
|
2251
|
+
throw new Error("invalid field element: expected bigint, got " + typeof E);
|
|
2252
|
+
return Te <= E && E < e;
|
|
2253
|
+
},
|
|
2254
|
+
is0: (E) => E === Te,
|
|
2255
|
+
// is valid and invertible
|
|
2256
|
+
isValidNot0: (E) => !V.is0(E) && V.isValid(E),
|
|
2257
|
+
isOdd: (E) => (E & Be) === Be,
|
|
2258
|
+
neg: (E) => qe(-E, e),
|
|
2259
|
+
eql: (E, b) => E === b,
|
|
2260
|
+
sqr: (E) => qe(E * E, e),
|
|
2261
|
+
add: (E, b) => qe(E + b, e),
|
|
2262
|
+
sub: (E, b) => qe(E - b, e),
|
|
2263
|
+
mul: (E, b) => qe(E * b, e),
|
|
2264
|
+
pow: (E, b) => An(V, E, b),
|
|
2265
|
+
div: (E, b) => qe(E * Ut(b, e), e),
|
|
2266
|
+
// Same as above, but doesn't normalize
|
|
2267
|
+
sqrN: (E) => E * E,
|
|
2268
|
+
addN: (E, b) => E + b,
|
|
2269
|
+
subN: (E, b) => E - b,
|
|
2270
|
+
mulN: (E, b) => E * b,
|
|
2271
|
+
inv: (E) => Ut(E, e),
|
|
2272
|
+
sqrt: m || ((E) => (H || (H = qn(e)), H(V, E))),
|
|
2273
|
+
toBytes: (E) => c ? Pt(E, U) : wt(E, U),
|
|
2274
|
+
fromBytes: (E, b = !0) => {
|
|
2275
|
+
if (p) {
|
|
2276
|
+
if (!p.includes(E.length) || E.length > U)
|
|
2277
|
+
throw new Error("Field.fromBytes: expected " + p + " bytes, got " + E.length);
|
|
2278
|
+
const A = new Uint8Array(U);
|
|
2279
|
+
A.set(E, c ? 0 : A.length - E.length), E = A;
|
|
2280
|
+
}
|
|
2281
|
+
if (E.length !== U)
|
|
2282
|
+
throw new Error("Field.fromBytes: expected " + U + " bytes, got " + E.length);
|
|
2283
|
+
let S = c ? Yt(E) : et(E);
|
|
2284
|
+
if (g && (S = qe(S, e)), !b && !V.isValid(S))
|
|
2285
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
2286
|
+
return S;
|
|
2287
|
+
},
|
|
2288
|
+
// TODO: we don't need it here, move out to separate fn
|
|
2289
|
+
invertBatch: (E) => tn(V, E),
|
|
2290
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
2291
|
+
// and it's unclear what to return in there.
|
|
2292
|
+
cmov: (E, b, S) => S ? b : E
|
|
2293
|
+
});
|
|
2294
|
+
return Object.freeze(V);
|
|
2295
|
+
}
|
|
2296
|
+
function rn(e) {
|
|
2297
|
+
if (typeof e != "bigint")
|
|
2298
|
+
throw new Error("field order must be bigint");
|
|
2299
|
+
const r = e.toString(2).length;
|
|
2300
|
+
return Math.ceil(r / 8);
|
|
2301
|
+
}
|
|
2302
|
+
function on(e) {
|
|
2303
|
+
const r = rn(e);
|
|
2304
|
+
return r + Math.ceil(r / 2);
|
|
2305
|
+
}
|
|
2306
|
+
function Un(e, r, c = !1) {
|
|
2307
|
+
const t = e.length, l = rn(r), m = on(r);
|
|
2308
|
+
if (t < 16 || t < m || t > 1024)
|
|
2309
|
+
throw new Error("expected " + m + "-1024 bytes of input, got " + t);
|
|
2310
|
+
const g = c ? Yt(e) : et(e), p = qe(g, r - Be) + Be;
|
|
2311
|
+
return c ? Pt(p, l) : wt(p, l);
|
|
2312
|
+
}
|
|
2313
|
+
const ke = BigInt(0), Le = BigInt(1);
|
|
2314
|
+
function Fe(e, r) {
|
|
2315
|
+
const c = r.negate();
|
|
2316
|
+
return e ? c : r;
|
|
2317
|
+
}
|
|
2318
|
+
function it(e, r) {
|
|
2319
|
+
const c = tn(e.Fp, r.map((t) => t.Z));
|
|
2320
|
+
return r.map((t, l) => e.fromAffine(t.toAffine(c[l])));
|
|
2321
|
+
}
|
|
2322
|
+
function sn(e, r) {
|
|
2323
|
+
if (!Number.isSafeInteger(e) || e <= 0 || e > r)
|
|
2324
|
+
throw new Error("invalid window size, expected [1.." + r + "], got W=" + e);
|
|
2325
|
+
}
|
|
2326
|
+
function ct(e, r) {
|
|
2327
|
+
sn(e, r);
|
|
2328
|
+
const c = Math.ceil(r / e) + 1, t = 2 ** (e - 1), l = 2 ** e, m = ze(e), g = BigInt(e);
|
|
2329
|
+
return { windows: c, windowSize: t, mask: m, maxNumber: l, shiftBy: g };
|
|
2330
|
+
}
|
|
2331
|
+
function jt(e, r, c) {
|
|
2332
|
+
const { windowSize: t, mask: l, maxNumber: m, shiftBy: g } = c;
|
|
2333
|
+
let p = Number(e & l), v = e >> g;
|
|
2334
|
+
p > t && (p -= m, v += Le);
|
|
2335
|
+
const U = r * t, H = U + Math.abs(p) - 1, V = p === 0, E = p < 0, b = r % 2 !== 0;
|
|
2336
|
+
return { nextN: v, offset: H, isZero: V, isNeg: E, isNegF: b, offsetF: U };
|
|
2337
|
+
}
|
|
2338
|
+
function Zn(e, r) {
|
|
2339
|
+
if (!Array.isArray(e))
|
|
2340
|
+
throw new Error("array expected");
|
|
2341
|
+
e.forEach((c, t) => {
|
|
2342
|
+
if (!(c instanceof r))
|
|
2343
|
+
throw new Error("invalid point at index " + t);
|
|
2344
|
+
});
|
|
2345
|
+
}
|
|
2346
|
+
function jn(e, r) {
|
|
2347
|
+
if (!Array.isArray(e))
|
|
2348
|
+
throw new Error("array of scalars expected");
|
|
2349
|
+
e.forEach((c, t) => {
|
|
2350
|
+
if (!r.isValid(c))
|
|
2351
|
+
throw new Error("invalid scalar at index " + t);
|
|
2352
|
+
});
|
|
2353
|
+
}
|
|
2354
|
+
const at = /* @__PURE__ */ new WeakMap(), cn = /* @__PURE__ */ new WeakMap();
|
|
2355
|
+
function ft(e) {
|
|
2356
|
+
return cn.get(e) || 1;
|
|
2357
|
+
}
|
|
2358
|
+
function Lt(e) {
|
|
2359
|
+
if (e !== ke)
|
|
2360
|
+
throw new Error("invalid wNAF");
|
|
2361
|
+
}
|
|
2362
|
+
class Ln {
|
|
2363
|
+
// Parametrized with a given Point class (not individual point)
|
|
2364
|
+
constructor(r, c) {
|
|
2365
|
+
this.BASE = r.BASE, this.ZERO = r.ZERO, this.Fn = r.Fn, this.bits = c;
|
|
2366
|
+
}
|
|
2367
|
+
// non-const time multiplication ladder
|
|
2368
|
+
_unsafeLadder(r, c, t = this.ZERO) {
|
|
2369
|
+
let l = r;
|
|
2370
|
+
for (; c > ke; )
|
|
2371
|
+
c & Le && (t = t.add(l)), l = l.double(), c >>= Le;
|
|
2372
|
+
return t;
|
|
2373
|
+
}
|
|
2374
|
+
/**
|
|
2375
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
2376
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
2377
|
+
* Number of precomputed points depends on the curve size:
|
|
2378
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
2379
|
+
* - 𝑊 is the window size
|
|
2380
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
2381
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
2382
|
+
* @param point Point instance
|
|
2383
|
+
* @param W window size
|
|
2384
|
+
* @returns precomputed point tables flattened to a single array
|
|
2385
|
+
*/
|
|
2386
|
+
precomputeWindow(r, c) {
|
|
2387
|
+
const { windows: t, windowSize: l } = ct(c, this.bits), m = [];
|
|
2388
|
+
let g = r, p = g;
|
|
2389
|
+
for (let v = 0; v < t; v++) {
|
|
2390
|
+
p = g, m.push(p);
|
|
2391
|
+
for (let U = 1; U < l; U++)
|
|
2392
|
+
p = p.add(g), m.push(p);
|
|
2393
|
+
g = p.double();
|
|
2394
|
+
}
|
|
2395
|
+
return m;
|
|
2396
|
+
}
|
|
2397
|
+
/**
|
|
2398
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2399
|
+
* More compact implementation:
|
|
2400
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
2401
|
+
* @returns real and fake (for const-time) points
|
|
2402
|
+
*/
|
|
2403
|
+
wNAF(r, c, t) {
|
|
2404
|
+
if (!this.Fn.isValid(t))
|
|
2405
|
+
throw new Error("invalid scalar");
|
|
2406
|
+
let l = this.ZERO, m = this.BASE;
|
|
2407
|
+
const g = ct(r, this.bits);
|
|
2408
|
+
for (let p = 0; p < g.windows; p++) {
|
|
2409
|
+
const { nextN: v, offset: U, isZero: H, isNeg: V, isNegF: E, offsetF: b } = jt(t, p, g);
|
|
2410
|
+
t = v, H ? m = m.add(Fe(E, c[b])) : l = l.add(Fe(V, c[U]));
|
|
2411
|
+
}
|
|
2412
|
+
return Lt(t), { p: l, f: m };
|
|
2413
|
+
}
|
|
2414
|
+
/**
|
|
2415
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2416
|
+
* @param acc accumulator point to add result of multiplication
|
|
2417
|
+
* @returns point
|
|
2418
|
+
*/
|
|
2419
|
+
wNAFUnsafe(r, c, t, l = this.ZERO) {
|
|
2420
|
+
const m = ct(r, this.bits);
|
|
2421
|
+
for (let g = 0; g < m.windows && t !== ke; g++) {
|
|
2422
|
+
const { nextN: p, offset: v, isZero: U, isNeg: H } = jt(t, g, m);
|
|
2423
|
+
if (t = p, !U) {
|
|
2424
|
+
const V = c[v];
|
|
2425
|
+
l = l.add(H ? V.negate() : V);
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
return Lt(t), l;
|
|
2429
|
+
}
|
|
2430
|
+
getPrecomputes(r, c, t) {
|
|
2431
|
+
let l = at.get(c);
|
|
2432
|
+
return l || (l = this.precomputeWindow(c, r), r !== 1 && (typeof t == "function" && (l = t(l)), at.set(c, l))), l;
|
|
2433
|
+
}
|
|
2434
|
+
cached(r, c, t) {
|
|
2435
|
+
const l = ft(r);
|
|
2436
|
+
return this.wNAF(l, this.getPrecomputes(l, r, t), c);
|
|
2437
|
+
}
|
|
2438
|
+
unsafe(r, c, t, l) {
|
|
2439
|
+
const m = ft(r);
|
|
2440
|
+
return m === 1 ? this._unsafeLadder(r, c, l) : this.wNAFUnsafe(m, this.getPrecomputes(m, r, t), c, l);
|
|
2441
|
+
}
|
|
2442
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
2443
|
+
// using windowed method. This specifies window size and
|
|
2444
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
2445
|
+
createCache(r, c) {
|
|
2446
|
+
sn(c, this.bits), cn.set(r, c), at.delete(r);
|
|
2447
|
+
}
|
|
2448
|
+
hasCache(r) {
|
|
2449
|
+
return ft(r) !== 1;
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
function Hn(e, r, c, t) {
|
|
2453
|
+
let l = r, m = e.ZERO, g = e.ZERO;
|
|
2454
|
+
for (; c > ke || t > ke; )
|
|
2455
|
+
c & Le && (m = m.add(l)), t & Le && (g = g.add(l)), l = l.double(), c >>= Le, t >>= Le;
|
|
2456
|
+
return { p1: m, p2: g };
|
|
2457
|
+
}
|
|
2458
|
+
function Mn(e, r, c, t) {
|
|
2459
|
+
Zn(c, e), jn(t, r);
|
|
2460
|
+
const l = c.length, m = t.length;
|
|
2461
|
+
if (l !== m)
|
|
2462
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
2463
|
+
const g = e.ZERO, p = Xt(BigInt(l));
|
|
2464
|
+
let v = 1;
|
|
2465
|
+
p > 12 ? v = p - 3 : p > 4 ? v = p - 2 : p > 0 && (v = 2);
|
|
2466
|
+
const U = ze(v), H = new Array(Number(U) + 1).fill(g), V = Math.floor((r.BITS - 1) / v) * v;
|
|
2467
|
+
let E = g;
|
|
2468
|
+
for (let b = V; b >= 0; b -= v) {
|
|
2469
|
+
H.fill(g);
|
|
2470
|
+
for (let A = 0; A < m; A++) {
|
|
2471
|
+
const M = t[A], Q = Number(M >> BigInt(b) & U);
|
|
2472
|
+
H[Q] = H[Q].add(c[A]);
|
|
2473
|
+
}
|
|
2474
|
+
let S = g;
|
|
2475
|
+
for (let A = H.length - 1, M = g; A > 0; A--)
|
|
2476
|
+
M = M.add(H[A]), S = S.add(M);
|
|
2477
|
+
if (E = E.add(S), b !== 0)
|
|
2478
|
+
for (let A = 0; A < v; A++)
|
|
2479
|
+
E = E.double();
|
|
2480
|
+
}
|
|
2481
|
+
return E;
|
|
2482
|
+
}
|
|
2483
|
+
function Ht(e, r, c) {
|
|
2484
|
+
if (r) {
|
|
2485
|
+
if (r.ORDER !== e)
|
|
2486
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
2487
|
+
return Nn(r), r;
|
|
2488
|
+
} else
|
|
2489
|
+
return De(e, { isLE: c });
|
|
2490
|
+
}
|
|
2491
|
+
function Vn(e, r, c = {}, t) {
|
|
2492
|
+
if (t === void 0 && (t = e === "edwards"), !r || typeof r != "object")
|
|
2493
|
+
throw new Error(`expected valid ${e} CURVE object`);
|
|
2494
|
+
for (const v of ["p", "n", "h"]) {
|
|
2495
|
+
const U = r[v];
|
|
2496
|
+
if (!(typeof U == "bigint" && U > ke))
|
|
2497
|
+
throw new Error(`CURVE.${v} must be positive bigint`);
|
|
2498
|
+
}
|
|
2499
|
+
const l = Ht(r.p, c.Fp, t), m = Ht(r.n, c.Fn, t), p = ["Gx", "Gy", "a", "b"];
|
|
2500
|
+
for (const v of p)
|
|
2501
|
+
if (!l.isValid(r[v]))
|
|
2502
|
+
throw new Error(`CURVE.${v} must be valid field element of CURVE.Fp`);
|
|
2503
|
+
return r = Object.freeze(Object.assign({}, r)), { CURVE: r, Fp: l, Fn: m };
|
|
2504
|
+
}
|
|
2505
|
+
const Mt = (e, r) => (e + (e >= 0 ? r : -r) / an) / r;
|
|
2506
|
+
function kn(e, r, c) {
|
|
2507
|
+
const [[t, l], [m, g]] = r, p = Mt(g * e, c), v = Mt(-l * e, c);
|
|
2508
|
+
let U = e - p * t - v * m, H = -p * l - v * g;
|
|
2509
|
+
const V = U < Ne, E = H < Ne;
|
|
2510
|
+
V && (U = -U), E && (H = -H);
|
|
2511
|
+
const b = ze(Math.ceil(Xt(c) / 2)) + Ve;
|
|
2512
|
+
if (U < Ne || U >= b || H < Ne || H >= b)
|
|
2513
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + e);
|
|
2514
|
+
return { k1neg: V, k1: U, k2neg: E, k2: H };
|
|
2515
|
+
}
|
|
2516
|
+
function dt(e) {
|
|
2517
|
+
if (!["compact", "recovered", "der"].includes(e))
|
|
2518
|
+
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
2519
|
+
return e;
|
|
2520
|
+
}
|
|
2521
|
+
function ut(e, r) {
|
|
2522
|
+
const c = {};
|
|
2523
|
+
for (let t of Object.keys(r))
|
|
2524
|
+
c[t] = e[t] === void 0 ? r[t] : e[t];
|
|
2525
|
+
return Je(c.lowS, "lowS"), Je(c.prehash, "prehash"), c.format !== void 0 && dt(c.format), c;
|
|
2526
|
+
}
|
|
2527
|
+
class Cn extends Error {
|
|
2528
|
+
constructor(r = "") {
|
|
2529
|
+
super(r);
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
const Ie = {
|
|
2533
|
+
// asn.1 DER encoding utils
|
|
2534
|
+
Err: Cn,
|
|
2535
|
+
// Basic building block is TLV (Tag-Length-Value)
|
|
2536
|
+
_tlv: {
|
|
2537
|
+
encode: (e, r) => {
|
|
2538
|
+
const { Err: c } = Ie;
|
|
2539
|
+
if (e < 0 || e > 256)
|
|
2540
|
+
throw new c("tlv.encode: wrong tag");
|
|
2541
|
+
if (r.length & 1)
|
|
2542
|
+
throw new c("tlv.encode: unpadded data");
|
|
2543
|
+
const t = r.length / 2, l = Xe(t);
|
|
2544
|
+
if (l.length / 2 & 128)
|
|
2545
|
+
throw new c("tlv.encode: long form length too big");
|
|
2546
|
+
const m = t > 127 ? Xe(l.length / 2 | 128) : "";
|
|
2547
|
+
return Xe(e) + m + l + r;
|
|
2548
|
+
},
|
|
2549
|
+
// v - value, l - left bytes (unparsed)
|
|
2550
|
+
decode(e, r) {
|
|
2551
|
+
const { Err: c } = Ie;
|
|
2552
|
+
let t = 0;
|
|
2553
|
+
if (e < 0 || e > 256)
|
|
2554
|
+
throw new c("tlv.encode: wrong tag");
|
|
2555
|
+
if (r.length < 2 || r[t++] !== e)
|
|
2556
|
+
throw new c("tlv.decode: wrong tlv");
|
|
2557
|
+
const l = r[t++], m = !!(l & 128);
|
|
2558
|
+
let g = 0;
|
|
2559
|
+
if (!m)
|
|
2560
|
+
g = l;
|
|
2561
|
+
else {
|
|
2562
|
+
const v = l & 127;
|
|
2563
|
+
if (!v)
|
|
2564
|
+
throw new c("tlv.decode(long): indefinite length not supported");
|
|
2565
|
+
if (v > 4)
|
|
2566
|
+
throw new c("tlv.decode(long): byte length is too big");
|
|
2567
|
+
const U = r.subarray(t, t + v);
|
|
2568
|
+
if (U.length !== v)
|
|
2569
|
+
throw new c("tlv.decode: length bytes not complete");
|
|
2570
|
+
if (U[0] === 0)
|
|
2571
|
+
throw new c("tlv.decode(long): zero leftmost byte");
|
|
2572
|
+
for (const H of U)
|
|
2573
|
+
g = g << 8 | H;
|
|
2574
|
+
if (t += v, g < 128)
|
|
2575
|
+
throw new c("tlv.decode(long): not minimal encoding");
|
|
2576
|
+
}
|
|
2577
|
+
const p = r.subarray(t, t + g);
|
|
2578
|
+
if (p.length !== g)
|
|
2579
|
+
throw new c("tlv.decode: wrong value length");
|
|
2580
|
+
return { v: p, l: r.subarray(t + g) };
|
|
2581
|
+
}
|
|
2582
|
+
},
|
|
2583
|
+
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
2584
|
+
// since we always use positive integers here. It must always be empty:
|
|
2585
|
+
// - add zero byte if exists
|
|
2586
|
+
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
2587
|
+
_int: {
|
|
2588
|
+
encode(e) {
|
|
2589
|
+
const { Err: r } = Ie;
|
|
2590
|
+
if (e < Ne)
|
|
2591
|
+
throw new r("integer: negative integers are not allowed");
|
|
2592
|
+
let c = Xe(e);
|
|
2593
|
+
if (Number.parseInt(c[0], 16) & 8 && (c = "00" + c), c.length & 1)
|
|
2594
|
+
throw new r("unexpected DER parsing assertion: unpadded hex");
|
|
2595
|
+
return c;
|
|
2596
|
+
},
|
|
2597
|
+
decode(e) {
|
|
2598
|
+
const { Err: r } = Ie;
|
|
2599
|
+
if (e[0] & 128)
|
|
2600
|
+
throw new r("invalid signature integer: negative");
|
|
2601
|
+
if (e[0] === 0 && !(e[1] & 128))
|
|
2602
|
+
throw new r("invalid signature integer: unnecessary leading zero");
|
|
2603
|
+
return et(e);
|
|
2604
|
+
}
|
|
2605
|
+
},
|
|
2606
|
+
toSig(e) {
|
|
2607
|
+
const { Err: r, _int: c, _tlv: t } = Ie, l = Se("signature", e), { v: m, l: g } = t.decode(48, l);
|
|
2608
|
+
if (g.length)
|
|
2609
|
+
throw new r("invalid signature: left bytes after parsing");
|
|
2610
|
+
const { v: p, l: v } = t.decode(2, m), { v: U, l: H } = t.decode(2, v);
|
|
2611
|
+
if (H.length)
|
|
2612
|
+
throw new r("invalid signature: left bytes after parsing");
|
|
2613
|
+
return { r: c.decode(p), s: c.decode(U) };
|
|
2614
|
+
},
|
|
2615
|
+
hexFromSig(e) {
|
|
2616
|
+
const { _tlv: r, _int: c } = Ie, t = r.encode(2, c.encode(e.r)), l = r.encode(2, c.encode(e.s)), m = t + l;
|
|
2617
|
+
return r.encode(48, m);
|
|
2618
|
+
}
|
|
2619
|
+
}, Ne = BigInt(0), Ve = BigInt(1), an = BigInt(2), Ge = BigInt(3), Kn = BigInt(4);
|
|
2620
|
+
function He(e, r) {
|
|
2621
|
+
const { BYTES: c } = e;
|
|
2622
|
+
let t;
|
|
2623
|
+
if (typeof r == "bigint")
|
|
2624
|
+
t = r;
|
|
2625
|
+
else {
|
|
2626
|
+
let l = Se("private key", r);
|
|
2627
|
+
try {
|
|
2628
|
+
t = e.fromBytes(l);
|
|
2629
|
+
} catch {
|
|
2630
|
+
throw new Error(`invalid private key: expected ui8a of size ${c}, got ${typeof r}`);
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
if (!e.isValidNot0(t))
|
|
2634
|
+
throw new Error("invalid private key: out of range [1..N-1]");
|
|
2635
|
+
return t;
|
|
2636
|
+
}
|
|
2637
|
+
function zn(e, r = {}) {
|
|
2638
|
+
const c = Vn("weierstrass", e, r), { Fp: t, Fn: l } = c;
|
|
2639
|
+
let m = c.CURVE;
|
|
2640
|
+
const { h: g, n: p } = m;
|
|
2641
|
+
gt(r, {}, {
|
|
2642
|
+
allowInfinityPoint: "boolean",
|
|
2643
|
+
clearCofactor: "function",
|
|
2644
|
+
isTorsionFree: "function",
|
|
2645
|
+
fromBytes: "function",
|
|
2646
|
+
toBytes: "function",
|
|
2647
|
+
endo: "object",
|
|
2648
|
+
wrapPrivateKey: "boolean"
|
|
2649
|
+
});
|
|
2650
|
+
const { endo: v } = r;
|
|
2651
|
+
if (v && (!t.is0(m.a) || typeof v.beta != "bigint" || !Array.isArray(v.basises)))
|
|
2652
|
+
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
2653
|
+
const U = un(t, l);
|
|
2654
|
+
function H() {
|
|
2655
|
+
if (!t.isOdd)
|
|
2656
|
+
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
2657
|
+
}
|
|
2658
|
+
function V(z, d, u) {
|
|
2659
|
+
const { x: n, y: a } = d.toAffine(), i = t.toBytes(n);
|
|
2660
|
+
if (Je(u, "isCompressed"), u) {
|
|
2661
|
+
H();
|
|
2662
|
+
const o = !t.isOdd(a);
|
|
2663
|
+
return Ae(fn(o), i);
|
|
2664
|
+
} else
|
|
2665
|
+
return Ae(Uint8Array.of(4), i, t.toBytes(a));
|
|
2666
|
+
}
|
|
2667
|
+
function E(z) {
|
|
2668
|
+
Ze(z, void 0, "Point");
|
|
2669
|
+
const { publicKey: d, publicKeyUncompressed: u } = U, n = z.length, a = z[0], i = z.subarray(1);
|
|
2670
|
+
if (n === d && (a === 2 || a === 3)) {
|
|
2671
|
+
const o = t.fromBytes(i);
|
|
2672
|
+
if (!t.isValid(o))
|
|
2673
|
+
throw new Error("bad point: is not on curve, wrong x");
|
|
2674
|
+
const h = A(o);
|
|
2675
|
+
let w;
|
|
2676
|
+
try {
|
|
2677
|
+
w = t.sqrt(h);
|
|
2678
|
+
} catch (y) {
|
|
2679
|
+
const B = y instanceof Error ? ": " + y.message : "";
|
|
2680
|
+
throw new Error("bad point: is not on curve, sqrt error" + B);
|
|
2681
|
+
}
|
|
2682
|
+
H();
|
|
2683
|
+
const s = t.isOdd(w);
|
|
2684
|
+
return (a & 1) === 1 !== s && (w = t.neg(w)), { x: o, y: w };
|
|
2685
|
+
} else if (n === u && a === 4) {
|
|
2686
|
+
const o = t.BYTES, h = t.fromBytes(i.subarray(0, o)), w = t.fromBytes(i.subarray(o, o * 2));
|
|
2687
|
+
if (!M(h, w))
|
|
2688
|
+
throw new Error("bad point: is not on curve");
|
|
2689
|
+
return { x: h, y: w };
|
|
2690
|
+
} else
|
|
2691
|
+
throw new Error(`bad point: got length ${n}, expected compressed=${d} or uncompressed=${u}`);
|
|
2692
|
+
}
|
|
2693
|
+
const b = r.toBytes || V, S = r.fromBytes || E;
|
|
2694
|
+
function A(z) {
|
|
2695
|
+
const d = t.sqr(z), u = t.mul(d, z);
|
|
2696
|
+
return t.add(t.add(u, t.mul(z, m.a)), m.b);
|
|
2697
|
+
}
|
|
2698
|
+
function M(z, d) {
|
|
2699
|
+
const u = t.sqr(d), n = A(z);
|
|
2700
|
+
return t.eql(u, n);
|
|
2701
|
+
}
|
|
2702
|
+
if (!M(m.Gx, m.Gy))
|
|
2703
|
+
throw new Error("bad curve params: generator point");
|
|
2704
|
+
const Q = t.mul(t.pow(m.a, Ge), Kn), F = t.mul(t.sqr(m.b), BigInt(27));
|
|
2705
|
+
if (t.is0(t.add(Q, F)))
|
|
2706
|
+
throw new Error("bad curve params: a or b");
|
|
2707
|
+
function G(z, d, u = !1) {
|
|
2708
|
+
if (!t.isValid(d) || u && t.is0(d))
|
|
2709
|
+
throw new Error(`bad point coordinate ${z}`);
|
|
2710
|
+
return d;
|
|
2711
|
+
}
|
|
2712
|
+
function se(z) {
|
|
2713
|
+
if (!(z instanceof _))
|
|
2714
|
+
throw new Error("ProjectivePoint expected");
|
|
2715
|
+
}
|
|
2716
|
+
function ie(z) {
|
|
2717
|
+
if (!v || !v.basises)
|
|
2718
|
+
throw new Error("no endo");
|
|
2719
|
+
return kn(z, v.basises, l.ORDER);
|
|
2720
|
+
}
|
|
2721
|
+
const j = At((z, d) => {
|
|
2722
|
+
const { X: u, Y: n, Z: a } = z;
|
|
2723
|
+
if (t.eql(a, t.ONE))
|
|
2724
|
+
return { x: u, y: n };
|
|
2725
|
+
const i = z.is0();
|
|
2726
|
+
d == null && (d = i ? t.ONE : t.inv(a));
|
|
2727
|
+
const o = t.mul(u, d), h = t.mul(n, d), w = t.mul(a, d);
|
|
2728
|
+
if (i)
|
|
2729
|
+
return { x: t.ZERO, y: t.ZERO };
|
|
2730
|
+
if (!t.eql(w, t.ONE))
|
|
2731
|
+
throw new Error("invZ was invalid");
|
|
2732
|
+
return { x: o, y: h };
|
|
2733
|
+
}), O = At((z) => {
|
|
2734
|
+
if (z.is0()) {
|
|
2735
|
+
if (r.allowInfinityPoint && !t.is0(z.Y))
|
|
2736
|
+
return;
|
|
2737
|
+
throw new Error("bad point: ZERO");
|
|
2738
|
+
}
|
|
2739
|
+
const { x: d, y: u } = z.toAffine();
|
|
2740
|
+
if (!t.isValid(d) || !t.isValid(u))
|
|
2741
|
+
throw new Error("bad point: x or y not field elements");
|
|
2742
|
+
if (!M(d, u))
|
|
2743
|
+
throw new Error("bad point: equation left != right");
|
|
2744
|
+
if (!z.isTorsionFree())
|
|
2745
|
+
throw new Error("bad point: not in prime-order subgroup");
|
|
2746
|
+
return !0;
|
|
2747
|
+
});
|
|
2748
|
+
function L(z, d, u, n, a) {
|
|
2749
|
+
return u = new _(t.mul(u.X, z), u.Y, u.Z), d = Fe(n, d), u = Fe(a, u), d.add(u);
|
|
2750
|
+
}
|
|
2751
|
+
class _ {
|
|
2752
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2753
|
+
constructor(d, u, n) {
|
|
2754
|
+
this.X = G("x", d), this.Y = G("y", u, !0), this.Z = G("z", n), Object.freeze(this);
|
|
2755
|
+
}
|
|
2756
|
+
static CURVE() {
|
|
2757
|
+
return m;
|
|
2758
|
+
}
|
|
2759
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2760
|
+
static fromAffine(d) {
|
|
2761
|
+
const { x: u, y: n } = d || {};
|
|
2762
|
+
if (!d || !t.isValid(u) || !t.isValid(n))
|
|
2763
|
+
throw new Error("invalid affine point");
|
|
2764
|
+
if (d instanceof _)
|
|
2765
|
+
throw new Error("projective point not allowed");
|
|
2766
|
+
return t.is0(u) && t.is0(n) ? _.ZERO : new _(u, n, t.ONE);
|
|
2767
|
+
}
|
|
2768
|
+
static fromBytes(d) {
|
|
2769
|
+
const u = _.fromAffine(S(Ze(d, void 0, "point")));
|
|
2770
|
+
return u.assertValidity(), u;
|
|
2771
|
+
}
|
|
2772
|
+
static fromHex(d) {
|
|
2773
|
+
return _.fromBytes(Se("pointHex", d));
|
|
2774
|
+
}
|
|
2775
|
+
get x() {
|
|
2776
|
+
return this.toAffine().x;
|
|
2777
|
+
}
|
|
2778
|
+
get y() {
|
|
2779
|
+
return this.toAffine().y;
|
|
2780
|
+
}
|
|
2781
|
+
/**
|
|
2782
|
+
*
|
|
2783
|
+
* @param windowSize
|
|
2784
|
+
* @param isLazy true will defer table computation until the first multiplication
|
|
2785
|
+
* @returns
|
|
2786
|
+
*/
|
|
2787
|
+
precompute(d = 8, u = !0) {
|
|
2788
|
+
return W.createCache(this, d), u || this.multiply(Ge), this;
|
|
2789
|
+
}
|
|
2790
|
+
// TODO: return `this`
|
|
2791
|
+
/** A point on curve is valid if it conforms to equation. */
|
|
2792
|
+
assertValidity() {
|
|
2793
|
+
O(this);
|
|
2794
|
+
}
|
|
2795
|
+
hasEvenY() {
|
|
2796
|
+
const { y: d } = this.toAffine();
|
|
2797
|
+
if (!t.isOdd)
|
|
2798
|
+
throw new Error("Field doesn't support isOdd");
|
|
2799
|
+
return !t.isOdd(d);
|
|
2800
|
+
}
|
|
2801
|
+
/** Compare one point to another. */
|
|
2802
|
+
equals(d) {
|
|
2803
|
+
se(d);
|
|
2804
|
+
const { X: u, Y: n, Z: a } = this, { X: i, Y: o, Z: h } = d, w = t.eql(t.mul(u, h), t.mul(i, a)), s = t.eql(t.mul(n, h), t.mul(o, a));
|
|
2805
|
+
return w && s;
|
|
2806
|
+
}
|
|
2807
|
+
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
2808
|
+
negate() {
|
|
2809
|
+
return new _(this.X, t.neg(this.Y), this.Z);
|
|
2810
|
+
}
|
|
2811
|
+
// Renes-Costello-Batina exception-free doubling formula.
|
|
2812
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2813
|
+
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
2814
|
+
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
2815
|
+
double() {
|
|
2816
|
+
const { a: d, b: u } = m, n = t.mul(u, Ge), { X: a, Y: i, Z: o } = this;
|
|
2817
|
+
let h = t.ZERO, w = t.ZERO, s = t.ZERO, f = t.mul(a, a), y = t.mul(i, i), B = t.mul(o, o), T = t.mul(a, i);
|
|
2818
|
+
return T = t.add(T, T), s = t.mul(a, o), s = t.add(s, s), h = t.mul(d, s), w = t.mul(n, B), w = t.add(h, w), h = t.sub(y, w), w = t.add(y, w), w = t.mul(h, w), h = t.mul(T, h), s = t.mul(n, s), B = t.mul(d, B), T = t.sub(f, B), T = t.mul(d, T), T = t.add(T, s), s = t.add(f, f), f = t.add(s, f), f = t.add(f, B), f = t.mul(f, T), w = t.add(w, f), B = t.mul(i, o), B = t.add(B, B), f = t.mul(B, T), h = t.sub(h, f), s = t.mul(B, y), s = t.add(s, s), s = t.add(s, s), new _(h, w, s);
|
|
2819
|
+
}
|
|
2820
|
+
// Renes-Costello-Batina exception-free addition formula.
|
|
2821
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2822
|
+
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
2823
|
+
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
2824
|
+
add(d) {
|
|
2825
|
+
se(d);
|
|
2826
|
+
const { X: u, Y: n, Z: a } = this, { X: i, Y: o, Z: h } = d;
|
|
2827
|
+
let w = t.ZERO, s = t.ZERO, f = t.ZERO;
|
|
2828
|
+
const y = m.a, B = t.mul(m.b, Ge);
|
|
2829
|
+
let T = t.mul(u, i), k = t.mul(n, o), Y = t.mul(a, h), I = t.add(u, n), q = t.add(i, o);
|
|
2830
|
+
I = t.mul(I, q), q = t.add(T, k), I = t.sub(I, q), q = t.add(u, a);
|
|
2831
|
+
let C = t.add(i, h);
|
|
2832
|
+
return q = t.mul(q, C), C = t.add(T, Y), q = t.sub(q, C), C = t.add(n, a), w = t.add(o, h), C = t.mul(C, w), w = t.add(k, Y), C = t.sub(C, w), f = t.mul(y, q), w = t.mul(B, Y), f = t.add(w, f), w = t.sub(k, f), f = t.add(k, f), s = t.mul(w, f), k = t.add(T, T), k = t.add(k, T), Y = t.mul(y, Y), q = t.mul(B, q), k = t.add(k, Y), Y = t.sub(T, Y), Y = t.mul(y, Y), q = t.add(q, Y), T = t.mul(k, q), s = t.add(s, T), T = t.mul(C, q), w = t.mul(I, w), w = t.sub(w, T), T = t.mul(I, k), f = t.mul(C, f), f = t.add(f, T), new _(w, s, f);
|
|
2833
|
+
}
|
|
2834
|
+
subtract(d) {
|
|
2835
|
+
return this.add(d.negate());
|
|
2836
|
+
}
|
|
2837
|
+
is0() {
|
|
2838
|
+
return this.equals(_.ZERO);
|
|
2839
|
+
}
|
|
2840
|
+
/**
|
|
2841
|
+
* Constant time multiplication.
|
|
2842
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
2843
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
2844
|
+
* Uses precomputes when available.
|
|
2845
|
+
* Uses endomorphism for Koblitz curves.
|
|
2846
|
+
* @param scalar by which the point would be multiplied
|
|
2847
|
+
* @returns New point
|
|
2848
|
+
*/
|
|
2849
|
+
multiply(d) {
|
|
2850
|
+
const { endo: u } = r;
|
|
2851
|
+
if (!l.isValidNot0(d))
|
|
2852
|
+
throw new Error("invalid scalar: out of range");
|
|
2853
|
+
let n, a;
|
|
2854
|
+
const i = (o) => W.cached(this, o, (h) => it(_, h));
|
|
2855
|
+
if (u) {
|
|
2856
|
+
const { k1neg: o, k1: h, k2neg: w, k2: s } = ie(d), { p: f, f: y } = i(h), { p: B, f: T } = i(s);
|
|
2857
|
+
a = y.add(T), n = L(u.beta, f, B, o, w);
|
|
2858
|
+
} else {
|
|
2859
|
+
const { p: o, f: h } = i(d);
|
|
2860
|
+
n = o, a = h;
|
|
2861
|
+
}
|
|
2862
|
+
return it(_, [n, a])[0];
|
|
2863
|
+
}
|
|
2864
|
+
/**
|
|
2865
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
2866
|
+
* It's faster, but should only be used when you don't care about
|
|
2867
|
+
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
2868
|
+
*/
|
|
2869
|
+
multiplyUnsafe(d) {
|
|
2870
|
+
const { endo: u } = r, n = this;
|
|
2871
|
+
if (!l.isValid(d))
|
|
2872
|
+
throw new Error("invalid scalar: out of range");
|
|
2873
|
+
if (d === Ne || n.is0())
|
|
2874
|
+
return _.ZERO;
|
|
2875
|
+
if (d === Ve)
|
|
2876
|
+
return n;
|
|
2877
|
+
if (W.hasCache(this))
|
|
2878
|
+
return this.multiply(d);
|
|
2879
|
+
if (u) {
|
|
2880
|
+
const { k1neg: a, k1: i, k2neg: o, k2: h } = ie(d), { p1: w, p2: s } = Hn(_, n, i, h);
|
|
2881
|
+
return L(u.beta, w, s, a, o);
|
|
2882
|
+
} else
|
|
2883
|
+
return W.unsafe(n, d);
|
|
2884
|
+
}
|
|
2885
|
+
multiplyAndAddUnsafe(d, u, n) {
|
|
2886
|
+
const a = this.multiplyUnsafe(u).add(d.multiplyUnsafe(n));
|
|
2887
|
+
return a.is0() ? void 0 : a;
|
|
2888
|
+
}
|
|
2889
|
+
/**
|
|
2890
|
+
* Converts Projective point to affine (x, y) coordinates.
|
|
2891
|
+
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
2892
|
+
*/
|
|
2893
|
+
toAffine(d) {
|
|
2894
|
+
return j(this, d);
|
|
2895
|
+
}
|
|
2896
|
+
/**
|
|
2897
|
+
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
2898
|
+
* Always torsion-free for cofactor=1 curves.
|
|
2899
|
+
*/
|
|
2900
|
+
isTorsionFree() {
|
|
2901
|
+
const { isTorsionFree: d } = r;
|
|
2902
|
+
return g === Ve ? !0 : d ? d(_, this) : W.unsafe(this, p).is0();
|
|
2903
|
+
}
|
|
2904
|
+
clearCofactor() {
|
|
2905
|
+
const { clearCofactor: d } = r;
|
|
2906
|
+
return g === Ve ? this : d ? d(_, this) : this.multiplyUnsafe(g);
|
|
2907
|
+
}
|
|
2908
|
+
isSmallOrder() {
|
|
2909
|
+
return this.multiplyUnsafe(g).is0();
|
|
2910
|
+
}
|
|
2911
|
+
toBytes(d = !0) {
|
|
2912
|
+
return Je(d, "isCompressed"), this.assertValidity(), b(_, this, d);
|
|
2913
|
+
}
|
|
2914
|
+
toHex(d = !0) {
|
|
2915
|
+
return Me(this.toBytes(d));
|
|
2916
|
+
}
|
|
2917
|
+
toString() {
|
|
2918
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
2919
|
+
}
|
|
2920
|
+
// TODO: remove
|
|
2921
|
+
get px() {
|
|
2922
|
+
return this.X;
|
|
2923
|
+
}
|
|
2924
|
+
get py() {
|
|
2925
|
+
return this.X;
|
|
2926
|
+
}
|
|
2927
|
+
get pz() {
|
|
2928
|
+
return this.Z;
|
|
2929
|
+
}
|
|
2930
|
+
toRawBytes(d = !0) {
|
|
2931
|
+
return this.toBytes(d);
|
|
2932
|
+
}
|
|
2933
|
+
_setWindowSize(d) {
|
|
2934
|
+
this.precompute(d);
|
|
2935
|
+
}
|
|
2936
|
+
static normalizeZ(d) {
|
|
2937
|
+
return it(_, d);
|
|
2938
|
+
}
|
|
2939
|
+
static msm(d, u) {
|
|
2940
|
+
return Mn(_, l, d, u);
|
|
2941
|
+
}
|
|
2942
|
+
static fromPrivateKey(d) {
|
|
2943
|
+
return _.BASE.multiply(He(l, d));
|
|
2944
|
+
}
|
|
2945
|
+
}
|
|
2946
|
+
_.BASE = new _(m.Gx, m.Gy, t.ONE), _.ZERO = new _(t.ZERO, t.ONE, t.ZERO), _.Fp = t, _.Fn = l;
|
|
2947
|
+
const K = l.BITS, W = new Ln(_, r.endo ? Math.ceil(K / 2) : K);
|
|
2948
|
+
return _.BASE.precompute(8), _;
|
|
2949
|
+
}
|
|
2950
|
+
function fn(e) {
|
|
2951
|
+
return Uint8Array.of(e ? 2 : 3);
|
|
2952
|
+
}
|
|
2953
|
+
function un(e, r) {
|
|
2954
|
+
return {
|
|
2955
|
+
secretKey: r.BYTES,
|
|
2956
|
+
publicKey: 1 + e.BYTES,
|
|
2957
|
+
publicKeyUncompressed: 1 + 2 * e.BYTES,
|
|
2958
|
+
publicKeyHasPrefix: !0,
|
|
2959
|
+
signature: 2 * r.BYTES
|
|
2960
|
+
};
|
|
2961
|
+
}
|
|
2962
|
+
function Dn(e, r = {}) {
|
|
2963
|
+
const { Fn: c } = e, t = r.randomBytes || kt, l = Object.assign(un(e.Fp, c), { seed: on(c.ORDER) });
|
|
2964
|
+
function m(b) {
|
|
2965
|
+
try {
|
|
2966
|
+
return !!He(c, b);
|
|
2967
|
+
} catch {
|
|
2968
|
+
return !1;
|
|
2969
|
+
}
|
|
2970
|
+
}
|
|
2971
|
+
function g(b, S) {
|
|
2972
|
+
const { publicKey: A, publicKeyUncompressed: M } = l;
|
|
2973
|
+
try {
|
|
2974
|
+
const Q = b.length;
|
|
2975
|
+
return S === !0 && Q !== A || S === !1 && Q !== M ? !1 : !!e.fromBytes(b);
|
|
2976
|
+
} catch {
|
|
2977
|
+
return !1;
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2980
|
+
function p(b = t(l.seed)) {
|
|
2981
|
+
return Un(Ze(b, l.seed, "seed"), c.ORDER);
|
|
2982
|
+
}
|
|
2983
|
+
function v(b, S = !0) {
|
|
2984
|
+
return e.BASE.multiply(He(c, b)).toBytes(S);
|
|
2985
|
+
}
|
|
2986
|
+
function U(b) {
|
|
2987
|
+
const S = p(b);
|
|
2988
|
+
return { secretKey: S, publicKey: v(S) };
|
|
2989
|
+
}
|
|
2990
|
+
function H(b) {
|
|
2991
|
+
if (typeof b == "bigint")
|
|
2992
|
+
return !1;
|
|
2993
|
+
if (b instanceof e)
|
|
2994
|
+
return !0;
|
|
2995
|
+
const { secretKey: S, publicKey: A, publicKeyUncompressed: M } = l;
|
|
2996
|
+
if (c.allowedLengths || S === A)
|
|
2997
|
+
return;
|
|
2998
|
+
const Q = Se("key", b).length;
|
|
2999
|
+
return Q === A || Q === M;
|
|
3000
|
+
}
|
|
3001
|
+
function V(b, S, A = !0) {
|
|
3002
|
+
if (H(b) === !0)
|
|
3003
|
+
throw new Error("first arg must be private key");
|
|
3004
|
+
if (H(S) === !1)
|
|
3005
|
+
throw new Error("second arg must be public key");
|
|
3006
|
+
const M = He(c, b);
|
|
3007
|
+
return e.fromHex(S).multiply(M).toBytes(A);
|
|
3008
|
+
}
|
|
3009
|
+
return Object.freeze({ getPublicKey: v, getSharedSecret: V, keygen: U, Point: e, utils: {
|
|
3010
|
+
isValidSecretKey: m,
|
|
3011
|
+
isValidPublicKey: g,
|
|
3012
|
+
randomSecretKey: p,
|
|
3013
|
+
// TODO: remove
|
|
3014
|
+
isValidPrivateKey: m,
|
|
3015
|
+
randomPrivateKey: p,
|
|
3016
|
+
normPrivateKeyToScalar: (b) => He(c, b),
|
|
3017
|
+
precompute(b = 8, S = e.BASE) {
|
|
3018
|
+
return S.precompute(b, !1);
|
|
3019
|
+
}
|
|
3020
|
+
}, lengths: l });
|
|
3021
|
+
}
|
|
3022
|
+
function Yn(e, r, c = {}) {
|
|
3023
|
+
mn(r), gt(c, {}, {
|
|
3024
|
+
hmac: "function",
|
|
3025
|
+
lowS: "boolean",
|
|
3026
|
+
randomBytes: "function",
|
|
3027
|
+
bits2int: "function",
|
|
3028
|
+
bits2int_modN: "function"
|
|
3029
|
+
});
|
|
3030
|
+
const t = c.randomBytes || kt, l = c.hmac || ((u, ...n) => bn(r, u, Ae(...n))), { Fp: m, Fn: g } = e, { ORDER: p, BITS: v } = g, { keygen: U, getPublicKey: H, getSharedSecret: V, utils: E, lengths: b } = Dn(e, c), S = {
|
|
3031
|
+
prehash: !1,
|
|
3032
|
+
lowS: typeof c.lowS == "boolean" ? c.lowS : !1,
|
|
3033
|
+
format: void 0,
|
|
3034
|
+
//'compact' as ECDSASigFormat,
|
|
3035
|
+
extraEntropy: !1
|
|
3036
|
+
}, A = "compact";
|
|
3037
|
+
function M(u) {
|
|
3038
|
+
const n = p >> Ve;
|
|
3039
|
+
return u > n;
|
|
3040
|
+
}
|
|
3041
|
+
function Q(u, n) {
|
|
3042
|
+
if (!g.isValidNot0(n))
|
|
3043
|
+
throw new Error(`invalid signature ${u}: out of range 1..Point.Fn.ORDER`);
|
|
3044
|
+
return n;
|
|
3045
|
+
}
|
|
3046
|
+
function F(u, n) {
|
|
3047
|
+
dt(n);
|
|
3048
|
+
const a = b.signature, i = n === "compact" ? a : n === "recovered" ? a + 1 : void 0;
|
|
3049
|
+
return Ze(u, i, `${n} signature`);
|
|
3050
|
+
}
|
|
3051
|
+
class G {
|
|
3052
|
+
constructor(n, a, i) {
|
|
3053
|
+
this.r = Q("r", n), this.s = Q("s", a), i != null && (this.recovery = i), Object.freeze(this);
|
|
3054
|
+
}
|
|
3055
|
+
static fromBytes(n, a = A) {
|
|
3056
|
+
F(n, a);
|
|
3057
|
+
let i;
|
|
3058
|
+
if (a === "der") {
|
|
3059
|
+
const { r: s, s: f } = Ie.toSig(Ze(n));
|
|
3060
|
+
return new G(s, f);
|
|
3061
|
+
}
|
|
3062
|
+
a === "recovered" && (i = n[0], a = "compact", n = n.subarray(1));
|
|
3063
|
+
const o = g.BYTES, h = n.subarray(0, o), w = n.subarray(o, o * 2);
|
|
3064
|
+
return new G(g.fromBytes(h), g.fromBytes(w), i);
|
|
3065
|
+
}
|
|
3066
|
+
static fromHex(n, a) {
|
|
3067
|
+
return this.fromBytes(Qe(n), a);
|
|
3068
|
+
}
|
|
3069
|
+
addRecoveryBit(n) {
|
|
3070
|
+
return new G(this.r, this.s, n);
|
|
3071
|
+
}
|
|
3072
|
+
recoverPublicKey(n) {
|
|
3073
|
+
const a = m.ORDER, { r: i, s: o, recovery: h } = this;
|
|
3074
|
+
if (h == null || ![0, 1, 2, 3].includes(h))
|
|
3075
|
+
throw new Error("recovery id invalid");
|
|
3076
|
+
if (p * an < a && h > 1)
|
|
3077
|
+
throw new Error("recovery id is ambiguous for h>1 curve");
|
|
3078
|
+
const s = h === 2 || h === 3 ? i + p : i;
|
|
3079
|
+
if (!m.isValid(s))
|
|
3080
|
+
throw new Error("recovery id 2 or 3 invalid");
|
|
3081
|
+
const f = m.toBytes(s), y = e.fromBytes(Ae(fn((h & 1) === 0), f)), B = g.inv(s), T = ie(Se("msgHash", n)), k = g.create(-T * B), Y = g.create(o * B), I = e.BASE.multiplyUnsafe(k).add(y.multiplyUnsafe(Y));
|
|
3082
|
+
if (I.is0())
|
|
3083
|
+
throw new Error("point at infinify");
|
|
3084
|
+
return I.assertValidity(), I;
|
|
3085
|
+
}
|
|
3086
|
+
// Signatures should be low-s, to prevent malleability.
|
|
3087
|
+
hasHighS() {
|
|
3088
|
+
return M(this.s);
|
|
3089
|
+
}
|
|
3090
|
+
toBytes(n = A) {
|
|
3091
|
+
if (dt(n), n === "der")
|
|
3092
|
+
return Qe(Ie.hexFromSig(this));
|
|
3093
|
+
const a = g.toBytes(this.r), i = g.toBytes(this.s);
|
|
3094
|
+
if (n === "recovered") {
|
|
3095
|
+
if (this.recovery == null)
|
|
3096
|
+
throw new Error("recovery bit must be present");
|
|
3097
|
+
return Ae(Uint8Array.of(this.recovery), a, i);
|
|
3098
|
+
}
|
|
3099
|
+
return Ae(a, i);
|
|
3100
|
+
}
|
|
3101
|
+
toHex(n) {
|
|
3102
|
+
return Me(this.toBytes(n));
|
|
3103
|
+
}
|
|
3104
|
+
// TODO: remove
|
|
3105
|
+
assertValidity() {
|
|
3106
|
+
}
|
|
3107
|
+
static fromCompact(n) {
|
|
3108
|
+
return G.fromBytes(Se("sig", n), "compact");
|
|
3109
|
+
}
|
|
3110
|
+
static fromDER(n) {
|
|
3111
|
+
return G.fromBytes(Se("sig", n), "der");
|
|
3112
|
+
}
|
|
3113
|
+
normalizeS() {
|
|
3114
|
+
return this.hasHighS() ? new G(this.r, g.neg(this.s), this.recovery) : this;
|
|
3115
|
+
}
|
|
3116
|
+
toDERRawBytes() {
|
|
3117
|
+
return this.toBytes("der");
|
|
3118
|
+
}
|
|
3119
|
+
toDERHex() {
|
|
3120
|
+
return Me(this.toBytes("der"));
|
|
3121
|
+
}
|
|
3122
|
+
toCompactRawBytes() {
|
|
3123
|
+
return this.toBytes("compact");
|
|
3124
|
+
}
|
|
3125
|
+
toCompactHex() {
|
|
3126
|
+
return Me(this.toBytes("compact"));
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
3129
|
+
const se = c.bits2int || function(n) {
|
|
3130
|
+
if (n.length > 8192)
|
|
3131
|
+
throw new Error("input is too large");
|
|
3132
|
+
const a = et(n), i = n.length * 8 - v;
|
|
3133
|
+
return i > 0 ? a >> BigInt(i) : a;
|
|
3134
|
+
}, ie = c.bits2int_modN || function(n) {
|
|
3135
|
+
return g.create(se(n));
|
|
3136
|
+
}, j = ze(v);
|
|
3137
|
+
function O(u) {
|
|
3138
|
+
return _n("num < 2^" + v, u, Ne, j), g.toBytes(u);
|
|
3139
|
+
}
|
|
3140
|
+
function L(u, n) {
|
|
3141
|
+
return Ze(u, void 0, "message"), n ? Ze(r(u), void 0, "prehashed message") : u;
|
|
3142
|
+
}
|
|
3143
|
+
function _(u, n, a) {
|
|
3144
|
+
if (["recovered", "canonical"].some((k) => k in a))
|
|
3145
|
+
throw new Error("sign() legacy options not supported");
|
|
3146
|
+
const { lowS: i, prehash: o, extraEntropy: h } = ut(a, S);
|
|
3147
|
+
u = L(u, o);
|
|
3148
|
+
const w = ie(u), s = He(g, n), f = [O(s), O(w)];
|
|
3149
|
+
if (h != null && h !== !1) {
|
|
3150
|
+
const k = h === !0 ? t(b.secretKey) : h;
|
|
3151
|
+
f.push(Se("extraEntropy", k));
|
|
3152
|
+
}
|
|
3153
|
+
const y = Ae(...f), B = w;
|
|
3154
|
+
function T(k) {
|
|
3155
|
+
const Y = se(k);
|
|
3156
|
+
if (!g.isValidNot0(Y))
|
|
3157
|
+
return;
|
|
3158
|
+
const I = g.inv(Y), q = e.BASE.multiply(Y).toAffine(), C = g.create(q.x);
|
|
3159
|
+
if (C === Ne)
|
|
3160
|
+
return;
|
|
3161
|
+
const te = g.create(I * g.create(B + C * s));
|
|
3162
|
+
if (te === Ne)
|
|
3163
|
+
return;
|
|
3164
|
+
let $ = (q.x === C ? 0 : 2) | Number(q.y & Ve), x = te;
|
|
3165
|
+
return i && M(te) && (x = g.neg(te), $ ^= 1), new G(C, x, $);
|
|
3166
|
+
}
|
|
3167
|
+
return { seed: y, k2sig: T };
|
|
3168
|
+
}
|
|
3169
|
+
function K(u, n, a = {}) {
|
|
3170
|
+
u = Se("message", u);
|
|
3171
|
+
const { seed: i, k2sig: o } = _(u, n, a);
|
|
3172
|
+
return Sn(r.outputLen, g.BYTES, l)(i, o);
|
|
3173
|
+
}
|
|
3174
|
+
function W(u) {
|
|
3175
|
+
let n;
|
|
3176
|
+
const a = typeof u == "string" || yt(u), i = !a && u !== null && typeof u == "object" && typeof u.r == "bigint" && typeof u.s == "bigint";
|
|
3177
|
+
if (!a && !i)
|
|
3178
|
+
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
3179
|
+
if (i)
|
|
3180
|
+
n = new G(u.r, u.s);
|
|
3181
|
+
else if (a) {
|
|
3182
|
+
try {
|
|
3183
|
+
n = G.fromBytes(Se("sig", u), "der");
|
|
3184
|
+
} catch (o) {
|
|
3185
|
+
if (!(o instanceof Ie.Err))
|
|
3186
|
+
throw o;
|
|
3187
|
+
}
|
|
3188
|
+
if (!n)
|
|
3189
|
+
try {
|
|
3190
|
+
n = G.fromBytes(Se("sig", u), "compact");
|
|
3191
|
+
} catch {
|
|
3192
|
+
return !1;
|
|
3193
|
+
}
|
|
3194
|
+
}
|
|
3195
|
+
return n || !1;
|
|
3196
|
+
}
|
|
3197
|
+
function z(u, n, a, i = {}) {
|
|
3198
|
+
const { lowS: o, prehash: h, format: w } = ut(i, S);
|
|
3199
|
+
if (a = Se("publicKey", a), n = L(Se("message", n), h), "strict" in i)
|
|
3200
|
+
throw new Error("options.strict was renamed to lowS");
|
|
3201
|
+
const s = w === void 0 ? W(u) : G.fromBytes(Se("sig", u), w);
|
|
3202
|
+
if (s === !1)
|
|
3203
|
+
return !1;
|
|
3204
|
+
try {
|
|
3205
|
+
const f = e.fromBytes(a);
|
|
3206
|
+
if (o && s.hasHighS())
|
|
3207
|
+
return !1;
|
|
3208
|
+
const { r: y, s: B } = s, T = ie(n), k = g.inv(B), Y = g.create(T * k), I = g.create(y * k), q = e.BASE.multiplyUnsafe(Y).add(f.multiplyUnsafe(I));
|
|
3209
|
+
return q.is0() ? !1 : g.create(q.x) === y;
|
|
3210
|
+
} catch {
|
|
3211
|
+
return !1;
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
function d(u, n, a = {}) {
|
|
3215
|
+
const { prehash: i } = ut(a, S);
|
|
3216
|
+
return n = L(n, i), G.fromBytes(u, "recovered").recoverPublicKey(n).toBytes();
|
|
3217
|
+
}
|
|
3218
|
+
return Object.freeze({
|
|
3219
|
+
keygen: U,
|
|
3220
|
+
getPublicKey: H,
|
|
3221
|
+
getSharedSecret: V,
|
|
3222
|
+
utils: E,
|
|
3223
|
+
lengths: b,
|
|
3224
|
+
Point: e,
|
|
3225
|
+
sign: K,
|
|
3226
|
+
verify: z,
|
|
3227
|
+
recoverPublicKey: d,
|
|
3228
|
+
Signature: G,
|
|
3229
|
+
hash: r
|
|
3230
|
+
});
|
|
3231
|
+
}
|
|
3232
|
+
function Pn(e) {
|
|
3233
|
+
const r = {
|
|
3234
|
+
a: e.a,
|
|
3235
|
+
b: e.b,
|
|
3236
|
+
p: e.Fp.ORDER,
|
|
3237
|
+
n: e.n,
|
|
3238
|
+
h: e.h,
|
|
3239
|
+
Gx: e.Gx,
|
|
3240
|
+
Gy: e.Gy
|
|
3241
|
+
}, c = e.Fp;
|
|
3242
|
+
let t = e.allowedPrivateKeyLengths ? Array.from(new Set(e.allowedPrivateKeyLengths.map((g) => Math.ceil(g / 2)))) : void 0;
|
|
3243
|
+
const l = De(r.n, {
|
|
3244
|
+
BITS: e.nBitLength,
|
|
3245
|
+
allowedLengths: t,
|
|
3246
|
+
modFromBytes: e.wrapPrivateKey
|
|
3247
|
+
}), m = {
|
|
3248
|
+
Fp: c,
|
|
3249
|
+
Fn: l,
|
|
3250
|
+
allowInfinityPoint: e.allowInfinityPoint,
|
|
3251
|
+
endo: e.endo,
|
|
3252
|
+
isTorsionFree: e.isTorsionFree,
|
|
3253
|
+
clearCofactor: e.clearCofactor,
|
|
3254
|
+
fromBytes: e.fromBytes,
|
|
3255
|
+
toBytes: e.toBytes
|
|
3256
|
+
};
|
|
3257
|
+
return { CURVE: r, curveOpts: m };
|
|
3258
|
+
}
|
|
3259
|
+
function Xn(e) {
|
|
3260
|
+
const { CURVE: r, curveOpts: c } = Pn(e), t = {
|
|
3261
|
+
hmac: e.hmac,
|
|
3262
|
+
randomBytes: e.randomBytes,
|
|
3263
|
+
lowS: e.lowS,
|
|
3264
|
+
bits2int: e.bits2int,
|
|
3265
|
+
bits2int_modN: e.bits2int_modN
|
|
3266
|
+
};
|
|
3267
|
+
return { CURVE: r, curveOpts: c, hash: e.hash, ecdsaOpts: t };
|
|
3268
|
+
}
|
|
3269
|
+
function Gn(e, r) {
|
|
3270
|
+
const c = r.Point;
|
|
3271
|
+
return Object.assign({}, r, {
|
|
3272
|
+
ProjectivePoint: c,
|
|
3273
|
+
CURVE: Object.assign({}, e, nn(c.Fn.ORDER, c.Fn.BITS))
|
|
3274
|
+
});
|
|
3275
|
+
}
|
|
3276
|
+
function Wn(e) {
|
|
3277
|
+
const { CURVE: r, curveOpts: c, hash: t, ecdsaOpts: l } = Xn(e), m = zn(r, c), g = Yn(m, t, l);
|
|
3278
|
+
return Gn(e, g);
|
|
3279
|
+
}
|
|
3280
|
+
function Qn(e, r) {
|
|
3281
|
+
const c = (t) => Wn({ ...e, hash: t });
|
|
3282
|
+
return { ...c(r), create: c };
|
|
3283
|
+
}
|
|
3284
|
+
const Bt = {
|
|
3285
|
+
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
3286
|
+
n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
3287
|
+
h: BigInt(1),
|
|
3288
|
+
a: BigInt(0),
|
|
3289
|
+
b: BigInt(7),
|
|
3290
|
+
Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
|
|
3291
|
+
Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
|
|
3292
|
+
}, Jn = {
|
|
3293
|
+
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
3294
|
+
basises: [
|
|
3295
|
+
[BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
|
|
3296
|
+
[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
|
|
3297
|
+
]
|
|
3298
|
+
}, Vt = /* @__PURE__ */ BigInt(2);
|
|
3299
|
+
function Fn(e) {
|
|
3300
|
+
const r = Bt.p, c = BigInt(3), t = BigInt(6), l = BigInt(11), m = BigInt(22), g = BigInt(23), p = BigInt(44), v = BigInt(88), U = e * e * e % r, H = U * U * e % r, V = Re(H, c, r) * H % r, E = Re(V, c, r) * H % r, b = Re(E, Vt, r) * U % r, S = Re(b, l, r) * b % r, A = Re(S, m, r) * S % r, M = Re(A, p, r) * A % r, Q = Re(M, v, r) * M % r, F = Re(Q, p, r) * A % r, G = Re(F, c, r) * H % r, se = Re(G, g, r) * S % r, ie = Re(se, t, r) * U % r, j = Re(ie, Vt, r);
|
|
3301
|
+
if (!ht.eql(ht.sqr(j), e))
|
|
3302
|
+
throw new Error("Cannot find square root");
|
|
3303
|
+
return j;
|
|
3304
|
+
}
|
|
3305
|
+
const ht = De(Bt.p, { sqrt: Fn }), or = Qn({ ...Bt, Fp: ht, lowS: !0, endo: Jn }, wn);
|
|
3306
|
+
export {
|
|
3307
|
+
rr as F,
|
|
3308
|
+
tr as a,
|
|
3309
|
+
$e as b,
|
|
3310
|
+
pn as c,
|
|
3311
|
+
nr as d,
|
|
3312
|
+
et as e,
|
|
3313
|
+
qe as m,
|
|
3314
|
+
er as r,
|
|
3315
|
+
or as s
|
|
3316
|
+
};
|