x402-proxy 0.10.7 → 0.10.9
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/CHANGELOG.md +23 -1
- package/README.md +7 -1
- package/dist/Credential-COZQnr1-.js +2055 -0
- package/dist/Mcp-CrCEqLqO.js +10 -0
- package/dist/Sse-ChldYgU7.js +9742 -0
- package/dist/Sse-kCB38G56.js +16482 -0
- package/dist/accounts-DsuvWwph.js +232 -0
- package/dist/accounts-DzvAlQRn.js +5 -0
- package/dist/accounts-IG-Cmrwy.js +229 -0
- package/dist/api-CUzmQvTQ.js +2802 -0
- package/dist/auth-DTzQmnZ_.js +1196 -0
- package/dist/bin/cli.js +585 -242
- package/dist/ccip-Bx-zoUCJ.js +240 -0
- package/dist/ccip-C2k1DD1T.js +153 -0
- package/dist/ccip-C6CQOJYv.js +152 -0
- package/dist/ccip-RZzsZ5Mv.js +156 -0
- package/dist/chain-CafcHffR.js +1997 -0
- package/dist/chain-DwfP5RGZ.js +1968 -0
- package/dist/chunk-DBEY4PJZ.js +16 -0
- package/dist/chunk-DjEMn6fM.js +36 -0
- package/dist/client-Blw2V7LF.js +657 -0
- package/dist/client-C37gWJOZ.js +102 -0
- package/dist/client-CEc4NYAA.js +6388 -0
- package/dist/client-CVDTUY0l.js +5152 -0
- package/dist/config-BUQsit4s.js +3 -0
- package/dist/config-DR1Fs_wL.js +6600 -0
- package/dist/{config-D9wIR3xc.js → config-rvKA3SYT.js} +10 -5
- package/dist/decodeFunctionData-DuFcwhC_.js +4510 -0
- package/dist/decodeFunctionData-JPOUdvil.js +4394 -0
- package/dist/derive-DNUl8LU9.js +9109 -0
- package/dist/dist-C2YO6HSQ.js +6581 -0
- package/dist/dist-DM5_F3r5.js +4 -0
- package/dist/dist-DxJCYyL5.js +1388 -0
- package/dist/hashTypedData-BHmP9dBd.js +859 -0
- package/dist/hashTypedData-CtEdfx4y.js +846 -0
- package/dist/helpers-CuUSw-tH.js +7125 -0
- package/dist/hmac-59IlS_by.js +648 -0
- package/dist/http-BAtucMbS.js +2060 -0
- package/dist/index.d.ts +1903 -9
- package/dist/index.js +18006 -50
- package/dist/index.node-CxkL0OFh.js +3592 -0
- package/dist/index.node-DvmeuZBj.js +3 -0
- package/dist/isAddressEqual-BLrd1Hg1.js +9 -0
- package/dist/isAddressEqual-DsAqfQOD.js +10 -0
- package/dist/localBatchGatewayRequest-C-RPJyDO.js +6260 -0
- package/dist/localBatchGatewayRequest-DOdQ9bR7.js +93 -0
- package/dist/localBatchGatewayRequest-DQkbZaSy.js +6261 -0
- package/dist/parseUnits-CApwcKSD.js +49 -0
- package/dist/parseUnits-cMO2udMe.js +48 -0
- package/dist/schemas-BxMFYNbH.js +1270 -0
- package/dist/secp256k1-BZpiyffY.js +2525 -0
- package/dist/secp256k1-BjenrLl5.js +1877 -0
- package/dist/secp256k1-CLPUX17u.js +3 -0
- package/dist/sendRawTransactionSync-DvSkhZtW.js +3612 -0
- package/dist/server-CSq0IuUq.js +565 -0
- package/dist/setup-BY4J49Lv.js +1110 -0
- package/dist/setup-wMOAgrsN.js +3 -0
- package/dist/sha256-FAs0qeni.js +17 -0
- package/dist/sha3-CYkWM8Xa.js +195 -0
- package/dist/sha3-DbMJRJ3C.js +194 -0
- package/dist/sse-B4LLqBQm.js +408 -0
- package/dist/status-Bu23RjW6.js +3 -0
- package/dist/{status-DihAcUSC.js → status-X21VnGUO.js} +16 -15
- package/dist/stdio-BADqxZdZ.js +85 -0
- package/dist/streamableHttp-BHkJypcI.js +358 -0
- package/dist/tempo-3nttrxgQ.js +17 -0
- package/dist/tempo-DER0P-ul.js +18 -0
- package/dist/types-BEKUz-Mf.js +1240 -0
- package/dist/types-DatK5vR5.js +3 -0
- package/dist/utils-BYjkXZDF.js +444 -0
- package/dist/utils-SeGHMW9O.js +445 -0
- package/dist/wallet-DKVlrR1S.js +3 -0
- package/dist/wallet-DSyht15_.js +17759 -0
- package/package.json +18 -71
- package/dist/config-B_upkJeK.js +0 -66
- package/dist/config-Be35NM5s.js +0 -3
- package/dist/config-J1m-CWXT.js +0 -27
- package/dist/derive-CL6e8K0Z.js +0 -81
- package/dist/openclaw/plugin.d.ts +0 -15
- package/dist/openclaw/plugin.js +0 -2067
- package/dist/openclaw.plugin.json +0 -93
- package/dist/setup-CNyMLnM-.js +0 -197
- package/dist/setup-DTIxPe58.js +0 -3
- package/dist/status-DZlJ4pS7.js +0 -3
- package/dist/wallet-B0S-rma9.js +0 -544
- package/dist/wallet-DBrVZJqe.js +0 -3
- package/openclaw.plugin.json +0 -93
- package/skills/SKILL.md +0 -183
- package/skills/references/library.md +0 -85
- package/skills/references/openclaw-plugin.md +0 -145
|
@@ -0,0 +1,2525 @@
|
|
|
1
|
+
import { n as __exportAll } from "./chunk-DBEY4PJZ.js";
|
|
2
|
+
import { $ as shrSH, A as createHasher$1, B as add3L, C as abytes, D as aoutput, E as anumber, H as add4L, I as toBytes, N as randomBytes, O as clean, P as rotr, Q as rotrSL, R as add, S as Hash, T as ahash, U as add5H, V as add4H, W as add5L, X as rotrBL, Y as rotrBH, Z as rotrSH, _ as numberToBytesBE, a as bitMask, b as utf8ToBytes, c as bytesToNumberLE, d as ensureBytes, et as shrSL, g as memoized, h as isBytes, i as bitLen, j as createView, k as concatBytes$1, l as concatBytes, m as inRange, n as abool, o as bytesToHex, p as hexToBytes, r as abytes$1, s as bytesToNumberBE, t as aInRange, tt as split, u as createHmacDrbg, v as numberToBytesLE, w as aexists, x as validateObject, y as numberToHexUnpadded, z as add3H } from "./utils-BYjkXZDF.js";
|
|
3
|
+
//#region node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/_md.js
|
|
4
|
+
/**
|
|
5
|
+
* Internal Merkle-Damgard hash utils.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/** Polyfill for Safari 14. https://caniuse.com/mdn-javascript_builtins_dataview_setbiguint64 */
|
|
9
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
10
|
+
if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE);
|
|
11
|
+
const _32n = BigInt(32);
|
|
12
|
+
const _u32_max = BigInt(4294967295);
|
|
13
|
+
const wh = Number(value >> _32n & _u32_max);
|
|
14
|
+
const wl = Number(value & _u32_max);
|
|
15
|
+
const h = isLE ? 4 : 0;
|
|
16
|
+
const l = isLE ? 0 : 4;
|
|
17
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
18
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
19
|
+
}
|
|
20
|
+
/** Choice: a ? b : c */
|
|
21
|
+
function Chi(a, b, c) {
|
|
22
|
+
return a & b ^ ~a & c;
|
|
23
|
+
}
|
|
24
|
+
/** Majority function, true if any two inputs is true. */
|
|
25
|
+
function Maj(a, b, c) {
|
|
26
|
+
return a & b ^ a & c ^ b & c;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Merkle-Damgard hash construction base class.
|
|
30
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
31
|
+
*/
|
|
32
|
+
var HashMD = class extends Hash {
|
|
33
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
34
|
+
super();
|
|
35
|
+
this.finished = false;
|
|
36
|
+
this.length = 0;
|
|
37
|
+
this.pos = 0;
|
|
38
|
+
this.destroyed = false;
|
|
39
|
+
this.blockLen = blockLen;
|
|
40
|
+
this.outputLen = outputLen;
|
|
41
|
+
this.padOffset = padOffset;
|
|
42
|
+
this.isLE = isLE;
|
|
43
|
+
this.buffer = new Uint8Array(blockLen);
|
|
44
|
+
this.view = createView(this.buffer);
|
|
45
|
+
}
|
|
46
|
+
update(data) {
|
|
47
|
+
aexists(this);
|
|
48
|
+
data = toBytes(data);
|
|
49
|
+
abytes(data);
|
|
50
|
+
const { view, buffer, blockLen } = this;
|
|
51
|
+
const len = data.length;
|
|
52
|
+
for (let pos = 0; pos < len;) {
|
|
53
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
54
|
+
if (take === blockLen) {
|
|
55
|
+
const dataView = createView(data);
|
|
56
|
+
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
60
|
+
this.pos += take;
|
|
61
|
+
pos += take;
|
|
62
|
+
if (this.pos === blockLen) {
|
|
63
|
+
this.process(view, 0);
|
|
64
|
+
this.pos = 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.length += data.length;
|
|
68
|
+
this.roundClean();
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
digestInto(out) {
|
|
72
|
+
aexists(this);
|
|
73
|
+
aoutput(out, this);
|
|
74
|
+
this.finished = true;
|
|
75
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
76
|
+
let { pos } = this;
|
|
77
|
+
buffer[pos++] = 128;
|
|
78
|
+
clean(this.buffer.subarray(pos));
|
|
79
|
+
if (this.padOffset > blockLen - pos) {
|
|
80
|
+
this.process(view, 0);
|
|
81
|
+
pos = 0;
|
|
82
|
+
}
|
|
83
|
+
for (let i = pos; i < blockLen; i++) buffer[i] = 0;
|
|
84
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
85
|
+
this.process(view, 0);
|
|
86
|
+
const oview = createView(out);
|
|
87
|
+
const len = this.outputLen;
|
|
88
|
+
if (len % 4) throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
89
|
+
const outLen = len / 4;
|
|
90
|
+
const state = this.get();
|
|
91
|
+
if (outLen > state.length) throw new Error("_sha2: outputLen bigger than state");
|
|
92
|
+
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
|
93
|
+
}
|
|
94
|
+
digest() {
|
|
95
|
+
const { buffer, outputLen } = this;
|
|
96
|
+
this.digestInto(buffer);
|
|
97
|
+
const res = buffer.slice(0, outputLen);
|
|
98
|
+
this.destroy();
|
|
99
|
+
return res;
|
|
100
|
+
}
|
|
101
|
+
_cloneInto(to) {
|
|
102
|
+
to || (to = new this.constructor());
|
|
103
|
+
to.set(...this.get());
|
|
104
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
105
|
+
to.destroyed = destroyed;
|
|
106
|
+
to.finished = finished;
|
|
107
|
+
to.length = length;
|
|
108
|
+
to.pos = pos;
|
|
109
|
+
if (length % blockLen) to.buffer.set(buffer);
|
|
110
|
+
return to;
|
|
111
|
+
}
|
|
112
|
+
clone() {
|
|
113
|
+
return this._cloneInto();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
118
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
119
|
+
*/
|
|
120
|
+
/** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
|
|
121
|
+
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
122
|
+
1779033703,
|
|
123
|
+
3144134277,
|
|
124
|
+
1013904242,
|
|
125
|
+
2773480762,
|
|
126
|
+
1359893119,
|
|
127
|
+
2600822924,
|
|
128
|
+
528734635,
|
|
129
|
+
1541459225
|
|
130
|
+
]);
|
|
131
|
+
/** Initial SHA384 state. Bits 0..64 of frac part of sqrt of primes 23..53 */
|
|
132
|
+
const SHA384_IV = /* @__PURE__ */ Uint32Array.from([
|
|
133
|
+
3418070365,
|
|
134
|
+
3238371032,
|
|
135
|
+
1654270250,
|
|
136
|
+
914150663,
|
|
137
|
+
2438529370,
|
|
138
|
+
812702999,
|
|
139
|
+
355462360,
|
|
140
|
+
4144912697,
|
|
141
|
+
1731405415,
|
|
142
|
+
4290775857,
|
|
143
|
+
2394180231,
|
|
144
|
+
1750603025,
|
|
145
|
+
3675008525,
|
|
146
|
+
1694076839,
|
|
147
|
+
1203062813,
|
|
148
|
+
3204075428
|
|
149
|
+
]);
|
|
150
|
+
/** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
|
|
151
|
+
const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
152
|
+
1779033703,
|
|
153
|
+
4089235720,
|
|
154
|
+
3144134277,
|
|
155
|
+
2227873595,
|
|
156
|
+
1013904242,
|
|
157
|
+
4271175723,
|
|
158
|
+
2773480762,
|
|
159
|
+
1595750129,
|
|
160
|
+
1359893119,
|
|
161
|
+
2917565137,
|
|
162
|
+
2600822924,
|
|
163
|
+
725511199,
|
|
164
|
+
528734635,
|
|
165
|
+
4215389547,
|
|
166
|
+
1541459225,
|
|
167
|
+
327033209
|
|
168
|
+
]);
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/sha2.js
|
|
171
|
+
/**
|
|
172
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
173
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
174
|
+
* Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
|
|
175
|
+
* [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
176
|
+
* @module
|
|
177
|
+
*/
|
|
178
|
+
/**
|
|
179
|
+
* Round constants:
|
|
180
|
+
* First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
|
|
181
|
+
*/
|
|
182
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
183
|
+
1116352408,
|
|
184
|
+
1899447441,
|
|
185
|
+
3049323471,
|
|
186
|
+
3921009573,
|
|
187
|
+
961987163,
|
|
188
|
+
1508970993,
|
|
189
|
+
2453635748,
|
|
190
|
+
2870763221,
|
|
191
|
+
3624381080,
|
|
192
|
+
310598401,
|
|
193
|
+
607225278,
|
|
194
|
+
1426881987,
|
|
195
|
+
1925078388,
|
|
196
|
+
2162078206,
|
|
197
|
+
2614888103,
|
|
198
|
+
3248222580,
|
|
199
|
+
3835390401,
|
|
200
|
+
4022224774,
|
|
201
|
+
264347078,
|
|
202
|
+
604807628,
|
|
203
|
+
770255983,
|
|
204
|
+
1249150122,
|
|
205
|
+
1555081692,
|
|
206
|
+
1996064986,
|
|
207
|
+
2554220882,
|
|
208
|
+
2821834349,
|
|
209
|
+
2952996808,
|
|
210
|
+
3210313671,
|
|
211
|
+
3336571891,
|
|
212
|
+
3584528711,
|
|
213
|
+
113926993,
|
|
214
|
+
338241895,
|
|
215
|
+
666307205,
|
|
216
|
+
773529912,
|
|
217
|
+
1294757372,
|
|
218
|
+
1396182291,
|
|
219
|
+
1695183700,
|
|
220
|
+
1986661051,
|
|
221
|
+
2177026350,
|
|
222
|
+
2456956037,
|
|
223
|
+
2730485921,
|
|
224
|
+
2820302411,
|
|
225
|
+
3259730800,
|
|
226
|
+
3345764771,
|
|
227
|
+
3516065817,
|
|
228
|
+
3600352804,
|
|
229
|
+
4094571909,
|
|
230
|
+
275423344,
|
|
231
|
+
430227734,
|
|
232
|
+
506948616,
|
|
233
|
+
659060556,
|
|
234
|
+
883997877,
|
|
235
|
+
958139571,
|
|
236
|
+
1322822218,
|
|
237
|
+
1537002063,
|
|
238
|
+
1747873779,
|
|
239
|
+
1955562222,
|
|
240
|
+
2024104815,
|
|
241
|
+
2227730452,
|
|
242
|
+
2361852424,
|
|
243
|
+
2428436474,
|
|
244
|
+
2756734187,
|
|
245
|
+
3204031479,
|
|
246
|
+
3329325298
|
|
247
|
+
]);
|
|
248
|
+
/** Reusable temporary buffer. "W" comes straight from spec. */
|
|
249
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
250
|
+
var SHA256 = class extends HashMD {
|
|
251
|
+
constructor(outputLen = 32) {
|
|
252
|
+
super(64, outputLen, 8, false);
|
|
253
|
+
this.A = SHA256_IV[0] | 0;
|
|
254
|
+
this.B = SHA256_IV[1] | 0;
|
|
255
|
+
this.C = SHA256_IV[2] | 0;
|
|
256
|
+
this.D = SHA256_IV[3] | 0;
|
|
257
|
+
this.E = SHA256_IV[4] | 0;
|
|
258
|
+
this.F = SHA256_IV[5] | 0;
|
|
259
|
+
this.G = SHA256_IV[6] | 0;
|
|
260
|
+
this.H = SHA256_IV[7] | 0;
|
|
261
|
+
}
|
|
262
|
+
get() {
|
|
263
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
264
|
+
return [
|
|
265
|
+
A,
|
|
266
|
+
B,
|
|
267
|
+
C,
|
|
268
|
+
D,
|
|
269
|
+
E,
|
|
270
|
+
F,
|
|
271
|
+
G,
|
|
272
|
+
H
|
|
273
|
+
];
|
|
274
|
+
}
|
|
275
|
+
set(A, B, C, D, E, F, G, H) {
|
|
276
|
+
this.A = A | 0;
|
|
277
|
+
this.B = B | 0;
|
|
278
|
+
this.C = C | 0;
|
|
279
|
+
this.D = D | 0;
|
|
280
|
+
this.E = E | 0;
|
|
281
|
+
this.F = F | 0;
|
|
282
|
+
this.G = G | 0;
|
|
283
|
+
this.H = H | 0;
|
|
284
|
+
}
|
|
285
|
+
process(view, offset) {
|
|
286
|
+
for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
|
|
287
|
+
for (let i = 16; i < 64; i++) {
|
|
288
|
+
const W15 = SHA256_W[i - 15];
|
|
289
|
+
const W2 = SHA256_W[i - 2];
|
|
290
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
291
|
+
SHA256_W[i] = (rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10) + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
292
|
+
}
|
|
293
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
294
|
+
for (let i = 0; i < 64; i++) {
|
|
295
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
296
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
297
|
+
const T2 = (rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22)) + Maj(A, B, C) | 0;
|
|
298
|
+
H = G;
|
|
299
|
+
G = F;
|
|
300
|
+
F = E;
|
|
301
|
+
E = D + T1 | 0;
|
|
302
|
+
D = C;
|
|
303
|
+
C = B;
|
|
304
|
+
B = A;
|
|
305
|
+
A = T1 + T2 | 0;
|
|
306
|
+
}
|
|
307
|
+
A = A + this.A | 0;
|
|
308
|
+
B = B + this.B | 0;
|
|
309
|
+
C = C + this.C | 0;
|
|
310
|
+
D = D + this.D | 0;
|
|
311
|
+
E = E + this.E | 0;
|
|
312
|
+
F = F + this.F | 0;
|
|
313
|
+
G = G + this.G | 0;
|
|
314
|
+
H = H + this.H | 0;
|
|
315
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
316
|
+
}
|
|
317
|
+
roundClean() {
|
|
318
|
+
clean(SHA256_W);
|
|
319
|
+
}
|
|
320
|
+
destroy() {
|
|
321
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
322
|
+
clean(this.buffer);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const K512 = split([
|
|
326
|
+
"0x428a2f98d728ae22",
|
|
327
|
+
"0x7137449123ef65cd",
|
|
328
|
+
"0xb5c0fbcfec4d3b2f",
|
|
329
|
+
"0xe9b5dba58189dbbc",
|
|
330
|
+
"0x3956c25bf348b538",
|
|
331
|
+
"0x59f111f1b605d019",
|
|
332
|
+
"0x923f82a4af194f9b",
|
|
333
|
+
"0xab1c5ed5da6d8118",
|
|
334
|
+
"0xd807aa98a3030242",
|
|
335
|
+
"0x12835b0145706fbe",
|
|
336
|
+
"0x243185be4ee4b28c",
|
|
337
|
+
"0x550c7dc3d5ffb4e2",
|
|
338
|
+
"0x72be5d74f27b896f",
|
|
339
|
+
"0x80deb1fe3b1696b1",
|
|
340
|
+
"0x9bdc06a725c71235",
|
|
341
|
+
"0xc19bf174cf692694",
|
|
342
|
+
"0xe49b69c19ef14ad2",
|
|
343
|
+
"0xefbe4786384f25e3",
|
|
344
|
+
"0x0fc19dc68b8cd5b5",
|
|
345
|
+
"0x240ca1cc77ac9c65",
|
|
346
|
+
"0x2de92c6f592b0275",
|
|
347
|
+
"0x4a7484aa6ea6e483",
|
|
348
|
+
"0x5cb0a9dcbd41fbd4",
|
|
349
|
+
"0x76f988da831153b5",
|
|
350
|
+
"0x983e5152ee66dfab",
|
|
351
|
+
"0xa831c66d2db43210",
|
|
352
|
+
"0xb00327c898fb213f",
|
|
353
|
+
"0xbf597fc7beef0ee4",
|
|
354
|
+
"0xc6e00bf33da88fc2",
|
|
355
|
+
"0xd5a79147930aa725",
|
|
356
|
+
"0x06ca6351e003826f",
|
|
357
|
+
"0x142929670a0e6e70",
|
|
358
|
+
"0x27b70a8546d22ffc",
|
|
359
|
+
"0x2e1b21385c26c926",
|
|
360
|
+
"0x4d2c6dfc5ac42aed",
|
|
361
|
+
"0x53380d139d95b3df",
|
|
362
|
+
"0x650a73548baf63de",
|
|
363
|
+
"0x766a0abb3c77b2a8",
|
|
364
|
+
"0x81c2c92e47edaee6",
|
|
365
|
+
"0x92722c851482353b",
|
|
366
|
+
"0xa2bfe8a14cf10364",
|
|
367
|
+
"0xa81a664bbc423001",
|
|
368
|
+
"0xc24b8b70d0f89791",
|
|
369
|
+
"0xc76c51a30654be30",
|
|
370
|
+
"0xd192e819d6ef5218",
|
|
371
|
+
"0xd69906245565a910",
|
|
372
|
+
"0xf40e35855771202a",
|
|
373
|
+
"0x106aa07032bbd1b8",
|
|
374
|
+
"0x19a4c116b8d2d0c8",
|
|
375
|
+
"0x1e376c085141ab53",
|
|
376
|
+
"0x2748774cdf8eeb99",
|
|
377
|
+
"0x34b0bcb5e19b48a8",
|
|
378
|
+
"0x391c0cb3c5c95a63",
|
|
379
|
+
"0x4ed8aa4ae3418acb",
|
|
380
|
+
"0x5b9cca4f7763e373",
|
|
381
|
+
"0x682e6ff3d6b2b8a3",
|
|
382
|
+
"0x748f82ee5defb2fc",
|
|
383
|
+
"0x78a5636f43172f60",
|
|
384
|
+
"0x84c87814a1f0ab72",
|
|
385
|
+
"0x8cc702081a6439ec",
|
|
386
|
+
"0x90befffa23631e28",
|
|
387
|
+
"0xa4506cebde82bde9",
|
|
388
|
+
"0xbef9a3f7b2c67915",
|
|
389
|
+
"0xc67178f2e372532b",
|
|
390
|
+
"0xca273eceea26619c",
|
|
391
|
+
"0xd186b8c721c0c207",
|
|
392
|
+
"0xeada7dd6cde0eb1e",
|
|
393
|
+
"0xf57d4f7fee6ed178",
|
|
394
|
+
"0x06f067aa72176fba",
|
|
395
|
+
"0x0a637dc5a2c898a6",
|
|
396
|
+
"0x113f9804bef90dae",
|
|
397
|
+
"0x1b710b35131c471b",
|
|
398
|
+
"0x28db77f523047d84",
|
|
399
|
+
"0x32caab7b40c72493",
|
|
400
|
+
"0x3c9ebe0a15c9bebc",
|
|
401
|
+
"0x431d67c49c100d4c",
|
|
402
|
+
"0x4cc5d4becb3e42b6",
|
|
403
|
+
"0x597f299cfc657e2a",
|
|
404
|
+
"0x5fcb6fab3ad6faec",
|
|
405
|
+
"0x6c44198c4a475817"
|
|
406
|
+
].map((n) => BigInt(n)));
|
|
407
|
+
const SHA512_Kh = K512[0];
|
|
408
|
+
const SHA512_Kl = K512[1];
|
|
409
|
+
const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
410
|
+
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
411
|
+
var SHA512 = class extends HashMD {
|
|
412
|
+
constructor(outputLen = 64) {
|
|
413
|
+
super(128, outputLen, 16, false);
|
|
414
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
415
|
+
this.Al = SHA512_IV[1] | 0;
|
|
416
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
417
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
418
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
419
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
420
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
421
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
422
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
423
|
+
this.El = SHA512_IV[9] | 0;
|
|
424
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
425
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
426
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
427
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
428
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
429
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
430
|
+
}
|
|
431
|
+
get() {
|
|
432
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
433
|
+
return [
|
|
434
|
+
Ah,
|
|
435
|
+
Al,
|
|
436
|
+
Bh,
|
|
437
|
+
Bl,
|
|
438
|
+
Ch,
|
|
439
|
+
Cl,
|
|
440
|
+
Dh,
|
|
441
|
+
Dl,
|
|
442
|
+
Eh,
|
|
443
|
+
El,
|
|
444
|
+
Fh,
|
|
445
|
+
Fl,
|
|
446
|
+
Gh,
|
|
447
|
+
Gl,
|
|
448
|
+
Hh,
|
|
449
|
+
Hl
|
|
450
|
+
];
|
|
451
|
+
}
|
|
452
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
453
|
+
this.Ah = Ah | 0;
|
|
454
|
+
this.Al = Al | 0;
|
|
455
|
+
this.Bh = Bh | 0;
|
|
456
|
+
this.Bl = Bl | 0;
|
|
457
|
+
this.Ch = Ch | 0;
|
|
458
|
+
this.Cl = Cl | 0;
|
|
459
|
+
this.Dh = Dh | 0;
|
|
460
|
+
this.Dl = Dl | 0;
|
|
461
|
+
this.Eh = Eh | 0;
|
|
462
|
+
this.El = El | 0;
|
|
463
|
+
this.Fh = Fh | 0;
|
|
464
|
+
this.Fl = Fl | 0;
|
|
465
|
+
this.Gh = Gh | 0;
|
|
466
|
+
this.Gl = Gl | 0;
|
|
467
|
+
this.Hh = Hh | 0;
|
|
468
|
+
this.Hl = Hl | 0;
|
|
469
|
+
}
|
|
470
|
+
process(view, offset) {
|
|
471
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
472
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
473
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
474
|
+
}
|
|
475
|
+
for (let i = 16; i < 80; i++) {
|
|
476
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
477
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
478
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
479
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
480
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
481
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
482
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
483
|
+
const SUMl = add4L(s0l, rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6), SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
484
|
+
SHA512_W_H[i] = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]) | 0;
|
|
485
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
486
|
+
}
|
|
487
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
488
|
+
for (let i = 0; i < 80; i++) {
|
|
489
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
490
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
491
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
492
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
493
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
494
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
495
|
+
const T1l = T1ll | 0;
|
|
496
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
497
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
498
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
499
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
500
|
+
Hh = Gh | 0;
|
|
501
|
+
Hl = Gl | 0;
|
|
502
|
+
Gh = Fh | 0;
|
|
503
|
+
Gl = Fl | 0;
|
|
504
|
+
Fh = Eh | 0;
|
|
505
|
+
Fl = El | 0;
|
|
506
|
+
({h: Eh, l: El} = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
507
|
+
Dh = Ch | 0;
|
|
508
|
+
Dl = Cl | 0;
|
|
509
|
+
Ch = Bh | 0;
|
|
510
|
+
Cl = Bl | 0;
|
|
511
|
+
Bh = Ah | 0;
|
|
512
|
+
Bl = Al | 0;
|
|
513
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
514
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
515
|
+
Al = All | 0;
|
|
516
|
+
}
|
|
517
|
+
({h: Ah, l: Al} = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
518
|
+
({h: Bh, l: Bl} = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
519
|
+
({h: Ch, l: Cl} = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
520
|
+
({h: Dh, l: Dl} = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
521
|
+
({h: Eh, l: El} = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
522
|
+
({h: Fh, l: Fl} = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
523
|
+
({h: Gh, l: Gl} = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
524
|
+
({h: Hh, l: Hl} = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
525
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
526
|
+
}
|
|
527
|
+
roundClean() {
|
|
528
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
529
|
+
}
|
|
530
|
+
destroy() {
|
|
531
|
+
clean(this.buffer);
|
|
532
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
var SHA384 = class extends SHA512 {
|
|
536
|
+
constructor() {
|
|
537
|
+
super(48);
|
|
538
|
+
this.Ah = SHA384_IV[0] | 0;
|
|
539
|
+
this.Al = SHA384_IV[1] | 0;
|
|
540
|
+
this.Bh = SHA384_IV[2] | 0;
|
|
541
|
+
this.Bl = SHA384_IV[3] | 0;
|
|
542
|
+
this.Ch = SHA384_IV[4] | 0;
|
|
543
|
+
this.Cl = SHA384_IV[5] | 0;
|
|
544
|
+
this.Dh = SHA384_IV[6] | 0;
|
|
545
|
+
this.Dl = SHA384_IV[7] | 0;
|
|
546
|
+
this.Eh = SHA384_IV[8] | 0;
|
|
547
|
+
this.El = SHA384_IV[9] | 0;
|
|
548
|
+
this.Fh = SHA384_IV[10] | 0;
|
|
549
|
+
this.Fl = SHA384_IV[11] | 0;
|
|
550
|
+
this.Gh = SHA384_IV[12] | 0;
|
|
551
|
+
this.Gl = SHA384_IV[13] | 0;
|
|
552
|
+
this.Hh = SHA384_IV[14] | 0;
|
|
553
|
+
this.Hl = SHA384_IV[15] | 0;
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
/**
|
|
557
|
+
* SHA2-256 hash function from RFC 4634.
|
|
558
|
+
*
|
|
559
|
+
* It is the fastest JS hash, even faster than Blake3.
|
|
560
|
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
561
|
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
562
|
+
*/
|
|
563
|
+
const sha256 = /* @__PURE__ */ createHasher$1(() => new SHA256());
|
|
564
|
+
/** SHA2-512 hash function from RFC 4634. */
|
|
565
|
+
const sha512 = /* @__PURE__ */ createHasher$1(() => new SHA512());
|
|
566
|
+
/** SHA2-384 hash function from RFC 4634. */
|
|
567
|
+
const sha384 = /* @__PURE__ */ createHasher$1(() => new SHA384());
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/hmac.js
|
|
570
|
+
/**
|
|
571
|
+
* HMAC: RFC2104 message authentication code.
|
|
572
|
+
* @module
|
|
573
|
+
*/
|
|
574
|
+
var HMAC = class extends Hash {
|
|
575
|
+
constructor(hash, _key) {
|
|
576
|
+
super();
|
|
577
|
+
this.finished = false;
|
|
578
|
+
this.destroyed = false;
|
|
579
|
+
ahash(hash);
|
|
580
|
+
const key = toBytes(_key);
|
|
581
|
+
this.iHash = hash.create();
|
|
582
|
+
if (typeof this.iHash.update !== "function") throw new Error("Expected instance of class which extends utils.Hash");
|
|
583
|
+
this.blockLen = this.iHash.blockLen;
|
|
584
|
+
this.outputLen = this.iHash.outputLen;
|
|
585
|
+
const blockLen = this.blockLen;
|
|
586
|
+
const pad = new Uint8Array(blockLen);
|
|
587
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
588
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 54;
|
|
589
|
+
this.iHash.update(pad);
|
|
590
|
+
this.oHash = hash.create();
|
|
591
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 106;
|
|
592
|
+
this.oHash.update(pad);
|
|
593
|
+
clean(pad);
|
|
594
|
+
}
|
|
595
|
+
update(buf) {
|
|
596
|
+
aexists(this);
|
|
597
|
+
this.iHash.update(buf);
|
|
598
|
+
return this;
|
|
599
|
+
}
|
|
600
|
+
digestInto(out) {
|
|
601
|
+
aexists(this);
|
|
602
|
+
abytes(out, this.outputLen);
|
|
603
|
+
this.finished = true;
|
|
604
|
+
this.iHash.digestInto(out);
|
|
605
|
+
this.oHash.update(out);
|
|
606
|
+
this.oHash.digestInto(out);
|
|
607
|
+
this.destroy();
|
|
608
|
+
}
|
|
609
|
+
digest() {
|
|
610
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
611
|
+
this.digestInto(out);
|
|
612
|
+
return out;
|
|
613
|
+
}
|
|
614
|
+
_cloneInto(to) {
|
|
615
|
+
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
616
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
617
|
+
to = to;
|
|
618
|
+
to.finished = finished;
|
|
619
|
+
to.destroyed = destroyed;
|
|
620
|
+
to.blockLen = blockLen;
|
|
621
|
+
to.outputLen = outputLen;
|
|
622
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
623
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
624
|
+
return to;
|
|
625
|
+
}
|
|
626
|
+
clone() {
|
|
627
|
+
return this._cloneInto();
|
|
628
|
+
}
|
|
629
|
+
destroy() {
|
|
630
|
+
this.destroyed = true;
|
|
631
|
+
this.oHash.destroy();
|
|
632
|
+
this.iHash.destroy();
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
/**
|
|
636
|
+
* HMAC: RFC2104 message authentication code.
|
|
637
|
+
* @param hash - function that would be used e.g. sha256
|
|
638
|
+
* @param key - message key
|
|
639
|
+
* @param message - message data
|
|
640
|
+
* @example
|
|
641
|
+
* import { hmac } from '@noble/hashes/hmac';
|
|
642
|
+
* import { sha256 } from '@noble/hashes/sha2';
|
|
643
|
+
* const mac1 = hmac(sha256, 'key', 'message');
|
|
644
|
+
*/
|
|
645
|
+
const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
|
646
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
|
647
|
+
//#endregion
|
|
648
|
+
//#region node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/modular.js
|
|
649
|
+
/**
|
|
650
|
+
* Utils for modular division and finite fields.
|
|
651
|
+
* A finite field over 11 is integer number operations `mod 11`.
|
|
652
|
+
* There is no division: it is replaced by modular multiplicative inverse.
|
|
653
|
+
* @module
|
|
654
|
+
*/
|
|
655
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
656
|
+
const _0n$3 = BigInt(0), _1n$3 = BigInt(1), _2n$2 = /* @__PURE__ */ BigInt(2), _3n$1 = /* @__PURE__ */ BigInt(3);
|
|
657
|
+
const _4n$1 = /* @__PURE__ */ BigInt(4), _5n = /* @__PURE__ */ BigInt(5), _8n = /* @__PURE__ */ BigInt(8);
|
|
658
|
+
function mod(a, b) {
|
|
659
|
+
const result = a % b;
|
|
660
|
+
return result >= _0n$3 ? result : b + result;
|
|
661
|
+
}
|
|
662
|
+
/** Does `x^(2^power)` mod p. `pow2(30, 4)` == `30^(2^4)` */
|
|
663
|
+
function pow2(x, power, modulo) {
|
|
664
|
+
let res = x;
|
|
665
|
+
while (power-- > _0n$3) {
|
|
666
|
+
res *= res;
|
|
667
|
+
res %= modulo;
|
|
668
|
+
}
|
|
669
|
+
return res;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Inverses number over modulo.
|
|
673
|
+
* Implemented using [Euclidean GCD](https://brilliant.org/wiki/extended-euclidean-algorithm/).
|
|
674
|
+
*/
|
|
675
|
+
function invert(number, modulo) {
|
|
676
|
+
if (number === _0n$3) throw new Error("invert: expected non-zero number");
|
|
677
|
+
if (modulo <= _0n$3) throw new Error("invert: expected positive modulus, got " + modulo);
|
|
678
|
+
let a = mod(number, modulo);
|
|
679
|
+
let b = modulo;
|
|
680
|
+
let x = _0n$3, y = _1n$3, u = _1n$3, v = _0n$3;
|
|
681
|
+
while (a !== _0n$3) {
|
|
682
|
+
const q = b / a;
|
|
683
|
+
const r = b % a;
|
|
684
|
+
const m = x - u * q;
|
|
685
|
+
const n = y - v * q;
|
|
686
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
687
|
+
}
|
|
688
|
+
if (b !== _1n$3) throw new Error("invert: does not exist");
|
|
689
|
+
return mod(x, modulo);
|
|
690
|
+
}
|
|
691
|
+
function sqrt3mod4(Fp, n) {
|
|
692
|
+
const p1div4 = (Fp.ORDER + _1n$3) / _4n$1;
|
|
693
|
+
const root = Fp.pow(n, p1div4);
|
|
694
|
+
if (!Fp.eql(Fp.sqr(root), n)) throw new Error("Cannot find square root");
|
|
695
|
+
return root;
|
|
696
|
+
}
|
|
697
|
+
function sqrt5mod8(Fp, n) {
|
|
698
|
+
const p5div8 = (Fp.ORDER - _5n) / _8n;
|
|
699
|
+
const n2 = Fp.mul(n, _2n$2);
|
|
700
|
+
const v = Fp.pow(n2, p5div8);
|
|
701
|
+
const nv = Fp.mul(n, v);
|
|
702
|
+
const i = Fp.mul(Fp.mul(nv, _2n$2), v);
|
|
703
|
+
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
704
|
+
if (!Fp.eql(Fp.sqr(root), n)) throw new Error("Cannot find square root");
|
|
705
|
+
return root;
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Tonelli-Shanks square root search algorithm.
|
|
709
|
+
* 1. https://eprint.iacr.org/2012/685.pdf (page 12)
|
|
710
|
+
* 2. Square Roots from 1; 24, 51, 10 to Dan Shanks
|
|
711
|
+
* @param P field order
|
|
712
|
+
* @returns function that takes field Fp (created from P) and number n
|
|
713
|
+
*/
|
|
714
|
+
function tonelliShanks(P) {
|
|
715
|
+
if (P < BigInt(3)) throw new Error("sqrt is not defined for small field");
|
|
716
|
+
let Q = P - _1n$3;
|
|
717
|
+
let S = 0;
|
|
718
|
+
while (Q % _2n$2 === _0n$3) {
|
|
719
|
+
Q /= _2n$2;
|
|
720
|
+
S++;
|
|
721
|
+
}
|
|
722
|
+
let Z = _2n$2;
|
|
723
|
+
const _Fp = Field(P);
|
|
724
|
+
while (FpLegendre(_Fp, Z) === 1) if (Z++ > 1e3) throw new Error("Cannot find square root: probably non-prime P");
|
|
725
|
+
if (S === 1) return sqrt3mod4;
|
|
726
|
+
let cc = _Fp.pow(Z, Q);
|
|
727
|
+
const Q1div2 = (Q + _1n$3) / _2n$2;
|
|
728
|
+
return function tonelliSlow(Fp, n) {
|
|
729
|
+
if (Fp.is0(n)) return n;
|
|
730
|
+
if (FpLegendre(Fp, n) !== 1) throw new Error("Cannot find square root");
|
|
731
|
+
let M = S;
|
|
732
|
+
let c = Fp.mul(Fp.ONE, cc);
|
|
733
|
+
let t = Fp.pow(n, Q);
|
|
734
|
+
let R = Fp.pow(n, Q1div2);
|
|
735
|
+
while (!Fp.eql(t, Fp.ONE)) {
|
|
736
|
+
if (Fp.is0(t)) return Fp.ZERO;
|
|
737
|
+
let i = 1;
|
|
738
|
+
let t_tmp = Fp.sqr(t);
|
|
739
|
+
while (!Fp.eql(t_tmp, Fp.ONE)) {
|
|
740
|
+
i++;
|
|
741
|
+
t_tmp = Fp.sqr(t_tmp);
|
|
742
|
+
if (i === M) throw new Error("Cannot find square root");
|
|
743
|
+
}
|
|
744
|
+
const exponent = _1n$3 << BigInt(M - i - 1);
|
|
745
|
+
const b = Fp.pow(c, exponent);
|
|
746
|
+
M = i;
|
|
747
|
+
c = Fp.sqr(b);
|
|
748
|
+
t = Fp.mul(t, c);
|
|
749
|
+
R = Fp.mul(R, b);
|
|
750
|
+
}
|
|
751
|
+
return R;
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Square root for a finite field. Will try optimized versions first:
|
|
756
|
+
*
|
|
757
|
+
* 1. P ≡ 3 (mod 4)
|
|
758
|
+
* 2. P ≡ 5 (mod 8)
|
|
759
|
+
* 3. Tonelli-Shanks algorithm
|
|
760
|
+
*
|
|
761
|
+
* Different algorithms can give different roots, it is up to user to decide which one they want.
|
|
762
|
+
* For example there is FpSqrtOdd/FpSqrtEven to choice root based on oddness (used for hash-to-curve).
|
|
763
|
+
*/
|
|
764
|
+
function FpSqrt(P) {
|
|
765
|
+
if (P % _4n$1 === _3n$1) return sqrt3mod4;
|
|
766
|
+
if (P % _8n === _5n) return sqrt5mod8;
|
|
767
|
+
return tonelliShanks(P);
|
|
768
|
+
}
|
|
769
|
+
const FIELD_FIELDS = [
|
|
770
|
+
"create",
|
|
771
|
+
"isValid",
|
|
772
|
+
"is0",
|
|
773
|
+
"neg",
|
|
774
|
+
"inv",
|
|
775
|
+
"sqrt",
|
|
776
|
+
"sqr",
|
|
777
|
+
"eql",
|
|
778
|
+
"add",
|
|
779
|
+
"sub",
|
|
780
|
+
"mul",
|
|
781
|
+
"pow",
|
|
782
|
+
"div",
|
|
783
|
+
"addN",
|
|
784
|
+
"subN",
|
|
785
|
+
"mulN",
|
|
786
|
+
"sqrN"
|
|
787
|
+
];
|
|
788
|
+
function validateField(field) {
|
|
789
|
+
return validateObject(field, FIELD_FIELDS.reduce((map, val) => {
|
|
790
|
+
map[val] = "function";
|
|
791
|
+
return map;
|
|
792
|
+
}, {
|
|
793
|
+
ORDER: "bigint",
|
|
794
|
+
MASK: "bigint",
|
|
795
|
+
BYTES: "isSafeInteger",
|
|
796
|
+
BITS: "isSafeInteger"
|
|
797
|
+
}));
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Same as `pow` but for Fp: non-constant-time.
|
|
801
|
+
* Unsafe in some contexts: uses ladder, so can expose bigint bits.
|
|
802
|
+
*/
|
|
803
|
+
function FpPow(Fp, num, power) {
|
|
804
|
+
if (power < _0n$3) throw new Error("invalid exponent, negatives unsupported");
|
|
805
|
+
if (power === _0n$3) return Fp.ONE;
|
|
806
|
+
if (power === _1n$3) return num;
|
|
807
|
+
let p = Fp.ONE;
|
|
808
|
+
let d = num;
|
|
809
|
+
while (power > _0n$3) {
|
|
810
|
+
if (power & _1n$3) p = Fp.mul(p, d);
|
|
811
|
+
d = Fp.sqr(d);
|
|
812
|
+
power >>= _1n$3;
|
|
813
|
+
}
|
|
814
|
+
return p;
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Efficiently invert an array of Field elements.
|
|
818
|
+
* Exception-free. Will return `undefined` for 0 elements.
|
|
819
|
+
* @param passZero map 0 to 0 (instead of undefined)
|
|
820
|
+
*/
|
|
821
|
+
function FpInvertBatch(Fp, nums, passZero = false) {
|
|
822
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
|
|
823
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
824
|
+
if (Fp.is0(num)) return acc;
|
|
825
|
+
inverted[i] = acc;
|
|
826
|
+
return Fp.mul(acc, num);
|
|
827
|
+
}, Fp.ONE);
|
|
828
|
+
const invertedAcc = Fp.inv(multipliedAcc);
|
|
829
|
+
nums.reduceRight((acc, num, i) => {
|
|
830
|
+
if (Fp.is0(num)) return acc;
|
|
831
|
+
inverted[i] = Fp.mul(acc, inverted[i]);
|
|
832
|
+
return Fp.mul(acc, num);
|
|
833
|
+
}, invertedAcc);
|
|
834
|
+
return inverted;
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Legendre symbol.
|
|
838
|
+
* Legendre constant is used to calculate Legendre symbol (a | p)
|
|
839
|
+
* which denotes the value of a^((p-1)/2) (mod p).
|
|
840
|
+
*
|
|
841
|
+
* * (a | p) ≡ 1 if a is a square (mod p), quadratic residue
|
|
842
|
+
* * (a | p) ≡ -1 if a is not a square (mod p), quadratic non residue
|
|
843
|
+
* * (a | p) ≡ 0 if a ≡ 0 (mod p)
|
|
844
|
+
*/
|
|
845
|
+
function FpLegendre(Fp, n) {
|
|
846
|
+
const p1mod2 = (Fp.ORDER - _1n$3) / _2n$2;
|
|
847
|
+
const powered = Fp.pow(n, p1mod2);
|
|
848
|
+
const yes = Fp.eql(powered, Fp.ONE);
|
|
849
|
+
const zero = Fp.eql(powered, Fp.ZERO);
|
|
850
|
+
const no = Fp.eql(powered, Fp.neg(Fp.ONE));
|
|
851
|
+
if (!yes && !zero && !no) throw new Error("invalid Legendre symbol result");
|
|
852
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
853
|
+
}
|
|
854
|
+
function nLength(n, nBitLength) {
|
|
855
|
+
if (nBitLength !== void 0) anumber(nBitLength);
|
|
856
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
857
|
+
return {
|
|
858
|
+
nBitLength: _nBitLength,
|
|
859
|
+
nByteLength: Math.ceil(_nBitLength / 8)
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Initializes a finite field over prime.
|
|
864
|
+
* Major performance optimizations:
|
|
865
|
+
* * a) denormalized operations like mulN instead of mul
|
|
866
|
+
* * b) same object shape: never add or remove keys
|
|
867
|
+
* * c) Object.freeze
|
|
868
|
+
* Fragile: always run a benchmark on a change.
|
|
869
|
+
* Security note: operations don't check 'isValid' for all elements for performance reasons,
|
|
870
|
+
* it is caller responsibility to check this.
|
|
871
|
+
* This is low-level code, please make sure you know what you're doing.
|
|
872
|
+
* @param ORDER prime positive bigint
|
|
873
|
+
* @param bitLen how many bits the field consumes
|
|
874
|
+
* @param isLE (def: false) if encoding / decoding should be in little-endian
|
|
875
|
+
* @param redef optional faster redefinitions of sqrt and other methods
|
|
876
|
+
*/
|
|
877
|
+
function Field(ORDER, bitLen, isLE = false, redef = {}) {
|
|
878
|
+
if (ORDER <= _0n$3) throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
879
|
+
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen);
|
|
880
|
+
if (BYTES > 2048) throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
881
|
+
let sqrtP;
|
|
882
|
+
const f = Object.freeze({
|
|
883
|
+
ORDER,
|
|
884
|
+
isLE,
|
|
885
|
+
BITS,
|
|
886
|
+
BYTES,
|
|
887
|
+
MASK: bitMask(BITS),
|
|
888
|
+
ZERO: _0n$3,
|
|
889
|
+
ONE: _1n$3,
|
|
890
|
+
create: (num) => mod(num, ORDER),
|
|
891
|
+
isValid: (num) => {
|
|
892
|
+
if (typeof num !== "bigint") throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
893
|
+
return _0n$3 <= num && num < ORDER;
|
|
894
|
+
},
|
|
895
|
+
is0: (num) => num === _0n$3,
|
|
896
|
+
isOdd: (num) => (num & _1n$3) === _1n$3,
|
|
897
|
+
neg: (num) => mod(-num, ORDER),
|
|
898
|
+
eql: (lhs, rhs) => lhs === rhs,
|
|
899
|
+
sqr: (num) => mod(num * num, ORDER),
|
|
900
|
+
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
901
|
+
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
902
|
+
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
903
|
+
pow: (num, power) => FpPow(f, num, power),
|
|
904
|
+
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
905
|
+
sqrN: (num) => num * num,
|
|
906
|
+
addN: (lhs, rhs) => lhs + rhs,
|
|
907
|
+
subN: (lhs, rhs) => lhs - rhs,
|
|
908
|
+
mulN: (lhs, rhs) => lhs * rhs,
|
|
909
|
+
inv: (num) => invert(num, ORDER),
|
|
910
|
+
sqrt: redef.sqrt || ((n) => {
|
|
911
|
+
if (!sqrtP) sqrtP = FpSqrt(ORDER);
|
|
912
|
+
return sqrtP(f, n);
|
|
913
|
+
}),
|
|
914
|
+
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
915
|
+
fromBytes: (bytes) => {
|
|
916
|
+
if (bytes.length !== BYTES) throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
917
|
+
return isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
918
|
+
},
|
|
919
|
+
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
920
|
+
cmov: (a, b, c) => c ? b : a
|
|
921
|
+
});
|
|
922
|
+
return Object.freeze(f);
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Returns total number of bytes consumed by the field element.
|
|
926
|
+
* For example, 32 bytes for usual 256-bit weierstrass curve.
|
|
927
|
+
* @param fieldOrder number of field elements, usually CURVE.n
|
|
928
|
+
* @returns byte length of field
|
|
929
|
+
*/
|
|
930
|
+
function getFieldBytesLength(fieldOrder) {
|
|
931
|
+
if (typeof fieldOrder !== "bigint") throw new Error("field order must be bigint");
|
|
932
|
+
const bitLength = fieldOrder.toString(2).length;
|
|
933
|
+
return Math.ceil(bitLength / 8);
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Returns minimal amount of bytes that can be safely reduced
|
|
937
|
+
* by field order.
|
|
938
|
+
* Should be 2^-128 for 128-bit curve such as P256.
|
|
939
|
+
* @param fieldOrder number of field elements, usually CURVE.n
|
|
940
|
+
* @returns byte length of target hash
|
|
941
|
+
*/
|
|
942
|
+
function getMinHashLength(fieldOrder) {
|
|
943
|
+
const length = getFieldBytesLength(fieldOrder);
|
|
944
|
+
return length + Math.ceil(length / 2);
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* "Constant-time" private key generation utility.
|
|
948
|
+
* Can take (n + n/2) or more bytes of uniform input e.g. from CSPRNG or KDF
|
|
949
|
+
* and convert them into private scalar, with the modulo bias being negligible.
|
|
950
|
+
* Needs at least 48 bytes of input for 32-byte private key.
|
|
951
|
+
* https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/
|
|
952
|
+
* FIPS 186-5, A.2 https://csrc.nist.gov/publications/detail/fips/186/5/final
|
|
953
|
+
* RFC 9380, https://www.rfc-editor.org/rfc/rfc9380#section-5
|
|
954
|
+
* @param hash hash output from SHA3 or a similar function
|
|
955
|
+
* @param groupOrder size of subgroup - (e.g. secp256k1.CURVE.n)
|
|
956
|
+
* @param isLE interpret hash bytes as LE num
|
|
957
|
+
* @returns valid private scalar
|
|
958
|
+
*/
|
|
959
|
+
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
960
|
+
const len = key.length;
|
|
961
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
962
|
+
const minLen = getMinHashLength(fieldOrder);
|
|
963
|
+
if (len < 16 || len < minLen || len > 1024) throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
964
|
+
const reduced = mod(isLE ? bytesToNumberLE(key) : bytesToNumberBE(key), fieldOrder - _1n$3) + _1n$3;
|
|
965
|
+
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
966
|
+
}
|
|
967
|
+
//#endregion
|
|
968
|
+
//#region node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/curve.js
|
|
969
|
+
/**
|
|
970
|
+
* Methods for elliptic curve multiplication by scalars.
|
|
971
|
+
* Contains wNAF, pippenger
|
|
972
|
+
* @module
|
|
973
|
+
*/
|
|
974
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
975
|
+
const _0n$2 = BigInt(0);
|
|
976
|
+
const _1n$2 = BigInt(1);
|
|
977
|
+
function constTimeNegate(condition, item) {
|
|
978
|
+
const neg = item.negate();
|
|
979
|
+
return condition ? neg : item;
|
|
980
|
+
}
|
|
981
|
+
function validateW(W, bits) {
|
|
982
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits) throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
983
|
+
}
|
|
984
|
+
function calcWOpts(W, scalarBits) {
|
|
985
|
+
validateW(W, scalarBits);
|
|
986
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
987
|
+
const windowSize = 2 ** (W - 1);
|
|
988
|
+
const maxNumber = 2 ** W;
|
|
989
|
+
return {
|
|
990
|
+
windows,
|
|
991
|
+
windowSize,
|
|
992
|
+
mask: bitMask(W),
|
|
993
|
+
maxNumber,
|
|
994
|
+
shiftBy: BigInt(W)
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
function calcOffsets(n, window, wOpts) {
|
|
998
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
999
|
+
let wbits = Number(n & mask);
|
|
1000
|
+
let nextN = n >> shiftBy;
|
|
1001
|
+
if (wbits > windowSize) {
|
|
1002
|
+
wbits -= maxNumber;
|
|
1003
|
+
nextN += _1n$2;
|
|
1004
|
+
}
|
|
1005
|
+
const offsetStart = window * windowSize;
|
|
1006
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
1007
|
+
const isZero = wbits === 0;
|
|
1008
|
+
const isNeg = wbits < 0;
|
|
1009
|
+
const isNegF = window % 2 !== 0;
|
|
1010
|
+
return {
|
|
1011
|
+
nextN,
|
|
1012
|
+
offset,
|
|
1013
|
+
isZero,
|
|
1014
|
+
isNeg,
|
|
1015
|
+
isNegF,
|
|
1016
|
+
offsetF: offsetStart
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
function validateMSMPoints(points, c) {
|
|
1020
|
+
if (!Array.isArray(points)) throw new Error("array expected");
|
|
1021
|
+
points.forEach((p, i) => {
|
|
1022
|
+
if (!(p instanceof c)) throw new Error("invalid point at index " + i);
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
function validateMSMScalars(scalars, field) {
|
|
1026
|
+
if (!Array.isArray(scalars)) throw new Error("array of scalars expected");
|
|
1027
|
+
scalars.forEach((s, i) => {
|
|
1028
|
+
if (!field.isValid(s)) throw new Error("invalid scalar at index " + i);
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
const pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
1032
|
+
const pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
1033
|
+
function getW(P) {
|
|
1034
|
+
return pointWindowSizes.get(P) || 1;
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Elliptic curve multiplication of Point by scalar. Fragile.
|
|
1038
|
+
* Scalars should always be less than curve order: this should be checked inside of a curve itself.
|
|
1039
|
+
* Creates precomputation tables for fast multiplication:
|
|
1040
|
+
* - private scalar is split by fixed size windows of W bits
|
|
1041
|
+
* - every window point is collected from window's table & added to accumulator
|
|
1042
|
+
* - since windows are different, same point inside tables won't be accessed more than once per calc
|
|
1043
|
+
* - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)
|
|
1044
|
+
* - +1 window is neccessary for wNAF
|
|
1045
|
+
* - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication
|
|
1046
|
+
*
|
|
1047
|
+
* @todo Research returning 2d JS array of windows, instead of a single window.
|
|
1048
|
+
* This would allow windows to be in different memory locations
|
|
1049
|
+
*/
|
|
1050
|
+
function wNAF(c, bits) {
|
|
1051
|
+
return {
|
|
1052
|
+
constTimeNegate,
|
|
1053
|
+
hasPrecomputes(elm) {
|
|
1054
|
+
return getW(elm) !== 1;
|
|
1055
|
+
},
|
|
1056
|
+
unsafeLadder(elm, n, p = c.ZERO) {
|
|
1057
|
+
let d = elm;
|
|
1058
|
+
while (n > _0n$2) {
|
|
1059
|
+
if (n & _1n$2) p = p.add(d);
|
|
1060
|
+
d = d.double();
|
|
1061
|
+
n >>= _1n$2;
|
|
1062
|
+
}
|
|
1063
|
+
return p;
|
|
1064
|
+
},
|
|
1065
|
+
precomputeWindow(elm, W) {
|
|
1066
|
+
const { windows, windowSize } = calcWOpts(W, bits);
|
|
1067
|
+
const points = [];
|
|
1068
|
+
let p = elm;
|
|
1069
|
+
let base = p;
|
|
1070
|
+
for (let window = 0; window < windows; window++) {
|
|
1071
|
+
base = p;
|
|
1072
|
+
points.push(base);
|
|
1073
|
+
for (let i = 1; i < windowSize; i++) {
|
|
1074
|
+
base = base.add(p);
|
|
1075
|
+
points.push(base);
|
|
1076
|
+
}
|
|
1077
|
+
p = base.double();
|
|
1078
|
+
}
|
|
1079
|
+
return points;
|
|
1080
|
+
},
|
|
1081
|
+
wNAF(W, precomputes, n) {
|
|
1082
|
+
let p = c.ZERO;
|
|
1083
|
+
let f = c.BASE;
|
|
1084
|
+
const wo = calcWOpts(W, bits);
|
|
1085
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1086
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
1087
|
+
n = nextN;
|
|
1088
|
+
if (isZero) f = f.add(constTimeNegate(isNegF, precomputes[offsetF]));
|
|
1089
|
+
else p = p.add(constTimeNegate(isNeg, precomputes[offset]));
|
|
1090
|
+
}
|
|
1091
|
+
return {
|
|
1092
|
+
p,
|
|
1093
|
+
f
|
|
1094
|
+
};
|
|
1095
|
+
},
|
|
1096
|
+
wNAFUnsafe(W, precomputes, n, acc = c.ZERO) {
|
|
1097
|
+
const wo = calcWOpts(W, bits);
|
|
1098
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1099
|
+
if (n === _0n$2) break;
|
|
1100
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
1101
|
+
n = nextN;
|
|
1102
|
+
if (isZero) continue;
|
|
1103
|
+
else {
|
|
1104
|
+
const item = precomputes[offset];
|
|
1105
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
return acc;
|
|
1109
|
+
},
|
|
1110
|
+
getPrecomputes(W, P, transform) {
|
|
1111
|
+
let comp = pointPrecomputes.get(P);
|
|
1112
|
+
if (!comp) {
|
|
1113
|
+
comp = this.precomputeWindow(P, W);
|
|
1114
|
+
if (W !== 1) pointPrecomputes.set(P, transform(comp));
|
|
1115
|
+
}
|
|
1116
|
+
return comp;
|
|
1117
|
+
},
|
|
1118
|
+
wNAFCached(P, n, transform) {
|
|
1119
|
+
const W = getW(P);
|
|
1120
|
+
return this.wNAF(W, this.getPrecomputes(W, P, transform), n);
|
|
1121
|
+
},
|
|
1122
|
+
wNAFCachedUnsafe(P, n, transform, prev) {
|
|
1123
|
+
const W = getW(P);
|
|
1124
|
+
if (W === 1) return this.unsafeLadder(P, n, prev);
|
|
1125
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, P, transform), n, prev);
|
|
1126
|
+
},
|
|
1127
|
+
setWindowSize(P, W) {
|
|
1128
|
+
validateW(W, bits);
|
|
1129
|
+
pointWindowSizes.set(P, W);
|
|
1130
|
+
pointPrecomputes.delete(P);
|
|
1131
|
+
}
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Pippenger algorithm for multi-scalar multiplication (MSM, Pa + Qb + Rc + ...).
|
|
1136
|
+
* 30x faster vs naive addition on L=4096, 10x faster than precomputes.
|
|
1137
|
+
* For N=254bit, L=1, it does: 1024 ADD + 254 DBL. For L=5: 1536 ADD + 254 DBL.
|
|
1138
|
+
* Algorithmically constant-time (for same L), even when 1 point + scalar, or when scalar = 0.
|
|
1139
|
+
* @param c Curve Point constructor
|
|
1140
|
+
* @param fieldN field over CURVE.N - important that it's not over CURVE.P
|
|
1141
|
+
* @param points array of L curve points
|
|
1142
|
+
* @param scalars array of L scalars (aka private keys / bigints)
|
|
1143
|
+
*/
|
|
1144
|
+
function pippenger(c, fieldN, points, scalars) {
|
|
1145
|
+
validateMSMPoints(points, c);
|
|
1146
|
+
validateMSMScalars(scalars, fieldN);
|
|
1147
|
+
const plength = points.length;
|
|
1148
|
+
const slength = scalars.length;
|
|
1149
|
+
if (plength !== slength) throw new Error("arrays of points and scalars must have equal length");
|
|
1150
|
+
const zero = c.ZERO;
|
|
1151
|
+
const wbits = bitLen(BigInt(plength));
|
|
1152
|
+
let windowSize = 1;
|
|
1153
|
+
if (wbits > 12) windowSize = wbits - 3;
|
|
1154
|
+
else if (wbits > 4) windowSize = wbits - 2;
|
|
1155
|
+
else if (wbits > 0) windowSize = 2;
|
|
1156
|
+
const MASK = bitMask(windowSize);
|
|
1157
|
+
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
1158
|
+
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
1159
|
+
let sum = zero;
|
|
1160
|
+
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
1161
|
+
buckets.fill(zero);
|
|
1162
|
+
for (let j = 0; j < slength; j++) {
|
|
1163
|
+
const scalar = scalars[j];
|
|
1164
|
+
const wbits = Number(scalar >> BigInt(i) & MASK);
|
|
1165
|
+
buckets[wbits] = buckets[wbits].add(points[j]);
|
|
1166
|
+
}
|
|
1167
|
+
let resI = zero;
|
|
1168
|
+
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
1169
|
+
sumI = sumI.add(buckets[j]);
|
|
1170
|
+
resI = resI.add(sumI);
|
|
1171
|
+
}
|
|
1172
|
+
sum = sum.add(resI);
|
|
1173
|
+
if (i !== 0) for (let j = 0; j < windowSize; j++) sum = sum.double();
|
|
1174
|
+
}
|
|
1175
|
+
return sum;
|
|
1176
|
+
}
|
|
1177
|
+
function validateBasic(curve) {
|
|
1178
|
+
validateField(curve.Fp);
|
|
1179
|
+
validateObject(curve, {
|
|
1180
|
+
n: "bigint",
|
|
1181
|
+
h: "bigint",
|
|
1182
|
+
Gx: "field",
|
|
1183
|
+
Gy: "field"
|
|
1184
|
+
}, {
|
|
1185
|
+
nBitLength: "isSafeInteger",
|
|
1186
|
+
nByteLength: "isSafeInteger"
|
|
1187
|
+
});
|
|
1188
|
+
return Object.freeze({
|
|
1189
|
+
...nLength(curve.n, curve.nBitLength),
|
|
1190
|
+
...curve,
|
|
1191
|
+
p: curve.Fp.ORDER
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
//#endregion
|
|
1195
|
+
//#region node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/weierstrass.js
|
|
1196
|
+
/**
|
|
1197
|
+
* Short Weierstrass curve methods. The formula is: y² = x³ + ax + b.
|
|
1198
|
+
*
|
|
1199
|
+
* ### Parameters
|
|
1200
|
+
*
|
|
1201
|
+
* To initialize a weierstrass curve, one needs to pass following params:
|
|
1202
|
+
*
|
|
1203
|
+
* * a: formula param
|
|
1204
|
+
* * b: formula param
|
|
1205
|
+
* * Fp: finite field of prime characteristic P; may be complex (Fp2). Arithmetics is done in field
|
|
1206
|
+
* * n: order of prime subgroup a.k.a total amount of valid curve points
|
|
1207
|
+
* * Gx: Base point (x, y) aka generator point. Gx = x coordinate
|
|
1208
|
+
* * Gy: ...y coordinate
|
|
1209
|
+
* * h: cofactor, usually 1. h*n = curve group order (n is only subgroup order)
|
|
1210
|
+
* * lowS: whether to enable (default) or disable "low-s" non-malleable signatures
|
|
1211
|
+
*
|
|
1212
|
+
* ### Design rationale for types
|
|
1213
|
+
*
|
|
1214
|
+
* * Interaction between classes from different curves should fail:
|
|
1215
|
+
* `k256.Point.BASE.add(p256.Point.BASE)`
|
|
1216
|
+
* * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
|
|
1217
|
+
* * Different calls of `curve()` would return different classes -
|
|
1218
|
+
* `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
|
|
1219
|
+
* it won't affect others
|
|
1220
|
+
*
|
|
1221
|
+
* TypeScript can't infer types for classes created inside a function. Classes is one instance
|
|
1222
|
+
* of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
|
|
1223
|
+
* unique type for every function call.
|
|
1224
|
+
*
|
|
1225
|
+
* We can use generic types via some param, like curve opts, but that would:
|
|
1226
|
+
* 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
|
|
1227
|
+
* which is hard to debug.
|
|
1228
|
+
* 2. Params can be generic and we can't enforce them to be constant value:
|
|
1229
|
+
* if somebody creates curve from non-constant params,
|
|
1230
|
+
* it would be allowed to interact with other curves with non-constant params
|
|
1231
|
+
*
|
|
1232
|
+
* @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
|
|
1233
|
+
* @module
|
|
1234
|
+
*/
|
|
1235
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
1236
|
+
function validateSigVerOpts(opts) {
|
|
1237
|
+
if (opts.lowS !== void 0) abool("lowS", opts.lowS);
|
|
1238
|
+
if (opts.prehash !== void 0) abool("prehash", opts.prehash);
|
|
1239
|
+
}
|
|
1240
|
+
function validatePointOpts(curve) {
|
|
1241
|
+
const opts = validateBasic(curve);
|
|
1242
|
+
validateObject(opts, {
|
|
1243
|
+
a: "field",
|
|
1244
|
+
b: "field"
|
|
1245
|
+
}, {
|
|
1246
|
+
allowInfinityPoint: "boolean",
|
|
1247
|
+
allowedPrivateKeyLengths: "array",
|
|
1248
|
+
clearCofactor: "function",
|
|
1249
|
+
fromBytes: "function",
|
|
1250
|
+
isTorsionFree: "function",
|
|
1251
|
+
toBytes: "function",
|
|
1252
|
+
wrapPrivateKey: "boolean"
|
|
1253
|
+
});
|
|
1254
|
+
const { endo, Fp, a } = opts;
|
|
1255
|
+
if (endo) {
|
|
1256
|
+
if (!Fp.eql(a, Fp.ZERO)) throw new Error("invalid endo: CURVE.a must be 0");
|
|
1257
|
+
if (typeof endo !== "object" || typeof endo.beta !== "bigint" || typeof endo.splitScalar !== "function") throw new Error("invalid endo: expected \"beta\": bigint and \"splitScalar\": function");
|
|
1258
|
+
}
|
|
1259
|
+
return Object.freeze({ ...opts });
|
|
1260
|
+
}
|
|
1261
|
+
var DERErr = class extends Error {
|
|
1262
|
+
constructor(m = "") {
|
|
1263
|
+
super(m);
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
1266
|
+
/**
|
|
1267
|
+
* ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
|
|
1268
|
+
*
|
|
1269
|
+
* [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
|
|
1270
|
+
*
|
|
1271
|
+
* Docs: https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/, https://luca.ntop.org/Teaching/Appunti/asn1.html
|
|
1272
|
+
*/
|
|
1273
|
+
const DER = {
|
|
1274
|
+
Err: DERErr,
|
|
1275
|
+
_tlv: {
|
|
1276
|
+
encode: (tag, data) => {
|
|
1277
|
+
const { Err: E } = DER;
|
|
1278
|
+
if (tag < 0 || tag > 256) throw new E("tlv.encode: wrong tag");
|
|
1279
|
+
if (data.length & 1) throw new E("tlv.encode: unpadded data");
|
|
1280
|
+
const dataLen = data.length / 2;
|
|
1281
|
+
const len = numberToHexUnpadded(dataLen);
|
|
1282
|
+
if (len.length / 2 & 128) throw new E("tlv.encode: long form length too big");
|
|
1283
|
+
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
1284
|
+
return numberToHexUnpadded(tag) + lenLen + len + data;
|
|
1285
|
+
},
|
|
1286
|
+
decode(tag, data) {
|
|
1287
|
+
const { Err: E } = DER;
|
|
1288
|
+
let pos = 0;
|
|
1289
|
+
if (tag < 0 || tag > 256) throw new E("tlv.encode: wrong tag");
|
|
1290
|
+
if (data.length < 2 || data[pos++] !== tag) throw new E("tlv.decode: wrong tlv");
|
|
1291
|
+
const first = data[pos++];
|
|
1292
|
+
const isLong = !!(first & 128);
|
|
1293
|
+
let length = 0;
|
|
1294
|
+
if (!isLong) length = first;
|
|
1295
|
+
else {
|
|
1296
|
+
const lenLen = first & 127;
|
|
1297
|
+
if (!lenLen) throw new E("tlv.decode(long): indefinite length not supported");
|
|
1298
|
+
if (lenLen > 4) throw new E("tlv.decode(long): byte length is too big");
|
|
1299
|
+
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
1300
|
+
if (lengthBytes.length !== lenLen) throw new E("tlv.decode: length bytes not complete");
|
|
1301
|
+
if (lengthBytes[0] === 0) throw new E("tlv.decode(long): zero leftmost byte");
|
|
1302
|
+
for (const b of lengthBytes) length = length << 8 | b;
|
|
1303
|
+
pos += lenLen;
|
|
1304
|
+
if (length < 128) throw new E("tlv.decode(long): not minimal encoding");
|
|
1305
|
+
}
|
|
1306
|
+
const v = data.subarray(pos, pos + length);
|
|
1307
|
+
if (v.length !== length) throw new E("tlv.decode: wrong value length");
|
|
1308
|
+
return {
|
|
1309
|
+
v,
|
|
1310
|
+
l: data.subarray(pos + length)
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
},
|
|
1314
|
+
_int: {
|
|
1315
|
+
encode(num) {
|
|
1316
|
+
const { Err: E } = DER;
|
|
1317
|
+
if (num < _0n$1) throw new E("integer: negative integers are not allowed");
|
|
1318
|
+
let hex = numberToHexUnpadded(num);
|
|
1319
|
+
if (Number.parseInt(hex[0], 16) & 8) hex = "00" + hex;
|
|
1320
|
+
if (hex.length & 1) throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
1321
|
+
return hex;
|
|
1322
|
+
},
|
|
1323
|
+
decode(data) {
|
|
1324
|
+
const { Err: E } = DER;
|
|
1325
|
+
if (data[0] & 128) throw new E("invalid signature integer: negative");
|
|
1326
|
+
if (data[0] === 0 && !(data[1] & 128)) throw new E("invalid signature integer: unnecessary leading zero");
|
|
1327
|
+
return bytesToNumberBE(data);
|
|
1328
|
+
}
|
|
1329
|
+
},
|
|
1330
|
+
toSig(hex) {
|
|
1331
|
+
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
1332
|
+
const data = ensureBytes("signature", hex);
|
|
1333
|
+
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
1334
|
+
if (seqLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
1335
|
+
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
1336
|
+
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
1337
|
+
if (sLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
1338
|
+
return {
|
|
1339
|
+
r: int.decode(rBytes),
|
|
1340
|
+
s: int.decode(sBytes)
|
|
1341
|
+
};
|
|
1342
|
+
},
|
|
1343
|
+
hexFromSig(sig) {
|
|
1344
|
+
const { _tlv: tlv, _int: int } = DER;
|
|
1345
|
+
const seq = tlv.encode(2, int.encode(sig.r)) + tlv.encode(2, int.encode(sig.s));
|
|
1346
|
+
return tlv.encode(48, seq);
|
|
1347
|
+
}
|
|
1348
|
+
};
|
|
1349
|
+
function numToSizedHex(num, size) {
|
|
1350
|
+
return bytesToHex(numberToBytesBE(num, size));
|
|
1351
|
+
}
|
|
1352
|
+
const _0n$1 = BigInt(0), _1n$1 = BigInt(1), _2n$1 = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
|
|
1353
|
+
function weierstrassPoints(opts) {
|
|
1354
|
+
const CURVE = validatePointOpts(opts);
|
|
1355
|
+
const { Fp } = CURVE;
|
|
1356
|
+
const Fn = Field(CURVE.n, CURVE.nBitLength);
|
|
1357
|
+
const toBytes = CURVE.toBytes || ((_c, point, _isCompressed) => {
|
|
1358
|
+
const a = point.toAffine();
|
|
1359
|
+
return concatBytes(Uint8Array.from([4]), Fp.toBytes(a.x), Fp.toBytes(a.y));
|
|
1360
|
+
});
|
|
1361
|
+
const fromBytes = CURVE.fromBytes || ((bytes) => {
|
|
1362
|
+
const tail = bytes.subarray(1);
|
|
1363
|
+
return {
|
|
1364
|
+
x: Fp.fromBytes(tail.subarray(0, Fp.BYTES)),
|
|
1365
|
+
y: Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES))
|
|
1366
|
+
};
|
|
1367
|
+
});
|
|
1368
|
+
/**
|
|
1369
|
+
* y² = x³ + ax + b: Short weierstrass curve formula. Takes x, returns y².
|
|
1370
|
+
* @returns y²
|
|
1371
|
+
*/
|
|
1372
|
+
function weierstrassEquation(x) {
|
|
1373
|
+
const { a, b } = CURVE;
|
|
1374
|
+
const x2 = Fp.sqr(x);
|
|
1375
|
+
const x3 = Fp.mul(x2, x);
|
|
1376
|
+
return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
|
|
1377
|
+
}
|
|
1378
|
+
function isValidXY(x, y) {
|
|
1379
|
+
const left = Fp.sqr(y);
|
|
1380
|
+
const right = weierstrassEquation(x);
|
|
1381
|
+
return Fp.eql(left, right);
|
|
1382
|
+
}
|
|
1383
|
+
if (!isValidXY(CURVE.Gx, CURVE.Gy)) throw new Error("bad curve params: generator point");
|
|
1384
|
+
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n), _4n);
|
|
1385
|
+
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
1386
|
+
if (Fp.is0(Fp.add(_4a3, _27b2))) throw new Error("bad curve params: a or b");
|
|
1387
|
+
function isWithinCurveOrder(num) {
|
|
1388
|
+
return inRange(num, _1n$1, CURVE.n);
|
|
1389
|
+
}
|
|
1390
|
+
function normPrivateKeyToScalar(key) {
|
|
1391
|
+
const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n: N } = CURVE;
|
|
1392
|
+
if (lengths && typeof key !== "bigint") {
|
|
1393
|
+
if (isBytes(key)) key = bytesToHex(key);
|
|
1394
|
+
if (typeof key !== "string" || !lengths.includes(key.length)) throw new Error("invalid private key");
|
|
1395
|
+
key = key.padStart(nByteLength * 2, "0");
|
|
1396
|
+
}
|
|
1397
|
+
let num;
|
|
1398
|
+
try {
|
|
1399
|
+
num = typeof key === "bigint" ? key : bytesToNumberBE(ensureBytes("private key", key, nByteLength));
|
|
1400
|
+
} catch (error) {
|
|
1401
|
+
throw new Error("invalid private key, expected hex or " + nByteLength + " bytes, got " + typeof key);
|
|
1402
|
+
}
|
|
1403
|
+
if (wrapPrivateKey) num = mod(num, N);
|
|
1404
|
+
aInRange("private key", num, _1n$1, N);
|
|
1405
|
+
return num;
|
|
1406
|
+
}
|
|
1407
|
+
function aprjpoint(other) {
|
|
1408
|
+
if (!(other instanceof Point)) throw new Error("ProjectivePoint expected");
|
|
1409
|
+
}
|
|
1410
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
1411
|
+
const { px: x, py: y, pz: z } = p;
|
|
1412
|
+
if (Fp.eql(z, Fp.ONE)) return {
|
|
1413
|
+
x,
|
|
1414
|
+
y
|
|
1415
|
+
};
|
|
1416
|
+
const is0 = p.is0();
|
|
1417
|
+
if (iz == null) iz = is0 ? Fp.ONE : Fp.inv(z);
|
|
1418
|
+
const ax = Fp.mul(x, iz);
|
|
1419
|
+
const ay = Fp.mul(y, iz);
|
|
1420
|
+
const zz = Fp.mul(z, iz);
|
|
1421
|
+
if (is0) return {
|
|
1422
|
+
x: Fp.ZERO,
|
|
1423
|
+
y: Fp.ZERO
|
|
1424
|
+
};
|
|
1425
|
+
if (!Fp.eql(zz, Fp.ONE)) throw new Error("invZ was invalid");
|
|
1426
|
+
return {
|
|
1427
|
+
x: ax,
|
|
1428
|
+
y: ay
|
|
1429
|
+
};
|
|
1430
|
+
});
|
|
1431
|
+
const assertValidMemo = memoized((p) => {
|
|
1432
|
+
if (p.is0()) {
|
|
1433
|
+
if (CURVE.allowInfinityPoint && !Fp.is0(p.py)) return;
|
|
1434
|
+
throw new Error("bad point: ZERO");
|
|
1435
|
+
}
|
|
1436
|
+
const { x, y } = p.toAffine();
|
|
1437
|
+
if (!Fp.isValid(x) || !Fp.isValid(y)) throw new Error("bad point: x or y not FE");
|
|
1438
|
+
if (!isValidXY(x, y)) throw new Error("bad point: equation left != right");
|
|
1439
|
+
if (!p.isTorsionFree()) throw new Error("bad point: not in prime-order subgroup");
|
|
1440
|
+
return true;
|
|
1441
|
+
});
|
|
1442
|
+
/**
|
|
1443
|
+
* Projective Point works in 3d / projective (homogeneous) coordinates: (X, Y, Z) ∋ (x=X/Z, y=Y/Z)
|
|
1444
|
+
* Default Point works in 2d / affine coordinates: (x, y)
|
|
1445
|
+
* We're doing calculations in projective, because its operations don't require costly inversion.
|
|
1446
|
+
*/
|
|
1447
|
+
class Point {
|
|
1448
|
+
constructor(px, py, pz) {
|
|
1449
|
+
if (px == null || !Fp.isValid(px)) throw new Error("x required");
|
|
1450
|
+
if (py == null || !Fp.isValid(py) || Fp.is0(py)) throw new Error("y required");
|
|
1451
|
+
if (pz == null || !Fp.isValid(pz)) throw new Error("z required");
|
|
1452
|
+
this.px = px;
|
|
1453
|
+
this.py = py;
|
|
1454
|
+
this.pz = pz;
|
|
1455
|
+
Object.freeze(this);
|
|
1456
|
+
}
|
|
1457
|
+
static fromAffine(p) {
|
|
1458
|
+
const { x, y } = p || {};
|
|
1459
|
+
if (!p || !Fp.isValid(x) || !Fp.isValid(y)) throw new Error("invalid affine point");
|
|
1460
|
+
if (p instanceof Point) throw new Error("projective point not allowed");
|
|
1461
|
+
const is0 = (i) => Fp.eql(i, Fp.ZERO);
|
|
1462
|
+
if (is0(x) && is0(y)) return Point.ZERO;
|
|
1463
|
+
return new Point(x, y, Fp.ONE);
|
|
1464
|
+
}
|
|
1465
|
+
get x() {
|
|
1466
|
+
return this.toAffine().x;
|
|
1467
|
+
}
|
|
1468
|
+
get y() {
|
|
1469
|
+
return this.toAffine().y;
|
|
1470
|
+
}
|
|
1471
|
+
/**
|
|
1472
|
+
* Takes a bunch of Projective Points but executes only one
|
|
1473
|
+
* inversion on all of them. Inversion is very slow operation,
|
|
1474
|
+
* so this improves performance massively.
|
|
1475
|
+
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
1476
|
+
*/
|
|
1477
|
+
static normalizeZ(points) {
|
|
1478
|
+
const toInv = FpInvertBatch(Fp, points.map((p) => p.pz));
|
|
1479
|
+
return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
1480
|
+
}
|
|
1481
|
+
/**
|
|
1482
|
+
* Converts hash string or Uint8Array to Point.
|
|
1483
|
+
* @param hex short/long ECDSA hex
|
|
1484
|
+
*/
|
|
1485
|
+
static fromHex(hex) {
|
|
1486
|
+
const P = Point.fromAffine(fromBytes(ensureBytes("pointHex", hex)));
|
|
1487
|
+
P.assertValidity();
|
|
1488
|
+
return P;
|
|
1489
|
+
}
|
|
1490
|
+
static fromPrivateKey(privateKey) {
|
|
1491
|
+
return Point.BASE.multiply(normPrivateKeyToScalar(privateKey));
|
|
1492
|
+
}
|
|
1493
|
+
static msm(points, scalars) {
|
|
1494
|
+
return pippenger(Point, Fn, points, scalars);
|
|
1495
|
+
}
|
|
1496
|
+
_setWindowSize(windowSize) {
|
|
1497
|
+
wnaf.setWindowSize(this, windowSize);
|
|
1498
|
+
}
|
|
1499
|
+
assertValidity() {
|
|
1500
|
+
assertValidMemo(this);
|
|
1501
|
+
}
|
|
1502
|
+
hasEvenY() {
|
|
1503
|
+
const { y } = this.toAffine();
|
|
1504
|
+
if (Fp.isOdd) return !Fp.isOdd(y);
|
|
1505
|
+
throw new Error("Field doesn't support isOdd");
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* Compare one point to another.
|
|
1509
|
+
*/
|
|
1510
|
+
equals(other) {
|
|
1511
|
+
aprjpoint(other);
|
|
1512
|
+
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
1513
|
+
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
1514
|
+
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
1515
|
+
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
1516
|
+
return U1 && U2;
|
|
1517
|
+
}
|
|
1518
|
+
/**
|
|
1519
|
+
* Flips point to one corresponding to (x, -y) in Affine coordinates.
|
|
1520
|
+
*/
|
|
1521
|
+
negate() {
|
|
1522
|
+
return new Point(this.px, Fp.neg(this.py), this.pz);
|
|
1523
|
+
}
|
|
1524
|
+
double() {
|
|
1525
|
+
const { a, b } = CURVE;
|
|
1526
|
+
const b3 = Fp.mul(b, _3n);
|
|
1527
|
+
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
1528
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
1529
|
+
let t0 = Fp.mul(X1, X1);
|
|
1530
|
+
let t1 = Fp.mul(Y1, Y1);
|
|
1531
|
+
let t2 = Fp.mul(Z1, Z1);
|
|
1532
|
+
let t3 = Fp.mul(X1, Y1);
|
|
1533
|
+
t3 = Fp.add(t3, t3);
|
|
1534
|
+
Z3 = Fp.mul(X1, Z1);
|
|
1535
|
+
Z3 = Fp.add(Z3, Z3);
|
|
1536
|
+
X3 = Fp.mul(a, Z3);
|
|
1537
|
+
Y3 = Fp.mul(b3, t2);
|
|
1538
|
+
Y3 = Fp.add(X3, Y3);
|
|
1539
|
+
X3 = Fp.sub(t1, Y3);
|
|
1540
|
+
Y3 = Fp.add(t1, Y3);
|
|
1541
|
+
Y3 = Fp.mul(X3, Y3);
|
|
1542
|
+
X3 = Fp.mul(t3, X3);
|
|
1543
|
+
Z3 = Fp.mul(b3, Z3);
|
|
1544
|
+
t2 = Fp.mul(a, t2);
|
|
1545
|
+
t3 = Fp.sub(t0, t2);
|
|
1546
|
+
t3 = Fp.mul(a, t3);
|
|
1547
|
+
t3 = Fp.add(t3, Z3);
|
|
1548
|
+
Z3 = Fp.add(t0, t0);
|
|
1549
|
+
t0 = Fp.add(Z3, t0);
|
|
1550
|
+
t0 = Fp.add(t0, t2);
|
|
1551
|
+
t0 = Fp.mul(t0, t3);
|
|
1552
|
+
Y3 = Fp.add(Y3, t0);
|
|
1553
|
+
t2 = Fp.mul(Y1, Z1);
|
|
1554
|
+
t2 = Fp.add(t2, t2);
|
|
1555
|
+
t0 = Fp.mul(t2, t3);
|
|
1556
|
+
X3 = Fp.sub(X3, t0);
|
|
1557
|
+
Z3 = Fp.mul(t2, t1);
|
|
1558
|
+
Z3 = Fp.add(Z3, Z3);
|
|
1559
|
+
Z3 = Fp.add(Z3, Z3);
|
|
1560
|
+
return new Point(X3, Y3, Z3);
|
|
1561
|
+
}
|
|
1562
|
+
add(other) {
|
|
1563
|
+
aprjpoint(other);
|
|
1564
|
+
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
1565
|
+
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
1566
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
1567
|
+
const a = CURVE.a;
|
|
1568
|
+
const b3 = Fp.mul(CURVE.b, _3n);
|
|
1569
|
+
let t0 = Fp.mul(X1, X2);
|
|
1570
|
+
let t1 = Fp.mul(Y1, Y2);
|
|
1571
|
+
let t2 = Fp.mul(Z1, Z2);
|
|
1572
|
+
let t3 = Fp.add(X1, Y1);
|
|
1573
|
+
let t4 = Fp.add(X2, Y2);
|
|
1574
|
+
t3 = Fp.mul(t3, t4);
|
|
1575
|
+
t4 = Fp.add(t0, t1);
|
|
1576
|
+
t3 = Fp.sub(t3, t4);
|
|
1577
|
+
t4 = Fp.add(X1, Z1);
|
|
1578
|
+
let t5 = Fp.add(X2, Z2);
|
|
1579
|
+
t4 = Fp.mul(t4, t5);
|
|
1580
|
+
t5 = Fp.add(t0, t2);
|
|
1581
|
+
t4 = Fp.sub(t4, t5);
|
|
1582
|
+
t5 = Fp.add(Y1, Z1);
|
|
1583
|
+
X3 = Fp.add(Y2, Z2);
|
|
1584
|
+
t5 = Fp.mul(t5, X3);
|
|
1585
|
+
X3 = Fp.add(t1, t2);
|
|
1586
|
+
t5 = Fp.sub(t5, X3);
|
|
1587
|
+
Z3 = Fp.mul(a, t4);
|
|
1588
|
+
X3 = Fp.mul(b3, t2);
|
|
1589
|
+
Z3 = Fp.add(X3, Z3);
|
|
1590
|
+
X3 = Fp.sub(t1, Z3);
|
|
1591
|
+
Z3 = Fp.add(t1, Z3);
|
|
1592
|
+
Y3 = Fp.mul(X3, Z3);
|
|
1593
|
+
t1 = Fp.add(t0, t0);
|
|
1594
|
+
t1 = Fp.add(t1, t0);
|
|
1595
|
+
t2 = Fp.mul(a, t2);
|
|
1596
|
+
t4 = Fp.mul(b3, t4);
|
|
1597
|
+
t1 = Fp.add(t1, t2);
|
|
1598
|
+
t2 = Fp.sub(t0, t2);
|
|
1599
|
+
t2 = Fp.mul(a, t2);
|
|
1600
|
+
t4 = Fp.add(t4, t2);
|
|
1601
|
+
t0 = Fp.mul(t1, t4);
|
|
1602
|
+
Y3 = Fp.add(Y3, t0);
|
|
1603
|
+
t0 = Fp.mul(t5, t4);
|
|
1604
|
+
X3 = Fp.mul(t3, X3);
|
|
1605
|
+
X3 = Fp.sub(X3, t0);
|
|
1606
|
+
t0 = Fp.mul(t3, t1);
|
|
1607
|
+
Z3 = Fp.mul(t5, Z3);
|
|
1608
|
+
Z3 = Fp.add(Z3, t0);
|
|
1609
|
+
return new Point(X3, Y3, Z3);
|
|
1610
|
+
}
|
|
1611
|
+
subtract(other) {
|
|
1612
|
+
return this.add(other.negate());
|
|
1613
|
+
}
|
|
1614
|
+
is0() {
|
|
1615
|
+
return this.equals(Point.ZERO);
|
|
1616
|
+
}
|
|
1617
|
+
wNAF(n) {
|
|
1618
|
+
return wnaf.wNAFCached(this, n, Point.normalizeZ);
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1622
|
+
* It's faster, but should only be used when you don't care about
|
|
1623
|
+
* an exposed private key e.g. sig verification, which works over *public* keys.
|
|
1624
|
+
*/
|
|
1625
|
+
multiplyUnsafe(sc) {
|
|
1626
|
+
const { endo, n: N } = CURVE;
|
|
1627
|
+
aInRange("scalar", sc, _0n$1, N);
|
|
1628
|
+
const I = Point.ZERO;
|
|
1629
|
+
if (sc === _0n$1) return I;
|
|
1630
|
+
if (this.is0() || sc === _1n$1) return this;
|
|
1631
|
+
if (!endo || wnaf.hasPrecomputes(this)) return wnaf.wNAFCachedUnsafe(this, sc, Point.normalizeZ);
|
|
1632
|
+
/** See docs for {@link EndomorphismOpts} */
|
|
1633
|
+
let { k1neg, k1, k2neg, k2 } = endo.splitScalar(sc);
|
|
1634
|
+
let k1p = I;
|
|
1635
|
+
let k2p = I;
|
|
1636
|
+
let d = this;
|
|
1637
|
+
while (k1 > _0n$1 || k2 > _0n$1) {
|
|
1638
|
+
if (k1 & _1n$1) k1p = k1p.add(d);
|
|
1639
|
+
if (k2 & _1n$1) k2p = k2p.add(d);
|
|
1640
|
+
d = d.double();
|
|
1641
|
+
k1 >>= _1n$1;
|
|
1642
|
+
k2 >>= _1n$1;
|
|
1643
|
+
}
|
|
1644
|
+
if (k1neg) k1p = k1p.negate();
|
|
1645
|
+
if (k2neg) k2p = k2p.negate();
|
|
1646
|
+
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
1647
|
+
return k1p.add(k2p);
|
|
1648
|
+
}
|
|
1649
|
+
/**
|
|
1650
|
+
* Constant time multiplication.
|
|
1651
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
1652
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
1653
|
+
* Uses precomputes when available.
|
|
1654
|
+
* Uses endomorphism for Koblitz curves.
|
|
1655
|
+
* @param scalar by which the point would be multiplied
|
|
1656
|
+
* @returns New point
|
|
1657
|
+
*/
|
|
1658
|
+
multiply(scalar) {
|
|
1659
|
+
const { endo, n: N } = CURVE;
|
|
1660
|
+
aInRange("scalar", scalar, _1n$1, N);
|
|
1661
|
+
let point, fake;
|
|
1662
|
+
/** See docs for {@link EndomorphismOpts} */
|
|
1663
|
+
if (endo) {
|
|
1664
|
+
const { k1neg, k1, k2neg, k2 } = endo.splitScalar(scalar);
|
|
1665
|
+
let { p: k1p, f: f1p } = this.wNAF(k1);
|
|
1666
|
+
let { p: k2p, f: f2p } = this.wNAF(k2);
|
|
1667
|
+
k1p = wnaf.constTimeNegate(k1neg, k1p);
|
|
1668
|
+
k2p = wnaf.constTimeNegate(k2neg, k2p);
|
|
1669
|
+
k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
1670
|
+
point = k1p.add(k2p);
|
|
1671
|
+
fake = f1p.add(f2p);
|
|
1672
|
+
} else {
|
|
1673
|
+
const { p, f } = this.wNAF(scalar);
|
|
1674
|
+
point = p;
|
|
1675
|
+
fake = f;
|
|
1676
|
+
}
|
|
1677
|
+
return Point.normalizeZ([point, fake])[0];
|
|
1678
|
+
}
|
|
1679
|
+
/**
|
|
1680
|
+
* Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
|
|
1681
|
+
* Not using Strauss-Shamir trick: precomputation tables are faster.
|
|
1682
|
+
* The trick could be useful if both P and Q are not G (not in our case).
|
|
1683
|
+
* @returns non-zero affine point
|
|
1684
|
+
*/
|
|
1685
|
+
multiplyAndAddUnsafe(Q, a, b) {
|
|
1686
|
+
const G = Point.BASE;
|
|
1687
|
+
const mul = (P, a) => a === _0n$1 || a === _1n$1 || !P.equals(G) ? P.multiplyUnsafe(a) : P.multiply(a);
|
|
1688
|
+
const sum = mul(this, a).add(mul(Q, b));
|
|
1689
|
+
return sum.is0() ? void 0 : sum;
|
|
1690
|
+
}
|
|
1691
|
+
toAffine(iz) {
|
|
1692
|
+
return toAffineMemo(this, iz);
|
|
1693
|
+
}
|
|
1694
|
+
isTorsionFree() {
|
|
1695
|
+
const { h: cofactor, isTorsionFree } = CURVE;
|
|
1696
|
+
if (cofactor === _1n$1) return true;
|
|
1697
|
+
if (isTorsionFree) return isTorsionFree(Point, this);
|
|
1698
|
+
throw new Error("isTorsionFree() has not been declared for the elliptic curve");
|
|
1699
|
+
}
|
|
1700
|
+
clearCofactor() {
|
|
1701
|
+
const { h: cofactor, clearCofactor } = CURVE;
|
|
1702
|
+
if (cofactor === _1n$1) return this;
|
|
1703
|
+
if (clearCofactor) return clearCofactor(Point, this);
|
|
1704
|
+
return this.multiplyUnsafe(CURVE.h);
|
|
1705
|
+
}
|
|
1706
|
+
toRawBytes(isCompressed = true) {
|
|
1707
|
+
abool("isCompressed", isCompressed);
|
|
1708
|
+
this.assertValidity();
|
|
1709
|
+
return toBytes(Point, this, isCompressed);
|
|
1710
|
+
}
|
|
1711
|
+
toHex(isCompressed = true) {
|
|
1712
|
+
abool("isCompressed", isCompressed);
|
|
1713
|
+
return bytesToHex(this.toRawBytes(isCompressed));
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
|
1717
|
+
Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
1718
|
+
const { endo, nBitLength } = CURVE;
|
|
1719
|
+
const wnaf = wNAF(Point, endo ? Math.ceil(nBitLength / 2) : nBitLength);
|
|
1720
|
+
return {
|
|
1721
|
+
CURVE,
|
|
1722
|
+
ProjectivePoint: Point,
|
|
1723
|
+
normPrivateKeyToScalar,
|
|
1724
|
+
weierstrassEquation,
|
|
1725
|
+
isWithinCurveOrder
|
|
1726
|
+
};
|
|
1727
|
+
}
|
|
1728
|
+
function validateOpts(curve) {
|
|
1729
|
+
const opts = validateBasic(curve);
|
|
1730
|
+
validateObject(opts, {
|
|
1731
|
+
hash: "hash",
|
|
1732
|
+
hmac: "function",
|
|
1733
|
+
randomBytes: "function"
|
|
1734
|
+
}, {
|
|
1735
|
+
bits2int: "function",
|
|
1736
|
+
bits2int_modN: "function",
|
|
1737
|
+
lowS: "boolean"
|
|
1738
|
+
});
|
|
1739
|
+
return Object.freeze({
|
|
1740
|
+
lowS: true,
|
|
1741
|
+
...opts
|
|
1742
|
+
});
|
|
1743
|
+
}
|
|
1744
|
+
/**
|
|
1745
|
+
* Creates short weierstrass curve and ECDSA signature methods for it.
|
|
1746
|
+
* @example
|
|
1747
|
+
* import { Field } from '@noble/curves/abstract/modular';
|
|
1748
|
+
* // Before that, define BigInt-s: a, b, p, n, Gx, Gy
|
|
1749
|
+
* const curve = weierstrass({ a, b, Fp: Field(p), n, Gx, Gy, h: 1n })
|
|
1750
|
+
*/
|
|
1751
|
+
function weierstrass(curveDef) {
|
|
1752
|
+
const CURVE = validateOpts(curveDef);
|
|
1753
|
+
const { Fp, n: CURVE_ORDER, nByteLength, nBitLength } = CURVE;
|
|
1754
|
+
const compressedLen = Fp.BYTES + 1;
|
|
1755
|
+
const uncompressedLen = 2 * Fp.BYTES + 1;
|
|
1756
|
+
function modN(a) {
|
|
1757
|
+
return mod(a, CURVE_ORDER);
|
|
1758
|
+
}
|
|
1759
|
+
function invN(a) {
|
|
1760
|
+
return invert(a, CURVE_ORDER);
|
|
1761
|
+
}
|
|
1762
|
+
const { ProjectivePoint: Point, normPrivateKeyToScalar, weierstrassEquation, isWithinCurveOrder } = weierstrassPoints({
|
|
1763
|
+
...CURVE,
|
|
1764
|
+
toBytes(_c, point, isCompressed) {
|
|
1765
|
+
const a = point.toAffine();
|
|
1766
|
+
const x = Fp.toBytes(a.x);
|
|
1767
|
+
const cat = concatBytes;
|
|
1768
|
+
abool("isCompressed", isCompressed);
|
|
1769
|
+
if (isCompressed) return cat(Uint8Array.from([point.hasEvenY() ? 2 : 3]), x);
|
|
1770
|
+
else return cat(Uint8Array.from([4]), x, Fp.toBytes(a.y));
|
|
1771
|
+
},
|
|
1772
|
+
fromBytes(bytes) {
|
|
1773
|
+
const len = bytes.length;
|
|
1774
|
+
const head = bytes[0];
|
|
1775
|
+
const tail = bytes.subarray(1);
|
|
1776
|
+
if (len === compressedLen && (head === 2 || head === 3)) {
|
|
1777
|
+
const x = bytesToNumberBE(tail);
|
|
1778
|
+
if (!inRange(x, _1n$1, Fp.ORDER)) throw new Error("Point is not on curve");
|
|
1779
|
+
const y2 = weierstrassEquation(x);
|
|
1780
|
+
let y;
|
|
1781
|
+
try {
|
|
1782
|
+
y = Fp.sqrt(y2);
|
|
1783
|
+
} catch (sqrtError) {
|
|
1784
|
+
const suffix = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
1785
|
+
throw new Error("Point is not on curve" + suffix);
|
|
1786
|
+
}
|
|
1787
|
+
const isYOdd = (y & _1n$1) === _1n$1;
|
|
1788
|
+
if ((head & 1) === 1 !== isYOdd) y = Fp.neg(y);
|
|
1789
|
+
return {
|
|
1790
|
+
x,
|
|
1791
|
+
y
|
|
1792
|
+
};
|
|
1793
|
+
} else if (len === uncompressedLen && head === 4) return {
|
|
1794
|
+
x: Fp.fromBytes(tail.subarray(0, Fp.BYTES)),
|
|
1795
|
+
y: Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES))
|
|
1796
|
+
};
|
|
1797
|
+
else {
|
|
1798
|
+
const cl = compressedLen;
|
|
1799
|
+
const ul = uncompressedLen;
|
|
1800
|
+
throw new Error("invalid Point, expected length of " + cl + ", or uncompressed " + ul + ", got " + len);
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
});
|
|
1804
|
+
function isBiggerThanHalfOrder(number) {
|
|
1805
|
+
return number > CURVE_ORDER >> _1n$1;
|
|
1806
|
+
}
|
|
1807
|
+
function normalizeS(s) {
|
|
1808
|
+
return isBiggerThanHalfOrder(s) ? modN(-s) : s;
|
|
1809
|
+
}
|
|
1810
|
+
const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
|
|
1811
|
+
/**
|
|
1812
|
+
* ECDSA signature with its (r, s) properties. Supports DER & compact representations.
|
|
1813
|
+
*/
|
|
1814
|
+
class Signature {
|
|
1815
|
+
constructor(r, s, recovery) {
|
|
1816
|
+
aInRange("r", r, _1n$1, CURVE_ORDER);
|
|
1817
|
+
aInRange("s", s, _1n$1, CURVE_ORDER);
|
|
1818
|
+
this.r = r;
|
|
1819
|
+
this.s = s;
|
|
1820
|
+
if (recovery != null) this.recovery = recovery;
|
|
1821
|
+
Object.freeze(this);
|
|
1822
|
+
}
|
|
1823
|
+
static fromCompact(hex) {
|
|
1824
|
+
const l = nByteLength;
|
|
1825
|
+
hex = ensureBytes("compactSignature", hex, l * 2);
|
|
1826
|
+
return new Signature(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
|
|
1827
|
+
}
|
|
1828
|
+
static fromDER(hex) {
|
|
1829
|
+
const { r, s } = DER.toSig(ensureBytes("DER", hex));
|
|
1830
|
+
return new Signature(r, s);
|
|
1831
|
+
}
|
|
1832
|
+
/**
|
|
1833
|
+
* @todo remove
|
|
1834
|
+
* @deprecated
|
|
1835
|
+
*/
|
|
1836
|
+
assertValidity() {}
|
|
1837
|
+
addRecoveryBit(recovery) {
|
|
1838
|
+
return new Signature(this.r, this.s, recovery);
|
|
1839
|
+
}
|
|
1840
|
+
recoverPublicKey(msgHash) {
|
|
1841
|
+
const { r, s, recovery: rec } = this;
|
|
1842
|
+
const h = bits2int_modN(ensureBytes("msgHash", msgHash));
|
|
1843
|
+
if (rec == null || ![
|
|
1844
|
+
0,
|
|
1845
|
+
1,
|
|
1846
|
+
2,
|
|
1847
|
+
3
|
|
1848
|
+
].includes(rec)) throw new Error("recovery id invalid");
|
|
1849
|
+
const radj = rec === 2 || rec === 3 ? r + CURVE.n : r;
|
|
1850
|
+
if (radj >= Fp.ORDER) throw new Error("recovery id 2 or 3 invalid");
|
|
1851
|
+
const prefix = (rec & 1) === 0 ? "02" : "03";
|
|
1852
|
+
const R = Point.fromHex(prefix + numToSizedHex(radj, Fp.BYTES));
|
|
1853
|
+
const ir = invN(radj);
|
|
1854
|
+
const u1 = modN(-h * ir);
|
|
1855
|
+
const u2 = modN(s * ir);
|
|
1856
|
+
const Q = Point.BASE.multiplyAndAddUnsafe(R, u1, u2);
|
|
1857
|
+
if (!Q) throw new Error("point at infinify");
|
|
1858
|
+
Q.assertValidity();
|
|
1859
|
+
return Q;
|
|
1860
|
+
}
|
|
1861
|
+
hasHighS() {
|
|
1862
|
+
return isBiggerThanHalfOrder(this.s);
|
|
1863
|
+
}
|
|
1864
|
+
normalizeS() {
|
|
1865
|
+
return this.hasHighS() ? new Signature(this.r, modN(-this.s), this.recovery) : this;
|
|
1866
|
+
}
|
|
1867
|
+
toDERRawBytes() {
|
|
1868
|
+
return hexToBytes(this.toDERHex());
|
|
1869
|
+
}
|
|
1870
|
+
toDERHex() {
|
|
1871
|
+
return DER.hexFromSig(this);
|
|
1872
|
+
}
|
|
1873
|
+
toCompactRawBytes() {
|
|
1874
|
+
return hexToBytes(this.toCompactHex());
|
|
1875
|
+
}
|
|
1876
|
+
toCompactHex() {
|
|
1877
|
+
const l = nByteLength;
|
|
1878
|
+
return numToSizedHex(this.r, l) + numToSizedHex(this.s, l);
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
const utils = {
|
|
1882
|
+
isValidPrivateKey(privateKey) {
|
|
1883
|
+
try {
|
|
1884
|
+
normPrivateKeyToScalar(privateKey);
|
|
1885
|
+
return true;
|
|
1886
|
+
} catch (error) {
|
|
1887
|
+
return false;
|
|
1888
|
+
}
|
|
1889
|
+
},
|
|
1890
|
+
normPrivateKeyToScalar,
|
|
1891
|
+
randomPrivateKey: () => {
|
|
1892
|
+
const length = getMinHashLength(CURVE.n);
|
|
1893
|
+
return mapHashToField(CURVE.randomBytes(length), CURVE.n);
|
|
1894
|
+
},
|
|
1895
|
+
precompute(windowSize = 8, point = Point.BASE) {
|
|
1896
|
+
point._setWindowSize(windowSize);
|
|
1897
|
+
point.multiply(BigInt(3));
|
|
1898
|
+
return point;
|
|
1899
|
+
}
|
|
1900
|
+
};
|
|
1901
|
+
/**
|
|
1902
|
+
* Computes public key for a private key. Checks for validity of the private key.
|
|
1903
|
+
* @param privateKey private key
|
|
1904
|
+
* @param isCompressed whether to return compact (default), or full key
|
|
1905
|
+
* @returns Public key, full when isCompressed=false; short when isCompressed=true
|
|
1906
|
+
*/
|
|
1907
|
+
function getPublicKey(privateKey, isCompressed = true) {
|
|
1908
|
+
return Point.fromPrivateKey(privateKey).toRawBytes(isCompressed);
|
|
1909
|
+
}
|
|
1910
|
+
/**
|
|
1911
|
+
* Quick and dirty check for item being public key. Does not validate hex, or being on-curve.
|
|
1912
|
+
*/
|
|
1913
|
+
function isProbPub(item) {
|
|
1914
|
+
if (typeof item === "bigint") return false;
|
|
1915
|
+
if (item instanceof Point) return true;
|
|
1916
|
+
const len = ensureBytes("key", item).length;
|
|
1917
|
+
const fpl = Fp.BYTES;
|
|
1918
|
+
const compLen = fpl + 1;
|
|
1919
|
+
const uncompLen = 2 * fpl + 1;
|
|
1920
|
+
if (CURVE.allowedPrivateKeyLengths || nByteLength === compLen) return;
|
|
1921
|
+
else return len === compLen || len === uncompLen;
|
|
1922
|
+
}
|
|
1923
|
+
/**
|
|
1924
|
+
* ECDH (Elliptic Curve Diffie Hellman).
|
|
1925
|
+
* Computes shared public key from private key and public key.
|
|
1926
|
+
* Checks: 1) private key validity 2) shared key is on-curve.
|
|
1927
|
+
* Does NOT hash the result.
|
|
1928
|
+
* @param privateA private key
|
|
1929
|
+
* @param publicB different public key
|
|
1930
|
+
* @param isCompressed whether to return compact (default), or full key
|
|
1931
|
+
* @returns shared public key
|
|
1932
|
+
*/
|
|
1933
|
+
function getSharedSecret(privateA, publicB, isCompressed = true) {
|
|
1934
|
+
if (isProbPub(privateA) === true) throw new Error("first arg must be private key");
|
|
1935
|
+
if (isProbPub(publicB) === false) throw new Error("second arg must be public key");
|
|
1936
|
+
return Point.fromHex(publicB).multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
|
|
1937
|
+
}
|
|
1938
|
+
const bits2int = CURVE.bits2int || function(bytes) {
|
|
1939
|
+
if (bytes.length > 8192) throw new Error("input is too large");
|
|
1940
|
+
const num = bytesToNumberBE(bytes);
|
|
1941
|
+
const delta = bytes.length * 8 - nBitLength;
|
|
1942
|
+
return delta > 0 ? num >> BigInt(delta) : num;
|
|
1943
|
+
};
|
|
1944
|
+
const bits2int_modN = CURVE.bits2int_modN || function(bytes) {
|
|
1945
|
+
return modN(bits2int(bytes));
|
|
1946
|
+
};
|
|
1947
|
+
const ORDER_MASK = bitMask(nBitLength);
|
|
1948
|
+
/**
|
|
1949
|
+
* Converts to bytes. Checks if num in `[0..ORDER_MASK-1]` e.g.: `[0..2^256-1]`.
|
|
1950
|
+
*/
|
|
1951
|
+
function int2octets(num) {
|
|
1952
|
+
aInRange("num < 2^" + nBitLength, num, _0n$1, ORDER_MASK);
|
|
1953
|
+
return numberToBytesBE(num, nByteLength);
|
|
1954
|
+
}
|
|
1955
|
+
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
|
1956
|
+
if (["recovered", "canonical"].some((k) => k in opts)) throw new Error("sign() legacy options not supported");
|
|
1957
|
+
const { hash, randomBytes } = CURVE;
|
|
1958
|
+
let { lowS, prehash, extraEntropy: ent } = opts;
|
|
1959
|
+
if (lowS == null) lowS = true;
|
|
1960
|
+
msgHash = ensureBytes("msgHash", msgHash);
|
|
1961
|
+
validateSigVerOpts(opts);
|
|
1962
|
+
if (prehash) msgHash = ensureBytes("prehashed msgHash", hash(msgHash));
|
|
1963
|
+
const h1int = bits2int_modN(msgHash);
|
|
1964
|
+
const d = normPrivateKeyToScalar(privateKey);
|
|
1965
|
+
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
1966
|
+
if (ent != null && ent !== false) {
|
|
1967
|
+
const e = ent === true ? randomBytes(Fp.BYTES) : ent;
|
|
1968
|
+
seedArgs.push(ensureBytes("extraEntropy", e));
|
|
1969
|
+
}
|
|
1970
|
+
const seed = concatBytes(...seedArgs);
|
|
1971
|
+
const m = h1int;
|
|
1972
|
+
function k2sig(kBytes) {
|
|
1973
|
+
const k = bits2int(kBytes);
|
|
1974
|
+
if (!isWithinCurveOrder(k)) return;
|
|
1975
|
+
const ik = invN(k);
|
|
1976
|
+
const q = Point.BASE.multiply(k).toAffine();
|
|
1977
|
+
const r = modN(q.x);
|
|
1978
|
+
if (r === _0n$1) return;
|
|
1979
|
+
const s = modN(ik * modN(m + r * d));
|
|
1980
|
+
if (s === _0n$1) return;
|
|
1981
|
+
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n$1);
|
|
1982
|
+
let normS = s;
|
|
1983
|
+
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
1984
|
+
normS = normalizeS(s);
|
|
1985
|
+
recovery ^= 1;
|
|
1986
|
+
}
|
|
1987
|
+
return new Signature(r, normS, recovery);
|
|
1988
|
+
}
|
|
1989
|
+
return {
|
|
1990
|
+
seed,
|
|
1991
|
+
k2sig
|
|
1992
|
+
};
|
|
1993
|
+
}
|
|
1994
|
+
const defaultSigOpts = {
|
|
1995
|
+
lowS: CURVE.lowS,
|
|
1996
|
+
prehash: false
|
|
1997
|
+
};
|
|
1998
|
+
const defaultVerOpts = {
|
|
1999
|
+
lowS: CURVE.lowS,
|
|
2000
|
+
prehash: false
|
|
2001
|
+
};
|
|
2002
|
+
/**
|
|
2003
|
+
* Signs message hash with a private key.
|
|
2004
|
+
* ```
|
|
2005
|
+
* sign(m, d, k) where
|
|
2006
|
+
* (x, y) = G × k
|
|
2007
|
+
* r = x mod n
|
|
2008
|
+
* s = (m + dr)/k mod n
|
|
2009
|
+
* ```
|
|
2010
|
+
* @param msgHash NOT message. msg needs to be hashed to `msgHash`, or use `prehash`.
|
|
2011
|
+
* @param privKey private key
|
|
2012
|
+
* @param opts lowS for non-malleable sigs. extraEntropy for mixing randomness into k. prehash will hash first arg.
|
|
2013
|
+
* @returns signature with recovery param
|
|
2014
|
+
*/
|
|
2015
|
+
function sign(msgHash, privKey, opts = defaultSigOpts) {
|
|
2016
|
+
const { seed, k2sig } = prepSig(msgHash, privKey, opts);
|
|
2017
|
+
const C = CURVE;
|
|
2018
|
+
return createHmacDrbg(C.hash.outputLen, C.nByteLength, C.hmac)(seed, k2sig);
|
|
2019
|
+
}
|
|
2020
|
+
Point.BASE._setWindowSize(8);
|
|
2021
|
+
/**
|
|
2022
|
+
* Verifies a signature against message hash and public key.
|
|
2023
|
+
* Rejects lowS signatures by default: to override,
|
|
2024
|
+
* specify option `{lowS: false}`. Implements section 4.1.4 from https://www.secg.org/sec1-v2.pdf:
|
|
2025
|
+
*
|
|
2026
|
+
* ```
|
|
2027
|
+
* verify(r, s, h, P) where
|
|
2028
|
+
* U1 = hs^-1 mod n
|
|
2029
|
+
* U2 = rs^-1 mod n
|
|
2030
|
+
* R = U1⋅G - U2⋅P
|
|
2031
|
+
* mod(R.x, n) == r
|
|
2032
|
+
* ```
|
|
2033
|
+
*/
|
|
2034
|
+
function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
|
|
2035
|
+
const sg = signature;
|
|
2036
|
+
msgHash = ensureBytes("msgHash", msgHash);
|
|
2037
|
+
publicKey = ensureBytes("publicKey", publicKey);
|
|
2038
|
+
const { lowS, prehash, format } = opts;
|
|
2039
|
+
validateSigVerOpts(opts);
|
|
2040
|
+
if ("strict" in opts) throw new Error("options.strict was renamed to lowS");
|
|
2041
|
+
if (format !== void 0 && format !== "compact" && format !== "der") throw new Error("format must be compact or der");
|
|
2042
|
+
const isHex = typeof sg === "string" || isBytes(sg);
|
|
2043
|
+
const isObj = !isHex && !format && typeof sg === "object" && sg !== null && typeof sg.r === "bigint" && typeof sg.s === "bigint";
|
|
2044
|
+
if (!isHex && !isObj) throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
2045
|
+
let _sig = void 0;
|
|
2046
|
+
let P;
|
|
2047
|
+
try {
|
|
2048
|
+
if (isObj) _sig = new Signature(sg.r, sg.s);
|
|
2049
|
+
if (isHex) {
|
|
2050
|
+
try {
|
|
2051
|
+
if (format !== "compact") _sig = Signature.fromDER(sg);
|
|
2052
|
+
} catch (derError) {
|
|
2053
|
+
if (!(derError instanceof DER.Err)) throw derError;
|
|
2054
|
+
}
|
|
2055
|
+
if (!_sig && format !== "der") _sig = Signature.fromCompact(sg);
|
|
2056
|
+
}
|
|
2057
|
+
P = Point.fromHex(publicKey);
|
|
2058
|
+
} catch (error) {
|
|
2059
|
+
return false;
|
|
2060
|
+
}
|
|
2061
|
+
if (!_sig) return false;
|
|
2062
|
+
if (lowS && _sig.hasHighS()) return false;
|
|
2063
|
+
if (prehash) msgHash = CURVE.hash(msgHash);
|
|
2064
|
+
const { r, s } = _sig;
|
|
2065
|
+
const h = bits2int_modN(msgHash);
|
|
2066
|
+
const is = invN(s);
|
|
2067
|
+
const u1 = modN(h * is);
|
|
2068
|
+
const u2 = modN(r * is);
|
|
2069
|
+
const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine();
|
|
2070
|
+
if (!R) return false;
|
|
2071
|
+
return modN(R.x) === r;
|
|
2072
|
+
}
|
|
2073
|
+
return {
|
|
2074
|
+
CURVE,
|
|
2075
|
+
getPublicKey,
|
|
2076
|
+
getSharedSecret,
|
|
2077
|
+
sign,
|
|
2078
|
+
verify,
|
|
2079
|
+
ProjectivePoint: Point,
|
|
2080
|
+
Signature,
|
|
2081
|
+
utils
|
|
2082
|
+
};
|
|
2083
|
+
}
|
|
2084
|
+
/**
|
|
2085
|
+
* Implementation of the Shallue and van de Woestijne method for any weierstrass curve.
|
|
2086
|
+
* TODO: check if there is a way to merge this with uvRatio in Edwards; move to modular.
|
|
2087
|
+
* b = True and y = sqrt(u / v) if (u / v) is square in F, and
|
|
2088
|
+
* b = False and y = sqrt(Z * (u / v)) otherwise.
|
|
2089
|
+
* @param Fp
|
|
2090
|
+
* @param Z
|
|
2091
|
+
* @returns
|
|
2092
|
+
*/
|
|
2093
|
+
function SWUFpSqrtRatio(Fp, Z) {
|
|
2094
|
+
const q = Fp.ORDER;
|
|
2095
|
+
let l = _0n$1;
|
|
2096
|
+
for (let o = q - _1n$1; o % _2n$1 === _0n$1; o /= _2n$1) l += _1n$1;
|
|
2097
|
+
const c1 = l;
|
|
2098
|
+
const _2n_pow_c1_1 = _2n$1 << c1 - _1n$1 - _1n$1;
|
|
2099
|
+
const _2n_pow_c1 = _2n_pow_c1_1 * _2n$1;
|
|
2100
|
+
const c2 = (q - _1n$1) / _2n_pow_c1;
|
|
2101
|
+
const c3 = (c2 - _1n$1) / _2n$1;
|
|
2102
|
+
const c4 = _2n_pow_c1 - _1n$1;
|
|
2103
|
+
const c5 = _2n_pow_c1_1;
|
|
2104
|
+
const c6 = Fp.pow(Z, c2);
|
|
2105
|
+
const c7 = Fp.pow(Z, (c2 + _1n$1) / _2n$1);
|
|
2106
|
+
let sqrtRatio = (u, v) => {
|
|
2107
|
+
let tv1 = c6;
|
|
2108
|
+
let tv2 = Fp.pow(v, c4);
|
|
2109
|
+
let tv3 = Fp.sqr(tv2);
|
|
2110
|
+
tv3 = Fp.mul(tv3, v);
|
|
2111
|
+
let tv5 = Fp.mul(u, tv3);
|
|
2112
|
+
tv5 = Fp.pow(tv5, c3);
|
|
2113
|
+
tv5 = Fp.mul(tv5, tv2);
|
|
2114
|
+
tv2 = Fp.mul(tv5, v);
|
|
2115
|
+
tv3 = Fp.mul(tv5, u);
|
|
2116
|
+
let tv4 = Fp.mul(tv3, tv2);
|
|
2117
|
+
tv5 = Fp.pow(tv4, c5);
|
|
2118
|
+
let isQR = Fp.eql(tv5, Fp.ONE);
|
|
2119
|
+
tv2 = Fp.mul(tv3, c7);
|
|
2120
|
+
tv5 = Fp.mul(tv4, tv1);
|
|
2121
|
+
tv3 = Fp.cmov(tv2, tv3, isQR);
|
|
2122
|
+
tv4 = Fp.cmov(tv5, tv4, isQR);
|
|
2123
|
+
for (let i = c1; i > _1n$1; i--) {
|
|
2124
|
+
let tv5 = i - _2n$1;
|
|
2125
|
+
tv5 = _2n$1 << tv5 - _1n$1;
|
|
2126
|
+
let tvv5 = Fp.pow(tv4, tv5);
|
|
2127
|
+
const e1 = Fp.eql(tvv5, Fp.ONE);
|
|
2128
|
+
tv2 = Fp.mul(tv3, tv1);
|
|
2129
|
+
tv1 = Fp.mul(tv1, tv1);
|
|
2130
|
+
tvv5 = Fp.mul(tv4, tv1);
|
|
2131
|
+
tv3 = Fp.cmov(tv2, tv3, e1);
|
|
2132
|
+
tv4 = Fp.cmov(tvv5, tv4, e1);
|
|
2133
|
+
}
|
|
2134
|
+
return {
|
|
2135
|
+
isValid: isQR,
|
|
2136
|
+
value: tv3
|
|
2137
|
+
};
|
|
2138
|
+
};
|
|
2139
|
+
if (Fp.ORDER % _4n === _3n) {
|
|
2140
|
+
const c1 = (Fp.ORDER - _3n) / _4n;
|
|
2141
|
+
const c2 = Fp.sqrt(Fp.neg(Z));
|
|
2142
|
+
sqrtRatio = (u, v) => {
|
|
2143
|
+
let tv1 = Fp.sqr(v);
|
|
2144
|
+
const tv2 = Fp.mul(u, v);
|
|
2145
|
+
tv1 = Fp.mul(tv1, tv2);
|
|
2146
|
+
let y1 = Fp.pow(tv1, c1);
|
|
2147
|
+
y1 = Fp.mul(y1, tv2);
|
|
2148
|
+
const y2 = Fp.mul(y1, c2);
|
|
2149
|
+
const tv3 = Fp.mul(Fp.sqr(y1), v);
|
|
2150
|
+
const isQR = Fp.eql(tv3, u);
|
|
2151
|
+
return {
|
|
2152
|
+
isValid: isQR,
|
|
2153
|
+
value: Fp.cmov(y2, y1, isQR)
|
|
2154
|
+
};
|
|
2155
|
+
};
|
|
2156
|
+
}
|
|
2157
|
+
return sqrtRatio;
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* Simplified Shallue-van de Woestijne-Ulas Method
|
|
2161
|
+
* https://www.rfc-editor.org/rfc/rfc9380#section-6.6.2
|
|
2162
|
+
*/
|
|
2163
|
+
function mapToCurveSimpleSWU(Fp, opts) {
|
|
2164
|
+
validateField(Fp);
|
|
2165
|
+
if (!Fp.isValid(opts.A) || !Fp.isValid(opts.B) || !Fp.isValid(opts.Z)) throw new Error("mapToCurveSimpleSWU: invalid opts");
|
|
2166
|
+
const sqrtRatio = SWUFpSqrtRatio(Fp, opts.Z);
|
|
2167
|
+
if (!Fp.isOdd) throw new Error("Fp.isOdd is not implemented!");
|
|
2168
|
+
return (u) => {
|
|
2169
|
+
let tv1, tv2, tv3, tv4, tv5, tv6, x, y;
|
|
2170
|
+
tv1 = Fp.sqr(u);
|
|
2171
|
+
tv1 = Fp.mul(tv1, opts.Z);
|
|
2172
|
+
tv2 = Fp.sqr(tv1);
|
|
2173
|
+
tv2 = Fp.add(tv2, tv1);
|
|
2174
|
+
tv3 = Fp.add(tv2, Fp.ONE);
|
|
2175
|
+
tv3 = Fp.mul(tv3, opts.B);
|
|
2176
|
+
tv4 = Fp.cmov(opts.Z, Fp.neg(tv2), !Fp.eql(tv2, Fp.ZERO));
|
|
2177
|
+
tv4 = Fp.mul(tv4, opts.A);
|
|
2178
|
+
tv2 = Fp.sqr(tv3);
|
|
2179
|
+
tv6 = Fp.sqr(tv4);
|
|
2180
|
+
tv5 = Fp.mul(tv6, opts.A);
|
|
2181
|
+
tv2 = Fp.add(tv2, tv5);
|
|
2182
|
+
tv2 = Fp.mul(tv2, tv3);
|
|
2183
|
+
tv6 = Fp.mul(tv6, tv4);
|
|
2184
|
+
tv5 = Fp.mul(tv6, opts.B);
|
|
2185
|
+
tv2 = Fp.add(tv2, tv5);
|
|
2186
|
+
x = Fp.mul(tv1, tv3);
|
|
2187
|
+
const { isValid, value } = sqrtRatio(tv2, tv6);
|
|
2188
|
+
y = Fp.mul(tv1, u);
|
|
2189
|
+
y = Fp.mul(y, value);
|
|
2190
|
+
x = Fp.cmov(x, tv3, isValid);
|
|
2191
|
+
y = Fp.cmov(y, value, isValid);
|
|
2192
|
+
const e1 = Fp.isOdd(u) === Fp.isOdd(y);
|
|
2193
|
+
y = Fp.cmov(Fp.neg(y), y, e1);
|
|
2194
|
+
const tv4_inv = FpInvertBatch(Fp, [tv4], true)[0];
|
|
2195
|
+
x = Fp.mul(x, tv4_inv);
|
|
2196
|
+
return {
|
|
2197
|
+
x,
|
|
2198
|
+
y
|
|
2199
|
+
};
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
//#endregion
|
|
2203
|
+
//#region node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/_shortw_utils.js
|
|
2204
|
+
/**
|
|
2205
|
+
* Utilities for short weierstrass curves, combined with noble-hashes.
|
|
2206
|
+
* @module
|
|
2207
|
+
*/
|
|
2208
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2209
|
+
/** connects noble-curves to noble-hashes */
|
|
2210
|
+
function getHash(hash) {
|
|
2211
|
+
return {
|
|
2212
|
+
hash,
|
|
2213
|
+
hmac: (key, ...msgs) => hmac(hash, key, concatBytes$1(...msgs)),
|
|
2214
|
+
randomBytes
|
|
2215
|
+
};
|
|
2216
|
+
}
|
|
2217
|
+
function createCurve(curveDef, defHash) {
|
|
2218
|
+
const create = (hash) => weierstrass({
|
|
2219
|
+
...curveDef,
|
|
2220
|
+
...getHash(hash)
|
|
2221
|
+
});
|
|
2222
|
+
return {
|
|
2223
|
+
...create(defHash),
|
|
2224
|
+
create
|
|
2225
|
+
};
|
|
2226
|
+
}
|
|
2227
|
+
//#endregion
|
|
2228
|
+
//#region node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/hash-to-curve.js
|
|
2229
|
+
const os2ip = bytesToNumberBE;
|
|
2230
|
+
function i2osp(value, length) {
|
|
2231
|
+
anum(value);
|
|
2232
|
+
anum(length);
|
|
2233
|
+
if (value < 0 || value >= 1 << 8 * length) throw new Error("invalid I2OSP input: " + value);
|
|
2234
|
+
const res = Array.from({ length }).fill(0);
|
|
2235
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
2236
|
+
res[i] = value & 255;
|
|
2237
|
+
value >>>= 8;
|
|
2238
|
+
}
|
|
2239
|
+
return new Uint8Array(res);
|
|
2240
|
+
}
|
|
2241
|
+
function strxor(a, b) {
|
|
2242
|
+
const arr = new Uint8Array(a.length);
|
|
2243
|
+
for (let i = 0; i < a.length; i++) arr[i] = a[i] ^ b[i];
|
|
2244
|
+
return arr;
|
|
2245
|
+
}
|
|
2246
|
+
function anum(item) {
|
|
2247
|
+
if (!Number.isSafeInteger(item)) throw new Error("number expected");
|
|
2248
|
+
}
|
|
2249
|
+
/**
|
|
2250
|
+
* Produces a uniformly random byte string using a cryptographic hash function H that outputs b bits.
|
|
2251
|
+
* [RFC 9380 5.3.1](https://www.rfc-editor.org/rfc/rfc9380#section-5.3.1).
|
|
2252
|
+
*/
|
|
2253
|
+
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
2254
|
+
abytes$1(msg);
|
|
2255
|
+
abytes$1(DST);
|
|
2256
|
+
anum(lenInBytes);
|
|
2257
|
+
if (DST.length > 255) DST = H(concatBytes(utf8ToBytes("H2C-OVERSIZE-DST-"), DST));
|
|
2258
|
+
const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
|
|
2259
|
+
const ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
2260
|
+
if (lenInBytes > 65535 || ell > 255) throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
2261
|
+
const DST_prime = concatBytes(DST, i2osp(DST.length, 1));
|
|
2262
|
+
const Z_pad = i2osp(0, r_in_bytes);
|
|
2263
|
+
const l_i_b_str = i2osp(lenInBytes, 2);
|
|
2264
|
+
const b = new Array(ell);
|
|
2265
|
+
const b_0 = H(concatBytes(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
|
|
2266
|
+
b[0] = H(concatBytes(b_0, i2osp(1, 1), DST_prime));
|
|
2267
|
+
for (let i = 1; i <= ell; i++) b[i] = H(concatBytes(...[
|
|
2268
|
+
strxor(b_0, b[i - 1]),
|
|
2269
|
+
i2osp(i + 1, 1),
|
|
2270
|
+
DST_prime
|
|
2271
|
+
]));
|
|
2272
|
+
return concatBytes(...b).slice(0, lenInBytes);
|
|
2273
|
+
}
|
|
2274
|
+
/**
|
|
2275
|
+
* Produces a uniformly random byte string using an extendable-output function (XOF) H.
|
|
2276
|
+
* 1. The collision resistance of H MUST be at least k bits.
|
|
2277
|
+
* 2. H MUST be an XOF that has been proved indifferentiable from
|
|
2278
|
+
* a random oracle under a reasonable cryptographic assumption.
|
|
2279
|
+
* [RFC 9380 5.3.2](https://www.rfc-editor.org/rfc/rfc9380#section-5.3.2).
|
|
2280
|
+
*/
|
|
2281
|
+
function expand_message_xof(msg, DST, lenInBytes, k, H) {
|
|
2282
|
+
abytes$1(msg);
|
|
2283
|
+
abytes$1(DST);
|
|
2284
|
+
anum(lenInBytes);
|
|
2285
|
+
if (DST.length > 255) {
|
|
2286
|
+
const dkLen = Math.ceil(2 * k / 8);
|
|
2287
|
+
DST = H.create({ dkLen }).update(utf8ToBytes("H2C-OVERSIZE-DST-")).update(DST).digest();
|
|
2288
|
+
}
|
|
2289
|
+
if (lenInBytes > 65535 || DST.length > 255) throw new Error("expand_message_xof: invalid lenInBytes");
|
|
2290
|
+
return H.create({ dkLen: lenInBytes }).update(msg).update(i2osp(lenInBytes, 2)).update(DST).update(i2osp(DST.length, 1)).digest();
|
|
2291
|
+
}
|
|
2292
|
+
/**
|
|
2293
|
+
* Hashes arbitrary-length byte strings to a list of one or more elements of a finite field F.
|
|
2294
|
+
* [RFC 9380 5.2](https://www.rfc-editor.org/rfc/rfc9380#section-5.2).
|
|
2295
|
+
* @param msg a byte string containing the message to hash
|
|
2296
|
+
* @param count the number of elements of F to output
|
|
2297
|
+
* @param options `{DST: string, p: bigint, m: number, k: number, expand: 'xmd' | 'xof', hash: H}`, see above
|
|
2298
|
+
* @returns [u_0, ..., u_(count - 1)], a list of field elements.
|
|
2299
|
+
*/
|
|
2300
|
+
function hash_to_field(msg, count, options) {
|
|
2301
|
+
validateObject(options, {
|
|
2302
|
+
DST: "stringOrUint8Array",
|
|
2303
|
+
p: "bigint",
|
|
2304
|
+
m: "isSafeInteger",
|
|
2305
|
+
k: "isSafeInteger",
|
|
2306
|
+
hash: "hash"
|
|
2307
|
+
});
|
|
2308
|
+
const { p, k, m, hash, expand, DST: _DST } = options;
|
|
2309
|
+
abytes$1(msg);
|
|
2310
|
+
anum(count);
|
|
2311
|
+
const DST = typeof _DST === "string" ? utf8ToBytes(_DST) : _DST;
|
|
2312
|
+
const log2p = p.toString(2).length;
|
|
2313
|
+
const L = Math.ceil((log2p + k) / 8);
|
|
2314
|
+
const len_in_bytes = count * m * L;
|
|
2315
|
+
let prb;
|
|
2316
|
+
if (expand === "xmd") prb = expand_message_xmd(msg, DST, len_in_bytes, hash);
|
|
2317
|
+
else if (expand === "xof") prb = expand_message_xof(msg, DST, len_in_bytes, k, hash);
|
|
2318
|
+
else if (expand === "_internal_pass") prb = msg;
|
|
2319
|
+
else throw new Error("expand must be \"xmd\" or \"xof\"");
|
|
2320
|
+
const u = new Array(count);
|
|
2321
|
+
for (let i = 0; i < count; i++) {
|
|
2322
|
+
const e = new Array(m);
|
|
2323
|
+
for (let j = 0; j < m; j++) {
|
|
2324
|
+
const elm_offset = L * (j + i * m);
|
|
2325
|
+
e[j] = mod(os2ip(prb.subarray(elm_offset, elm_offset + L)), p);
|
|
2326
|
+
}
|
|
2327
|
+
u[i] = e;
|
|
2328
|
+
}
|
|
2329
|
+
return u;
|
|
2330
|
+
}
|
|
2331
|
+
function isogenyMap(field, map) {
|
|
2332
|
+
const coeff = map.map((i) => Array.from(i).reverse());
|
|
2333
|
+
return (x, y) => {
|
|
2334
|
+
const [xn, xd, yn, yd] = coeff.map((val) => val.reduce((acc, i) => field.add(field.mul(acc, x), i)));
|
|
2335
|
+
const [xd_inv, yd_inv] = FpInvertBatch(field, [xd, yd], true);
|
|
2336
|
+
x = field.mul(xn, xd_inv);
|
|
2337
|
+
y = field.mul(y, field.mul(yn, yd_inv));
|
|
2338
|
+
return {
|
|
2339
|
+
x,
|
|
2340
|
+
y
|
|
2341
|
+
};
|
|
2342
|
+
};
|
|
2343
|
+
}
|
|
2344
|
+
/** Creates hash-to-curve methods from EC Point and mapToCurve function. */
|
|
2345
|
+
function createHasher(Point, mapToCurve, defaults) {
|
|
2346
|
+
if (typeof mapToCurve !== "function") throw new Error("mapToCurve() must be defined");
|
|
2347
|
+
function map(num) {
|
|
2348
|
+
return Point.fromAffine(mapToCurve(num));
|
|
2349
|
+
}
|
|
2350
|
+
function clear(initial) {
|
|
2351
|
+
const P = initial.clearCofactor();
|
|
2352
|
+
if (P.equals(Point.ZERO)) return Point.ZERO;
|
|
2353
|
+
P.assertValidity();
|
|
2354
|
+
return P;
|
|
2355
|
+
}
|
|
2356
|
+
return {
|
|
2357
|
+
defaults,
|
|
2358
|
+
hashToCurve(msg, options) {
|
|
2359
|
+
const u = hash_to_field(msg, 2, {
|
|
2360
|
+
...defaults,
|
|
2361
|
+
DST: defaults.DST,
|
|
2362
|
+
...options
|
|
2363
|
+
});
|
|
2364
|
+
const u0 = map(u[0]);
|
|
2365
|
+
const u1 = map(u[1]);
|
|
2366
|
+
return clear(u0.add(u1));
|
|
2367
|
+
},
|
|
2368
|
+
encodeToCurve(msg, options) {
|
|
2369
|
+
return clear(map(hash_to_field(msg, 1, {
|
|
2370
|
+
...defaults,
|
|
2371
|
+
DST: defaults.encodeDST,
|
|
2372
|
+
...options
|
|
2373
|
+
})[0]));
|
|
2374
|
+
},
|
|
2375
|
+
mapToCurve(scalars) {
|
|
2376
|
+
if (!Array.isArray(scalars)) throw new Error("expected array of bigints");
|
|
2377
|
+
for (const i of scalars) if (typeof i !== "bigint") throw new Error("expected array of bigints");
|
|
2378
|
+
return clear(map(scalars));
|
|
2379
|
+
}
|
|
2380
|
+
};
|
|
2381
|
+
}
|
|
2382
|
+
//#endregion
|
|
2383
|
+
//#region node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/secp256k1.js
|
|
2384
|
+
/**
|
|
2385
|
+
* NIST secp256k1. See [pdf](https://www.secg.org/sec2-v2.pdf).
|
|
2386
|
+
*
|
|
2387
|
+
* Seems to be rigid (not backdoored)
|
|
2388
|
+
* [as per discussion](https://bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975).
|
|
2389
|
+
*
|
|
2390
|
+
* secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
|
|
2391
|
+
* Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
|
|
2392
|
+
* For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
|
|
2393
|
+
* [See explanation](https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066).
|
|
2394
|
+
* @module
|
|
2395
|
+
*/
|
|
2396
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2397
|
+
var secp256k1_exports = /* @__PURE__ */ __exportAll({
|
|
2398
|
+
secp256k1: () => secp256k1,
|
|
2399
|
+
secp256k1_hasher: () => secp256k1_hasher
|
|
2400
|
+
});
|
|
2401
|
+
const secp256k1P = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f");
|
|
2402
|
+
const secp256k1N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
|
|
2403
|
+
const _0n = BigInt(0);
|
|
2404
|
+
const _1n = BigInt(1);
|
|
2405
|
+
const _2n = BigInt(2);
|
|
2406
|
+
const divNearest = (a, b) => (a + b / _2n) / b;
|
|
2407
|
+
/**
|
|
2408
|
+
* √n = n^((p+1)/4) for fields p = 3 mod 4. We unwrap the loop and multiply bit-by-bit.
|
|
2409
|
+
* (P+1n/4n).toString(2) would produce bits [223x 1, 0, 22x 1, 4x 0, 11, 00]
|
|
2410
|
+
*/
|
|
2411
|
+
function sqrtMod(y) {
|
|
2412
|
+
const P = secp256k1P;
|
|
2413
|
+
const _3n = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
2414
|
+
const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
|
|
2415
|
+
const b2 = y * y * y % P;
|
|
2416
|
+
const b3 = b2 * b2 * y % P;
|
|
2417
|
+
const b11 = pow2(pow2(pow2(b3, _3n, P) * b3 % P, _3n, P) * b3 % P, _2n, P) * b2 % P;
|
|
2418
|
+
const b22 = pow2(b11, _11n, P) * b11 % P;
|
|
2419
|
+
const b44 = pow2(b22, _22n, P) * b22 % P;
|
|
2420
|
+
const b88 = pow2(b44, _44n, P) * b44 % P;
|
|
2421
|
+
const root = pow2(pow2(pow2(pow2(pow2(pow2(b88, _88n, P) * b88 % P, _44n, P) * b44 % P, _3n, P) * b3 % P, _23n, P) * b22 % P, _6n, P) * b2 % P, _2n, P);
|
|
2422
|
+
if (!Fpk1.eql(Fpk1.sqr(root), y)) throw new Error("Cannot find square root");
|
|
2423
|
+
return root;
|
|
2424
|
+
}
|
|
2425
|
+
const Fpk1 = Field(secp256k1P, void 0, void 0, { sqrt: sqrtMod });
|
|
2426
|
+
/**
|
|
2427
|
+
* secp256k1 curve, ECDSA and ECDH methods.
|
|
2428
|
+
*
|
|
2429
|
+
* Field: `2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n`
|
|
2430
|
+
*
|
|
2431
|
+
* @example
|
|
2432
|
+
* ```js
|
|
2433
|
+
* import { secp256k1 } from '@noble/curves/secp256k1';
|
|
2434
|
+
* const priv = secp256k1.utils.randomPrivateKey();
|
|
2435
|
+
* const pub = secp256k1.getPublicKey(priv);
|
|
2436
|
+
* const msg = new Uint8Array(32).fill(1); // message hash (not message) in ecdsa
|
|
2437
|
+
* const sig = secp256k1.sign(msg, priv); // `{prehash: true}` option is available
|
|
2438
|
+
* const isValid = secp256k1.verify(sig, msg, pub) === true;
|
|
2439
|
+
* ```
|
|
2440
|
+
*/
|
|
2441
|
+
const secp256k1 = createCurve({
|
|
2442
|
+
a: _0n,
|
|
2443
|
+
b: BigInt(7),
|
|
2444
|
+
Fp: Fpk1,
|
|
2445
|
+
n: secp256k1N,
|
|
2446
|
+
Gx: BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),
|
|
2447
|
+
Gy: BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),
|
|
2448
|
+
h: BigInt(1),
|
|
2449
|
+
lowS: true,
|
|
2450
|
+
endo: {
|
|
2451
|
+
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
2452
|
+
splitScalar: (k) => {
|
|
2453
|
+
const n = secp256k1N;
|
|
2454
|
+
const a1 = BigInt("0x3086d221a7d46bcde86c90e49284eb15");
|
|
2455
|
+
const b1 = -_1n * BigInt("0xe4437ed6010e88286f547fa90abfe4c3");
|
|
2456
|
+
const a2 = BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8");
|
|
2457
|
+
const b2 = a1;
|
|
2458
|
+
const POW_2_128 = BigInt("0x100000000000000000000000000000000");
|
|
2459
|
+
const c1 = divNearest(b2 * k, n);
|
|
2460
|
+
const c2 = divNearest(-b1 * k, n);
|
|
2461
|
+
let k1 = mod(k - c1 * a1 - c2 * a2, n);
|
|
2462
|
+
let k2 = mod(-c1 * b1 - c2 * b2, n);
|
|
2463
|
+
const k1neg = k1 > POW_2_128;
|
|
2464
|
+
const k2neg = k2 > POW_2_128;
|
|
2465
|
+
if (k1neg) k1 = n - k1;
|
|
2466
|
+
if (k2neg) k2 = n - k2;
|
|
2467
|
+
if (k1 > POW_2_128 || k2 > POW_2_128) throw new Error("splitScalar: Endomorphism failed, k=" + k);
|
|
2468
|
+
return {
|
|
2469
|
+
k1neg,
|
|
2470
|
+
k1,
|
|
2471
|
+
k2neg,
|
|
2472
|
+
k2
|
|
2473
|
+
};
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
}, sha256);
|
|
2477
|
+
secp256k1.ProjectivePoint;
|
|
2478
|
+
secp256k1.utils.randomPrivateKey;
|
|
2479
|
+
const isoMap = isogenyMap(Fpk1, [
|
|
2480
|
+
[
|
|
2481
|
+
"0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7",
|
|
2482
|
+
"0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581",
|
|
2483
|
+
"0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262",
|
|
2484
|
+
"0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c"
|
|
2485
|
+
],
|
|
2486
|
+
[
|
|
2487
|
+
"0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b",
|
|
2488
|
+
"0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14",
|
|
2489
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
2490
|
+
],
|
|
2491
|
+
[
|
|
2492
|
+
"0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c",
|
|
2493
|
+
"0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3",
|
|
2494
|
+
"0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931",
|
|
2495
|
+
"0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84"
|
|
2496
|
+
],
|
|
2497
|
+
[
|
|
2498
|
+
"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b",
|
|
2499
|
+
"0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573",
|
|
2500
|
+
"0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f",
|
|
2501
|
+
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
2502
|
+
]
|
|
2503
|
+
].map((i) => i.map((j) => BigInt(j))));
|
|
2504
|
+
const mapSWU = mapToCurveSimpleSWU(Fpk1, {
|
|
2505
|
+
A: BigInt("0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533"),
|
|
2506
|
+
B: BigInt("1771"),
|
|
2507
|
+
Z: Fpk1.create(BigInt("-11"))
|
|
2508
|
+
});
|
|
2509
|
+
/** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
|
|
2510
|
+
const secp256k1_hasher = createHasher(secp256k1.ProjectivePoint, (scalars) => {
|
|
2511
|
+
const { x, y } = mapSWU(Fpk1.create(scalars[0]));
|
|
2512
|
+
return isoMap(x, y);
|
|
2513
|
+
}, {
|
|
2514
|
+
DST: "secp256k1_XMD:SHA-256_SSWU_RO_",
|
|
2515
|
+
encodeDST: "secp256k1_XMD:SHA-256_SSWU_NU_",
|
|
2516
|
+
p: Fpk1.ORDER,
|
|
2517
|
+
m: 1,
|
|
2518
|
+
k: 128,
|
|
2519
|
+
expand: "xmd",
|
|
2520
|
+
hash: sha256
|
|
2521
|
+
});
|
|
2522
|
+
secp256k1_hasher.hashToCurve;
|
|
2523
|
+
secp256k1_hasher.encodeToCurve;
|
|
2524
|
+
//#endregion
|
|
2525
|
+
export { mapToCurveSimpleSWU as a, sha256 as c, createCurve as i, sha384 as l, secp256k1_exports as n, Field as o, createHasher as r, hmac as s, secp256k1 as t, sha512 as u };
|