@toon-protocol/client-mcp 0.26.2
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/LICENSE +190 -0
- package/README.md +261 -0
- package/dist/anon-proxy-6N362VEV-M7AX2QD7.js +24 -0
- package/dist/anon-proxy-6N362VEV-M7AX2QD7.js.map +1 -0
- package/dist/chunk-245J23EB.js +278 -0
- package/dist/chunk-245J23EB.js.map +1 -0
- package/dist/chunk-2SGZPDGE.js +625 -0
- package/dist/chunk-2SGZPDGE.js.map +1 -0
- package/dist/chunk-32QD72IL.js +83 -0
- package/dist/chunk-32QD72IL.js.map +1 -0
- package/dist/chunk-5YIZ2JQO.js +205 -0
- package/dist/chunk-5YIZ2JQO.js.map +1 -0
- package/dist/chunk-LR7W2ISE.js +657 -0
- package/dist/chunk-LR7W2ISE.js.map +1 -0
- package/dist/chunk-QTDCFXPF.js +2802 -0
- package/dist/chunk-QTDCFXPF.js.map +1 -0
- package/dist/chunk-VA7XC4FD.js +185 -0
- package/dist/chunk-VA7XC4FD.js.map +1 -0
- package/dist/chunk-WMYY5I3H.js +10818 -0
- package/dist/chunk-WMYY5I3H.js.map +1 -0
- package/dist/daemon.d.ts +1 -0
- package/dist/daemon.js +137 -0
- package/dist/daemon.js.map +1 -0
- package/dist/ed25519-OFFWPWRE.js +26 -0
- package/dist/ed25519-OFFWPWRE.js.map +1 -0
- package/dist/gateway-QOK47RKS-HB65KIKC.js +15 -0
- package/dist/gateway-QOK47RKS-HB65KIKC.js.map +1 -0
- package/dist/hmac-7WSXTWW4.js +11 -0
- package/dist/hmac-7WSXTWW4.js.map +1 -0
- package/dist/index.d.ts +642 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp.d.ts +1 -0
- package/dist/mcp.js +80 -0
- package/dist/mcp.js.map +1 -0
- package/dist/sha512-LMOIUNFJ.js +33 -0
- package/dist/sha512-LMOIUNFJ.js.map +1 -0
- package/dist/socks5-WTJBYGME-IXWLQDE7.js +138 -0
- package/dist/socks5-WTJBYGME-IXWLQDE7.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,2802 @@
|
|
|
1
|
+
import { createRequire as __cr } from 'module'; const require = __cr(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// ../../node_modules/.pnpm/@noble+hashes@2.0.1/node_modules/@noble/hashes/utils.js
|
|
4
|
+
function isBytes(a) {
|
|
5
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
6
|
+
}
|
|
7
|
+
function anumber(n, title = "") {
|
|
8
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
9
|
+
const prefix = title && `"${title}" `;
|
|
10
|
+
throw new Error(`${prefix}expected integer >= 0, got ${n}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function abytes(value, length, title = "") {
|
|
14
|
+
const bytes = isBytes(value);
|
|
15
|
+
const len = value?.length;
|
|
16
|
+
const needsLen = length !== void 0;
|
|
17
|
+
if (!bytes || needsLen && len !== length) {
|
|
18
|
+
const prefix = title && `"${title}" `;
|
|
19
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
20
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
21
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
function aexists(instance, checkFinished = true) {
|
|
26
|
+
if (instance.destroyed)
|
|
27
|
+
throw new Error("Hash instance has been destroyed");
|
|
28
|
+
if (checkFinished && instance.finished)
|
|
29
|
+
throw new Error("Hash#digest() has already been called");
|
|
30
|
+
}
|
|
31
|
+
function aoutput(out, instance) {
|
|
32
|
+
abytes(out, void 0, "digestInto() output");
|
|
33
|
+
const min = instance.outputLen;
|
|
34
|
+
if (out.length < min) {
|
|
35
|
+
throw new Error('"digestInto() output" expected to be of length >=' + min);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function clean(...arrays) {
|
|
39
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
40
|
+
arrays[i].fill(0);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function createView(arr) {
|
|
44
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
45
|
+
}
|
|
46
|
+
function rotr(word, shift) {
|
|
47
|
+
return word << 32 - shift | word >>> shift;
|
|
48
|
+
}
|
|
49
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
52
|
+
))();
|
|
53
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
54
|
+
function bytesToHex(bytes) {
|
|
55
|
+
abytes(bytes);
|
|
56
|
+
if (hasHexBuiltin)
|
|
57
|
+
return bytes.toHex();
|
|
58
|
+
let hex = "";
|
|
59
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
60
|
+
hex += hexes[bytes[i]];
|
|
61
|
+
}
|
|
62
|
+
return hex;
|
|
63
|
+
}
|
|
64
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
65
|
+
function asciiToBase16(ch) {
|
|
66
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
67
|
+
return ch - asciis._0;
|
|
68
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
69
|
+
return ch - (asciis.A - 10);
|
|
70
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
71
|
+
return ch - (asciis.a - 10);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
function hexToBytes(hex) {
|
|
75
|
+
if (typeof hex !== "string")
|
|
76
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
77
|
+
if (hasHexBuiltin)
|
|
78
|
+
return Uint8Array.fromHex(hex);
|
|
79
|
+
const hl = hex.length;
|
|
80
|
+
const al = hl / 2;
|
|
81
|
+
if (hl % 2)
|
|
82
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
83
|
+
const array = new Uint8Array(al);
|
|
84
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
85
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
86
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
87
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
88
|
+
const char = hex[hi] + hex[hi + 1];
|
|
89
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
90
|
+
}
|
|
91
|
+
array[ai] = n1 * 16 + n2;
|
|
92
|
+
}
|
|
93
|
+
return array;
|
|
94
|
+
}
|
|
95
|
+
function concatBytes(...arrays) {
|
|
96
|
+
let sum = 0;
|
|
97
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
98
|
+
const a = arrays[i];
|
|
99
|
+
abytes(a);
|
|
100
|
+
sum += a.length;
|
|
101
|
+
}
|
|
102
|
+
const res = new Uint8Array(sum);
|
|
103
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
104
|
+
const a = arrays[i];
|
|
105
|
+
res.set(a, pad);
|
|
106
|
+
pad += a.length;
|
|
107
|
+
}
|
|
108
|
+
return res;
|
|
109
|
+
}
|
|
110
|
+
function createHasher(hashCons, info = {}) {
|
|
111
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
112
|
+
const tmp = hashCons(void 0);
|
|
113
|
+
hashC.outputLen = tmp.outputLen;
|
|
114
|
+
hashC.blockLen = tmp.blockLen;
|
|
115
|
+
hashC.create = (opts) => hashCons(opts);
|
|
116
|
+
Object.assign(hashC, info);
|
|
117
|
+
return Object.freeze(hashC);
|
|
118
|
+
}
|
|
119
|
+
function randomBytes(bytesLength = 32) {
|
|
120
|
+
const cr = typeof globalThis === "object" ? globalThis.crypto : null;
|
|
121
|
+
if (typeof cr?.getRandomValues !== "function")
|
|
122
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
123
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
124
|
+
}
|
|
125
|
+
var oidNist = (suffix) => ({
|
|
126
|
+
oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, suffix])
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// ../../node_modules/.pnpm/@noble+hashes@2.0.1/node_modules/@noble/hashes/_md.js
|
|
130
|
+
function Chi(a, b, c) {
|
|
131
|
+
return a & b ^ ~a & c;
|
|
132
|
+
}
|
|
133
|
+
function Maj(a, b, c) {
|
|
134
|
+
return a & b ^ a & c ^ b & c;
|
|
135
|
+
}
|
|
136
|
+
var HashMD = class {
|
|
137
|
+
blockLen;
|
|
138
|
+
outputLen;
|
|
139
|
+
padOffset;
|
|
140
|
+
isLE;
|
|
141
|
+
// For partial updates less than block size
|
|
142
|
+
buffer;
|
|
143
|
+
view;
|
|
144
|
+
finished = false;
|
|
145
|
+
length = 0;
|
|
146
|
+
pos = 0;
|
|
147
|
+
destroyed = false;
|
|
148
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
149
|
+
this.blockLen = blockLen;
|
|
150
|
+
this.outputLen = outputLen;
|
|
151
|
+
this.padOffset = padOffset;
|
|
152
|
+
this.isLE = isLE;
|
|
153
|
+
this.buffer = new Uint8Array(blockLen);
|
|
154
|
+
this.view = createView(this.buffer);
|
|
155
|
+
}
|
|
156
|
+
update(data) {
|
|
157
|
+
aexists(this);
|
|
158
|
+
abytes(data);
|
|
159
|
+
const { view, buffer, blockLen } = this;
|
|
160
|
+
const len = data.length;
|
|
161
|
+
for (let pos = 0; pos < len; ) {
|
|
162
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
163
|
+
if (take === blockLen) {
|
|
164
|
+
const dataView = createView(data);
|
|
165
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
166
|
+
this.process(dataView, pos);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
170
|
+
this.pos += take;
|
|
171
|
+
pos += take;
|
|
172
|
+
if (this.pos === blockLen) {
|
|
173
|
+
this.process(view, 0);
|
|
174
|
+
this.pos = 0;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
this.length += data.length;
|
|
178
|
+
this.roundClean();
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
digestInto(out) {
|
|
182
|
+
aexists(this);
|
|
183
|
+
aoutput(out, this);
|
|
184
|
+
this.finished = true;
|
|
185
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
186
|
+
let { pos } = this;
|
|
187
|
+
buffer[pos++] = 128;
|
|
188
|
+
clean(this.buffer.subarray(pos));
|
|
189
|
+
if (this.padOffset > blockLen - pos) {
|
|
190
|
+
this.process(view, 0);
|
|
191
|
+
pos = 0;
|
|
192
|
+
}
|
|
193
|
+
for (let i = pos; i < blockLen; i++)
|
|
194
|
+
buffer[i] = 0;
|
|
195
|
+
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
|
|
196
|
+
this.process(view, 0);
|
|
197
|
+
const oview = createView(out);
|
|
198
|
+
const len = this.outputLen;
|
|
199
|
+
if (len % 4)
|
|
200
|
+
throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
201
|
+
const outLen = len / 4;
|
|
202
|
+
const state = this.get();
|
|
203
|
+
if (outLen > state.length)
|
|
204
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
205
|
+
for (let i = 0; i < outLen; i++)
|
|
206
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
207
|
+
}
|
|
208
|
+
digest() {
|
|
209
|
+
const { buffer, outputLen } = this;
|
|
210
|
+
this.digestInto(buffer);
|
|
211
|
+
const res = buffer.slice(0, outputLen);
|
|
212
|
+
this.destroy();
|
|
213
|
+
return res;
|
|
214
|
+
}
|
|
215
|
+
_cloneInto(to) {
|
|
216
|
+
to ||= new this.constructor();
|
|
217
|
+
to.set(...this.get());
|
|
218
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
219
|
+
to.destroyed = destroyed;
|
|
220
|
+
to.finished = finished;
|
|
221
|
+
to.length = length;
|
|
222
|
+
to.pos = pos;
|
|
223
|
+
if (length % blockLen)
|
|
224
|
+
to.buffer.set(buffer);
|
|
225
|
+
return to;
|
|
226
|
+
}
|
|
227
|
+
clone() {
|
|
228
|
+
return this._cloneInto();
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
232
|
+
1779033703,
|
|
233
|
+
3144134277,
|
|
234
|
+
1013904242,
|
|
235
|
+
2773480762,
|
|
236
|
+
1359893119,
|
|
237
|
+
2600822924,
|
|
238
|
+
528734635,
|
|
239
|
+
1541459225
|
|
240
|
+
]);
|
|
241
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
242
|
+
1779033703,
|
|
243
|
+
4089235720,
|
|
244
|
+
3144134277,
|
|
245
|
+
2227873595,
|
|
246
|
+
1013904242,
|
|
247
|
+
4271175723,
|
|
248
|
+
2773480762,
|
|
249
|
+
1595750129,
|
|
250
|
+
1359893119,
|
|
251
|
+
2917565137,
|
|
252
|
+
2600822924,
|
|
253
|
+
725511199,
|
|
254
|
+
528734635,
|
|
255
|
+
4215389547,
|
|
256
|
+
1541459225,
|
|
257
|
+
327033209
|
|
258
|
+
]);
|
|
259
|
+
|
|
260
|
+
// ../../node_modules/.pnpm/@noble+hashes@2.0.1/node_modules/@noble/hashes/_u64.js
|
|
261
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
262
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
263
|
+
function fromBig(n, le = false) {
|
|
264
|
+
if (le)
|
|
265
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
266
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
267
|
+
}
|
|
268
|
+
function split(lst, le = false) {
|
|
269
|
+
const len = lst.length;
|
|
270
|
+
let Ah = new Uint32Array(len);
|
|
271
|
+
let Al = new Uint32Array(len);
|
|
272
|
+
for (let i = 0; i < len; i++) {
|
|
273
|
+
const { h, l } = fromBig(lst[i], le);
|
|
274
|
+
[Ah[i], Al[i]] = [h, l];
|
|
275
|
+
}
|
|
276
|
+
return [Ah, Al];
|
|
277
|
+
}
|
|
278
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
279
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
280
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
281
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
282
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
283
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
284
|
+
function add(Ah, Al, Bh, Bl) {
|
|
285
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
286
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
287
|
+
}
|
|
288
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
289
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
290
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
291
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
292
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
293
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
294
|
+
|
|
295
|
+
// ../../node_modules/.pnpm/@noble+hashes@2.0.1/node_modules/@noble/hashes/sha2.js
|
|
296
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
297
|
+
1116352408,
|
|
298
|
+
1899447441,
|
|
299
|
+
3049323471,
|
|
300
|
+
3921009573,
|
|
301
|
+
961987163,
|
|
302
|
+
1508970993,
|
|
303
|
+
2453635748,
|
|
304
|
+
2870763221,
|
|
305
|
+
3624381080,
|
|
306
|
+
310598401,
|
|
307
|
+
607225278,
|
|
308
|
+
1426881987,
|
|
309
|
+
1925078388,
|
|
310
|
+
2162078206,
|
|
311
|
+
2614888103,
|
|
312
|
+
3248222580,
|
|
313
|
+
3835390401,
|
|
314
|
+
4022224774,
|
|
315
|
+
264347078,
|
|
316
|
+
604807628,
|
|
317
|
+
770255983,
|
|
318
|
+
1249150122,
|
|
319
|
+
1555081692,
|
|
320
|
+
1996064986,
|
|
321
|
+
2554220882,
|
|
322
|
+
2821834349,
|
|
323
|
+
2952996808,
|
|
324
|
+
3210313671,
|
|
325
|
+
3336571891,
|
|
326
|
+
3584528711,
|
|
327
|
+
113926993,
|
|
328
|
+
338241895,
|
|
329
|
+
666307205,
|
|
330
|
+
773529912,
|
|
331
|
+
1294757372,
|
|
332
|
+
1396182291,
|
|
333
|
+
1695183700,
|
|
334
|
+
1986661051,
|
|
335
|
+
2177026350,
|
|
336
|
+
2456956037,
|
|
337
|
+
2730485921,
|
|
338
|
+
2820302411,
|
|
339
|
+
3259730800,
|
|
340
|
+
3345764771,
|
|
341
|
+
3516065817,
|
|
342
|
+
3600352804,
|
|
343
|
+
4094571909,
|
|
344
|
+
275423344,
|
|
345
|
+
430227734,
|
|
346
|
+
506948616,
|
|
347
|
+
659060556,
|
|
348
|
+
883997877,
|
|
349
|
+
958139571,
|
|
350
|
+
1322822218,
|
|
351
|
+
1537002063,
|
|
352
|
+
1747873779,
|
|
353
|
+
1955562222,
|
|
354
|
+
2024104815,
|
|
355
|
+
2227730452,
|
|
356
|
+
2361852424,
|
|
357
|
+
2428436474,
|
|
358
|
+
2756734187,
|
|
359
|
+
3204031479,
|
|
360
|
+
3329325298
|
|
361
|
+
]);
|
|
362
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
363
|
+
var SHA2_32B = class extends HashMD {
|
|
364
|
+
constructor(outputLen) {
|
|
365
|
+
super(64, outputLen, 8, false);
|
|
366
|
+
}
|
|
367
|
+
get() {
|
|
368
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
369
|
+
return [A, B, C, D, E, F, G, H];
|
|
370
|
+
}
|
|
371
|
+
// prettier-ignore
|
|
372
|
+
set(A, B, C, D, E, F, G, H) {
|
|
373
|
+
this.A = A | 0;
|
|
374
|
+
this.B = B | 0;
|
|
375
|
+
this.C = C | 0;
|
|
376
|
+
this.D = D | 0;
|
|
377
|
+
this.E = E | 0;
|
|
378
|
+
this.F = F | 0;
|
|
379
|
+
this.G = G | 0;
|
|
380
|
+
this.H = H | 0;
|
|
381
|
+
}
|
|
382
|
+
process(view, offset) {
|
|
383
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
384
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
385
|
+
for (let i = 16; i < 64; i++) {
|
|
386
|
+
const W15 = SHA256_W[i - 15];
|
|
387
|
+
const W2 = SHA256_W[i - 2];
|
|
388
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
389
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
390
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
391
|
+
}
|
|
392
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
393
|
+
for (let i = 0; i < 64; i++) {
|
|
394
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
395
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
396
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
397
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
398
|
+
H = G;
|
|
399
|
+
G = F;
|
|
400
|
+
F = E;
|
|
401
|
+
E = D + T1 | 0;
|
|
402
|
+
D = C;
|
|
403
|
+
C = B;
|
|
404
|
+
B = A;
|
|
405
|
+
A = T1 + T2 | 0;
|
|
406
|
+
}
|
|
407
|
+
A = A + this.A | 0;
|
|
408
|
+
B = B + this.B | 0;
|
|
409
|
+
C = C + this.C | 0;
|
|
410
|
+
D = D + this.D | 0;
|
|
411
|
+
E = E + this.E | 0;
|
|
412
|
+
F = F + this.F | 0;
|
|
413
|
+
G = G + this.G | 0;
|
|
414
|
+
H = H + this.H | 0;
|
|
415
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
416
|
+
}
|
|
417
|
+
roundClean() {
|
|
418
|
+
clean(SHA256_W);
|
|
419
|
+
}
|
|
420
|
+
destroy() {
|
|
421
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
422
|
+
clean(this.buffer);
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
var _SHA256 = class extends SHA2_32B {
|
|
426
|
+
// We cannot use array here since array allows indexing by variable
|
|
427
|
+
// which means optimizer/compiler cannot use registers.
|
|
428
|
+
A = SHA256_IV[0] | 0;
|
|
429
|
+
B = SHA256_IV[1] | 0;
|
|
430
|
+
C = SHA256_IV[2] | 0;
|
|
431
|
+
D = SHA256_IV[3] | 0;
|
|
432
|
+
E = SHA256_IV[4] | 0;
|
|
433
|
+
F = SHA256_IV[5] | 0;
|
|
434
|
+
G = SHA256_IV[6] | 0;
|
|
435
|
+
H = SHA256_IV[7] | 0;
|
|
436
|
+
constructor() {
|
|
437
|
+
super(32);
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
441
|
+
"0x428a2f98d728ae22",
|
|
442
|
+
"0x7137449123ef65cd",
|
|
443
|
+
"0xb5c0fbcfec4d3b2f",
|
|
444
|
+
"0xe9b5dba58189dbbc",
|
|
445
|
+
"0x3956c25bf348b538",
|
|
446
|
+
"0x59f111f1b605d019",
|
|
447
|
+
"0x923f82a4af194f9b",
|
|
448
|
+
"0xab1c5ed5da6d8118",
|
|
449
|
+
"0xd807aa98a3030242",
|
|
450
|
+
"0x12835b0145706fbe",
|
|
451
|
+
"0x243185be4ee4b28c",
|
|
452
|
+
"0x550c7dc3d5ffb4e2",
|
|
453
|
+
"0x72be5d74f27b896f",
|
|
454
|
+
"0x80deb1fe3b1696b1",
|
|
455
|
+
"0x9bdc06a725c71235",
|
|
456
|
+
"0xc19bf174cf692694",
|
|
457
|
+
"0xe49b69c19ef14ad2",
|
|
458
|
+
"0xefbe4786384f25e3",
|
|
459
|
+
"0x0fc19dc68b8cd5b5",
|
|
460
|
+
"0x240ca1cc77ac9c65",
|
|
461
|
+
"0x2de92c6f592b0275",
|
|
462
|
+
"0x4a7484aa6ea6e483",
|
|
463
|
+
"0x5cb0a9dcbd41fbd4",
|
|
464
|
+
"0x76f988da831153b5",
|
|
465
|
+
"0x983e5152ee66dfab",
|
|
466
|
+
"0xa831c66d2db43210",
|
|
467
|
+
"0xb00327c898fb213f",
|
|
468
|
+
"0xbf597fc7beef0ee4",
|
|
469
|
+
"0xc6e00bf33da88fc2",
|
|
470
|
+
"0xd5a79147930aa725",
|
|
471
|
+
"0x06ca6351e003826f",
|
|
472
|
+
"0x142929670a0e6e70",
|
|
473
|
+
"0x27b70a8546d22ffc",
|
|
474
|
+
"0x2e1b21385c26c926",
|
|
475
|
+
"0x4d2c6dfc5ac42aed",
|
|
476
|
+
"0x53380d139d95b3df",
|
|
477
|
+
"0x650a73548baf63de",
|
|
478
|
+
"0x766a0abb3c77b2a8",
|
|
479
|
+
"0x81c2c92e47edaee6",
|
|
480
|
+
"0x92722c851482353b",
|
|
481
|
+
"0xa2bfe8a14cf10364",
|
|
482
|
+
"0xa81a664bbc423001",
|
|
483
|
+
"0xc24b8b70d0f89791",
|
|
484
|
+
"0xc76c51a30654be30",
|
|
485
|
+
"0xd192e819d6ef5218",
|
|
486
|
+
"0xd69906245565a910",
|
|
487
|
+
"0xf40e35855771202a",
|
|
488
|
+
"0x106aa07032bbd1b8",
|
|
489
|
+
"0x19a4c116b8d2d0c8",
|
|
490
|
+
"0x1e376c085141ab53",
|
|
491
|
+
"0x2748774cdf8eeb99",
|
|
492
|
+
"0x34b0bcb5e19b48a8",
|
|
493
|
+
"0x391c0cb3c5c95a63",
|
|
494
|
+
"0x4ed8aa4ae3418acb",
|
|
495
|
+
"0x5b9cca4f7763e373",
|
|
496
|
+
"0x682e6ff3d6b2b8a3",
|
|
497
|
+
"0x748f82ee5defb2fc",
|
|
498
|
+
"0x78a5636f43172f60",
|
|
499
|
+
"0x84c87814a1f0ab72",
|
|
500
|
+
"0x8cc702081a6439ec",
|
|
501
|
+
"0x90befffa23631e28",
|
|
502
|
+
"0xa4506cebde82bde9",
|
|
503
|
+
"0xbef9a3f7b2c67915",
|
|
504
|
+
"0xc67178f2e372532b",
|
|
505
|
+
"0xca273eceea26619c",
|
|
506
|
+
"0xd186b8c721c0c207",
|
|
507
|
+
"0xeada7dd6cde0eb1e",
|
|
508
|
+
"0xf57d4f7fee6ed178",
|
|
509
|
+
"0x06f067aa72176fba",
|
|
510
|
+
"0x0a637dc5a2c898a6",
|
|
511
|
+
"0x113f9804bef90dae",
|
|
512
|
+
"0x1b710b35131c471b",
|
|
513
|
+
"0x28db77f523047d84",
|
|
514
|
+
"0x32caab7b40c72493",
|
|
515
|
+
"0x3c9ebe0a15c9bebc",
|
|
516
|
+
"0x431d67c49c100d4c",
|
|
517
|
+
"0x4cc5d4becb3e42b6",
|
|
518
|
+
"0x597f299cfc657e2a",
|
|
519
|
+
"0x5fcb6fab3ad6faec",
|
|
520
|
+
"0x6c44198c4a475817"
|
|
521
|
+
].map((n) => BigInt(n))))();
|
|
522
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
523
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
524
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
525
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
526
|
+
var SHA2_64B = class extends HashMD {
|
|
527
|
+
constructor(outputLen) {
|
|
528
|
+
super(128, outputLen, 16, false);
|
|
529
|
+
}
|
|
530
|
+
// prettier-ignore
|
|
531
|
+
get() {
|
|
532
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
533
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
534
|
+
}
|
|
535
|
+
// prettier-ignore
|
|
536
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
537
|
+
this.Ah = Ah | 0;
|
|
538
|
+
this.Al = Al | 0;
|
|
539
|
+
this.Bh = Bh | 0;
|
|
540
|
+
this.Bl = Bl | 0;
|
|
541
|
+
this.Ch = Ch | 0;
|
|
542
|
+
this.Cl = Cl | 0;
|
|
543
|
+
this.Dh = Dh | 0;
|
|
544
|
+
this.Dl = Dl | 0;
|
|
545
|
+
this.Eh = Eh | 0;
|
|
546
|
+
this.El = El | 0;
|
|
547
|
+
this.Fh = Fh | 0;
|
|
548
|
+
this.Fl = Fl | 0;
|
|
549
|
+
this.Gh = Gh | 0;
|
|
550
|
+
this.Gl = Gl | 0;
|
|
551
|
+
this.Hh = Hh | 0;
|
|
552
|
+
this.Hl = Hl | 0;
|
|
553
|
+
}
|
|
554
|
+
process(view, offset) {
|
|
555
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
556
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
557
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
558
|
+
}
|
|
559
|
+
for (let i = 16; i < 80; i++) {
|
|
560
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
561
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
562
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
563
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
564
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
565
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
566
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
567
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
568
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
569
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
570
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
571
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
572
|
+
}
|
|
573
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
574
|
+
for (let i = 0; i < 80; i++) {
|
|
575
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
576
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
577
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
578
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
579
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
580
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
581
|
+
const T1l = T1ll | 0;
|
|
582
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
583
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
584
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
585
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
586
|
+
Hh = Gh | 0;
|
|
587
|
+
Hl = Gl | 0;
|
|
588
|
+
Gh = Fh | 0;
|
|
589
|
+
Gl = Fl | 0;
|
|
590
|
+
Fh = Eh | 0;
|
|
591
|
+
Fl = El | 0;
|
|
592
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
593
|
+
Dh = Ch | 0;
|
|
594
|
+
Dl = Cl | 0;
|
|
595
|
+
Ch = Bh | 0;
|
|
596
|
+
Cl = Bl | 0;
|
|
597
|
+
Bh = Ah | 0;
|
|
598
|
+
Bl = Al | 0;
|
|
599
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
600
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
601
|
+
Al = All | 0;
|
|
602
|
+
}
|
|
603
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
604
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
605
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
606
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
607
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
608
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
609
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
610
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
611
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
612
|
+
}
|
|
613
|
+
roundClean() {
|
|
614
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
615
|
+
}
|
|
616
|
+
destroy() {
|
|
617
|
+
clean(this.buffer);
|
|
618
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
var _SHA512 = class extends SHA2_64B {
|
|
622
|
+
Ah = SHA512_IV[0] | 0;
|
|
623
|
+
Al = SHA512_IV[1] | 0;
|
|
624
|
+
Bh = SHA512_IV[2] | 0;
|
|
625
|
+
Bl = SHA512_IV[3] | 0;
|
|
626
|
+
Ch = SHA512_IV[4] | 0;
|
|
627
|
+
Cl = SHA512_IV[5] | 0;
|
|
628
|
+
Dh = SHA512_IV[6] | 0;
|
|
629
|
+
Dl = SHA512_IV[7] | 0;
|
|
630
|
+
Eh = SHA512_IV[8] | 0;
|
|
631
|
+
El = SHA512_IV[9] | 0;
|
|
632
|
+
Fh = SHA512_IV[10] | 0;
|
|
633
|
+
Fl = SHA512_IV[11] | 0;
|
|
634
|
+
Gh = SHA512_IV[12] | 0;
|
|
635
|
+
Gl = SHA512_IV[13] | 0;
|
|
636
|
+
Hh = SHA512_IV[14] | 0;
|
|
637
|
+
Hl = SHA512_IV[15] | 0;
|
|
638
|
+
constructor() {
|
|
639
|
+
super(64);
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
var sha256 = /* @__PURE__ */ createHasher(
|
|
643
|
+
() => new _SHA256(),
|
|
644
|
+
/* @__PURE__ */ oidNist(1)
|
|
645
|
+
);
|
|
646
|
+
var sha512 = /* @__PURE__ */ createHasher(
|
|
647
|
+
() => new _SHA512(),
|
|
648
|
+
/* @__PURE__ */ oidNist(3)
|
|
649
|
+
);
|
|
650
|
+
|
|
651
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/utils.js
|
|
652
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
653
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
654
|
+
function abool(value, title = "") {
|
|
655
|
+
if (typeof value !== "boolean") {
|
|
656
|
+
const prefix = title && `"${title}" `;
|
|
657
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
658
|
+
}
|
|
659
|
+
return value;
|
|
660
|
+
}
|
|
661
|
+
function abignumber(n) {
|
|
662
|
+
if (typeof n === "bigint") {
|
|
663
|
+
if (!isPosBig(n))
|
|
664
|
+
throw new Error("positive bigint expected, got " + n);
|
|
665
|
+
} else
|
|
666
|
+
anumber(n);
|
|
667
|
+
return n;
|
|
668
|
+
}
|
|
669
|
+
function asafenumber(value, title = "") {
|
|
670
|
+
if (!Number.isSafeInteger(value)) {
|
|
671
|
+
const prefix = title && `"${title}" `;
|
|
672
|
+
throw new Error(prefix + "expected safe integer, got type=" + typeof value);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
function hexToNumber(hex) {
|
|
676
|
+
if (typeof hex !== "string")
|
|
677
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
678
|
+
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
679
|
+
}
|
|
680
|
+
function bytesToNumberBE(bytes) {
|
|
681
|
+
return hexToNumber(bytesToHex(bytes));
|
|
682
|
+
}
|
|
683
|
+
function bytesToNumberLE(bytes) {
|
|
684
|
+
return hexToNumber(bytesToHex(copyBytes(abytes(bytes)).reverse()));
|
|
685
|
+
}
|
|
686
|
+
function numberToBytesBE(n, len) {
|
|
687
|
+
anumber(len);
|
|
688
|
+
n = abignumber(n);
|
|
689
|
+
const res = hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
690
|
+
if (res.length !== len)
|
|
691
|
+
throw new Error("number too large");
|
|
692
|
+
return res;
|
|
693
|
+
}
|
|
694
|
+
function numberToBytesLE(n, len) {
|
|
695
|
+
return numberToBytesBE(n, len).reverse();
|
|
696
|
+
}
|
|
697
|
+
function equalBytes(a, b) {
|
|
698
|
+
if (a.length !== b.length)
|
|
699
|
+
return false;
|
|
700
|
+
let diff = 0;
|
|
701
|
+
for (let i = 0; i < a.length; i++)
|
|
702
|
+
diff |= a[i] ^ b[i];
|
|
703
|
+
return diff === 0;
|
|
704
|
+
}
|
|
705
|
+
function copyBytes(bytes) {
|
|
706
|
+
return Uint8Array.from(bytes);
|
|
707
|
+
}
|
|
708
|
+
function asciiToBytes(ascii) {
|
|
709
|
+
return Uint8Array.from(ascii, (c, i) => {
|
|
710
|
+
const charCode = c.charCodeAt(0);
|
|
711
|
+
if (c.length !== 1 || charCode > 127) {
|
|
712
|
+
throw new Error(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
|
|
713
|
+
}
|
|
714
|
+
return charCode;
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
718
|
+
function inRange(n, min, max) {
|
|
719
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
720
|
+
}
|
|
721
|
+
function aInRange(title, n, min, max) {
|
|
722
|
+
if (!inRange(n, min, max))
|
|
723
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
724
|
+
}
|
|
725
|
+
function bitLen(n) {
|
|
726
|
+
let len;
|
|
727
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
728
|
+
;
|
|
729
|
+
return len;
|
|
730
|
+
}
|
|
731
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
732
|
+
function validateObject(object, fields = {}, optFields = {}) {
|
|
733
|
+
if (!object || typeof object !== "object")
|
|
734
|
+
throw new Error("expected valid options object");
|
|
735
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
736
|
+
const val = object[fieldName];
|
|
737
|
+
if (isOpt && val === void 0)
|
|
738
|
+
return;
|
|
739
|
+
const current = typeof val;
|
|
740
|
+
if (current !== expectedType || val === null)
|
|
741
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
742
|
+
}
|
|
743
|
+
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
744
|
+
iter(fields, false);
|
|
745
|
+
iter(optFields, true);
|
|
746
|
+
}
|
|
747
|
+
var notImplemented = () => {
|
|
748
|
+
throw new Error("not implemented");
|
|
749
|
+
};
|
|
750
|
+
function memoized(fn) {
|
|
751
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
752
|
+
return (arg, ...args) => {
|
|
753
|
+
const val = map.get(arg);
|
|
754
|
+
if (val !== void 0)
|
|
755
|
+
return val;
|
|
756
|
+
const computed = fn(arg, ...args);
|
|
757
|
+
map.set(arg, computed);
|
|
758
|
+
return computed;
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/abstract/modular.js
|
|
763
|
+
var _0n2 = /* @__PURE__ */ BigInt(0);
|
|
764
|
+
var _1n2 = /* @__PURE__ */ BigInt(1);
|
|
765
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
766
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
767
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
768
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
769
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
770
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
771
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
772
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
773
|
+
function mod(a, b) {
|
|
774
|
+
const result = a % b;
|
|
775
|
+
return result >= _0n2 ? result : b + result;
|
|
776
|
+
}
|
|
777
|
+
function pow2(x, power, modulo) {
|
|
778
|
+
let res = x;
|
|
779
|
+
while (power-- > _0n2) {
|
|
780
|
+
res *= res;
|
|
781
|
+
res %= modulo;
|
|
782
|
+
}
|
|
783
|
+
return res;
|
|
784
|
+
}
|
|
785
|
+
function invert(number, modulo) {
|
|
786
|
+
if (number === _0n2)
|
|
787
|
+
throw new Error("invert: expected non-zero number");
|
|
788
|
+
if (modulo <= _0n2)
|
|
789
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
790
|
+
let a = mod(number, modulo);
|
|
791
|
+
let b = modulo;
|
|
792
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
793
|
+
while (a !== _0n2) {
|
|
794
|
+
const q = b / a;
|
|
795
|
+
const r = b % a;
|
|
796
|
+
const m = x - u * q;
|
|
797
|
+
const n = y - v * q;
|
|
798
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
799
|
+
}
|
|
800
|
+
const gcd = b;
|
|
801
|
+
if (gcd !== _1n2)
|
|
802
|
+
throw new Error("invert: does not exist");
|
|
803
|
+
return mod(x, modulo);
|
|
804
|
+
}
|
|
805
|
+
function assertIsSquare(Fp2, root, n) {
|
|
806
|
+
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
807
|
+
throw new Error("Cannot find square root");
|
|
808
|
+
}
|
|
809
|
+
function sqrt3mod4(Fp2, n) {
|
|
810
|
+
const p1div4 = (Fp2.ORDER + _1n2) / _4n;
|
|
811
|
+
const root = Fp2.pow(n, p1div4);
|
|
812
|
+
assertIsSquare(Fp2, root, n);
|
|
813
|
+
return root;
|
|
814
|
+
}
|
|
815
|
+
function sqrt5mod8(Fp2, n) {
|
|
816
|
+
const p5div8 = (Fp2.ORDER - _5n) / _8n;
|
|
817
|
+
const n2 = Fp2.mul(n, _2n);
|
|
818
|
+
const v = Fp2.pow(n2, p5div8);
|
|
819
|
+
const nv = Fp2.mul(n, v);
|
|
820
|
+
const i = Fp2.mul(Fp2.mul(nv, _2n), v);
|
|
821
|
+
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
822
|
+
assertIsSquare(Fp2, root, n);
|
|
823
|
+
return root;
|
|
824
|
+
}
|
|
825
|
+
function sqrt9mod16(P) {
|
|
826
|
+
const Fp_ = Field(P);
|
|
827
|
+
const tn = tonelliShanks(P);
|
|
828
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
829
|
+
const c2 = tn(Fp_, c1);
|
|
830
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
831
|
+
const c4 = (P + _7n) / _16n;
|
|
832
|
+
return (Fp2, n) => {
|
|
833
|
+
let tv1 = Fp2.pow(n, c4);
|
|
834
|
+
let tv2 = Fp2.mul(tv1, c1);
|
|
835
|
+
const tv3 = Fp2.mul(tv1, c2);
|
|
836
|
+
const tv4 = Fp2.mul(tv1, c3);
|
|
837
|
+
const e1 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
838
|
+
const e2 = Fp2.eql(Fp2.sqr(tv3), n);
|
|
839
|
+
tv1 = Fp2.cmov(tv1, tv2, e1);
|
|
840
|
+
tv2 = Fp2.cmov(tv4, tv3, e2);
|
|
841
|
+
const e3 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
842
|
+
const root = Fp2.cmov(tv1, tv2, e3);
|
|
843
|
+
assertIsSquare(Fp2, root, n);
|
|
844
|
+
return root;
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
function tonelliShanks(P) {
|
|
848
|
+
if (P < _3n)
|
|
849
|
+
throw new Error("sqrt is not defined for small field");
|
|
850
|
+
let Q = P - _1n2;
|
|
851
|
+
let S = 0;
|
|
852
|
+
while (Q % _2n === _0n2) {
|
|
853
|
+
Q /= _2n;
|
|
854
|
+
S++;
|
|
855
|
+
}
|
|
856
|
+
let Z = _2n;
|
|
857
|
+
const _Fp = Field(P);
|
|
858
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
859
|
+
if (Z++ > 1e3)
|
|
860
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
861
|
+
}
|
|
862
|
+
if (S === 1)
|
|
863
|
+
return sqrt3mod4;
|
|
864
|
+
let cc = _Fp.pow(Z, Q);
|
|
865
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
866
|
+
return function tonelliSlow(Fp2, n) {
|
|
867
|
+
if (Fp2.is0(n))
|
|
868
|
+
return n;
|
|
869
|
+
if (FpLegendre(Fp2, n) !== 1)
|
|
870
|
+
throw new Error("Cannot find square root");
|
|
871
|
+
let M = S;
|
|
872
|
+
let c = Fp2.mul(Fp2.ONE, cc);
|
|
873
|
+
let t = Fp2.pow(n, Q);
|
|
874
|
+
let R = Fp2.pow(n, Q1div2);
|
|
875
|
+
while (!Fp2.eql(t, Fp2.ONE)) {
|
|
876
|
+
if (Fp2.is0(t))
|
|
877
|
+
return Fp2.ZERO;
|
|
878
|
+
let i = 1;
|
|
879
|
+
let t_tmp = Fp2.sqr(t);
|
|
880
|
+
while (!Fp2.eql(t_tmp, Fp2.ONE)) {
|
|
881
|
+
i++;
|
|
882
|
+
t_tmp = Fp2.sqr(t_tmp);
|
|
883
|
+
if (i === M)
|
|
884
|
+
throw new Error("Cannot find square root");
|
|
885
|
+
}
|
|
886
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
887
|
+
const b = Fp2.pow(c, exponent);
|
|
888
|
+
M = i;
|
|
889
|
+
c = Fp2.sqr(b);
|
|
890
|
+
t = Fp2.mul(t, c);
|
|
891
|
+
R = Fp2.mul(R, b);
|
|
892
|
+
}
|
|
893
|
+
return R;
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
function FpSqrt(P) {
|
|
897
|
+
if (P % _4n === _3n)
|
|
898
|
+
return sqrt3mod4;
|
|
899
|
+
if (P % _8n === _5n)
|
|
900
|
+
return sqrt5mod8;
|
|
901
|
+
if (P % _16n === _9n)
|
|
902
|
+
return sqrt9mod16(P);
|
|
903
|
+
return tonelliShanks(P);
|
|
904
|
+
}
|
|
905
|
+
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
906
|
+
var FIELD_FIELDS = [
|
|
907
|
+
"create",
|
|
908
|
+
"isValid",
|
|
909
|
+
"is0",
|
|
910
|
+
"neg",
|
|
911
|
+
"inv",
|
|
912
|
+
"sqrt",
|
|
913
|
+
"sqr",
|
|
914
|
+
"eql",
|
|
915
|
+
"add",
|
|
916
|
+
"sub",
|
|
917
|
+
"mul",
|
|
918
|
+
"pow",
|
|
919
|
+
"div",
|
|
920
|
+
"addN",
|
|
921
|
+
"subN",
|
|
922
|
+
"mulN",
|
|
923
|
+
"sqrN"
|
|
924
|
+
];
|
|
925
|
+
function validateField(field) {
|
|
926
|
+
const initial = {
|
|
927
|
+
ORDER: "bigint",
|
|
928
|
+
BYTES: "number",
|
|
929
|
+
BITS: "number"
|
|
930
|
+
};
|
|
931
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
932
|
+
map[val] = "function";
|
|
933
|
+
return map;
|
|
934
|
+
}, initial);
|
|
935
|
+
validateObject(field, opts);
|
|
936
|
+
return field;
|
|
937
|
+
}
|
|
938
|
+
function FpPow(Fp2, num, power) {
|
|
939
|
+
if (power < _0n2)
|
|
940
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
941
|
+
if (power === _0n2)
|
|
942
|
+
return Fp2.ONE;
|
|
943
|
+
if (power === _1n2)
|
|
944
|
+
return num;
|
|
945
|
+
let p = Fp2.ONE;
|
|
946
|
+
let d = num;
|
|
947
|
+
while (power > _0n2) {
|
|
948
|
+
if (power & _1n2)
|
|
949
|
+
p = Fp2.mul(p, d);
|
|
950
|
+
d = Fp2.sqr(d);
|
|
951
|
+
power >>= _1n2;
|
|
952
|
+
}
|
|
953
|
+
return p;
|
|
954
|
+
}
|
|
955
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
956
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
|
|
957
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
958
|
+
if (Fp2.is0(num))
|
|
959
|
+
return acc;
|
|
960
|
+
inverted[i] = acc;
|
|
961
|
+
return Fp2.mul(acc, num);
|
|
962
|
+
}, Fp2.ONE);
|
|
963
|
+
const invertedAcc = Fp2.inv(multipliedAcc);
|
|
964
|
+
nums.reduceRight((acc, num, i) => {
|
|
965
|
+
if (Fp2.is0(num))
|
|
966
|
+
return acc;
|
|
967
|
+
inverted[i] = Fp2.mul(acc, inverted[i]);
|
|
968
|
+
return Fp2.mul(acc, num);
|
|
969
|
+
}, invertedAcc);
|
|
970
|
+
return inverted;
|
|
971
|
+
}
|
|
972
|
+
function FpLegendre(Fp2, n) {
|
|
973
|
+
const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
|
|
974
|
+
const powered = Fp2.pow(n, p1mod2);
|
|
975
|
+
const yes = Fp2.eql(powered, Fp2.ONE);
|
|
976
|
+
const zero = Fp2.eql(powered, Fp2.ZERO);
|
|
977
|
+
const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
|
|
978
|
+
if (!yes && !zero && !no)
|
|
979
|
+
throw new Error("invalid Legendre symbol result");
|
|
980
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
981
|
+
}
|
|
982
|
+
function nLength(n, nBitLength) {
|
|
983
|
+
if (nBitLength !== void 0)
|
|
984
|
+
anumber(nBitLength);
|
|
985
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
986
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
987
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
988
|
+
}
|
|
989
|
+
var _Field = class {
|
|
990
|
+
ORDER;
|
|
991
|
+
BITS;
|
|
992
|
+
BYTES;
|
|
993
|
+
isLE;
|
|
994
|
+
ZERO = _0n2;
|
|
995
|
+
ONE = _1n2;
|
|
996
|
+
_lengths;
|
|
997
|
+
_sqrt;
|
|
998
|
+
// cached sqrt
|
|
999
|
+
_mod;
|
|
1000
|
+
constructor(ORDER, opts = {}) {
|
|
1001
|
+
if (ORDER <= _0n2)
|
|
1002
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
1003
|
+
let _nbitLength = void 0;
|
|
1004
|
+
this.isLE = false;
|
|
1005
|
+
if (opts != null && typeof opts === "object") {
|
|
1006
|
+
if (typeof opts.BITS === "number")
|
|
1007
|
+
_nbitLength = opts.BITS;
|
|
1008
|
+
if (typeof opts.sqrt === "function")
|
|
1009
|
+
this.sqrt = opts.sqrt;
|
|
1010
|
+
if (typeof opts.isLE === "boolean")
|
|
1011
|
+
this.isLE = opts.isLE;
|
|
1012
|
+
if (opts.allowedLengths)
|
|
1013
|
+
this._lengths = opts.allowedLengths?.slice();
|
|
1014
|
+
if (typeof opts.modFromBytes === "boolean")
|
|
1015
|
+
this._mod = opts.modFromBytes;
|
|
1016
|
+
}
|
|
1017
|
+
const { nBitLength, nByteLength } = nLength(ORDER, _nbitLength);
|
|
1018
|
+
if (nByteLength > 2048)
|
|
1019
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
1020
|
+
this.ORDER = ORDER;
|
|
1021
|
+
this.BITS = nBitLength;
|
|
1022
|
+
this.BYTES = nByteLength;
|
|
1023
|
+
this._sqrt = void 0;
|
|
1024
|
+
Object.preventExtensions(this);
|
|
1025
|
+
}
|
|
1026
|
+
create(num) {
|
|
1027
|
+
return mod(num, this.ORDER);
|
|
1028
|
+
}
|
|
1029
|
+
isValid(num) {
|
|
1030
|
+
if (typeof num !== "bigint")
|
|
1031
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
1032
|
+
return _0n2 <= num && num < this.ORDER;
|
|
1033
|
+
}
|
|
1034
|
+
is0(num) {
|
|
1035
|
+
return num === _0n2;
|
|
1036
|
+
}
|
|
1037
|
+
// is valid and invertible
|
|
1038
|
+
isValidNot0(num) {
|
|
1039
|
+
return !this.is0(num) && this.isValid(num);
|
|
1040
|
+
}
|
|
1041
|
+
isOdd(num) {
|
|
1042
|
+
return (num & _1n2) === _1n2;
|
|
1043
|
+
}
|
|
1044
|
+
neg(num) {
|
|
1045
|
+
return mod(-num, this.ORDER);
|
|
1046
|
+
}
|
|
1047
|
+
eql(lhs, rhs) {
|
|
1048
|
+
return lhs === rhs;
|
|
1049
|
+
}
|
|
1050
|
+
sqr(num) {
|
|
1051
|
+
return mod(num * num, this.ORDER);
|
|
1052
|
+
}
|
|
1053
|
+
add(lhs, rhs) {
|
|
1054
|
+
return mod(lhs + rhs, this.ORDER);
|
|
1055
|
+
}
|
|
1056
|
+
sub(lhs, rhs) {
|
|
1057
|
+
return mod(lhs - rhs, this.ORDER);
|
|
1058
|
+
}
|
|
1059
|
+
mul(lhs, rhs) {
|
|
1060
|
+
return mod(lhs * rhs, this.ORDER);
|
|
1061
|
+
}
|
|
1062
|
+
pow(num, power) {
|
|
1063
|
+
return FpPow(this, num, power);
|
|
1064
|
+
}
|
|
1065
|
+
div(lhs, rhs) {
|
|
1066
|
+
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
1067
|
+
}
|
|
1068
|
+
// Same as above, but doesn't normalize
|
|
1069
|
+
sqrN(num) {
|
|
1070
|
+
return num * num;
|
|
1071
|
+
}
|
|
1072
|
+
addN(lhs, rhs) {
|
|
1073
|
+
return lhs + rhs;
|
|
1074
|
+
}
|
|
1075
|
+
subN(lhs, rhs) {
|
|
1076
|
+
return lhs - rhs;
|
|
1077
|
+
}
|
|
1078
|
+
mulN(lhs, rhs) {
|
|
1079
|
+
return lhs * rhs;
|
|
1080
|
+
}
|
|
1081
|
+
inv(num) {
|
|
1082
|
+
return invert(num, this.ORDER);
|
|
1083
|
+
}
|
|
1084
|
+
sqrt(num) {
|
|
1085
|
+
if (!this._sqrt)
|
|
1086
|
+
this._sqrt = FpSqrt(this.ORDER);
|
|
1087
|
+
return this._sqrt(this, num);
|
|
1088
|
+
}
|
|
1089
|
+
toBytes(num) {
|
|
1090
|
+
return this.isLE ? numberToBytesLE(num, this.BYTES) : numberToBytesBE(num, this.BYTES);
|
|
1091
|
+
}
|
|
1092
|
+
fromBytes(bytes, skipValidation = false) {
|
|
1093
|
+
abytes(bytes);
|
|
1094
|
+
const { _lengths: allowedLengths, BYTES, isLE, ORDER, _mod: modFromBytes } = this;
|
|
1095
|
+
if (allowedLengths) {
|
|
1096
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
1097
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
1098
|
+
}
|
|
1099
|
+
const padded = new Uint8Array(BYTES);
|
|
1100
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
1101
|
+
bytes = padded;
|
|
1102
|
+
}
|
|
1103
|
+
if (bytes.length !== BYTES)
|
|
1104
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
1105
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
1106
|
+
if (modFromBytes)
|
|
1107
|
+
scalar = mod(scalar, ORDER);
|
|
1108
|
+
if (!skipValidation) {
|
|
1109
|
+
if (!this.isValid(scalar))
|
|
1110
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
1111
|
+
}
|
|
1112
|
+
return scalar;
|
|
1113
|
+
}
|
|
1114
|
+
// TODO: we don't need it here, move out to separate fn
|
|
1115
|
+
invertBatch(lst) {
|
|
1116
|
+
return FpInvertBatch(this, lst);
|
|
1117
|
+
}
|
|
1118
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
1119
|
+
// and it's unclear what to return in there.
|
|
1120
|
+
cmov(a, b, condition) {
|
|
1121
|
+
return condition ? b : a;
|
|
1122
|
+
}
|
|
1123
|
+
};
|
|
1124
|
+
function Field(ORDER, opts = {}) {
|
|
1125
|
+
return new _Field(ORDER, opts);
|
|
1126
|
+
}
|
|
1127
|
+
function FpSqrtEven(Fp2, elm) {
|
|
1128
|
+
if (!Fp2.isOdd)
|
|
1129
|
+
throw new Error("Field doesn't have isOdd");
|
|
1130
|
+
const root = Fp2.sqrt(elm);
|
|
1131
|
+
return Fp2.isOdd(root) ? Fp2.neg(root) : root;
|
|
1132
|
+
}
|
|
1133
|
+
function getFieldBytesLength(fieldOrder) {
|
|
1134
|
+
if (typeof fieldOrder !== "bigint")
|
|
1135
|
+
throw new Error("field order must be bigint");
|
|
1136
|
+
const bitLength = fieldOrder.toString(2).length;
|
|
1137
|
+
return Math.ceil(bitLength / 8);
|
|
1138
|
+
}
|
|
1139
|
+
function getMinHashLength(fieldOrder) {
|
|
1140
|
+
const length = getFieldBytesLength(fieldOrder);
|
|
1141
|
+
return length + Math.ceil(length / 2);
|
|
1142
|
+
}
|
|
1143
|
+
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
1144
|
+
abytes(key);
|
|
1145
|
+
const len = key.length;
|
|
1146
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
1147
|
+
const minLen = getMinHashLength(fieldOrder);
|
|
1148
|
+
if (len < 16 || len < minLen || len > 1024)
|
|
1149
|
+
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
1150
|
+
const num = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
|
1151
|
+
const reduced = mod(num, fieldOrder - _1n2) + _1n2;
|
|
1152
|
+
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/abstract/curve.js
|
|
1156
|
+
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
1157
|
+
var _1n3 = /* @__PURE__ */ BigInt(1);
|
|
1158
|
+
function negateCt(condition, item) {
|
|
1159
|
+
const neg = item.negate();
|
|
1160
|
+
return condition ? neg : item;
|
|
1161
|
+
}
|
|
1162
|
+
function normalizeZ(c, points) {
|
|
1163
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
1164
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
1165
|
+
}
|
|
1166
|
+
function validateW(W, bits) {
|
|
1167
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
1168
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
1169
|
+
}
|
|
1170
|
+
function calcWOpts(W, scalarBits) {
|
|
1171
|
+
validateW(W, scalarBits);
|
|
1172
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
1173
|
+
const windowSize = 2 ** (W - 1);
|
|
1174
|
+
const maxNumber = 2 ** W;
|
|
1175
|
+
const mask = bitMask(W);
|
|
1176
|
+
const shiftBy = BigInt(W);
|
|
1177
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
1178
|
+
}
|
|
1179
|
+
function calcOffsets(n, window, wOpts) {
|
|
1180
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
1181
|
+
let wbits = Number(n & mask);
|
|
1182
|
+
let nextN = n >> shiftBy;
|
|
1183
|
+
if (wbits > windowSize) {
|
|
1184
|
+
wbits -= maxNumber;
|
|
1185
|
+
nextN += _1n3;
|
|
1186
|
+
}
|
|
1187
|
+
const offsetStart = window * windowSize;
|
|
1188
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
1189
|
+
const isZero = wbits === 0;
|
|
1190
|
+
const isNeg = wbits < 0;
|
|
1191
|
+
const isNegF = window % 2 !== 0;
|
|
1192
|
+
const offsetF = offsetStart;
|
|
1193
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
1194
|
+
}
|
|
1195
|
+
function validateMSMPoints(points, c) {
|
|
1196
|
+
if (!Array.isArray(points))
|
|
1197
|
+
throw new Error("array expected");
|
|
1198
|
+
points.forEach((p, i) => {
|
|
1199
|
+
if (!(p instanceof c))
|
|
1200
|
+
throw new Error("invalid point at index " + i);
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
1203
|
+
function validateMSMScalars(scalars, field) {
|
|
1204
|
+
if (!Array.isArray(scalars))
|
|
1205
|
+
throw new Error("array of scalars expected");
|
|
1206
|
+
scalars.forEach((s, i) => {
|
|
1207
|
+
if (!field.isValid(s))
|
|
1208
|
+
throw new Error("invalid scalar at index " + i);
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
1212
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
1213
|
+
function getW(P) {
|
|
1214
|
+
return pointWindowSizes.get(P) || 1;
|
|
1215
|
+
}
|
|
1216
|
+
function assert0(n) {
|
|
1217
|
+
if (n !== _0n3)
|
|
1218
|
+
throw new Error("invalid wNAF");
|
|
1219
|
+
}
|
|
1220
|
+
var wNAF = class {
|
|
1221
|
+
BASE;
|
|
1222
|
+
ZERO;
|
|
1223
|
+
Fn;
|
|
1224
|
+
bits;
|
|
1225
|
+
// Parametrized with a given Point class (not individual point)
|
|
1226
|
+
constructor(Point, bits) {
|
|
1227
|
+
this.BASE = Point.BASE;
|
|
1228
|
+
this.ZERO = Point.ZERO;
|
|
1229
|
+
this.Fn = Point.Fn;
|
|
1230
|
+
this.bits = bits;
|
|
1231
|
+
}
|
|
1232
|
+
// non-const time multiplication ladder
|
|
1233
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
1234
|
+
let d = elm;
|
|
1235
|
+
while (n > _0n3) {
|
|
1236
|
+
if (n & _1n3)
|
|
1237
|
+
p = p.add(d);
|
|
1238
|
+
d = d.double();
|
|
1239
|
+
n >>= _1n3;
|
|
1240
|
+
}
|
|
1241
|
+
return p;
|
|
1242
|
+
}
|
|
1243
|
+
/**
|
|
1244
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
1245
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
1246
|
+
* Number of precomputed points depends on the curve size:
|
|
1247
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
1248
|
+
* - 𝑊 is the window size
|
|
1249
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
1250
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
1251
|
+
* @param point Point instance
|
|
1252
|
+
* @param W window size
|
|
1253
|
+
* @returns precomputed point tables flattened to a single array
|
|
1254
|
+
*/
|
|
1255
|
+
precomputeWindow(point, W) {
|
|
1256
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
1257
|
+
const points = [];
|
|
1258
|
+
let p = point;
|
|
1259
|
+
let base = p;
|
|
1260
|
+
for (let window = 0; window < windows; window++) {
|
|
1261
|
+
base = p;
|
|
1262
|
+
points.push(base);
|
|
1263
|
+
for (let i = 1; i < windowSize; i++) {
|
|
1264
|
+
base = base.add(p);
|
|
1265
|
+
points.push(base);
|
|
1266
|
+
}
|
|
1267
|
+
p = base.double();
|
|
1268
|
+
}
|
|
1269
|
+
return points;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
1273
|
+
* More compact implementation:
|
|
1274
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
1275
|
+
* @returns real and fake (for const-time) points
|
|
1276
|
+
*/
|
|
1277
|
+
wNAF(W, precomputes, n) {
|
|
1278
|
+
if (!this.Fn.isValid(n))
|
|
1279
|
+
throw new Error("invalid scalar");
|
|
1280
|
+
let p = this.ZERO;
|
|
1281
|
+
let f = this.BASE;
|
|
1282
|
+
const wo = calcWOpts(W, this.bits);
|
|
1283
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1284
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
1285
|
+
n = nextN;
|
|
1286
|
+
if (isZero) {
|
|
1287
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
1288
|
+
} else {
|
|
1289
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
assert0(n);
|
|
1293
|
+
return { p, f };
|
|
1294
|
+
}
|
|
1295
|
+
/**
|
|
1296
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
1297
|
+
* @param acc accumulator point to add result of multiplication
|
|
1298
|
+
* @returns point
|
|
1299
|
+
*/
|
|
1300
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
1301
|
+
const wo = calcWOpts(W, this.bits);
|
|
1302
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1303
|
+
if (n === _0n3)
|
|
1304
|
+
break;
|
|
1305
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
1306
|
+
n = nextN;
|
|
1307
|
+
if (isZero) {
|
|
1308
|
+
continue;
|
|
1309
|
+
} else {
|
|
1310
|
+
const item = precomputes[offset];
|
|
1311
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
assert0(n);
|
|
1315
|
+
return acc;
|
|
1316
|
+
}
|
|
1317
|
+
getPrecomputes(W, point, transform) {
|
|
1318
|
+
let comp = pointPrecomputes.get(point);
|
|
1319
|
+
if (!comp) {
|
|
1320
|
+
comp = this.precomputeWindow(point, W);
|
|
1321
|
+
if (W !== 1) {
|
|
1322
|
+
if (typeof transform === "function")
|
|
1323
|
+
comp = transform(comp);
|
|
1324
|
+
pointPrecomputes.set(point, comp);
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
return comp;
|
|
1328
|
+
}
|
|
1329
|
+
cached(point, scalar, transform) {
|
|
1330
|
+
const W = getW(point);
|
|
1331
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
1332
|
+
}
|
|
1333
|
+
unsafe(point, scalar, transform, prev) {
|
|
1334
|
+
const W = getW(point);
|
|
1335
|
+
if (W === 1)
|
|
1336
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
1337
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
1338
|
+
}
|
|
1339
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
1340
|
+
// using windowed method. This specifies window size and
|
|
1341
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
1342
|
+
createCache(P, W) {
|
|
1343
|
+
validateW(W, this.bits);
|
|
1344
|
+
pointWindowSizes.set(P, W);
|
|
1345
|
+
pointPrecomputes.delete(P);
|
|
1346
|
+
}
|
|
1347
|
+
hasCache(elm) {
|
|
1348
|
+
return getW(elm) !== 1;
|
|
1349
|
+
}
|
|
1350
|
+
};
|
|
1351
|
+
function pippenger(c, points, scalars) {
|
|
1352
|
+
const fieldN = c.Fn;
|
|
1353
|
+
validateMSMPoints(points, c);
|
|
1354
|
+
validateMSMScalars(scalars, fieldN);
|
|
1355
|
+
const plength = points.length;
|
|
1356
|
+
const slength = scalars.length;
|
|
1357
|
+
if (plength !== slength)
|
|
1358
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
1359
|
+
const zero = c.ZERO;
|
|
1360
|
+
const wbits = bitLen(BigInt(plength));
|
|
1361
|
+
let windowSize = 1;
|
|
1362
|
+
if (wbits > 12)
|
|
1363
|
+
windowSize = wbits - 3;
|
|
1364
|
+
else if (wbits > 4)
|
|
1365
|
+
windowSize = wbits - 2;
|
|
1366
|
+
else if (wbits > 0)
|
|
1367
|
+
windowSize = 2;
|
|
1368
|
+
const MASK = bitMask(windowSize);
|
|
1369
|
+
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
1370
|
+
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
1371
|
+
let sum = zero;
|
|
1372
|
+
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
1373
|
+
buckets.fill(zero);
|
|
1374
|
+
for (let j = 0; j < slength; j++) {
|
|
1375
|
+
const scalar = scalars[j];
|
|
1376
|
+
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
|
1377
|
+
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
1378
|
+
}
|
|
1379
|
+
let resI = zero;
|
|
1380
|
+
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
1381
|
+
sumI = sumI.add(buckets[j]);
|
|
1382
|
+
resI = resI.add(sumI);
|
|
1383
|
+
}
|
|
1384
|
+
sum = sum.add(resI);
|
|
1385
|
+
if (i !== 0)
|
|
1386
|
+
for (let j = 0; j < windowSize; j++)
|
|
1387
|
+
sum = sum.double();
|
|
1388
|
+
}
|
|
1389
|
+
return sum;
|
|
1390
|
+
}
|
|
1391
|
+
function createField(order, field, isLE) {
|
|
1392
|
+
if (field) {
|
|
1393
|
+
if (field.ORDER !== order)
|
|
1394
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
1395
|
+
validateField(field);
|
|
1396
|
+
return field;
|
|
1397
|
+
} else {
|
|
1398
|
+
return Field(order, { isLE });
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
1402
|
+
if (FpFnLE === void 0)
|
|
1403
|
+
FpFnLE = type === "edwards";
|
|
1404
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
1405
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
1406
|
+
for (const p of ["p", "n", "h"]) {
|
|
1407
|
+
const val = CURVE[p];
|
|
1408
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
1409
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
1410
|
+
}
|
|
1411
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
1412
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
1413
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
1414
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
1415
|
+
for (const p of params) {
|
|
1416
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
1417
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
1418
|
+
}
|
|
1419
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
1420
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
1421
|
+
}
|
|
1422
|
+
function createKeygen(randomSecretKey, getPublicKey) {
|
|
1423
|
+
return function keygen(seed) {
|
|
1424
|
+
const secretKey = randomSecretKey(seed);
|
|
1425
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
1426
|
+
};
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/abstract/edwards.js
|
|
1430
|
+
var _0n4 = BigInt(0);
|
|
1431
|
+
var _1n4 = BigInt(1);
|
|
1432
|
+
var _2n2 = BigInt(2);
|
|
1433
|
+
var _8n2 = BigInt(8);
|
|
1434
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
1435
|
+
const x2 = Fp2.sqr(x);
|
|
1436
|
+
const y2 = Fp2.sqr(y);
|
|
1437
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
1438
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
1439
|
+
return Fp2.eql(left, right);
|
|
1440
|
+
}
|
|
1441
|
+
function edwards(params, extraOpts = {}) {
|
|
1442
|
+
const validated = createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
1443
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
1444
|
+
let CURVE = validated.CURVE;
|
|
1445
|
+
const { h: cofactor } = CURVE;
|
|
1446
|
+
validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
1447
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
1448
|
+
const modP = (n) => Fp2.create(n);
|
|
1449
|
+
const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
|
|
1450
|
+
try {
|
|
1451
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
1452
|
+
} catch (e) {
|
|
1453
|
+
return { isValid: false, value: _0n4 };
|
|
1454
|
+
}
|
|
1455
|
+
});
|
|
1456
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
1457
|
+
throw new Error("bad curve params: generator point");
|
|
1458
|
+
function acoord(title, n, banZero = false) {
|
|
1459
|
+
const min = banZero ? _1n4 : _0n4;
|
|
1460
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
1461
|
+
return n;
|
|
1462
|
+
}
|
|
1463
|
+
function aedpoint(other) {
|
|
1464
|
+
if (!(other instanceof Point))
|
|
1465
|
+
throw new Error("EdwardsPoint expected");
|
|
1466
|
+
}
|
|
1467
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
1468
|
+
const { X, Y, Z } = p;
|
|
1469
|
+
const is0 = p.is0();
|
|
1470
|
+
if (iz == null)
|
|
1471
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
1472
|
+
const x = modP(X * iz);
|
|
1473
|
+
const y = modP(Y * iz);
|
|
1474
|
+
const zz = Fp2.mul(Z, iz);
|
|
1475
|
+
if (is0)
|
|
1476
|
+
return { x: _0n4, y: _1n4 };
|
|
1477
|
+
if (zz !== _1n4)
|
|
1478
|
+
throw new Error("invZ was invalid");
|
|
1479
|
+
return { x, y };
|
|
1480
|
+
});
|
|
1481
|
+
const assertValidMemo = memoized((p) => {
|
|
1482
|
+
const { a, d } = CURVE;
|
|
1483
|
+
if (p.is0())
|
|
1484
|
+
throw new Error("bad point: ZERO");
|
|
1485
|
+
const { X, Y, Z, T } = p;
|
|
1486
|
+
const X2 = modP(X * X);
|
|
1487
|
+
const Y2 = modP(Y * Y);
|
|
1488
|
+
const Z2 = modP(Z * Z);
|
|
1489
|
+
const Z4 = modP(Z2 * Z2);
|
|
1490
|
+
const aX2 = modP(X2 * a);
|
|
1491
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
1492
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
1493
|
+
if (left !== right)
|
|
1494
|
+
throw new Error("bad point: equation left != right (1)");
|
|
1495
|
+
const XY = modP(X * Y);
|
|
1496
|
+
const ZT = modP(Z * T);
|
|
1497
|
+
if (XY !== ZT)
|
|
1498
|
+
throw new Error("bad point: equation left != right (2)");
|
|
1499
|
+
return true;
|
|
1500
|
+
});
|
|
1501
|
+
class Point {
|
|
1502
|
+
// base / generator point
|
|
1503
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
1504
|
+
// zero / infinity / identity point
|
|
1505
|
+
static ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
1506
|
+
// 0, 1, 1, 0
|
|
1507
|
+
// math field
|
|
1508
|
+
static Fp = Fp2;
|
|
1509
|
+
// scalar field
|
|
1510
|
+
static Fn = Fn2;
|
|
1511
|
+
X;
|
|
1512
|
+
Y;
|
|
1513
|
+
Z;
|
|
1514
|
+
T;
|
|
1515
|
+
constructor(X, Y, Z, T) {
|
|
1516
|
+
this.X = acoord("x", X);
|
|
1517
|
+
this.Y = acoord("y", Y);
|
|
1518
|
+
this.Z = acoord("z", Z, true);
|
|
1519
|
+
this.T = acoord("t", T);
|
|
1520
|
+
Object.freeze(this);
|
|
1521
|
+
}
|
|
1522
|
+
static CURVE() {
|
|
1523
|
+
return CURVE;
|
|
1524
|
+
}
|
|
1525
|
+
static fromAffine(p) {
|
|
1526
|
+
if (p instanceof Point)
|
|
1527
|
+
throw new Error("extended point not allowed");
|
|
1528
|
+
const { x, y } = p || {};
|
|
1529
|
+
acoord("x", x);
|
|
1530
|
+
acoord("y", y);
|
|
1531
|
+
return new Point(x, y, _1n4, modP(x * y));
|
|
1532
|
+
}
|
|
1533
|
+
// Uses algo from RFC8032 5.1.3.
|
|
1534
|
+
static fromBytes(bytes, zip215 = false) {
|
|
1535
|
+
const len = Fp2.BYTES;
|
|
1536
|
+
const { a, d } = CURVE;
|
|
1537
|
+
bytes = copyBytes(abytes(bytes, len, "point"));
|
|
1538
|
+
abool(zip215, "zip215");
|
|
1539
|
+
const normed = copyBytes(bytes);
|
|
1540
|
+
const lastByte = bytes[len - 1];
|
|
1541
|
+
normed[len - 1] = lastByte & ~128;
|
|
1542
|
+
const y = bytesToNumberLE(normed);
|
|
1543
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
1544
|
+
aInRange("point.y", y, _0n4, max);
|
|
1545
|
+
const y2 = modP(y * y);
|
|
1546
|
+
const u = modP(y2 - _1n4);
|
|
1547
|
+
const v = modP(d * y2 - a);
|
|
1548
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
1549
|
+
if (!isValid)
|
|
1550
|
+
throw new Error("bad point: invalid y coordinate");
|
|
1551
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
1552
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
1553
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
1554
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
1555
|
+
if (isLastByteOdd !== isXOdd)
|
|
1556
|
+
x = modP(-x);
|
|
1557
|
+
return Point.fromAffine({ x, y });
|
|
1558
|
+
}
|
|
1559
|
+
static fromHex(hex, zip215 = false) {
|
|
1560
|
+
return Point.fromBytes(hexToBytes(hex), zip215);
|
|
1561
|
+
}
|
|
1562
|
+
get x() {
|
|
1563
|
+
return this.toAffine().x;
|
|
1564
|
+
}
|
|
1565
|
+
get y() {
|
|
1566
|
+
return this.toAffine().y;
|
|
1567
|
+
}
|
|
1568
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
1569
|
+
wnaf.createCache(this, windowSize);
|
|
1570
|
+
if (!isLazy)
|
|
1571
|
+
this.multiply(_2n2);
|
|
1572
|
+
return this;
|
|
1573
|
+
}
|
|
1574
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
1575
|
+
assertValidity() {
|
|
1576
|
+
assertValidMemo(this);
|
|
1577
|
+
}
|
|
1578
|
+
// Compare one point to another.
|
|
1579
|
+
equals(other) {
|
|
1580
|
+
aedpoint(other);
|
|
1581
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1582
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
1583
|
+
const X1Z2 = modP(X1 * Z2);
|
|
1584
|
+
const X2Z1 = modP(X2 * Z1);
|
|
1585
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
1586
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
1587
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
1588
|
+
}
|
|
1589
|
+
is0() {
|
|
1590
|
+
return this.equals(Point.ZERO);
|
|
1591
|
+
}
|
|
1592
|
+
negate() {
|
|
1593
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
1594
|
+
}
|
|
1595
|
+
// Fast algo for doubling Extended Point.
|
|
1596
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
1597
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
1598
|
+
double() {
|
|
1599
|
+
const { a } = CURVE;
|
|
1600
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1601
|
+
const A = modP(X1 * X1);
|
|
1602
|
+
const B = modP(Y1 * Y1);
|
|
1603
|
+
const C = modP(_2n2 * modP(Z1 * Z1));
|
|
1604
|
+
const D = modP(a * A);
|
|
1605
|
+
const x1y1 = X1 + Y1;
|
|
1606
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
1607
|
+
const G = D + B;
|
|
1608
|
+
const F = G - C;
|
|
1609
|
+
const H = D - B;
|
|
1610
|
+
const X3 = modP(E * F);
|
|
1611
|
+
const Y3 = modP(G * H);
|
|
1612
|
+
const T3 = modP(E * H);
|
|
1613
|
+
const Z3 = modP(F * G);
|
|
1614
|
+
return new Point(X3, Y3, Z3, T3);
|
|
1615
|
+
}
|
|
1616
|
+
// Fast algo for adding 2 Extended Points.
|
|
1617
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
1618
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
1619
|
+
add(other) {
|
|
1620
|
+
aedpoint(other);
|
|
1621
|
+
const { a, d } = CURVE;
|
|
1622
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
1623
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
1624
|
+
const A = modP(X1 * X2);
|
|
1625
|
+
const B = modP(Y1 * Y2);
|
|
1626
|
+
const C = modP(T1 * d * T2);
|
|
1627
|
+
const D = modP(Z1 * Z2);
|
|
1628
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
1629
|
+
const F = D - C;
|
|
1630
|
+
const G = D + C;
|
|
1631
|
+
const H = modP(B - a * A);
|
|
1632
|
+
const X3 = modP(E * F);
|
|
1633
|
+
const Y3 = modP(G * H);
|
|
1634
|
+
const T3 = modP(E * H);
|
|
1635
|
+
const Z3 = modP(F * G);
|
|
1636
|
+
return new Point(X3, Y3, Z3, T3);
|
|
1637
|
+
}
|
|
1638
|
+
subtract(other) {
|
|
1639
|
+
return this.add(other.negate());
|
|
1640
|
+
}
|
|
1641
|
+
// Constant-time multiplication.
|
|
1642
|
+
multiply(scalar) {
|
|
1643
|
+
if (!Fn2.isValidNot0(scalar))
|
|
1644
|
+
throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
1645
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
|
|
1646
|
+
return normalizeZ(Point, [p, f])[0];
|
|
1647
|
+
}
|
|
1648
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1649
|
+
// It's faster, but should only be used when you don't care about
|
|
1650
|
+
// an exposed private key e.g. sig verification.
|
|
1651
|
+
// Does NOT allow scalars higher than CURVE.n.
|
|
1652
|
+
// Accepts optional accumulator to merge with multiply (important for sparse scalars)
|
|
1653
|
+
multiplyUnsafe(scalar, acc = Point.ZERO) {
|
|
1654
|
+
if (!Fn2.isValid(scalar))
|
|
1655
|
+
throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
1656
|
+
if (scalar === _0n4)
|
|
1657
|
+
return Point.ZERO;
|
|
1658
|
+
if (this.is0() || scalar === _1n4)
|
|
1659
|
+
return this;
|
|
1660
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
|
|
1661
|
+
}
|
|
1662
|
+
// Checks if point is of small order.
|
|
1663
|
+
// If you add something to small order point, you will have "dirty"
|
|
1664
|
+
// point with torsion component.
|
|
1665
|
+
// Multiplies point by cofactor and checks if the result is 0.
|
|
1666
|
+
isSmallOrder() {
|
|
1667
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
1668
|
+
}
|
|
1669
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
1670
|
+
// Returns `false` is the point is dirty.
|
|
1671
|
+
isTorsionFree() {
|
|
1672
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
1673
|
+
}
|
|
1674
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
1675
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
1676
|
+
toAffine(invertedZ) {
|
|
1677
|
+
return toAffineMemo(this, invertedZ);
|
|
1678
|
+
}
|
|
1679
|
+
clearCofactor() {
|
|
1680
|
+
if (cofactor === _1n4)
|
|
1681
|
+
return this;
|
|
1682
|
+
return this.multiplyUnsafe(cofactor);
|
|
1683
|
+
}
|
|
1684
|
+
toBytes() {
|
|
1685
|
+
const { x, y } = this.toAffine();
|
|
1686
|
+
const bytes = Fp2.toBytes(y);
|
|
1687
|
+
bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
|
|
1688
|
+
return bytes;
|
|
1689
|
+
}
|
|
1690
|
+
toHex() {
|
|
1691
|
+
return bytesToHex(this.toBytes());
|
|
1692
|
+
}
|
|
1693
|
+
toString() {
|
|
1694
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
1698
|
+
Point.BASE.precompute(8);
|
|
1699
|
+
return Point;
|
|
1700
|
+
}
|
|
1701
|
+
var PrimeEdwardsPoint = class {
|
|
1702
|
+
static BASE;
|
|
1703
|
+
static ZERO;
|
|
1704
|
+
static Fp;
|
|
1705
|
+
static Fn;
|
|
1706
|
+
ep;
|
|
1707
|
+
constructor(ep) {
|
|
1708
|
+
this.ep = ep;
|
|
1709
|
+
}
|
|
1710
|
+
// Static methods that must be implemented by subclasses
|
|
1711
|
+
static fromBytes(_bytes) {
|
|
1712
|
+
notImplemented();
|
|
1713
|
+
}
|
|
1714
|
+
static fromHex(_hex) {
|
|
1715
|
+
notImplemented();
|
|
1716
|
+
}
|
|
1717
|
+
get x() {
|
|
1718
|
+
return this.toAffine().x;
|
|
1719
|
+
}
|
|
1720
|
+
get y() {
|
|
1721
|
+
return this.toAffine().y;
|
|
1722
|
+
}
|
|
1723
|
+
// Common implementations
|
|
1724
|
+
clearCofactor() {
|
|
1725
|
+
return this;
|
|
1726
|
+
}
|
|
1727
|
+
assertValidity() {
|
|
1728
|
+
this.ep.assertValidity();
|
|
1729
|
+
}
|
|
1730
|
+
toAffine(invertedZ) {
|
|
1731
|
+
return this.ep.toAffine(invertedZ);
|
|
1732
|
+
}
|
|
1733
|
+
toHex() {
|
|
1734
|
+
return bytesToHex(this.toBytes());
|
|
1735
|
+
}
|
|
1736
|
+
toString() {
|
|
1737
|
+
return this.toHex();
|
|
1738
|
+
}
|
|
1739
|
+
isTorsionFree() {
|
|
1740
|
+
return true;
|
|
1741
|
+
}
|
|
1742
|
+
isSmallOrder() {
|
|
1743
|
+
return false;
|
|
1744
|
+
}
|
|
1745
|
+
add(other) {
|
|
1746
|
+
this.assertSame(other);
|
|
1747
|
+
return this.init(this.ep.add(other.ep));
|
|
1748
|
+
}
|
|
1749
|
+
subtract(other) {
|
|
1750
|
+
this.assertSame(other);
|
|
1751
|
+
return this.init(this.ep.subtract(other.ep));
|
|
1752
|
+
}
|
|
1753
|
+
multiply(scalar) {
|
|
1754
|
+
return this.init(this.ep.multiply(scalar));
|
|
1755
|
+
}
|
|
1756
|
+
multiplyUnsafe(scalar) {
|
|
1757
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
1758
|
+
}
|
|
1759
|
+
double() {
|
|
1760
|
+
return this.init(this.ep.double());
|
|
1761
|
+
}
|
|
1762
|
+
negate() {
|
|
1763
|
+
return this.init(this.ep.negate());
|
|
1764
|
+
}
|
|
1765
|
+
precompute(windowSize, isLazy) {
|
|
1766
|
+
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
1767
|
+
}
|
|
1768
|
+
};
|
|
1769
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
1770
|
+
if (typeof cHash !== "function")
|
|
1771
|
+
throw new Error('"hash" function param is required');
|
|
1772
|
+
validateObject(eddsaOpts, {}, {
|
|
1773
|
+
adjustScalarBytes: "function",
|
|
1774
|
+
randomBytes: "function",
|
|
1775
|
+
domain: "function",
|
|
1776
|
+
prehash: "function",
|
|
1777
|
+
mapToCurve: "function"
|
|
1778
|
+
});
|
|
1779
|
+
const { prehash } = eddsaOpts;
|
|
1780
|
+
const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
|
|
1781
|
+
const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
|
|
1782
|
+
const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
|
|
1783
|
+
const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
|
|
1784
|
+
abool(phflag, "phflag");
|
|
1785
|
+
if (ctx.length || phflag)
|
|
1786
|
+
throw new Error("Contexts/pre-hash are not supported");
|
|
1787
|
+
return data;
|
|
1788
|
+
});
|
|
1789
|
+
function modN_LE(hash) {
|
|
1790
|
+
return Fn2.create(bytesToNumberLE(hash));
|
|
1791
|
+
}
|
|
1792
|
+
function getPrivateScalar(key) {
|
|
1793
|
+
const len = lengths.secretKey;
|
|
1794
|
+
abytes(key, lengths.secretKey, "secretKey");
|
|
1795
|
+
const hashed = abytes(cHash(key), 2 * len, "hashedSecretKey");
|
|
1796
|
+
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
1797
|
+
const prefix = hashed.slice(len, 2 * len);
|
|
1798
|
+
const scalar = modN_LE(head);
|
|
1799
|
+
return { head, prefix, scalar };
|
|
1800
|
+
}
|
|
1801
|
+
function getExtendedPublicKey(secretKey) {
|
|
1802
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
1803
|
+
const point = BASE.multiply(scalar);
|
|
1804
|
+
const pointBytes = point.toBytes();
|
|
1805
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
1806
|
+
}
|
|
1807
|
+
function getPublicKey(secretKey) {
|
|
1808
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
1809
|
+
}
|
|
1810
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
1811
|
+
const msg = concatBytes(...msgs);
|
|
1812
|
+
return modN_LE(cHash(domain(msg, abytes(context, void 0, "context"), !!prehash)));
|
|
1813
|
+
}
|
|
1814
|
+
function sign(msg, secretKey, options = {}) {
|
|
1815
|
+
msg = abytes(msg, void 0, "message");
|
|
1816
|
+
if (prehash)
|
|
1817
|
+
msg = prehash(msg);
|
|
1818
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
1819
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
1820
|
+
const R = BASE.multiply(r).toBytes();
|
|
1821
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
1822
|
+
const s = Fn2.create(r + k * scalar);
|
|
1823
|
+
if (!Fn2.isValid(s))
|
|
1824
|
+
throw new Error("sign failed: invalid s");
|
|
1825
|
+
const rs = concatBytes(R, Fn2.toBytes(s));
|
|
1826
|
+
return abytes(rs, lengths.signature, "result");
|
|
1827
|
+
}
|
|
1828
|
+
const verifyOpts = { zip215: true };
|
|
1829
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
1830
|
+
const { context, zip215 } = options;
|
|
1831
|
+
const len = lengths.signature;
|
|
1832
|
+
sig = abytes(sig, len, "signature");
|
|
1833
|
+
msg = abytes(msg, void 0, "message");
|
|
1834
|
+
publicKey = abytes(publicKey, lengths.publicKey, "publicKey");
|
|
1835
|
+
if (zip215 !== void 0)
|
|
1836
|
+
abool(zip215, "zip215");
|
|
1837
|
+
if (prehash)
|
|
1838
|
+
msg = prehash(msg);
|
|
1839
|
+
const mid = len / 2;
|
|
1840
|
+
const r = sig.subarray(0, mid);
|
|
1841
|
+
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
1842
|
+
let A, R, SB;
|
|
1843
|
+
try {
|
|
1844
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
1845
|
+
R = Point.fromBytes(r, zip215);
|
|
1846
|
+
SB = BASE.multiplyUnsafe(s);
|
|
1847
|
+
} catch (error) {
|
|
1848
|
+
return false;
|
|
1849
|
+
}
|
|
1850
|
+
if (!zip215 && A.isSmallOrder())
|
|
1851
|
+
return false;
|
|
1852
|
+
const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
|
|
1853
|
+
const RkA = R.add(A.multiplyUnsafe(k));
|
|
1854
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
1855
|
+
}
|
|
1856
|
+
const _size = Fp2.BYTES;
|
|
1857
|
+
const lengths = {
|
|
1858
|
+
secretKey: _size,
|
|
1859
|
+
publicKey: _size,
|
|
1860
|
+
signature: 2 * _size,
|
|
1861
|
+
seed: _size
|
|
1862
|
+
};
|
|
1863
|
+
function randomSecretKey(seed = randomBytes2(lengths.seed)) {
|
|
1864
|
+
return abytes(seed, lengths.seed, "seed");
|
|
1865
|
+
}
|
|
1866
|
+
function isValidSecretKey(key) {
|
|
1867
|
+
return isBytes(key) && key.length === Fn2.BYTES;
|
|
1868
|
+
}
|
|
1869
|
+
function isValidPublicKey(key, zip215) {
|
|
1870
|
+
try {
|
|
1871
|
+
return !!Point.fromBytes(key, zip215);
|
|
1872
|
+
} catch (error) {
|
|
1873
|
+
return false;
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
const utils = {
|
|
1877
|
+
getExtendedPublicKey,
|
|
1878
|
+
randomSecretKey,
|
|
1879
|
+
isValidSecretKey,
|
|
1880
|
+
isValidPublicKey,
|
|
1881
|
+
/**
|
|
1882
|
+
* Converts ed public key to x public key. Uses formula:
|
|
1883
|
+
* - ed25519:
|
|
1884
|
+
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
1885
|
+
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
1886
|
+
* - ed448:
|
|
1887
|
+
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
1888
|
+
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
1889
|
+
*/
|
|
1890
|
+
toMontgomery(publicKey) {
|
|
1891
|
+
const { y } = Point.fromBytes(publicKey);
|
|
1892
|
+
const size = lengths.publicKey;
|
|
1893
|
+
const is25519 = size === 32;
|
|
1894
|
+
if (!is25519 && size !== 57)
|
|
1895
|
+
throw new Error("only defined for 25519 and 448");
|
|
1896
|
+
const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
|
|
1897
|
+
return Fp2.toBytes(u);
|
|
1898
|
+
},
|
|
1899
|
+
toMontgomerySecret(secretKey) {
|
|
1900
|
+
const size = lengths.secretKey;
|
|
1901
|
+
abytes(secretKey, size);
|
|
1902
|
+
const hashed = cHash(secretKey.subarray(0, size));
|
|
1903
|
+
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
1904
|
+
}
|
|
1905
|
+
};
|
|
1906
|
+
return Object.freeze({
|
|
1907
|
+
keygen: createKeygen(randomSecretKey, getPublicKey),
|
|
1908
|
+
getPublicKey,
|
|
1909
|
+
sign,
|
|
1910
|
+
verify,
|
|
1911
|
+
utils,
|
|
1912
|
+
Point,
|
|
1913
|
+
lengths
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/abstract/hash-to-curve.js
|
|
1918
|
+
var os2ip = bytesToNumberBE;
|
|
1919
|
+
function i2osp(value, length) {
|
|
1920
|
+
asafenumber(value);
|
|
1921
|
+
asafenumber(length);
|
|
1922
|
+
if (value < 0 || value >= 1 << 8 * length)
|
|
1923
|
+
throw new Error("invalid I2OSP input: " + value);
|
|
1924
|
+
const res = Array.from({ length }).fill(0);
|
|
1925
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
1926
|
+
res[i] = value & 255;
|
|
1927
|
+
value >>>= 8;
|
|
1928
|
+
}
|
|
1929
|
+
return new Uint8Array(res);
|
|
1930
|
+
}
|
|
1931
|
+
function strxor(a, b) {
|
|
1932
|
+
const arr = new Uint8Array(a.length);
|
|
1933
|
+
for (let i = 0; i < a.length; i++) {
|
|
1934
|
+
arr[i] = a[i] ^ b[i];
|
|
1935
|
+
}
|
|
1936
|
+
return arr;
|
|
1937
|
+
}
|
|
1938
|
+
function normDST(DST) {
|
|
1939
|
+
if (!isBytes(DST) && typeof DST !== "string")
|
|
1940
|
+
throw new Error("DST must be Uint8Array or ascii string");
|
|
1941
|
+
return typeof DST === "string" ? asciiToBytes(DST) : DST;
|
|
1942
|
+
}
|
|
1943
|
+
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
1944
|
+
abytes(msg);
|
|
1945
|
+
asafenumber(lenInBytes);
|
|
1946
|
+
DST = normDST(DST);
|
|
1947
|
+
if (DST.length > 255)
|
|
1948
|
+
DST = H(concatBytes(asciiToBytes("H2C-OVERSIZE-DST-"), DST));
|
|
1949
|
+
const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
|
|
1950
|
+
const ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
1951
|
+
if (lenInBytes > 65535 || ell > 255)
|
|
1952
|
+
throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
1953
|
+
const DST_prime = concatBytes(DST, i2osp(DST.length, 1));
|
|
1954
|
+
const Z_pad = i2osp(0, r_in_bytes);
|
|
1955
|
+
const l_i_b_str = i2osp(lenInBytes, 2);
|
|
1956
|
+
const b = new Array(ell);
|
|
1957
|
+
const b_0 = H(concatBytes(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
|
|
1958
|
+
b[0] = H(concatBytes(b_0, i2osp(1, 1), DST_prime));
|
|
1959
|
+
for (let i = 1; i <= ell; i++) {
|
|
1960
|
+
const args = [strxor(b_0, b[i - 1]), i2osp(i + 1, 1), DST_prime];
|
|
1961
|
+
b[i] = H(concatBytes(...args));
|
|
1962
|
+
}
|
|
1963
|
+
const pseudo_random_bytes = concatBytes(...b);
|
|
1964
|
+
return pseudo_random_bytes.slice(0, lenInBytes);
|
|
1965
|
+
}
|
|
1966
|
+
function expand_message_xof(msg, DST, lenInBytes, k, H) {
|
|
1967
|
+
abytes(msg);
|
|
1968
|
+
asafenumber(lenInBytes);
|
|
1969
|
+
DST = normDST(DST);
|
|
1970
|
+
if (DST.length > 255) {
|
|
1971
|
+
const dkLen = Math.ceil(2 * k / 8);
|
|
1972
|
+
DST = H.create({ dkLen }).update(asciiToBytes("H2C-OVERSIZE-DST-")).update(DST).digest();
|
|
1973
|
+
}
|
|
1974
|
+
if (lenInBytes > 65535 || DST.length > 255)
|
|
1975
|
+
throw new Error("expand_message_xof: invalid lenInBytes");
|
|
1976
|
+
return H.create({ dkLen: lenInBytes }).update(msg).update(i2osp(lenInBytes, 2)).update(DST).update(i2osp(DST.length, 1)).digest();
|
|
1977
|
+
}
|
|
1978
|
+
function hash_to_field(msg, count, options) {
|
|
1979
|
+
validateObject(options, {
|
|
1980
|
+
p: "bigint",
|
|
1981
|
+
m: "number",
|
|
1982
|
+
k: "number",
|
|
1983
|
+
hash: "function"
|
|
1984
|
+
});
|
|
1985
|
+
const { p, k, m, hash, expand, DST } = options;
|
|
1986
|
+
asafenumber(hash.outputLen, "valid hash");
|
|
1987
|
+
abytes(msg);
|
|
1988
|
+
asafenumber(count);
|
|
1989
|
+
const log2p = p.toString(2).length;
|
|
1990
|
+
const L = Math.ceil((log2p + k) / 8);
|
|
1991
|
+
const len_in_bytes = count * m * L;
|
|
1992
|
+
let prb;
|
|
1993
|
+
if (expand === "xmd") {
|
|
1994
|
+
prb = expand_message_xmd(msg, DST, len_in_bytes, hash);
|
|
1995
|
+
} else if (expand === "xof") {
|
|
1996
|
+
prb = expand_message_xof(msg, DST, len_in_bytes, k, hash);
|
|
1997
|
+
} else if (expand === "_internal_pass") {
|
|
1998
|
+
prb = msg;
|
|
1999
|
+
} else {
|
|
2000
|
+
throw new Error('expand must be "xmd" or "xof"');
|
|
2001
|
+
}
|
|
2002
|
+
const u = new Array(count);
|
|
2003
|
+
for (let i = 0; i < count; i++) {
|
|
2004
|
+
const e = new Array(m);
|
|
2005
|
+
for (let j = 0; j < m; j++) {
|
|
2006
|
+
const elm_offset = L * (j + i * m);
|
|
2007
|
+
const tv = prb.subarray(elm_offset, elm_offset + L);
|
|
2008
|
+
e[j] = mod(os2ip(tv), p);
|
|
2009
|
+
}
|
|
2010
|
+
u[i] = e;
|
|
2011
|
+
}
|
|
2012
|
+
return u;
|
|
2013
|
+
}
|
|
2014
|
+
var _DST_scalar = asciiToBytes("HashToScalar-");
|
|
2015
|
+
function createHasher2(Point, mapToCurve, defaults) {
|
|
2016
|
+
if (typeof mapToCurve !== "function")
|
|
2017
|
+
throw new Error("mapToCurve() must be defined");
|
|
2018
|
+
function map(num) {
|
|
2019
|
+
return Point.fromAffine(mapToCurve(num));
|
|
2020
|
+
}
|
|
2021
|
+
function clear(initial) {
|
|
2022
|
+
const P = initial.clearCofactor();
|
|
2023
|
+
if (P.equals(Point.ZERO))
|
|
2024
|
+
return Point.ZERO;
|
|
2025
|
+
P.assertValidity();
|
|
2026
|
+
return P;
|
|
2027
|
+
}
|
|
2028
|
+
return {
|
|
2029
|
+
defaults: Object.freeze(defaults),
|
|
2030
|
+
Point,
|
|
2031
|
+
hashToCurve(msg, options) {
|
|
2032
|
+
const opts = Object.assign({}, defaults, options);
|
|
2033
|
+
const u = hash_to_field(msg, 2, opts);
|
|
2034
|
+
const u0 = map(u[0]);
|
|
2035
|
+
const u1 = map(u[1]);
|
|
2036
|
+
return clear(u0.add(u1));
|
|
2037
|
+
},
|
|
2038
|
+
encodeToCurve(msg, options) {
|
|
2039
|
+
const optsDst = defaults.encodeDST ? { DST: defaults.encodeDST } : {};
|
|
2040
|
+
const opts = Object.assign({}, defaults, optsDst, options);
|
|
2041
|
+
const u = hash_to_field(msg, 1, opts);
|
|
2042
|
+
const u0 = map(u[0]);
|
|
2043
|
+
return clear(u0);
|
|
2044
|
+
},
|
|
2045
|
+
/** See {@link H2CHasher} */
|
|
2046
|
+
mapToCurve(scalars) {
|
|
2047
|
+
if (defaults.m === 1) {
|
|
2048
|
+
if (typeof scalars !== "bigint")
|
|
2049
|
+
throw new Error("expected bigint (m=1)");
|
|
2050
|
+
return clear(map([scalars]));
|
|
2051
|
+
}
|
|
2052
|
+
if (!Array.isArray(scalars))
|
|
2053
|
+
throw new Error("expected array of bigints");
|
|
2054
|
+
for (const i of scalars)
|
|
2055
|
+
if (typeof i !== "bigint")
|
|
2056
|
+
throw new Error("expected array of bigints");
|
|
2057
|
+
return clear(map(scalars));
|
|
2058
|
+
},
|
|
2059
|
+
// hash_to_scalar can produce 0: https://www.rfc-editor.org/errata/eid8393
|
|
2060
|
+
// RFC 9380, draft-irtf-cfrg-bbs-signatures-08
|
|
2061
|
+
hashToScalar(msg, options) {
|
|
2062
|
+
const N = Point.Fn.ORDER;
|
|
2063
|
+
const opts = Object.assign({}, defaults, { p: N, m: 1, DST: _DST_scalar }, options);
|
|
2064
|
+
return hash_to_field(msg, 1, opts)[0][0];
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
2067
|
+
}
|
|
2068
|
+
|
|
2069
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/abstract/montgomery.js
|
|
2070
|
+
var _0n5 = BigInt(0);
|
|
2071
|
+
var _1n5 = BigInt(1);
|
|
2072
|
+
var _2n3 = BigInt(2);
|
|
2073
|
+
function validateOpts(curve) {
|
|
2074
|
+
validateObject(curve, {
|
|
2075
|
+
adjustScalarBytes: "function",
|
|
2076
|
+
powPminus2: "function"
|
|
2077
|
+
});
|
|
2078
|
+
return Object.freeze({ ...curve });
|
|
2079
|
+
}
|
|
2080
|
+
function montgomery(curveDef) {
|
|
2081
|
+
const CURVE = validateOpts(curveDef);
|
|
2082
|
+
const { P, type, adjustScalarBytes: adjustScalarBytes2, powPminus2, randomBytes: rand } = CURVE;
|
|
2083
|
+
const is25519 = type === "x25519";
|
|
2084
|
+
if (!is25519 && type !== "x448")
|
|
2085
|
+
throw new Error("invalid type");
|
|
2086
|
+
const randomBytes_ = rand || randomBytes;
|
|
2087
|
+
const montgomeryBits = is25519 ? 255 : 448;
|
|
2088
|
+
const fieldLen = is25519 ? 32 : 56;
|
|
2089
|
+
const Gu = is25519 ? BigInt(9) : BigInt(5);
|
|
2090
|
+
const a24 = is25519 ? BigInt(121665) : BigInt(39081);
|
|
2091
|
+
const minScalar = is25519 ? _2n3 ** BigInt(254) : _2n3 ** BigInt(447);
|
|
2092
|
+
const maxAdded = is25519 ? BigInt(8) * _2n3 ** BigInt(251) - _1n5 : BigInt(4) * _2n3 ** BigInt(445) - _1n5;
|
|
2093
|
+
const maxScalar = minScalar + maxAdded + _1n5;
|
|
2094
|
+
const modP = (n) => mod(n, P);
|
|
2095
|
+
const GuBytes = encodeU(Gu);
|
|
2096
|
+
function encodeU(u) {
|
|
2097
|
+
return numberToBytesLE(modP(u), fieldLen);
|
|
2098
|
+
}
|
|
2099
|
+
function decodeU(u) {
|
|
2100
|
+
const _u = copyBytes(abytes(u, fieldLen, "uCoordinate"));
|
|
2101
|
+
if (is25519)
|
|
2102
|
+
_u[31] &= 127;
|
|
2103
|
+
return modP(bytesToNumberLE(_u));
|
|
2104
|
+
}
|
|
2105
|
+
function decodeScalar(scalar) {
|
|
2106
|
+
return bytesToNumberLE(adjustScalarBytes2(copyBytes(abytes(scalar, fieldLen, "scalar"))));
|
|
2107
|
+
}
|
|
2108
|
+
function scalarMult(scalar, u) {
|
|
2109
|
+
const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));
|
|
2110
|
+
if (pu === _0n5)
|
|
2111
|
+
throw new Error("invalid private or public key received");
|
|
2112
|
+
return encodeU(pu);
|
|
2113
|
+
}
|
|
2114
|
+
function scalarMultBase(scalar) {
|
|
2115
|
+
return scalarMult(scalar, GuBytes);
|
|
2116
|
+
}
|
|
2117
|
+
const getPublicKey = scalarMultBase;
|
|
2118
|
+
const getSharedSecret = scalarMult;
|
|
2119
|
+
function cswap(swap, x_2, x_3) {
|
|
2120
|
+
const dummy = modP(swap * (x_2 - x_3));
|
|
2121
|
+
x_2 = modP(x_2 - dummy);
|
|
2122
|
+
x_3 = modP(x_3 + dummy);
|
|
2123
|
+
return { x_2, x_3 };
|
|
2124
|
+
}
|
|
2125
|
+
function montgomeryLadder(u, scalar) {
|
|
2126
|
+
aInRange("u", u, _0n5, P);
|
|
2127
|
+
aInRange("scalar", scalar, minScalar, maxScalar);
|
|
2128
|
+
const k = scalar;
|
|
2129
|
+
const x_1 = u;
|
|
2130
|
+
let x_2 = _1n5;
|
|
2131
|
+
let z_2 = _0n5;
|
|
2132
|
+
let x_3 = u;
|
|
2133
|
+
let z_3 = _1n5;
|
|
2134
|
+
let swap = _0n5;
|
|
2135
|
+
for (let t = BigInt(montgomeryBits - 1); t >= _0n5; t--) {
|
|
2136
|
+
const k_t = k >> t & _1n5;
|
|
2137
|
+
swap ^= k_t;
|
|
2138
|
+
({ x_2, x_3 } = cswap(swap, x_2, x_3));
|
|
2139
|
+
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
2140
|
+
swap = k_t;
|
|
2141
|
+
const A = x_2 + z_2;
|
|
2142
|
+
const AA = modP(A * A);
|
|
2143
|
+
const B = x_2 - z_2;
|
|
2144
|
+
const BB = modP(B * B);
|
|
2145
|
+
const E = AA - BB;
|
|
2146
|
+
const C = x_3 + z_3;
|
|
2147
|
+
const D = x_3 - z_3;
|
|
2148
|
+
const DA = modP(D * A);
|
|
2149
|
+
const CB = modP(C * B);
|
|
2150
|
+
const dacb = DA + CB;
|
|
2151
|
+
const da_cb = DA - CB;
|
|
2152
|
+
x_3 = modP(dacb * dacb);
|
|
2153
|
+
z_3 = modP(x_1 * modP(da_cb * da_cb));
|
|
2154
|
+
x_2 = modP(AA * BB);
|
|
2155
|
+
z_2 = modP(E * (AA + modP(a24 * E)));
|
|
2156
|
+
}
|
|
2157
|
+
({ x_2, x_3 } = cswap(swap, x_2, x_3));
|
|
2158
|
+
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
2159
|
+
const z2 = powPminus2(z_2);
|
|
2160
|
+
return modP(x_2 * z2);
|
|
2161
|
+
}
|
|
2162
|
+
const lengths = {
|
|
2163
|
+
secretKey: fieldLen,
|
|
2164
|
+
publicKey: fieldLen,
|
|
2165
|
+
seed: fieldLen
|
|
2166
|
+
};
|
|
2167
|
+
const randomSecretKey = (seed = randomBytes_(fieldLen)) => {
|
|
2168
|
+
abytes(seed, lengths.seed, "seed");
|
|
2169
|
+
return seed;
|
|
2170
|
+
};
|
|
2171
|
+
const utils = { randomSecretKey };
|
|
2172
|
+
return Object.freeze({
|
|
2173
|
+
keygen: createKeygen(randomSecretKey, getPublicKey),
|
|
2174
|
+
getSharedSecret,
|
|
2175
|
+
getPublicKey,
|
|
2176
|
+
scalarMult,
|
|
2177
|
+
scalarMultBase,
|
|
2178
|
+
utils,
|
|
2179
|
+
GuBytes: GuBytes.slice(),
|
|
2180
|
+
lengths
|
|
2181
|
+
});
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/abstract/oprf.js
|
|
2185
|
+
function createORPF(opts) {
|
|
2186
|
+
validateObject(opts, {
|
|
2187
|
+
name: "string",
|
|
2188
|
+
hash: "function",
|
|
2189
|
+
hashToScalar: "function",
|
|
2190
|
+
hashToGroup: "function"
|
|
2191
|
+
});
|
|
2192
|
+
const { name, Point, hash } = opts;
|
|
2193
|
+
const { Fn: Fn2 } = Point;
|
|
2194
|
+
const hashToGroup = (msg, ctx) => opts.hashToGroup(msg, {
|
|
2195
|
+
DST: concatBytes(asciiToBytes("HashToGroup-"), ctx)
|
|
2196
|
+
});
|
|
2197
|
+
const hashToScalarPrefixed = (msg, ctx) => opts.hashToScalar(msg, { DST: concatBytes(_DST_scalar, ctx) });
|
|
2198
|
+
const randomScalar = (rng = randomBytes) => {
|
|
2199
|
+
const t = mapHashToField(rng(getMinHashLength(Fn2.ORDER)), Fn2.ORDER, Fn2.isLE);
|
|
2200
|
+
return Fn2.isLE ? bytesToNumberLE(t) : bytesToNumberBE(t);
|
|
2201
|
+
};
|
|
2202
|
+
const msm = (points, scalars) => pippenger(Point, points, scalars);
|
|
2203
|
+
const getCtx = (mode) => concatBytes(asciiToBytes("OPRFV1-"), new Uint8Array([mode]), asciiToBytes("-" + name));
|
|
2204
|
+
const ctxOPRF = getCtx(0);
|
|
2205
|
+
const ctxVOPRF = getCtx(1);
|
|
2206
|
+
const ctxPOPRF = getCtx(2);
|
|
2207
|
+
function encode(...args) {
|
|
2208
|
+
const res = [];
|
|
2209
|
+
for (const a of args) {
|
|
2210
|
+
if (typeof a === "number")
|
|
2211
|
+
res.push(numberToBytesBE(a, 2));
|
|
2212
|
+
else if (typeof a === "string")
|
|
2213
|
+
res.push(asciiToBytes(a));
|
|
2214
|
+
else {
|
|
2215
|
+
abytes(a);
|
|
2216
|
+
res.push(numberToBytesBE(a.length, 2), a);
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
return concatBytes(...res);
|
|
2220
|
+
}
|
|
2221
|
+
const hashInput = (...bytes) => hash(encode(...bytes, "Finalize"));
|
|
2222
|
+
function getTranscripts(B, C, D, ctx) {
|
|
2223
|
+
const Bm = B.toBytes();
|
|
2224
|
+
const seed = hash(encode(Bm, concatBytes(asciiToBytes("Seed-"), ctx)));
|
|
2225
|
+
const res = [];
|
|
2226
|
+
for (let i = 0; i < C.length; i++) {
|
|
2227
|
+
const Ci = C[i].toBytes();
|
|
2228
|
+
const Di = D[i].toBytes();
|
|
2229
|
+
const di = hashToScalarPrefixed(encode(seed, i, Ci, Di, "Composite"), ctx);
|
|
2230
|
+
res.push(di);
|
|
2231
|
+
}
|
|
2232
|
+
return res;
|
|
2233
|
+
}
|
|
2234
|
+
function computeComposites(B, C, D, ctx) {
|
|
2235
|
+
const T = getTranscripts(B, C, D, ctx);
|
|
2236
|
+
const M = msm(C, T);
|
|
2237
|
+
const Z = msm(D, T);
|
|
2238
|
+
return { M, Z };
|
|
2239
|
+
}
|
|
2240
|
+
function computeCompositesFast(k, B, C, D, ctx) {
|
|
2241
|
+
const T = getTranscripts(B, C, D, ctx);
|
|
2242
|
+
const M = msm(C, T);
|
|
2243
|
+
const Z = M.multiply(k);
|
|
2244
|
+
return { M, Z };
|
|
2245
|
+
}
|
|
2246
|
+
function challengeTranscript(B, M, Z, t2, t3, ctx) {
|
|
2247
|
+
const [Bm, a0, a1, a2, a3] = [B, M, Z, t2, t3].map((i) => i.toBytes());
|
|
2248
|
+
return hashToScalarPrefixed(encode(Bm, a0, a1, a2, a3, "Challenge"), ctx);
|
|
2249
|
+
}
|
|
2250
|
+
function generateProof(ctx, k, B, C, D, rng) {
|
|
2251
|
+
const { M, Z } = computeCompositesFast(k, B, C, D, ctx);
|
|
2252
|
+
const r = randomScalar(rng);
|
|
2253
|
+
const t2 = Point.BASE.multiply(r);
|
|
2254
|
+
const t3 = M.multiply(r);
|
|
2255
|
+
const c = challengeTranscript(B, M, Z, t2, t3, ctx);
|
|
2256
|
+
const s = Fn2.sub(r, Fn2.mul(c, k));
|
|
2257
|
+
return concatBytes(...[c, s].map((i) => Fn2.toBytes(i)));
|
|
2258
|
+
}
|
|
2259
|
+
function verifyProof(ctx, B, C, D, proof) {
|
|
2260
|
+
abytes(proof, 2 * Fn2.BYTES);
|
|
2261
|
+
const { M, Z } = computeComposites(B, C, D, ctx);
|
|
2262
|
+
const [c, s] = [proof.subarray(0, Fn2.BYTES), proof.subarray(Fn2.BYTES)].map((f) => Fn2.fromBytes(f));
|
|
2263
|
+
const t2 = Point.BASE.multiply(s).add(B.multiply(c));
|
|
2264
|
+
const t3 = M.multiply(s).add(Z.multiply(c));
|
|
2265
|
+
const expectedC = challengeTranscript(B, M, Z, t2, t3, ctx);
|
|
2266
|
+
if (!Fn2.eql(c, expectedC))
|
|
2267
|
+
throw new Error("proof verification failed");
|
|
2268
|
+
}
|
|
2269
|
+
function generateKeyPair() {
|
|
2270
|
+
const skS = randomScalar();
|
|
2271
|
+
const pkS = Point.BASE.multiply(skS);
|
|
2272
|
+
return { secretKey: Fn2.toBytes(skS), publicKey: pkS.toBytes() };
|
|
2273
|
+
}
|
|
2274
|
+
function deriveKeyPair(ctx, seed, info) {
|
|
2275
|
+
const dst = concatBytes(asciiToBytes("DeriveKeyPair"), ctx);
|
|
2276
|
+
const msg = concatBytes(seed, encode(info), Uint8Array.of(0));
|
|
2277
|
+
for (let counter = 0; counter <= 255; counter++) {
|
|
2278
|
+
msg[msg.length - 1] = counter;
|
|
2279
|
+
const skS = opts.hashToScalar(msg, { DST: dst });
|
|
2280
|
+
if (Fn2.is0(skS))
|
|
2281
|
+
continue;
|
|
2282
|
+
return { secretKey: Fn2.toBytes(skS), publicKey: Point.BASE.multiply(skS).toBytes() };
|
|
2283
|
+
}
|
|
2284
|
+
throw new Error("Cannot derive key");
|
|
2285
|
+
}
|
|
2286
|
+
function blind(ctx, input, rng = randomBytes) {
|
|
2287
|
+
const blind2 = randomScalar(rng);
|
|
2288
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
2289
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2290
|
+
throw new Error("Input point at infinity");
|
|
2291
|
+
const blinded = inputPoint.multiply(blind2);
|
|
2292
|
+
return { blind: Fn2.toBytes(blind2), blinded: blinded.toBytes() };
|
|
2293
|
+
}
|
|
2294
|
+
function evaluate(ctx, secretKey, input) {
|
|
2295
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2296
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
2297
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2298
|
+
throw new Error("Input point at infinity");
|
|
2299
|
+
const unblinded = inputPoint.multiply(skS).toBytes();
|
|
2300
|
+
return hashInput(input, unblinded);
|
|
2301
|
+
}
|
|
2302
|
+
const oprf = {
|
|
2303
|
+
generateKeyPair,
|
|
2304
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxOPRF, seed, keyInfo),
|
|
2305
|
+
blind: (input, rng = randomBytes) => blind(ctxOPRF, input, rng),
|
|
2306
|
+
blindEvaluate(secretKey, blindedPoint) {
|
|
2307
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2308
|
+
const elm = Point.fromBytes(blindedPoint);
|
|
2309
|
+
return elm.multiply(skS).toBytes();
|
|
2310
|
+
},
|
|
2311
|
+
finalize(input, blindBytes, evaluatedBytes) {
|
|
2312
|
+
const blind2 = Fn2.fromBytes(blindBytes);
|
|
2313
|
+
const evalPoint = Point.fromBytes(evaluatedBytes);
|
|
2314
|
+
const unblinded = evalPoint.multiply(Fn2.inv(blind2)).toBytes();
|
|
2315
|
+
return hashInput(input, unblinded);
|
|
2316
|
+
},
|
|
2317
|
+
evaluate: (secretKey, input) => evaluate(ctxOPRF, secretKey, input)
|
|
2318
|
+
};
|
|
2319
|
+
const voprf = {
|
|
2320
|
+
generateKeyPair,
|
|
2321
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxVOPRF, seed, keyInfo),
|
|
2322
|
+
blind: (input, rng = randomBytes) => blind(ctxVOPRF, input, rng),
|
|
2323
|
+
blindEvaluateBatch(secretKey, publicKey, blinded, rng = randomBytes) {
|
|
2324
|
+
if (!Array.isArray(blinded))
|
|
2325
|
+
throw new Error("expected array");
|
|
2326
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2327
|
+
const pkS = Point.fromBytes(publicKey);
|
|
2328
|
+
const blindedPoints = blinded.map(Point.fromBytes);
|
|
2329
|
+
const evaluated = blindedPoints.map((i) => i.multiply(skS));
|
|
2330
|
+
const proof = generateProof(ctxVOPRF, skS, pkS, blindedPoints, evaluated, rng);
|
|
2331
|
+
return { evaluated: evaluated.map((i) => i.toBytes()), proof };
|
|
2332
|
+
},
|
|
2333
|
+
blindEvaluate(secretKey, publicKey, blinded, rng = randomBytes) {
|
|
2334
|
+
const res = this.blindEvaluateBatch(secretKey, publicKey, [blinded], rng);
|
|
2335
|
+
return { evaluated: res.evaluated[0], proof: res.proof };
|
|
2336
|
+
},
|
|
2337
|
+
finalizeBatch(items, publicKey, proof) {
|
|
2338
|
+
if (!Array.isArray(items))
|
|
2339
|
+
throw new Error("expected array");
|
|
2340
|
+
const pkS = Point.fromBytes(publicKey);
|
|
2341
|
+
const blindedPoints = items.map((i) => i.blinded).map(Point.fromBytes);
|
|
2342
|
+
const evalPoints = items.map((i) => i.evaluated).map(Point.fromBytes);
|
|
2343
|
+
verifyProof(ctxVOPRF, pkS, blindedPoints, evalPoints, proof);
|
|
2344
|
+
return items.map((i) => oprf.finalize(i.input, i.blind, i.evaluated));
|
|
2345
|
+
},
|
|
2346
|
+
finalize(input, blind2, evaluated, blinded, publicKey, proof) {
|
|
2347
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], publicKey, proof)[0];
|
|
2348
|
+
},
|
|
2349
|
+
evaluate: (secretKey, input) => evaluate(ctxVOPRF, secretKey, input)
|
|
2350
|
+
};
|
|
2351
|
+
const poprf = (info) => {
|
|
2352
|
+
const m = hashToScalarPrefixed(encode("Info", info), ctxPOPRF);
|
|
2353
|
+
const T = Point.BASE.multiply(m);
|
|
2354
|
+
return {
|
|
2355
|
+
generateKeyPair,
|
|
2356
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxPOPRF, seed, keyInfo),
|
|
2357
|
+
blind(input, publicKey, rng = randomBytes) {
|
|
2358
|
+
const pkS = Point.fromBytes(publicKey);
|
|
2359
|
+
const tweakedKey = T.add(pkS);
|
|
2360
|
+
if (tweakedKey.equals(Point.ZERO))
|
|
2361
|
+
throw new Error("tweakedKey point at infinity");
|
|
2362
|
+
const blind2 = randomScalar(rng);
|
|
2363
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
2364
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2365
|
+
throw new Error("Input point at infinity");
|
|
2366
|
+
const blindedPoint = inputPoint.multiply(blind2);
|
|
2367
|
+
return {
|
|
2368
|
+
blind: Fn2.toBytes(blind2),
|
|
2369
|
+
blinded: blindedPoint.toBytes(),
|
|
2370
|
+
tweakedKey: tweakedKey.toBytes()
|
|
2371
|
+
};
|
|
2372
|
+
},
|
|
2373
|
+
blindEvaluateBatch(secretKey, blinded, rng = randomBytes) {
|
|
2374
|
+
if (!Array.isArray(blinded))
|
|
2375
|
+
throw new Error("expected array");
|
|
2376
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2377
|
+
const t = Fn2.add(skS, m);
|
|
2378
|
+
const invT = Fn2.inv(t);
|
|
2379
|
+
const blindedPoints = blinded.map(Point.fromBytes);
|
|
2380
|
+
const evalPoints = blindedPoints.map((i) => i.multiply(invT));
|
|
2381
|
+
const tweakedKey = Point.BASE.multiply(t);
|
|
2382
|
+
const proof = generateProof(ctxPOPRF, t, tweakedKey, evalPoints, blindedPoints, rng);
|
|
2383
|
+
return { evaluated: evalPoints.map((i) => i.toBytes()), proof };
|
|
2384
|
+
},
|
|
2385
|
+
blindEvaluate(secretKey, blinded, rng = randomBytes) {
|
|
2386
|
+
const res = this.blindEvaluateBatch(secretKey, [blinded], rng);
|
|
2387
|
+
return { evaluated: res.evaluated[0], proof: res.proof };
|
|
2388
|
+
},
|
|
2389
|
+
finalizeBatch(items, proof, tweakedKey) {
|
|
2390
|
+
if (!Array.isArray(items))
|
|
2391
|
+
throw new Error("expected array");
|
|
2392
|
+
const evalPoints = items.map((i) => i.evaluated).map(Point.fromBytes);
|
|
2393
|
+
verifyProof(ctxPOPRF, Point.fromBytes(tweakedKey), evalPoints, items.map((i) => i.blinded).map(Point.fromBytes), proof);
|
|
2394
|
+
return items.map((i, j) => {
|
|
2395
|
+
const blind2 = Fn2.fromBytes(i.blind);
|
|
2396
|
+
const point = evalPoints[j].multiply(Fn2.inv(blind2)).toBytes();
|
|
2397
|
+
return hashInput(i.input, info, point);
|
|
2398
|
+
});
|
|
2399
|
+
},
|
|
2400
|
+
finalize(input, blind2, evaluated, blinded, proof, tweakedKey) {
|
|
2401
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], proof, tweakedKey)[0];
|
|
2402
|
+
},
|
|
2403
|
+
evaluate(secretKey, input) {
|
|
2404
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2405
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
2406
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2407
|
+
throw new Error("Input point at infinity");
|
|
2408
|
+
const t = Fn2.add(skS, m);
|
|
2409
|
+
const invT = Fn2.inv(t);
|
|
2410
|
+
const unblinded = inputPoint.multiply(invT).toBytes();
|
|
2411
|
+
return hashInput(input, info, unblinded);
|
|
2412
|
+
}
|
|
2413
|
+
};
|
|
2414
|
+
};
|
|
2415
|
+
return Object.freeze({ name, oprf, voprf, poprf, __tests: { Fn: Fn2 } });
|
|
2416
|
+
}
|
|
2417
|
+
|
|
2418
|
+
// ../../node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/ed25519.js
|
|
2419
|
+
var _0n6 = /* @__PURE__ */ BigInt(0);
|
|
2420
|
+
var _1n6 = BigInt(1);
|
|
2421
|
+
var _2n4 = BigInt(2);
|
|
2422
|
+
var _3n2 = /* @__PURE__ */ BigInt(3);
|
|
2423
|
+
var _5n2 = BigInt(5);
|
|
2424
|
+
var _8n3 = BigInt(8);
|
|
2425
|
+
var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
2426
|
+
var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
2427
|
+
p: ed25519_CURVE_p,
|
|
2428
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
2429
|
+
h: _8n3,
|
|
2430
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
2431
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
2432
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
2433
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
2434
|
+
}))();
|
|
2435
|
+
function ed25519_pow_2_252_3(x) {
|
|
2436
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
2437
|
+
const P = ed25519_CURVE_p;
|
|
2438
|
+
const x2 = x * x % P;
|
|
2439
|
+
const b2 = x2 * x % P;
|
|
2440
|
+
const b4 = pow2(b2, _2n4, P) * b2 % P;
|
|
2441
|
+
const b5 = pow2(b4, _1n6, P) * x % P;
|
|
2442
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
2443
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
2444
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
2445
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
2446
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
2447
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
2448
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
2449
|
+
const pow_p_5_8 = pow2(b250, _2n4, P) * x % P;
|
|
2450
|
+
return { pow_p_5_8, b2 };
|
|
2451
|
+
}
|
|
2452
|
+
function adjustScalarBytes(bytes) {
|
|
2453
|
+
bytes[0] &= 248;
|
|
2454
|
+
bytes[31] &= 127;
|
|
2455
|
+
bytes[31] |= 64;
|
|
2456
|
+
return bytes;
|
|
2457
|
+
}
|
|
2458
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
2459
|
+
function uvRatio(u, v) {
|
|
2460
|
+
const P = ed25519_CURVE_p;
|
|
2461
|
+
const v3 = mod(v * v * v, P);
|
|
2462
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
2463
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
2464
|
+
let x = mod(u * v3 * pow, P);
|
|
2465
|
+
const vx2 = mod(v * x * x, P);
|
|
2466
|
+
const root1 = x;
|
|
2467
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
2468
|
+
const useRoot1 = vx2 === u;
|
|
2469
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
2470
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
2471
|
+
if (useRoot1)
|
|
2472
|
+
x = root1;
|
|
2473
|
+
if (useRoot2 || noRoot)
|
|
2474
|
+
x = root2;
|
|
2475
|
+
if (isNegativeLE(x, P))
|
|
2476
|
+
x = mod(-x, P);
|
|
2477
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
2478
|
+
}
|
|
2479
|
+
var ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
|
|
2480
|
+
var Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
2481
|
+
var Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
|
|
2482
|
+
function ed25519_domain(data, ctx, phflag) {
|
|
2483
|
+
if (ctx.length > 255)
|
|
2484
|
+
throw new Error("Context is too big");
|
|
2485
|
+
return concatBytes(asciiToBytes("SigEd25519 no Ed25519 collisions"), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
|
|
2486
|
+
}
|
|
2487
|
+
function ed(opts) {
|
|
2488
|
+
return eddsa(ed25519_Point, sha512, Object.assign({ adjustScalarBytes }, opts));
|
|
2489
|
+
}
|
|
2490
|
+
var ed25519 = /* @__PURE__ */ ed({});
|
|
2491
|
+
var ed25519ctx = /* @__PURE__ */ ed({ domain: ed25519_domain });
|
|
2492
|
+
var ed25519ph = /* @__PURE__ */ ed({ domain: ed25519_domain, prehash: sha512 });
|
|
2493
|
+
var x25519 = /* @__PURE__ */ (() => {
|
|
2494
|
+
const P = ed25519_CURVE_p;
|
|
2495
|
+
return montgomery({
|
|
2496
|
+
P,
|
|
2497
|
+
type: "x25519",
|
|
2498
|
+
powPminus2: (x) => {
|
|
2499
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
2500
|
+
return mod(pow2(pow_p_5_8, _3n2, P) * b2, P);
|
|
2501
|
+
},
|
|
2502
|
+
adjustScalarBytes
|
|
2503
|
+
});
|
|
2504
|
+
})();
|
|
2505
|
+
var ELL2_C1 = /* @__PURE__ */ (() => (ed25519_CURVE_p + _3n2) / _8n3)();
|
|
2506
|
+
var ELL2_C2 = /* @__PURE__ */ (() => Fp.pow(_2n4, ELL2_C1))();
|
|
2507
|
+
var ELL2_C3 = /* @__PURE__ */ (() => Fp.sqrt(Fp.neg(Fp.ONE)))();
|
|
2508
|
+
function _map_to_curve_elligator2_curve25519(u) {
|
|
2509
|
+
const ELL2_C4 = (ed25519_CURVE_p - _5n2) / _8n3;
|
|
2510
|
+
const ELL2_J = BigInt(486662);
|
|
2511
|
+
let tv1 = Fp.sqr(u);
|
|
2512
|
+
tv1 = Fp.mul(tv1, _2n4);
|
|
2513
|
+
let xd = Fp.add(tv1, Fp.ONE);
|
|
2514
|
+
let x1n = Fp.neg(ELL2_J);
|
|
2515
|
+
let tv2 = Fp.sqr(xd);
|
|
2516
|
+
let gxd = Fp.mul(tv2, xd);
|
|
2517
|
+
let gx1 = Fp.mul(tv1, ELL2_J);
|
|
2518
|
+
gx1 = Fp.mul(gx1, x1n);
|
|
2519
|
+
gx1 = Fp.add(gx1, tv2);
|
|
2520
|
+
gx1 = Fp.mul(gx1, x1n);
|
|
2521
|
+
let tv3 = Fp.sqr(gxd);
|
|
2522
|
+
tv2 = Fp.sqr(tv3);
|
|
2523
|
+
tv3 = Fp.mul(tv3, gxd);
|
|
2524
|
+
tv3 = Fp.mul(tv3, gx1);
|
|
2525
|
+
tv2 = Fp.mul(tv2, tv3);
|
|
2526
|
+
let y11 = Fp.pow(tv2, ELL2_C4);
|
|
2527
|
+
y11 = Fp.mul(y11, tv3);
|
|
2528
|
+
let y12 = Fp.mul(y11, ELL2_C3);
|
|
2529
|
+
tv2 = Fp.sqr(y11);
|
|
2530
|
+
tv2 = Fp.mul(tv2, gxd);
|
|
2531
|
+
let e1 = Fp.eql(tv2, gx1);
|
|
2532
|
+
let y1 = Fp.cmov(y12, y11, e1);
|
|
2533
|
+
let x2n = Fp.mul(x1n, tv1);
|
|
2534
|
+
let y21 = Fp.mul(y11, u);
|
|
2535
|
+
y21 = Fp.mul(y21, ELL2_C2);
|
|
2536
|
+
let y22 = Fp.mul(y21, ELL2_C3);
|
|
2537
|
+
let gx2 = Fp.mul(gx1, tv1);
|
|
2538
|
+
tv2 = Fp.sqr(y21);
|
|
2539
|
+
tv2 = Fp.mul(tv2, gxd);
|
|
2540
|
+
let e2 = Fp.eql(tv2, gx2);
|
|
2541
|
+
let y2 = Fp.cmov(y22, y21, e2);
|
|
2542
|
+
tv2 = Fp.sqr(y1);
|
|
2543
|
+
tv2 = Fp.mul(tv2, gxd);
|
|
2544
|
+
let e3 = Fp.eql(tv2, gx1);
|
|
2545
|
+
let xn = Fp.cmov(x2n, x1n, e3);
|
|
2546
|
+
let y = Fp.cmov(y2, y1, e3);
|
|
2547
|
+
let e4 = Fp.isOdd(y);
|
|
2548
|
+
y = Fp.cmov(y, Fp.neg(y), e3 !== e4);
|
|
2549
|
+
return { xMn: xn, xMd: xd, yMn: y, yMd: _1n6 };
|
|
2550
|
+
}
|
|
2551
|
+
var ELL2_C1_EDWARDS = /* @__PURE__ */ (() => FpSqrtEven(Fp, Fp.neg(BigInt(486664))))();
|
|
2552
|
+
function map_to_curve_elligator2_edwards25519(u) {
|
|
2553
|
+
const { xMn, xMd, yMn, yMd } = _map_to_curve_elligator2_curve25519(u);
|
|
2554
|
+
let xn = Fp.mul(xMn, yMd);
|
|
2555
|
+
xn = Fp.mul(xn, ELL2_C1_EDWARDS);
|
|
2556
|
+
let xd = Fp.mul(xMd, yMn);
|
|
2557
|
+
let yn = Fp.sub(xMn, xMd);
|
|
2558
|
+
let yd = Fp.add(xMn, xMd);
|
|
2559
|
+
let tv1 = Fp.mul(xd, yd);
|
|
2560
|
+
let e = Fp.eql(tv1, Fp.ZERO);
|
|
2561
|
+
xn = Fp.cmov(xn, Fp.ZERO, e);
|
|
2562
|
+
xd = Fp.cmov(xd, Fp.ONE, e);
|
|
2563
|
+
yn = Fp.cmov(yn, Fp.ONE, e);
|
|
2564
|
+
yd = Fp.cmov(yd, Fp.ONE, e);
|
|
2565
|
+
const [xd_inv, yd_inv] = FpInvertBatch(Fp, [xd, yd], true);
|
|
2566
|
+
return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) };
|
|
2567
|
+
}
|
|
2568
|
+
var ed25519_hasher = /* @__PURE__ */ (() => createHasher2(ed25519_Point, (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]), {
|
|
2569
|
+
DST: "edwards25519_XMD:SHA-512_ELL2_RO_",
|
|
2570
|
+
encodeDST: "edwards25519_XMD:SHA-512_ELL2_NU_",
|
|
2571
|
+
p: ed25519_CURVE_p,
|
|
2572
|
+
m: 1,
|
|
2573
|
+
k: 128,
|
|
2574
|
+
expand: "xmd",
|
|
2575
|
+
hash: sha512
|
|
2576
|
+
}))();
|
|
2577
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
2578
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
2579
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
2580
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
2581
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
2582
|
+
var invertSqrt = (number) => uvRatio(_1n6, number);
|
|
2583
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
2584
|
+
var bytes255ToNumberLE = (bytes) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
2585
|
+
function calcElligatorRistrettoMap(r0) {
|
|
2586
|
+
const { d } = ed25519_CURVE;
|
|
2587
|
+
const P = ed25519_CURVE_p;
|
|
2588
|
+
const mod2 = (n) => Fp.create(n);
|
|
2589
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
2590
|
+
const Ns = mod2((r + _1n6) * ONE_MINUS_D_SQ);
|
|
2591
|
+
let c = BigInt(-1);
|
|
2592
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
2593
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
2594
|
+
let s_ = mod2(s * r0);
|
|
2595
|
+
if (!isNegativeLE(s_, P))
|
|
2596
|
+
s_ = mod2(-s_);
|
|
2597
|
+
if (!Ns_D_is_sq)
|
|
2598
|
+
s = s_;
|
|
2599
|
+
if (!Ns_D_is_sq)
|
|
2600
|
+
c = r;
|
|
2601
|
+
const Nt = mod2(c * (r - _1n6) * D_MINUS_ONE_SQ - D);
|
|
2602
|
+
const s2 = s * s;
|
|
2603
|
+
const W0 = mod2((s + s) * D);
|
|
2604
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
2605
|
+
const W2 = mod2(_1n6 - s2);
|
|
2606
|
+
const W3 = mod2(_1n6 + s2);
|
|
2607
|
+
return new ed25519_Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
2608
|
+
}
|
|
2609
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
2610
|
+
// Do NOT change syntax: the following gymnastics is done,
|
|
2611
|
+
// because typescript strips comments, which makes bundlers disable tree-shaking.
|
|
2612
|
+
// prettier-ignore
|
|
2613
|
+
static BASE = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.BASE))();
|
|
2614
|
+
// prettier-ignore
|
|
2615
|
+
static ZERO = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.ZERO))();
|
|
2616
|
+
// prettier-ignore
|
|
2617
|
+
static Fp = /* @__PURE__ */ (() => Fp)();
|
|
2618
|
+
// prettier-ignore
|
|
2619
|
+
static Fn = /* @__PURE__ */ (() => Fn)();
|
|
2620
|
+
constructor(ep) {
|
|
2621
|
+
super(ep);
|
|
2622
|
+
}
|
|
2623
|
+
static fromAffine(ap) {
|
|
2624
|
+
return new __RistrettoPoint(ed25519_Point.fromAffine(ap));
|
|
2625
|
+
}
|
|
2626
|
+
assertSame(other) {
|
|
2627
|
+
if (!(other instanceof __RistrettoPoint))
|
|
2628
|
+
throw new Error("RistrettoPoint expected");
|
|
2629
|
+
}
|
|
2630
|
+
init(ep) {
|
|
2631
|
+
return new __RistrettoPoint(ep);
|
|
2632
|
+
}
|
|
2633
|
+
static fromBytes(bytes) {
|
|
2634
|
+
abytes(bytes, 32);
|
|
2635
|
+
const { a, d } = ed25519_CURVE;
|
|
2636
|
+
const P = ed25519_CURVE_p;
|
|
2637
|
+
const mod2 = (n) => Fp.create(n);
|
|
2638
|
+
const s = bytes255ToNumberLE(bytes);
|
|
2639
|
+
if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
2640
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
2641
|
+
const s2 = mod2(s * s);
|
|
2642
|
+
const u1 = mod2(_1n6 + a * s2);
|
|
2643
|
+
const u2 = mod2(_1n6 - a * s2);
|
|
2644
|
+
const u1_2 = mod2(u1 * u1);
|
|
2645
|
+
const u2_2 = mod2(u2 * u2);
|
|
2646
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
2647
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
2648
|
+
const Dx = mod2(I * u2);
|
|
2649
|
+
const Dy = mod2(I * Dx * v);
|
|
2650
|
+
let x = mod2((s + s) * Dx);
|
|
2651
|
+
if (isNegativeLE(x, P))
|
|
2652
|
+
x = mod2(-x);
|
|
2653
|
+
const y = mod2(u1 * Dy);
|
|
2654
|
+
const t = mod2(x * y);
|
|
2655
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n6)
|
|
2656
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
2657
|
+
return new __RistrettoPoint(new ed25519_Point(x, y, _1n6, t));
|
|
2658
|
+
}
|
|
2659
|
+
/**
|
|
2660
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
2661
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
2662
|
+
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
2663
|
+
*/
|
|
2664
|
+
static fromHex(hex) {
|
|
2665
|
+
return __RistrettoPoint.fromBytes(hexToBytes(hex));
|
|
2666
|
+
}
|
|
2667
|
+
/**
|
|
2668
|
+
* Encodes ristretto point to Uint8Array.
|
|
2669
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
2670
|
+
*/
|
|
2671
|
+
toBytes() {
|
|
2672
|
+
let { X, Y, Z, T } = this.ep;
|
|
2673
|
+
const P = ed25519_CURVE_p;
|
|
2674
|
+
const mod2 = (n) => Fp.create(n);
|
|
2675
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
2676
|
+
const u2 = mod2(X * Y);
|
|
2677
|
+
const u2sq = mod2(u2 * u2);
|
|
2678
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
2679
|
+
const D1 = mod2(invsqrt * u1);
|
|
2680
|
+
const D2 = mod2(invsqrt * u2);
|
|
2681
|
+
const zInv = mod2(D1 * D2 * T);
|
|
2682
|
+
let D;
|
|
2683
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
2684
|
+
let _x = mod2(Y * SQRT_M1);
|
|
2685
|
+
let _y = mod2(X * SQRT_M1);
|
|
2686
|
+
X = _x;
|
|
2687
|
+
Y = _y;
|
|
2688
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
2689
|
+
} else {
|
|
2690
|
+
D = D2;
|
|
2691
|
+
}
|
|
2692
|
+
if (isNegativeLE(X * zInv, P))
|
|
2693
|
+
Y = mod2(-Y);
|
|
2694
|
+
let s = mod2((Z - Y) * D);
|
|
2695
|
+
if (isNegativeLE(s, P))
|
|
2696
|
+
s = mod2(-s);
|
|
2697
|
+
return Fp.toBytes(s);
|
|
2698
|
+
}
|
|
2699
|
+
/**
|
|
2700
|
+
* Compares two Ristretto points.
|
|
2701
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
2702
|
+
*/
|
|
2703
|
+
equals(other) {
|
|
2704
|
+
this.assertSame(other);
|
|
2705
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
2706
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
2707
|
+
const mod2 = (n) => Fp.create(n);
|
|
2708
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
2709
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
2710
|
+
return one || two;
|
|
2711
|
+
}
|
|
2712
|
+
is0() {
|
|
2713
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
2714
|
+
}
|
|
2715
|
+
};
|
|
2716
|
+
var ristretto255 = { Point: _RistrettoPoint };
|
|
2717
|
+
var ristretto255_hasher = {
|
|
2718
|
+
Point: _RistrettoPoint,
|
|
2719
|
+
/**
|
|
2720
|
+
* Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
|
|
2721
|
+
* * There are no test vectors
|
|
2722
|
+
* * encodeToCurve / mapToCurve is undefined
|
|
2723
|
+
* * mapToCurve would be `calcElligatorRistrettoMap(scalars[0])`, not ristretto255_map!
|
|
2724
|
+
* * hashToScalar is undefined too, so we just use OPRF implementation
|
|
2725
|
+
* * We cannot re-use 'createHasher', because ristretto255_map is different algorithm/RFC
|
|
2726
|
+
(os2ip -> bytes255ToNumberLE)
|
|
2727
|
+
* * mapToCurve == calcElligatorRistrettoMap, hashToCurve == ristretto255_map
|
|
2728
|
+
* * hashToScalar is undefined in RFC9380 for ristretto, we are using version from OPRF here, using bytes255ToNumblerLE will create different result if we use bytes255ToNumberLE as os2ip
|
|
2729
|
+
* * current version is closest to spec.
|
|
2730
|
+
*/
|
|
2731
|
+
hashToCurve(msg, options) {
|
|
2732
|
+
const DST = options?.DST || "ristretto255_XMD:SHA-512_R255MAP_RO_";
|
|
2733
|
+
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
2734
|
+
return ristretto255_hasher.deriveToCurve(xmd);
|
|
2735
|
+
},
|
|
2736
|
+
hashToScalar(msg, options = { DST: _DST_scalar }) {
|
|
2737
|
+
const xmd = expand_message_xmd(msg, options.DST, 64, sha512);
|
|
2738
|
+
return Fn.create(bytesToNumberLE(xmd));
|
|
2739
|
+
},
|
|
2740
|
+
/**
|
|
2741
|
+
* HashToCurve-like construction based on RFC 9496 (Element Derivation).
|
|
2742
|
+
* Converts 64 uniform random bytes into a curve point.
|
|
2743
|
+
*
|
|
2744
|
+
* WARNING: This represents an older hash-to-curve construction, preceding the finalization of RFC 9380.
|
|
2745
|
+
* It was later reused as a component in the newer `hash_to_ristretto255` function defined in RFC 9380.
|
|
2746
|
+
*/
|
|
2747
|
+
deriveToCurve(bytes) {
|
|
2748
|
+
abytes(bytes, 64);
|
|
2749
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
2750
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
2751
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
2752
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
2753
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
2754
|
+
}
|
|
2755
|
+
};
|
|
2756
|
+
var ristretto255_oprf = /* @__PURE__ */ (() => createORPF({
|
|
2757
|
+
name: "ristretto255-SHA512",
|
|
2758
|
+
Point: _RistrettoPoint,
|
|
2759
|
+
hash: sha512,
|
|
2760
|
+
hashToGroup: ristretto255_hasher.hashToCurve,
|
|
2761
|
+
hashToScalar: ristretto255_hasher.hashToScalar
|
|
2762
|
+
}))();
|
|
2763
|
+
var ED25519_TORSION_SUBGROUP = [
|
|
2764
|
+
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
2765
|
+
"c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a",
|
|
2766
|
+
"0000000000000000000000000000000000000000000000000000000000000080",
|
|
2767
|
+
"26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05",
|
|
2768
|
+
"ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
|
2769
|
+
"26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85",
|
|
2770
|
+
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
2771
|
+
"c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa"
|
|
2772
|
+
];
|
|
2773
|
+
|
|
2774
|
+
export {
|
|
2775
|
+
hexToBytes,
|
|
2776
|
+
sha256,
|
|
2777
|
+
ed25519,
|
|
2778
|
+
ed25519ctx,
|
|
2779
|
+
ed25519ph,
|
|
2780
|
+
x25519,
|
|
2781
|
+
_map_to_curve_elligator2_curve25519,
|
|
2782
|
+
ed25519_hasher,
|
|
2783
|
+
ristretto255,
|
|
2784
|
+
ristretto255_hasher,
|
|
2785
|
+
ristretto255_oprf,
|
|
2786
|
+
ED25519_TORSION_SUBGROUP
|
|
2787
|
+
};
|
|
2788
|
+
/*! Bundled license information:
|
|
2789
|
+
|
|
2790
|
+
@noble/hashes/utils.js:
|
|
2791
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2792
|
+
|
|
2793
|
+
@noble/curves/utils.js:
|
|
2794
|
+
@noble/curves/abstract/modular.js:
|
|
2795
|
+
@noble/curves/abstract/curve.js:
|
|
2796
|
+
@noble/curves/abstract/edwards.js:
|
|
2797
|
+
@noble/curves/abstract/montgomery.js:
|
|
2798
|
+
@noble/curves/abstract/oprf.js:
|
|
2799
|
+
@noble/curves/ed25519.js:
|
|
2800
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2801
|
+
*/
|
|
2802
|
+
//# sourceMappingURL=chunk-QTDCFXPF.js.map
|