@oneaddress/partner-sdk 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +482 -350
- package/dist/chunk-2QGC7BHC.mjs +3752 -0
- package/dist/{chunk-EHR6T6MA.mjs → chunk-O2RSPJVP.mjs} +188 -25
- package/dist/edge.d.mts +1 -3
- package/dist/edge.d.ts +1 -3
- package/dist/edge.js +4089 -31
- package/dist/edge.mjs +19 -3
- package/dist/index.d.mts +284 -22
- package/dist/index.d.ts +284 -22
- package/dist/index.js +3682 -117
- package/dist/index.mjs +17 -3
- package/dist/{src-NUBMOPDK.mjs → src-DYGFEKX6.mjs} +13 -1
- package/package.json +38 -38
- package/dist/chunk-TGLIJMGK.mjs +0 -498
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
9
|
var __esm = (fn, res) => function __init() {
|
|
9
10
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
11
|
};
|
|
@@ -20,15 +21,3183 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
20
21
|
}
|
|
21
22
|
return to;
|
|
22
23
|
};
|
|
23
|
-
var __toESM = (
|
|
24
|
+
var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
|
|
24
25
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
26
|
// file that has been converted to a CommonJS file using a Babel-
|
|
26
27
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
28
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
-
isNodeMode || !
|
|
29
|
-
|
|
29
|
+
isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
|
|
30
|
+
mod2
|
|
30
31
|
));
|
|
31
|
-
var __toCommonJS = (
|
|
32
|
+
var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
|
|
33
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
34
|
+
|
|
35
|
+
// ../../../noble-install/node_modules/@noble/hashes/utils.js
|
|
36
|
+
function isBytes(a) {
|
|
37
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
38
|
+
}
|
|
39
|
+
function anumber(n, title = "") {
|
|
40
|
+
if (typeof n !== "number") {
|
|
41
|
+
const prefix = title && `"${title}" `;
|
|
42
|
+
throw new TypeError(`${prefix}expected number, got ${typeof n}`);
|
|
43
|
+
}
|
|
44
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
45
|
+
const prefix = title && `"${title}" `;
|
|
46
|
+
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function abytes(value, length, title = "") {
|
|
50
|
+
const bytes = isBytes(value);
|
|
51
|
+
const len = value?.length;
|
|
52
|
+
const needsLen = length !== void 0;
|
|
53
|
+
if (!bytes || needsLen && len !== length) {
|
|
54
|
+
const prefix = title && `"${title}" `;
|
|
55
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
56
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
57
|
+
const message = prefix + "expected Uint8Array" + ofLen + ", got " + got;
|
|
58
|
+
if (!bytes)
|
|
59
|
+
throw new TypeError(message);
|
|
60
|
+
throw new RangeError(message);
|
|
61
|
+
}
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
function aexists(instance, checkFinished = true) {
|
|
65
|
+
if (instance.destroyed)
|
|
66
|
+
throw new Error("Hash instance has been destroyed");
|
|
67
|
+
if (checkFinished && instance.finished)
|
|
68
|
+
throw new Error("Hash#digest() has already been called");
|
|
69
|
+
}
|
|
70
|
+
function aoutput(out, instance) {
|
|
71
|
+
abytes(out, void 0, "digestInto() output");
|
|
72
|
+
const min = instance.outputLen;
|
|
73
|
+
if (out.length < min) {
|
|
74
|
+
throw new RangeError('"digestInto() output" expected to be of length >=' + min);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function u32(arr) {
|
|
78
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
79
|
+
}
|
|
80
|
+
function clean(...arrays) {
|
|
81
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
82
|
+
arrays[i].fill(0);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function createView(arr) {
|
|
86
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
87
|
+
}
|
|
88
|
+
function byteSwap(word) {
|
|
89
|
+
return word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
|
|
90
|
+
}
|
|
91
|
+
function byteSwap32(arr) {
|
|
92
|
+
for (let i = 0; i < arr.length; i++) {
|
|
93
|
+
arr[i] = byteSwap(arr[i]);
|
|
94
|
+
}
|
|
95
|
+
return arr;
|
|
96
|
+
}
|
|
97
|
+
function bytesToHex(bytes) {
|
|
98
|
+
abytes(bytes);
|
|
99
|
+
if (hasHexBuiltin)
|
|
100
|
+
return bytes.toHex();
|
|
101
|
+
let hex = "";
|
|
102
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
103
|
+
hex += hexes[bytes[i]];
|
|
104
|
+
}
|
|
105
|
+
return hex;
|
|
106
|
+
}
|
|
107
|
+
function asciiToBase16(ch) {
|
|
108
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
109
|
+
return ch - asciis._0;
|
|
110
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
111
|
+
return ch - (asciis.A - 10);
|
|
112
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
113
|
+
return ch - (asciis.a - 10);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
function hexToBytes(hex) {
|
|
117
|
+
if (typeof hex !== "string")
|
|
118
|
+
throw new TypeError("hex string expected, got " + typeof hex);
|
|
119
|
+
if (hasHexBuiltin) {
|
|
120
|
+
try {
|
|
121
|
+
return Uint8Array.fromHex(hex);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (error instanceof SyntaxError)
|
|
124
|
+
throw new RangeError(error.message);
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const hl = hex.length;
|
|
129
|
+
const al = hl / 2;
|
|
130
|
+
if (hl % 2)
|
|
131
|
+
throw new RangeError("hex string expected, got unpadded hex of length " + hl);
|
|
132
|
+
const array = new Uint8Array(al);
|
|
133
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
134
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
135
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
136
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
137
|
+
const char = hex[hi] + hex[hi + 1];
|
|
138
|
+
throw new RangeError('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
139
|
+
}
|
|
140
|
+
array[ai] = n1 * 16 + n2;
|
|
141
|
+
}
|
|
142
|
+
return array;
|
|
143
|
+
}
|
|
144
|
+
function concatBytes(...arrays) {
|
|
145
|
+
let sum = 0;
|
|
146
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
147
|
+
const a = arrays[i];
|
|
148
|
+
abytes(a);
|
|
149
|
+
sum += a.length;
|
|
150
|
+
}
|
|
151
|
+
const res = new Uint8Array(sum);
|
|
152
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
153
|
+
const a = arrays[i];
|
|
154
|
+
res.set(a, pad);
|
|
155
|
+
pad += a.length;
|
|
156
|
+
}
|
|
157
|
+
return res;
|
|
158
|
+
}
|
|
159
|
+
function createHasher(hashCons, info = {}) {
|
|
160
|
+
const hashC = (msg, opts2) => hashCons(opts2).update(msg).digest();
|
|
161
|
+
const tmp = hashCons(void 0);
|
|
162
|
+
hashC.outputLen = tmp.outputLen;
|
|
163
|
+
hashC.blockLen = tmp.blockLen;
|
|
164
|
+
hashC.canXOF = tmp.canXOF;
|
|
165
|
+
hashC.create = (opts2) => hashCons(opts2);
|
|
166
|
+
Object.assign(hashC, info);
|
|
167
|
+
return Object.freeze(hashC);
|
|
168
|
+
}
|
|
169
|
+
function randomBytes(bytesLength = 32) {
|
|
170
|
+
anumber(bytesLength, "bytesLength");
|
|
171
|
+
const cr = typeof globalThis === "object" ? globalThis.crypto : null;
|
|
172
|
+
if (typeof cr?.getRandomValues !== "function")
|
|
173
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
174
|
+
if (bytesLength > 65536)
|
|
175
|
+
throw new RangeError(`"bytesLength" expected <= 65536, got ${bytesLength}`);
|
|
176
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
177
|
+
}
|
|
178
|
+
var isLE, swap32IfBE, hasHexBuiltin, hexes, asciis, oidNist;
|
|
179
|
+
var init_utils = __esm({
|
|
180
|
+
"../../../noble-install/node_modules/@noble/hashes/utils.js"() {
|
|
181
|
+
"use strict";
|
|
182
|
+
isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
|
|
183
|
+
swap32IfBE = isLE ? (u) => u : byteSwap32;
|
|
184
|
+
hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
185
|
+
// @ts-ignore
|
|
186
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
187
|
+
))();
|
|
188
|
+
hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
189
|
+
asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
190
|
+
oidNist = (suffix) => ({
|
|
191
|
+
// Current NIST hashAlgs suffixes used here fit in one DER subidentifier octet.
|
|
192
|
+
// Larger suffix values would need base-128 OID encoding and a different length byte.
|
|
193
|
+
oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, suffix])
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// ../../../noble-install/node_modules/@noble/curves/utils.js
|
|
199
|
+
function abool(value, title = "") {
|
|
200
|
+
if (typeof value !== "boolean") {
|
|
201
|
+
const prefix = title && `"${title}" `;
|
|
202
|
+
throw new TypeError(prefix + "expected boolean, got type=" + typeof value);
|
|
203
|
+
}
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
function abignumber(n) {
|
|
207
|
+
if (typeof n === "bigint") {
|
|
208
|
+
if (!isPosBig(n))
|
|
209
|
+
throw new RangeError("positive bigint expected, got " + n);
|
|
210
|
+
} else
|
|
211
|
+
anumber2(n);
|
|
212
|
+
return n;
|
|
213
|
+
}
|
|
214
|
+
function asafenumber(value, title = "") {
|
|
215
|
+
if (typeof value !== "number") {
|
|
216
|
+
const prefix = title && `"${title}" `;
|
|
217
|
+
throw new TypeError(prefix + "expected number, got type=" + typeof value);
|
|
218
|
+
}
|
|
219
|
+
if (!Number.isSafeInteger(value)) {
|
|
220
|
+
const prefix = title && `"${title}" `;
|
|
221
|
+
throw new RangeError(prefix + "expected safe integer, got " + value);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function hexToNumber(hex) {
|
|
225
|
+
if (typeof hex !== "string")
|
|
226
|
+
throw new TypeError("hex string expected, got " + typeof hex);
|
|
227
|
+
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
228
|
+
}
|
|
229
|
+
function bytesToNumberBE(bytes) {
|
|
230
|
+
return hexToNumber(bytesToHex(bytes));
|
|
231
|
+
}
|
|
232
|
+
function bytesToNumberLE(bytes) {
|
|
233
|
+
return hexToNumber(bytesToHex(copyBytes(abytes(bytes)).reverse()));
|
|
234
|
+
}
|
|
235
|
+
function numberToBytesBE(n, len) {
|
|
236
|
+
anumber(len);
|
|
237
|
+
if (len === 0)
|
|
238
|
+
throw new RangeError("zero length");
|
|
239
|
+
n = abignumber(n);
|
|
240
|
+
const hex = n.toString(16);
|
|
241
|
+
if (hex.length > len * 2)
|
|
242
|
+
throw new RangeError("number too large");
|
|
243
|
+
return hexToBytes(hex.padStart(len * 2, "0"));
|
|
244
|
+
}
|
|
245
|
+
function numberToBytesLE(n, len) {
|
|
246
|
+
return numberToBytesBE(n, len).reverse();
|
|
247
|
+
}
|
|
248
|
+
function equalBytes(a, b) {
|
|
249
|
+
a = abytes2(a);
|
|
250
|
+
b = abytes2(b);
|
|
251
|
+
if (a.length !== b.length)
|
|
252
|
+
return false;
|
|
253
|
+
let diff = 0;
|
|
254
|
+
for (let i = 0; i < a.length; i++)
|
|
255
|
+
diff |= a[i] ^ b[i];
|
|
256
|
+
return diff === 0;
|
|
257
|
+
}
|
|
258
|
+
function copyBytes(bytes) {
|
|
259
|
+
return Uint8Array.from(abytes2(bytes));
|
|
260
|
+
}
|
|
261
|
+
function asciiToBytes(ascii) {
|
|
262
|
+
if (typeof ascii !== "string")
|
|
263
|
+
throw new TypeError("ascii string expected, got " + typeof ascii);
|
|
264
|
+
return Uint8Array.from(ascii, (c, i) => {
|
|
265
|
+
const charCode = c.charCodeAt(0);
|
|
266
|
+
if (c.length !== 1 || charCode > 127) {
|
|
267
|
+
throw new RangeError(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
|
|
268
|
+
}
|
|
269
|
+
return charCode;
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
function inRange(n, min, max) {
|
|
273
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
274
|
+
}
|
|
275
|
+
function aInRange(title, n, min, max) {
|
|
276
|
+
if (!inRange(n, min, max))
|
|
277
|
+
throw new RangeError("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
278
|
+
}
|
|
279
|
+
function bitLen(n) {
|
|
280
|
+
if (n < _0n)
|
|
281
|
+
throw new Error("expected non-negative bigint, got " + n);
|
|
282
|
+
let len;
|
|
283
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
284
|
+
;
|
|
285
|
+
return len;
|
|
286
|
+
}
|
|
287
|
+
function validateObject(object, fields = {}, optFields = {}) {
|
|
288
|
+
if (Object.prototype.toString.call(object) !== "[object Object]")
|
|
289
|
+
throw new TypeError("expected valid options object");
|
|
290
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
291
|
+
if (!isOpt && expectedType !== "function" && !Object.hasOwn(object, fieldName))
|
|
292
|
+
throw new TypeError(`param "${fieldName}" is invalid: expected own property`);
|
|
293
|
+
const val = object[fieldName];
|
|
294
|
+
if (isOpt && val === void 0)
|
|
295
|
+
return;
|
|
296
|
+
const current = typeof val;
|
|
297
|
+
if (current !== expectedType || val === null)
|
|
298
|
+
throw new TypeError(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
299
|
+
}
|
|
300
|
+
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
301
|
+
iter(fields, false);
|
|
302
|
+
iter(optFields, true);
|
|
303
|
+
}
|
|
304
|
+
var abytes2, anumber2, bytesToHex2, concatBytes2, hexToBytes2, isBytes2, randomBytes2, _0n, _1n, isPosBig, bitMask, notImplemented;
|
|
305
|
+
var init_utils2 = __esm({
|
|
306
|
+
"../../../noble-install/node_modules/@noble/curves/utils.js"() {
|
|
307
|
+
"use strict";
|
|
308
|
+
init_utils();
|
|
309
|
+
abytes2 = (value, length, title) => abytes(value, length, title);
|
|
310
|
+
anumber2 = anumber;
|
|
311
|
+
bytesToHex2 = bytesToHex;
|
|
312
|
+
concatBytes2 = (...arrays) => concatBytes(...arrays);
|
|
313
|
+
hexToBytes2 = (hex) => hexToBytes(hex);
|
|
314
|
+
isBytes2 = isBytes;
|
|
315
|
+
randomBytes2 = (bytesLength) => randomBytes(bytesLength);
|
|
316
|
+
_0n = /* @__PURE__ */ BigInt(0);
|
|
317
|
+
_1n = /* @__PURE__ */ BigInt(1);
|
|
318
|
+
isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
319
|
+
bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
320
|
+
notImplemented = () => {
|
|
321
|
+
throw new Error("not implemented");
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
// ../../../noble-install/node_modules/@noble/curves/abstract/modular.js
|
|
327
|
+
function mod(a, b) {
|
|
328
|
+
if (b <= _0n2)
|
|
329
|
+
throw new Error("mod: expected positive modulus, got " + b);
|
|
330
|
+
const result = a % b;
|
|
331
|
+
return result >= _0n2 ? result : b + result;
|
|
332
|
+
}
|
|
333
|
+
function pow2(x, power, modulo) {
|
|
334
|
+
if (power < _0n2)
|
|
335
|
+
throw new Error("pow2: expected non-negative exponent, got " + power);
|
|
336
|
+
let res = x;
|
|
337
|
+
while (power-- > _0n2) {
|
|
338
|
+
res *= res;
|
|
339
|
+
res %= modulo;
|
|
340
|
+
}
|
|
341
|
+
return res;
|
|
342
|
+
}
|
|
343
|
+
function invert(number, modulo) {
|
|
344
|
+
if (number === _0n2)
|
|
345
|
+
throw new Error("invert: expected non-zero number");
|
|
346
|
+
if (modulo <= _0n2)
|
|
347
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
348
|
+
let a = mod(number, modulo);
|
|
349
|
+
let b = modulo;
|
|
350
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
351
|
+
while (a !== _0n2) {
|
|
352
|
+
const q = b / a;
|
|
353
|
+
const r = b - a * q;
|
|
354
|
+
const m = x - u * q;
|
|
355
|
+
const n = y - v * q;
|
|
356
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
357
|
+
}
|
|
358
|
+
const gcd = b;
|
|
359
|
+
if (gcd !== _1n2)
|
|
360
|
+
throw new Error("invert: does not exist");
|
|
361
|
+
return mod(x, modulo);
|
|
362
|
+
}
|
|
363
|
+
function assertIsSquare(Fp2, root, n) {
|
|
364
|
+
const F2 = Fp2;
|
|
365
|
+
if (!F2.eql(F2.sqr(root), n))
|
|
366
|
+
throw new Error("Cannot find square root");
|
|
367
|
+
}
|
|
368
|
+
function sqrt3mod4(Fp2, n) {
|
|
369
|
+
const F2 = Fp2;
|
|
370
|
+
const p1div4 = (F2.ORDER + _1n2) / _4n;
|
|
371
|
+
const root = F2.pow(n, p1div4);
|
|
372
|
+
assertIsSquare(F2, root, n);
|
|
373
|
+
return root;
|
|
374
|
+
}
|
|
375
|
+
function sqrt5mod8(Fp2, n) {
|
|
376
|
+
const F2 = Fp2;
|
|
377
|
+
const p5div8 = (F2.ORDER - _5n) / _8n;
|
|
378
|
+
const n2 = F2.mul(n, _2n);
|
|
379
|
+
const v = F2.pow(n2, p5div8);
|
|
380
|
+
const nv = F2.mul(n, v);
|
|
381
|
+
const i = F2.mul(F2.mul(nv, _2n), v);
|
|
382
|
+
const root = F2.mul(nv, F2.sub(i, F2.ONE));
|
|
383
|
+
assertIsSquare(F2, root, n);
|
|
384
|
+
return root;
|
|
385
|
+
}
|
|
386
|
+
function sqrt9mod16(P) {
|
|
387
|
+
const Fp_ = Field(P);
|
|
388
|
+
const tn = tonelliShanks(P);
|
|
389
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
390
|
+
const c2 = tn(Fp_, c1);
|
|
391
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
392
|
+
const c4 = (P + _7n) / _16n;
|
|
393
|
+
return ((Fp2, n) => {
|
|
394
|
+
const F2 = Fp2;
|
|
395
|
+
let tv1 = F2.pow(n, c4);
|
|
396
|
+
let tv2 = F2.mul(tv1, c1);
|
|
397
|
+
const tv3 = F2.mul(tv1, c2);
|
|
398
|
+
const tv4 = F2.mul(tv1, c3);
|
|
399
|
+
const e1 = F2.eql(F2.sqr(tv2), n);
|
|
400
|
+
const e2 = F2.eql(F2.sqr(tv3), n);
|
|
401
|
+
tv1 = F2.cmov(tv1, tv2, e1);
|
|
402
|
+
tv2 = F2.cmov(tv4, tv3, e2);
|
|
403
|
+
const e3 = F2.eql(F2.sqr(tv2), n);
|
|
404
|
+
const root = F2.cmov(tv1, tv2, e3);
|
|
405
|
+
assertIsSquare(F2, root, n);
|
|
406
|
+
return root;
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
function tonelliShanks(P) {
|
|
410
|
+
if (P < _3n)
|
|
411
|
+
throw new Error("sqrt is not defined for small field");
|
|
412
|
+
let Q2 = P - _1n2;
|
|
413
|
+
let S = 0;
|
|
414
|
+
while (Q2 % _2n === _0n2) {
|
|
415
|
+
Q2 /= _2n;
|
|
416
|
+
S++;
|
|
417
|
+
}
|
|
418
|
+
let Z = _2n;
|
|
419
|
+
const _Fp = Field(P);
|
|
420
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
421
|
+
if (Z++ > 1e3)
|
|
422
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
423
|
+
}
|
|
424
|
+
if (S === 1)
|
|
425
|
+
return sqrt3mod4;
|
|
426
|
+
let cc = _Fp.pow(Z, Q2);
|
|
427
|
+
const Q1div2 = (Q2 + _1n2) / _2n;
|
|
428
|
+
return function tonelliSlow(Fp2, n) {
|
|
429
|
+
const F2 = Fp2;
|
|
430
|
+
if (F2.is0(n))
|
|
431
|
+
return n;
|
|
432
|
+
if (FpLegendre(F2, n) !== 1)
|
|
433
|
+
throw new Error("Cannot find square root");
|
|
434
|
+
let M = S;
|
|
435
|
+
let c = F2.mul(F2.ONE, cc);
|
|
436
|
+
let t = F2.pow(n, Q2);
|
|
437
|
+
let R = F2.pow(n, Q1div2);
|
|
438
|
+
while (!F2.eql(t, F2.ONE)) {
|
|
439
|
+
if (F2.is0(t))
|
|
440
|
+
return F2.ZERO;
|
|
441
|
+
let i = 1;
|
|
442
|
+
let t_tmp = F2.sqr(t);
|
|
443
|
+
while (!F2.eql(t_tmp, F2.ONE)) {
|
|
444
|
+
i++;
|
|
445
|
+
t_tmp = F2.sqr(t_tmp);
|
|
446
|
+
if (i === M)
|
|
447
|
+
throw new Error("Cannot find square root");
|
|
448
|
+
}
|
|
449
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
450
|
+
const b = F2.pow(c, exponent);
|
|
451
|
+
M = i;
|
|
452
|
+
c = F2.sqr(b);
|
|
453
|
+
t = F2.mul(t, c);
|
|
454
|
+
R = F2.mul(R, b);
|
|
455
|
+
}
|
|
456
|
+
return R;
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
function FpSqrt(P) {
|
|
460
|
+
if (P % _4n === _3n)
|
|
461
|
+
return sqrt3mod4;
|
|
462
|
+
if (P % _8n === _5n)
|
|
463
|
+
return sqrt5mod8;
|
|
464
|
+
if (P % _16n === _9n)
|
|
465
|
+
return sqrt9mod16(P);
|
|
466
|
+
return tonelliShanks(P);
|
|
467
|
+
}
|
|
468
|
+
function validateField(field) {
|
|
469
|
+
const initial = {
|
|
470
|
+
ORDER: "bigint",
|
|
471
|
+
BYTES: "number",
|
|
472
|
+
BITS: "number"
|
|
473
|
+
};
|
|
474
|
+
const opts2 = FIELD_FIELDS.reduce((map, val) => {
|
|
475
|
+
map[val] = "function";
|
|
476
|
+
return map;
|
|
477
|
+
}, initial);
|
|
478
|
+
validateObject(field, opts2);
|
|
479
|
+
asafenumber(field.BYTES, "BYTES");
|
|
480
|
+
asafenumber(field.BITS, "BITS");
|
|
481
|
+
if (field.BYTES < 1 || field.BITS < 1)
|
|
482
|
+
throw new Error("invalid field: expected BYTES/BITS > 0");
|
|
483
|
+
if (field.ORDER <= _1n2)
|
|
484
|
+
throw new Error("invalid field: expected ORDER > 1, got " + field.ORDER);
|
|
485
|
+
return field;
|
|
486
|
+
}
|
|
487
|
+
function FpPow(Fp2, num, power) {
|
|
488
|
+
const F2 = Fp2;
|
|
489
|
+
if (power < _0n2)
|
|
490
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
491
|
+
if (power === _0n2)
|
|
492
|
+
return F2.ONE;
|
|
493
|
+
if (power === _1n2)
|
|
494
|
+
return num;
|
|
495
|
+
let p = F2.ONE;
|
|
496
|
+
let d = num;
|
|
497
|
+
while (power > _0n2) {
|
|
498
|
+
if (power & _1n2)
|
|
499
|
+
p = F2.mul(p, d);
|
|
500
|
+
d = F2.sqr(d);
|
|
501
|
+
power >>= _1n2;
|
|
502
|
+
}
|
|
503
|
+
return p;
|
|
504
|
+
}
|
|
505
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
506
|
+
const F2 = Fp2;
|
|
507
|
+
const inverted = new Array(nums.length).fill(passZero ? F2.ZERO : void 0);
|
|
508
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
509
|
+
if (F2.is0(num))
|
|
510
|
+
return acc;
|
|
511
|
+
inverted[i] = acc;
|
|
512
|
+
return F2.mul(acc, num);
|
|
513
|
+
}, F2.ONE);
|
|
514
|
+
const invertedAcc = F2.inv(multipliedAcc);
|
|
515
|
+
nums.reduceRight((acc, num, i) => {
|
|
516
|
+
if (F2.is0(num))
|
|
517
|
+
return acc;
|
|
518
|
+
inverted[i] = F2.mul(acc, inverted[i]);
|
|
519
|
+
return F2.mul(acc, num);
|
|
520
|
+
}, invertedAcc);
|
|
521
|
+
return inverted;
|
|
522
|
+
}
|
|
523
|
+
function FpLegendre(Fp2, n) {
|
|
524
|
+
const F2 = Fp2;
|
|
525
|
+
const p1mod2 = (F2.ORDER - _1n2) / _2n;
|
|
526
|
+
const powered = F2.pow(n, p1mod2);
|
|
527
|
+
const yes = F2.eql(powered, F2.ONE);
|
|
528
|
+
const zero = F2.eql(powered, F2.ZERO);
|
|
529
|
+
const no = F2.eql(powered, F2.neg(F2.ONE));
|
|
530
|
+
if (!yes && !zero && !no)
|
|
531
|
+
throw new Error("invalid Legendre symbol result");
|
|
532
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
533
|
+
}
|
|
534
|
+
function nLength(n, nBitLength) {
|
|
535
|
+
if (nBitLength !== void 0)
|
|
536
|
+
anumber2(nBitLength);
|
|
537
|
+
if (n <= _0n2)
|
|
538
|
+
throw new Error("invalid n length: expected positive n, got " + n);
|
|
539
|
+
if (nBitLength !== void 0 && nBitLength < 1)
|
|
540
|
+
throw new Error("invalid n length: expected positive bit length, got " + nBitLength);
|
|
541
|
+
const bits = bitLen(n);
|
|
542
|
+
if (nBitLength !== void 0 && nBitLength < bits)
|
|
543
|
+
throw new Error(`invalid n length: expected bit length (${bits}) >= n.length (${nBitLength})`);
|
|
544
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : bits;
|
|
545
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
546
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
547
|
+
}
|
|
548
|
+
function Field(ORDER, opts2 = {}) {
|
|
549
|
+
return new _Field(ORDER, opts2);
|
|
550
|
+
}
|
|
551
|
+
var _0n2, _1n2, _2n, _3n, _4n, _5n, _7n, _8n, _9n, _16n, isNegativeLE, FIELD_FIELDS, FIELD_SQRT, _Field;
|
|
552
|
+
var init_modular = __esm({
|
|
553
|
+
"../../../noble-install/node_modules/@noble/curves/abstract/modular.js"() {
|
|
554
|
+
"use strict";
|
|
555
|
+
init_utils2();
|
|
556
|
+
_0n2 = /* @__PURE__ */ BigInt(0);
|
|
557
|
+
_1n2 = /* @__PURE__ */ BigInt(1);
|
|
558
|
+
_2n = /* @__PURE__ */ BigInt(2);
|
|
559
|
+
_3n = /* @__PURE__ */ BigInt(3);
|
|
560
|
+
_4n = /* @__PURE__ */ BigInt(4);
|
|
561
|
+
_5n = /* @__PURE__ */ BigInt(5);
|
|
562
|
+
_7n = /* @__PURE__ */ BigInt(7);
|
|
563
|
+
_8n = /* @__PURE__ */ BigInt(8);
|
|
564
|
+
_9n = /* @__PURE__ */ BigInt(9);
|
|
565
|
+
_16n = /* @__PURE__ */ BigInt(16);
|
|
566
|
+
isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
567
|
+
FIELD_FIELDS = [
|
|
568
|
+
"create",
|
|
569
|
+
"isValid",
|
|
570
|
+
"is0",
|
|
571
|
+
"neg",
|
|
572
|
+
"inv",
|
|
573
|
+
"sqrt",
|
|
574
|
+
"sqr",
|
|
575
|
+
"eql",
|
|
576
|
+
"add",
|
|
577
|
+
"sub",
|
|
578
|
+
"mul",
|
|
579
|
+
"pow",
|
|
580
|
+
"div",
|
|
581
|
+
"addN",
|
|
582
|
+
"subN",
|
|
583
|
+
"mulN",
|
|
584
|
+
"sqrN"
|
|
585
|
+
];
|
|
586
|
+
FIELD_SQRT = /* @__PURE__ */ new WeakMap();
|
|
587
|
+
_Field = class {
|
|
588
|
+
constructor(ORDER, opts2 = {}) {
|
|
589
|
+
__publicField(this, "ORDER");
|
|
590
|
+
__publicField(this, "BITS");
|
|
591
|
+
__publicField(this, "BYTES");
|
|
592
|
+
__publicField(this, "isLE");
|
|
593
|
+
__publicField(this, "ZERO", _0n2);
|
|
594
|
+
__publicField(this, "ONE", _1n2);
|
|
595
|
+
__publicField(this, "_lengths");
|
|
596
|
+
__publicField(this, "_mod");
|
|
597
|
+
if (ORDER <= _1n2)
|
|
598
|
+
throw new Error("invalid field: expected ORDER > 1, got " + ORDER);
|
|
599
|
+
let _nbitLength = void 0;
|
|
600
|
+
this.isLE = false;
|
|
601
|
+
if (opts2 != null && typeof opts2 === "object") {
|
|
602
|
+
if (typeof opts2.BITS === "number")
|
|
603
|
+
_nbitLength = opts2.BITS;
|
|
604
|
+
if (typeof opts2.sqrt === "function")
|
|
605
|
+
Object.defineProperty(this, "sqrt", { value: opts2.sqrt, enumerable: true });
|
|
606
|
+
if (typeof opts2.isLE === "boolean")
|
|
607
|
+
this.isLE = opts2.isLE;
|
|
608
|
+
if (opts2.allowedLengths)
|
|
609
|
+
this._lengths = Object.freeze(opts2.allowedLengths.slice());
|
|
610
|
+
if (typeof opts2.modFromBytes === "boolean")
|
|
611
|
+
this._mod = opts2.modFromBytes;
|
|
612
|
+
}
|
|
613
|
+
const { nBitLength, nByteLength } = nLength(ORDER, _nbitLength);
|
|
614
|
+
if (nByteLength > 2048)
|
|
615
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
616
|
+
this.ORDER = ORDER;
|
|
617
|
+
this.BITS = nBitLength;
|
|
618
|
+
this.BYTES = nByteLength;
|
|
619
|
+
Object.freeze(this);
|
|
620
|
+
}
|
|
621
|
+
create(num) {
|
|
622
|
+
return mod(num, this.ORDER);
|
|
623
|
+
}
|
|
624
|
+
isValid(num) {
|
|
625
|
+
if (typeof num !== "bigint")
|
|
626
|
+
throw new TypeError("invalid field element: expected bigint, got " + typeof num);
|
|
627
|
+
return _0n2 <= num && num < this.ORDER;
|
|
628
|
+
}
|
|
629
|
+
is0(num) {
|
|
630
|
+
return num === _0n2;
|
|
631
|
+
}
|
|
632
|
+
// is valid and invertible
|
|
633
|
+
isValidNot0(num) {
|
|
634
|
+
return !this.is0(num) && this.isValid(num);
|
|
635
|
+
}
|
|
636
|
+
isOdd(num) {
|
|
637
|
+
return (num & _1n2) === _1n2;
|
|
638
|
+
}
|
|
639
|
+
neg(num) {
|
|
640
|
+
return mod(-num, this.ORDER);
|
|
641
|
+
}
|
|
642
|
+
eql(lhs, rhs) {
|
|
643
|
+
return lhs === rhs;
|
|
644
|
+
}
|
|
645
|
+
sqr(num) {
|
|
646
|
+
return mod(num * num, this.ORDER);
|
|
647
|
+
}
|
|
648
|
+
add(lhs, rhs) {
|
|
649
|
+
return mod(lhs + rhs, this.ORDER);
|
|
650
|
+
}
|
|
651
|
+
sub(lhs, rhs) {
|
|
652
|
+
return mod(lhs - rhs, this.ORDER);
|
|
653
|
+
}
|
|
654
|
+
mul(lhs, rhs) {
|
|
655
|
+
return mod(lhs * rhs, this.ORDER);
|
|
656
|
+
}
|
|
657
|
+
pow(num, power) {
|
|
658
|
+
return FpPow(this, num, power);
|
|
659
|
+
}
|
|
660
|
+
div(lhs, rhs) {
|
|
661
|
+
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
662
|
+
}
|
|
663
|
+
// Same as above, but doesn't normalize
|
|
664
|
+
sqrN(num) {
|
|
665
|
+
return num * num;
|
|
666
|
+
}
|
|
667
|
+
addN(lhs, rhs) {
|
|
668
|
+
return lhs + rhs;
|
|
669
|
+
}
|
|
670
|
+
subN(lhs, rhs) {
|
|
671
|
+
return lhs - rhs;
|
|
672
|
+
}
|
|
673
|
+
mulN(lhs, rhs) {
|
|
674
|
+
return lhs * rhs;
|
|
675
|
+
}
|
|
676
|
+
inv(num) {
|
|
677
|
+
return invert(num, this.ORDER);
|
|
678
|
+
}
|
|
679
|
+
sqrt(num) {
|
|
680
|
+
let sqrt = FIELD_SQRT.get(this);
|
|
681
|
+
if (!sqrt)
|
|
682
|
+
FIELD_SQRT.set(this, sqrt = FpSqrt(this.ORDER));
|
|
683
|
+
return sqrt(this, num);
|
|
684
|
+
}
|
|
685
|
+
toBytes(num) {
|
|
686
|
+
return this.isLE ? numberToBytesLE(num, this.BYTES) : numberToBytesBE(num, this.BYTES);
|
|
687
|
+
}
|
|
688
|
+
fromBytes(bytes, skipValidation = false) {
|
|
689
|
+
abytes2(bytes);
|
|
690
|
+
const { _lengths: allowedLengths, BYTES, isLE: isLE2, ORDER, _mod: modFromBytes } = this;
|
|
691
|
+
if (allowedLengths) {
|
|
692
|
+
if (bytes.length < 1 || !allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
693
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
694
|
+
}
|
|
695
|
+
const padded = new Uint8Array(BYTES);
|
|
696
|
+
padded.set(bytes, isLE2 ? 0 : padded.length - bytes.length);
|
|
697
|
+
bytes = padded;
|
|
698
|
+
}
|
|
699
|
+
if (bytes.length !== BYTES)
|
|
700
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
701
|
+
let scalar = isLE2 ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
702
|
+
if (modFromBytes)
|
|
703
|
+
scalar = mod(scalar, ORDER);
|
|
704
|
+
if (!skipValidation) {
|
|
705
|
+
if (!this.isValid(scalar))
|
|
706
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
707
|
+
}
|
|
708
|
+
return scalar;
|
|
709
|
+
}
|
|
710
|
+
// TODO: we don't need it here, move out to separate fn
|
|
711
|
+
invertBatch(lst) {
|
|
712
|
+
return FpInvertBatch(this, lst);
|
|
713
|
+
}
|
|
714
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
715
|
+
// and it's unclear what to return in there.
|
|
716
|
+
cmov(a, b, condition) {
|
|
717
|
+
abool(condition, "condition");
|
|
718
|
+
return condition ? b : a;
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
Object.freeze(_Field.prototype);
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
// ../../../noble-install/node_modules/@noble/curves/abstract/curve.js
|
|
726
|
+
function negateCt(condition, item) {
|
|
727
|
+
const neg = item.negate();
|
|
728
|
+
return condition ? neg : item;
|
|
729
|
+
}
|
|
730
|
+
function normalizeZ(c, points) {
|
|
731
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
732
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
733
|
+
}
|
|
734
|
+
function validateW(W, bits) {
|
|
735
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
736
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
737
|
+
}
|
|
738
|
+
function calcWOpts(W, scalarBits) {
|
|
739
|
+
validateW(W, scalarBits);
|
|
740
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
741
|
+
const windowSize = 2 ** (W - 1);
|
|
742
|
+
const maxNumber = 2 ** W;
|
|
743
|
+
const mask = bitMask(W);
|
|
744
|
+
const shiftBy = BigInt(W);
|
|
745
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
746
|
+
}
|
|
747
|
+
function calcOffsets(n, window, wOpts) {
|
|
748
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
749
|
+
let wbits = Number(n & mask);
|
|
750
|
+
let nextN = n >> shiftBy;
|
|
751
|
+
if (wbits > windowSize) {
|
|
752
|
+
wbits -= maxNumber;
|
|
753
|
+
nextN += _1n3;
|
|
754
|
+
}
|
|
755
|
+
const offsetStart = window * windowSize;
|
|
756
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
757
|
+
const isZero = wbits === 0;
|
|
758
|
+
const isNeg = wbits < 0;
|
|
759
|
+
const isNegF = window % 2 !== 0;
|
|
760
|
+
const offsetF = offsetStart;
|
|
761
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
762
|
+
}
|
|
763
|
+
function getW(P) {
|
|
764
|
+
return pointWindowSizes.get(P) || 1;
|
|
765
|
+
}
|
|
766
|
+
function assert0(n) {
|
|
767
|
+
if (n !== _0n3)
|
|
768
|
+
throw new Error("invalid wNAF");
|
|
769
|
+
}
|
|
770
|
+
function createField(order, field, isLE2) {
|
|
771
|
+
if (field) {
|
|
772
|
+
if (field.ORDER !== order)
|
|
773
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
774
|
+
validateField(field);
|
|
775
|
+
return field;
|
|
776
|
+
} else {
|
|
777
|
+
return Field(order, { isLE: isLE2 });
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
781
|
+
if (FpFnLE === void 0)
|
|
782
|
+
FpFnLE = type === "edwards";
|
|
783
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
784
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
785
|
+
for (const p of ["p", "n", "h"]) {
|
|
786
|
+
const val = CURVE[p];
|
|
787
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
788
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
789
|
+
}
|
|
790
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
791
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
792
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
793
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
794
|
+
for (const p of params) {
|
|
795
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
796
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
797
|
+
}
|
|
798
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
799
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
800
|
+
}
|
|
801
|
+
function createKeygen(randomSecretKey, getPublicKey) {
|
|
802
|
+
return function keygen(seed) {
|
|
803
|
+
const secretKey = randomSecretKey(seed);
|
|
804
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
var _0n3, _1n3, pointPrecomputes, pointWindowSizes, wNAF;
|
|
808
|
+
var init_curve = __esm({
|
|
809
|
+
"../../../noble-install/node_modules/@noble/curves/abstract/curve.js"() {
|
|
810
|
+
"use strict";
|
|
811
|
+
init_utils2();
|
|
812
|
+
init_modular();
|
|
813
|
+
_0n3 = /* @__PURE__ */ BigInt(0);
|
|
814
|
+
_1n3 = /* @__PURE__ */ BigInt(1);
|
|
815
|
+
pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
816
|
+
pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
817
|
+
wNAF = class {
|
|
818
|
+
// Parametrized with a given Point class (not individual point)
|
|
819
|
+
constructor(Point, bits) {
|
|
820
|
+
__publicField(this, "BASE");
|
|
821
|
+
__publicField(this, "ZERO");
|
|
822
|
+
__publicField(this, "Fn");
|
|
823
|
+
__publicField(this, "bits");
|
|
824
|
+
this.BASE = Point.BASE;
|
|
825
|
+
this.ZERO = Point.ZERO;
|
|
826
|
+
this.Fn = Point.Fn;
|
|
827
|
+
this.bits = bits;
|
|
828
|
+
}
|
|
829
|
+
// non-const time multiplication ladder
|
|
830
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
831
|
+
let d = elm;
|
|
832
|
+
while (n > _0n3) {
|
|
833
|
+
if (n & _1n3)
|
|
834
|
+
p = p.add(d);
|
|
835
|
+
d = d.double();
|
|
836
|
+
n >>= _1n3;
|
|
837
|
+
}
|
|
838
|
+
return p;
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
842
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
843
|
+
* Number of precomputed points depends on the curve size:
|
|
844
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
845
|
+
* - 𝑊 is the window size
|
|
846
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
847
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
848
|
+
* @param point - Point instance
|
|
849
|
+
* @param W - window size
|
|
850
|
+
* @returns precomputed point tables flattened to a single array
|
|
851
|
+
*/
|
|
852
|
+
precomputeWindow(point, W) {
|
|
853
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
854
|
+
const points = [];
|
|
855
|
+
let p = point;
|
|
856
|
+
let base = p;
|
|
857
|
+
for (let window = 0; window < windows; window++) {
|
|
858
|
+
base = p;
|
|
859
|
+
points.push(base);
|
|
860
|
+
for (let i = 1; i < windowSize; i++) {
|
|
861
|
+
base = base.add(p);
|
|
862
|
+
points.push(base);
|
|
863
|
+
}
|
|
864
|
+
p = base.double();
|
|
865
|
+
}
|
|
866
|
+
return points;
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
870
|
+
* More compact implementation:
|
|
871
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
872
|
+
* @returns real and fake (for const-time) points
|
|
873
|
+
*/
|
|
874
|
+
wNAF(W, precomputes, n) {
|
|
875
|
+
if (!this.Fn.isValid(n))
|
|
876
|
+
throw new Error("invalid scalar");
|
|
877
|
+
let p = this.ZERO;
|
|
878
|
+
let f = this.BASE;
|
|
879
|
+
const wo = calcWOpts(W, this.bits);
|
|
880
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
881
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
882
|
+
n = nextN;
|
|
883
|
+
if (isZero) {
|
|
884
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
885
|
+
} else {
|
|
886
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
assert0(n);
|
|
890
|
+
return { p, f };
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Implements unsafe EC multiplication using precomputed tables
|
|
894
|
+
* and w-ary non-adjacent form.
|
|
895
|
+
* @param acc - accumulator point to add result of multiplication
|
|
896
|
+
* @returns point
|
|
897
|
+
*/
|
|
898
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
899
|
+
const wo = calcWOpts(W, this.bits);
|
|
900
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
901
|
+
if (n === _0n3)
|
|
902
|
+
break;
|
|
903
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
904
|
+
n = nextN;
|
|
905
|
+
if (isZero) {
|
|
906
|
+
continue;
|
|
907
|
+
} else {
|
|
908
|
+
const item = precomputes[offset];
|
|
909
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
assert0(n);
|
|
913
|
+
return acc;
|
|
914
|
+
}
|
|
915
|
+
getPrecomputes(W, point, transform) {
|
|
916
|
+
let comp = pointPrecomputes.get(point);
|
|
917
|
+
if (!comp) {
|
|
918
|
+
comp = this.precomputeWindow(point, W);
|
|
919
|
+
if (W !== 1) {
|
|
920
|
+
if (typeof transform === "function")
|
|
921
|
+
comp = transform(comp);
|
|
922
|
+
pointPrecomputes.set(point, comp);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return comp;
|
|
926
|
+
}
|
|
927
|
+
cached(point, scalar, transform) {
|
|
928
|
+
const W = getW(point);
|
|
929
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
930
|
+
}
|
|
931
|
+
unsafe(point, scalar, transform, prev) {
|
|
932
|
+
const W = getW(point);
|
|
933
|
+
if (W === 1)
|
|
934
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
935
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
936
|
+
}
|
|
937
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
938
|
+
// using windowed method. This specifies window size and
|
|
939
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
940
|
+
createCache(P, W) {
|
|
941
|
+
validateW(W, this.bits);
|
|
942
|
+
pointWindowSizes.set(P, W);
|
|
943
|
+
pointPrecomputes.delete(P);
|
|
944
|
+
}
|
|
945
|
+
hasCache(elm) {
|
|
946
|
+
return getW(elm) !== 1;
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
});
|
|
951
|
+
|
|
952
|
+
// ../../../noble-install/node_modules/@noble/curves/abstract/edwards.js
|
|
953
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
954
|
+
const x2 = Fp2.sqr(x);
|
|
955
|
+
const y2 = Fp2.sqr(y);
|
|
956
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
957
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
958
|
+
return Fp2.eql(left, right);
|
|
959
|
+
}
|
|
960
|
+
function edwards(params, extraOpts = {}) {
|
|
961
|
+
const opts2 = extraOpts;
|
|
962
|
+
const validated = createCurveFields("edwards", params, opts2, opts2.FpFnLE);
|
|
963
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
964
|
+
let CURVE = validated.CURVE;
|
|
965
|
+
const { h: cofactor } = CURVE;
|
|
966
|
+
validateObject(opts2, {}, { uvRatio: "function" });
|
|
967
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
968
|
+
const modP = (n) => Fp2.create(n);
|
|
969
|
+
const uvRatio2 = opts2.uvRatio === void 0 ? (u, v) => {
|
|
970
|
+
try {
|
|
971
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
972
|
+
} catch (e) {
|
|
973
|
+
return { isValid: false, value: _0n4 };
|
|
974
|
+
}
|
|
975
|
+
} : opts2.uvRatio;
|
|
976
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
977
|
+
throw new Error("bad curve params: generator point");
|
|
978
|
+
function acoord(title, n, banZero = false) {
|
|
979
|
+
const min = banZero ? _1n4 : _0n4;
|
|
980
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
981
|
+
return n;
|
|
982
|
+
}
|
|
983
|
+
function aedpoint(other) {
|
|
984
|
+
if (!(other instanceof Point))
|
|
985
|
+
throw new Error("EdwardsPoint expected");
|
|
986
|
+
}
|
|
987
|
+
const _Point = class _Point {
|
|
988
|
+
constructor(X, Y, Z, T) {
|
|
989
|
+
__publicField(this, "X");
|
|
990
|
+
__publicField(this, "Y");
|
|
991
|
+
__publicField(this, "Z");
|
|
992
|
+
__publicField(this, "T");
|
|
993
|
+
this.X = acoord("x", X);
|
|
994
|
+
this.Y = acoord("y", Y);
|
|
995
|
+
this.Z = acoord("z", Z, true);
|
|
996
|
+
this.T = acoord("t", T);
|
|
997
|
+
Object.freeze(this);
|
|
998
|
+
}
|
|
999
|
+
static CURVE() {
|
|
1000
|
+
return CURVE;
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Create one extended Edwards point from affine coordinates.
|
|
1004
|
+
* Does NOT validate that the point is on-curve or torsion-free.
|
|
1005
|
+
* Use `.assertValidity()` on adversarial inputs.
|
|
1006
|
+
*/
|
|
1007
|
+
static fromAffine(p) {
|
|
1008
|
+
if (p instanceof _Point)
|
|
1009
|
+
throw new Error("extended point not allowed");
|
|
1010
|
+
const { x, y } = p || {};
|
|
1011
|
+
acoord("x", x);
|
|
1012
|
+
acoord("y", y);
|
|
1013
|
+
return new _Point(x, y, _1n4, modP(x * y));
|
|
1014
|
+
}
|
|
1015
|
+
// Uses algo from RFC8032 5.1.3.
|
|
1016
|
+
static fromBytes(bytes, zip215 = false) {
|
|
1017
|
+
const len = Fp2.BYTES;
|
|
1018
|
+
const { a, d } = CURVE;
|
|
1019
|
+
bytes = copyBytes(abytes2(bytes, len, "point"));
|
|
1020
|
+
abool(zip215, "zip215");
|
|
1021
|
+
const normed = copyBytes(bytes);
|
|
1022
|
+
const lastByte = bytes[len - 1];
|
|
1023
|
+
normed[len - 1] = lastByte & ~128;
|
|
1024
|
+
const y = bytesToNumberLE(normed);
|
|
1025
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
1026
|
+
aInRange("point.y", y, _0n4, max);
|
|
1027
|
+
const y2 = modP(y * y);
|
|
1028
|
+
const u = modP(y2 - _1n4);
|
|
1029
|
+
const v = modP(d * y2 - a);
|
|
1030
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
1031
|
+
if (!isValid)
|
|
1032
|
+
throw new Error("bad point: invalid y coordinate");
|
|
1033
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
1034
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
1035
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
1036
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
1037
|
+
if (isLastByteOdd !== isXOdd)
|
|
1038
|
+
x = modP(-x);
|
|
1039
|
+
return _Point.fromAffine({ x, y });
|
|
1040
|
+
}
|
|
1041
|
+
static fromHex(hex, zip215 = false) {
|
|
1042
|
+
return _Point.fromBytes(hexToBytes2(hex), zip215);
|
|
1043
|
+
}
|
|
1044
|
+
get x() {
|
|
1045
|
+
return this.toAffine().x;
|
|
1046
|
+
}
|
|
1047
|
+
get y() {
|
|
1048
|
+
return this.toAffine().y;
|
|
1049
|
+
}
|
|
1050
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
1051
|
+
wnaf.createCache(this, windowSize);
|
|
1052
|
+
if (!isLazy)
|
|
1053
|
+
this.multiply(_2n2);
|
|
1054
|
+
return this;
|
|
1055
|
+
}
|
|
1056
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
1057
|
+
assertValidity() {
|
|
1058
|
+
const p = this;
|
|
1059
|
+
const { a, d } = CURVE;
|
|
1060
|
+
if (p.is0())
|
|
1061
|
+
throw new Error("bad point: ZERO");
|
|
1062
|
+
const { X, Y, Z, T } = p;
|
|
1063
|
+
const X2 = modP(X * X);
|
|
1064
|
+
const Y2 = modP(Y * Y);
|
|
1065
|
+
const Z2 = modP(Z * Z);
|
|
1066
|
+
const Z4 = modP(Z2 * Z2);
|
|
1067
|
+
const aX2 = modP(X2 * a);
|
|
1068
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
1069
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
1070
|
+
if (left !== right)
|
|
1071
|
+
throw new Error("bad point: equation left != right (1)");
|
|
1072
|
+
const XY = modP(X * Y);
|
|
1073
|
+
const ZT = modP(Z * T);
|
|
1074
|
+
if (XY !== ZT)
|
|
1075
|
+
throw new Error("bad point: equation left != right (2)");
|
|
1076
|
+
}
|
|
1077
|
+
// Compare one point to another.
|
|
1078
|
+
equals(other) {
|
|
1079
|
+
aedpoint(other);
|
|
1080
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1081
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
1082
|
+
const X1Z2 = modP(X1 * Z2);
|
|
1083
|
+
const X2Z1 = modP(X2 * Z1);
|
|
1084
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
1085
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
1086
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
1087
|
+
}
|
|
1088
|
+
is0() {
|
|
1089
|
+
return this.equals(_Point.ZERO);
|
|
1090
|
+
}
|
|
1091
|
+
negate() {
|
|
1092
|
+
return new _Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
1093
|
+
}
|
|
1094
|
+
// Fast algo for doubling Extended Point.
|
|
1095
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
1096
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
1097
|
+
double() {
|
|
1098
|
+
const { a } = CURVE;
|
|
1099
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1100
|
+
const A = modP(X1 * X1);
|
|
1101
|
+
const B = modP(Y1 * Y1);
|
|
1102
|
+
const C = modP(_2n2 * modP(Z1 * Z1));
|
|
1103
|
+
const D = modP(a * A);
|
|
1104
|
+
const x1y1 = X1 + Y1;
|
|
1105
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
1106
|
+
const G = D + B;
|
|
1107
|
+
const F2 = G - C;
|
|
1108
|
+
const H = D - B;
|
|
1109
|
+
const X3 = modP(E * F2);
|
|
1110
|
+
const Y3 = modP(G * H);
|
|
1111
|
+
const T3 = modP(E * H);
|
|
1112
|
+
const Z3 = modP(F2 * G);
|
|
1113
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
1114
|
+
}
|
|
1115
|
+
// Fast algo for adding 2 Extended Points.
|
|
1116
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
1117
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
1118
|
+
add(other) {
|
|
1119
|
+
aedpoint(other);
|
|
1120
|
+
const { a, d } = CURVE;
|
|
1121
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
1122
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
1123
|
+
const A = modP(X1 * X2);
|
|
1124
|
+
const B = modP(Y1 * Y2);
|
|
1125
|
+
const C = modP(T1 * d * T2);
|
|
1126
|
+
const D = modP(Z1 * Z2);
|
|
1127
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
1128
|
+
const F2 = D - C;
|
|
1129
|
+
const G = D + C;
|
|
1130
|
+
const H = modP(B - a * A);
|
|
1131
|
+
const X3 = modP(E * F2);
|
|
1132
|
+
const Y3 = modP(G * H);
|
|
1133
|
+
const T3 = modP(E * H);
|
|
1134
|
+
const Z3 = modP(F2 * G);
|
|
1135
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
1136
|
+
}
|
|
1137
|
+
subtract(other) {
|
|
1138
|
+
aedpoint(other);
|
|
1139
|
+
return this.add(other.negate());
|
|
1140
|
+
}
|
|
1141
|
+
// Constant-time multiplication.
|
|
1142
|
+
multiply(scalar) {
|
|
1143
|
+
if (!Fn2.isValidNot0(scalar))
|
|
1144
|
+
throw new RangeError("invalid scalar: expected 1 <= sc < curve.n");
|
|
1145
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(_Point, p2));
|
|
1146
|
+
return normalizeZ(_Point, [p, f])[0];
|
|
1147
|
+
}
|
|
1148
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1149
|
+
// It's faster, but should only be used when you don't care about
|
|
1150
|
+
// an exposed private key e.g. sig verification.
|
|
1151
|
+
// Keeps the same subgroup-scalar contract: 0 is allowed for public-scalar callers, but
|
|
1152
|
+
// n and larger values are rejected instead of being reduced mod n to the identity point.
|
|
1153
|
+
multiplyUnsafe(scalar) {
|
|
1154
|
+
if (!Fn2.isValid(scalar))
|
|
1155
|
+
throw new RangeError("invalid scalar: expected 0 <= sc < curve.n");
|
|
1156
|
+
if (scalar === _0n4)
|
|
1157
|
+
return _Point.ZERO;
|
|
1158
|
+
if (this.is0() || scalar === _1n4)
|
|
1159
|
+
return this;
|
|
1160
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(_Point, p));
|
|
1161
|
+
}
|
|
1162
|
+
// Checks if point is of small order.
|
|
1163
|
+
// If you add something to small order point, you will have "dirty"
|
|
1164
|
+
// point with torsion component.
|
|
1165
|
+
// Clears cofactor and checks if the result is 0.
|
|
1166
|
+
isSmallOrder() {
|
|
1167
|
+
return this.clearCofactor().is0();
|
|
1168
|
+
}
|
|
1169
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
1170
|
+
// Returns `false` is the point is dirty.
|
|
1171
|
+
isTorsionFree() {
|
|
1172
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
1173
|
+
}
|
|
1174
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
1175
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
1176
|
+
toAffine(invertedZ) {
|
|
1177
|
+
const p = this;
|
|
1178
|
+
let iz = invertedZ;
|
|
1179
|
+
const { X, Y, Z } = p;
|
|
1180
|
+
const is0 = p.is0();
|
|
1181
|
+
if (iz == null)
|
|
1182
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
1183
|
+
const x = modP(X * iz);
|
|
1184
|
+
const y = modP(Y * iz);
|
|
1185
|
+
const zz = Fp2.mul(Z, iz);
|
|
1186
|
+
if (is0)
|
|
1187
|
+
return { x: _0n4, y: _1n4 };
|
|
1188
|
+
if (zz !== _1n4)
|
|
1189
|
+
throw new Error("invZ was invalid");
|
|
1190
|
+
return { x, y };
|
|
1191
|
+
}
|
|
1192
|
+
clearCofactor() {
|
|
1193
|
+
if (cofactor === _1n4)
|
|
1194
|
+
return this;
|
|
1195
|
+
return this.multiplyUnsafe(cofactor);
|
|
1196
|
+
}
|
|
1197
|
+
toBytes() {
|
|
1198
|
+
const { x, y } = this.toAffine();
|
|
1199
|
+
const bytes = Fp2.toBytes(y);
|
|
1200
|
+
bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
|
|
1201
|
+
return bytes;
|
|
1202
|
+
}
|
|
1203
|
+
toHex() {
|
|
1204
|
+
return bytesToHex2(this.toBytes());
|
|
1205
|
+
}
|
|
1206
|
+
toString() {
|
|
1207
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
// base / generator point
|
|
1211
|
+
__publicField(_Point, "BASE", new _Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy)));
|
|
1212
|
+
// zero / infinity / identity point
|
|
1213
|
+
__publicField(_Point, "ZERO", new _Point(_0n4, _1n4, _1n4, _0n4));
|
|
1214
|
+
// 0, 1, 1, 0
|
|
1215
|
+
// math field
|
|
1216
|
+
__publicField(_Point, "Fp", Fp2);
|
|
1217
|
+
// scalar field
|
|
1218
|
+
__publicField(_Point, "Fn", Fn2);
|
|
1219
|
+
let Point = _Point;
|
|
1220
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
1221
|
+
if (Fn2.BITS >= 8)
|
|
1222
|
+
Point.BASE.precompute(8);
|
|
1223
|
+
Object.freeze(Point.prototype);
|
|
1224
|
+
Object.freeze(Point);
|
|
1225
|
+
return Point;
|
|
1226
|
+
}
|
|
1227
|
+
var _0n4, _1n4, _2n2, _8n2, PrimeEdwardsPoint;
|
|
1228
|
+
var init_edwards = __esm({
|
|
1229
|
+
"../../../noble-install/node_modules/@noble/curves/abstract/edwards.js"() {
|
|
1230
|
+
"use strict";
|
|
1231
|
+
init_utils2();
|
|
1232
|
+
init_curve();
|
|
1233
|
+
_0n4 = /* @__PURE__ */ BigInt(0);
|
|
1234
|
+
_1n4 = /* @__PURE__ */ BigInt(1);
|
|
1235
|
+
_2n2 = /* @__PURE__ */ BigInt(2);
|
|
1236
|
+
_8n2 = /* @__PURE__ */ BigInt(8);
|
|
1237
|
+
PrimeEdwardsPoint = class {
|
|
1238
|
+
/**
|
|
1239
|
+
* Wrap one internal Edwards representative directly.
|
|
1240
|
+
* This is not a canonical encoding boundary: alternate Edwards
|
|
1241
|
+
* representatives may still describe the same abstract wrapper element.
|
|
1242
|
+
*/
|
|
1243
|
+
constructor(ep) {
|
|
1244
|
+
__publicField(this, "ep");
|
|
1245
|
+
this.ep = ep;
|
|
1246
|
+
}
|
|
1247
|
+
// Static methods that must be implemented by subclasses
|
|
1248
|
+
static fromBytes(_bytes) {
|
|
1249
|
+
notImplemented();
|
|
1250
|
+
}
|
|
1251
|
+
static fromHex(_hex) {
|
|
1252
|
+
notImplemented();
|
|
1253
|
+
}
|
|
1254
|
+
get x() {
|
|
1255
|
+
return this.toAffine().x;
|
|
1256
|
+
}
|
|
1257
|
+
get y() {
|
|
1258
|
+
return this.toAffine().y;
|
|
1259
|
+
}
|
|
1260
|
+
// Common implementations
|
|
1261
|
+
clearCofactor() {
|
|
1262
|
+
return this;
|
|
1263
|
+
}
|
|
1264
|
+
assertValidity() {
|
|
1265
|
+
this.ep.assertValidity();
|
|
1266
|
+
}
|
|
1267
|
+
/**
|
|
1268
|
+
* Return affine coordinates of the current internal Edwards representative.
|
|
1269
|
+
* This is a convenience helper, not a canonical Ristretto/Decaf encoding.
|
|
1270
|
+
* Equal abstract elements may expose different `x` / `y`; use
|
|
1271
|
+
* `toBytes()` / `fromBytes()` for canonical roundtrips.
|
|
1272
|
+
*/
|
|
1273
|
+
toAffine(invertedZ) {
|
|
1274
|
+
return this.ep.toAffine(invertedZ);
|
|
1275
|
+
}
|
|
1276
|
+
toHex() {
|
|
1277
|
+
return bytesToHex2(this.toBytes());
|
|
1278
|
+
}
|
|
1279
|
+
toString() {
|
|
1280
|
+
return this.toHex();
|
|
1281
|
+
}
|
|
1282
|
+
isTorsionFree() {
|
|
1283
|
+
return true;
|
|
1284
|
+
}
|
|
1285
|
+
isSmallOrder() {
|
|
1286
|
+
return false;
|
|
1287
|
+
}
|
|
1288
|
+
add(other) {
|
|
1289
|
+
this.assertSame(other);
|
|
1290
|
+
return this.init(this.ep.add(other.ep));
|
|
1291
|
+
}
|
|
1292
|
+
subtract(other) {
|
|
1293
|
+
this.assertSame(other);
|
|
1294
|
+
return this.init(this.ep.subtract(other.ep));
|
|
1295
|
+
}
|
|
1296
|
+
multiply(scalar) {
|
|
1297
|
+
return this.init(this.ep.multiply(scalar));
|
|
1298
|
+
}
|
|
1299
|
+
multiplyUnsafe(scalar) {
|
|
1300
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
1301
|
+
}
|
|
1302
|
+
double() {
|
|
1303
|
+
return this.init(this.ep.double());
|
|
1304
|
+
}
|
|
1305
|
+
negate() {
|
|
1306
|
+
return this.init(this.ep.negate());
|
|
1307
|
+
}
|
|
1308
|
+
precompute(windowSize, isLazy) {
|
|
1309
|
+
this.ep.precompute(windowSize, isLazy);
|
|
1310
|
+
return this;
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
__publicField(PrimeEdwardsPoint, "BASE");
|
|
1314
|
+
__publicField(PrimeEdwardsPoint, "ZERO");
|
|
1315
|
+
__publicField(PrimeEdwardsPoint, "Fp");
|
|
1316
|
+
__publicField(PrimeEdwardsPoint, "Fn");
|
|
1317
|
+
}
|
|
1318
|
+
});
|
|
1319
|
+
|
|
1320
|
+
// ../../../noble-install/node_modules/@noble/curves/abstract/montgomery.js
|
|
1321
|
+
function validateOpts(curve) {
|
|
1322
|
+
validateObject(curve, {
|
|
1323
|
+
P: "bigint",
|
|
1324
|
+
type: "string",
|
|
1325
|
+
adjustScalarBytes: "function",
|
|
1326
|
+
powPminus2: "function"
|
|
1327
|
+
}, {
|
|
1328
|
+
randomBytes: "function"
|
|
1329
|
+
});
|
|
1330
|
+
return Object.freeze({ ...curve });
|
|
1331
|
+
}
|
|
1332
|
+
function montgomery(curveDef) {
|
|
1333
|
+
const CURVE = validateOpts(curveDef);
|
|
1334
|
+
const { P, type, adjustScalarBytes: adjustScalarBytes2, powPminus2, randomBytes: rand } = CURVE;
|
|
1335
|
+
const is25519 = type === "x25519";
|
|
1336
|
+
if (!is25519 && type !== "x448")
|
|
1337
|
+
throw new Error("invalid type");
|
|
1338
|
+
const randomBytes_ = rand === void 0 ? randomBytes2 : rand;
|
|
1339
|
+
const montgomeryBits = is25519 ? 255 : 448;
|
|
1340
|
+
const fieldLen = is25519 ? 32 : 56;
|
|
1341
|
+
const Gu = is25519 ? BigInt(9) : BigInt(5);
|
|
1342
|
+
const a24 = is25519 ? BigInt(121665) : BigInt(39081);
|
|
1343
|
+
const minScalar = is25519 ? _2n3 ** BigInt(254) : _2n3 ** BigInt(447);
|
|
1344
|
+
const maxAdded = is25519 ? BigInt(8) * _2n3 ** BigInt(251) - _1n5 : BigInt(4) * _2n3 ** BigInt(445) - _1n5;
|
|
1345
|
+
const maxScalar = minScalar + maxAdded + _1n5;
|
|
1346
|
+
const modP = (n) => mod(n, P);
|
|
1347
|
+
const GuBytes = encodeU(Gu);
|
|
1348
|
+
function encodeU(u) {
|
|
1349
|
+
return numberToBytesLE(modP(u), fieldLen);
|
|
1350
|
+
}
|
|
1351
|
+
function decodeU(u) {
|
|
1352
|
+
const _u = copyBytes(abytes2(u, fieldLen, "uCoordinate"));
|
|
1353
|
+
if (is25519)
|
|
1354
|
+
_u[31] &= 127;
|
|
1355
|
+
return modP(bytesToNumberLE(_u));
|
|
1356
|
+
}
|
|
1357
|
+
function decodeScalar(scalar) {
|
|
1358
|
+
return bytesToNumberLE(adjustScalarBytes2(copyBytes(abytes2(scalar, fieldLen, "scalar"))));
|
|
1359
|
+
}
|
|
1360
|
+
function scalarMult(scalar, u) {
|
|
1361
|
+
const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));
|
|
1362
|
+
if (pu === _0n5)
|
|
1363
|
+
throw new Error("invalid private or public key received");
|
|
1364
|
+
return encodeU(pu);
|
|
1365
|
+
}
|
|
1366
|
+
function scalarMultBase(scalar) {
|
|
1367
|
+
return scalarMult(scalar, GuBytes);
|
|
1368
|
+
}
|
|
1369
|
+
const getPublicKey = scalarMultBase;
|
|
1370
|
+
const getSharedSecret = scalarMult;
|
|
1371
|
+
function cswap(swap, x_2, x_3) {
|
|
1372
|
+
const dummy = modP(swap * (x_2 - x_3));
|
|
1373
|
+
x_2 = modP(x_2 - dummy);
|
|
1374
|
+
x_3 = modP(x_3 + dummy);
|
|
1375
|
+
return { x_2, x_3 };
|
|
1376
|
+
}
|
|
1377
|
+
function montgomeryLadder(u, scalar) {
|
|
1378
|
+
aInRange("u", u, _0n5, P);
|
|
1379
|
+
aInRange("scalar", scalar, minScalar, maxScalar);
|
|
1380
|
+
const k = scalar;
|
|
1381
|
+
const x_1 = u;
|
|
1382
|
+
let x_2 = _1n5;
|
|
1383
|
+
let z_2 = _0n5;
|
|
1384
|
+
let x_3 = u;
|
|
1385
|
+
let z_3 = _1n5;
|
|
1386
|
+
let swap = _0n5;
|
|
1387
|
+
for (let t = BigInt(montgomeryBits - 1); t >= _0n5; t--) {
|
|
1388
|
+
const k_t = k >> t & _1n5;
|
|
1389
|
+
swap ^= k_t;
|
|
1390
|
+
({ x_2, x_3 } = cswap(swap, x_2, x_3));
|
|
1391
|
+
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
1392
|
+
swap = k_t;
|
|
1393
|
+
const A = x_2 + z_2;
|
|
1394
|
+
const AA = modP(A * A);
|
|
1395
|
+
const B = x_2 - z_2;
|
|
1396
|
+
const BB = modP(B * B);
|
|
1397
|
+
const E = AA - BB;
|
|
1398
|
+
const C = x_3 + z_3;
|
|
1399
|
+
const D = x_3 - z_3;
|
|
1400
|
+
const DA = modP(D * A);
|
|
1401
|
+
const CB = modP(C * B);
|
|
1402
|
+
const dacb = DA + CB;
|
|
1403
|
+
const da_cb = DA - CB;
|
|
1404
|
+
x_3 = modP(dacb * dacb);
|
|
1405
|
+
z_3 = modP(x_1 * modP(da_cb * da_cb));
|
|
1406
|
+
x_2 = modP(AA * BB);
|
|
1407
|
+
z_2 = modP(E * (AA + modP(a24 * E)));
|
|
1408
|
+
}
|
|
1409
|
+
({ x_2, x_3 } = cswap(swap, x_2, x_3));
|
|
1410
|
+
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
1411
|
+
const z2 = powPminus2(z_2);
|
|
1412
|
+
return modP(x_2 * z2);
|
|
1413
|
+
}
|
|
1414
|
+
const lengths = {
|
|
1415
|
+
secretKey: fieldLen,
|
|
1416
|
+
publicKey: fieldLen,
|
|
1417
|
+
seed: fieldLen
|
|
1418
|
+
};
|
|
1419
|
+
const randomSecretKey = (seed) => {
|
|
1420
|
+
seed = seed === void 0 ? randomBytes_(fieldLen) : seed;
|
|
1421
|
+
abytes2(seed, lengths.seed, "seed");
|
|
1422
|
+
return seed;
|
|
1423
|
+
};
|
|
1424
|
+
const utils = { randomSecretKey };
|
|
1425
|
+
Object.freeze(lengths);
|
|
1426
|
+
Object.freeze(utils);
|
|
1427
|
+
return Object.freeze({
|
|
1428
|
+
keygen: createKeygen(randomSecretKey, getPublicKey),
|
|
1429
|
+
getSharedSecret,
|
|
1430
|
+
getPublicKey,
|
|
1431
|
+
scalarMult,
|
|
1432
|
+
scalarMultBase,
|
|
1433
|
+
utils,
|
|
1434
|
+
GuBytes: GuBytes.slice(),
|
|
1435
|
+
lengths
|
|
1436
|
+
});
|
|
1437
|
+
}
|
|
1438
|
+
var _0n5, _1n5, _2n3;
|
|
1439
|
+
var init_montgomery = __esm({
|
|
1440
|
+
"../../../noble-install/node_modules/@noble/curves/abstract/montgomery.js"() {
|
|
1441
|
+
"use strict";
|
|
1442
|
+
init_utils2();
|
|
1443
|
+
init_curve();
|
|
1444
|
+
init_modular();
|
|
1445
|
+
_0n5 = BigInt(0);
|
|
1446
|
+
_1n5 = BigInt(1);
|
|
1447
|
+
_2n3 = BigInt(2);
|
|
1448
|
+
}
|
|
1449
|
+
});
|
|
1450
|
+
|
|
1451
|
+
// ../../../noble-install/node_modules/@noble/hashes/_md.js
|
|
1452
|
+
var HashMD, SHA512_IV;
|
|
1453
|
+
var init_md = __esm({
|
|
1454
|
+
"../../../noble-install/node_modules/@noble/hashes/_md.js"() {
|
|
1455
|
+
"use strict";
|
|
1456
|
+
init_utils();
|
|
1457
|
+
HashMD = class {
|
|
1458
|
+
constructor(blockLen, outputLen, padOffset, isLE2) {
|
|
1459
|
+
__publicField(this, "blockLen");
|
|
1460
|
+
__publicField(this, "outputLen");
|
|
1461
|
+
__publicField(this, "canXOF", false);
|
|
1462
|
+
__publicField(this, "padOffset");
|
|
1463
|
+
__publicField(this, "isLE");
|
|
1464
|
+
// For partial updates less than block size
|
|
1465
|
+
__publicField(this, "buffer");
|
|
1466
|
+
__publicField(this, "view");
|
|
1467
|
+
__publicField(this, "finished", false);
|
|
1468
|
+
__publicField(this, "length", 0);
|
|
1469
|
+
__publicField(this, "pos", 0);
|
|
1470
|
+
__publicField(this, "destroyed", false);
|
|
1471
|
+
this.blockLen = blockLen;
|
|
1472
|
+
this.outputLen = outputLen;
|
|
1473
|
+
this.padOffset = padOffset;
|
|
1474
|
+
this.isLE = isLE2;
|
|
1475
|
+
this.buffer = new Uint8Array(blockLen);
|
|
1476
|
+
this.view = createView(this.buffer);
|
|
1477
|
+
}
|
|
1478
|
+
update(data) {
|
|
1479
|
+
aexists(this);
|
|
1480
|
+
abytes(data);
|
|
1481
|
+
const { view, buffer, blockLen } = this;
|
|
1482
|
+
const len = data.length;
|
|
1483
|
+
for (let pos = 0; pos < len; ) {
|
|
1484
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
1485
|
+
if (take === blockLen) {
|
|
1486
|
+
const dataView = createView(data);
|
|
1487
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
1488
|
+
this.process(dataView, pos);
|
|
1489
|
+
continue;
|
|
1490
|
+
}
|
|
1491
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
1492
|
+
this.pos += take;
|
|
1493
|
+
pos += take;
|
|
1494
|
+
if (this.pos === blockLen) {
|
|
1495
|
+
this.process(view, 0);
|
|
1496
|
+
this.pos = 0;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
this.length += data.length;
|
|
1500
|
+
this.roundClean();
|
|
1501
|
+
return this;
|
|
1502
|
+
}
|
|
1503
|
+
digestInto(out) {
|
|
1504
|
+
aexists(this);
|
|
1505
|
+
aoutput(out, this);
|
|
1506
|
+
this.finished = true;
|
|
1507
|
+
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
|
1508
|
+
let { pos } = this;
|
|
1509
|
+
buffer[pos++] = 128;
|
|
1510
|
+
clean(this.buffer.subarray(pos));
|
|
1511
|
+
if (this.padOffset > blockLen - pos) {
|
|
1512
|
+
this.process(view, 0);
|
|
1513
|
+
pos = 0;
|
|
1514
|
+
}
|
|
1515
|
+
for (let i = pos; i < blockLen; i++)
|
|
1516
|
+
buffer[i] = 0;
|
|
1517
|
+
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE2);
|
|
1518
|
+
this.process(view, 0);
|
|
1519
|
+
const oview = createView(out);
|
|
1520
|
+
const len = this.outputLen;
|
|
1521
|
+
if (len % 4)
|
|
1522
|
+
throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
1523
|
+
const outLen = len / 4;
|
|
1524
|
+
const state = this.get();
|
|
1525
|
+
if (outLen > state.length)
|
|
1526
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
1527
|
+
for (let i = 0; i < outLen; i++)
|
|
1528
|
+
oview.setUint32(4 * i, state[i], isLE2);
|
|
1529
|
+
}
|
|
1530
|
+
digest() {
|
|
1531
|
+
const { buffer, outputLen } = this;
|
|
1532
|
+
this.digestInto(buffer);
|
|
1533
|
+
const res = buffer.slice(0, outputLen);
|
|
1534
|
+
this.destroy();
|
|
1535
|
+
return res;
|
|
1536
|
+
}
|
|
1537
|
+
_cloneInto(to) {
|
|
1538
|
+
to || (to = new this.constructor());
|
|
1539
|
+
to.set(...this.get());
|
|
1540
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
1541
|
+
to.destroyed = destroyed;
|
|
1542
|
+
to.finished = finished;
|
|
1543
|
+
to.length = length;
|
|
1544
|
+
to.pos = pos;
|
|
1545
|
+
if (length % blockLen)
|
|
1546
|
+
to.buffer.set(buffer);
|
|
1547
|
+
return to;
|
|
1548
|
+
}
|
|
1549
|
+
clone() {
|
|
1550
|
+
return this._cloneInto();
|
|
1551
|
+
}
|
|
1552
|
+
};
|
|
1553
|
+
SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
1554
|
+
1779033703,
|
|
1555
|
+
4089235720,
|
|
1556
|
+
3144134277,
|
|
1557
|
+
2227873595,
|
|
1558
|
+
1013904242,
|
|
1559
|
+
4271175723,
|
|
1560
|
+
2773480762,
|
|
1561
|
+
1595750129,
|
|
1562
|
+
1359893119,
|
|
1563
|
+
2917565137,
|
|
1564
|
+
2600822924,
|
|
1565
|
+
725511199,
|
|
1566
|
+
528734635,
|
|
1567
|
+
4215389547,
|
|
1568
|
+
1541459225,
|
|
1569
|
+
327033209
|
|
1570
|
+
]);
|
|
1571
|
+
}
|
|
1572
|
+
});
|
|
1573
|
+
|
|
1574
|
+
// ../../../noble-install/node_modules/@noble/hashes/_u64.js
|
|
1575
|
+
function fromBig(n, le = false) {
|
|
1576
|
+
if (le)
|
|
1577
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
1578
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
1579
|
+
}
|
|
1580
|
+
function split(lst, le = false) {
|
|
1581
|
+
const len = lst.length;
|
|
1582
|
+
let Ah = new Uint32Array(len);
|
|
1583
|
+
let Al = new Uint32Array(len);
|
|
1584
|
+
for (let i = 0; i < len; i++) {
|
|
1585
|
+
const { h, l } = fromBig(lst[i], le);
|
|
1586
|
+
[Ah[i], Al[i]] = [h, l];
|
|
1587
|
+
}
|
|
1588
|
+
return [Ah, Al];
|
|
1589
|
+
}
|
|
1590
|
+
function add(Ah, Al, Bh, Bl) {
|
|
1591
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1592
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
1593
|
+
}
|
|
1594
|
+
var U32_MASK64, _32n, shrSH, shrSL, rotrSH, rotrSL, rotrBH, rotrBL, rotlSH, rotlSL, rotlBH, rotlBL, add3L, add3H, add4L, add4H, add5L, add5H;
|
|
1595
|
+
var init_u64 = __esm({
|
|
1596
|
+
"../../../noble-install/node_modules/@noble/hashes/_u64.js"() {
|
|
1597
|
+
"use strict";
|
|
1598
|
+
U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
1599
|
+
_32n = /* @__PURE__ */ BigInt(32);
|
|
1600
|
+
shrSH = (h, _l, s) => h >>> s;
|
|
1601
|
+
shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1602
|
+
rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
1603
|
+
rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1604
|
+
rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
1605
|
+
rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
1606
|
+
rotlSH = (h, l, s) => h << s | l >>> 32 - s;
|
|
1607
|
+
rotlSL = (h, l, s) => l << s | h >>> 32 - s;
|
|
1608
|
+
rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
|
|
1609
|
+
rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
|
|
1610
|
+
add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1611
|
+
add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
1612
|
+
add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1613
|
+
add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
1614
|
+
add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1615
|
+
add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
1616
|
+
}
|
|
1617
|
+
});
|
|
1618
|
+
|
|
1619
|
+
// ../../../noble-install/node_modules/@noble/hashes/sha2.js
|
|
1620
|
+
var K512, SHA512_Kh, SHA512_Kl, SHA512_W_H, SHA512_W_L, SHA2_64B, _SHA512, sha512;
|
|
1621
|
+
var init_sha2 = __esm({
|
|
1622
|
+
"../../../noble-install/node_modules/@noble/hashes/sha2.js"() {
|
|
1623
|
+
"use strict";
|
|
1624
|
+
init_md();
|
|
1625
|
+
init_u64();
|
|
1626
|
+
init_utils();
|
|
1627
|
+
K512 = /* @__PURE__ */ (() => split([
|
|
1628
|
+
"0x428a2f98d728ae22",
|
|
1629
|
+
"0x7137449123ef65cd",
|
|
1630
|
+
"0xb5c0fbcfec4d3b2f",
|
|
1631
|
+
"0xe9b5dba58189dbbc",
|
|
1632
|
+
"0x3956c25bf348b538",
|
|
1633
|
+
"0x59f111f1b605d019",
|
|
1634
|
+
"0x923f82a4af194f9b",
|
|
1635
|
+
"0xab1c5ed5da6d8118",
|
|
1636
|
+
"0xd807aa98a3030242",
|
|
1637
|
+
"0x12835b0145706fbe",
|
|
1638
|
+
"0x243185be4ee4b28c",
|
|
1639
|
+
"0x550c7dc3d5ffb4e2",
|
|
1640
|
+
"0x72be5d74f27b896f",
|
|
1641
|
+
"0x80deb1fe3b1696b1",
|
|
1642
|
+
"0x9bdc06a725c71235",
|
|
1643
|
+
"0xc19bf174cf692694",
|
|
1644
|
+
"0xe49b69c19ef14ad2",
|
|
1645
|
+
"0xefbe4786384f25e3",
|
|
1646
|
+
"0x0fc19dc68b8cd5b5",
|
|
1647
|
+
"0x240ca1cc77ac9c65",
|
|
1648
|
+
"0x2de92c6f592b0275",
|
|
1649
|
+
"0x4a7484aa6ea6e483",
|
|
1650
|
+
"0x5cb0a9dcbd41fbd4",
|
|
1651
|
+
"0x76f988da831153b5",
|
|
1652
|
+
"0x983e5152ee66dfab",
|
|
1653
|
+
"0xa831c66d2db43210",
|
|
1654
|
+
"0xb00327c898fb213f",
|
|
1655
|
+
"0xbf597fc7beef0ee4",
|
|
1656
|
+
"0xc6e00bf33da88fc2",
|
|
1657
|
+
"0xd5a79147930aa725",
|
|
1658
|
+
"0x06ca6351e003826f",
|
|
1659
|
+
"0x142929670a0e6e70",
|
|
1660
|
+
"0x27b70a8546d22ffc",
|
|
1661
|
+
"0x2e1b21385c26c926",
|
|
1662
|
+
"0x4d2c6dfc5ac42aed",
|
|
1663
|
+
"0x53380d139d95b3df",
|
|
1664
|
+
"0x650a73548baf63de",
|
|
1665
|
+
"0x766a0abb3c77b2a8",
|
|
1666
|
+
"0x81c2c92e47edaee6",
|
|
1667
|
+
"0x92722c851482353b",
|
|
1668
|
+
"0xa2bfe8a14cf10364",
|
|
1669
|
+
"0xa81a664bbc423001",
|
|
1670
|
+
"0xc24b8b70d0f89791",
|
|
1671
|
+
"0xc76c51a30654be30",
|
|
1672
|
+
"0xd192e819d6ef5218",
|
|
1673
|
+
"0xd69906245565a910",
|
|
1674
|
+
"0xf40e35855771202a",
|
|
1675
|
+
"0x106aa07032bbd1b8",
|
|
1676
|
+
"0x19a4c116b8d2d0c8",
|
|
1677
|
+
"0x1e376c085141ab53",
|
|
1678
|
+
"0x2748774cdf8eeb99",
|
|
1679
|
+
"0x34b0bcb5e19b48a8",
|
|
1680
|
+
"0x391c0cb3c5c95a63",
|
|
1681
|
+
"0x4ed8aa4ae3418acb",
|
|
1682
|
+
"0x5b9cca4f7763e373",
|
|
1683
|
+
"0x682e6ff3d6b2b8a3",
|
|
1684
|
+
"0x748f82ee5defb2fc",
|
|
1685
|
+
"0x78a5636f43172f60",
|
|
1686
|
+
"0x84c87814a1f0ab72",
|
|
1687
|
+
"0x8cc702081a6439ec",
|
|
1688
|
+
"0x90befffa23631e28",
|
|
1689
|
+
"0xa4506cebde82bde9",
|
|
1690
|
+
"0xbef9a3f7b2c67915",
|
|
1691
|
+
"0xc67178f2e372532b",
|
|
1692
|
+
"0xca273eceea26619c",
|
|
1693
|
+
"0xd186b8c721c0c207",
|
|
1694
|
+
"0xeada7dd6cde0eb1e",
|
|
1695
|
+
"0xf57d4f7fee6ed178",
|
|
1696
|
+
"0x06f067aa72176fba",
|
|
1697
|
+
"0x0a637dc5a2c898a6",
|
|
1698
|
+
"0x113f9804bef90dae",
|
|
1699
|
+
"0x1b710b35131c471b",
|
|
1700
|
+
"0x28db77f523047d84",
|
|
1701
|
+
"0x32caab7b40c72493",
|
|
1702
|
+
"0x3c9ebe0a15c9bebc",
|
|
1703
|
+
"0x431d67c49c100d4c",
|
|
1704
|
+
"0x4cc5d4becb3e42b6",
|
|
1705
|
+
"0x597f299cfc657e2a",
|
|
1706
|
+
"0x5fcb6fab3ad6faec",
|
|
1707
|
+
"0x6c44198c4a475817"
|
|
1708
|
+
].map((n) => BigInt(n))))();
|
|
1709
|
+
SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
1710
|
+
SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
1711
|
+
SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1712
|
+
SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1713
|
+
SHA2_64B = class extends HashMD {
|
|
1714
|
+
constructor(outputLen) {
|
|
1715
|
+
super(128, outputLen, 16, false);
|
|
1716
|
+
}
|
|
1717
|
+
// prettier-ignore
|
|
1718
|
+
get() {
|
|
1719
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1720
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
1721
|
+
}
|
|
1722
|
+
// prettier-ignore
|
|
1723
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
1724
|
+
this.Ah = Ah | 0;
|
|
1725
|
+
this.Al = Al | 0;
|
|
1726
|
+
this.Bh = Bh | 0;
|
|
1727
|
+
this.Bl = Bl | 0;
|
|
1728
|
+
this.Ch = Ch | 0;
|
|
1729
|
+
this.Cl = Cl | 0;
|
|
1730
|
+
this.Dh = Dh | 0;
|
|
1731
|
+
this.Dl = Dl | 0;
|
|
1732
|
+
this.Eh = Eh | 0;
|
|
1733
|
+
this.El = El | 0;
|
|
1734
|
+
this.Fh = Fh | 0;
|
|
1735
|
+
this.Fl = Fl | 0;
|
|
1736
|
+
this.Gh = Gh | 0;
|
|
1737
|
+
this.Gl = Gl | 0;
|
|
1738
|
+
this.Hh = Hh | 0;
|
|
1739
|
+
this.Hl = Hl | 0;
|
|
1740
|
+
}
|
|
1741
|
+
process(view, offset) {
|
|
1742
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1743
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
1744
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
1745
|
+
}
|
|
1746
|
+
for (let i = 16; i < 80; i++) {
|
|
1747
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
1748
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
1749
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
1750
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
1751
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
1752
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
1753
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
1754
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
1755
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
1756
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
1757
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
1758
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
1759
|
+
}
|
|
1760
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1761
|
+
for (let i = 0; i < 80; i++) {
|
|
1762
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
1763
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
1764
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
1765
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
1766
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
1767
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
1768
|
+
const T1l = T1ll | 0;
|
|
1769
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
1770
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
1771
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
1772
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
1773
|
+
Hh = Gh | 0;
|
|
1774
|
+
Hl = Gl | 0;
|
|
1775
|
+
Gh = Fh | 0;
|
|
1776
|
+
Gl = Fl | 0;
|
|
1777
|
+
Fh = Eh | 0;
|
|
1778
|
+
Fl = El | 0;
|
|
1779
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
1780
|
+
Dh = Ch | 0;
|
|
1781
|
+
Dl = Cl | 0;
|
|
1782
|
+
Ch = Bh | 0;
|
|
1783
|
+
Cl = Bl | 0;
|
|
1784
|
+
Bh = Ah | 0;
|
|
1785
|
+
Bl = Al | 0;
|
|
1786
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
1787
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
1788
|
+
Al = All | 0;
|
|
1789
|
+
}
|
|
1790
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
1791
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
1792
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
1793
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
1794
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
1795
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
1796
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
1797
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
1798
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
1799
|
+
}
|
|
1800
|
+
roundClean() {
|
|
1801
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
1802
|
+
}
|
|
1803
|
+
destroy() {
|
|
1804
|
+
this.destroyed = true;
|
|
1805
|
+
clean(this.buffer);
|
|
1806
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1807
|
+
}
|
|
1808
|
+
};
|
|
1809
|
+
_SHA512 = class extends SHA2_64B {
|
|
1810
|
+
constructor() {
|
|
1811
|
+
super(64);
|
|
1812
|
+
__publicField(this, "Ah", SHA512_IV[0] | 0);
|
|
1813
|
+
__publicField(this, "Al", SHA512_IV[1] | 0);
|
|
1814
|
+
__publicField(this, "Bh", SHA512_IV[2] | 0);
|
|
1815
|
+
__publicField(this, "Bl", SHA512_IV[3] | 0);
|
|
1816
|
+
__publicField(this, "Ch", SHA512_IV[4] | 0);
|
|
1817
|
+
__publicField(this, "Cl", SHA512_IV[5] | 0);
|
|
1818
|
+
__publicField(this, "Dh", SHA512_IV[6] | 0);
|
|
1819
|
+
__publicField(this, "Dl", SHA512_IV[7] | 0);
|
|
1820
|
+
__publicField(this, "Eh", SHA512_IV[8] | 0);
|
|
1821
|
+
__publicField(this, "El", SHA512_IV[9] | 0);
|
|
1822
|
+
__publicField(this, "Fh", SHA512_IV[10] | 0);
|
|
1823
|
+
__publicField(this, "Fl", SHA512_IV[11] | 0);
|
|
1824
|
+
__publicField(this, "Gh", SHA512_IV[12] | 0);
|
|
1825
|
+
__publicField(this, "Gl", SHA512_IV[13] | 0);
|
|
1826
|
+
__publicField(this, "Hh", SHA512_IV[14] | 0);
|
|
1827
|
+
__publicField(this, "Hl", SHA512_IV[15] | 0);
|
|
1828
|
+
}
|
|
1829
|
+
};
|
|
1830
|
+
sha512 = /* @__PURE__ */ createHasher(
|
|
1831
|
+
() => new _SHA512(),
|
|
1832
|
+
/* @__PURE__ */ oidNist(3)
|
|
1833
|
+
);
|
|
1834
|
+
}
|
|
1835
|
+
});
|
|
1836
|
+
|
|
1837
|
+
// ../../../noble-install/node_modules/@noble/curves/abstract/fft.js
|
|
1838
|
+
function checkU32(n) {
|
|
1839
|
+
if (!Number.isSafeInteger(n) || n < 0 || n > 4294967295)
|
|
1840
|
+
throw new Error("wrong u32 integer:" + n);
|
|
1841
|
+
return n;
|
|
1842
|
+
}
|
|
1843
|
+
function isPowerOfTwo(x) {
|
|
1844
|
+
checkU32(x);
|
|
1845
|
+
return (x & x - 1) === 0 && x !== 0;
|
|
1846
|
+
}
|
|
1847
|
+
function reverseBits(n, bits) {
|
|
1848
|
+
checkU32(n);
|
|
1849
|
+
if (!Number.isSafeInteger(bits) || bits < 0 || bits > 32)
|
|
1850
|
+
throw new Error(`expected integer 0 <= bits <= 32, got ${bits}`);
|
|
1851
|
+
let reversed = 0;
|
|
1852
|
+
for (let i = 0; i < bits; i++, n >>>= 1)
|
|
1853
|
+
reversed = reversed << 1 | n & 1;
|
|
1854
|
+
return reversed >>> 0;
|
|
1855
|
+
}
|
|
1856
|
+
function log2(n) {
|
|
1857
|
+
checkU32(n);
|
|
1858
|
+
return 31 - Math.clz32(n);
|
|
1859
|
+
}
|
|
1860
|
+
function bitReversalInplace(values) {
|
|
1861
|
+
const n = values.length;
|
|
1862
|
+
if (!isPowerOfTwo(n))
|
|
1863
|
+
throw new Error("expected positive power-of-two length, got " + n);
|
|
1864
|
+
const bits = log2(n);
|
|
1865
|
+
for (let i = 0; i < n; i++) {
|
|
1866
|
+
const j = reverseBits(i, bits);
|
|
1867
|
+
if (i < j) {
|
|
1868
|
+
const tmp = values[i];
|
|
1869
|
+
values[i] = values[j];
|
|
1870
|
+
values[j] = tmp;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
return values;
|
|
1874
|
+
}
|
|
1875
|
+
var FFTCore;
|
|
1876
|
+
var init_fft = __esm({
|
|
1877
|
+
"../../../noble-install/node_modules/@noble/curves/abstract/fft.js"() {
|
|
1878
|
+
"use strict";
|
|
1879
|
+
FFTCore = (F2, coreOpts) => {
|
|
1880
|
+
const { N: N2, roots, dit, invertButterflies = false, skipStages = 0, brp = true } = coreOpts;
|
|
1881
|
+
const bits = log2(N2);
|
|
1882
|
+
if (!isPowerOfTwo(N2))
|
|
1883
|
+
throw new Error("FFT: Polynomial size should be power of two");
|
|
1884
|
+
if (roots.length !== N2)
|
|
1885
|
+
throw new Error(`FFT: wrong roots length: expected ${N2}, got ${roots.length}`);
|
|
1886
|
+
const isDit = dit !== invertButterflies;
|
|
1887
|
+
isDit;
|
|
1888
|
+
return (values) => {
|
|
1889
|
+
if (values.length !== N2)
|
|
1890
|
+
throw new Error("FFT: wrong Polynomial length");
|
|
1891
|
+
if (dit && brp)
|
|
1892
|
+
bitReversalInplace(values);
|
|
1893
|
+
for (let i = 0, g = 1; i < bits - skipStages; i++) {
|
|
1894
|
+
const s = dit ? i + 1 + skipStages : bits - i;
|
|
1895
|
+
const m = 1 << s;
|
|
1896
|
+
const m2 = m >> 1;
|
|
1897
|
+
const stride = N2 >> s;
|
|
1898
|
+
for (let k = 0; k < N2; k += m) {
|
|
1899
|
+
for (let j = 0, grp = g++; j < m2; j++) {
|
|
1900
|
+
const rootPos = invertButterflies ? dit ? N2 - grp : grp : j * stride;
|
|
1901
|
+
const i0 = k + j;
|
|
1902
|
+
const i1 = k + j + m2;
|
|
1903
|
+
const omega = roots[rootPos];
|
|
1904
|
+
const b = values[i1];
|
|
1905
|
+
const a = values[i0];
|
|
1906
|
+
if (isDit) {
|
|
1907
|
+
const t = F2.mul(b, omega);
|
|
1908
|
+
values[i0] = F2.add(a, t);
|
|
1909
|
+
values[i1] = F2.sub(a, t);
|
|
1910
|
+
} else if (invertButterflies) {
|
|
1911
|
+
values[i0] = F2.add(b, a);
|
|
1912
|
+
values[i1] = F2.mul(F2.sub(b, a), omega);
|
|
1913
|
+
} else {
|
|
1914
|
+
values[i0] = F2.add(a, b);
|
|
1915
|
+
values[i1] = F2.mul(F2.sub(a, b), omega);
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
if (!dit && brp)
|
|
1921
|
+
bitReversalInplace(values);
|
|
1922
|
+
return values;
|
|
1923
|
+
};
|
|
1924
|
+
};
|
|
1925
|
+
}
|
|
1926
|
+
});
|
|
1927
|
+
|
|
1928
|
+
// ../../../noble-install/node_modules/@noble/curves/abstract/hash-to-curve.js
|
|
1929
|
+
function i2osp(value, length) {
|
|
1930
|
+
asafenumber(value);
|
|
1931
|
+
asafenumber(length);
|
|
1932
|
+
if (length < 0 || length > 4)
|
|
1933
|
+
throw new Error("invalid I2OSP length: " + length);
|
|
1934
|
+
if (value < 0 || value > 2 ** (8 * length) - 1)
|
|
1935
|
+
throw new Error("invalid I2OSP input: " + value);
|
|
1936
|
+
const res = Array.from({ length }).fill(0);
|
|
1937
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
1938
|
+
res[i] = value & 255;
|
|
1939
|
+
value >>>= 8;
|
|
1940
|
+
}
|
|
1941
|
+
return new Uint8Array(res);
|
|
1942
|
+
}
|
|
1943
|
+
function strxor(a, b) {
|
|
1944
|
+
const arr = new Uint8Array(a.length);
|
|
1945
|
+
for (let i = 0; i < a.length; i++) {
|
|
1946
|
+
arr[i] = a[i] ^ b[i];
|
|
1947
|
+
}
|
|
1948
|
+
return arr;
|
|
1949
|
+
}
|
|
1950
|
+
function normDST(DST) {
|
|
1951
|
+
if (!isBytes2(DST) && typeof DST !== "string")
|
|
1952
|
+
throw new Error("DST must be Uint8Array or ascii string");
|
|
1953
|
+
const dst = typeof DST === "string" ? asciiToBytes(DST) : DST;
|
|
1954
|
+
if (dst.length === 0)
|
|
1955
|
+
throw new Error("DST must be non-empty");
|
|
1956
|
+
return dst;
|
|
1957
|
+
}
|
|
1958
|
+
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
1959
|
+
abytes2(msg);
|
|
1960
|
+
asafenumber(lenInBytes);
|
|
1961
|
+
DST = normDST(DST);
|
|
1962
|
+
if (DST.length > 255)
|
|
1963
|
+
DST = H(concatBytes2(asciiToBytes("H2C-OVERSIZE-DST-"), DST));
|
|
1964
|
+
const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
|
|
1965
|
+
const ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
1966
|
+
if (lenInBytes > 65535 || ell > 255)
|
|
1967
|
+
throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
1968
|
+
const DST_prime = concatBytes2(DST, i2osp(DST.length, 1));
|
|
1969
|
+
const Z_pad = new Uint8Array(r_in_bytes);
|
|
1970
|
+
const l_i_b_str = i2osp(lenInBytes, 2);
|
|
1971
|
+
const b = new Array(ell);
|
|
1972
|
+
const b_0 = H(concatBytes2(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
|
|
1973
|
+
b[0] = H(concatBytes2(b_0, i2osp(1, 1), DST_prime));
|
|
1974
|
+
for (let i = 1; i < ell; i++) {
|
|
1975
|
+
const args = [strxor(b_0, b[i - 1]), i2osp(i + 1, 1), DST_prime];
|
|
1976
|
+
b[i] = H(concatBytes2(...args));
|
|
1977
|
+
}
|
|
1978
|
+
const pseudo_random_bytes = concatBytes2(...b);
|
|
1979
|
+
return pseudo_random_bytes.slice(0, lenInBytes);
|
|
1980
|
+
}
|
|
1981
|
+
var _DST_scalar;
|
|
1982
|
+
var init_hash_to_curve = __esm({
|
|
1983
|
+
"../../../noble-install/node_modules/@noble/curves/abstract/hash-to-curve.js"() {
|
|
1984
|
+
"use strict";
|
|
1985
|
+
init_utils2();
|
|
1986
|
+
_DST_scalar = "HashToScalar-";
|
|
1987
|
+
}
|
|
1988
|
+
});
|
|
1989
|
+
|
|
1990
|
+
// ../../../noble-install/node_modules/@noble/curves/ed25519.js
|
|
1991
|
+
function ed25519_pow_2_252_3(x) {
|
|
1992
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
1993
|
+
const P = ed25519_CURVE_p;
|
|
1994
|
+
const x2 = x * x % P;
|
|
1995
|
+
const b2 = x2 * x % P;
|
|
1996
|
+
const b4 = pow2(b2, _2n4, P) * b2 % P;
|
|
1997
|
+
const b5 = pow2(b4, _1n6, P) * x % P;
|
|
1998
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
1999
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
2000
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
2001
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
2002
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
2003
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
2004
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
2005
|
+
const pow_p_5_8 = pow2(b250, _2n4, P) * x % P;
|
|
2006
|
+
return { pow_p_5_8, b2 };
|
|
2007
|
+
}
|
|
2008
|
+
function adjustScalarBytes(bytes) {
|
|
2009
|
+
bytes[0] &= 248;
|
|
2010
|
+
bytes[31] &= 127;
|
|
2011
|
+
bytes[31] |= 64;
|
|
2012
|
+
return bytes;
|
|
2013
|
+
}
|
|
2014
|
+
function uvRatio(u, v) {
|
|
2015
|
+
const P = ed25519_CURVE_p;
|
|
2016
|
+
const v3 = mod(v * v * v, P);
|
|
2017
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
2018
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
2019
|
+
let x = mod(u * v3 * pow, P);
|
|
2020
|
+
const vx2 = mod(v * x * x, P);
|
|
2021
|
+
const root1 = x;
|
|
2022
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
2023
|
+
const useRoot1 = vx2 === u;
|
|
2024
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
2025
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
2026
|
+
if (useRoot1)
|
|
2027
|
+
x = root1;
|
|
2028
|
+
if (useRoot2 || noRoot)
|
|
2029
|
+
x = root2;
|
|
2030
|
+
if (isNegativeLE(x, P))
|
|
2031
|
+
x = mod(-x, P);
|
|
2032
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
2033
|
+
}
|
|
2034
|
+
function calcElligatorRistrettoMap(r0) {
|
|
2035
|
+
const { d } = ed25519_CURVE;
|
|
2036
|
+
const P = ed25519_CURVE_p;
|
|
2037
|
+
const mod2 = (n) => Fp.create(n);
|
|
2038
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
2039
|
+
const Ns = mod2((r + _1n6) * ONE_MINUS_D_SQ);
|
|
2040
|
+
let c = BigInt(-1);
|
|
2041
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
2042
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
2043
|
+
let s_ = mod2(s * r0);
|
|
2044
|
+
if (!isNegativeLE(s_, P))
|
|
2045
|
+
s_ = mod2(-s_);
|
|
2046
|
+
if (!Ns_D_is_sq)
|
|
2047
|
+
s = s_;
|
|
2048
|
+
if (!Ns_D_is_sq)
|
|
2049
|
+
c = r;
|
|
2050
|
+
const Nt = mod2(c * (r - _1n6) * D_MINUS_ONE_SQ - D);
|
|
2051
|
+
const s2 = s * s;
|
|
2052
|
+
const W0 = mod2((s + s) * D);
|
|
2053
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
2054
|
+
const W2 = mod2(_1n6 - s2);
|
|
2055
|
+
const W3 = mod2(_1n6 + s2);
|
|
2056
|
+
return new ed25519_Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
2057
|
+
}
|
|
2058
|
+
var _0n6, _1n6, _2n4, _3n2, _5n2, _8n3, ed25519_CURVE_p, ed25519_CURVE, ED25519_SQRT_M1, ed25519_Point, Fp, Fn, x25519, SQRT_M1, SQRT_AD_MINUS_ONE, INVSQRT_A_MINUS_D, ONE_MINUS_D_SQ, D_MINUS_ONE_SQ, invertSqrt, MAX_255B, bytes255ToNumberLE, __RistrettoPoint, _RistrettoPoint, ristretto255_hasher;
|
|
2059
|
+
var init_ed25519 = __esm({
|
|
2060
|
+
"../../../noble-install/node_modules/@noble/curves/ed25519.js"() {
|
|
2061
|
+
"use strict";
|
|
2062
|
+
init_sha2();
|
|
2063
|
+
init_utils();
|
|
2064
|
+
init_edwards();
|
|
2065
|
+
init_hash_to_curve();
|
|
2066
|
+
init_modular();
|
|
2067
|
+
init_montgomery();
|
|
2068
|
+
init_utils2();
|
|
2069
|
+
_0n6 = /* @__PURE__ */ BigInt(0);
|
|
2070
|
+
_1n6 = /* @__PURE__ */ BigInt(1);
|
|
2071
|
+
_2n4 = /* @__PURE__ */ BigInt(2);
|
|
2072
|
+
_3n2 = /* @__PURE__ */ BigInt(3);
|
|
2073
|
+
_5n2 = /* @__PURE__ */ BigInt(5);
|
|
2074
|
+
_8n3 = /* @__PURE__ */ BigInt(8);
|
|
2075
|
+
ed25519_CURVE_p = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
2076
|
+
ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
2077
|
+
p: ed25519_CURVE_p,
|
|
2078
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
2079
|
+
h: _8n3,
|
|
2080
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
2081
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
2082
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
2083
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
2084
|
+
}))();
|
|
2085
|
+
ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
2086
|
+
ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
|
|
2087
|
+
Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
2088
|
+
Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
|
|
2089
|
+
x25519 = /* @__PURE__ */ (() => {
|
|
2090
|
+
const P = ed25519_CURVE_p;
|
|
2091
|
+
return montgomery({
|
|
2092
|
+
P,
|
|
2093
|
+
type: "x25519",
|
|
2094
|
+
powPminus2: (x) => {
|
|
2095
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
2096
|
+
return mod(pow2(pow_p_5_8, _3n2, P) * b2, P);
|
|
2097
|
+
},
|
|
2098
|
+
adjustScalarBytes
|
|
2099
|
+
});
|
|
2100
|
+
})();
|
|
2101
|
+
SQRT_M1 = ED25519_SQRT_M1;
|
|
2102
|
+
SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
2103
|
+
INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
2104
|
+
ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
2105
|
+
D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
2106
|
+
invertSqrt = (number) => uvRatio(_1n6, number);
|
|
2107
|
+
MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
2108
|
+
bytes255ToNumberLE = (bytes) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
2109
|
+
__RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
2110
|
+
constructor(ep) {
|
|
2111
|
+
super(ep);
|
|
2112
|
+
}
|
|
2113
|
+
/**
|
|
2114
|
+
* Create one Ristretto255 point from affine Edwards coordinates.
|
|
2115
|
+
* This wraps the internal Edwards representative directly and is not a
|
|
2116
|
+
* canonical ristretto255 decoding path.
|
|
2117
|
+
* Use `toBytes()` / `fromBytes()` if canonical ristretto255 bytes matter.
|
|
2118
|
+
*/
|
|
2119
|
+
static fromAffine(ap) {
|
|
2120
|
+
return new __RistrettoPoint(ed25519_Point.fromAffine(ap));
|
|
2121
|
+
}
|
|
2122
|
+
assertSame(other) {
|
|
2123
|
+
if (!(other instanceof __RistrettoPoint))
|
|
2124
|
+
throw new Error("RistrettoPoint expected");
|
|
2125
|
+
}
|
|
2126
|
+
init(ep) {
|
|
2127
|
+
return new __RistrettoPoint(ep);
|
|
2128
|
+
}
|
|
2129
|
+
static fromBytes(bytes) {
|
|
2130
|
+
abytes(bytes, 32);
|
|
2131
|
+
const { a, d } = ed25519_CURVE;
|
|
2132
|
+
const P = ed25519_CURVE_p;
|
|
2133
|
+
const mod2 = (n) => Fp.create(n);
|
|
2134
|
+
const s = bytes255ToNumberLE(bytes);
|
|
2135
|
+
if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
2136
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
2137
|
+
const s2 = mod2(s * s);
|
|
2138
|
+
const u1 = mod2(_1n6 + a * s2);
|
|
2139
|
+
const u2 = mod2(_1n6 - a * s2);
|
|
2140
|
+
const u1_2 = mod2(u1 * u1);
|
|
2141
|
+
const u2_2 = mod2(u2 * u2);
|
|
2142
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
2143
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
2144
|
+
const Dx = mod2(I * u2);
|
|
2145
|
+
const Dy = mod2(I * Dx * v);
|
|
2146
|
+
let x = mod2((s + s) * Dx);
|
|
2147
|
+
if (isNegativeLE(x, P))
|
|
2148
|
+
x = mod2(-x);
|
|
2149
|
+
const y = mod2(u1 * Dy);
|
|
2150
|
+
const t = mod2(x * y);
|
|
2151
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n6)
|
|
2152
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
2153
|
+
return new __RistrettoPoint(new ed25519_Point(x, y, _1n6, t));
|
|
2154
|
+
}
|
|
2155
|
+
/**
|
|
2156
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
2157
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
2158
|
+
* @param hex - Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
2159
|
+
*/
|
|
2160
|
+
static fromHex(hex) {
|
|
2161
|
+
return __RistrettoPoint.fromBytes(hexToBytes(hex));
|
|
2162
|
+
}
|
|
2163
|
+
/**
|
|
2164
|
+
* Encodes ristretto point to Uint8Array.
|
|
2165
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
2166
|
+
*/
|
|
2167
|
+
toBytes() {
|
|
2168
|
+
let { X, Y, Z, T } = this.ep;
|
|
2169
|
+
const P = ed25519_CURVE_p;
|
|
2170
|
+
const mod2 = (n) => Fp.create(n);
|
|
2171
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
2172
|
+
const u2 = mod2(X * Y);
|
|
2173
|
+
const u2sq = mod2(u2 * u2);
|
|
2174
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
2175
|
+
const D1 = mod2(invsqrt * u1);
|
|
2176
|
+
const D2 = mod2(invsqrt * u2);
|
|
2177
|
+
const zInv = mod2(D1 * D2 * T);
|
|
2178
|
+
let D;
|
|
2179
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
2180
|
+
let _x = mod2(Y * SQRT_M1);
|
|
2181
|
+
let _y = mod2(X * SQRT_M1);
|
|
2182
|
+
X = _x;
|
|
2183
|
+
Y = _y;
|
|
2184
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
2185
|
+
} else {
|
|
2186
|
+
D = D2;
|
|
2187
|
+
}
|
|
2188
|
+
if (isNegativeLE(X * zInv, P))
|
|
2189
|
+
Y = mod2(-Y);
|
|
2190
|
+
let s = mod2((Z - Y) * D);
|
|
2191
|
+
if (isNegativeLE(s, P))
|
|
2192
|
+
s = mod2(-s);
|
|
2193
|
+
return Fp.toBytes(s);
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* Compares two Ristretto points.
|
|
2197
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
2198
|
+
*/
|
|
2199
|
+
equals(other) {
|
|
2200
|
+
this.assertSame(other);
|
|
2201
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
2202
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
2203
|
+
const mod2 = (n) => Fp.create(n);
|
|
2204
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
2205
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
2206
|
+
return one || two;
|
|
2207
|
+
}
|
|
2208
|
+
is0() {
|
|
2209
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
2210
|
+
}
|
|
2211
|
+
};
|
|
2212
|
+
// Do NOT change syntax: the following gymnastics is done,
|
|
2213
|
+
// because typescript strips comments, which makes bundlers disable tree-shaking.
|
|
2214
|
+
// prettier-ignore
|
|
2215
|
+
__publicField(__RistrettoPoint, "BASE", /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.BASE))());
|
|
2216
|
+
// prettier-ignore
|
|
2217
|
+
__publicField(__RistrettoPoint, "ZERO", /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.ZERO))());
|
|
2218
|
+
// prettier-ignore
|
|
2219
|
+
__publicField(__RistrettoPoint, "Fp", /* @__PURE__ */ (() => Fp)());
|
|
2220
|
+
// prettier-ignore
|
|
2221
|
+
__publicField(__RistrettoPoint, "Fn", /* @__PURE__ */ (() => Fn)());
|
|
2222
|
+
_RistrettoPoint = __RistrettoPoint;
|
|
2223
|
+
Object.freeze(_RistrettoPoint.BASE);
|
|
2224
|
+
Object.freeze(_RistrettoPoint.ZERO);
|
|
2225
|
+
Object.freeze(_RistrettoPoint.prototype);
|
|
2226
|
+
Object.freeze(_RistrettoPoint);
|
|
2227
|
+
ristretto255_hasher = Object.freeze({
|
|
2228
|
+
Point: _RistrettoPoint,
|
|
2229
|
+
/**
|
|
2230
|
+
* Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
|
|
2231
|
+
* * There are no test vectors
|
|
2232
|
+
* * encodeToCurve / mapToCurve is undefined
|
|
2233
|
+
* * mapToCurve would be `calcElligatorRistrettoMap(scalars[0])`, not ristretto255_map!
|
|
2234
|
+
* * hashToScalar is undefined too, so we just use OPRF implementation
|
|
2235
|
+
* * We cannot re-use 'createHasher', because ristretto255_map is different algorithm/RFC
|
|
2236
|
+
(os2ip -> bytes255ToNumberLE)
|
|
2237
|
+
* * mapToCurve == calcElligatorRistrettoMap, hashToCurve == ristretto255_map
|
|
2238
|
+
* * hashToScalar is undefined in RFC9380 for ristretto, so we use the OPRF
|
|
2239
|
+
version here. Using `bytes255ToNumblerLE` will create a different result
|
|
2240
|
+
if we use `bytes255ToNumberLE` as os2ip
|
|
2241
|
+
* * current version is closest to spec.
|
|
2242
|
+
*/
|
|
2243
|
+
hashToCurve(msg, options) {
|
|
2244
|
+
const DST = options?.DST === void 0 ? "ristretto255_XMD:SHA-512_R255MAP_RO_" : options.DST;
|
|
2245
|
+
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
2246
|
+
return ristretto255_hasher.deriveToCurve(xmd);
|
|
2247
|
+
},
|
|
2248
|
+
hashToScalar(msg, options = { DST: _DST_scalar }) {
|
|
2249
|
+
const xmd = expand_message_xmd(msg, options.DST, 64, sha512);
|
|
2250
|
+
return Fn.create(bytesToNumberLE(xmd));
|
|
2251
|
+
},
|
|
2252
|
+
/**
|
|
2253
|
+
* HashToCurve-like construction based on RFC 9496 (Element Derivation).
|
|
2254
|
+
* Converts 64 uniform random bytes into a curve point.
|
|
2255
|
+
*
|
|
2256
|
+
* WARNING: This represents an older hash-to-curve construction from before
|
|
2257
|
+
* RFC 9380 was finalized.
|
|
2258
|
+
* It was later reused as a component in the newer
|
|
2259
|
+
* `hash_to_ristretto255` function defined in RFC 9380.
|
|
2260
|
+
*/
|
|
2261
|
+
deriveToCurve(bytes) {
|
|
2262
|
+
abytes(bytes, 64);
|
|
2263
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
2264
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
2265
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
2266
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
2267
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
2268
|
+
}
|
|
2269
|
+
});
|
|
2270
|
+
}
|
|
2271
|
+
});
|
|
2272
|
+
|
|
2273
|
+
// ../../../noble-install/node_modules/@noble/hashes/sha3.js
|
|
2274
|
+
function keccakP(s, rounds = 24) {
|
|
2275
|
+
anumber(rounds, "rounds");
|
|
2276
|
+
if (rounds < 1 || rounds > 24)
|
|
2277
|
+
throw new Error('"rounds" expected integer 1..24');
|
|
2278
|
+
const B = new Uint32Array(5 * 2);
|
|
2279
|
+
for (let round = 24 - rounds; round < 24; round++) {
|
|
2280
|
+
for (let x = 0; x < 10; x++)
|
|
2281
|
+
B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
|
|
2282
|
+
for (let x = 0; x < 10; x += 2) {
|
|
2283
|
+
const idx1 = (x + 8) % 10;
|
|
2284
|
+
const idx0 = (x + 2) % 10;
|
|
2285
|
+
const B0 = B[idx0];
|
|
2286
|
+
const B1 = B[idx0 + 1];
|
|
2287
|
+
const Th = rotlH(B0, B1, 1) ^ B[idx1];
|
|
2288
|
+
const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
|
|
2289
|
+
for (let y = 0; y < 50; y += 10) {
|
|
2290
|
+
s[x + y] ^= Th;
|
|
2291
|
+
s[x + y + 1] ^= Tl;
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
let curH = s[2];
|
|
2295
|
+
let curL = s[3];
|
|
2296
|
+
for (let t = 0; t < 24; t++) {
|
|
2297
|
+
const shift = SHA3_ROTL[t];
|
|
2298
|
+
const Th = rotlH(curH, curL, shift);
|
|
2299
|
+
const Tl = rotlL(curH, curL, shift);
|
|
2300
|
+
const PI = SHA3_PI[t];
|
|
2301
|
+
curH = s[PI];
|
|
2302
|
+
curL = s[PI + 1];
|
|
2303
|
+
s[PI] = Th;
|
|
2304
|
+
s[PI + 1] = Tl;
|
|
2305
|
+
}
|
|
2306
|
+
for (let y = 0; y < 50; y += 10) {
|
|
2307
|
+
const b0 = s[y], b1 = s[y + 1], b2 = s[y + 2], b3 = s[y + 3];
|
|
2308
|
+
s[y] ^= ~s[y + 2] & s[y + 4];
|
|
2309
|
+
s[y + 1] ^= ~s[y + 3] & s[y + 5];
|
|
2310
|
+
s[y + 2] ^= ~s[y + 4] & s[y + 6];
|
|
2311
|
+
s[y + 3] ^= ~s[y + 5] & s[y + 7];
|
|
2312
|
+
s[y + 4] ^= ~s[y + 6] & s[y + 8];
|
|
2313
|
+
s[y + 5] ^= ~s[y + 7] & s[y + 9];
|
|
2314
|
+
s[y + 6] ^= ~s[y + 8] & b0;
|
|
2315
|
+
s[y + 7] ^= ~s[y + 9] & b1;
|
|
2316
|
+
s[y + 8] ^= ~b0 & b2;
|
|
2317
|
+
s[y + 9] ^= ~b1 & b3;
|
|
2318
|
+
}
|
|
2319
|
+
s[0] ^= SHA3_IOTA_H[round];
|
|
2320
|
+
s[1] ^= SHA3_IOTA_L[round];
|
|
2321
|
+
}
|
|
2322
|
+
clean(B);
|
|
2323
|
+
}
|
|
2324
|
+
var _0n7, _1n7, _2n5, _7n2, _256n, _0x71n, SHA3_PI, SHA3_ROTL, _SHA3_IOTA, IOTAS, SHA3_IOTA_H, SHA3_IOTA_L, rotlH, rotlL, Keccak, genKeccak, sha3_256, sha3_512, genShake, shake128, shake256;
|
|
2325
|
+
var init_sha3 = __esm({
|
|
2326
|
+
"../../../noble-install/node_modules/@noble/hashes/sha3.js"() {
|
|
2327
|
+
"use strict";
|
|
2328
|
+
init_u64();
|
|
2329
|
+
init_utils();
|
|
2330
|
+
_0n7 = BigInt(0);
|
|
2331
|
+
_1n7 = BigInt(1);
|
|
2332
|
+
_2n5 = BigInt(2);
|
|
2333
|
+
_7n2 = BigInt(7);
|
|
2334
|
+
_256n = BigInt(256);
|
|
2335
|
+
_0x71n = BigInt(113);
|
|
2336
|
+
SHA3_PI = [];
|
|
2337
|
+
SHA3_ROTL = [];
|
|
2338
|
+
_SHA3_IOTA = [];
|
|
2339
|
+
for (let round = 0, R = _1n7, x = 1, y = 0; round < 24; round++) {
|
|
2340
|
+
[x, y] = [y, (2 * x + 3 * y) % 5];
|
|
2341
|
+
SHA3_PI.push(2 * (5 * y + x));
|
|
2342
|
+
SHA3_ROTL.push((round + 1) * (round + 2) / 2 % 64);
|
|
2343
|
+
let t = _0n7;
|
|
2344
|
+
for (let j = 0; j < 7; j++) {
|
|
2345
|
+
R = (R << _1n7 ^ (R >> _7n2) * _0x71n) % _256n;
|
|
2346
|
+
if (R & _2n5)
|
|
2347
|
+
t ^= _1n7 << (_1n7 << BigInt(j)) - _1n7;
|
|
2348
|
+
}
|
|
2349
|
+
_SHA3_IOTA.push(t);
|
|
2350
|
+
}
|
|
2351
|
+
IOTAS = split(_SHA3_IOTA, true);
|
|
2352
|
+
SHA3_IOTA_H = IOTAS[0];
|
|
2353
|
+
SHA3_IOTA_L = IOTAS[1];
|
|
2354
|
+
rotlH = (h, l, s) => s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s);
|
|
2355
|
+
rotlL = (h, l, s) => s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s);
|
|
2356
|
+
Keccak = class _Keccak {
|
|
2357
|
+
// NOTE: we accept arguments in bytes instead of bits here.
|
|
2358
|
+
constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
|
|
2359
|
+
__publicField(this, "state");
|
|
2360
|
+
__publicField(this, "pos", 0);
|
|
2361
|
+
__publicField(this, "posOut", 0);
|
|
2362
|
+
__publicField(this, "finished", false);
|
|
2363
|
+
__publicField(this, "state32");
|
|
2364
|
+
__publicField(this, "destroyed", false);
|
|
2365
|
+
__publicField(this, "blockLen");
|
|
2366
|
+
__publicField(this, "suffix");
|
|
2367
|
+
__publicField(this, "outputLen");
|
|
2368
|
+
__publicField(this, "canXOF");
|
|
2369
|
+
__publicField(this, "enableXOF", false);
|
|
2370
|
+
__publicField(this, "rounds");
|
|
2371
|
+
this.blockLen = blockLen;
|
|
2372
|
+
this.suffix = suffix;
|
|
2373
|
+
this.outputLen = outputLen;
|
|
2374
|
+
this.enableXOF = enableXOF;
|
|
2375
|
+
this.canXOF = enableXOF;
|
|
2376
|
+
this.rounds = rounds;
|
|
2377
|
+
anumber(outputLen, "outputLen");
|
|
2378
|
+
if (!(0 < blockLen && blockLen < 200))
|
|
2379
|
+
throw new Error("only keccak-f1600 function is supported");
|
|
2380
|
+
this.state = new Uint8Array(200);
|
|
2381
|
+
this.state32 = u32(this.state);
|
|
2382
|
+
}
|
|
2383
|
+
clone() {
|
|
2384
|
+
return this._cloneInto();
|
|
2385
|
+
}
|
|
2386
|
+
keccak() {
|
|
2387
|
+
swap32IfBE(this.state32);
|
|
2388
|
+
keccakP(this.state32, this.rounds);
|
|
2389
|
+
swap32IfBE(this.state32);
|
|
2390
|
+
this.posOut = 0;
|
|
2391
|
+
this.pos = 0;
|
|
2392
|
+
}
|
|
2393
|
+
update(data) {
|
|
2394
|
+
aexists(this);
|
|
2395
|
+
abytes(data);
|
|
2396
|
+
const { blockLen, state } = this;
|
|
2397
|
+
const len = data.length;
|
|
2398
|
+
for (let pos = 0; pos < len; ) {
|
|
2399
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
2400
|
+
for (let i = 0; i < take; i++)
|
|
2401
|
+
state[this.pos++] ^= data[pos++];
|
|
2402
|
+
if (this.pos === blockLen)
|
|
2403
|
+
this.keccak();
|
|
2404
|
+
}
|
|
2405
|
+
return this;
|
|
2406
|
+
}
|
|
2407
|
+
finish() {
|
|
2408
|
+
if (this.finished)
|
|
2409
|
+
return;
|
|
2410
|
+
this.finished = true;
|
|
2411
|
+
const { state, suffix, pos, blockLen } = this;
|
|
2412
|
+
state[pos] ^= suffix;
|
|
2413
|
+
if ((suffix & 128) !== 0 && pos === blockLen - 1)
|
|
2414
|
+
this.keccak();
|
|
2415
|
+
state[blockLen - 1] ^= 128;
|
|
2416
|
+
this.keccak();
|
|
2417
|
+
}
|
|
2418
|
+
writeInto(out) {
|
|
2419
|
+
aexists(this, false);
|
|
2420
|
+
abytes(out);
|
|
2421
|
+
this.finish();
|
|
2422
|
+
const bufferOut = this.state;
|
|
2423
|
+
const { blockLen } = this;
|
|
2424
|
+
for (let pos = 0, len = out.length; pos < len; ) {
|
|
2425
|
+
if (this.posOut >= blockLen)
|
|
2426
|
+
this.keccak();
|
|
2427
|
+
const take = Math.min(blockLen - this.posOut, len - pos);
|
|
2428
|
+
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
|
2429
|
+
this.posOut += take;
|
|
2430
|
+
pos += take;
|
|
2431
|
+
}
|
|
2432
|
+
return out;
|
|
2433
|
+
}
|
|
2434
|
+
xofInto(out) {
|
|
2435
|
+
if (!this.enableXOF)
|
|
2436
|
+
throw new Error("XOF is not possible for this instance");
|
|
2437
|
+
return this.writeInto(out);
|
|
2438
|
+
}
|
|
2439
|
+
xof(bytes) {
|
|
2440
|
+
anumber(bytes);
|
|
2441
|
+
return this.xofInto(new Uint8Array(bytes));
|
|
2442
|
+
}
|
|
2443
|
+
digestInto(out) {
|
|
2444
|
+
aoutput(out, this);
|
|
2445
|
+
if (this.finished)
|
|
2446
|
+
throw new Error("digest() was already called");
|
|
2447
|
+
this.writeInto(out.subarray(0, this.outputLen));
|
|
2448
|
+
this.destroy();
|
|
2449
|
+
}
|
|
2450
|
+
digest() {
|
|
2451
|
+
const out = new Uint8Array(this.outputLen);
|
|
2452
|
+
this.digestInto(out);
|
|
2453
|
+
return out;
|
|
2454
|
+
}
|
|
2455
|
+
destroy() {
|
|
2456
|
+
this.destroyed = true;
|
|
2457
|
+
clean(this.state);
|
|
2458
|
+
}
|
|
2459
|
+
_cloneInto(to) {
|
|
2460
|
+
const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
|
|
2461
|
+
to || (to = new _Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
|
|
2462
|
+
to.blockLen = blockLen;
|
|
2463
|
+
to.state32.set(this.state32);
|
|
2464
|
+
to.pos = this.pos;
|
|
2465
|
+
to.posOut = this.posOut;
|
|
2466
|
+
to.finished = this.finished;
|
|
2467
|
+
to.rounds = rounds;
|
|
2468
|
+
to.suffix = suffix;
|
|
2469
|
+
to.outputLen = outputLen;
|
|
2470
|
+
to.enableXOF = enableXOF;
|
|
2471
|
+
to.canXOF = this.canXOF;
|
|
2472
|
+
to.destroyed = this.destroyed;
|
|
2473
|
+
return to;
|
|
2474
|
+
}
|
|
2475
|
+
};
|
|
2476
|
+
genKeccak = (suffix, blockLen, outputLen, info = {}) => createHasher(() => new Keccak(blockLen, suffix, outputLen), info);
|
|
2477
|
+
sha3_256 = /* @__PURE__ */ genKeccak(
|
|
2478
|
+
6,
|
|
2479
|
+
136,
|
|
2480
|
+
32,
|
|
2481
|
+
/* @__PURE__ */ oidNist(8)
|
|
2482
|
+
);
|
|
2483
|
+
sha3_512 = /* @__PURE__ */ genKeccak(
|
|
2484
|
+
6,
|
|
2485
|
+
72,
|
|
2486
|
+
64,
|
|
2487
|
+
/* @__PURE__ */ oidNist(10)
|
|
2488
|
+
);
|
|
2489
|
+
genShake = (suffix, blockLen, outputLen, info = {}) => createHasher((opts2 = {}) => new Keccak(blockLen, suffix, opts2.dkLen === void 0 ? outputLen : opts2.dkLen, true), info);
|
|
2490
|
+
shake128 = /* @__PURE__ */ genShake(31, 168, 16, /* @__PURE__ */ oidNist(11));
|
|
2491
|
+
shake256 = /* @__PURE__ */ genShake(31, 136, 32, /* @__PURE__ */ oidNist(12));
|
|
2492
|
+
}
|
|
2493
|
+
});
|
|
2494
|
+
|
|
2495
|
+
// ../../../noble-install/node_modules/@noble/post-quantum/utils.js
|
|
2496
|
+
function equalBytes2(a, b) {
|
|
2497
|
+
if (a.length !== b.length)
|
|
2498
|
+
return false;
|
|
2499
|
+
let diff = 0;
|
|
2500
|
+
for (let i = 0; i < a.length; i++)
|
|
2501
|
+
diff |= a[i] ^ b[i];
|
|
2502
|
+
return diff === 0;
|
|
2503
|
+
}
|
|
2504
|
+
function copyBytes2(bytes) {
|
|
2505
|
+
return Uint8Array.from(abytes(bytes));
|
|
2506
|
+
}
|
|
2507
|
+
function splitCoder(label, ...lengths) {
|
|
2508
|
+
const getLength = (c) => typeof c === "number" ? c : c.bytesLen;
|
|
2509
|
+
const bytesLen = lengths.reduce((sum, a) => sum + getLength(a), 0);
|
|
2510
|
+
return {
|
|
2511
|
+
bytesLen,
|
|
2512
|
+
encode: (bufs) => {
|
|
2513
|
+
const res = new Uint8Array(bytesLen);
|
|
2514
|
+
for (let i = 0, pos = 0; i < lengths.length; i++) {
|
|
2515
|
+
const c = lengths[i];
|
|
2516
|
+
const l = getLength(c);
|
|
2517
|
+
const b = typeof c === "number" ? bufs[i] : c.encode(bufs[i]);
|
|
2518
|
+
abytes(b, l, label);
|
|
2519
|
+
res.set(b, pos);
|
|
2520
|
+
if (typeof c !== "number")
|
|
2521
|
+
b.fill(0);
|
|
2522
|
+
pos += l;
|
|
2523
|
+
}
|
|
2524
|
+
return res;
|
|
2525
|
+
},
|
|
2526
|
+
decode: (buf) => {
|
|
2527
|
+
abytes(buf, bytesLen, label);
|
|
2528
|
+
const res = [];
|
|
2529
|
+
for (const c of lengths) {
|
|
2530
|
+
const l = getLength(c);
|
|
2531
|
+
const b = buf.subarray(0, l);
|
|
2532
|
+
res.push(typeof c === "number" ? b : c.decode(b));
|
|
2533
|
+
buf = buf.subarray(l);
|
|
2534
|
+
}
|
|
2535
|
+
return res;
|
|
2536
|
+
}
|
|
2537
|
+
};
|
|
2538
|
+
}
|
|
2539
|
+
function vecCoder(c, vecLen) {
|
|
2540
|
+
const coder = c;
|
|
2541
|
+
const bytesLen = vecLen * coder.bytesLen;
|
|
2542
|
+
return {
|
|
2543
|
+
bytesLen,
|
|
2544
|
+
encode: (u) => {
|
|
2545
|
+
if (u.length !== vecLen)
|
|
2546
|
+
throw new RangeError(`vecCoder.encode: wrong length=${u.length}. Expected: ${vecLen}`);
|
|
2547
|
+
const res = new Uint8Array(bytesLen);
|
|
2548
|
+
for (let i = 0, pos = 0; i < u.length; i++) {
|
|
2549
|
+
const b = coder.encode(u[i]);
|
|
2550
|
+
res.set(b, pos);
|
|
2551
|
+
b.fill(0);
|
|
2552
|
+
pos += b.length;
|
|
2553
|
+
}
|
|
2554
|
+
return res;
|
|
2555
|
+
},
|
|
2556
|
+
decode: (a) => {
|
|
2557
|
+
abytes(a, bytesLen);
|
|
2558
|
+
const r = [];
|
|
2559
|
+
for (let i = 0; i < a.length; i += coder.bytesLen)
|
|
2560
|
+
r.push(coder.decode(a.subarray(i, i + coder.bytesLen)));
|
|
2561
|
+
return r;
|
|
2562
|
+
}
|
|
2563
|
+
};
|
|
2564
|
+
}
|
|
2565
|
+
function cleanBytes(...list) {
|
|
2566
|
+
for (const t of list) {
|
|
2567
|
+
if (Array.isArray(t))
|
|
2568
|
+
for (const b of t)
|
|
2569
|
+
b.fill(0);
|
|
2570
|
+
else
|
|
2571
|
+
t.fill(0);
|
|
2572
|
+
}
|
|
2573
|
+
}
|
|
2574
|
+
function getMask(bits) {
|
|
2575
|
+
if (!Number.isSafeInteger(bits) || bits < 0 || bits > 32)
|
|
2576
|
+
throw new RangeError(`expected bits in [0..32], got ${bits}`);
|
|
2577
|
+
return bits === 32 ? 4294967295 : ~(-1 << bits) >>> 0;
|
|
2578
|
+
}
|
|
2579
|
+
var abytesDoc, randomBytes3;
|
|
2580
|
+
var init_utils3 = __esm({
|
|
2581
|
+
"../../../noble-install/node_modules/@noble/post-quantum/utils.js"() {
|
|
2582
|
+
"use strict";
|
|
2583
|
+
init_utils();
|
|
2584
|
+
abytesDoc = abytes;
|
|
2585
|
+
randomBytes3 = randomBytes;
|
|
2586
|
+
}
|
|
2587
|
+
});
|
|
2588
|
+
|
|
2589
|
+
// ../../../noble-install/node_modules/@noble/post-quantum/_crystals.js
|
|
2590
|
+
var genCrystals, createXofShake, XOF128;
|
|
2591
|
+
var init_crystals = __esm({
|
|
2592
|
+
"../../../noble-install/node_modules/@noble/post-quantum/_crystals.js"() {
|
|
2593
|
+
"use strict";
|
|
2594
|
+
init_fft();
|
|
2595
|
+
init_sha3();
|
|
2596
|
+
init_utils3();
|
|
2597
|
+
genCrystals = (opts2) => {
|
|
2598
|
+
const { newPoly, N: N2, Q: Q2, F: F2, ROOT_OF_UNITY: ROOT_OF_UNITY2, brvBits, isKyber } = opts2;
|
|
2599
|
+
const mod2 = (a, modulo = Q2) => {
|
|
2600
|
+
const result = a % modulo | 0;
|
|
2601
|
+
return (result >= 0 ? result | 0 : modulo + result | 0) | 0;
|
|
2602
|
+
};
|
|
2603
|
+
const smod = (a, modulo = Q2) => {
|
|
2604
|
+
const r = mod2(a, modulo) | 0;
|
|
2605
|
+
return (r > modulo >> 1 ? r - modulo | 0 : r) | 0;
|
|
2606
|
+
};
|
|
2607
|
+
function getZettas() {
|
|
2608
|
+
const out = newPoly(N2);
|
|
2609
|
+
for (let i = 0; i < N2; i++) {
|
|
2610
|
+
const b = reverseBits(i, brvBits);
|
|
2611
|
+
const p = BigInt(ROOT_OF_UNITY2) ** BigInt(b) % BigInt(Q2);
|
|
2612
|
+
out[i] = Number(p) | 0;
|
|
2613
|
+
}
|
|
2614
|
+
return out;
|
|
2615
|
+
}
|
|
2616
|
+
const nttZetas = getZettas();
|
|
2617
|
+
const field = {
|
|
2618
|
+
add: (a, b) => mod2((a | 0) + (b | 0)) | 0,
|
|
2619
|
+
sub: (a, b) => mod2((a | 0) - (b | 0)) | 0,
|
|
2620
|
+
mul: (a, b) => mod2((a | 0) * (b | 0)) | 0,
|
|
2621
|
+
inv: (_a) => {
|
|
2622
|
+
throw new Error("not implemented");
|
|
2623
|
+
}
|
|
2624
|
+
};
|
|
2625
|
+
const nttOpts = {
|
|
2626
|
+
N: N2,
|
|
2627
|
+
roots: nttZetas,
|
|
2628
|
+
invertButterflies: true,
|
|
2629
|
+
skipStages: isKyber ? 1 : 0,
|
|
2630
|
+
brp: false
|
|
2631
|
+
};
|
|
2632
|
+
const dif = FFTCore(field, { dit: false, ...nttOpts });
|
|
2633
|
+
const dit = FFTCore(field, { dit: true, ...nttOpts });
|
|
2634
|
+
const NTT = {
|
|
2635
|
+
encode: (r) => {
|
|
2636
|
+
return dif(r);
|
|
2637
|
+
},
|
|
2638
|
+
decode: (r) => {
|
|
2639
|
+
dit(r);
|
|
2640
|
+
for (let i = 0; i < r.length; i++)
|
|
2641
|
+
r[i] = mod2(F2 * r[i]);
|
|
2642
|
+
return r;
|
|
2643
|
+
}
|
|
2644
|
+
};
|
|
2645
|
+
const bitsCoder = (d, c) => {
|
|
2646
|
+
const mask = getMask(d);
|
|
2647
|
+
const bytesLen = d * (N2 / 8);
|
|
2648
|
+
return {
|
|
2649
|
+
bytesLen,
|
|
2650
|
+
encode: (poly_) => {
|
|
2651
|
+
const poly = poly_;
|
|
2652
|
+
const r = new Uint8Array(bytesLen);
|
|
2653
|
+
for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < poly.length; i++) {
|
|
2654
|
+
buf |= (c.encode(poly[i]) & mask) << bufLen;
|
|
2655
|
+
bufLen += d;
|
|
2656
|
+
for (; bufLen >= 8; bufLen -= 8, buf >>= 8)
|
|
2657
|
+
r[pos++] = buf & getMask(bufLen);
|
|
2658
|
+
}
|
|
2659
|
+
return r;
|
|
2660
|
+
},
|
|
2661
|
+
decode: (bytes) => {
|
|
2662
|
+
const r = newPoly(N2);
|
|
2663
|
+
for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < bytes.length; i++) {
|
|
2664
|
+
buf |= bytes[i] << bufLen;
|
|
2665
|
+
bufLen += 8;
|
|
2666
|
+
for (; bufLen >= d; bufLen -= d, buf >>= d)
|
|
2667
|
+
r[pos++] = c.decode(buf & mask);
|
|
2668
|
+
}
|
|
2669
|
+
return r;
|
|
2670
|
+
}
|
|
2671
|
+
};
|
|
2672
|
+
};
|
|
2673
|
+
return {
|
|
2674
|
+
mod: mod2,
|
|
2675
|
+
smod,
|
|
2676
|
+
nttZetas,
|
|
2677
|
+
NTT: {
|
|
2678
|
+
encode: (r) => NTT.encode(r),
|
|
2679
|
+
decode: (r) => NTT.decode(r)
|
|
2680
|
+
},
|
|
2681
|
+
bitsCoder
|
|
2682
|
+
};
|
|
2683
|
+
};
|
|
2684
|
+
createXofShake = (shake) => (seed, blockLen) => {
|
|
2685
|
+
if (!blockLen)
|
|
2686
|
+
blockLen = shake.blockLen;
|
|
2687
|
+
const _seed = new Uint8Array(seed.length + 2);
|
|
2688
|
+
_seed.set(seed);
|
|
2689
|
+
const seedLen = seed.length;
|
|
2690
|
+
const buf = new Uint8Array(blockLen);
|
|
2691
|
+
let h = shake.create({});
|
|
2692
|
+
let calls = 0;
|
|
2693
|
+
let xofs = 0;
|
|
2694
|
+
return {
|
|
2695
|
+
stats: () => ({ calls, xofs }),
|
|
2696
|
+
get: (x, y) => {
|
|
2697
|
+
_seed[seedLen + 0] = x;
|
|
2698
|
+
_seed[seedLen + 1] = y;
|
|
2699
|
+
h.destroy();
|
|
2700
|
+
h = shake.create({}).update(_seed);
|
|
2701
|
+
calls++;
|
|
2702
|
+
return () => {
|
|
2703
|
+
xofs++;
|
|
2704
|
+
return h.xofInto(buf);
|
|
2705
|
+
};
|
|
2706
|
+
},
|
|
2707
|
+
clean: () => {
|
|
2708
|
+
h.destroy();
|
|
2709
|
+
cleanBytes(buf, _seed);
|
|
2710
|
+
}
|
|
2711
|
+
};
|
|
2712
|
+
};
|
|
2713
|
+
XOF128 = /* @__PURE__ */ createXofShake(shake128);
|
|
2714
|
+
}
|
|
2715
|
+
});
|
|
2716
|
+
|
|
2717
|
+
// ../../../noble-install/node_modules/@noble/post-quantum/ml-kem.js
|
|
2718
|
+
function polyAdd(a_, b_) {
|
|
2719
|
+
const a = a_;
|
|
2720
|
+
const b = b_;
|
|
2721
|
+
for (let i = 0; i < N; i++)
|
|
2722
|
+
a[i] = crystals.mod(a[i] + b[i]);
|
|
2723
|
+
}
|
|
2724
|
+
function polySub(a_, b_) {
|
|
2725
|
+
const a = a_;
|
|
2726
|
+
const b = b_;
|
|
2727
|
+
for (let i = 0; i < N; i++)
|
|
2728
|
+
a[i] = crystals.mod(a[i] - b[i]);
|
|
2729
|
+
}
|
|
2730
|
+
function BaseCaseMultiply(a0, a1, b0, b1, zeta) {
|
|
2731
|
+
const c0 = crystals.mod(a1 * b1 * zeta + a0 * b0);
|
|
2732
|
+
const c1 = crystals.mod(a0 * b1 + a1 * b0);
|
|
2733
|
+
return { c0, c1 };
|
|
2734
|
+
}
|
|
2735
|
+
function MultiplyNTTs(f_, g_) {
|
|
2736
|
+
const f = f_;
|
|
2737
|
+
const g = g_;
|
|
2738
|
+
for (let i = 0; i < N / 2; i++) {
|
|
2739
|
+
let z = crystals.nttZetas[64 + (i >> 1)];
|
|
2740
|
+
if (i & 1)
|
|
2741
|
+
z = -z;
|
|
2742
|
+
const { c0, c1 } = BaseCaseMultiply(f[2 * i + 0], f[2 * i + 1], g[2 * i + 0], g[2 * i + 1], z);
|
|
2743
|
+
f[2 * i + 0] = c0;
|
|
2744
|
+
f[2 * i + 1] = c1;
|
|
2745
|
+
}
|
|
2746
|
+
return f;
|
|
2747
|
+
}
|
|
2748
|
+
function SampleNTT(xof_) {
|
|
2749
|
+
const xof = xof_;
|
|
2750
|
+
const r = new Uint16Array(N);
|
|
2751
|
+
for (let j = 0; j < N; ) {
|
|
2752
|
+
const b = xof();
|
|
2753
|
+
if (b.length % 3)
|
|
2754
|
+
throw new Error("SampleNTT: unaligned block");
|
|
2755
|
+
for (let i = 0; j < N && i + 3 <= b.length; i += 3) {
|
|
2756
|
+
const d1 = (b[i + 0] >> 0 | b[i + 1] << 8) & 4095;
|
|
2757
|
+
const d2 = (b[i + 1] >> 4 | b[i + 2] << 4) & 4095;
|
|
2758
|
+
if (d1 < Q)
|
|
2759
|
+
r[j++] = d1;
|
|
2760
|
+
if (j < N && d2 < Q)
|
|
2761
|
+
r[j++] = d2;
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
return r;
|
|
2765
|
+
}
|
|
2766
|
+
function sampleCBD(PRF_, seed, nonce, eta) {
|
|
2767
|
+
const PRF = PRF_;
|
|
2768
|
+
return sampleCBDBytes(PRF(eta * N / 4, seed, nonce), eta);
|
|
2769
|
+
}
|
|
2770
|
+
function createKyber(opts2) {
|
|
2771
|
+
const rawOpts = opts2;
|
|
2772
|
+
const KPKE = genKPKE(rawOpts);
|
|
2773
|
+
const { HASH256, HASH512, KDF } = rawOpts;
|
|
2774
|
+
const { secretCoder: KPKESecretCoder, lengths } = KPKE;
|
|
2775
|
+
const secretCoder = splitCoder("secretKey", lengths.secretKey, lengths.publicKey, 32, 32);
|
|
2776
|
+
const msgLen = 32;
|
|
2777
|
+
const seedLen = 64;
|
|
2778
|
+
const kemLengths = Object.freeze({
|
|
2779
|
+
...lengths,
|
|
2780
|
+
seed: 64,
|
|
2781
|
+
msg: msgLen,
|
|
2782
|
+
msgRand: msgLen,
|
|
2783
|
+
secretKey: secretCoder.bytesLen
|
|
2784
|
+
});
|
|
2785
|
+
return Object.freeze({
|
|
2786
|
+
info: Object.freeze({ type: "ml-kem" }),
|
|
2787
|
+
lengths: kemLengths,
|
|
2788
|
+
keygen: (seed = randomBytes3(seedLen)) => {
|
|
2789
|
+
abytesDoc(seed, seedLen, "seed");
|
|
2790
|
+
const { publicKey, secretKey: sk } = KPKE.keygen(seed.subarray(0, 32));
|
|
2791
|
+
const publicKeyHash = HASH256(publicKey);
|
|
2792
|
+
const secretKey = secretCoder.encode([sk, publicKey, publicKeyHash, seed.subarray(32)]);
|
|
2793
|
+
cleanBytes(sk, publicKeyHash);
|
|
2794
|
+
return {
|
|
2795
|
+
publicKey,
|
|
2796
|
+
secretKey
|
|
2797
|
+
};
|
|
2798
|
+
},
|
|
2799
|
+
getPublicKey: (secretKey) => {
|
|
2800
|
+
const [_sk, publicKey, _publicKeyHash, _z] = secretCoder.decode(secretKey);
|
|
2801
|
+
return Uint8Array.from(publicKey);
|
|
2802
|
+
},
|
|
2803
|
+
encapsulate: (publicKey, msg = randomBytes3(msgLen)) => {
|
|
2804
|
+
abytesDoc(publicKey, lengths.publicKey, "publicKey");
|
|
2805
|
+
abytesDoc(msg, msgLen, "message");
|
|
2806
|
+
const eke = publicKey.subarray(0, 384 * opts2.K);
|
|
2807
|
+
const ek = KPKESecretCoder.encode(KPKESecretCoder.decode(copyBytes2(eke)));
|
|
2808
|
+
if (!equalBytes2(ek, eke)) {
|
|
2809
|
+
cleanBytes(ek);
|
|
2810
|
+
throw new Error("ML-KEM.encapsulate: wrong publicKey modulus");
|
|
2811
|
+
}
|
|
2812
|
+
cleanBytes(ek);
|
|
2813
|
+
const kr = HASH512.create().update(msg).update(HASH256(publicKey)).digest();
|
|
2814
|
+
const cipherText = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
|
|
2815
|
+
cleanBytes(kr.subarray(32));
|
|
2816
|
+
return {
|
|
2817
|
+
cipherText,
|
|
2818
|
+
sharedSecret: kr.subarray(0, 32)
|
|
2819
|
+
};
|
|
2820
|
+
},
|
|
2821
|
+
decapsulate: (cipherText, secretKey) => {
|
|
2822
|
+
abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
|
|
2823
|
+
abytesDoc(cipherText, lengths.cipherText, "cipherText");
|
|
2824
|
+
const k768 = secretCoder.bytesLen - 96;
|
|
2825
|
+
const start = k768 + 32;
|
|
2826
|
+
const test = HASH256(secretKey.subarray(k768 / 2, start));
|
|
2827
|
+
if (!equalBytes2(test, secretKey.subarray(start, start + 32)))
|
|
2828
|
+
throw new Error("invalid secretKey: hash check failed");
|
|
2829
|
+
const [sk, publicKey, publicKeyHash, z] = secretCoder.decode(secretKey);
|
|
2830
|
+
const msg = KPKE.decrypt(cipherText, sk);
|
|
2831
|
+
const kr = HASH512.create().update(msg).update(publicKeyHash).digest();
|
|
2832
|
+
const Khat = kr.subarray(0, 32);
|
|
2833
|
+
const cipherText2 = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
|
|
2834
|
+
const isValid = equalBytes2(cipherText, cipherText2);
|
|
2835
|
+
const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
|
|
2836
|
+
cleanBytes(msg, cipherText2, !isValid ? Khat : Kbar);
|
|
2837
|
+
return isValid ? Khat : Kbar;
|
|
2838
|
+
}
|
|
2839
|
+
});
|
|
2840
|
+
}
|
|
2841
|
+
function shakePRF(dkLen, key, nonce) {
|
|
2842
|
+
return shake256.create({ dkLen }).update(key).update(new Uint8Array([nonce])).digest();
|
|
2843
|
+
}
|
|
2844
|
+
var N, Q, F, ROOT_OF_UNITY, crystals, PARAMS, compress, byteCoder, polyCoder, sampleCBDBytes, genKPKE, opts, mk, ml_kem768;
|
|
2845
|
+
var init_ml_kem = __esm({
|
|
2846
|
+
"../../../noble-install/node_modules/@noble/post-quantum/ml-kem.js"() {
|
|
2847
|
+
"use strict";
|
|
2848
|
+
init_sha3();
|
|
2849
|
+
init_utils();
|
|
2850
|
+
init_crystals();
|
|
2851
|
+
init_utils3();
|
|
2852
|
+
N = 256;
|
|
2853
|
+
Q = 3329;
|
|
2854
|
+
F = 3303;
|
|
2855
|
+
ROOT_OF_UNITY = 17;
|
|
2856
|
+
crystals = /* @__PURE__ */ genCrystals({
|
|
2857
|
+
N,
|
|
2858
|
+
Q,
|
|
2859
|
+
F,
|
|
2860
|
+
ROOT_OF_UNITY,
|
|
2861
|
+
newPoly: (n) => new Uint16Array(n),
|
|
2862
|
+
brvBits: 7,
|
|
2863
|
+
isKyber: true
|
|
2864
|
+
});
|
|
2865
|
+
PARAMS = /* @__PURE__ */ (() => Object.freeze({
|
|
2866
|
+
512: Object.freeze({ N, Q, K: 2, ETA1: 3, ETA2: 2, du: 10, dv: 4, RBGstrength: 128 }),
|
|
2867
|
+
768: Object.freeze({ N, Q, K: 3, ETA1: 2, ETA2: 2, du: 10, dv: 4, RBGstrength: 192 }),
|
|
2868
|
+
1024: Object.freeze({ N, Q, K: 4, ETA1: 2, ETA2: 2, du: 11, dv: 5, RBGstrength: 256 })
|
|
2869
|
+
}))();
|
|
2870
|
+
compress = (d) => {
|
|
2871
|
+
if (d >= 12)
|
|
2872
|
+
return { encode: (i) => i, decode: (i) => i >= Q ? i - Q : i };
|
|
2873
|
+
const a = 2 ** (d - 1);
|
|
2874
|
+
return {
|
|
2875
|
+
// This only matches standalone Compress_d after bitsCoder masks the result into Z_(2^d).
|
|
2876
|
+
encode: (i) => ((i << d) + Q / 2) / Q,
|
|
2877
|
+
// const decompress = (i: number) => round((Q / 2 ** d) * i);
|
|
2878
|
+
decode: (i) => i * Q + a >>> d
|
|
2879
|
+
};
|
|
2880
|
+
};
|
|
2881
|
+
byteCoder = (d) => crystals.bitsCoder(d, d === 12 ? { encode: (i) => i, decode: (i) => i >= Q ? i - Q : i } : { encode: (i) => i, decode: (i) => i });
|
|
2882
|
+
polyCoder = (d) => d === 12 ? byteCoder(12) : crystals.bitsCoder(d, compress(d));
|
|
2883
|
+
sampleCBDBytes = (buf, eta) => {
|
|
2884
|
+
const r = new Uint16Array(N);
|
|
2885
|
+
const b32 = u32(buf);
|
|
2886
|
+
swap32IfBE(b32);
|
|
2887
|
+
let len = 0;
|
|
2888
|
+
for (let i = 0, p = 0, bb = 0, t0 = 0; i < b32.length; i++) {
|
|
2889
|
+
let b = b32[i];
|
|
2890
|
+
for (let j = 0; j < 32; j++) {
|
|
2891
|
+
bb += b & 1;
|
|
2892
|
+
b >>= 1;
|
|
2893
|
+
len += 1;
|
|
2894
|
+
if (len === eta) {
|
|
2895
|
+
t0 = bb;
|
|
2896
|
+
bb = 0;
|
|
2897
|
+
} else if (len === 2 * eta) {
|
|
2898
|
+
r[p++] = crystals.mod(t0 - bb);
|
|
2899
|
+
bb = 0;
|
|
2900
|
+
len = 0;
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
2903
|
+
}
|
|
2904
|
+
swap32IfBE(b32);
|
|
2905
|
+
if (len)
|
|
2906
|
+
throw new Error(`sampleCBD: leftover bits: ${len}`);
|
|
2907
|
+
return r;
|
|
2908
|
+
};
|
|
2909
|
+
genKPKE = (opts_) => {
|
|
2910
|
+
const opts2 = opts_;
|
|
2911
|
+
const { K, PRF, XOF, HASH512, ETA1, ETA2, du, dv } = opts2;
|
|
2912
|
+
const poly1 = polyCoder(1);
|
|
2913
|
+
const polyV = polyCoder(dv);
|
|
2914
|
+
const polyU = polyCoder(du);
|
|
2915
|
+
const publicCoder = splitCoder("publicKey", vecCoder(polyCoder(12), K), 32);
|
|
2916
|
+
const secretCoder = vecCoder(polyCoder(12), K);
|
|
2917
|
+
const cipherCoder = splitCoder("ciphertext", vecCoder(polyU, K), polyV);
|
|
2918
|
+
const seedCoder = splitCoder("seed", 32, 32);
|
|
2919
|
+
return {
|
|
2920
|
+
secretCoder,
|
|
2921
|
+
lengths: {
|
|
2922
|
+
secretKey: secretCoder.bytesLen,
|
|
2923
|
+
publicKey: publicCoder.bytesLen,
|
|
2924
|
+
cipherText: cipherCoder.bytesLen
|
|
2925
|
+
},
|
|
2926
|
+
keygen: (seed) => {
|
|
2927
|
+
abytesDoc(seed, 32, "seed");
|
|
2928
|
+
const seedDst = new Uint8Array(33);
|
|
2929
|
+
seedDst.set(seed);
|
|
2930
|
+
seedDst[32] = K;
|
|
2931
|
+
const seedHash = HASH512(seedDst);
|
|
2932
|
+
const [rho, sigma] = seedCoder.decode(seedHash);
|
|
2933
|
+
const sHat = [];
|
|
2934
|
+
const tHat = [];
|
|
2935
|
+
for (let i = 0; i < K; i++)
|
|
2936
|
+
sHat.push(crystals.NTT.encode(sampleCBD(PRF, sigma, i, ETA1)));
|
|
2937
|
+
const x = XOF(rho);
|
|
2938
|
+
for (let i = 0; i < K; i++) {
|
|
2939
|
+
const e = crystals.NTT.encode(sampleCBD(PRF, sigma, K + i, ETA1));
|
|
2940
|
+
for (let j = 0; j < K; j++) {
|
|
2941
|
+
const aji = SampleNTT(x.get(j, i));
|
|
2942
|
+
polyAdd(e, MultiplyNTTs(aji, sHat[j]));
|
|
2943
|
+
}
|
|
2944
|
+
tHat.push(e);
|
|
2945
|
+
}
|
|
2946
|
+
x.clean();
|
|
2947
|
+
const res = {
|
|
2948
|
+
publicKey: publicCoder.encode([tHat, rho]),
|
|
2949
|
+
secretKey: secretCoder.encode(sHat)
|
|
2950
|
+
};
|
|
2951
|
+
cleanBytes(rho, sigma, sHat, tHat, seedDst, seedHash);
|
|
2952
|
+
return res;
|
|
2953
|
+
},
|
|
2954
|
+
encrypt: (publicKey, msg, seed) => {
|
|
2955
|
+
const [tHat, rho] = publicCoder.decode(publicKey);
|
|
2956
|
+
const rHat = [];
|
|
2957
|
+
for (let i = 0; i < K; i++)
|
|
2958
|
+
rHat.push(crystals.NTT.encode(sampleCBD(PRF, seed, i, ETA1)));
|
|
2959
|
+
const x = XOF(rho);
|
|
2960
|
+
const tmp2 = new Uint16Array(N);
|
|
2961
|
+
const u = [];
|
|
2962
|
+
for (let i = 0; i < K; i++) {
|
|
2963
|
+
const e1 = sampleCBD(PRF, seed, K + i, ETA2);
|
|
2964
|
+
const tmp = new Uint16Array(N);
|
|
2965
|
+
for (let j = 0; j < K; j++) {
|
|
2966
|
+
const aij = SampleNTT(x.get(i, j));
|
|
2967
|
+
polyAdd(tmp, MultiplyNTTs(aij, rHat[j]));
|
|
2968
|
+
}
|
|
2969
|
+
polyAdd(e1, crystals.NTT.decode(tmp));
|
|
2970
|
+
u.push(e1);
|
|
2971
|
+
polyAdd(tmp2, MultiplyNTTs(tHat[i], rHat[i]));
|
|
2972
|
+
cleanBytes(tmp);
|
|
2973
|
+
}
|
|
2974
|
+
x.clean();
|
|
2975
|
+
const e2 = sampleCBD(PRF, seed, 2 * K, ETA2);
|
|
2976
|
+
polyAdd(e2, crystals.NTT.decode(tmp2));
|
|
2977
|
+
const v = poly1.decode(msg);
|
|
2978
|
+
polyAdd(v, e2);
|
|
2979
|
+
cleanBytes(tHat, rHat, tmp2, e2);
|
|
2980
|
+
return cipherCoder.encode([u, v]);
|
|
2981
|
+
},
|
|
2982
|
+
decrypt: (cipherText, privateKey) => {
|
|
2983
|
+
const [u, v] = cipherCoder.decode(cipherText);
|
|
2984
|
+
const sk = secretCoder.decode(privateKey);
|
|
2985
|
+
const tmp = new Uint16Array(N);
|
|
2986
|
+
for (let i = 0; i < K; i++)
|
|
2987
|
+
polyAdd(tmp, MultiplyNTTs(sk[i], crystals.NTT.encode(u[i])));
|
|
2988
|
+
polySub(v, crystals.NTT.decode(tmp));
|
|
2989
|
+
cleanBytes(tmp, sk, u);
|
|
2990
|
+
return poly1.encode(v);
|
|
2991
|
+
}
|
|
2992
|
+
};
|
|
2993
|
+
};
|
|
2994
|
+
opts = /* @__PURE__ */ (() => ({
|
|
2995
|
+
HASH256: sha3_256,
|
|
2996
|
+
HASH512: sha3_512,
|
|
2997
|
+
KDF: shake256,
|
|
2998
|
+
XOF: XOF128,
|
|
2999
|
+
PRF: shakePRF
|
|
3000
|
+
}))();
|
|
3001
|
+
mk = (params) => createKyber({
|
|
3002
|
+
...opts,
|
|
3003
|
+
...params
|
|
3004
|
+
});
|
|
3005
|
+
ml_kem768 = /* @__PURE__ */ (() => mk(PARAMS[768]))();
|
|
3006
|
+
}
|
|
3007
|
+
});
|
|
3008
|
+
|
|
3009
|
+
// ../../../noble-install/node_modules/@noble/post-quantum/hybrid.js
|
|
3010
|
+
function ecKeygen(curve, allowZeroKey = false) {
|
|
3011
|
+
const lengths = curve.lengths;
|
|
3012
|
+
let keygen = curve.keygen;
|
|
3013
|
+
if (allowZeroKey) {
|
|
3014
|
+
if (!("getSharedSecret" in curve && "sign" in curve && "verify" in curve))
|
|
3015
|
+
throw new Error("allowZeroKey requires a Weierstrass curve");
|
|
3016
|
+
const wCurve = curve;
|
|
3017
|
+
const Fn2 = wCurve.Point.Fn;
|
|
3018
|
+
keygen = (seed = randomBytes3(lengths.seed)) => {
|
|
3019
|
+
abytes(seed, lengths.seed, "seed");
|
|
3020
|
+
const seedScalar = Fn2.isLE ? bytesToNumberLE(seed) : bytesToNumberBE(seed);
|
|
3021
|
+
const secretKey = Fn2.toBytes(Fn2.create(seedScalar));
|
|
3022
|
+
return {
|
|
3023
|
+
secretKey,
|
|
3024
|
+
publicKey: curve.getPublicKey(secretKey)
|
|
3025
|
+
};
|
|
3026
|
+
};
|
|
3027
|
+
}
|
|
3028
|
+
return {
|
|
3029
|
+
lengths: { secretKey: lengths.secretKey, publicKey: lengths.publicKey, seed: lengths.seed },
|
|
3030
|
+
keygen: (seed) => keygen(seed),
|
|
3031
|
+
getPublicKey: (secretKey) => curve.getPublicKey(secretKey)
|
|
3032
|
+
};
|
|
3033
|
+
}
|
|
3034
|
+
function ecdhKem(curve, allowZeroKey = false) {
|
|
3035
|
+
const kg = ecKeygen(curve, allowZeroKey);
|
|
3036
|
+
if (!curve.getSharedSecret)
|
|
3037
|
+
throw new Error("wrong curve");
|
|
3038
|
+
return {
|
|
3039
|
+
lengths: { ...kg.lengths, msg: kg.lengths.seed, cipherText: kg.lengths.publicKey },
|
|
3040
|
+
keygen: kg.keygen,
|
|
3041
|
+
getPublicKey: kg.getPublicKey,
|
|
3042
|
+
encapsulate(publicKey, rand = randomBytes3(curve.lengths.seed)) {
|
|
3043
|
+
const seed = copyBytes2(rand);
|
|
3044
|
+
let ek = void 0;
|
|
3045
|
+
try {
|
|
3046
|
+
ek = this.keygen(seed).secretKey;
|
|
3047
|
+
const sharedSecret = this.decapsulate(publicKey, ek);
|
|
3048
|
+
const cipherText = curve.getPublicKey(ek);
|
|
3049
|
+
return { sharedSecret, cipherText };
|
|
3050
|
+
} finally {
|
|
3051
|
+
cleanBytes(seed);
|
|
3052
|
+
if (ek)
|
|
3053
|
+
cleanBytes(ek);
|
|
3054
|
+
}
|
|
3055
|
+
},
|
|
3056
|
+
decapsulate(cipherText, secretKey) {
|
|
3057
|
+
const res = curve.getSharedSecret(secretKey, cipherText);
|
|
3058
|
+
return curve.lengths.publicKeyHasPrefix ? res.subarray(1) : res;
|
|
3059
|
+
}
|
|
3060
|
+
};
|
|
3061
|
+
}
|
|
3062
|
+
function splitLengths(lst, name) {
|
|
3063
|
+
return splitCoder(name, ...lst.map((i) => {
|
|
3064
|
+
if (typeof i.lengths[name] !== "number")
|
|
3065
|
+
throw new Error("wrong length: " + name);
|
|
3066
|
+
return i.lengths[name];
|
|
3067
|
+
}));
|
|
3068
|
+
}
|
|
3069
|
+
function expandSeedXof(xof) {
|
|
3070
|
+
return ((seed, seedLen) => xof(seed, { dkLen: seedLen }));
|
|
3071
|
+
}
|
|
3072
|
+
function combineKeys(realSeedLen, expandSeed_, ...ck_) {
|
|
3073
|
+
const expandSeed = expandSeed_;
|
|
3074
|
+
const ck = ck_;
|
|
3075
|
+
const seedCoder = splitLengths(ck, "seed");
|
|
3076
|
+
const pkCoder = splitLengths(ck, "publicKey");
|
|
3077
|
+
if (realSeedLen === void 0)
|
|
3078
|
+
realSeedLen = seedCoder.bytesLen;
|
|
3079
|
+
anumber(realSeedLen);
|
|
3080
|
+
function expandDecapsulationKey(seed) {
|
|
3081
|
+
abytes(seed, realSeedLen);
|
|
3082
|
+
const expandedRaw = expandSeed(seed, seedCoder.bytesLen);
|
|
3083
|
+
const expandedSeed = expandedRaw.buffer === seed.buffer ? copyBytes2(expandedRaw) : expandedRaw;
|
|
3084
|
+
const expanded = [];
|
|
3085
|
+
const keySecret = [];
|
|
3086
|
+
const secretKey = [];
|
|
3087
|
+
const publicKey = [];
|
|
3088
|
+
let ok = false;
|
|
3089
|
+
try {
|
|
3090
|
+
for (const part of seedCoder.decode(expandedSeed))
|
|
3091
|
+
expanded.push(copyBytes2(part));
|
|
3092
|
+
for (let i = 0; i < ck.length; i++) {
|
|
3093
|
+
const keys = ck[i].keygen(expanded[i]);
|
|
3094
|
+
keySecret.push(keys.secretKey);
|
|
3095
|
+
secretKey.push(copyBytes2(keys.secretKey));
|
|
3096
|
+
publicKey.push(keys.publicKey);
|
|
3097
|
+
}
|
|
3098
|
+
ok = true;
|
|
3099
|
+
return { secretKey, publicKey };
|
|
3100
|
+
} finally {
|
|
3101
|
+
cleanBytes(expandedSeed, expanded, keySecret);
|
|
3102
|
+
if (!ok)
|
|
3103
|
+
cleanBytes(secretKey);
|
|
3104
|
+
}
|
|
3105
|
+
}
|
|
3106
|
+
return {
|
|
3107
|
+
info: { lengths: { seed: realSeedLen, publicKey: pkCoder.bytesLen, secretKey: realSeedLen } },
|
|
3108
|
+
getPublicKey(secretKey) {
|
|
3109
|
+
return this.keygen(secretKey).publicKey;
|
|
3110
|
+
},
|
|
3111
|
+
keygen(seed = randomBytes3(realSeedLen)) {
|
|
3112
|
+
const { publicKey: pk, secretKey } = expandDecapsulationKey(seed);
|
|
3113
|
+
try {
|
|
3114
|
+
const publicKey = pkCoder.encode(pk);
|
|
3115
|
+
return { secretKey: seed, publicKey };
|
|
3116
|
+
} finally {
|
|
3117
|
+
cleanBytes(pk);
|
|
3118
|
+
cleanBytes(secretKey);
|
|
3119
|
+
}
|
|
3120
|
+
},
|
|
3121
|
+
expandDecapsulationKey,
|
|
3122
|
+
realSeedLen
|
|
3123
|
+
};
|
|
3124
|
+
}
|
|
3125
|
+
function combineKEMS(realSeedLen, realMsgLen, expandSeed, combiner, ...kems) {
|
|
3126
|
+
const rawCombiner = combiner;
|
|
3127
|
+
const rawKems = kems;
|
|
3128
|
+
const keys = combineKeys(realSeedLen, expandSeed, ...rawKems);
|
|
3129
|
+
const ctCoder = splitLengths(rawKems, "cipherText");
|
|
3130
|
+
const pkCoder = splitLengths(rawKems, "publicKey");
|
|
3131
|
+
const msgCoder = splitLengths(rawKems, "msg");
|
|
3132
|
+
if (realMsgLen === void 0)
|
|
3133
|
+
realMsgLen = msgCoder.bytesLen;
|
|
3134
|
+
anumber(realMsgLen);
|
|
3135
|
+
const lengths = Object.freeze({
|
|
3136
|
+
...keys.info.lengths,
|
|
3137
|
+
msg: realMsgLen,
|
|
3138
|
+
msgRand: msgCoder.bytesLen,
|
|
3139
|
+
cipherText: ctCoder.bytesLen
|
|
3140
|
+
});
|
|
3141
|
+
return Object.freeze({
|
|
3142
|
+
lengths,
|
|
3143
|
+
getPublicKey: keys.getPublicKey,
|
|
3144
|
+
keygen: keys.keygen,
|
|
3145
|
+
encapsulate(pk, randomness = randomBytes3(msgCoder.bytesLen)) {
|
|
3146
|
+
const pks = pkCoder.decode(pk);
|
|
3147
|
+
const rand = msgCoder.decode(randomness);
|
|
3148
|
+
const sharedSecret = [];
|
|
3149
|
+
const cipherText = [];
|
|
3150
|
+
try {
|
|
3151
|
+
for (let i = 0; i < rawKems.length; i++) {
|
|
3152
|
+
const enc = rawKems[i].encapsulate(pks[i], rand[i]);
|
|
3153
|
+
sharedSecret.push(enc.sharedSecret);
|
|
3154
|
+
cipherText.push(enc.cipherText);
|
|
3155
|
+
}
|
|
3156
|
+
return {
|
|
3157
|
+
// Detach the combiner result before cleanup: a caller-provided combiner may alias one of
|
|
3158
|
+
// the child sharedSecret buffers, and those child buffers are zeroized immediately below.
|
|
3159
|
+
sharedSecret: copyBytes2(rawCombiner(pks, cipherText, sharedSecret)),
|
|
3160
|
+
cipherText: ctCoder.encode(cipherText)
|
|
3161
|
+
};
|
|
3162
|
+
} finally {
|
|
3163
|
+
cleanBytes(sharedSecret, cipherText);
|
|
3164
|
+
}
|
|
3165
|
+
},
|
|
3166
|
+
decapsulate(ct, seed) {
|
|
3167
|
+
const cts = ctCoder.decode(ct);
|
|
3168
|
+
const { publicKey, secretKey } = keys.expandDecapsulationKey(seed);
|
|
3169
|
+
const sharedSecret = rawKems.map((i, j) => i.decapsulate(cts[j], secretKey[j]));
|
|
3170
|
+
try {
|
|
3171
|
+
return copyBytes2(rawCombiner(publicKey, cts, sharedSecret));
|
|
3172
|
+
} finally {
|
|
3173
|
+
cleanBytes(secretKey, sharedSecret);
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
});
|
|
3177
|
+
}
|
|
3178
|
+
var x25519kem, ml_kem768_x25519, XWing;
|
|
3179
|
+
var init_hybrid = __esm({
|
|
3180
|
+
"../../../noble-install/node_modules/@noble/post-quantum/hybrid.js"() {
|
|
3181
|
+
"use strict";
|
|
3182
|
+
init_ed25519();
|
|
3183
|
+
init_utils2();
|
|
3184
|
+
init_sha3();
|
|
3185
|
+
init_utils();
|
|
3186
|
+
init_ml_kem();
|
|
3187
|
+
init_utils3();
|
|
3188
|
+
x25519kem = /* @__PURE__ */ ecdhKem(x25519);
|
|
3189
|
+
ml_kem768_x25519 = /* @__PURE__ */ (() => combineKEMS(
|
|
3190
|
+
32,
|
|
3191
|
+
32,
|
|
3192
|
+
expandSeedXof(shake256),
|
|
3193
|
+
// Awesome label, so much escaping hell in a single line.
|
|
3194
|
+
(pk, ct, ss) => sha3_256(concatBytes2(ss[0], ss[1], ct[1], pk[1], asciiToBytes("\\.//^\\"))),
|
|
3195
|
+
ml_kem768,
|
|
3196
|
+
x25519kem
|
|
3197
|
+
))();
|
|
3198
|
+
XWing = /* @__PURE__ */ (() => ml_kem768_x25519)();
|
|
3199
|
+
}
|
|
3200
|
+
});
|
|
32
3201
|
|
|
33
3202
|
// ../crypto/src/replay-window.ts
|
|
34
3203
|
function isFreshTimestamp(timestampHeader, toleranceSecs = DEFAULT_REPLAY_WINDOW_SECS, nowSecs) {
|
|
@@ -51,7 +3220,7 @@ var init_replay_window = __esm({
|
|
|
51
3220
|
function isBlockedIPv4(host) {
|
|
52
3221
|
const parts = host.split(".");
|
|
53
3222
|
if (parts.length !== 4) return false;
|
|
54
|
-
const octets = parts.map((p) => /^\d{1,3}$/.test(p) ? Number(p) : NaN);
|
|
3223
|
+
const octets = parts.map((p) => /^\d{1,3}$/.test(p) ? Number(p) : Number.NaN);
|
|
55
3224
|
if (octets.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return false;
|
|
56
3225
|
const [a, b] = octets;
|
|
57
3226
|
if (a === 0) return true;
|
|
@@ -64,8 +3233,8 @@ function isBlockedIPv4(host) {
|
|
|
64
3233
|
return false;
|
|
65
3234
|
}
|
|
66
3235
|
function v4FromHexPair(highHex, lowHex) {
|
|
67
|
-
const high = parseInt(highHex, 16);
|
|
68
|
-
const low = parseInt(lowHex, 16);
|
|
3236
|
+
const high = Number.parseInt(highHex, 16);
|
|
3237
|
+
const low = Number.parseInt(lowHex, 16);
|
|
69
3238
|
if (!Number.isInteger(high) || !Number.isInteger(low)) return null;
|
|
70
3239
|
const a = high >> 8 & 255;
|
|
71
3240
|
const b = high & 255;
|
|
@@ -74,34 +3243,34 @@ function v4FromHexPair(highHex, lowHex) {
|
|
|
74
3243
|
return `${a}.${b}.${c}.${d}`;
|
|
75
3244
|
}
|
|
76
3245
|
function isBlockedIPv6(host) {
|
|
77
|
-
let
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (!
|
|
83
|
-
if (
|
|
84
|
-
if (
|
|
85
|
-
if (
|
|
86
|
-
if (
|
|
87
|
-
const mappedDotted = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(
|
|
3246
|
+
let clean2 = host.toLowerCase();
|
|
3247
|
+
if (clean2.startsWith("[") && clean2.endsWith("]")) {
|
|
3248
|
+
clean2 = clean2.slice(1, -1);
|
|
3249
|
+
}
|
|
3250
|
+
clean2 = clean2.split("%")[0];
|
|
3251
|
+
if (!clean2.includes(":")) return false;
|
|
3252
|
+
if (clean2 === "::1") return true;
|
|
3253
|
+
if (clean2 === "::") return true;
|
|
3254
|
+
if (clean2.startsWith("fe80:")) return true;
|
|
3255
|
+
if (clean2.startsWith("fc") || clean2.startsWith("fd")) return true;
|
|
3256
|
+
const mappedDotted = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(clean2);
|
|
88
3257
|
if (mappedDotted) return isBlockedIPv4(mappedDotted[1]);
|
|
89
|
-
const mappedHex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(
|
|
3258
|
+
const mappedHex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(clean2);
|
|
90
3259
|
if (mappedHex) {
|
|
91
3260
|
const v4 = v4FromHexPair(mappedHex[1], mappedHex[2]);
|
|
92
3261
|
if (v4) return isBlockedIPv4(v4);
|
|
93
3262
|
}
|
|
94
|
-
const compatibleDotted = /^::(\d+\.\d+\.\d+\.\d+)$/.exec(
|
|
3263
|
+
const compatibleDotted = /^::(\d+\.\d+\.\d+\.\d+)$/.exec(clean2);
|
|
95
3264
|
if (compatibleDotted) return isBlockedIPv4(compatibleDotted[1]);
|
|
96
|
-
const compatibleHex = /^::([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(
|
|
3265
|
+
const compatibleHex = /^::([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(clean2);
|
|
97
3266
|
if (compatibleHex) {
|
|
98
3267
|
const v4 = v4FromHexPair(compatibleHex[1], compatibleHex[2]);
|
|
99
3268
|
if (v4) return isBlockedIPv4(v4);
|
|
100
3269
|
}
|
|
101
|
-
if (
|
|
102
|
-
const nat64Dotted = /^64:ff9b:(?:1:)?:(\d+\.\d+\.\d+\.\d+)$/.exec(
|
|
3270
|
+
if (clean2.startsWith("64:ff9b:")) {
|
|
3271
|
+
const nat64Dotted = /^64:ff9b:(?:1:)?:(\d+\.\d+\.\d+\.\d+)$/.exec(clean2);
|
|
103
3272
|
if (nat64Dotted) return isBlockedIPv4(nat64Dotted[1]);
|
|
104
|
-
const nat64Hex = /^64:ff9b:(?:1:)?:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(
|
|
3273
|
+
const nat64Hex = /^64:ff9b:(?:1:)?:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(clean2);
|
|
105
3274
|
if (nat64Hex) {
|
|
106
3275
|
const v4 = v4FromHexPair(nat64Hex[1], nat64Hex[2]);
|
|
107
3276
|
if (v4) return isBlockedIPv4(v4);
|
|
@@ -110,7 +3279,7 @@ function isBlockedIPv6(host) {
|
|
|
110
3279
|
}
|
|
111
3280
|
return false;
|
|
112
3281
|
}
|
|
113
|
-
function isSafeWebhookUrl(url,
|
|
3282
|
+
function isSafeWebhookUrl(url, opts2 = {}) {
|
|
114
3283
|
let parsed;
|
|
115
3284
|
try {
|
|
116
3285
|
parsed = new URL(url);
|
|
@@ -120,7 +3289,7 @@ function isSafeWebhookUrl(url, opts = {}) {
|
|
|
120
3289
|
if (!["http:", "https:"].includes(parsed.protocol)) {
|
|
121
3290
|
return { ok: false, reason: `Protocol ${parsed.protocol} not allowed \u2014 use http(s)` };
|
|
122
3291
|
}
|
|
123
|
-
if (
|
|
3292
|
+
if (opts2.requireHttps && parsed.protocol !== "https:") {
|
|
124
3293
|
return { ok: false, reason: "Webhook URL must use https://" };
|
|
125
3294
|
}
|
|
126
3295
|
const host = parsed.hostname.toLowerCase();
|
|
@@ -157,16 +3326,22 @@ var init_safe_url = __esm({
|
|
|
157
3326
|
var src_exports = {};
|
|
158
3327
|
__export(src_exports, {
|
|
159
3328
|
DEFAULT_REPLAY_WINDOW_SECS: () => DEFAULT_REPLAY_WINDOW_SECS,
|
|
3329
|
+
computeBlindIndex: () => computeBlindIndex,
|
|
3330
|
+
computeCustomerBlindIndex: () => computeCustomerBlindIndex,
|
|
160
3331
|
decryptAddress: () => decryptAddress,
|
|
161
3332
|
decryptSession: () => decryptSession,
|
|
162
3333
|
encryptAddressForAll: () => encryptAddressForAll,
|
|
163
3334
|
encryptAddressForPartner: () => encryptAddressForPartner,
|
|
3335
|
+
encryptEnvelopeForPartner: () => encryptEnvelopeForPartner,
|
|
3336
|
+
encryptPayloadForPartner: () => encryptPayloadForPartner,
|
|
164
3337
|
encryptSessionForAll: () => encryptSessionForAll,
|
|
165
3338
|
encryptSessionForPartner: () => encryptSessionForPartner,
|
|
3339
|
+
generateHybridPartnerKeyPair: () => generateHybridPartnerKeyPair,
|
|
166
3340
|
generatePartnerKeyPair: () => generatePartnerKeyPair,
|
|
167
3341
|
isFreshTimestamp: () => isFreshTimestamp,
|
|
168
3342
|
isSafeWebhookUrl: () => isSafeWebhookUrl,
|
|
169
3343
|
normaliseAddress: () => normaliseAddress,
|
|
3344
|
+
normalizeForBlindIndex: () => normalizeForBlindIndex,
|
|
170
3345
|
signWebhookPayload: () => signWebhookPayload,
|
|
171
3346
|
verifyWebhookSignature: () => verifyWebhookSignature
|
|
172
3347
|
});
|
|
@@ -177,7 +3352,40 @@ function b64ToBytes(b64) {
|
|
|
177
3352
|
return bytes;
|
|
178
3353
|
}
|
|
179
3354
|
function bytesToB64(bytes) {
|
|
180
|
-
return btoa(String.
|
|
3355
|
+
return btoa(String.fromCodePoint(...bytes));
|
|
3356
|
+
}
|
|
3357
|
+
async function importP256PublicKey(keyBytes) {
|
|
3358
|
+
if (keyBytes.length === 65 && keyBytes[0] === 4) {
|
|
3359
|
+
return subtle().importKey(
|
|
3360
|
+
"raw",
|
|
3361
|
+
keyBytes,
|
|
3362
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3363
|
+
false,
|
|
3364
|
+
[]
|
|
3365
|
+
);
|
|
3366
|
+
}
|
|
3367
|
+
return subtle().importKey(
|
|
3368
|
+
"spki",
|
|
3369
|
+
keyBytes,
|
|
3370
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3371
|
+
false,
|
|
3372
|
+
[]
|
|
3373
|
+
);
|
|
3374
|
+
}
|
|
3375
|
+
async function importRsaOaepPublicKey(keyBytes) {
|
|
3376
|
+
return subtle().importKey(
|
|
3377
|
+
"spki",
|
|
3378
|
+
keyBytes,
|
|
3379
|
+
{ name: "RSA-OAEP", hash: "SHA-256" },
|
|
3380
|
+
false,
|
|
3381
|
+
["encrypt"]
|
|
3382
|
+
);
|
|
3383
|
+
}
|
|
3384
|
+
function isRsaScheme(algorithm) {
|
|
3385
|
+
return (algorithm ?? "").toUpperCase().startsWith("RSA");
|
|
3386
|
+
}
|
|
3387
|
+
function isHybridScheme(algorithm) {
|
|
3388
|
+
return (algorithm ?? "").toUpperCase().startsWith("MLKEM");
|
|
181
3389
|
}
|
|
182
3390
|
function toTitleCase(s) {
|
|
183
3391
|
return s.trim().replace(
|
|
@@ -216,16 +3424,16 @@ async function generatePartnerKeyPair() {
|
|
|
216
3424
|
privateKeyB64: bytesToB64(new Uint8Array(privRaw))
|
|
217
3425
|
};
|
|
218
3426
|
}
|
|
3427
|
+
function generateHybridPartnerKeyPair() {
|
|
3428
|
+
const { secretKey, publicKey } = XWing.keygen();
|
|
3429
|
+
return {
|
|
3430
|
+
publicKeyB64: bytesToB64(publicKey),
|
|
3431
|
+
privateKeyB64: bytesToB64(secretKey)
|
|
3432
|
+
};
|
|
3433
|
+
}
|
|
219
3434
|
async function encryptAddressForPartner(address, partner) {
|
|
220
3435
|
const address_ = normaliseAddress(address);
|
|
221
|
-
const partnerPubKey = await
|
|
222
|
-
"spki",
|
|
223
|
-
b64ToBytes(partner.publicKeyB64),
|
|
224
|
-
{ name: "ECDH", namedCurve: "P-256" },
|
|
225
|
-
false,
|
|
226
|
-
[]
|
|
227
|
-
// public key — no usages
|
|
228
|
-
);
|
|
3436
|
+
const partnerPubKey = await importP256PublicKey(b64ToBytes(partner.publicKeyB64));
|
|
229
3437
|
const ephemeral = await subtle().generateKey(
|
|
230
3438
|
{ name: "ECDH", namedCurve: "P-256" },
|
|
231
3439
|
true,
|
|
@@ -272,6 +3480,44 @@ async function encryptAddressForPartner(address, partner) {
|
|
|
272
3480
|
hkdfSalt: bytesToB64(hkdfSaltBytes)
|
|
273
3481
|
};
|
|
274
3482
|
}
|
|
3483
|
+
async function encryptPayloadForPartner(payload, partner) {
|
|
3484
|
+
const partnerPubKey = await importP256PublicKey(b64ToBytes(partner.publicKeyB64));
|
|
3485
|
+
const ephemeral = await subtle().generateKey(
|
|
3486
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3487
|
+
true,
|
|
3488
|
+
["deriveBits"]
|
|
3489
|
+
);
|
|
3490
|
+
const hkdfSaltBytes = globalThis.crypto.getRandomValues(new Uint8Array(32));
|
|
3491
|
+
const ecdhBits = await subtle().deriveBits(
|
|
3492
|
+
{ name: "ECDH", public: partnerPubKey },
|
|
3493
|
+
ephemeral.privateKey,
|
|
3494
|
+
256
|
|
3495
|
+
);
|
|
3496
|
+
const hkdfKey = await subtle().importKey("raw", ecdhBits, "HKDF", false, ["deriveBits"]);
|
|
3497
|
+
const aesRaw = await subtle().deriveBits(
|
|
3498
|
+
{
|
|
3499
|
+
name: "HKDF",
|
|
3500
|
+
hash: "SHA-256",
|
|
3501
|
+
salt: hkdfSaltBytes,
|
|
3502
|
+
info: new TextEncoder().encode(`oneaddress:${partner.partnerId}`)
|
|
3503
|
+
},
|
|
3504
|
+
hkdfKey,
|
|
3505
|
+
256
|
|
3506
|
+
);
|
|
3507
|
+
const aesKey = await subtle().importKey("raw", aesRaw, { name: "AES-GCM" }, false, ["encrypt"]);
|
|
3508
|
+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
|
|
3509
|
+
const plaintext = new TextEncoder().encode(JSON.stringify(payload));
|
|
3510
|
+
const ciphertextBuf = await subtle().encrypt({ name: "AES-GCM", iv }, aesKey, plaintext);
|
|
3511
|
+
const ephPubRaw = await subtle().exportKey("raw", ephemeral.publicKey);
|
|
3512
|
+
return {
|
|
3513
|
+
ciphertext: bytesToB64(new Uint8Array(ciphertextBuf)),
|
|
3514
|
+
iv: bytesToB64(iv),
|
|
3515
|
+
ephemeralPublicKey: bytesToB64(new Uint8Array(ephPubRaw)),
|
|
3516
|
+
partnerId: partner.partnerId,
|
|
3517
|
+
encryptedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3518
|
+
hkdfSalt: bytesToB64(hkdfSaltBytes)
|
|
3519
|
+
};
|
|
3520
|
+
}
|
|
275
3521
|
async function encryptAddressForAll(address, partners) {
|
|
276
3522
|
const results = await Promise.all(
|
|
277
3523
|
partners.map((p) => encryptAddressForPartner(address, p))
|
|
@@ -325,6 +3571,9 @@ async function decryptAddress(encrypted, privateKeyB64) {
|
|
|
325
3571
|
function sessionWrapInfo(partnerId) {
|
|
326
3572
|
return new TextEncoder().encode(`oneaddress:session-key-wrap:${partnerId}`);
|
|
327
3573
|
}
|
|
3574
|
+
function sessionWrapInfoPq(partnerId) {
|
|
3575
|
+
return new TextEncoder().encode(`oneaddress:session-key-wrap:pq1:${partnerId}`);
|
|
3576
|
+
}
|
|
328
3577
|
function concat(...parts) {
|
|
329
3578
|
const total = parts.reduce((n, p) => n + p.length, 0);
|
|
330
3579
|
const out = new Uint8Array(total);
|
|
@@ -343,24 +3592,67 @@ function b64UrlToBytes(b64url) {
|
|
|
343
3592
|
return b64ToBytes(b64url.replace(/-/g, "+").replace(/_/g, "/") + pad);
|
|
344
3593
|
}
|
|
345
3594
|
async function encryptSessionForPartner(session_data, partner) {
|
|
3595
|
+
return encryptEnvelopeForPartner(session_data, partner);
|
|
3596
|
+
}
|
|
3597
|
+
async function encryptEnvelopeForPartner(payload, partner) {
|
|
346
3598
|
const sk = await subtle().generateKey(
|
|
347
3599
|
{ name: "AES-GCM", length: SESSION_KEY_BITS },
|
|
348
3600
|
true,
|
|
349
3601
|
["encrypt"]
|
|
350
3602
|
);
|
|
351
3603
|
const sessionIv = globalThis.crypto.getRandomValues(new Uint8Array(SESSION_IV_LENGTH));
|
|
352
|
-
const plaintext = new TextEncoder().encode(JSON.stringify(
|
|
3604
|
+
const plaintext = new TextEncoder().encode(JSON.stringify(payload));
|
|
353
3605
|
const sessionCt = new Uint8Array(
|
|
354
3606
|
await subtle().encrypt({ name: "AES-GCM", iv: sessionIv }, sk, plaintext)
|
|
355
3607
|
);
|
|
356
3608
|
const session_envelope = bytesToB64Url(concat(sessionIv, sessionCt));
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
b64ToBytes(partner.public_key_b64)
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
3609
|
+
const skRawForWrap = new Uint8Array(await subtle().exportKey("raw", sk));
|
|
3610
|
+
if (isHybridScheme(partner.algorithm)) {
|
|
3611
|
+
const { cipherText: kemCt, sharedSecret: sharedSecret2 } = XWing.encapsulate(b64ToBytes(partner.public_key_b64));
|
|
3612
|
+
const wrapSalt2 = globalThis.crypto.getRandomValues(new Uint8Array(WRAP_SALT_LENGTH));
|
|
3613
|
+
const wrapKeyRaw2 = await subtle().deriveBits(
|
|
3614
|
+
{ name: "HKDF", hash: "SHA-256", salt: wrapSalt2, info: sessionWrapInfoPq(partner.partner_id) },
|
|
3615
|
+
await subtle().importKey("raw", sharedSecret2, "HKDF", false, ["deriveBits"]),
|
|
3616
|
+
256
|
|
3617
|
+
);
|
|
3618
|
+
const wrapKey2 = await subtle().importKey("raw", wrapKeyRaw2, { name: "AES-GCM" }, false, ["encrypt"]);
|
|
3619
|
+
const wrapIv2 = globalThis.crypto.getRandomValues(new Uint8Array(WRAP_IV_LENGTH));
|
|
3620
|
+
const wrappedSkBytes2 = new Uint8Array(
|
|
3621
|
+
await subtle().encrypt({ name: "AES-GCM", iv: wrapIv2 }, wrapKey2, skRawForWrap)
|
|
3622
|
+
);
|
|
3623
|
+
skRawForWrap.fill(0);
|
|
3624
|
+
return {
|
|
3625
|
+
partner_id: partner.partner_id,
|
|
3626
|
+
session_envelope,
|
|
3627
|
+
session_key_share: {
|
|
3628
|
+
key_id: partner.key_id,
|
|
3629
|
+
wrapped_key: bytesToB64Url(concat(wrapSalt2, wrapIv2, wrappedSkBytes2)),
|
|
3630
|
+
epk: bytesToB64Url(kemCt),
|
|
3631
|
+
// X-Wing KEM ciphertext (no ephemeral EC key)
|
|
3632
|
+
alg: "MLKEM768X25519+HKDF-SHA256+A256GCM",
|
|
3633
|
+
v: 2
|
|
3634
|
+
}
|
|
3635
|
+
};
|
|
3636
|
+
}
|
|
3637
|
+
if (isRsaScheme(partner.algorithm)) {
|
|
3638
|
+
const partnerRsaPub = await importRsaOaepPublicKey(b64ToBytes(partner.public_key_b64));
|
|
3639
|
+
const rsaWrapped = new Uint8Array(
|
|
3640
|
+
await subtle().encrypt({ name: "RSA-OAEP" }, partnerRsaPub, skRawForWrap)
|
|
3641
|
+
);
|
|
3642
|
+
skRawForWrap.fill(0);
|
|
3643
|
+
return {
|
|
3644
|
+
partner_id: partner.partner_id,
|
|
3645
|
+
session_envelope,
|
|
3646
|
+
session_key_share: {
|
|
3647
|
+
key_id: partner.key_id,
|
|
3648
|
+
wrapped_key: bytesToB64Url(rsaWrapped),
|
|
3649
|
+
// No epk — RSA key transport has no ephemeral key.
|
|
3650
|
+
alg: "RSA-OAEP-256+A256GCM",
|
|
3651
|
+
v: 2
|
|
3652
|
+
}
|
|
3653
|
+
};
|
|
3654
|
+
}
|
|
3655
|
+
const partnerPub = await importP256PublicKey(b64ToBytes(partner.public_key_b64));
|
|
364
3656
|
const ephemeral = await subtle().generateKey(
|
|
365
3657
|
{ name: "ECDH", namedCurve: "P-256" },
|
|
366
3658
|
true,
|
|
@@ -389,12 +3681,11 @@ async function encryptSessionForPartner(session_data, partner) {
|
|
|
389
3681
|
false,
|
|
390
3682
|
["encrypt"]
|
|
391
3683
|
);
|
|
392
|
-
const skRaw = new Uint8Array(await subtle().exportKey("raw", sk));
|
|
393
3684
|
const wrapIv = globalThis.crypto.getRandomValues(new Uint8Array(WRAP_IV_LENGTH));
|
|
394
3685
|
const wrappedSkBytes = new Uint8Array(
|
|
395
|
-
await subtle().encrypt({ name: "AES-GCM", iv: wrapIv }, wrapKey,
|
|
3686
|
+
await subtle().encrypt({ name: "AES-GCM", iv: wrapIv }, wrapKey, skRawForWrap)
|
|
396
3687
|
);
|
|
397
|
-
|
|
3688
|
+
skRawForWrap.fill(0);
|
|
398
3689
|
const wrapped_key = bytesToB64Url(concat(wrapSalt, wrapIv, wrappedSkBytes));
|
|
399
3690
|
const epkSpki = new Uint8Array(await subtle().exportKey("spki", ephemeral.publicKey));
|
|
400
3691
|
const epk = bytesToB64Url(epkSpki);
|
|
@@ -417,60 +3708,98 @@ async function encryptSessionForAll(partners) {
|
|
|
417
3708
|
return Object.fromEntries(results.map((r) => [r.partner_id, r]));
|
|
418
3709
|
}
|
|
419
3710
|
async function decryptSession(share, envelopeB64, privateKeyB64, partnerId) {
|
|
420
|
-
if (share.alg !== "ECDH-ES+HKDF-SHA256+A256GCM") {
|
|
3711
|
+
if (share.alg !== "ECDH-ES+HKDF-SHA256+A256GCM" && share.alg !== "RSA-OAEP-256+A256GCM" && share.alg !== "MLKEM768X25519+HKDF-SHA256+A256GCM") {
|
|
421
3712
|
throw new Error(`decryptSession: unsupported alg ${share.alg}`);
|
|
422
3713
|
}
|
|
423
3714
|
if (share.v !== 2) {
|
|
424
3715
|
throw new Error(`decryptSession: unsupported share version ${share.v}`);
|
|
425
3716
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
if (
|
|
447
|
-
|
|
3717
|
+
let skRawBuf;
|
|
3718
|
+
if (share.alg === "MLKEM768X25519+HKDF-SHA256+A256GCM") {
|
|
3719
|
+
if (!share.epk) {
|
|
3720
|
+
throw new Error("decryptSession: hybrid share missing epk (KEM ciphertext)");
|
|
3721
|
+
}
|
|
3722
|
+
const sharedSecret = XWing.decapsulate(b64UrlToBytes(share.epk), b64ToBytes(privateKeyB64));
|
|
3723
|
+
const wrappedBytes = b64UrlToBytes(share.wrapped_key);
|
|
3724
|
+
if (wrappedBytes.length < WRAP_SALT_LENGTH + WRAP_IV_LENGTH + 16) {
|
|
3725
|
+
throw new Error("decryptSession: wrapped_key too short");
|
|
3726
|
+
}
|
|
3727
|
+
const wrapSalt = wrappedBytes.slice(0, WRAP_SALT_LENGTH);
|
|
3728
|
+
const wrapIv = wrappedBytes.slice(WRAP_SALT_LENGTH, WRAP_SALT_LENGTH + WRAP_IV_LENGTH);
|
|
3729
|
+
const wrappedSkBytes = wrappedBytes.slice(WRAP_SALT_LENGTH + WRAP_IV_LENGTH);
|
|
3730
|
+
const wrapKeyRaw = await subtle().deriveBits(
|
|
3731
|
+
{ name: "HKDF", hash: "SHA-256", salt: wrapSalt, info: sessionWrapInfoPq(partnerId) },
|
|
3732
|
+
await subtle().importKey("raw", sharedSecret, "HKDF", false, ["deriveBits"]),
|
|
3733
|
+
256
|
|
3734
|
+
);
|
|
3735
|
+
const wrapKey = await subtle().importKey("raw", wrapKeyRaw, { name: "AES-GCM" }, false, ["decrypt"]);
|
|
3736
|
+
skRawBuf = await subtle().decrypt({ name: "AES-GCM", iv: wrapIv }, wrapKey, wrappedSkBytes);
|
|
3737
|
+
} else if (share.alg === "RSA-OAEP-256+A256GCM") {
|
|
3738
|
+
const rsaPrivateKey = await subtle().importKey(
|
|
3739
|
+
"pkcs8",
|
|
3740
|
+
b64ToBytes(privateKeyB64),
|
|
3741
|
+
{ name: "RSA-OAEP", hash: "SHA-256" },
|
|
3742
|
+
false,
|
|
3743
|
+
["decrypt"]
|
|
3744
|
+
);
|
|
3745
|
+
skRawBuf = await subtle().decrypt(
|
|
3746
|
+
{ name: "RSA-OAEP" },
|
|
3747
|
+
rsaPrivateKey,
|
|
3748
|
+
b64UrlToBytes(share.wrapped_key)
|
|
3749
|
+
);
|
|
3750
|
+
} else {
|
|
3751
|
+
if (!share.epk) {
|
|
3752
|
+
throw new Error("decryptSession: ECDH-ES share missing epk");
|
|
3753
|
+
}
|
|
3754
|
+
const privateKey = await subtle().importKey(
|
|
3755
|
+
"pkcs8",
|
|
3756
|
+
b64ToBytes(privateKeyB64),
|
|
3757
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3758
|
+
false,
|
|
3759
|
+
["deriveBits"]
|
|
3760
|
+
);
|
|
3761
|
+
const ephemeralPub = await subtle().importKey(
|
|
3762
|
+
"spki",
|
|
3763
|
+
b64UrlToBytes(share.epk),
|
|
3764
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3765
|
+
false,
|
|
3766
|
+
[]
|
|
3767
|
+
);
|
|
3768
|
+
const sharedSecret = await subtle().deriveBits(
|
|
3769
|
+
{ name: "ECDH", public: ephemeralPub },
|
|
3770
|
+
privateKey,
|
|
3771
|
+
256
|
|
3772
|
+
);
|
|
3773
|
+
const wrappedBytes = b64UrlToBytes(share.wrapped_key);
|
|
3774
|
+
if (wrappedBytes.length < WRAP_SALT_LENGTH + WRAP_IV_LENGTH + 16) {
|
|
3775
|
+
throw new Error("decryptSession: wrapped_key too short");
|
|
3776
|
+
}
|
|
3777
|
+
const wrapSalt = wrappedBytes.slice(0, WRAP_SALT_LENGTH);
|
|
3778
|
+
const wrapIv = wrappedBytes.slice(WRAP_SALT_LENGTH, WRAP_SALT_LENGTH + WRAP_IV_LENGTH);
|
|
3779
|
+
const wrappedSkBytes = wrappedBytes.slice(WRAP_SALT_LENGTH + WRAP_IV_LENGTH);
|
|
3780
|
+
const wrapKeyRaw = await subtle().deriveBits(
|
|
3781
|
+
{
|
|
3782
|
+
name: "HKDF",
|
|
3783
|
+
hash: "SHA-256",
|
|
3784
|
+
salt: wrapSalt,
|
|
3785
|
+
info: sessionWrapInfo(partnerId)
|
|
3786
|
+
},
|
|
3787
|
+
await subtle().importKey("raw", sharedSecret, "HKDF", false, ["deriveBits"]),
|
|
3788
|
+
256
|
|
3789
|
+
);
|
|
3790
|
+
const wrapKey = await subtle().importKey(
|
|
3791
|
+
"raw",
|
|
3792
|
+
wrapKeyRaw,
|
|
3793
|
+
{ name: "AES-GCM" },
|
|
3794
|
+
false,
|
|
3795
|
+
["decrypt"]
|
|
3796
|
+
);
|
|
3797
|
+
skRawBuf = await subtle().decrypt(
|
|
3798
|
+
{ name: "AES-GCM", iv: wrapIv },
|
|
3799
|
+
wrapKey,
|
|
3800
|
+
wrappedSkBytes
|
|
3801
|
+
);
|
|
448
3802
|
}
|
|
449
|
-
const wrapSalt = wrappedBytes.slice(0, WRAP_SALT_LENGTH);
|
|
450
|
-
const wrapIv = wrappedBytes.slice(WRAP_SALT_LENGTH, WRAP_SALT_LENGTH + WRAP_IV_LENGTH);
|
|
451
|
-
const wrappedSkBytes = wrappedBytes.slice(WRAP_SALT_LENGTH + WRAP_IV_LENGTH);
|
|
452
|
-
const wrapKeyRaw = await subtle().deriveBits(
|
|
453
|
-
{
|
|
454
|
-
name: "HKDF",
|
|
455
|
-
hash: "SHA-256",
|
|
456
|
-
salt: wrapSalt,
|
|
457
|
-
info: sessionWrapInfo(partnerId)
|
|
458
|
-
},
|
|
459
|
-
await subtle().importKey("raw", sharedSecret, "HKDF", false, ["deriveBits"]),
|
|
460
|
-
256
|
|
461
|
-
);
|
|
462
|
-
const wrapKey = await subtle().importKey(
|
|
463
|
-
"raw",
|
|
464
|
-
wrapKeyRaw,
|
|
465
|
-
{ name: "AES-GCM" },
|
|
466
|
-
false,
|
|
467
|
-
["decrypt"]
|
|
468
|
-
);
|
|
469
|
-
const skRawBuf = await subtle().decrypt(
|
|
470
|
-
{ name: "AES-GCM", iv: wrapIv },
|
|
471
|
-
wrapKey,
|
|
472
|
-
wrappedSkBytes
|
|
473
|
-
);
|
|
474
3803
|
const sk = await subtle().importKey(
|
|
475
3804
|
"raw",
|
|
476
3805
|
skRawBuf,
|
|
@@ -491,6 +3820,52 @@ async function decryptSession(share, envelopeB64, privateKeyB64, partnerId) {
|
|
|
491
3820
|
);
|
|
492
3821
|
return JSON.parse(new TextDecoder().decode(plainBuf));
|
|
493
3822
|
}
|
|
3823
|
+
function normalizeForBlindIndex(field, value) {
|
|
3824
|
+
const base = value.normalize("NFKC").trim();
|
|
3825
|
+
switch (field) {
|
|
3826
|
+
case "email":
|
|
3827
|
+
return base.toLowerCase();
|
|
3828
|
+
case "name":
|
|
3829
|
+
return base.toLowerCase().replace(/\s+/g, " ");
|
|
3830
|
+
case "account_number":
|
|
3831
|
+
return base.replace(/\s+/g, "");
|
|
3832
|
+
}
|
|
3833
|
+
}
|
|
3834
|
+
async function computeBlindIndex(secret, field, value) {
|
|
3835
|
+
const normalized = normalizeForBlindIndex(field, value);
|
|
3836
|
+
const key = await subtle().importKey(
|
|
3837
|
+
"raw",
|
|
3838
|
+
new TextEncoder().encode(secret),
|
|
3839
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
3840
|
+
false,
|
|
3841
|
+
["sign"]
|
|
3842
|
+
);
|
|
3843
|
+
const message = new TextEncoder().encode(`oneaddress:bidx:${field}:${normalized}`);
|
|
3844
|
+
const sig = await subtle().sign("HMAC", key, message);
|
|
3845
|
+
return bytesToB64Url(new Uint8Array(sig));
|
|
3846
|
+
}
|
|
3847
|
+
async function computeCustomerBlindIndex(secret, fields) {
|
|
3848
|
+
const out = { names: [] };
|
|
3849
|
+
if (fields.email && fields.email.trim()) {
|
|
3850
|
+
out.email = await computeBlindIndex(secret, "email", fields.email);
|
|
3851
|
+
}
|
|
3852
|
+
if (fields.account_number && fields.account_number.trim()) {
|
|
3853
|
+
out.account_number = await computeBlindIndex(secret, "account_number", fields.account_number);
|
|
3854
|
+
}
|
|
3855
|
+
const nameValues = [];
|
|
3856
|
+
if (fields.name && fields.name.trim()) nameValues.push(fields.name);
|
|
3857
|
+
for (const n of fields.known_names ?? []) {
|
|
3858
|
+
if (n && n.trim()) nameValues.push(n);
|
|
3859
|
+
}
|
|
3860
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3861
|
+
for (const v of nameValues) {
|
|
3862
|
+
const norm = normalizeForBlindIndex("name", v);
|
|
3863
|
+
if (seen.has(norm)) continue;
|
|
3864
|
+
seen.add(norm);
|
|
3865
|
+
out.names.push(await computeBlindIndex(secret, "name", v));
|
|
3866
|
+
}
|
|
3867
|
+
return out;
|
|
3868
|
+
}
|
|
494
3869
|
async function signWebhookPayload(payload, secret) {
|
|
495
3870
|
const key = await subtle().importKey(
|
|
496
3871
|
"raw",
|
|
@@ -520,6 +3895,7 @@ var subtle, AU_STATE_FULL, SESSION_KEY_BITS, SESSION_IV_LENGTH, WRAP_SALT_LENGTH
|
|
|
520
3895
|
var init_src = __esm({
|
|
521
3896
|
"../crypto/src/index.ts"() {
|
|
522
3897
|
"use strict";
|
|
3898
|
+
init_hybrid();
|
|
523
3899
|
init_replay_window();
|
|
524
3900
|
init_safe_url();
|
|
525
3901
|
subtle = () => globalThis.crypto.subtle;
|
|
@@ -555,17 +3931,24 @@ __export(index_exports, {
|
|
|
555
3931
|
CURRENT_VERSION: () => CURRENT_VERSION,
|
|
556
3932
|
DEFAULT_REPLAY_WINDOW_SECS: () => DEFAULT_REPLAY_WINDOW_SECS,
|
|
557
3933
|
SUPPORTED_VERSIONS: () => SUPPORTED_VERSIONS,
|
|
3934
|
+
canonicalD5LoaForm: () => canonicalD5LoaForm,
|
|
558
3935
|
compareAddresses: () => compareAddresses,
|
|
3936
|
+
computeBlindIndex: () => computeBlindIndex,
|
|
559
3937
|
createOneAddressHandler: () => createOneAddressHandler,
|
|
3938
|
+
d5LoaRef: () => d5LoaRef,
|
|
560
3939
|
decrypt: () => decryptAddress2,
|
|
561
3940
|
decryptAddress: () => decryptAddress2,
|
|
3941
|
+
decryptLoaEncrypted: () => decryptLoaEncrypted,
|
|
562
3942
|
decryptSession: () => decryptSession2,
|
|
563
3943
|
fetchLoaPublicKey: () => fetchLoaPublicKey,
|
|
564
3944
|
isFreshTimestamp: () => isFreshTimestamp,
|
|
3945
|
+
normalizeForBlindIndex: () => normalizeForBlindIndex,
|
|
565
3946
|
transform: () => transformAddress,
|
|
566
3947
|
transformAddress: () => transformAddress,
|
|
3948
|
+
transformSession: () => transformSession,
|
|
567
3949
|
validateAddress: () => validateAddress,
|
|
568
3950
|
verify: () => verifySignature,
|
|
3951
|
+
verifyD5LOA: () => verifyD5LOA,
|
|
569
3952
|
verifyGrace: () => verifyWithGrace,
|
|
570
3953
|
verifyLOA: () => verifyLOA,
|
|
571
3954
|
verifySignature: () => verifySignature,
|
|
@@ -581,6 +3964,7 @@ var SUPPORTED_VERSIONS = ["2026.1"];
|
|
|
581
3964
|
|
|
582
3965
|
// src/index.ts
|
|
583
3966
|
init_src();
|
|
3967
|
+
init_src();
|
|
584
3968
|
function verifySignature(rawBody, signature, timestamp, secret) {
|
|
585
3969
|
if (!/^[0-9a-f]{64}$/i.test(signature)) return false;
|
|
586
3970
|
const sigPayload = `${timestamp}.${rawBody}`;
|
|
@@ -662,6 +4046,13 @@ async function decryptSession2(share, envelopeB64, privateKeyPem, partnerId) {
|
|
|
662
4046
|
async function transformAddress(payload, privateKeyPem, partnerId, recipientSpkiPem) {
|
|
663
4047
|
const plaintext = await decryptAddress2(payload, privateKeyPem, partnerId);
|
|
664
4048
|
const plaintextBytes = new TextEncoder().encode(JSON.stringify(plaintext));
|
|
4049
|
+
return reEncryptForRecipient(
|
|
4050
|
+
plaintextBytes,
|
|
4051
|
+
recipientSpkiPem,
|
|
4052
|
+
new TextEncoder().encode(`oneaddress:transform:${partnerId}`)
|
|
4053
|
+
);
|
|
4054
|
+
}
|
|
4055
|
+
async function reEncryptForRecipient(plaintextBytes, recipientSpkiPem, info) {
|
|
665
4056
|
const recipientKey = await import_crypto.default.subtle.importKey(
|
|
666
4057
|
"spki",
|
|
667
4058
|
pemToBuffer(recipientSpkiPem),
|
|
@@ -681,7 +4072,6 @@ async function transformAddress(payload, privateKeyPem, partnerId, recipientSpki
|
|
|
681
4072
|
);
|
|
682
4073
|
const hkdfSaltBytes = import_crypto.default.getRandomValues(new Uint8Array(32));
|
|
683
4074
|
const hkdfKey = await import_crypto.default.subtle.importKey("raw", sharedBits, "HKDF", false, ["deriveBits"]);
|
|
684
|
-
const info = new TextEncoder().encode(`oneaddress:transform:${partnerId}`);
|
|
685
4075
|
const aesKeyBits = await import_crypto.default.subtle.deriveBits(
|
|
686
4076
|
{ name: "HKDF", hash: "SHA-256", salt: hkdfSaltBytes, info },
|
|
687
4077
|
hkdfKey,
|
|
@@ -702,6 +4092,15 @@ async function transformAddress(payload, privateKeyPem, partnerId, recipientSpki
|
|
|
702
4092
|
hkdfSalt: Buffer.from(hkdfSaltBytes).toString("base64")
|
|
703
4093
|
};
|
|
704
4094
|
}
|
|
4095
|
+
async function transformSession(share, envelopeB64, privateKeyPem, partnerId, recipientSpkiPem) {
|
|
4096
|
+
const sessionData = await decryptSession2(share, envelopeB64, privateKeyPem, partnerId);
|
|
4097
|
+
const plaintextBytes = new TextEncoder().encode(JSON.stringify(sessionData));
|
|
4098
|
+
return reEncryptForRecipient(
|
|
4099
|
+
plaintextBytes,
|
|
4100
|
+
recipientSpkiPem,
|
|
4101
|
+
new TextEncoder().encode(`oneaddress:transform-session:${partnerId}`)
|
|
4102
|
+
);
|
|
4103
|
+
}
|
|
705
4104
|
var OA_APP_BASE_URL = "https://oneaddress.io";
|
|
706
4105
|
var ALLOWED_LOA_HOSTS = /* @__PURE__ */ new Set(["oneaddress.io", "app.oneaddress.io", "localhost", "127.0.0.1"]);
|
|
707
4106
|
async function fetchLoaPublicKey(appBaseUrl = OA_APP_BASE_URL) {
|
|
@@ -751,6 +4150,56 @@ async function verifyLOA(loa, publicKeyPem) {
|
|
|
751
4150
|
return false;
|
|
752
4151
|
}
|
|
753
4152
|
}
|
|
4153
|
+
function canonicalD5LoaForm(loa) {
|
|
4154
|
+
return [
|
|
4155
|
+
loa.consumer_name,
|
|
4156
|
+
loa.authorisation_date,
|
|
4157
|
+
loa.scope,
|
|
4158
|
+
loa.update_uid,
|
|
4159
|
+
loa.oneaddress_acn
|
|
4160
|
+
].join("|");
|
|
4161
|
+
}
|
|
4162
|
+
function toB64Url(buf) {
|
|
4163
|
+
return buf.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
4164
|
+
}
|
|
4165
|
+
function d5LoaRef(loa) {
|
|
4166
|
+
return toB64Url(import_crypto.default.createHash("sha256").update(canonicalD5LoaForm(loa), "utf8").digest());
|
|
4167
|
+
}
|
|
4168
|
+
async function verifyD5LOA(loa, publicKeyPem) {
|
|
4169
|
+
if (loa.signature_algorithm !== "ECDSA-P256") {
|
|
4170
|
+
console.warn(
|
|
4171
|
+
// NOSONAR — intentional SDK diagnostic, not user-controlled data
|
|
4172
|
+
"[OneAddress SDK] D5 LOA algorithm is HMAC-SHA256 (deprecated) \u2014 cannot be verified by partners."
|
|
4173
|
+
);
|
|
4174
|
+
return false;
|
|
4175
|
+
}
|
|
4176
|
+
try {
|
|
4177
|
+
const key = await import_crypto.default.subtle.importKey(
|
|
4178
|
+
"spki",
|
|
4179
|
+
pemToBuffer(publicKeyPem),
|
|
4180
|
+
{ name: "ECDSA", namedCurve: "P-256" },
|
|
4181
|
+
false,
|
|
4182
|
+
["verify"]
|
|
4183
|
+
);
|
|
4184
|
+
return await import_crypto.default.subtle.verify(
|
|
4185
|
+
{ name: "ECDSA", hash: "SHA-256" },
|
|
4186
|
+
key,
|
|
4187
|
+
Buffer.from(loa.oneaddress_signature, "hex"),
|
|
4188
|
+
new TextEncoder().encode(canonicalD5LoaForm(loa))
|
|
4189
|
+
);
|
|
4190
|
+
} catch {
|
|
4191
|
+
return false;
|
|
4192
|
+
}
|
|
4193
|
+
}
|
|
4194
|
+
async function decryptLoaEncrypted(loaEncrypted, privateKeyPem, partnerId) {
|
|
4195
|
+
const data = await decryptSession2(
|
|
4196
|
+
loaEncrypted.session_key_share,
|
|
4197
|
+
loaEncrypted.session_envelope,
|
|
4198
|
+
privateKeyPem,
|
|
4199
|
+
partnerId
|
|
4200
|
+
);
|
|
4201
|
+
return data;
|
|
4202
|
+
}
|
|
754
4203
|
var ADDRESS_REQUIRED_FIELDS = [
|
|
755
4204
|
"street",
|
|
756
4205
|
"suburb",
|
|
@@ -835,12 +4284,49 @@ function createOneAddressHandler(options) {
|
|
|
835
4284
|
previousWebhookSecret,
|
|
836
4285
|
webhookSecretGraceUntil,
|
|
837
4286
|
privateKeyPem,
|
|
4287
|
+
loaPublicKeyPem,
|
|
838
4288
|
onUpdate,
|
|
839
4289
|
onVerify,
|
|
840
4290
|
onAccountVerify,
|
|
841
4291
|
timestampToleranceSecs = 300,
|
|
842
4292
|
idempotencyStore
|
|
843
4293
|
} = options;
|
|
4294
|
+
async function buildD5Pieces(body, partnerId) {
|
|
4295
|
+
const share = body.session_key_share;
|
|
4296
|
+
const data = await decryptSession2(share, body.session_envelope, privateKeyPem, partnerId);
|
|
4297
|
+
const address = validateAddress({
|
|
4298
|
+
street: data.new_address?.street,
|
|
4299
|
+
suburb: data.new_address?.suburb,
|
|
4300
|
+
state: data.new_address?.state,
|
|
4301
|
+
postcode: data.new_address?.postcode,
|
|
4302
|
+
country: data.new_address?.country,
|
|
4303
|
+
updated_at: data.dispatch_timestamp ?? body.timestamp ?? ""
|
|
4304
|
+
});
|
|
4305
|
+
const customer = {
|
|
4306
|
+
email: "",
|
|
4307
|
+
name: data.verified_name ?? "",
|
|
4308
|
+
account_number: data.account_number ?? null,
|
|
4309
|
+
known_names: Array.isArray(data.known_names) ? data.known_names : []
|
|
4310
|
+
};
|
|
4311
|
+
let loa;
|
|
4312
|
+
let loa_ref;
|
|
4313
|
+
if (body.loa_encrypted && typeof body.loa_encrypted === "object") {
|
|
4314
|
+
loa = await decryptLoaEncrypted(
|
|
4315
|
+
body.loa_encrypted,
|
|
4316
|
+
privateKeyPem,
|
|
4317
|
+
partnerId
|
|
4318
|
+
);
|
|
4319
|
+
loa_ref = d5LoaRef(loa);
|
|
4320
|
+
if (loaPublicKeyPem) {
|
|
4321
|
+
const valid = await verifyD5LOA(loa, loaPublicKeyPem);
|
|
4322
|
+
if (!valid) throw new Error("D5 LOA signature verification failed");
|
|
4323
|
+
}
|
|
4324
|
+
}
|
|
4325
|
+
return { address, customer, loa, loa_ref, raw: data };
|
|
4326
|
+
}
|
|
4327
|
+
function isD5Body(body) {
|
|
4328
|
+
return typeof body.session_envelope === "string" && !!body.session_key_share && typeof body.session_key_share === "object";
|
|
4329
|
+
}
|
|
844
4330
|
return async function handler(req) {
|
|
845
4331
|
const rawBody = await req.text();
|
|
846
4332
|
const sig = req.headers.get("x-oneaddress-signature") ?? "";
|
|
@@ -895,27 +4381,66 @@ function createOneAddressHandler(options) {
|
|
|
895
4381
|
}
|
|
896
4382
|
try {
|
|
897
4383
|
if (body.event === "address.updated") {
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
4384
|
+
let event;
|
|
4385
|
+
if (isD5Body(body)) {
|
|
4386
|
+
const d5 = await buildD5Pieces(body, partnerId);
|
|
4387
|
+
event = {
|
|
4388
|
+
event: "address.updated",
|
|
4389
|
+
version: body.oneaddress_version ?? body.version ?? "2.0",
|
|
4390
|
+
id: body.id ?? "",
|
|
4391
|
+
timestamp: tsNum,
|
|
4392
|
+
partner_id: partnerId,
|
|
4393
|
+
dispatchId,
|
|
4394
|
+
customer: d5.customer,
|
|
4395
|
+
address: d5.address,
|
|
4396
|
+
loa: d5.loa,
|
|
4397
|
+
loa_ref: d5.loa_ref,
|
|
4398
|
+
id_verification: body.id_verification,
|
|
4399
|
+
raw: d5.raw
|
|
4400
|
+
};
|
|
4401
|
+
} else {
|
|
4402
|
+
if (!body.address_encrypted || typeof body.address_encrypted !== "object") {
|
|
4403
|
+
return new Response(JSON.stringify({ error: "Missing or invalid address_encrypted payload" }), {
|
|
4404
|
+
status: 400,
|
|
4405
|
+
headers: { "Content-Type": "application/json" }
|
|
4406
|
+
});
|
|
4407
|
+
}
|
|
4408
|
+
const encrypted = body.address_encrypted;
|
|
4409
|
+
const rawAddress = await decryptAddress2(encrypted, privateKeyPem, partnerId);
|
|
4410
|
+
let loa;
|
|
4411
|
+
let loa_ref;
|
|
4412
|
+
if (body.loa_encrypted && typeof body.loa_encrypted === "object") {
|
|
4413
|
+
loa = await decryptLoaEncrypted(
|
|
4414
|
+
body.loa_encrypted,
|
|
4415
|
+
privateKeyPem,
|
|
4416
|
+
partnerId
|
|
4417
|
+
);
|
|
4418
|
+
loa_ref = d5LoaRef(loa);
|
|
4419
|
+
if (loaPublicKeyPem && !await verifyD5LOA(loa, loaPublicKeyPem)) {
|
|
4420
|
+
throw new Error("D5 LOA signature verification failed");
|
|
4421
|
+
}
|
|
4422
|
+
}
|
|
4423
|
+
const customer = {
|
|
4424
|
+
email: "",
|
|
4425
|
+
name: rawAddress.fullName ?? "",
|
|
4426
|
+
account_number: rawAddress.accountReference ?? null,
|
|
4427
|
+
known_names: Array.isArray(rawAddress.knownNames) ? rawAddress.knownNames : []
|
|
4428
|
+
};
|
|
4429
|
+
event = {
|
|
4430
|
+
event: "address.updated",
|
|
4431
|
+
version: body.oneaddress_version ?? body.version ?? "1",
|
|
4432
|
+
id: body.id ?? "",
|
|
4433
|
+
timestamp: tsNum,
|
|
4434
|
+
partner_id: partnerId,
|
|
4435
|
+
dispatchId,
|
|
4436
|
+
customer,
|
|
4437
|
+
address: validateAddress(rawAddress),
|
|
4438
|
+
loa,
|
|
4439
|
+
loa_ref,
|
|
4440
|
+
id_verification: body.id_verification,
|
|
4441
|
+
raw: rawAddress
|
|
4442
|
+
};
|
|
903
4443
|
}
|
|
904
|
-
const encrypted = body.address_encrypted;
|
|
905
|
-
const rawAddress = await decryptAddress2(encrypted, privateKeyPem, partnerId);
|
|
906
|
-
const event = {
|
|
907
|
-
event: "address.updated",
|
|
908
|
-
version: body.oneaddress_version ?? body.version ?? "1",
|
|
909
|
-
id: body.id ?? "",
|
|
910
|
-
timestamp: tsNum,
|
|
911
|
-
partner_id: partnerId,
|
|
912
|
-
dispatchId,
|
|
913
|
-
customer: body.customer,
|
|
914
|
-
address: validateAddress(rawAddress),
|
|
915
|
-
loa: body.loa,
|
|
916
|
-
id_verification: body.id_verification,
|
|
917
|
-
raw: rawAddress
|
|
918
|
-
};
|
|
919
4444
|
const result = await onUpdate(event);
|
|
920
4445
|
if (dispatchId && idempotencyStore) {
|
|
921
4446
|
await idempotencyStore.set(dispatchId).catch(
|
|
@@ -946,7 +4471,8 @@ function createOneAddressHandler(options) {
|
|
|
946
4471
|
dispatchId,
|
|
947
4472
|
batch_id: body.batch_id ?? 0,
|
|
948
4473
|
user_id: body.user_id ?? "",
|
|
949
|
-
customer: body.customer
|
|
4474
|
+
customer: body.customer,
|
|
4475
|
+
customer_match: body.customer_match,
|
|
950
4476
|
address: validateAddress(rawAddress),
|
|
951
4477
|
loa: body.loa,
|
|
952
4478
|
id_verification: body.id_verification,
|
|
@@ -959,6 +4485,22 @@ function createOneAddressHandler(options) {
|
|
|
959
4485
|
});
|
|
960
4486
|
}
|
|
961
4487
|
if (body.event === "account.verify" && onAccountVerify) {
|
|
4488
|
+
let customer;
|
|
4489
|
+
if (body.customer_encrypted && typeof body.customer_encrypted === "object") {
|
|
4490
|
+
const raw = await decryptAddress2(
|
|
4491
|
+
body.customer_encrypted,
|
|
4492
|
+
privateKeyPem,
|
|
4493
|
+
partnerId
|
|
4494
|
+
);
|
|
4495
|
+
customer = {
|
|
4496
|
+
email: raw.email ?? "",
|
|
4497
|
+
name: raw.name ?? "",
|
|
4498
|
+
account_number: raw.account_number ?? null,
|
|
4499
|
+
known_names: Array.isArray(raw.known_names) ? raw.known_names : []
|
|
4500
|
+
};
|
|
4501
|
+
} else {
|
|
4502
|
+
customer = body.customer ?? {};
|
|
4503
|
+
}
|
|
962
4504
|
const event = {
|
|
963
4505
|
event: "account.verify",
|
|
964
4506
|
version: body.oneaddress_version ?? body.version ?? "1",
|
|
@@ -967,7 +4509,7 @@ function createOneAddressHandler(options) {
|
|
|
967
4509
|
partner_id: partnerId,
|
|
968
4510
|
dispatchId,
|
|
969
4511
|
user_id: body.user_id ?? "",
|
|
970
|
-
customer
|
|
4512
|
+
customer,
|
|
971
4513
|
raw: body
|
|
972
4514
|
};
|
|
973
4515
|
const result = await onAccountVerify(event);
|
|
@@ -995,19 +4537,42 @@ function createOneAddressHandler(options) {
|
|
|
995
4537
|
CURRENT_VERSION,
|
|
996
4538
|
DEFAULT_REPLAY_WINDOW_SECS,
|
|
997
4539
|
SUPPORTED_VERSIONS,
|
|
4540
|
+
canonicalD5LoaForm,
|
|
998
4541
|
compareAddresses,
|
|
4542
|
+
computeBlindIndex,
|
|
999
4543
|
createOneAddressHandler,
|
|
4544
|
+
d5LoaRef,
|
|
1000
4545
|
decrypt,
|
|
1001
4546
|
decryptAddress,
|
|
4547
|
+
decryptLoaEncrypted,
|
|
1002
4548
|
decryptSession,
|
|
1003
4549
|
fetchLoaPublicKey,
|
|
1004
4550
|
isFreshTimestamp,
|
|
4551
|
+
normalizeForBlindIndex,
|
|
1005
4552
|
transform,
|
|
1006
4553
|
transformAddress,
|
|
4554
|
+
transformSession,
|
|
1007
4555
|
validateAddress,
|
|
1008
4556
|
verify,
|
|
4557
|
+
verifyD5LOA,
|
|
1009
4558
|
verifyGrace,
|
|
1010
4559
|
verifyLOA,
|
|
1011
4560
|
verifySignature,
|
|
1012
4561
|
verifyWithGrace
|
|
1013
4562
|
});
|
|
4563
|
+
/*! Bundled license information:
|
|
4564
|
+
|
|
4565
|
+
@noble/curves/utils.js:
|
|
4566
|
+
@noble/curves/abstract/modular.js:
|
|
4567
|
+
@noble/curves/abstract/curve.js:
|
|
4568
|
+
@noble/curves/abstract/edwards.js:
|
|
4569
|
+
@noble/curves/abstract/montgomery.js:
|
|
4570
|
+
@noble/curves/ed25519.js:
|
|
4571
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
4572
|
+
|
|
4573
|
+
@noble/post-quantum/utils.js:
|
|
4574
|
+
@noble/post-quantum/_crystals.js:
|
|
4575
|
+
@noble/post-quantum/ml-kem.js:
|
|
4576
|
+
@noble/post-quantum/hybrid.js:
|
|
4577
|
+
(*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) *)
|
|
4578
|
+
*/
|