@oneaddress/conformance 3.2.0 → 3.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/dist/checks.d.ts +9 -1
- package/dist/checks.js +1489 -600
- package/dist/index.js +1485 -598
- package/package.json +1 -1
package/dist/checks.js
CHANGED
|
@@ -39,7 +39,8 @@ __export(checks_exports, {
|
|
|
39
39
|
markUnprovenChecks: () => markUnprovenChecks,
|
|
40
40
|
plannedCoreCheckIds: () => plannedCoreCheckIds,
|
|
41
41
|
runChecks: () => runChecks,
|
|
42
|
-
runChecksDetailed: () => runChecksDetailed
|
|
42
|
+
runChecksDetailed: () => runChecksDetailed,
|
|
43
|
+
signPullV2: () => signPullV2
|
|
43
44
|
});
|
|
44
45
|
module.exports = __toCommonJS(checks_exports);
|
|
45
46
|
var import_node_crypto = __toESM(require("crypto"));
|
|
@@ -48,42 +49,55 @@ var import_node_crypto = __toESM(require("crypto"));
|
|
|
48
49
|
function isBytes(a) {
|
|
49
50
|
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
50
51
|
}
|
|
52
|
+
var atitle = (title) => title ? `"${title}" ` : "";
|
|
51
53
|
function anumber(n, title = "") {
|
|
52
|
-
if (typeof n !== "number")
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const prefix = title && `"${title}" `;
|
|
58
|
-
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
59
|
-
}
|
|
54
|
+
if (typeof n !== "number")
|
|
55
|
+
throw new TypeError(atitle(title) + "expected number, got " + typeof n);
|
|
56
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
57
|
+
throw new RangeError(atitle(title) + "expected integer >= 0, got " + n);
|
|
58
|
+
return n;
|
|
60
59
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const needsLen = length !== void 0;
|
|
65
|
-
if (!bytes || needsLen && len !== length) {
|
|
66
|
-
const prefix = title && `"${title}" `;
|
|
67
|
-
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
68
|
-
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
69
|
-
const message = prefix + "expected Uint8Array" + ofLen + ", got " + got;
|
|
70
|
-
if (!bytes)
|
|
71
|
-
throw new TypeError(message);
|
|
72
|
-
throw new RangeError(message);
|
|
73
|
-
}
|
|
60
|
+
function abool(value, title = "") {
|
|
61
|
+
if (typeof value !== "boolean")
|
|
62
|
+
throw new TypeError(atitle(title) + "expected boolean, got type=" + typeof value);
|
|
74
63
|
return value;
|
|
75
64
|
}
|
|
65
|
+
function abytes(value, length, title = "") {
|
|
66
|
+
if (isBytes(value) && (length === void 0 || value.length === length))
|
|
67
|
+
return value;
|
|
68
|
+
if (length !== void 0)
|
|
69
|
+
anumber(length, "length");
|
|
70
|
+
const bytes = isBytes(value);
|
|
71
|
+
const ofLen = length !== void 0 ? ` of length ${length}` : "";
|
|
72
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
73
|
+
const message = atitle(title) + "expected Uint8Array" + ofLen + ", got " + got;
|
|
74
|
+
if (!bytes)
|
|
75
|
+
throw new TypeError(message);
|
|
76
|
+
throw new RangeError(message);
|
|
77
|
+
}
|
|
78
|
+
var aobject = (value, label) => {
|
|
79
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
80
|
+
throw new TypeError((label === "object" ? "" : `"${label}" `) + "expected object, got type=" + typeof value);
|
|
81
|
+
};
|
|
82
|
+
var aopts = (value, label) => {
|
|
83
|
+
aobject(value, label);
|
|
84
|
+
const proto = Object.getPrototypeOf(value);
|
|
85
|
+
if (proto !== Object.prototype && proto !== null)
|
|
86
|
+
throw new TypeError(`"${label}" expected plain object`);
|
|
87
|
+
if (Object.hasOwn(value, "__proto__"))
|
|
88
|
+
throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
89
|
+
};
|
|
76
90
|
function aexists(instance, checkFinished = true) {
|
|
77
91
|
if (instance.destroyed)
|
|
78
|
-
throw new Error("
|
|
92
|
+
throw new Error("hash was destroyed");
|
|
79
93
|
if (checkFinished && instance.finished)
|
|
80
|
-
throw new Error("
|
|
94
|
+
throw new Error("digest() was already called");
|
|
81
95
|
}
|
|
82
96
|
function aoutput(out, instance) {
|
|
83
|
-
abytes(out, void 0, "
|
|
97
|
+
abytes(out, void 0, "output");
|
|
84
98
|
const min = instance.outputLen;
|
|
85
|
-
if (out.length
|
|
86
|
-
throw new RangeError('"
|
|
99
|
+
if (!(out.length >= min)) {
|
|
100
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
87
101
|
}
|
|
88
102
|
}
|
|
89
103
|
function u32(arr) {
|
|
@@ -123,15 +137,8 @@ function bytesToHex(bytes) {
|
|
|
123
137
|
}
|
|
124
138
|
return hex;
|
|
125
139
|
}
|
|
126
|
-
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
127
140
|
function asciiToBase16(ch) {
|
|
128
|
-
|
|
129
|
-
return ch - asciis._0;
|
|
130
|
-
if (ch >= asciis.A && ch <= asciis.F)
|
|
131
|
-
return ch - (asciis.A - 10);
|
|
132
|
-
if (ch >= asciis.a && ch <= asciis.f)
|
|
133
|
-
return ch - (asciis.a - 10);
|
|
134
|
-
return;
|
|
141
|
+
return ch >= 48 && ch <= 57 ? ch - 48 : ch >= 65 && ch <= 70 ? ch - (65 - 10) : ch >= 97 && ch <= 102 ? ch - (97 - 10) : void 0;
|
|
135
142
|
}
|
|
136
143
|
function hexToBytes(hex) {
|
|
137
144
|
if (typeof hex !== "string")
|
|
@@ -176,7 +183,17 @@ function concatBytes(...arrays) {
|
|
|
176
183
|
}
|
|
177
184
|
return res;
|
|
178
185
|
}
|
|
186
|
+
function checkOpts(defaults, opts2, title = "opts") {
|
|
187
|
+
aopts(defaults, "defaults");
|
|
188
|
+
if (opts2 !== void 0)
|
|
189
|
+
aopts(opts2, title);
|
|
190
|
+
const merged = Object.assign(/* @__PURE__ */ Object.create(null), defaults, opts2);
|
|
191
|
+
return merged;
|
|
192
|
+
}
|
|
179
193
|
function createHasher(hashCons, info = {}) {
|
|
194
|
+
if (typeof hashCons !== "function")
|
|
195
|
+
throw new TypeError('"hashCons" expected function, got type=' + typeof hashCons);
|
|
196
|
+
info = checkOpts({}, info, "info");
|
|
180
197
|
const hashC = (msg, opts2) => hashCons(opts2).update(msg).digest();
|
|
181
198
|
const tmp = hashCons(void 0);
|
|
182
199
|
hashC.outputLen = tmp.outputLen;
|
|
@@ -202,8 +219,26 @@ var oidNist = (suffix) => ({
|
|
|
202
219
|
});
|
|
203
220
|
|
|
204
221
|
// ../../node_modules/@noble/curves/utils.js
|
|
222
|
+
function aarray(item, title, inner = () => {
|
|
223
|
+
}) {
|
|
224
|
+
if (!Array.isArray(item))
|
|
225
|
+
throw new TypeError(`"${title}" expected array, got type=${typeof item}`);
|
|
226
|
+
for (let i = 0; i < item.length; i++)
|
|
227
|
+
inner(item[i], `${title}[${i}]`);
|
|
228
|
+
return item;
|
|
229
|
+
}
|
|
205
230
|
var abytes2 = (value, length, title) => abytes(value, length, title);
|
|
206
231
|
var anumber2 = anumber;
|
|
232
|
+
function aobject2(value, title = "object") {
|
|
233
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
234
|
+
throw new TypeError(title === "object" ? "expected valid options object" : `"${title}" expected object, got type=${typeof value}`);
|
|
235
|
+
return value;
|
|
236
|
+
}
|
|
237
|
+
function afunction(value, title) {
|
|
238
|
+
if (typeof value !== "function")
|
|
239
|
+
throw new TypeError(`"${title}" is invalid: expected function, got ${typeof value}`);
|
|
240
|
+
return value;
|
|
241
|
+
}
|
|
207
242
|
var bytesToHex2 = bytesToHex;
|
|
208
243
|
var concatBytes2 = (...arrays) => concatBytes(...arrays);
|
|
209
244
|
var hexToBytes2 = (hex) => hexToBytes(hex);
|
|
@@ -211,11 +246,10 @@ var isBytes2 = isBytes;
|
|
|
211
246
|
var randomBytes2 = (bytesLength) => randomBytes(bytesLength);
|
|
212
247
|
var _0n = /* @__PURE__ */ BigInt(0);
|
|
213
248
|
var _1n = /* @__PURE__ */ BigInt(1);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
throw new TypeError(
|
|
218
|
-
}
|
|
249
|
+
var atitle2 = (title) => title ? `"${title}" ` : "";
|
|
250
|
+
function abool2(value, title = "") {
|
|
251
|
+
if (typeof value !== "boolean")
|
|
252
|
+
throw new TypeError(atitle2(title) + "expected boolean, got type=" + typeof value);
|
|
219
253
|
return value;
|
|
220
254
|
}
|
|
221
255
|
function abignumber(n) {
|
|
@@ -250,12 +284,13 @@ function bytesToNumberLE(bytes) {
|
|
|
250
284
|
function numberToBytesBE(n, len) {
|
|
251
285
|
anumber(len);
|
|
252
286
|
if (len === 0)
|
|
253
|
-
throw new
|
|
287
|
+
throw new Error("zero output length is invalid");
|
|
254
288
|
n = abignumber(n);
|
|
289
|
+
const expectedLen = len * 2;
|
|
255
290
|
const hex = n.toString(16);
|
|
256
|
-
if (hex.length >
|
|
257
|
-
throw new RangeError("number too large");
|
|
258
|
-
return hexToBytes(hex.padStart(
|
|
291
|
+
if (hex.length > expectedLen)
|
|
292
|
+
throw new RangeError("number is too large");
|
|
293
|
+
return hexToBytes(hex.padStart(expectedLen, "0"));
|
|
259
294
|
}
|
|
260
295
|
function numberToBytesLE(n, len) {
|
|
261
296
|
return numberToBytesBE(n, len).reverse();
|
|
@@ -284,7 +319,9 @@ function asciiToBytes(ascii) {
|
|
|
284
319
|
return charCode;
|
|
285
320
|
});
|
|
286
321
|
}
|
|
287
|
-
|
|
322
|
+
function isPosBig(n) {
|
|
323
|
+
return typeof n === "bigint" && _0n <= n;
|
|
324
|
+
}
|
|
288
325
|
function inRange(n, min, max) {
|
|
289
326
|
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
290
327
|
}
|
|
@@ -295,24 +332,27 @@ function aInRange(title, n, min, max) {
|
|
|
295
332
|
function bitLen(n) {
|
|
296
333
|
if (n < _0n)
|
|
297
334
|
throw new Error("expected non-negative bigint, got " + n);
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
function validateObject(object, fields = {}, optFields = {}) {
|
|
305
|
-
|
|
306
|
-
|
|
335
|
+
return n === _0n ? 0 : n.toString(2).length;
|
|
336
|
+
}
|
|
337
|
+
var bitMask = (n) => {
|
|
338
|
+
asafenumber(n, "n");
|
|
339
|
+
return (_1n << BigInt(n)) - _1n;
|
|
340
|
+
};
|
|
341
|
+
function validateObject(object, fields = {}, optFields = {}, title = "object") {
|
|
342
|
+
aobject2(object, title);
|
|
343
|
+
aobject2(fields, "fields");
|
|
344
|
+
aobject2(optFields, "optFields");
|
|
307
345
|
function checkField(fieldName, expectedType, isOpt) {
|
|
308
|
-
|
|
309
|
-
throw new TypeError(`param "${fieldName}" is invalid: expected own property`);
|
|
346
|
+
const label = title === "object" ? `param "${String(fieldName)}"` : `"${title}.${String(fieldName)}"`;
|
|
310
347
|
const val = object[fieldName];
|
|
348
|
+
if (!Object.hasOwn(object, fieldName) && (isOpt ? val !== void 0 : expectedType !== "function")) {
|
|
349
|
+
throw new TypeError(`${label} is invalid: expected own property`);
|
|
350
|
+
}
|
|
311
351
|
if (isOpt && val === void 0)
|
|
312
352
|
return;
|
|
313
353
|
const current = typeof val;
|
|
314
354
|
if (current !== expectedType || val === null)
|
|
315
|
-
throw new TypeError(
|
|
355
|
+
throw new TypeError(`${label} is invalid: expected ${expectedType}, got ${current}`);
|
|
316
356
|
}
|
|
317
357
|
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
318
358
|
iter(fields, false);
|
|
@@ -332,14 +372,64 @@ var _5n = /* @__PURE__ */ BigInt(5);
|
|
|
332
372
|
var _7n = /* @__PURE__ */ BigInt(7);
|
|
333
373
|
var _8n = /* @__PURE__ */ BigInt(8);
|
|
334
374
|
var _9n = /* @__PURE__ */ BigInt(9);
|
|
375
|
+
var _15n = /* @__PURE__ */ BigInt(15);
|
|
335
376
|
var _16n = /* @__PURE__ */ BigInt(16);
|
|
377
|
+
var POW_WINDOWED_MIN = /* @__PURE__ */ BigInt("0x10000000000000000");
|
|
336
378
|
function mod(a, b) {
|
|
337
379
|
if (b <= _0n2)
|
|
338
380
|
throw new Error("mod: expected positive modulus, got " + b);
|
|
339
381
|
const result = a % b;
|
|
340
382
|
return result >= _0n2 ? result : b + result;
|
|
341
383
|
}
|
|
384
|
+
function pow(num, power, modulo) {
|
|
385
|
+
if (modulo <= _1n2)
|
|
386
|
+
throw new Error("pow: expected modulus > 1, got " + modulo);
|
|
387
|
+
if (typeof power !== "bigint")
|
|
388
|
+
throw new TypeError("invalid exponent: expected bigint, got " + typeof power);
|
|
389
|
+
if (power < _0n2)
|
|
390
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
391
|
+
if (power === _0n2)
|
|
392
|
+
return _1n2;
|
|
393
|
+
if (power === _1n2)
|
|
394
|
+
return num;
|
|
395
|
+
let d = num % modulo;
|
|
396
|
+
if (d < _0n2)
|
|
397
|
+
d += modulo;
|
|
398
|
+
if (power < POW_WINDOWED_MIN) {
|
|
399
|
+
let p2 = _1n2;
|
|
400
|
+
while (power > _0n2) {
|
|
401
|
+
if (power & _1n2)
|
|
402
|
+
p2 = p2 * d % modulo;
|
|
403
|
+
d = d * d % modulo;
|
|
404
|
+
power >>= _1n2;
|
|
405
|
+
}
|
|
406
|
+
return p2;
|
|
407
|
+
}
|
|
408
|
+
const digits = [];
|
|
409
|
+
while (power > _0n2) {
|
|
410
|
+
digits.push(Number(power & _15n));
|
|
411
|
+
power >>= _4n;
|
|
412
|
+
}
|
|
413
|
+
const table = new Array(16);
|
|
414
|
+
table[0] = _1n2;
|
|
415
|
+
table[1] = d;
|
|
416
|
+
for (let i = 2; i < 16; i++)
|
|
417
|
+
table[i] = table[i - 1] * d % modulo;
|
|
418
|
+
let p = table[digits[digits.length - 1]];
|
|
419
|
+
for (let w = digits.length - 2; w >= 0; w--) {
|
|
420
|
+
p = p * p % modulo;
|
|
421
|
+
p = p * p % modulo;
|
|
422
|
+
p = p * p % modulo;
|
|
423
|
+
p = p * p % modulo;
|
|
424
|
+
const digit = digits[w];
|
|
425
|
+
if (digit !== 0)
|
|
426
|
+
p = p * table[digit] % modulo;
|
|
427
|
+
}
|
|
428
|
+
return p;
|
|
429
|
+
}
|
|
342
430
|
function pow2(x, power, modulo) {
|
|
431
|
+
if (modulo <= _1n2)
|
|
432
|
+
throw new Error("pow2: expected modulus > 1, got " + modulo);
|
|
343
433
|
if (power < _0n2)
|
|
344
434
|
throw new Error("pow2: expected non-negative exponent, got " + power);
|
|
345
435
|
let res = x;
|
|
@@ -352,28 +442,42 @@ function pow2(x, power, modulo) {
|
|
|
352
442
|
function invert(number, modulo) {
|
|
353
443
|
if (number === _0n2)
|
|
354
444
|
throw new Error("invert: expected non-zero number");
|
|
355
|
-
if (modulo <=
|
|
356
|
-
throw new Error("invert: expected
|
|
445
|
+
if (modulo <= _1n2)
|
|
446
|
+
throw new Error("invert: expected modulus > 1, got " + modulo);
|
|
357
447
|
let a = mod(number, modulo);
|
|
358
448
|
let b = modulo;
|
|
359
|
-
let x = _0n2,
|
|
449
|
+
let x = _0n2, u = _1n2;
|
|
360
450
|
while (a !== _0n2) {
|
|
361
451
|
const q = b / a;
|
|
362
452
|
const r = b - a * q;
|
|
363
453
|
const m = x - u * q;
|
|
364
|
-
|
|
365
|
-
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
454
|
+
b = a, a = r, x = u, u = m;
|
|
366
455
|
}
|
|
367
456
|
const gcd = b;
|
|
368
457
|
if (gcd !== _1n2)
|
|
369
458
|
throw new Error("invert: does not exist");
|
|
370
459
|
return mod(x, modulo);
|
|
371
460
|
}
|
|
461
|
+
function invertCt(a, prime) {
|
|
462
|
+
if (prime <= _1n2)
|
|
463
|
+
throw new Error("invertCt: expected prime modulus > 1, got " + prime);
|
|
464
|
+
const an = mod(a, prime);
|
|
465
|
+
if (an === _0n2)
|
|
466
|
+
throw new Error("invertCt: expected non-zero number");
|
|
467
|
+
const inverse = pow(an, prime - _2n, prime);
|
|
468
|
+
if (mod(an * inverse, prime) !== _1n2)
|
|
469
|
+
throw new Error("invertCt: does not exist");
|
|
470
|
+
return inverse;
|
|
471
|
+
}
|
|
372
472
|
function assertIsSquare(Fp2, root, n) {
|
|
373
473
|
const F2 = Fp2;
|
|
374
474
|
if (!F2.eql(F2.sqr(root), n))
|
|
375
475
|
throw new Error("Cannot find square root");
|
|
376
476
|
}
|
|
477
|
+
function aoddModulus(order, fnName) {
|
|
478
|
+
if ((order & _1n2) === _0n2)
|
|
479
|
+
throw new Error(fnName + ": expected odd modulus, got " + order);
|
|
480
|
+
}
|
|
377
481
|
function sqrt3mod4(Fp2, n) {
|
|
378
482
|
const F2 = Fp2;
|
|
379
483
|
const p1div4 = (F2.ORDER + _1n2) / _4n;
|
|
@@ -418,6 +522,7 @@ function sqrt9mod16(P) {
|
|
|
418
522
|
function tonelliShanks(P) {
|
|
419
523
|
if (P < _3n)
|
|
420
524
|
throw new Error("sqrt is not defined for small field");
|
|
525
|
+
aoddModulus(P, "tonelliShanks");
|
|
421
526
|
let Q2 = P - _1n2;
|
|
422
527
|
let S = 0;
|
|
423
528
|
while (Q2 % _2n === _0n2) {
|
|
@@ -446,7 +551,7 @@ function tonelliShanks(P) {
|
|
|
446
551
|
let R = F2.pow(n, Q1div2);
|
|
447
552
|
while (!F2.eql(t, F2.ONE)) {
|
|
448
553
|
if (F2.is0(t))
|
|
449
|
-
|
|
554
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
450
555
|
let i = 1;
|
|
451
556
|
let t_tmp = F2.sqr(t);
|
|
452
557
|
while (!F2.eql(t_tmp, F2.ONE)) {
|
|
@@ -466,6 +571,7 @@ function tonelliShanks(P) {
|
|
|
466
571
|
};
|
|
467
572
|
}
|
|
468
573
|
function FpSqrt(P) {
|
|
574
|
+
aoddModulus(P, "Fp.sqrt");
|
|
469
575
|
if (P % _4n === _3n)
|
|
470
576
|
return sqrt3mod4;
|
|
471
577
|
if (P % _8n === _5n)
|
|
@@ -495,43 +601,23 @@ var FIELD_FIELDS = [
|
|
|
495
601
|
"sqrN"
|
|
496
602
|
];
|
|
497
603
|
function validateField(field) {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
BITS: "number"
|
|
502
|
-
};
|
|
503
|
-
const opts2 = FIELD_FIELDS.reduce((map, val) => {
|
|
504
|
-
map[val] = "function";
|
|
505
|
-
return map;
|
|
506
|
-
}, initial);
|
|
507
|
-
validateObject(field, opts2);
|
|
604
|
+
aobject2(field, "field");
|
|
605
|
+
if (typeof field.ORDER !== "bigint")
|
|
606
|
+
throw new TypeError('param "ORDER" is invalid: expected bigint, got ' + typeof field.ORDER);
|
|
508
607
|
asafenumber(field.BYTES, "BYTES");
|
|
509
608
|
asafenumber(field.BITS, "BITS");
|
|
609
|
+
for (const name of FIELD_FIELDS)
|
|
610
|
+
afunction(field[name], "field." + name);
|
|
510
611
|
if (field.BYTES < 1 || field.BITS < 1)
|
|
511
612
|
throw new Error("invalid field: expected BYTES/BITS > 0");
|
|
512
613
|
if (field.ORDER <= _1n2)
|
|
513
614
|
throw new Error("invalid field: expected ORDER > 1, got " + field.ORDER);
|
|
514
615
|
return field;
|
|
515
616
|
}
|
|
516
|
-
function FpPow(Fp2, num, power) {
|
|
517
|
-
const F2 = Fp2;
|
|
518
|
-
if (power < _0n2)
|
|
519
|
-
throw new Error("invalid exponent, negatives unsupported");
|
|
520
|
-
if (power === _0n2)
|
|
521
|
-
return F2.ONE;
|
|
522
|
-
if (power === _1n2)
|
|
523
|
-
return num;
|
|
524
|
-
let p = F2.ONE;
|
|
525
|
-
let d = num;
|
|
526
|
-
while (power > _0n2) {
|
|
527
|
-
if (power & _1n2)
|
|
528
|
-
p = F2.mul(p, d);
|
|
529
|
-
d = F2.sqr(d);
|
|
530
|
-
power >>= _1n2;
|
|
531
|
-
}
|
|
532
|
-
return p;
|
|
533
|
-
}
|
|
534
617
|
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
618
|
+
validateField(Fp2);
|
|
619
|
+
aarray(nums, "nums");
|
|
620
|
+
abool2(passZero, "passZero");
|
|
535
621
|
const F2 = Fp2;
|
|
536
622
|
const inverted = new Array(nums.length).fill(passZero ? F2.ZERO : void 0);
|
|
537
623
|
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
@@ -550,7 +636,9 @@ function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
|
550
636
|
return inverted;
|
|
551
637
|
}
|
|
552
638
|
function FpLegendre(Fp2, n) {
|
|
639
|
+
validateField(Fp2);
|
|
553
640
|
const F2 = Fp2;
|
|
641
|
+
aoddModulus(F2.ORDER, "FpLegendre");
|
|
554
642
|
const p1mod2 = (F2.ORDER - _1n2) / _2n;
|
|
555
643
|
const powered = F2.pow(n, p1mod2);
|
|
556
644
|
const yes = F2.eql(powered, F2.ONE);
|
|
@@ -569,7 +657,7 @@ function nLength(n, nBitLength) {
|
|
|
569
657
|
throw new Error("invalid n length: expected positive bit length, got " + nBitLength);
|
|
570
658
|
const bits = bitLen(n);
|
|
571
659
|
if (nBitLength !== void 0 && nBitLength < bits)
|
|
572
|
-
throw new Error(`invalid n length: expected
|
|
660
|
+
throw new Error(`invalid n length: expected nBitLength (${nBitLength}) >= bitLen(n) (${bits})`);
|
|
573
661
|
const _nBitLength = nBitLength !== void 0 ? nBitLength : bits;
|
|
574
662
|
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
575
663
|
return { nBitLength: _nBitLength, nByteLength };
|
|
@@ -646,7 +734,7 @@ var _Field = class {
|
|
|
646
734
|
return mod(lhs * rhs, this.ORDER);
|
|
647
735
|
}
|
|
648
736
|
pow(num, power) {
|
|
649
|
-
return
|
|
737
|
+
return pow(num, power, this.ORDER);
|
|
650
738
|
}
|
|
651
739
|
div(lhs, rhs) {
|
|
652
740
|
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
@@ -700,201 +788,389 @@ var _Field = class {
|
|
|
700
788
|
}
|
|
701
789
|
// TODO: we don't need it here, move out to separate fn
|
|
702
790
|
invertBatch(lst) {
|
|
703
|
-
return FpInvertBatch(this, lst);
|
|
791
|
+
return FpInvertBatch(this, lst, true);
|
|
704
792
|
}
|
|
705
793
|
// We can't move this out because Fp6, Fp12 implement it
|
|
706
794
|
// and it's unclear what to return in there.
|
|
707
795
|
cmov(a, b, condition) {
|
|
708
|
-
|
|
796
|
+
abool2(condition, "condition");
|
|
709
797
|
return condition ? b : a;
|
|
710
798
|
}
|
|
711
799
|
};
|
|
712
|
-
Object.freeze(_Field.prototype);
|
|
713
800
|
function Field(ORDER, opts2 = {}) {
|
|
801
|
+
Object.freeze(_Field.prototype);
|
|
714
802
|
return new _Field(ORDER, opts2);
|
|
715
803
|
}
|
|
804
|
+
function getFieldBytesLength(fieldOrder) {
|
|
805
|
+
if (typeof fieldOrder !== "bigint")
|
|
806
|
+
throw new Error("field order must be bigint");
|
|
807
|
+
if (fieldOrder <= _1n2)
|
|
808
|
+
throw new Error("field order must be greater than 1");
|
|
809
|
+
const bitLength = bitLen(fieldOrder - _1n2);
|
|
810
|
+
return Math.ceil(bitLength / 8);
|
|
811
|
+
}
|
|
812
|
+
function getMinHashLength(fieldOrder) {
|
|
813
|
+
const length = getFieldBytesLength(fieldOrder);
|
|
814
|
+
return length + Math.ceil(length / 2);
|
|
815
|
+
}
|
|
816
|
+
function mapHashToField(key, fieldOrder, isLE2 = false) {
|
|
817
|
+
abytes2(key);
|
|
818
|
+
const len = key.length;
|
|
819
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
820
|
+
const minLen = Math.max(getMinHashLength(fieldOrder), 16);
|
|
821
|
+
if (len < minLen || len > 1024)
|
|
822
|
+
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
823
|
+
const num = isLE2 ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
|
824
|
+
const reduced = mod(num, fieldOrder - _1n2) + _1n2;
|
|
825
|
+
return isLE2 ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
826
|
+
}
|
|
716
827
|
|
|
717
828
|
// ../../node_modules/@noble/curves/abstract/curve.js
|
|
718
829
|
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
719
830
|
var _1n3 = /* @__PURE__ */ BigInt(1);
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
831
|
+
var _4n2 = /* @__PURE__ */ BigInt(4);
|
|
832
|
+
var BLIND_BYTES = 16;
|
|
833
|
+
var BLIND_BITS = 128;
|
|
834
|
+
var FW_WINDOW = 5;
|
|
835
|
+
var TABLE_BYTES_MAX = /* @__PURE__ */ (() => 2 ** 31)();
|
|
836
|
+
function validatePointCons(Point) {
|
|
837
|
+
const pc = Point;
|
|
838
|
+
if (typeof pc !== "function")
|
|
839
|
+
throw new TypeError('"Point" expected constructor, got type=' + typeof Point);
|
|
840
|
+
afunction(pc.fromAffine, "Point.fromAffine");
|
|
841
|
+
afunction(pc.fromBytes, "Point.fromBytes");
|
|
842
|
+
afunction(pc.fromHex, "Point.fromHex");
|
|
843
|
+
aobject2(pc.BASE, "Point.BASE");
|
|
844
|
+
aobject2(pc.ZERO, "Point.ZERO");
|
|
845
|
+
validateField(pc.Fp);
|
|
846
|
+
validateField(pc.Fn);
|
|
723
847
|
}
|
|
724
848
|
function normalizeZ(c, points) {
|
|
849
|
+
validatePointCons(c);
|
|
850
|
+
validateMSMPoints(points, c);
|
|
725
851
|
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
726
852
|
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
727
853
|
}
|
|
728
|
-
function validateW(W, bits) {
|
|
729
|
-
if (!Number.isSafeInteger(W) || W
|
|
730
|
-
throw new Error("invalid window size, expected [
|
|
854
|
+
function validateW(W, bits, min = 1) {
|
|
855
|
+
if (!Number.isSafeInteger(W) || W < min || W > bits)
|
|
856
|
+
throw new Error("invalid window size, expected [" + min + ".." + bits + "], got W=" + W);
|
|
857
|
+
}
|
|
858
|
+
function validateTableBytes(numPoints, fpBytes) {
|
|
859
|
+
const bytes = numPoints * (4 * fpBytes + 128);
|
|
860
|
+
if (bytes > TABLE_BYTES_MAX)
|
|
861
|
+
throw new Error("invalid window size: table would need ~" + Math.ceil(bytes / 2 ** 20) + " MiB, max " + TABLE_BYTES_MAX / 2 ** 20 + " MiB");
|
|
862
|
+
}
|
|
863
|
+
function probeRandomBytes(randomBytes4, length) {
|
|
864
|
+
if (randomBytes4 === void 0)
|
|
865
|
+
return void 0;
|
|
866
|
+
afunction(randomBytes4, "randomBytes");
|
|
867
|
+
try {
|
|
868
|
+
const probe = randomBytes4(length);
|
|
869
|
+
if (!isBytes2(probe) || probe.length !== length)
|
|
870
|
+
return void 0;
|
|
871
|
+
} catch {
|
|
872
|
+
return void 0;
|
|
873
|
+
}
|
|
874
|
+
return randomBytes4;
|
|
875
|
+
}
|
|
876
|
+
function validateMSMPoints(points, c) {
|
|
877
|
+
aarray(points, "points");
|
|
878
|
+
points.forEach((p, i) => {
|
|
879
|
+
if (!(p instanceof c))
|
|
880
|
+
throw new Error("invalid point at index " + i);
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
function validateMSMScalars(scalars, field, maxScalar) {
|
|
884
|
+
if (!Array.isArray(scalars))
|
|
885
|
+
throw new Error("array of scalars expected");
|
|
886
|
+
scalars.forEach((s, i) => {
|
|
887
|
+
const ok = maxScalar === void 0 ? field.isValid(s) : isPosBig(s) && s < maxScalar;
|
|
888
|
+
if (!ok)
|
|
889
|
+
throw new Error("invalid scalar at index " + i);
|
|
890
|
+
});
|
|
731
891
|
}
|
|
732
|
-
function calcWOpts(W, scalarBits) {
|
|
733
|
-
validateW(W, scalarBits);
|
|
734
|
-
const windows = Math.ceil(scalarBits / W) + 1;
|
|
735
|
-
const windowSize = 2 ** (W - 1);
|
|
736
|
-
const maxNumber = 2 ** W;
|
|
737
|
-
const mask = bitMask(W);
|
|
738
|
-
const shiftBy = BigInt(W);
|
|
739
|
-
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
740
|
-
}
|
|
741
|
-
function calcOffsets(n, window, wOpts) {
|
|
742
|
-
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
743
|
-
let wbits = Number(n & mask);
|
|
744
|
-
let nextN = n >> shiftBy;
|
|
745
|
-
if (wbits > windowSize) {
|
|
746
|
-
wbits -= maxNumber;
|
|
747
|
-
nextN += _1n3;
|
|
748
|
-
}
|
|
749
|
-
const offsetStart = window * windowSize;
|
|
750
|
-
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
751
|
-
const isZero = wbits === 0;
|
|
752
|
-
const isNeg = wbits < 0;
|
|
753
|
-
const isNegF = window % 2 !== 0;
|
|
754
|
-
const offsetF = offsetStart;
|
|
755
|
-
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
756
|
-
}
|
|
757
|
-
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
758
892
|
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
759
|
-
function
|
|
893
|
+
function getWindowSize(P) {
|
|
760
894
|
return pointWindowSizes.get(P) || 1;
|
|
761
895
|
}
|
|
762
|
-
function
|
|
896
|
+
function oddMultiples(p, size) {
|
|
897
|
+
const dbl = p.double();
|
|
898
|
+
const t = [p];
|
|
899
|
+
for (let j = 1; j < size; j++)
|
|
900
|
+
t.push(t[j - 1].add(dbl));
|
|
901
|
+
return t;
|
|
902
|
+
}
|
|
903
|
+
function wnafDigits(n, W) {
|
|
904
|
+
const size = 2 ** W;
|
|
905
|
+
const half = size / 2;
|
|
906
|
+
const mask = BigInt(size - 1);
|
|
907
|
+
const d = [];
|
|
908
|
+
while (n > _0n3) {
|
|
909
|
+
let w = 0;
|
|
910
|
+
if (n & _1n3) {
|
|
911
|
+
w = Number(n & mask);
|
|
912
|
+
if (w >= half)
|
|
913
|
+
w -= size;
|
|
914
|
+
n -= BigInt(w);
|
|
915
|
+
}
|
|
916
|
+
d.push(w);
|
|
917
|
+
n >>= _1n3;
|
|
918
|
+
}
|
|
919
|
+
return d;
|
|
920
|
+
}
|
|
921
|
+
function signedWindowDigits(n, W, windows) {
|
|
922
|
+
const size = 2 ** W;
|
|
923
|
+
const half = size / 2;
|
|
924
|
+
const mask = BigInt(size - 1);
|
|
925
|
+
const shiftBy = BigInt(W);
|
|
926
|
+
const d = [];
|
|
927
|
+
for (let w = 0; w < windows; w++) {
|
|
928
|
+
let v = Number(n & mask);
|
|
929
|
+
n >>= shiftBy;
|
|
930
|
+
if (v > half) {
|
|
931
|
+
v -= size;
|
|
932
|
+
n += _1n3;
|
|
933
|
+
}
|
|
934
|
+
d.push(v);
|
|
935
|
+
}
|
|
763
936
|
if (n !== _0n3)
|
|
764
|
-
throw new Error("invalid
|
|
937
|
+
throw new Error("invalid wnaf");
|
|
938
|
+
return d;
|
|
939
|
+
}
|
|
940
|
+
function wnafWalk(zero, tables, digits) {
|
|
941
|
+
let max = 0;
|
|
942
|
+
for (const d of digits)
|
|
943
|
+
max = Math.max(max, d.length);
|
|
944
|
+
let acc = zero;
|
|
945
|
+
for (let bit = max - 1; bit >= 0; bit--) {
|
|
946
|
+
if (bit !== max - 1)
|
|
947
|
+
acc = acc.double();
|
|
948
|
+
for (let i = 0; i < digits.length; i++) {
|
|
949
|
+
const w = digits[i][bit];
|
|
950
|
+
if (w) {
|
|
951
|
+
const item = tables[i][Math.abs(w) - 1 >> 1];
|
|
952
|
+
acc = acc.add(w < 0 ? item.negate() : item);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return acc;
|
|
765
957
|
}
|
|
766
|
-
var
|
|
958
|
+
var ScalarMultiplier = class {
|
|
767
959
|
// Parametrized with a given Point class (not individual point)
|
|
768
|
-
constructor(Point,
|
|
960
|
+
constructor(Point, randomBytes4) {
|
|
961
|
+
__publicField(this, "Point");
|
|
769
962
|
__publicField(this, "BASE");
|
|
770
963
|
__publicField(this, "ZERO");
|
|
771
|
-
__publicField(this, "
|
|
964
|
+
__publicField(this, "randomBytes");
|
|
965
|
+
__publicField(this, "wnafPrecomputes", /* @__PURE__ */ new WeakMap());
|
|
966
|
+
__publicField(this, "baseCanBeBlinded");
|
|
772
967
|
__publicField(this, "bits");
|
|
968
|
+
validatePointCons(Point);
|
|
969
|
+
this.randomBytes = probeRandomBytes(randomBytes4, BLIND_BYTES);
|
|
970
|
+
this.Point = Point;
|
|
773
971
|
this.BASE = Point.BASE;
|
|
774
972
|
this.ZERO = Point.ZERO;
|
|
775
|
-
this.
|
|
776
|
-
this.bits = bits;
|
|
777
|
-
}
|
|
778
|
-
// non-const time multiplication ladder
|
|
779
|
-
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
780
|
-
let d = elm;
|
|
781
|
-
while (n > _0n3) {
|
|
782
|
-
if (n & _1n3)
|
|
783
|
-
p = p.add(d);
|
|
784
|
-
d = d.double();
|
|
785
|
-
n >>= _1n3;
|
|
786
|
-
}
|
|
787
|
-
return p;
|
|
973
|
+
this.bits = Point.Fn.BITS;
|
|
788
974
|
}
|
|
789
975
|
/**
|
|
790
|
-
* Creates a wNAF precomputation window
|
|
791
|
-
*
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
* -
|
|
795
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
796
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
976
|
+
* Creates a signed fixed-window wNAF precomputation table: for every window w, the
|
|
977
|
+
* multiples `[1..2^(W−1)]⋅2^(w⋅W)⋅P`, flattened. All doublings are baked into the table,
|
|
978
|
+
* so cached multiplication is additions-only. `windows = ceil(bits/W) + 1`: the extra
|
|
979
|
+
* window absorbs the final carry of signed-digit recoding.
|
|
980
|
+
* For a 256-bit curve and W=6, the table is 44⋅32 = 1408 points.
|
|
797
981
|
* @param point - Point instance
|
|
798
982
|
* @param W - window size
|
|
799
|
-
* @
|
|
983
|
+
* @param bits - scalar bitlength the table must cover
|
|
800
984
|
*/
|
|
801
|
-
|
|
802
|
-
const
|
|
803
|
-
const
|
|
804
|
-
|
|
805
|
-
let base =
|
|
806
|
-
for (let
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
points.push(base);
|
|
985
|
+
buildWnafTable(point, W, bits) {
|
|
986
|
+
const windows = Math.ceil(bits / W) + 1;
|
|
987
|
+
const half = 2 ** (W - 1);
|
|
988
|
+
const comp = [];
|
|
989
|
+
let base = point;
|
|
990
|
+
for (let w = 0; w < windows; w++) {
|
|
991
|
+
let acc = base;
|
|
992
|
+
for (let i = 0; i < half; i++) {
|
|
993
|
+
comp.push(acc);
|
|
994
|
+
acc = acc.add(base);
|
|
812
995
|
}
|
|
813
|
-
|
|
996
|
+
base = comp[comp.length - 1].double();
|
|
814
997
|
}
|
|
815
|
-
return
|
|
998
|
+
return { W, bits, windows, comp };
|
|
816
999
|
}
|
|
817
1000
|
/**
|
|
818
|
-
* Implements ec multiplication using precomputed
|
|
819
|
-
*
|
|
820
|
-
*
|
|
1001
|
+
* Implements ec multiplication using precomputed signed fixed-window wNAF tables.
|
|
1002
|
+
* Constant-time: fixed window count with one table addition per window — zero digits feed
|
|
1003
|
+
* the fake accumulator — and no doublings; the lookup scans the whole window slice.
|
|
1004
|
+
* Scalar bounds are validated by the public entry points ({@link ScalarMultiplier.mulCT},
|
|
1005
|
+
* {@link ScalarMultiplier.mulCTBlinded}, {@link ScalarMultiplier.mulUnsafe});
|
|
1006
|
+
* signedWindowDigits throws if `n` exceeds the table.
|
|
821
1007
|
* @returns real and fake (for const-time) points
|
|
822
1008
|
*/
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
1009
|
+
wnafCachedCT(precomputes, n) {
|
|
1010
|
+
const { W, windows, comp } = precomputes;
|
|
1011
|
+
const half = 2 ** (W - 1);
|
|
1012
|
+
const digits = signedWindowDigits(n, W, windows);
|
|
826
1013
|
let p = this.ZERO;
|
|
827
1014
|
let f = this.BASE;
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
1015
|
+
for (let w = 0; w < windows; w++) {
|
|
1016
|
+
const digit = digits[w];
|
|
1017
|
+
const start = w * half;
|
|
1018
|
+
const idx = Math.abs(digit) - 1;
|
|
1019
|
+
let sel = comp[start];
|
|
1020
|
+
for (let i = 1; i < half; i++)
|
|
1021
|
+
sel = i === idx ? comp[start + i] : sel;
|
|
1022
|
+
const neg = sel.negate();
|
|
1023
|
+
if (digit === 0)
|
|
1024
|
+
f = f.add(comp[start]);
|
|
1025
|
+
else
|
|
1026
|
+
p = p.add(digit < 0 ? neg : sel);
|
|
837
1027
|
}
|
|
838
|
-
assert0(n);
|
|
839
1028
|
return { p, f };
|
|
840
1029
|
}
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
848
|
-
const wo = calcWOpts(W, this.bits);
|
|
849
|
-
for (let window = 0; window < wo.windows; window++) {
|
|
850
|
-
if (n === _0n3)
|
|
851
|
-
break;
|
|
852
|
-
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
853
|
-
n = nextN;
|
|
854
|
-
if (isZero) {
|
|
855
|
-
continue;
|
|
856
|
-
} else {
|
|
857
|
-
const item = precomputes[offset];
|
|
858
|
-
acc = acc.add(isNeg ? item.negate() : item);
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
assert0(n);
|
|
862
|
-
return acc;
|
|
863
|
-
}
|
|
864
|
-
getPrecomputes(W, point, transform) {
|
|
865
|
-
let comp = pointPrecomputes.get(point);
|
|
1030
|
+
// Cache key is point identity plus (W, bits); at most two entries exist per point (public-width
|
|
1031
|
+
// `Fn.BITS` and blinded `Fn.BITS + BLIND_BITS`). Callers must not reuse the same point with
|
|
1032
|
+
// incompatible `transform(...)` layouts and expect a separate cache entry.
|
|
1033
|
+
getWnafPrecomputes(W, point, bits, transform) {
|
|
1034
|
+
let entries = this.wnafPrecomputes.get(point);
|
|
1035
|
+
let comp = entries?.find((entry) => entry.W === W && entry.bits === bits);
|
|
866
1036
|
if (!comp) {
|
|
867
|
-
comp = this.
|
|
868
|
-
if (
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
1037
|
+
comp = this.buildWnafTable(point, W, bits);
|
|
1038
|
+
if (typeof transform === "function")
|
|
1039
|
+
comp = { ...comp, comp: transform(comp.comp) };
|
|
1040
|
+
if (!entries) {
|
|
1041
|
+
entries = [];
|
|
1042
|
+
this.wnafPrecomputes.set(point, entries);
|
|
872
1043
|
}
|
|
1044
|
+
entries.push(comp);
|
|
873
1045
|
}
|
|
874
1046
|
return comp;
|
|
875
1047
|
}
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
1048
|
+
assertPoint(point) {
|
|
1049
|
+
if (!(point instanceof this.Point))
|
|
1050
|
+
throw new TypeError('"point" expected Point instance, got type=' + typeof point);
|
|
1051
|
+
}
|
|
1052
|
+
// Shared prologue of the constant-time entry points. Rejects scalar 0: in key/signature-style
|
|
1053
|
+
// callers a zero scalar means broken upstream plumbing, and concrete Points already reject it.
|
|
1054
|
+
// Uses inRange instead of Fn.isValidNot0: validateField() only certifies the arithmetic subset.
|
|
1055
|
+
validateMulInput(point, scalar) {
|
|
1056
|
+
this.assertPoint(point);
|
|
1057
|
+
if (!inRange(scalar, _1n3, this.Point.Fn.ORDER))
|
|
1058
|
+
throw new Error("invalid scalar");
|
|
879
1059
|
}
|
|
880
|
-
|
|
881
|
-
|
|
1060
|
+
// Constant-time dispatch shared by mulCT / mulCTBlinded. Un-precomputed points (W===1, e.g.
|
|
1061
|
+
// ECDH peer keys) skip building a throwaway cached table in favor of a small fixed-window
|
|
1062
|
+
// multiply. `n` must be < 2^bits.
|
|
1063
|
+
runCT(point, n, bits, transform) {
|
|
1064
|
+
const W = getWindowSize(point);
|
|
882
1065
|
if (W === 1)
|
|
883
|
-
return this.
|
|
884
|
-
return this.
|
|
1066
|
+
return this.fixedWindowCT(point, n, bits);
|
|
1067
|
+
return this.wnafCachedCT(this.getWnafPrecomputes(W, point, bits, transform), n);
|
|
1068
|
+
}
|
|
1069
|
+
mulCT(point, scalar, transform) {
|
|
1070
|
+
this.validateMulInput(point, scalar);
|
|
1071
|
+
return this.runCT(point, scalar, this.bits, transform);
|
|
1072
|
+
}
|
|
1073
|
+
mulCTBlinded(point, scalar, transform) {
|
|
1074
|
+
this.validateMulInput(point, scalar);
|
|
1075
|
+
if (this.randomBytes === void 0)
|
|
1076
|
+
throw new Error("randomBytes is required for scalar blinding");
|
|
1077
|
+
const bits = this.Point.Fn.BITS + BLIND_BITS;
|
|
1078
|
+
const blind = this.randomBytes(BLIND_BYTES);
|
|
1079
|
+
if (!isBytes2(blind) || blind.length !== BLIND_BYTES)
|
|
1080
|
+
throw new Error("randomBytes returned invalid byte array");
|
|
1081
|
+
blind[0] = blind[0] & 63 | 128;
|
|
1082
|
+
const n = scalar + bytesToNumberBE(blind) * this.Point.Fn.ORDER;
|
|
1083
|
+
return this.runCT(point, n, bits, transform);
|
|
885
1084
|
}
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
1085
|
+
/**
|
|
1086
|
+
* Constant-time multiplication `n*point` for an un-precomputed point, via a small fixed window.
|
|
1087
|
+
* A cached wNAF table only pays off when reused; a flat 2^FW_WINDOW table (`size-1` adds) is
|
|
1088
|
+
* far cheaper to build for a single use. The point-operation sequence is independent of `n`:
|
|
1089
|
+
* build the table, then per window exactly FW_WINDOW doublings, a data-oblivious scan over
|
|
1090
|
+
* every table entry, and one addition (adds the identity when the window digit is 0 — never
|
|
1091
|
+
* skipped).
|
|
1092
|
+
*
|
|
1093
|
+
* `n` must be `< 2^bits`. Assumes complete addition (adding the identity costs the same as any
|
|
1094
|
+
* add), which holds for the Weierstrass/Edwards point types used here. The table is left in
|
|
1095
|
+
* projective form (no normalizeZ): normalizing this small a table costs more than the
|
|
1096
|
+
* mixed-add savings it would buy for a single multiply.
|
|
1097
|
+
* @returns real point `p`; `f` duplicates it only to match {@link wnafCachedCT}'s return shape
|
|
1098
|
+
* (this path needs no fake accumulator — its op-count is already scalar-independent).
|
|
1099
|
+
*/
|
|
1100
|
+
fixedWindowCT(point, n, bits) {
|
|
1101
|
+
const W = FW_WINDOW;
|
|
1102
|
+
const size = 1 << W;
|
|
1103
|
+
const mask = bitMask(W);
|
|
1104
|
+
const table = new Array(size);
|
|
1105
|
+
table[0] = this.ZERO;
|
|
1106
|
+
for (let i = 1; i < size; i++)
|
|
1107
|
+
table[i] = table[i - 1].add(point);
|
|
1108
|
+
const windows = Math.ceil(bits / W);
|
|
1109
|
+
let acc = this.ZERO;
|
|
1110
|
+
for (let window = windows - 1; window >= 0; window--) {
|
|
1111
|
+
if (window !== windows - 1)
|
|
1112
|
+
for (let d = 0; d < W; d++)
|
|
1113
|
+
acc = acc.double();
|
|
1114
|
+
const digit = Number(n >> BigInt(window * W) & mask);
|
|
1115
|
+
let sel = table[0];
|
|
1116
|
+
for (let i = 1; i < size; i++)
|
|
1117
|
+
sel = i === digit ? table[i] : sel;
|
|
1118
|
+
acc = acc.add(sel);
|
|
1119
|
+
}
|
|
1120
|
+
return { p: acc, f: acc };
|
|
1121
|
+
}
|
|
1122
|
+
shouldBlind(point, cofactor) {
|
|
1123
|
+
if (this.randomBytes === void 0)
|
|
1124
|
+
return false;
|
|
1125
|
+
if (cofactor === _1n3)
|
|
1126
|
+
return true;
|
|
1127
|
+
if (point !== this.BASE)
|
|
1128
|
+
return false;
|
|
1129
|
+
if (this.baseCanBeBlinded === void 0)
|
|
1130
|
+
this.baseCanBeBlinded = this.mulUnsafe(this.BASE, this.Point.Fn.ORDER).is0();
|
|
1131
|
+
return this.baseCanBeBlinded;
|
|
1132
|
+
}
|
|
1133
|
+
mulSecret(point, scalar, cofactor, transform) {
|
|
1134
|
+
return this.shouldBlind(point, cofactor) ? this.mulCTBlinded(point, scalar, transform) : this.mulCT(point, scalar, transform);
|
|
1135
|
+
}
|
|
1136
|
+
mulUnsafe(point, scalar, transform) {
|
|
1137
|
+
this.assertPoint(point);
|
|
1138
|
+
if (!isPosBig(scalar))
|
|
1139
|
+
throw new Error("invalid scalar");
|
|
1140
|
+
const W = getWindowSize(point);
|
|
1141
|
+
if (W === 1 || scalar >= this.Point.Fn.ORDER)
|
|
1142
|
+
return mulAddUnsafe(this.Point, [point], [scalar], true);
|
|
1143
|
+
const precomputes = this.getWnafPrecomputes(W, point, this.bits, transform);
|
|
1144
|
+
return this.wnafCachedCT(precomputes, scalar).p;
|
|
1145
|
+
}
|
|
1146
|
+
// Remembers the window size used for precomputed wNAF multiplication of the given point
|
|
1147
|
+
// and drops any previously built tables. Usually only the base point is precomputed.
|
|
1148
|
+
// W=1 resets the point to the un-precomputed (table-less) paths.
|
|
1149
|
+
// W is additionally capped so tables stay under ~2 GiB ({@link TABLE_BYTES_MAX}).
|
|
1150
|
+
setWindowSize(point, W) {
|
|
1151
|
+
this.assertPoint(point);
|
|
890
1152
|
validateW(W, this.bits);
|
|
891
|
-
|
|
892
|
-
|
|
1153
|
+
const windows = Math.ceil((this.bits + BLIND_BITS) / W) + 1;
|
|
1154
|
+
validateTableBytes(windows * 2 ** (W - 1), this.Point.Fp.BYTES);
|
|
1155
|
+
pointWindowSizes.set(point, W);
|
|
1156
|
+
this.wnafPrecomputes.delete(point);
|
|
893
1157
|
}
|
|
894
|
-
|
|
895
|
-
|
|
1158
|
+
// True when a window size is set: tables themselves are built lazily on first multiply.
|
|
1159
|
+
hasWindowSize(point) {
|
|
1160
|
+
return getWindowSize(point) !== 1;
|
|
896
1161
|
}
|
|
897
1162
|
};
|
|
1163
|
+
function mulAddUnsafe(c, points, scalars, allowOversized = false) {
|
|
1164
|
+
validatePointCons(c);
|
|
1165
|
+
validateMSMPoints(points, c);
|
|
1166
|
+
abool2(allowOversized, "allowOversized");
|
|
1167
|
+
validateMSMScalars(scalars, c.Fn, allowOversized ? c.Fn.ORDER ** _4n2 : void 0);
|
|
1168
|
+
if (points.length !== scalars.length)
|
|
1169
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
1170
|
+
const tables = points.map((p) => oddMultiples(p, 4));
|
|
1171
|
+
const digits = scalars.map((n) => wnafDigits(n, 4));
|
|
1172
|
+
return wnafWalk(c.ZERO, tables, digits);
|
|
1173
|
+
}
|
|
898
1174
|
function createField(order, field, isLE2) {
|
|
899
1175
|
if (field) {
|
|
900
1176
|
if (field.ORDER !== order)
|
|
@@ -906,13 +1182,16 @@ function createField(order, field, isLE2) {
|
|
|
906
1182
|
}
|
|
907
1183
|
}
|
|
908
1184
|
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
1185
|
+
if (type !== "weierstrass" && type !== "edwards")
|
|
1186
|
+
throw new Error('expected curve type "weierstrass" or "edwards"');
|
|
909
1187
|
if (FpFnLE === void 0)
|
|
910
1188
|
FpFnLE = type === "edwards";
|
|
911
1189
|
if (!CURVE || typeof CURVE !== "object")
|
|
912
1190
|
throw new Error(`expected valid ${type} CURVE object`);
|
|
1191
|
+
validateObject(curveOpts);
|
|
913
1192
|
for (const p of ["p", "n", "h"]) {
|
|
914
1193
|
const val = CURVE[p];
|
|
915
|
-
if (!(
|
|
1194
|
+
if (!(isPosBig(val) && val !== _0n3))
|
|
916
1195
|
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
917
1196
|
}
|
|
918
1197
|
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
@@ -937,6 +1216,7 @@ function createKeygen(randomSecretKey, getPublicKey) {
|
|
|
937
1216
|
var _0n4 = /* @__PURE__ */ BigInt(0);
|
|
938
1217
|
var _1n4 = /* @__PURE__ */ BigInt(1);
|
|
939
1218
|
var _2n2 = /* @__PURE__ */ BigInt(2);
|
|
1219
|
+
var _4n3 = /* @__PURE__ */ BigInt(4);
|
|
940
1220
|
var _8n2 = /* @__PURE__ */ BigInt(8);
|
|
941
1221
|
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
942
1222
|
const x2 = Fp2.sqr(x);
|
|
@@ -946,14 +1226,24 @@ function isEdValidXY(Fp2, CURVE, x, y) {
|
|
|
946
1226
|
return Fp2.eql(left, right);
|
|
947
1227
|
}
|
|
948
1228
|
function edwards(params, extraOpts = {}) {
|
|
1229
|
+
validateObject(extraOpts, {}, {}, "extraOpts");
|
|
949
1230
|
const opts2 = extraOpts;
|
|
950
1231
|
const validated = createCurveFields("edwards", params, opts2, opts2.FpFnLE);
|
|
951
1232
|
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
952
1233
|
let CURVE = validated.CURVE;
|
|
953
1234
|
const { h: cofactor } = CURVE;
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
1235
|
+
if (FpLegendre(Fp2, CURVE.a) !== 1)
|
|
1236
|
+
throw new Error("edwards: CURVE.a must be a square in Fp for complete addition formulas");
|
|
1237
|
+
if (FpLegendre(Fp2, CURVE.d) !== -1)
|
|
1238
|
+
throw new Error("edwards: CURVE.d must be a non-square in Fp for complete addition formulas");
|
|
1239
|
+
validateObject(opts2, {}, { uvRatio: "function", randomBytes: "function" });
|
|
1240
|
+
const randomBytes4 = opts2.randomBytes === void 0 ? randomBytes2 : opts2.randomBytes;
|
|
1241
|
+
const MASK = _2n2 << BigInt(Fp2.BYTES * 8) - _1n4;
|
|
1242
|
+
function isOdd(n) {
|
|
1243
|
+
if (!Fp2.isOdd)
|
|
1244
|
+
throw new Error("Field does not have .isOdd()");
|
|
1245
|
+
return Fp2.isOdd(n);
|
|
1246
|
+
}
|
|
957
1247
|
const uvRatio2 = opts2.uvRatio === void 0 ? (u, v) => {
|
|
958
1248
|
try {
|
|
959
1249
|
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
@@ -963,6 +1253,7 @@ function edwards(params, extraOpts = {}) {
|
|
|
963
1253
|
} : opts2.uvRatio;
|
|
964
1254
|
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
965
1255
|
throw new Error("bad curve params: generator point");
|
|
1256
|
+
const mulA = Fp2.eql(CURVE.a, Fp2.neg(Fp2.ONE)) ? (x) => Fp2.neg(x) : Fp2.eql(CURVE.a, Fp2.ONE) ? (x) => x : (x) => Fp2.mul(CURVE.a, x);
|
|
966
1257
|
function acoord(title, n, banZero = false) {
|
|
967
1258
|
const min = banZero ? _1n4 : _0n4;
|
|
968
1259
|
aInRange("coordinate " + title, n, min, MASK);
|
|
@@ -998,32 +1289,32 @@ function edwards(params, extraOpts = {}) {
|
|
|
998
1289
|
const { x, y } = p || {};
|
|
999
1290
|
acoord("x", x);
|
|
1000
1291
|
acoord("y", y);
|
|
1001
|
-
return new _Point(x, y,
|
|
1292
|
+
return new _Point(x, y, Fp2.ONE, Fp2.mul(x, y));
|
|
1002
1293
|
}
|
|
1003
1294
|
// Uses algo from RFC8032 5.1.3.
|
|
1004
1295
|
static fromBytes(bytes, zip215 = false) {
|
|
1005
1296
|
const len = Fp2.BYTES;
|
|
1006
1297
|
const { a, d } = CURVE;
|
|
1007
1298
|
bytes = copyBytes(abytes2(bytes, len, "point"));
|
|
1008
|
-
|
|
1299
|
+
abool2(zip215, "zip215");
|
|
1009
1300
|
const normed = copyBytes(bytes);
|
|
1010
1301
|
const lastByte = bytes[len - 1];
|
|
1011
1302
|
normed[len - 1] = lastByte & ~128;
|
|
1012
1303
|
const y = bytesToNumberLE(normed);
|
|
1013
1304
|
const max = zip215 ? MASK : Fp2.ORDER;
|
|
1014
1305
|
aInRange("point.y", y, _0n4, max);
|
|
1015
|
-
const y2 =
|
|
1016
|
-
const u =
|
|
1017
|
-
const v =
|
|
1306
|
+
const y2 = Fp2.sqr(y);
|
|
1307
|
+
const u = Fp2.sub(y2, Fp2.ONE);
|
|
1308
|
+
const v = Fp2.sub(Fp2.mulN(d, y2), a);
|
|
1018
1309
|
let { isValid, value: x } = uvRatio2(u, v);
|
|
1019
1310
|
if (!isValid)
|
|
1020
1311
|
throw new Error("bad point: invalid y coordinate");
|
|
1021
|
-
const isXOdd = (x
|
|
1312
|
+
const isXOdd = isOdd(x);
|
|
1022
1313
|
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
1023
|
-
if (!zip215 && x
|
|
1314
|
+
if (!zip215 && Fp2.is0(x) && isLastByteOdd)
|
|
1024
1315
|
throw new Error("bad point: x=0 and x_0=1");
|
|
1025
1316
|
if (isLastByteOdd !== isXOdd)
|
|
1026
|
-
x =
|
|
1317
|
+
x = Fp2.neg(x);
|
|
1027
1318
|
return _Point.fromAffine({ x, y });
|
|
1028
1319
|
}
|
|
1029
1320
|
static fromHex(hex, zip215 = false) {
|
|
@@ -1035,8 +1326,8 @@ function edwards(params, extraOpts = {}) {
|
|
|
1035
1326
|
get y() {
|
|
1036
1327
|
return this.toAffine().y;
|
|
1037
1328
|
}
|
|
1038
|
-
precompute(windowSize =
|
|
1039
|
-
wnaf.
|
|
1329
|
+
precompute(windowSize = 6, isLazy = true) {
|
|
1330
|
+
wnaf.setWindowSize(this, windowSize);
|
|
1040
1331
|
if (!isLazy)
|
|
1041
1332
|
this.multiply(_2n2);
|
|
1042
1333
|
return this;
|
|
@@ -1048,18 +1339,18 @@ function edwards(params, extraOpts = {}) {
|
|
|
1048
1339
|
if (p.is0())
|
|
1049
1340
|
throw new Error("bad point: ZERO");
|
|
1050
1341
|
const { X, Y, Z, T } = p;
|
|
1051
|
-
const X2 =
|
|
1052
|
-
const Y2 =
|
|
1053
|
-
const Z2 =
|
|
1054
|
-
const Z4 =
|
|
1055
|
-
const aX2 =
|
|
1056
|
-
const left =
|
|
1057
|
-
const right =
|
|
1058
|
-
if (left
|
|
1342
|
+
const X2 = Fp2.sqr(X);
|
|
1343
|
+
const Y2 = Fp2.sqr(Y);
|
|
1344
|
+
const Z2 = Fp2.sqr(Z);
|
|
1345
|
+
const Z4 = Fp2.sqr(Z2);
|
|
1346
|
+
const aX2 = Fp2.mul(X2, a);
|
|
1347
|
+
const left = Fp2.mul(Fp2.add(aX2, Y2), Z2);
|
|
1348
|
+
const right = Fp2.add(Z4, Fp2.mul(d, Fp2.mul(X2, Y2)));
|
|
1349
|
+
if (!Fp2.eql(left, right))
|
|
1059
1350
|
throw new Error("bad point: equation left != right (1)");
|
|
1060
|
-
const XY =
|
|
1061
|
-
const ZT =
|
|
1062
|
-
if (XY
|
|
1351
|
+
const XY = Fp2.mul(X, Y);
|
|
1352
|
+
const ZT = Fp2.mul(Z, T);
|
|
1353
|
+
if (!Fp2.eql(XY, ZT))
|
|
1063
1354
|
throw new Error("bad point: equation left != right (2)");
|
|
1064
1355
|
}
|
|
1065
1356
|
// Compare one point to another.
|
|
@@ -1067,37 +1358,36 @@ function edwards(params, extraOpts = {}) {
|
|
|
1067
1358
|
aedpoint(other);
|
|
1068
1359
|
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1069
1360
|
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
1070
|
-
const X1Z2 =
|
|
1071
|
-
const X2Z1 =
|
|
1072
|
-
const Y1Z2 =
|
|
1073
|
-
const Y2Z1 =
|
|
1074
|
-
return X1Z2
|
|
1361
|
+
const X1Z2 = Fp2.mul(X1, Z2);
|
|
1362
|
+
const X2Z1 = Fp2.mul(X2, Z1);
|
|
1363
|
+
const Y1Z2 = Fp2.mul(Y1, Z2);
|
|
1364
|
+
const Y2Z1 = Fp2.mul(Y2, Z1);
|
|
1365
|
+
return Fp2.eql(X1Z2, X2Z1) && Fp2.eql(Y1Z2, Y2Z1);
|
|
1075
1366
|
}
|
|
1076
1367
|
is0() {
|
|
1077
1368
|
return this.equals(_Point.ZERO);
|
|
1078
1369
|
}
|
|
1079
1370
|
negate() {
|
|
1080
|
-
return new _Point(
|
|
1371
|
+
return new _Point(Fp2.neg(this.X), this.Y, this.Z, Fp2.neg(this.T));
|
|
1081
1372
|
}
|
|
1082
1373
|
// Fast algo for doubling Extended Point.
|
|
1083
1374
|
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
1084
1375
|
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
1085
1376
|
double() {
|
|
1086
|
-
const { a } = CURVE;
|
|
1087
1377
|
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1088
|
-
const A =
|
|
1089
|
-
const
|
|
1090
|
-
const C =
|
|
1091
|
-
const D =
|
|
1092
|
-
const x1y1 = X1
|
|
1093
|
-
const E =
|
|
1094
|
-
const G = D
|
|
1095
|
-
const F2 = G
|
|
1096
|
-
const H = D
|
|
1097
|
-
const X3 =
|
|
1098
|
-
const Y3 =
|
|
1099
|
-
const T3 =
|
|
1100
|
-
const Z3 =
|
|
1378
|
+
const A = Fp2.sqr(X1);
|
|
1379
|
+
const B2 = Fp2.sqr(Y1);
|
|
1380
|
+
const C = Fp2.mul(Fp2.sqr(Z1), _2n2);
|
|
1381
|
+
const D = mulA(A);
|
|
1382
|
+
const x1y1 = Fp2.addN(X1, Y1);
|
|
1383
|
+
const E = Fp2.sub(Fp2.subN(Fp2.sqr(x1y1), A), B2);
|
|
1384
|
+
const G = Fp2.addN(D, B2);
|
|
1385
|
+
const F2 = Fp2.subN(G, C);
|
|
1386
|
+
const H = Fp2.subN(D, B2);
|
|
1387
|
+
const X3 = Fp2.mul(E, F2);
|
|
1388
|
+
const Y3 = Fp2.mul(G, H);
|
|
1389
|
+
const T3 = Fp2.mul(E, H);
|
|
1390
|
+
const Z3 = Fp2.mul(F2, G);
|
|
1101
1391
|
return new _Point(X3, Y3, Z3, T3);
|
|
1102
1392
|
}
|
|
1103
1393
|
// Fast algo for adding 2 Extended Points.
|
|
@@ -1105,21 +1395,21 @@ function edwards(params, extraOpts = {}) {
|
|
|
1105
1395
|
// Cost: 9M + 1*a + 1*d + 7add.
|
|
1106
1396
|
add(other) {
|
|
1107
1397
|
aedpoint(other);
|
|
1108
|
-
const {
|
|
1398
|
+
const { d } = CURVE;
|
|
1109
1399
|
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
1110
1400
|
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
1111
|
-
const A =
|
|
1112
|
-
const
|
|
1113
|
-
const C =
|
|
1114
|
-
const D =
|
|
1115
|
-
const E =
|
|
1116
|
-
const F2 = D
|
|
1117
|
-
const G = D
|
|
1118
|
-
const H =
|
|
1119
|
-
const X3 =
|
|
1120
|
-
const Y3 =
|
|
1121
|
-
const T3 =
|
|
1122
|
-
const Z3 =
|
|
1401
|
+
const A = Fp2.mul(X1, X2);
|
|
1402
|
+
const B2 = Fp2.mul(Y1, Y2);
|
|
1403
|
+
const C = Fp2.mul(Fp2.mulN(T1, d), T2);
|
|
1404
|
+
const D = Fp2.mul(Z1, Z2);
|
|
1405
|
+
const E = Fp2.sub(Fp2.subN(Fp2.mulN(Fp2.addN(X1, Y1), Fp2.addN(X2, Y2)), A), B2);
|
|
1406
|
+
const F2 = Fp2.subN(D, C);
|
|
1407
|
+
const G = Fp2.addN(D, C);
|
|
1408
|
+
const H = Fp2.sub(B2, mulA(A));
|
|
1409
|
+
const X3 = Fp2.mul(E, F2);
|
|
1410
|
+
const Y3 = Fp2.mul(G, H);
|
|
1411
|
+
const T3 = Fp2.mul(E, H);
|
|
1412
|
+
const Z3 = Fp2.mul(F2, G);
|
|
1123
1413
|
return new _Point(X3, Y3, Z3, T3);
|
|
1124
1414
|
}
|
|
1125
1415
|
subtract(other) {
|
|
@@ -1130,8 +1420,8 @@ function edwards(params, extraOpts = {}) {
|
|
|
1130
1420
|
multiply(scalar) {
|
|
1131
1421
|
if (!Fn2.isValidNot0(scalar))
|
|
1132
1422
|
throw new RangeError("invalid scalar: expected 1 <= sc < curve.n");
|
|
1133
|
-
const { p, f } = wnaf.
|
|
1134
|
-
return
|
|
1423
|
+
const { p, f } = wnaf.mulSecret(this, scalar, cofactor, normalize);
|
|
1424
|
+
return normalize([p, f])[0];
|
|
1135
1425
|
}
|
|
1136
1426
|
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1137
1427
|
// It's faster, but should only be used when you don't care about
|
|
@@ -1145,7 +1435,7 @@ function edwards(params, extraOpts = {}) {
|
|
|
1145
1435
|
return _Point.ZERO;
|
|
1146
1436
|
if (this.is0() || scalar === _1n4)
|
|
1147
1437
|
return this;
|
|
1148
|
-
return wnaf.
|
|
1438
|
+
return wnaf.mulUnsafe(this, scalar, normalize);
|
|
1149
1439
|
}
|
|
1150
1440
|
// Checks if point is of small order.
|
|
1151
1441
|
// If you add something to small order point, you will have "dirty"
|
|
@@ -1157,35 +1447,43 @@ function edwards(params, extraOpts = {}) {
|
|
|
1157
1447
|
// Multiplies point by curve order and checks if the result is 0.
|
|
1158
1448
|
// Returns `false` is the point is dirty.
|
|
1159
1449
|
isTorsionFree() {
|
|
1160
|
-
return wnaf.
|
|
1450
|
+
return wnaf.mulUnsafe(this, CURVE.n).is0();
|
|
1161
1451
|
}
|
|
1162
1452
|
// Converts Extended point to default (x, y) coordinates.
|
|
1163
1453
|
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
1164
1454
|
toAffine(invertedZ) {
|
|
1165
1455
|
const p = this;
|
|
1166
1456
|
let iz = invertedZ;
|
|
1457
|
+
if (iz != null && typeof iz !== "bigint")
|
|
1458
|
+
throw new TypeError('"invertedZ" expected bigint, got type=' + typeof iz);
|
|
1167
1459
|
const { X, Y, Z } = p;
|
|
1168
1460
|
const is0 = p.is0();
|
|
1169
1461
|
if (iz == null)
|
|
1170
|
-
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
1171
|
-
const x =
|
|
1172
|
-
const y =
|
|
1462
|
+
iz = is0 ? Fp2.create(_8n2) : Fp2.inv(Z);
|
|
1463
|
+
const x = Fp2.mul(X, iz);
|
|
1464
|
+
const y = Fp2.mul(Y, iz);
|
|
1173
1465
|
const zz = Fp2.mul(Z, iz);
|
|
1174
1466
|
if (is0)
|
|
1175
|
-
return { x:
|
|
1176
|
-
if (zz
|
|
1467
|
+
return { x: Fp2.ZERO, y: Fp2.ONE };
|
|
1468
|
+
if (!Fp2.eql(zz, Fp2.ONE))
|
|
1177
1469
|
throw new Error("invZ was invalid");
|
|
1178
1470
|
return { x, y };
|
|
1179
1471
|
}
|
|
1180
1472
|
clearCofactor() {
|
|
1181
1473
|
if (cofactor === _1n4)
|
|
1182
1474
|
return this;
|
|
1475
|
+
if (cofactor === _2n2)
|
|
1476
|
+
return this.double();
|
|
1477
|
+
if (cofactor === _4n3)
|
|
1478
|
+
return this.double().double();
|
|
1479
|
+
if (cofactor === _8n2)
|
|
1480
|
+
return this.double().double().double();
|
|
1183
1481
|
return this.multiplyUnsafe(cofactor);
|
|
1184
1482
|
}
|
|
1185
1483
|
toBytes() {
|
|
1186
1484
|
const { x, y } = this.toAffine();
|
|
1187
1485
|
const bytes = Fp2.toBytes(y);
|
|
1188
|
-
bytes[bytes.length - 1] |= x
|
|
1486
|
+
bytes[bytes.length - 1] |= isOdd(x) ? 128 : 0;
|
|
1189
1487
|
return bytes;
|
|
1190
1488
|
}
|
|
1191
1489
|
toHex() {
|
|
@@ -1195,19 +1493,15 @@ function edwards(params, extraOpts = {}) {
|
|
|
1195
1493
|
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1196
1494
|
}
|
|
1197
1495
|
};
|
|
1198
|
-
|
|
1199
|
-
__publicField(_Point, "
|
|
1200
|
-
// zero / infinity / identity point
|
|
1201
|
-
__publicField(_Point, "ZERO", new _Point(_0n4, _1n4, _1n4, _0n4));
|
|
1202
|
-
// 0, 1, 1, 0
|
|
1203
|
-
// math field
|
|
1496
|
+
__publicField(_Point, "BASE", new _Point(CURVE.Gx, CURVE.Gy, Fp2.ONE, Fp2.mul(CURVE.Gx, CURVE.Gy)));
|
|
1497
|
+
__publicField(_Point, "ZERO", new _Point(Fp2.ZERO, Fp2.ONE, Fp2.ONE, Fp2.ZERO));
|
|
1204
1498
|
__publicField(_Point, "Fp", Fp2);
|
|
1205
|
-
// scalar field
|
|
1206
1499
|
__publicField(_Point, "Fn", Fn2);
|
|
1207
1500
|
let Point = _Point;
|
|
1208
|
-
const
|
|
1209
|
-
|
|
1210
|
-
|
|
1501
|
+
const normalize = (points) => normalizeZ(Point, points);
|
|
1502
|
+
const wnaf = new ScalarMultiplier(Point, randomBytes4);
|
|
1503
|
+
if (wnaf.bits >= 6)
|
|
1504
|
+
Point.BASE.precompute(6);
|
|
1211
1505
|
Object.freeze(Point.prototype);
|
|
1212
1506
|
Object.freeze(Point);
|
|
1213
1507
|
return Point;
|
|
@@ -1294,9 +1588,21 @@ __publicField(PrimeEdwardsPoint, "Fp");
|
|
|
1294
1588
|
__publicField(PrimeEdwardsPoint, "Fn");
|
|
1295
1589
|
|
|
1296
1590
|
// ../../node_modules/@noble/curves/abstract/montgomery.js
|
|
1297
|
-
var _0n5 = BigInt(0);
|
|
1298
|
-
var _1n5 = BigInt(1);
|
|
1299
|
-
var _2n3 = BigInt(2);
|
|
1591
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
1592
|
+
var _1n5 = /* @__PURE__ */ BigInt(1);
|
|
1593
|
+
var _2n3 = /* @__PURE__ */ BigInt(2);
|
|
1594
|
+
function cmask(P, swap) {
|
|
1595
|
+
return P + swap - (swap >> _1n5 << _1n5);
|
|
1596
|
+
}
|
|
1597
|
+
function cswap(P) {
|
|
1598
|
+
const offset = BigInt(6) * P;
|
|
1599
|
+
return (mask, x_2, x_3) => {
|
|
1600
|
+
const sum = x_2 + x_3;
|
|
1601
|
+
const d = offset + x_3 - x_2;
|
|
1602
|
+
const a = (d * mask + x_2) % P;
|
|
1603
|
+
return { x_2: a, x_3: sum - a };
|
|
1604
|
+
};
|
|
1605
|
+
}
|
|
1300
1606
|
function validateOpts(curve) {
|
|
1301
1607
|
validateObject(curve, {
|
|
1302
1608
|
P: "bigint",
|
|
@@ -1304,23 +1610,26 @@ function validateOpts(curve) {
|
|
|
1304
1610
|
adjustScalarBytes: "function",
|
|
1305
1611
|
powPminus2: "function"
|
|
1306
1612
|
}, {
|
|
1307
|
-
randomBytes: "function"
|
|
1613
|
+
randomBytes: "function",
|
|
1614
|
+
scalarMultBase: "function"
|
|
1308
1615
|
});
|
|
1309
1616
|
return Object.freeze({ ...curve });
|
|
1310
1617
|
}
|
|
1311
1618
|
function montgomery(curveDef) {
|
|
1312
1619
|
const CURVE = validateOpts(curveDef);
|
|
1313
1620
|
const { P, type, adjustScalarBytes: adjustScalarBytes2, powPminus2, randomBytes: rand } = CURVE;
|
|
1621
|
+
const mulBaseHook = CURVE.scalarMultBase;
|
|
1314
1622
|
const is25519 = type === "x25519";
|
|
1315
1623
|
if (!is25519 && type !== "x448")
|
|
1316
1624
|
throw new Error("invalid type");
|
|
1317
1625
|
const randomBytes_ = rand === void 0 ? randomBytes2 : rand;
|
|
1318
1626
|
const montgomeryBits = is25519 ? 255 : 448;
|
|
1627
|
+
const swap = cswap(P);
|
|
1319
1628
|
const fieldLen = is25519 ? 32 : 56;
|
|
1320
1629
|
const Gu = is25519 ? BigInt(9) : BigInt(5);
|
|
1321
1630
|
const a24 = is25519 ? BigInt(121665) : BigInt(39081);
|
|
1322
1631
|
const minScalar = is25519 ? _2n3 ** BigInt(254) : _2n3 ** BigInt(447);
|
|
1323
|
-
const maxAdded = is25519 ? BigInt(8) * _2n3 ** BigInt(251) - _1n5 : BigInt(4) * _2n3 ** BigInt(445) - _1n5;
|
|
1632
|
+
const maxAdded = is25519 ? BigInt(8) * (_2n3 ** BigInt(251) - _1n5) : BigInt(4) * (_2n3 ** BigInt(445) - _1n5);
|
|
1324
1633
|
const maxScalar = minScalar + maxAdded + _1n5;
|
|
1325
1634
|
const modP = (n) => mod(n, P);
|
|
1326
1635
|
const GuBytes = encodeU(Gu);
|
|
@@ -1336,23 +1645,34 @@ function montgomery(curveDef) {
|
|
|
1336
1645
|
function decodeScalar(scalar) {
|
|
1337
1646
|
return bytesToNumberLE(adjustScalarBytes2(copyBytes(abytes2(scalar, fieldLen, "scalar"))));
|
|
1338
1647
|
}
|
|
1648
|
+
const lowOrderU = new Set(is25519 ? [
|
|
1649
|
+
_0n5,
|
|
1650
|
+
_1n5,
|
|
1651
|
+
P - _1n5,
|
|
1652
|
+
BigInt("325606250916557431795983626356110631294008115727848805560023387167927233504"),
|
|
1653
|
+
BigInt("39382357235489614581723060781553021112529911719440698176882885853963445705823")
|
|
1654
|
+
] : [_0n5, _1n5, P - _1n5]);
|
|
1339
1655
|
function scalarMult(scalar, u) {
|
|
1340
|
-
const
|
|
1656
|
+
const pointU = decodeU(u);
|
|
1657
|
+
if (lowOrderU.has(pointU))
|
|
1658
|
+
throw new Error("invalid private or public key received");
|
|
1659
|
+
const pu = montgomeryLadder(pointU, decodeScalar(scalar));
|
|
1341
1660
|
if (pu === _0n5)
|
|
1342
1661
|
throw new Error("invalid private or public key received");
|
|
1343
1662
|
return encodeU(pu);
|
|
1344
1663
|
}
|
|
1345
1664
|
function scalarMultBase(scalar) {
|
|
1346
|
-
|
|
1665
|
+
if (mulBaseHook === void 0)
|
|
1666
|
+
return scalarMult(scalar, GuBytes);
|
|
1667
|
+
const k = decodeScalar(scalar);
|
|
1668
|
+
aInRange("scalar", k, minScalar, maxScalar);
|
|
1669
|
+
const pu = modP(mulBaseHook(k));
|
|
1670
|
+
if (pu === _0n5)
|
|
1671
|
+
throw new Error("invalid private or public key received");
|
|
1672
|
+
return encodeU(pu);
|
|
1347
1673
|
}
|
|
1348
1674
|
const getPublicKey = scalarMultBase;
|
|
1349
1675
|
const getSharedSecret = scalarMult;
|
|
1350
|
-
function cswap(swap, x_2, x_3) {
|
|
1351
|
-
const dummy = modP(swap * (x_2 - x_3));
|
|
1352
|
-
x_2 = modP(x_2 - dummy);
|
|
1353
|
-
x_3 = modP(x_3 + dummy);
|
|
1354
|
-
return { x_2, x_3 };
|
|
1355
|
-
}
|
|
1356
1676
|
function montgomeryLadder(u, scalar) {
|
|
1357
1677
|
aInRange("u", u, _0n5, P);
|
|
1358
1678
|
aInRange("scalar", scalar, minScalar, maxScalar);
|
|
@@ -1362,22 +1682,20 @@ function montgomery(curveDef) {
|
|
|
1362
1682
|
let z_2 = _0n5;
|
|
1363
1683
|
let x_3 = u;
|
|
1364
1684
|
let z_3 = _1n5;
|
|
1365
|
-
|
|
1685
|
+
const kx = k ^ k >> _1n5;
|
|
1366
1686
|
for (let t = BigInt(montgomeryBits - 1); t >= _0n5; t--) {
|
|
1367
|
-
const
|
|
1368
|
-
swap
|
|
1369
|
-
({ x_2, x_3 } =
|
|
1370
|
-
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
1371
|
-
swap = k_t;
|
|
1687
|
+
const mask2 = cmask(P, kx >> t);
|
|
1688
|
+
({ x_2, x_3 } = swap(mask2, x_2, x_3));
|
|
1689
|
+
({ x_2: z_2, x_3: z_3 } = swap(mask2, z_2, z_3));
|
|
1372
1690
|
const A = x_2 + z_2;
|
|
1373
1691
|
const AA = modP(A * A);
|
|
1374
|
-
const
|
|
1375
|
-
const BB = modP(
|
|
1692
|
+
const B2 = x_2 - z_2;
|
|
1693
|
+
const BB = modP(B2 * B2);
|
|
1376
1694
|
const E = AA - BB;
|
|
1377
1695
|
const C = x_3 + z_3;
|
|
1378
1696
|
const D = x_3 - z_3;
|
|
1379
1697
|
const DA = modP(D * A);
|
|
1380
|
-
const CB = modP(C *
|
|
1698
|
+
const CB = modP(C * B2);
|
|
1381
1699
|
const dacb = DA + CB;
|
|
1382
1700
|
const da_cb = DA - CB;
|
|
1383
1701
|
x_3 = modP(dacb * dacb);
|
|
@@ -1385,8 +1703,9 @@ function montgomery(curveDef) {
|
|
|
1385
1703
|
x_2 = modP(AA * BB);
|
|
1386
1704
|
z_2 = modP(E * (AA + modP(a24 * E)));
|
|
1387
1705
|
}
|
|
1388
|
-
|
|
1389
|
-
({ x_2
|
|
1706
|
+
const mask = cmask(P, k);
|
|
1707
|
+
({ x_2, x_3 } = swap(mask, x_2, x_3));
|
|
1708
|
+
({ x_2: z_2, x_3: z_3 } = swap(mask, z_2, z_3));
|
|
1390
1709
|
const z2 = powPminus2(z_2);
|
|
1391
1710
|
return modP(x_2 * z2);
|
|
1392
1711
|
}
|
|
@@ -1415,6 +1734,49 @@ function montgomery(curveDef) {
|
|
|
1415
1734
|
});
|
|
1416
1735
|
}
|
|
1417
1736
|
|
|
1737
|
+
// ../../node_modules/@noble/hashes/_u64.js
|
|
1738
|
+
var U32_MASK64 = /* @__PURE__ */ (() => BigInt(2 ** 32 - 1))();
|
|
1739
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
1740
|
+
function fromBig(n, le = false) {
|
|
1741
|
+
if (le)
|
|
1742
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
1743
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
1744
|
+
}
|
|
1745
|
+
function split(lst, le = false) {
|
|
1746
|
+
const len = lst.length;
|
|
1747
|
+
let Ah = new Uint32Array(len);
|
|
1748
|
+
let Al = new Uint32Array(len);
|
|
1749
|
+
for (let i = 0; i < len; i++) {
|
|
1750
|
+
const { h, l } = fromBig(lst[i], le);
|
|
1751
|
+
[Ah[i], Al[i]] = [h, l];
|
|
1752
|
+
}
|
|
1753
|
+
return [Ah, Al];
|
|
1754
|
+
}
|
|
1755
|
+
var fromNumH = (n) => n / 2 ** 32 | 0;
|
|
1756
|
+
var fromNumL = (n) => n >>> 0;
|
|
1757
|
+
function setU64FromNum(view, byteOffset, n, isLE2) {
|
|
1758
|
+
const h = fromNumH(n);
|
|
1759
|
+
const l = fromNumL(n);
|
|
1760
|
+
view.setUint32(byteOffset, isLE2 ? l : h, isLE2);
|
|
1761
|
+
view.setUint32(byteOffset + 4, isLE2 ? h : l, isLE2);
|
|
1762
|
+
}
|
|
1763
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
1764
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1765
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
1766
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1767
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
1768
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
1769
|
+
function add(Ah, Al, Bh, Bl) {
|
|
1770
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1771
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
1772
|
+
}
|
|
1773
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1774
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
1775
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1776
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
1777
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1778
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
1779
|
+
|
|
1418
1780
|
// ../../node_modules/@noble/hashes/_md.js
|
|
1419
1781
|
var HashMD = class {
|
|
1420
1782
|
constructor(blockLen, outputLen, padOffset, isLE2) {
|
|
@@ -1442,24 +1804,28 @@ var HashMD = class {
|
|
|
1442
1804
|
abytes(data);
|
|
1443
1805
|
const { view, buffer, blockLen } = this;
|
|
1444
1806
|
const len = data.length;
|
|
1807
|
+
let processed = false;
|
|
1445
1808
|
for (let pos = 0; pos < len; ) {
|
|
1446
1809
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
1447
1810
|
if (take === blockLen) {
|
|
1448
1811
|
const dataView = createView(data);
|
|
1449
1812
|
for (; blockLen <= len - pos; pos += blockLen)
|
|
1450
1813
|
this.process(dataView, pos);
|
|
1814
|
+
processed = true;
|
|
1451
1815
|
continue;
|
|
1452
1816
|
}
|
|
1453
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
1817
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
1454
1818
|
this.pos += take;
|
|
1455
1819
|
pos += take;
|
|
1456
1820
|
if (this.pos === blockLen) {
|
|
1457
1821
|
this.process(view, 0);
|
|
1458
1822
|
this.pos = 0;
|
|
1823
|
+
processed = true;
|
|
1459
1824
|
}
|
|
1460
1825
|
}
|
|
1461
1826
|
this.length += data.length;
|
|
1462
|
-
|
|
1827
|
+
if (processed)
|
|
1828
|
+
this.roundClean();
|
|
1463
1829
|
return this;
|
|
1464
1830
|
}
|
|
1465
1831
|
digestInto(out) {
|
|
@@ -1469,23 +1835,20 @@ var HashMD = class {
|
|
|
1469
1835
|
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
|
1470
1836
|
let { pos } = this;
|
|
1471
1837
|
buffer[pos++] = 128;
|
|
1472
|
-
|
|
1838
|
+
buffer.fill(0, pos);
|
|
1473
1839
|
if (this.padOffset > blockLen - pos) {
|
|
1474
1840
|
this.process(view, 0);
|
|
1475
|
-
|
|
1841
|
+
buffer.fill(0);
|
|
1476
1842
|
}
|
|
1477
|
-
|
|
1478
|
-
buffer[i] = 0;
|
|
1479
|
-
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE2);
|
|
1843
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE2);
|
|
1480
1844
|
this.process(view, 0);
|
|
1481
|
-
|
|
1845
|
+
this.roundClean();
|
|
1846
|
+
const oview = out === buffer ? view : createView(out);
|
|
1482
1847
|
const len = this.outputLen;
|
|
1483
|
-
if (len % 4)
|
|
1484
|
-
throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
1485
1848
|
const outLen = len / 4;
|
|
1486
1849
|
const state = this.get();
|
|
1487
|
-
if (outLen > state.length)
|
|
1488
|
-
throw new Error("
|
|
1850
|
+
if (len % 4 || outLen > state.length)
|
|
1851
|
+
throw new Error("invalid outputLen");
|
|
1489
1852
|
for (let i = 0; i < outLen; i++)
|
|
1490
1853
|
oview.setUint32(4 * i, state[i], isLE2);
|
|
1491
1854
|
}
|
|
@@ -1496,15 +1859,13 @@ var HashMD = class {
|
|
|
1496
1859
|
this.destroy();
|
|
1497
1860
|
return res;
|
|
1498
1861
|
}
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
to.set(...this.get());
|
|
1502
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
1862
|
+
_cloneIntoMeta(to) {
|
|
1863
|
+
const { buffer, length, finished, destroyed, pos } = this;
|
|
1503
1864
|
to.destroyed = destroyed;
|
|
1504
1865
|
to.finished = finished;
|
|
1505
1866
|
to.length = length;
|
|
1506
1867
|
to.pos = pos;
|
|
1507
|
-
if (
|
|
1868
|
+
if (pos)
|
|
1508
1869
|
to.buffer.set(buffer);
|
|
1509
1870
|
return to;
|
|
1510
1871
|
}
|
|
@@ -1531,45 +1892,6 @@ var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
|
1531
1892
|
327033209
|
|
1532
1893
|
]);
|
|
1533
1894
|
|
|
1534
|
-
// ../../node_modules/@noble/hashes/_u64.js
|
|
1535
|
-
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
1536
|
-
var _32n = /* @__PURE__ */ BigInt(32);
|
|
1537
|
-
function fromBig(n, le = false) {
|
|
1538
|
-
if (le)
|
|
1539
|
-
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
1540
|
-
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
1541
|
-
}
|
|
1542
|
-
function split(lst, le = false) {
|
|
1543
|
-
const len = lst.length;
|
|
1544
|
-
let Ah = new Uint32Array(len);
|
|
1545
|
-
let Al = new Uint32Array(len);
|
|
1546
|
-
for (let i = 0; i < len; i++) {
|
|
1547
|
-
const { h, l } = fromBig(lst[i], le);
|
|
1548
|
-
[Ah[i], Al[i]] = [h, l];
|
|
1549
|
-
}
|
|
1550
|
-
return [Ah, Al];
|
|
1551
|
-
}
|
|
1552
|
-
var shrSH = (h, _l, s) => h >>> s;
|
|
1553
|
-
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1554
|
-
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
1555
|
-
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1556
|
-
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
1557
|
-
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
1558
|
-
var rotlSH = (h, l, s) => h << s | l >>> 32 - s;
|
|
1559
|
-
var rotlSL = (h, l, s) => l << s | h >>> 32 - s;
|
|
1560
|
-
var rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
|
|
1561
|
-
var rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
|
|
1562
|
-
function add(Ah, Al, Bh, Bl) {
|
|
1563
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1564
|
-
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
1565
|
-
}
|
|
1566
|
-
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1567
|
-
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
1568
|
-
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1569
|
-
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
1570
|
-
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1571
|
-
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
1572
|
-
|
|
1573
1895
|
// ../../node_modules/@noble/hashes/sha2.js
|
|
1574
1896
|
var K512 = /* @__PURE__ */ (() => split([
|
|
1575
1897
|
"0x428a2f98d728ae22",
|
|
@@ -1658,8 +1980,45 @@ var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
|
1658
1980
|
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1659
1981
|
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1660
1982
|
var SHA2_64B = class extends HashMD {
|
|
1661
|
-
constructor(outputLen) {
|
|
1983
|
+
constructor(outputLen, IV) {
|
|
1662
1984
|
super(128, outputLen, 16, false);
|
|
1985
|
+
// We cannot use array here since array allows indexing by variable
|
|
1986
|
+
// which means optimizer/compiler cannot use registers.
|
|
1987
|
+
// h -- high 32 bits, l -- low 32 bits
|
|
1988
|
+
// Numeric initializers matter: starting the fields as `undefined` changes
|
|
1989
|
+
// V8's field representation and slows hashing down (measured on sha256).
|
|
1990
|
+
__publicField(this, "Ah", 0);
|
|
1991
|
+
__publicField(this, "Al", 0);
|
|
1992
|
+
__publicField(this, "Bh", 0);
|
|
1993
|
+
__publicField(this, "Bl", 0);
|
|
1994
|
+
__publicField(this, "Ch", 0);
|
|
1995
|
+
__publicField(this, "Cl", 0);
|
|
1996
|
+
__publicField(this, "Dh", 0);
|
|
1997
|
+
__publicField(this, "Dl", 0);
|
|
1998
|
+
__publicField(this, "Eh", 0);
|
|
1999
|
+
__publicField(this, "El", 0);
|
|
2000
|
+
__publicField(this, "Fh", 0);
|
|
2001
|
+
__publicField(this, "Fl", 0);
|
|
2002
|
+
__publicField(this, "Gh", 0);
|
|
2003
|
+
__publicField(this, "Gl", 0);
|
|
2004
|
+
__publicField(this, "Hh", 0);
|
|
2005
|
+
__publicField(this, "Hl", 0);
|
|
2006
|
+
this.Ah = IV[0] | 0;
|
|
2007
|
+
this.Al = IV[1] | 0;
|
|
2008
|
+
this.Bh = IV[2] | 0;
|
|
2009
|
+
this.Bl = IV[3] | 0;
|
|
2010
|
+
this.Ch = IV[4] | 0;
|
|
2011
|
+
this.Cl = IV[5] | 0;
|
|
2012
|
+
this.Dh = IV[6] | 0;
|
|
2013
|
+
this.Dl = IV[7] | 0;
|
|
2014
|
+
this.Eh = IV[8] | 0;
|
|
2015
|
+
this.El = IV[9] | 0;
|
|
2016
|
+
this.Fh = IV[10] | 0;
|
|
2017
|
+
this.Fl = IV[11] | 0;
|
|
2018
|
+
this.Gh = IV[12] | 0;
|
|
2019
|
+
this.Gl = IV[13] | 0;
|
|
2020
|
+
this.Hh = IV[14] | 0;
|
|
2021
|
+
this.Hl = IV[15] | 0;
|
|
1663
2022
|
}
|
|
1664
2023
|
// prettier-ignore
|
|
1665
2024
|
get() {
|
|
@@ -1685,6 +2044,10 @@ var SHA2_64B = class extends HashMD {
|
|
|
1685
2044
|
this.Hh = Hh | 0;
|
|
1686
2045
|
this.Hl = Hl | 0;
|
|
1687
2046
|
}
|
|
2047
|
+
_cloneInto(to) {
|
|
2048
|
+
(to || (to = new this.constructor())).set(...this.get());
|
|
2049
|
+
return this._cloneIntoMeta(to);
|
|
2050
|
+
}
|
|
1688
2051
|
process(view, offset) {
|
|
1689
2052
|
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1690
2053
|
SHA512_W_H[i] = view.getUint32(offset);
|
|
@@ -1755,23 +2118,7 @@ var SHA2_64B = class extends HashMD {
|
|
|
1755
2118
|
};
|
|
1756
2119
|
var _SHA512 = class extends SHA2_64B {
|
|
1757
2120
|
constructor() {
|
|
1758
|
-
super(64);
|
|
1759
|
-
__publicField(this, "Ah", SHA512_IV[0] | 0);
|
|
1760
|
-
__publicField(this, "Al", SHA512_IV[1] | 0);
|
|
1761
|
-
__publicField(this, "Bh", SHA512_IV[2] | 0);
|
|
1762
|
-
__publicField(this, "Bl", SHA512_IV[3] | 0);
|
|
1763
|
-
__publicField(this, "Ch", SHA512_IV[4] | 0);
|
|
1764
|
-
__publicField(this, "Cl", SHA512_IV[5] | 0);
|
|
1765
|
-
__publicField(this, "Dh", SHA512_IV[6] | 0);
|
|
1766
|
-
__publicField(this, "Dl", SHA512_IV[7] | 0);
|
|
1767
|
-
__publicField(this, "Eh", SHA512_IV[8] | 0);
|
|
1768
|
-
__publicField(this, "El", SHA512_IV[9] | 0);
|
|
1769
|
-
__publicField(this, "Fh", SHA512_IV[10] | 0);
|
|
1770
|
-
__publicField(this, "Fl", SHA512_IV[11] | 0);
|
|
1771
|
-
__publicField(this, "Gh", SHA512_IV[12] | 0);
|
|
1772
|
-
__publicField(this, "Gl", SHA512_IV[13] | 0);
|
|
1773
|
-
__publicField(this, "Hh", SHA512_IV[14] | 0);
|
|
1774
|
-
__publicField(this, "Hl", SHA512_IV[15] | 0);
|
|
2121
|
+
super(64, SHA512_IV);
|
|
1775
2122
|
}
|
|
1776
2123
|
};
|
|
1777
2124
|
var sha512 = /* @__PURE__ */ createHasher(
|
|
@@ -1780,17 +2127,21 @@ var sha512 = /* @__PURE__ */ createHasher(
|
|
|
1780
2127
|
);
|
|
1781
2128
|
|
|
1782
2129
|
// ../../node_modules/@noble/curves/abstract/fft.js
|
|
1783
|
-
function checkU32(n) {
|
|
2130
|
+
function checkU32(n, title = "n") {
|
|
2131
|
+
if (typeof n !== "number")
|
|
2132
|
+
throw new TypeError(`wrong u32 integer "${title}": expected number, got type=${typeof n}`);
|
|
1784
2133
|
if (!Number.isSafeInteger(n) || n < 0 || n > 4294967295)
|
|
1785
|
-
throw new
|
|
2134
|
+
throw new RangeError(`wrong u32 integer "${title}": expected 0..4294967295, got ${n}`);
|
|
1786
2135
|
return n;
|
|
1787
2136
|
}
|
|
1788
2137
|
function isPowerOfTwo(x) {
|
|
1789
|
-
checkU32(x);
|
|
2138
|
+
checkU32(x, "x");
|
|
1790
2139
|
return (x & x - 1) === 0 && x !== 0;
|
|
1791
2140
|
}
|
|
1792
2141
|
function reverseBits(n, bits) {
|
|
1793
2142
|
checkU32(n);
|
|
2143
|
+
if (typeof bits !== "number")
|
|
2144
|
+
throw new TypeError('"bits" expected number, got type=' + typeof bits);
|
|
1794
2145
|
if (!Number.isSafeInteger(bits) || bits < 0 || bits > 32)
|
|
1795
2146
|
throw new Error(`expected integer 0 <= bits <= 32, got ${bits}`);
|
|
1796
2147
|
let reversed = 0;
|
|
@@ -1803,6 +2154,8 @@ function log2(n) {
|
|
|
1803
2154
|
return 31 - Math.clz32(n);
|
|
1804
2155
|
}
|
|
1805
2156
|
function bitReversalInplace(values) {
|
|
2157
|
+
if (!values || typeof values !== "object" || typeof values.length !== "number")
|
|
2158
|
+
throw new TypeError('"values" expected array-like, got type=' + typeof values);
|
|
1806
2159
|
const n = values.length;
|
|
1807
2160
|
if (!isPowerOfTwo(n))
|
|
1808
2161
|
throw new Error("expected positive power-of-two length, got " + n);
|
|
@@ -1818,14 +2171,19 @@ function bitReversalInplace(values) {
|
|
|
1818
2171
|
return values;
|
|
1819
2172
|
}
|
|
1820
2173
|
var FFTCore = (F2, coreOpts) => {
|
|
2174
|
+
validateObject(coreOpts, { N: "number", roots: "object", dit: "boolean" }, { invertButterflies: "boolean", skipStages: "number", brp: "boolean" }, "coreOpts");
|
|
1821
2175
|
const { N: N2, roots, dit, invertButterflies = false, skipStages = 0, brp = true } = coreOpts;
|
|
2176
|
+
checkU32(N2, "coreOpts.N");
|
|
1822
2177
|
const bits = log2(N2);
|
|
1823
2178
|
if (!isPowerOfTwo(N2))
|
|
1824
2179
|
throw new Error("FFT: Polynomial size should be power of two");
|
|
2180
|
+
checkU32(skipStages, "coreOpts.skipStages");
|
|
2181
|
+
const maxSkipStages = bits === 0 ? 0 : bits - 1;
|
|
2182
|
+
if (skipStages > maxSkipStages)
|
|
2183
|
+
throw new Error(`FFT: wrong skipStages: expected 0 <= skipStages <= ${maxSkipStages}`);
|
|
1825
2184
|
if (roots.length !== N2)
|
|
1826
2185
|
throw new Error(`FFT: wrong roots length: expected ${N2}, got ${roots.length}`);
|
|
1827
2186
|
const isDit = dit !== invertButterflies;
|
|
1828
|
-
isDit;
|
|
1829
2187
|
return (values) => {
|
|
1830
2188
|
if (values.length !== N2)
|
|
1831
2189
|
throw new Error("FFT: wrong Polynomial length");
|
|
@@ -1897,6 +2255,10 @@ function normDST(DST) {
|
|
|
1897
2255
|
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
1898
2256
|
abytes2(msg);
|
|
1899
2257
|
asafenumber(lenInBytes);
|
|
2258
|
+
if (typeof H !== "function")
|
|
2259
|
+
throw new Error("expand_message_xmd: expected hash function");
|
|
2260
|
+
asafenumber(H.outputLen, "hash.outputLen");
|
|
2261
|
+
asafenumber(H.blockLen, "hash.blockLen");
|
|
1900
2262
|
DST = normDST(DST);
|
|
1901
2263
|
if (DST.length > 255)
|
|
1902
2264
|
DST = H(concatBytes2(asciiToBytes("H2C-OVERSIZE-DST-"), DST));
|
|
@@ -1919,6 +2281,275 @@ function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
|
1919
2281
|
}
|
|
1920
2282
|
var _DST_scalar = "HashToScalar-";
|
|
1921
2283
|
|
|
2284
|
+
// ../../node_modules/@noble/curves/abstract/oprf.js
|
|
2285
|
+
var _DST_scalarBytes = /* @__PURE__ */ asciiToBytes(_DST_scalar);
|
|
2286
|
+
function createOPRF(opts2) {
|
|
2287
|
+
validateObject(opts2, {
|
|
2288
|
+
name: "string",
|
|
2289
|
+
hash: "function",
|
|
2290
|
+
hashToScalar: "function",
|
|
2291
|
+
hashToGroup: "function"
|
|
2292
|
+
});
|
|
2293
|
+
validatePointCons(opts2.Point);
|
|
2294
|
+
const { name, Point, hash, hashToGroup: hashToGroupHook, hashToScalar } = opts2;
|
|
2295
|
+
const { Fn: Fn2 } = Point;
|
|
2296
|
+
const invertSecret = (value) => invertCt(value, Fn2.ORDER);
|
|
2297
|
+
const hashToGroup = (msg, ctx) => hashToGroupHook(msg, {
|
|
2298
|
+
DST: concatBytes2(asciiToBytes("HashToGroup-"), ctx)
|
|
2299
|
+
});
|
|
2300
|
+
const hashToScalarPrefixed = (msg, ctx) => hashToScalar(msg, { DST: concatBytes2(_DST_scalarBytes, ctx) });
|
|
2301
|
+
const randomScalar = (rng = randomBytes2) => {
|
|
2302
|
+
if (typeof rng !== "function")
|
|
2303
|
+
throw new TypeError('"rng" expected function, got type=' + typeof rng);
|
|
2304
|
+
const t = mapHashToField(rng(getMinHashLength(Fn2.ORDER)), Fn2.ORDER, Fn2.isLE);
|
|
2305
|
+
return Fn2.isLE ? bytesToNumberLE(t) : bytesToNumberBE(t);
|
|
2306
|
+
};
|
|
2307
|
+
const msm = (points, scalars) => mulAddUnsafe(Point, points, scalars);
|
|
2308
|
+
const getCtx = (mode) => concatBytes2(asciiToBytes("OPRFV1-"), new Uint8Array([mode]), asciiToBytes("-" + name));
|
|
2309
|
+
const ctxOPRF = getCtx(0);
|
|
2310
|
+
const ctxVOPRF = getCtx(1);
|
|
2311
|
+
const ctxPOPRF = getCtx(2);
|
|
2312
|
+
function encode(...args) {
|
|
2313
|
+
const res2 = [];
|
|
2314
|
+
for (const a of args) {
|
|
2315
|
+
if (typeof a === "number")
|
|
2316
|
+
res2.push(numberToBytesBE(a, 2));
|
|
2317
|
+
else if (typeof a === "string")
|
|
2318
|
+
res2.push(asciiToBytes(a));
|
|
2319
|
+
else {
|
|
2320
|
+
abytes2(a);
|
|
2321
|
+
res2.push(numberToBytesBE(a.length, 2), a);
|
|
2322
|
+
}
|
|
2323
|
+
}
|
|
2324
|
+
return concatBytes2(...res2);
|
|
2325
|
+
}
|
|
2326
|
+
const inputBytes = (title, bytes) => {
|
|
2327
|
+
abytes2(bytes, void 0, title);
|
|
2328
|
+
if (bytes.length > 65535)
|
|
2329
|
+
throw new Error(`"${title}" expected Uint8Array of length <= 65535, got length=${bytes.length}`);
|
|
2330
|
+
return bytes;
|
|
2331
|
+
};
|
|
2332
|
+
const hashInput = (...bytes) => hash(encode(...bytes, "Finalize"));
|
|
2333
|
+
function getTranscripts(B2, C, D, ctx) {
|
|
2334
|
+
const Bm = B2.toBytes();
|
|
2335
|
+
const seed = hash(encode(Bm, concatBytes2(asciiToBytes("Seed-"), ctx)));
|
|
2336
|
+
const res2 = [];
|
|
2337
|
+
for (let i = 0; i < C.length; i++) {
|
|
2338
|
+
const Ci = C[i].toBytes();
|
|
2339
|
+
const Di = D[i].toBytes();
|
|
2340
|
+
const di = hashToScalarPrefixed(encode(seed, i, Ci, Di, "Composite"), ctx);
|
|
2341
|
+
res2.push(di);
|
|
2342
|
+
}
|
|
2343
|
+
return res2;
|
|
2344
|
+
}
|
|
2345
|
+
function computeComposites(B2, C, D, ctx) {
|
|
2346
|
+
const T = getTranscripts(B2, C, D, ctx);
|
|
2347
|
+
const M = msm(C, T);
|
|
2348
|
+
const Z = msm(D, T);
|
|
2349
|
+
return { M, Z };
|
|
2350
|
+
}
|
|
2351
|
+
function computeCompositesFast(k, B2, C, D, ctx) {
|
|
2352
|
+
const T = getTranscripts(B2, C, D, ctx);
|
|
2353
|
+
const M = msm(C, T);
|
|
2354
|
+
const Z = M.multiply(k);
|
|
2355
|
+
return { M, Z };
|
|
2356
|
+
}
|
|
2357
|
+
function challengeTranscript(B2, M, Z, t2, t3, ctx) {
|
|
2358
|
+
const [Bm, a0, a1, a2, a3] = [B2, M, Z, t2, t3].map((i) => i.toBytes());
|
|
2359
|
+
return hashToScalarPrefixed(encode(Bm, a0, a1, a2, a3, "Challenge"), ctx);
|
|
2360
|
+
}
|
|
2361
|
+
function generateProof(ctx, k, B2, C, D, rng) {
|
|
2362
|
+
const { M, Z } = computeCompositesFast(k, B2, C, D, ctx);
|
|
2363
|
+
const r = randomScalar(rng);
|
|
2364
|
+
const t2 = Point.BASE.multiply(r);
|
|
2365
|
+
const t3 = M.multiply(r);
|
|
2366
|
+
const c = challengeTranscript(B2, M, Z, t2, t3, ctx);
|
|
2367
|
+
const s = Fn2.sub(r, Fn2.mul(c, k));
|
|
2368
|
+
return concatBytes2(...[c, s].map((i) => Fn2.toBytes(i)));
|
|
2369
|
+
}
|
|
2370
|
+
function verifyProof(ctx, B2, C, D, proof) {
|
|
2371
|
+
abytes2(proof, 2 * Fn2.BYTES);
|
|
2372
|
+
const { M, Z } = computeComposites(B2, C, D, ctx);
|
|
2373
|
+
const [c, s] = [proof.subarray(0, Fn2.BYTES), proof.subarray(Fn2.BYTES)].map((f) => Fn2.fromBytes(f));
|
|
2374
|
+
const t2 = msm([Point.BASE, B2], [s, c]);
|
|
2375
|
+
const t3 = msm([M, Z], [s, c]);
|
|
2376
|
+
const expectedC = challengeTranscript(B2, M, Z, t2, t3, ctx);
|
|
2377
|
+
if (!Fn2.eql(c, expectedC))
|
|
2378
|
+
throw new Error("proof verification failed");
|
|
2379
|
+
}
|
|
2380
|
+
function generateKeyPair() {
|
|
2381
|
+
const skS = randomScalar();
|
|
2382
|
+
const pkS = Point.BASE.multiply(skS);
|
|
2383
|
+
return { secretKey: Fn2.toBytes(skS), publicKey: pkS.toBytes() };
|
|
2384
|
+
}
|
|
2385
|
+
function deriveKeyPair(ctx, seed, info) {
|
|
2386
|
+
abytes2(seed, 32, "seed");
|
|
2387
|
+
info = inputBytes("keyInfo", info);
|
|
2388
|
+
const dst = concatBytes2(asciiToBytes("DeriveKeyPair"), ctx);
|
|
2389
|
+
const msg = concatBytes2(seed, encode(info), Uint8Array.of(0));
|
|
2390
|
+
for (let counter = 0; counter <= 255; counter++) {
|
|
2391
|
+
msg[msg.length - 1] = counter;
|
|
2392
|
+
const skS = hashToScalar(msg, { DST: dst });
|
|
2393
|
+
if (Fn2.is0(skS))
|
|
2394
|
+
continue;
|
|
2395
|
+
return {
|
|
2396
|
+
secretKey: Fn2.toBytes(skS),
|
|
2397
|
+
publicKey: Point.BASE.multiply(skS).toBytes()
|
|
2398
|
+
};
|
|
2399
|
+
}
|
|
2400
|
+
throw new Error("Cannot derive key");
|
|
2401
|
+
}
|
|
2402
|
+
const wirePoint = (label, bytes) => {
|
|
2403
|
+
const point = Point.fromBytes(bytes);
|
|
2404
|
+
if (point.equals(Point.ZERO))
|
|
2405
|
+
throw new Error(label + " point at infinity");
|
|
2406
|
+
return point;
|
|
2407
|
+
};
|
|
2408
|
+
function blind(ctx, input, rng = randomBytes2) {
|
|
2409
|
+
input = inputBytes("input", input);
|
|
2410
|
+
const blind2 = randomScalar(rng);
|
|
2411
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
2412
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2413
|
+
throw new Error("Input point at infinity");
|
|
2414
|
+
const blinded = inputPoint.multiply(blind2);
|
|
2415
|
+
return { blind: Fn2.toBytes(blind2), blinded: blinded.toBytes() };
|
|
2416
|
+
}
|
|
2417
|
+
function evaluate(ctx, secretKey, input) {
|
|
2418
|
+
input = inputBytes("input", input);
|
|
2419
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2420
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
2421
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2422
|
+
throw new Error("Input point at infinity");
|
|
2423
|
+
const unblinded = inputPoint.multiply(skS).toBytes();
|
|
2424
|
+
return hashInput(input, unblinded);
|
|
2425
|
+
}
|
|
2426
|
+
const oprf2 = Object.freeze({
|
|
2427
|
+
generateKeyPair,
|
|
2428
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxOPRF, seed, keyInfo),
|
|
2429
|
+
blind: (input, rng = randomBytes2) => blind(ctxOPRF, input, rng),
|
|
2430
|
+
blindEvaluate(secretKey, blindedPoint) {
|
|
2431
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2432
|
+
const elm = wirePoint("blinded", blindedPoint);
|
|
2433
|
+
return elm.multiply(skS).toBytes();
|
|
2434
|
+
},
|
|
2435
|
+
finalize(input, blindBytes, evaluatedBytes) {
|
|
2436
|
+
input = inputBytes("input", input);
|
|
2437
|
+
const blind2 = Fn2.fromBytes(blindBytes);
|
|
2438
|
+
const evalPoint = wirePoint("evaluated", evaluatedBytes);
|
|
2439
|
+
const unblinded = evalPoint.multiply(Fn2.inv(blind2)).toBytes();
|
|
2440
|
+
return hashInput(input, unblinded);
|
|
2441
|
+
},
|
|
2442
|
+
evaluate: (secretKey, input) => evaluate(ctxOPRF, secretKey, input)
|
|
2443
|
+
});
|
|
2444
|
+
const voprf = Object.freeze({
|
|
2445
|
+
generateKeyPair,
|
|
2446
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxVOPRF, seed, keyInfo),
|
|
2447
|
+
blind: (input, rng = randomBytes2) => blind(ctxVOPRF, input, rng),
|
|
2448
|
+
blindEvaluateBatch(secretKey, publicKey, blinded, rng = randomBytes2) {
|
|
2449
|
+
if (!Array.isArray(blinded))
|
|
2450
|
+
throw new Error("expected array");
|
|
2451
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2452
|
+
const pkS = wirePoint("public key", publicKey);
|
|
2453
|
+
const blindedPoints = blinded.map((i) => wirePoint("blinded", i));
|
|
2454
|
+
const evaluated = blindedPoints.map((i) => i.multiply(skS));
|
|
2455
|
+
const proof = generateProof(ctxVOPRF, skS, pkS, blindedPoints, evaluated, rng);
|
|
2456
|
+
return { evaluated: evaluated.map((i) => i.toBytes()), proof };
|
|
2457
|
+
},
|
|
2458
|
+
blindEvaluate(secretKey, publicKey, blinded, rng = randomBytes2) {
|
|
2459
|
+
const res2 = this.blindEvaluateBatch(secretKey, publicKey, [blinded], rng);
|
|
2460
|
+
return { evaluated: res2.evaluated[0], proof: res2.proof };
|
|
2461
|
+
},
|
|
2462
|
+
finalizeBatch(items, publicKey, proof) {
|
|
2463
|
+
if (!Array.isArray(items))
|
|
2464
|
+
throw new Error("expected array");
|
|
2465
|
+
const pkS = wirePoint("public key", publicKey);
|
|
2466
|
+
const blindedPoints = items.map((i) => wirePoint("blinded", i.blinded));
|
|
2467
|
+
const evalPoints = items.map((i) => wirePoint("evaluated", i.evaluated));
|
|
2468
|
+
verifyProof(ctxVOPRF, pkS, blindedPoints, evalPoints, proof);
|
|
2469
|
+
return items.map((i, j) => {
|
|
2470
|
+
const input = inputBytes("input", i.input);
|
|
2471
|
+
const blind2 = Fn2.fromBytes(i.blind);
|
|
2472
|
+
const unblinded = evalPoints[j].multiply(Fn2.inv(blind2)).toBytes();
|
|
2473
|
+
return hashInput(input, unblinded);
|
|
2474
|
+
});
|
|
2475
|
+
},
|
|
2476
|
+
finalize(input, blind2, evaluated, blinded, publicKey, proof) {
|
|
2477
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], publicKey, proof)[0];
|
|
2478
|
+
},
|
|
2479
|
+
evaluate: (secretKey, input) => evaluate(ctxVOPRF, secretKey, input)
|
|
2480
|
+
});
|
|
2481
|
+
const poprf = (info) => {
|
|
2482
|
+
info = copyBytes(inputBytes("info", info));
|
|
2483
|
+
const m = hashToScalarPrefixed(encode("Info", info), ctxPOPRF);
|
|
2484
|
+
const T = Point.BASE.multiply(m);
|
|
2485
|
+
return Object.freeze({
|
|
2486
|
+
generateKeyPair,
|
|
2487
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxPOPRF, seed, keyInfo),
|
|
2488
|
+
blind(input, publicKey, rng = randomBytes2) {
|
|
2489
|
+
input = inputBytes("input", input);
|
|
2490
|
+
const pkS = wirePoint("public key", publicKey);
|
|
2491
|
+
const tweakedKey = T.add(pkS);
|
|
2492
|
+
if (tweakedKey.equals(Point.ZERO))
|
|
2493
|
+
throw new Error("tweakedKey point at infinity");
|
|
2494
|
+
const blind2 = randomScalar(rng);
|
|
2495
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
2496
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2497
|
+
throw new Error("Input point at infinity");
|
|
2498
|
+
const blindedPoint = inputPoint.multiply(blind2);
|
|
2499
|
+
return {
|
|
2500
|
+
blind: Fn2.toBytes(blind2),
|
|
2501
|
+
blinded: blindedPoint.toBytes(),
|
|
2502
|
+
tweakedKey: tweakedKey.toBytes()
|
|
2503
|
+
};
|
|
2504
|
+
},
|
|
2505
|
+
blindEvaluateBatch(secretKey, blinded, rng = randomBytes2) {
|
|
2506
|
+
if (!Array.isArray(blinded))
|
|
2507
|
+
throw new Error("expected array");
|
|
2508
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2509
|
+
const t = Fn2.add(skS, m);
|
|
2510
|
+
const invT = invertSecret(t);
|
|
2511
|
+
const blindedPoints = blinded.map((i) => wirePoint("blinded", i));
|
|
2512
|
+
const evalPoints = blindedPoints.map((i) => i.multiply(invT));
|
|
2513
|
+
const tweakedKey = Point.BASE.multiply(t);
|
|
2514
|
+
const proof = generateProof(ctxPOPRF, t, tweakedKey, evalPoints, blindedPoints, rng);
|
|
2515
|
+
return { evaluated: evalPoints.map((i) => i.toBytes()), proof };
|
|
2516
|
+
},
|
|
2517
|
+
blindEvaluate(secretKey, blinded, rng = randomBytes2) {
|
|
2518
|
+
const res2 = this.blindEvaluateBatch(secretKey, [blinded], rng);
|
|
2519
|
+
return { evaluated: res2.evaluated[0], proof: res2.proof };
|
|
2520
|
+
},
|
|
2521
|
+
finalizeBatch(items, proof, tweakedKey) {
|
|
2522
|
+
if (!Array.isArray(items))
|
|
2523
|
+
throw new Error("expected array");
|
|
2524
|
+
const inputs = items.map((i) => inputBytes("input", i.input));
|
|
2525
|
+
const evalPoints = items.map((i) => wirePoint("evaluated", i.evaluated));
|
|
2526
|
+
verifyProof(ctxPOPRF, wirePoint("tweakedKey", tweakedKey), evalPoints, items.map((i) => wirePoint("blinded", i.blinded)), proof);
|
|
2527
|
+
return items.map((i, j) => {
|
|
2528
|
+
const blind2 = Fn2.fromBytes(i.blind);
|
|
2529
|
+
const point = evalPoints[j].multiply(Fn2.inv(blind2)).toBytes();
|
|
2530
|
+
return hashInput(inputs[j], info, point);
|
|
2531
|
+
});
|
|
2532
|
+
},
|
|
2533
|
+
finalize(input, blind2, evaluated, blinded, proof, tweakedKey) {
|
|
2534
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], proof, tweakedKey)[0];
|
|
2535
|
+
},
|
|
2536
|
+
evaluate(secretKey, input) {
|
|
2537
|
+
input = inputBytes("input", input);
|
|
2538
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2539
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
2540
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2541
|
+
throw new Error("Input point at infinity");
|
|
2542
|
+
const t = Fn2.add(skS, m);
|
|
2543
|
+
const invT = invertSecret(t);
|
|
2544
|
+
const unblinded = inputPoint.multiply(invT).toBytes();
|
|
2545
|
+
return hashInput(input, info, unblinded);
|
|
2546
|
+
}
|
|
2547
|
+
});
|
|
2548
|
+
};
|
|
2549
|
+
const res = { name, oprf: oprf2, voprf, poprf, __tests: Object.freeze({ Fn: Fn2, invertSecret }) };
|
|
2550
|
+
return Object.freeze(res);
|
|
2551
|
+
}
|
|
2552
|
+
|
|
1922
2553
|
// ../../node_modules/@noble/curves/ed25519.js
|
|
1923
2554
|
var _0n6 = /* @__PURE__ */ BigInt(0);
|
|
1924
2555
|
var _1n6 = /* @__PURE__ */ BigInt(1);
|
|
@@ -1964,8 +2595,8 @@ function uvRatio(u, v) {
|
|
|
1964
2595
|
const P = ed25519_CURVE_p;
|
|
1965
2596
|
const v3 = mod(v * v * v, P);
|
|
1966
2597
|
const v7 = mod(v3 * v3 * v, P);
|
|
1967
|
-
const
|
|
1968
|
-
let x = mod(u * v3 *
|
|
2598
|
+
const pow3 = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
2599
|
+
let x = mod(u * v3 * pow3, P);
|
|
1969
2600
|
const vx2 = mod(v * x * x, P);
|
|
1970
2601
|
const root1 = x;
|
|
1971
2602
|
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
@@ -1985,14 +2616,25 @@ var Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
|
1985
2616
|
var Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
|
|
1986
2617
|
var x25519 = /* @__PURE__ */ (() => {
|
|
1987
2618
|
const P = ed25519_CURVE_p;
|
|
2619
|
+
const powPminus2 = (x) => {
|
|
2620
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
2621
|
+
return mod(pow2(pow_p_5_8, _3n2, P) * b2, P);
|
|
2622
|
+
};
|
|
1988
2623
|
return montgomery({
|
|
1989
2624
|
P,
|
|
1990
2625
|
type: "x25519",
|
|
1991
|
-
powPminus2
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
2626
|
+
powPminus2,
|
|
2627
|
+
adjustScalarBytes,
|
|
2628
|
+
// ~3x faster fixed-base: [k]B on the birationally-equivalent Edwards curve using cached
|
|
2629
|
+
// base tables, mapped back via u = (1+y)/(1-y) = (Z+Y)/(Z-Y) with one Fermat inversion.
|
|
2630
|
+
// Same construction as libsodium's crypto_scalarmult_curve25519_base.
|
|
2631
|
+
scalarMultBase: (k) => {
|
|
2632
|
+
const kn = mod(k, ed25519_Point.Fn.ORDER);
|
|
2633
|
+
if (kn === _0n6)
|
|
2634
|
+
return _0n6;
|
|
2635
|
+
const p = ed25519_Point.BASE.multiply(kn);
|
|
2636
|
+
return mod((p.Z + p.Y) * powPminus2(mod(p.Z - p.Y, P)), P);
|
|
2637
|
+
}
|
|
1996
2638
|
});
|
|
1997
2639
|
})();
|
|
1998
2640
|
var SQRT_M1 = ED25519_SQRT_M1;
|
|
@@ -2005,27 +2647,25 @@ var MAX_255B = /* @__PURE__ */ BigInt("0x7ffffffffffffffffffffffffffffffffffffff
|
|
|
2005
2647
|
var bytes255ToNumberLE = (bytes) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
2006
2648
|
function calcElligatorRistrettoMap(r0) {
|
|
2007
2649
|
const { d } = ed25519_CURVE;
|
|
2008
|
-
const
|
|
2009
|
-
const
|
|
2010
|
-
const r = mod2(SQRT_M1 * r0 * r0);
|
|
2011
|
-
const Ns = mod2((r + _1n6) * ONE_MINUS_D_SQ);
|
|
2650
|
+
const r = Fp.mul(Fp.mulN(SQRT_M1, r0), r0);
|
|
2651
|
+
const Ns = Fp.mul(Fp.addN(r, _1n6), ONE_MINUS_D_SQ);
|
|
2012
2652
|
let c = BigInt(-1);
|
|
2013
|
-
const D =
|
|
2653
|
+
const D = Fp.mul(Fp.subN(c, Fp.mulN(d, r)), Fp.add(r, d));
|
|
2014
2654
|
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
2015
|
-
let s_ =
|
|
2016
|
-
if (!
|
|
2017
|
-
s_ =
|
|
2655
|
+
let s_ = Fp.mul(s, r0);
|
|
2656
|
+
if (!Fp.isOdd(s_))
|
|
2657
|
+
s_ = Fp.neg(s_);
|
|
2018
2658
|
if (!Ns_D_is_sq)
|
|
2019
2659
|
s = s_;
|
|
2020
2660
|
if (!Ns_D_is_sq)
|
|
2021
2661
|
c = r;
|
|
2022
|
-
const Nt =
|
|
2023
|
-
const s2 = s
|
|
2024
|
-
const W0 =
|
|
2025
|
-
const W1 =
|
|
2026
|
-
const W2 =
|
|
2027
|
-
const W3 =
|
|
2028
|
-
return new ed25519_Point(
|
|
2662
|
+
const Nt = Fp.sub(Fp.mulN(Fp.mulN(c, Fp.subN(r, _1n6)), D_MINUS_ONE_SQ), D);
|
|
2663
|
+
const s2 = Fp.sqrN(s);
|
|
2664
|
+
const W0 = Fp.mul(Fp.addN(s, s), D);
|
|
2665
|
+
const W1 = Fp.mul(Nt, SQRT_AD_MINUS_ONE);
|
|
2666
|
+
const W2 = Fp.sub(_1n6, s2);
|
|
2667
|
+
const W3 = Fp.add(_1n6, s2);
|
|
2668
|
+
return new ed25519_Point(Fp.mul(W0, W3), Fp.mul(W2, W1), Fp.mul(W1, W3), Fp.mul(W0, W2));
|
|
2029
2669
|
}
|
|
2030
2670
|
var __RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
2031
2671
|
constructor(ep) {
|
|
@@ -2050,28 +2690,26 @@ var __RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
|
2050
2690
|
static fromBytes(bytes) {
|
|
2051
2691
|
abytes(bytes, 32);
|
|
2052
2692
|
const { a, d } = ed25519_CURVE;
|
|
2053
|
-
const P = ed25519_CURVE_p;
|
|
2054
|
-
const mod2 = (n) => Fp.create(n);
|
|
2055
2693
|
const s = bytes255ToNumberLE(bytes);
|
|
2056
|
-
if (!equalBytes(Fp.toBytes(s), bytes) ||
|
|
2694
|
+
if (!equalBytes(Fp.toBytes(s), bytes) || Fp.isOdd(s))
|
|
2057
2695
|
throw new Error("invalid ristretto255 encoding 1");
|
|
2058
|
-
const s2 =
|
|
2059
|
-
const u1 =
|
|
2060
|
-
const u2 =
|
|
2061
|
-
const u1_2 =
|
|
2062
|
-
const u2_2 =
|
|
2063
|
-
const v =
|
|
2064
|
-
const { isValid, value: I } = invertSqrt(
|
|
2065
|
-
const Dx =
|
|
2066
|
-
const Dy =
|
|
2067
|
-
let x =
|
|
2068
|
-
if (
|
|
2069
|
-
x =
|
|
2070
|
-
const y =
|
|
2071
|
-
const t =
|
|
2072
|
-
if (!isValid ||
|
|
2696
|
+
const s2 = Fp.sqr(s);
|
|
2697
|
+
const u1 = Fp.add(_1n6, Fp.mulN(a, s2));
|
|
2698
|
+
const u2 = Fp.sub(_1n6, Fp.mulN(a, s2));
|
|
2699
|
+
const u1_2 = Fp.sqr(u1);
|
|
2700
|
+
const u2_2 = Fp.sqr(u2);
|
|
2701
|
+
const v = Fp.sub(Fp.mulN(Fp.mulN(a, d), u1_2), u2_2);
|
|
2702
|
+
const { isValid, value: I } = invertSqrt(Fp.mul(v, u2_2));
|
|
2703
|
+
const Dx = Fp.mul(I, u2);
|
|
2704
|
+
const Dy = Fp.mul(Fp.mulN(I, Dx), v);
|
|
2705
|
+
let x = Fp.mul(Fp.addN(s, s), Dx);
|
|
2706
|
+
if (Fp.isOdd(x))
|
|
2707
|
+
x = Fp.neg(x);
|
|
2708
|
+
const y = Fp.mul(u1, Dy);
|
|
2709
|
+
const t = Fp.mul(x, y);
|
|
2710
|
+
if (!isValid || Fp.isOdd(t) || Fp.is0(y))
|
|
2073
2711
|
throw new Error("invalid ristretto255 encoding 2");
|
|
2074
|
-
return new __RistrettoPoint(new ed25519_Point(x, y,
|
|
2712
|
+
return new __RistrettoPoint(new ed25519_Point(x, y, Fp.ONE, t));
|
|
2075
2713
|
}
|
|
2076
2714
|
/**
|
|
2077
2715
|
* Converts ristretto-encoded string to ristretto point.
|
|
@@ -2087,30 +2725,28 @@ var __RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
|
2087
2725
|
*/
|
|
2088
2726
|
toBytes() {
|
|
2089
2727
|
let { X, Y, Z, T } = this.ep;
|
|
2090
|
-
const
|
|
2091
|
-
const
|
|
2092
|
-
const
|
|
2093
|
-
const
|
|
2094
|
-
const
|
|
2095
|
-
const
|
|
2096
|
-
const
|
|
2097
|
-
const D2 = mod2(invsqrt * u2);
|
|
2098
|
-
const zInv = mod2(D1 * D2 * T);
|
|
2728
|
+
const u1 = Fp.mul(Fp.add(Z, Y), Fp.sub(Z, Y));
|
|
2729
|
+
const u2 = Fp.mul(X, Y);
|
|
2730
|
+
const u2sq = Fp.sqr(u2);
|
|
2731
|
+
const { value: invsqrt } = invertSqrt(Fp.mul(u1, u2sq));
|
|
2732
|
+
const D1 = Fp.mul(invsqrt, u1);
|
|
2733
|
+
const D2 = Fp.mul(invsqrt, u2);
|
|
2734
|
+
const zInv = Fp.mul(Fp.mulN(D1, D2), T);
|
|
2099
2735
|
let D;
|
|
2100
|
-
if (
|
|
2101
|
-
let _x =
|
|
2102
|
-
let _y =
|
|
2736
|
+
if (Fp.isOdd(Fp.mul(T, zInv))) {
|
|
2737
|
+
let _x = Fp.mul(Y, SQRT_M1);
|
|
2738
|
+
let _y = Fp.mul(X, SQRT_M1);
|
|
2103
2739
|
X = _x;
|
|
2104
2740
|
Y = _y;
|
|
2105
|
-
D =
|
|
2741
|
+
D = Fp.mul(D1, INVSQRT_A_MINUS_D);
|
|
2106
2742
|
} else {
|
|
2107
2743
|
D = D2;
|
|
2108
2744
|
}
|
|
2109
|
-
if (
|
|
2110
|
-
Y =
|
|
2111
|
-
let s =
|
|
2112
|
-
if (
|
|
2113
|
-
s =
|
|
2745
|
+
if (Fp.isOdd(Fp.mul(X, zInv)))
|
|
2746
|
+
Y = Fp.neg(Y);
|
|
2747
|
+
let s = Fp.mul(Fp.subN(Z, Y), D);
|
|
2748
|
+
if (Fp.isOdd(s))
|
|
2749
|
+
s = Fp.neg(s);
|
|
2114
2750
|
return Fp.toBytes(s);
|
|
2115
2751
|
}
|
|
2116
2752
|
/**
|
|
@@ -2121,9 +2757,8 @@ var __RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
|
2121
2757
|
this.assertSame(other);
|
|
2122
2758
|
const { X: X1, Y: Y1 } = this.ep;
|
|
2123
2759
|
const { X: X2, Y: Y2 } = other.ep;
|
|
2124
|
-
const
|
|
2125
|
-
const
|
|
2126
|
-
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
2760
|
+
const one = Fp.eql(Fp.mul(X1, Y2), Fp.mul(Y1, X2));
|
|
2761
|
+
const two = Fp.eql(Fp.mul(Y1, Y2), Fp.mul(X1, X2));
|
|
2127
2762
|
return one || two;
|
|
2128
2763
|
}
|
|
2129
2764
|
is0() {
|
|
@@ -2141,11 +2776,7 @@ __publicField(__RistrettoPoint, "Fp", /* @__PURE__ */ (() => Fp)());
|
|
|
2141
2776
|
// prettier-ignore
|
|
2142
2777
|
__publicField(__RistrettoPoint, "Fn", /* @__PURE__ */ (() => Fn)());
|
|
2143
2778
|
var _RistrettoPoint = __RistrettoPoint;
|
|
2144
|
-
Object.freeze(
|
|
2145
|
-
Object.freeze(_RistrettoPoint.ZERO);
|
|
2146
|
-
Object.freeze(_RistrettoPoint.prototype);
|
|
2147
|
-
Object.freeze(_RistrettoPoint);
|
|
2148
|
-
var ristretto255_hasher = Object.freeze({
|
|
2779
|
+
var ristretto255_hasher = /* @__PURE__ */ Object.freeze({
|
|
2149
2780
|
Point: _RistrettoPoint,
|
|
2150
2781
|
/**
|
|
2151
2782
|
* Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
|
|
@@ -2166,8 +2797,9 @@ var ristretto255_hasher = Object.freeze({
|
|
|
2166
2797
|
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
2167
2798
|
return ristretto255_hasher.deriveToCurve(xmd);
|
|
2168
2799
|
},
|
|
2169
|
-
hashToScalar(msg, options
|
|
2170
|
-
const
|
|
2800
|
+
hashToScalar(msg, options) {
|
|
2801
|
+
const DST = options?.DST === void 0 ? _DST_scalar : options.DST;
|
|
2802
|
+
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
2171
2803
|
return Fn.create(bytesToNumberLE(xmd));
|
|
2172
2804
|
},
|
|
2173
2805
|
/**
|
|
@@ -2188,6 +2820,13 @@ var ristretto255_hasher = Object.freeze({
|
|
|
2188
2820
|
return new _RistrettoPoint(R1.add(R2));
|
|
2189
2821
|
}
|
|
2190
2822
|
});
|
|
2823
|
+
var ristretto255_oprf = /* @__PURE__ */ (() => createOPRF({
|
|
2824
|
+
name: "ristretto255-SHA512",
|
|
2825
|
+
Point: _RistrettoPoint,
|
|
2826
|
+
hash: sha512,
|
|
2827
|
+
hashToGroup: ristretto255_hasher.hashToCurve,
|
|
2828
|
+
hashToScalar: ristretto255_hasher.hashToScalar
|
|
2829
|
+
}))();
|
|
2191
2830
|
|
|
2192
2831
|
// ../../node_modules/@noble/hashes/sha3.js
|
|
2193
2832
|
var _0n7 = BigInt(0);
|
|
@@ -2214,13 +2853,21 @@ for (let round = 0, R = _1n7, x = 1, y = 0; round < 24; round++) {
|
|
|
2214
2853
|
var IOTAS = split(_SHA3_IOTA, true);
|
|
2215
2854
|
var SHA3_IOTA_H = IOTAS[0];
|
|
2216
2855
|
var SHA3_IOTA_L = IOTAS[1];
|
|
2856
|
+
var rotlSH = (h, l, s) => h << s | l >>> 32 - s;
|
|
2857
|
+
var rotlSL = (h, l, s) => l << s | h >>> 32 - s;
|
|
2858
|
+
var rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
|
|
2859
|
+
var rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
|
|
2217
2860
|
var rotlH = (h, l, s) => s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s);
|
|
2218
2861
|
var rotlL = (h, l, s) => s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s);
|
|
2862
|
+
var B = new Uint32Array(5 * 2);
|
|
2219
2863
|
function keccakP(s, rounds = 24) {
|
|
2864
|
+
if (!(s instanceof Uint32Array))
|
|
2865
|
+
throw new TypeError('"s" expected Uint32Array(50), got type=' + typeof s);
|
|
2866
|
+
if (s.length !== 50)
|
|
2867
|
+
throw new RangeError('"s" expected Uint32Array(50), got length=' + s.length);
|
|
2220
2868
|
anumber(rounds, "rounds");
|
|
2221
2869
|
if (rounds < 1 || rounds > 24)
|
|
2222
2870
|
throw new Error('"rounds" expected integer 1..24');
|
|
2223
|
-
const B = new Uint32Array(5 * 2);
|
|
2224
2871
|
for (let round = 24 - rounds; round < 24; round++) {
|
|
2225
2872
|
for (let x = 0; x < 10; x++)
|
|
2226
2873
|
B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
|
|
@@ -2281,6 +2928,10 @@ var Keccak = class _Keccak {
|
|
|
2281
2928
|
__publicField(this, "canXOF");
|
|
2282
2929
|
__publicField(this, "enableXOF", false);
|
|
2283
2930
|
__publicField(this, "rounds");
|
|
2931
|
+
anumber(blockLen, "blockLen");
|
|
2932
|
+
anumber(suffix, "suffix");
|
|
2933
|
+
anumber(rounds, "rounds");
|
|
2934
|
+
abool(enableXOF, "enableXOF");
|
|
2284
2935
|
this.blockLen = blockLen;
|
|
2285
2936
|
this.suffix = suffix;
|
|
2286
2937
|
this.outputLen = outputLen;
|
|
@@ -2289,7 +2940,7 @@ var Keccak = class _Keccak {
|
|
|
2289
2940
|
this.rounds = rounds;
|
|
2290
2941
|
anumber(outputLen, "outputLen");
|
|
2291
2942
|
if (!(0 < blockLen && blockLen < 200))
|
|
2292
|
-
throw new Error("
|
|
2943
|
+
throw new Error('"blockLen" must be 1..199');
|
|
2293
2944
|
this.state = new Uint8Array(200);
|
|
2294
2945
|
this.state32 = u32(this.state);
|
|
2295
2946
|
}
|
|
@@ -2306,9 +2957,20 @@ var Keccak = class _Keccak {
|
|
|
2306
2957
|
update(data) {
|
|
2307
2958
|
aexists(this);
|
|
2308
2959
|
abytes(data);
|
|
2309
|
-
const { blockLen, state } = this;
|
|
2960
|
+
const { blockLen, state, state32 } = this;
|
|
2310
2961
|
const len = data.length;
|
|
2962
|
+
const canUseU32 = blockLen % 4 === 0 && data.byteOffset % 4 === 0;
|
|
2963
|
+
const blockLen32 = blockLen / 4;
|
|
2964
|
+
const data32 = canUseU32 && len >= blockLen ? u32(data) : void 0;
|
|
2311
2965
|
for (let pos = 0; pos < len; ) {
|
|
2966
|
+
if (data32 !== void 0 && this.pos === 0 && pos % 4 === 0 && len - pos >= blockLen) {
|
|
2967
|
+
for (let i = 0, o = pos / 4; i < blockLen32; i++)
|
|
2968
|
+
state32[i] ^= data32[o + i];
|
|
2969
|
+
pos += blockLen;
|
|
2970
|
+
this.pos = blockLen;
|
|
2971
|
+
this.keccak();
|
|
2972
|
+
continue;
|
|
2973
|
+
}
|
|
2312
2974
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
2313
2975
|
for (let i = 0; i < take; i++)
|
|
2314
2976
|
state[this.pos++] ^= data[pos++];
|
|
@@ -2346,7 +3008,7 @@ var Keccak = class _Keccak {
|
|
|
2346
3008
|
}
|
|
2347
3009
|
xofInto(out) {
|
|
2348
3010
|
if (!this.enableXOF)
|
|
2349
|
-
throw new Error("XOF is not
|
|
3011
|
+
throw new Error("XOF is not enabled");
|
|
2350
3012
|
return this.writeInto(out);
|
|
2351
3013
|
}
|
|
2352
3014
|
xof(bytes) {
|
|
@@ -2357,7 +3019,7 @@ var Keccak = class _Keccak {
|
|
|
2357
3019
|
aoutput(out, this);
|
|
2358
3020
|
if (this.finished)
|
|
2359
3021
|
throw new Error("digest() was already called");
|
|
2360
|
-
this.writeInto(out.subarray(0, this.outputLen));
|
|
3022
|
+
this.writeInto(out.length === this.outputLen ? out : out.subarray(0, this.outputLen));
|
|
2361
3023
|
this.destroy();
|
|
2362
3024
|
}
|
|
2363
3025
|
digest() {
|
|
@@ -2399,14 +3061,32 @@ var sha3_512 = /* @__PURE__ */ genKeccak(
|
|
|
2399
3061
|
64,
|
|
2400
3062
|
/* @__PURE__ */ oidNist(10)
|
|
2401
3063
|
);
|
|
2402
|
-
var genShake = (suffix, blockLen, outputLen, info = {}) => createHasher((opts2 = {}) =>
|
|
3064
|
+
var genShake = (suffix, blockLen, outputLen, info = {}) => createHasher((opts2 = {}) => {
|
|
3065
|
+
opts2 = checkOpts({}, opts2);
|
|
3066
|
+
return new Keccak(blockLen, suffix, opts2.dkLen === void 0 ? outputLen : opts2.dkLen, true);
|
|
3067
|
+
}, info);
|
|
2403
3068
|
var shake128 = /* @__PURE__ */ genShake(31, 168, 16, /* @__PURE__ */ oidNist(11));
|
|
2404
3069
|
var shake256 = /* @__PURE__ */ genShake(31, 136, 32, /* @__PURE__ */ oidNist(12));
|
|
2405
3070
|
|
|
2406
3071
|
// ../../node_modules/@noble/post-quantum/utils.js
|
|
2407
3072
|
var abytesDoc = abytes;
|
|
2408
3073
|
var randomBytes3 = randomBytes;
|
|
3074
|
+
function aarray2(item, title, inner = () => {
|
|
3075
|
+
}) {
|
|
3076
|
+
if (!Array.isArray(item))
|
|
3077
|
+
throw new TypeError(`"${title}" expected array, got type=${typeof item}`);
|
|
3078
|
+
for (let i = 0; i < item.length; i++)
|
|
3079
|
+
inner(item[i], `${title}[${i}]`);
|
|
3080
|
+
return item;
|
|
3081
|
+
}
|
|
3082
|
+
function aobject3(value, title = "object") {
|
|
3083
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
3084
|
+
throw new TypeError(title === "object" ? "expected valid options object" : `"${title}" expected object, got type=${typeof value}`);
|
|
3085
|
+
return value;
|
|
3086
|
+
}
|
|
2409
3087
|
function equalBytes2(a, b) {
|
|
3088
|
+
a = abytes(a);
|
|
3089
|
+
b = abytes(b);
|
|
2410
3090
|
if (a.length !== b.length)
|
|
2411
3091
|
return false;
|
|
2412
3092
|
let diff = 0;
|
|
@@ -2415,7 +3095,7 @@ function equalBytes2(a, b) {
|
|
|
2415
3095
|
return diff === 0;
|
|
2416
3096
|
}
|
|
2417
3097
|
function copyBytes2(bytes) {
|
|
2418
|
-
return Uint8Array
|
|
3098
|
+
return new Uint8Array(abytes(bytes));
|
|
2419
3099
|
}
|
|
2420
3100
|
function splitCoder(label, ...lengths) {
|
|
2421
3101
|
const getLength = (c) => typeof c === "number" ? c : c.bytesLen;
|
|
@@ -2455,11 +3135,12 @@ function vecCoder(c, vecLen) {
|
|
|
2455
3135
|
return {
|
|
2456
3136
|
bytesLen,
|
|
2457
3137
|
encode: (u) => {
|
|
2458
|
-
|
|
2459
|
-
|
|
3138
|
+
const uArr = aarray2(u, "u");
|
|
3139
|
+
if (uArr.length !== vecLen)
|
|
3140
|
+
throw new RangeError(`vecCoder.encode: wrong length=${uArr.length}. Expected: ${vecLen}`);
|
|
2460
3141
|
const res = new Uint8Array(bytesLen);
|
|
2461
|
-
for (let i = 0, pos = 0; i <
|
|
2462
|
-
const b = coder.encode(
|
|
3142
|
+
for (let i = 0, pos = 0; i < uArr.length; i++) {
|
|
3143
|
+
const b = coder.encode(uArr[i]);
|
|
2463
3144
|
res.set(b, pos);
|
|
2464
3145
|
b.fill(0);
|
|
2465
3146
|
pos += b.length;
|
|
@@ -2485,8 +3166,9 @@ function cleanBytes(...list) {
|
|
|
2485
3166
|
}
|
|
2486
3167
|
}
|
|
2487
3168
|
function getMask(bits) {
|
|
2488
|
-
|
|
2489
|
-
|
|
3169
|
+
anumber(bits, "bits");
|
|
3170
|
+
if (bits > 32)
|
|
3171
|
+
throw new RangeError('"bits" expected <= 32, got ' + bits);
|
|
2490
3172
|
return bits === 32 ? 4294967295 : ~(-1 << bits) >>> 0;
|
|
2491
3173
|
}
|
|
2492
3174
|
|
|
@@ -2511,13 +3193,25 @@ var genCrystals = (opts2) => {
|
|
|
2511
3193
|
return out;
|
|
2512
3194
|
}
|
|
2513
3195
|
const nttZetas = getZettas();
|
|
2514
|
-
const
|
|
3196
|
+
const inv = (_a) => {
|
|
3197
|
+
throw new Error("not implemented");
|
|
3198
|
+
};
|
|
3199
|
+
const field = isKyber ? {
|
|
3200
|
+
add: (a, b) => {
|
|
3201
|
+
const r = a + b | 0;
|
|
3202
|
+
return r >= Q2 ? r - Q2 | 0 : r;
|
|
3203
|
+
},
|
|
3204
|
+
sub: (a, b) => {
|
|
3205
|
+
const r = a - b | 0;
|
|
3206
|
+
return r < 0 ? r + Q2 | 0 : r;
|
|
3207
|
+
},
|
|
3208
|
+
mul: (a, b) => mod2((a | 0) * (b | 0)) | 0,
|
|
3209
|
+
inv
|
|
3210
|
+
} : {
|
|
2515
3211
|
add: (a, b) => mod2((a | 0) + (b | 0)) | 0,
|
|
2516
3212
|
sub: (a, b) => mod2((a | 0) - (b | 0)) | 0,
|
|
2517
3213
|
mul: (a, b) => mod2((a | 0) * (b | 0)) | 0,
|
|
2518
|
-
inv
|
|
2519
|
-
throw new Error("not implemented");
|
|
2520
|
-
}
|
|
3214
|
+
inv
|
|
2521
3215
|
};
|
|
2522
3216
|
const nttOpts = {
|
|
2523
3217
|
N: N2,
|
|
@@ -2540,6 +3234,12 @@ var genCrystals = (opts2) => {
|
|
|
2540
3234
|
}
|
|
2541
3235
|
};
|
|
2542
3236
|
const bitsCoder = (d, c) => {
|
|
3237
|
+
for (let i = 0, bufLen = 0; i < N2; i++) {
|
|
3238
|
+
bufLen += d;
|
|
3239
|
+
if (bufLen > 32)
|
|
3240
|
+
getMask(bufLen);
|
|
3241
|
+
bufLen %= 8;
|
|
3242
|
+
}
|
|
2543
3243
|
const mask = getMask(d);
|
|
2544
3244
|
const bytesLen = d * (N2 / 8);
|
|
2545
3245
|
return {
|
|
@@ -2551,7 +3251,7 @@ var genCrystals = (opts2) => {
|
|
|
2551
3251
|
buf |= (c.encode(poly[i]) & mask) << bufLen;
|
|
2552
3252
|
bufLen += d;
|
|
2553
3253
|
for (; bufLen >= 8; bufLen -= 8, buf >>= 8)
|
|
2554
|
-
r[pos++] = buf &
|
|
3254
|
+
r[pos++] = buf & 255;
|
|
2555
3255
|
}
|
|
2556
3256
|
return r;
|
|
2557
3257
|
},
|
|
@@ -2644,17 +3344,21 @@ var polyCoder = (d) => d === 12 ? byteCoder(12) : crystals.bitsCoder(d, compress
|
|
|
2644
3344
|
function polyAdd(a_, b_) {
|
|
2645
3345
|
const a = a_;
|
|
2646
3346
|
const b = b_;
|
|
2647
|
-
for (let i = 0; i < N; i++)
|
|
2648
|
-
|
|
3347
|
+
for (let i = 0; i < N; i++) {
|
|
3348
|
+
const r = a[i] + b[i];
|
|
3349
|
+
a[i] = r >= Q ? r - Q : r;
|
|
3350
|
+
}
|
|
2649
3351
|
}
|
|
2650
3352
|
function polySub(a_, b_) {
|
|
2651
3353
|
const a = a_;
|
|
2652
3354
|
const b = b_;
|
|
2653
|
-
for (let i = 0; i < N; i++)
|
|
2654
|
-
|
|
3355
|
+
for (let i = 0; i < N; i++) {
|
|
3356
|
+
const r = a[i] - b[i];
|
|
3357
|
+
a[i] = r < 0 ? r + Q : r;
|
|
3358
|
+
}
|
|
2655
3359
|
}
|
|
2656
3360
|
function BaseCaseMultiply(a0, a1, b0, b1, zeta) {
|
|
2657
|
-
const c0 = crystals.mod(a1 * b1 * zeta + a0 * b0);
|
|
3361
|
+
const c0 = crystals.mod(crystals.mod(a1 * b1) * zeta + a0 * b0);
|
|
2658
3362
|
const c1 = crystals.mod(a0 * b1 + a1 * b0);
|
|
2659
3363
|
return { c0, c1 };
|
|
2660
3364
|
}
|
|
@@ -2729,6 +3433,31 @@ var genKPKE = (opts_) => {
|
|
|
2729
3433
|
const secretCoder = vecCoder(polyCoder(12), K);
|
|
2730
3434
|
const cipherCoder = splitCoder("ciphertext", vecCoder(polyU, K), polyV);
|
|
2731
3435
|
const seedCoder = splitCoder("seed", 32, 32);
|
|
3436
|
+
const encryptCore = (tHat, getA, msg, seed) => {
|
|
3437
|
+
const rHat = [];
|
|
3438
|
+
for (let i = 0; i < K; i++)
|
|
3439
|
+
rHat.push(crystals.NTT.encode(sampleCBD(PRF, seed, i, ETA1)));
|
|
3440
|
+
const tmp2 = new Uint16Array(N);
|
|
3441
|
+
const u = [];
|
|
3442
|
+
for (let i = 0; i < K; i++) {
|
|
3443
|
+
const e1 = sampleCBD(PRF, seed, K + i, ETA2);
|
|
3444
|
+
const tmp = new Uint16Array(N);
|
|
3445
|
+
for (let j = 0; j < K; j++) {
|
|
3446
|
+
const aij = getA(i, j);
|
|
3447
|
+
polyAdd(tmp, MultiplyNTTs(aij, rHat[j]));
|
|
3448
|
+
}
|
|
3449
|
+
polyAdd(e1, crystals.NTT.decode(tmp));
|
|
3450
|
+
u.push(e1);
|
|
3451
|
+
polyAdd(tmp2, MultiplyNTTs(tHat[i], rHat[i]));
|
|
3452
|
+
cleanBytes(tmp);
|
|
3453
|
+
}
|
|
3454
|
+
const e2 = sampleCBD(PRF, seed, 2 * K, ETA2);
|
|
3455
|
+
polyAdd(e2, crystals.NTT.decode(tmp2));
|
|
3456
|
+
const v = poly1.decode(msg);
|
|
3457
|
+
polyAdd(v, e2);
|
|
3458
|
+
cleanBytes(tHat, rHat, tmp2, e2);
|
|
3459
|
+
return cipherCoder.encode([u, v]);
|
|
3460
|
+
};
|
|
2732
3461
|
return {
|
|
2733
3462
|
secretCoder,
|
|
2734
3463
|
lengths: {
|
|
@@ -2766,31 +3495,26 @@ var genKPKE = (opts_) => {
|
|
|
2766
3495
|
},
|
|
2767
3496
|
encrypt: (publicKey, msg, seed) => {
|
|
2768
3497
|
const [tHat, rho] = publicCoder.decode(publicKey);
|
|
2769
|
-
const rHat = [];
|
|
2770
|
-
for (let i = 0; i < K; i++)
|
|
2771
|
-
rHat.push(crystals.NTT.encode(sampleCBD(PRF, seed, i, ETA1)));
|
|
2772
3498
|
const x = XOF(rho);
|
|
2773
|
-
const
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
3499
|
+
const res = encryptCore(tHat, (i, j) => SampleNTT(x.get(i, j)), msg, seed);
|
|
3500
|
+
x.clean();
|
|
3501
|
+
return res;
|
|
3502
|
+
},
|
|
3503
|
+
// Expands the full  matrix (public data derived from rho) once, so repeated encryptions
|
|
3504
|
+
// against the same ek skip the K² SampleNTT XOF expansions. Cached polys are copied per
|
|
3505
|
+
// call because encryptCore mutates its inputs in place.
|
|
3506
|
+
prepare: (publicKey) => {
|
|
3507
|
+
const [tHat, rho] = publicCoder.decode(publicKey);
|
|
3508
|
+
const x = XOF(rho);
|
|
3509
|
+
const A = [];
|
|
3510
|
+
for (let i = 0; i < K; i++)
|
|
3511
|
+
for (let j = 0; j < K; j++)
|
|
3512
|
+
A.push(SampleNTT(x.get(i, j)));
|
|
2787
3513
|
x.clean();
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
cleanBytes(tHat, rHat, tmp2, e2);
|
|
2793
|
-
return cipherCoder.encode([u, v]);
|
|
3514
|
+
return {
|
|
3515
|
+
encrypt: (msg, seed) => encryptCore(tHat.map((p) => p.slice()), (i, j) => A[i * K + j].slice(), msg, seed),
|
|
3516
|
+
clean: () => cleanBytes(tHat, A)
|
|
3517
|
+
};
|
|
2794
3518
|
},
|
|
2795
3519
|
decrypt: (cipherText, privateKey) => {
|
|
2796
3520
|
const [u, v] = cipherCoder.decode(cipherText);
|
|
@@ -2799,8 +3523,9 @@ var genKPKE = (opts_) => {
|
|
|
2799
3523
|
for (let i = 0; i < K; i++)
|
|
2800
3524
|
polyAdd(tmp, MultiplyNTTs(sk[i], crystals.NTT.encode(u[i])));
|
|
2801
3525
|
polySub(v, crystals.NTT.decode(tmp));
|
|
2802
|
-
|
|
2803
|
-
|
|
3526
|
+
const res = poly1.encode(v);
|
|
3527
|
+
cleanBytes(tmp, sk, u, v);
|
|
3528
|
+
return res;
|
|
2804
3529
|
}
|
|
2805
3530
|
};
|
|
2806
3531
|
};
|
|
@@ -2812,6 +3537,14 @@ function createKyber(opts2) {
|
|
|
2812
3537
|
const secretCoder = splitCoder("secretKey", lengths.secretKey, lengths.publicKey, 32, 32);
|
|
2813
3538
|
const msgLen = 32;
|
|
2814
3539
|
const seedLen = 64;
|
|
3540
|
+
const validateModulus = (publicKey, fn) => {
|
|
3541
|
+
const eke = publicKey.subarray(0, 384 * rawOpts.K);
|
|
3542
|
+
const ek = KPKESecretCoder.encode(KPKESecretCoder.decode(copyBytes2(eke)));
|
|
3543
|
+
const ok = equalBytes2(ek, eke);
|
|
3544
|
+
cleanBytes(ek);
|
|
3545
|
+
if (!ok)
|
|
3546
|
+
throw new Error(`ML-KEM.${fn}: wrong publicKey modulus`);
|
|
3547
|
+
};
|
|
2815
3548
|
const kemLengths = Object.freeze({
|
|
2816
3549
|
...lengths,
|
|
2817
3550
|
seed: 64,
|
|
@@ -2822,38 +3555,55 @@ function createKyber(opts2) {
|
|
|
2822
3555
|
return Object.freeze({
|
|
2823
3556
|
info: Object.freeze({ type: "ml-kem" }),
|
|
2824
3557
|
lengths: kemLengths,
|
|
2825
|
-
keygen: (seed
|
|
2826
|
-
|
|
2827
|
-
const
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
3558
|
+
keygen: (seed) => {
|
|
3559
|
+
const ownSeed = seed === void 0;
|
|
3560
|
+
const s = ownSeed ? randomBytes3(seedLen) : seed;
|
|
3561
|
+
let sk;
|
|
3562
|
+
let publicKeyHash;
|
|
3563
|
+
try {
|
|
3564
|
+
abytesDoc(s, seedLen, "seed");
|
|
3565
|
+
const keys = KPKE.keygen(s.subarray(0, 32));
|
|
3566
|
+
const publicKey = keys.publicKey;
|
|
3567
|
+
sk = keys.secretKey;
|
|
3568
|
+
publicKeyHash = HASH256(publicKey);
|
|
3569
|
+
const secretKey = secretCoder.encode([sk, publicKey, publicKeyHash, s.subarray(32)]);
|
|
3570
|
+
return {
|
|
3571
|
+
publicKey,
|
|
3572
|
+
secretKey
|
|
3573
|
+
};
|
|
3574
|
+
} finally {
|
|
3575
|
+
if (sk !== void 0)
|
|
3576
|
+
cleanBytes(sk);
|
|
3577
|
+
if (publicKeyHash !== void 0)
|
|
3578
|
+
cleanBytes(publicKeyHash);
|
|
3579
|
+
if (ownSeed)
|
|
3580
|
+
cleanBytes(s);
|
|
3581
|
+
}
|
|
2835
3582
|
},
|
|
2836
3583
|
getPublicKey: (secretKey) => {
|
|
2837
3584
|
const [_sk, publicKey, _publicKeyHash, _z] = secretCoder.decode(secretKey);
|
|
2838
3585
|
return Uint8Array.from(publicKey);
|
|
2839
3586
|
},
|
|
2840
|
-
encapsulate: (publicKey, msg
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
3587
|
+
encapsulate: (publicKey, msg) => {
|
|
3588
|
+
const ownMsg = msg === void 0;
|
|
3589
|
+
const m = ownMsg ? randomBytes3(msgLen) : msg;
|
|
3590
|
+
let kr;
|
|
3591
|
+
try {
|
|
3592
|
+
abytesDoc(publicKey, lengths.publicKey, "publicKey");
|
|
3593
|
+
abytesDoc(m, msgLen, "message");
|
|
3594
|
+
validateModulus(publicKey, "encapsulate");
|
|
3595
|
+
kr = HASH512.create().update(m).update(HASH256(publicKey)).digest();
|
|
3596
|
+
const cipherText = KPKE.encrypt(publicKey, m, kr.subarray(32, 64));
|
|
3597
|
+
return {
|
|
3598
|
+
cipherText,
|
|
3599
|
+
sharedSecret: kr.subarray(0, 32)
|
|
3600
|
+
};
|
|
3601
|
+
} finally {
|
|
3602
|
+
if (kr !== void 0)
|
|
3603
|
+
cleanBytes(kr.subarray(32));
|
|
3604
|
+
if (ownMsg)
|
|
3605
|
+
cleanBytes(m);
|
|
2848
3606
|
}
|
|
2849
|
-
cleanBytes(ek);
|
|
2850
|
-
const kr = HASH512.create().update(msg).update(HASH256(publicKey)).digest();
|
|
2851
|
-
const cipherText = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
|
|
2852
|
-
cleanBytes(kr.subarray(32));
|
|
2853
|
-
return {
|
|
2854
|
-
cipherText,
|
|
2855
|
-
sharedSecret: kr.subarray(0, 32)
|
|
2856
|
-
};
|
|
2857
3607
|
},
|
|
2858
3608
|
decapsulate: (cipherText, secretKey) => {
|
|
2859
3609
|
abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
|
|
@@ -2870,8 +3620,58 @@ function createKyber(opts2) {
|
|
|
2870
3620
|
const cipherText2 = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
|
|
2871
3621
|
const isValid = equalBytes2(cipherText, cipherText2);
|
|
2872
3622
|
const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
|
|
2873
|
-
cleanBytes(msg, cipherText2, !isValid ? Khat : Kbar);
|
|
3623
|
+
cleanBytes(msg, cipherText2, kr.subarray(32), !isValid ? Khat : Kbar);
|
|
2874
3624
|
return isValid ? Khat : Kbar;
|
|
3625
|
+
},
|
|
3626
|
+
/**
|
|
3627
|
+
* Experimental prototype: pre-expand a public key so repeated encapsulate/decapsulate
|
|
3628
|
+
* against the same key skip re-validation, H(ek), t̂ decoding and the K² SampleNTT
|
|
3629
|
+
* XOF expansions of Â. Only public data is cached; see {@link KEMPrepared}.
|
|
3630
|
+
*/
|
|
3631
|
+
prepare: (publicKey) => {
|
|
3632
|
+
abytesDoc(publicKey, lengths.publicKey, "publicKey");
|
|
3633
|
+
validateModulus(publicKey, "prepare");
|
|
3634
|
+
const ek = copyBytes2(publicKey);
|
|
3635
|
+
const publicKeyHash = HASH256(ek);
|
|
3636
|
+
const cached = KPKE.prepare(ek);
|
|
3637
|
+
return Object.freeze({
|
|
3638
|
+
publicKey: ek,
|
|
3639
|
+
encapsulate: (msg) => {
|
|
3640
|
+
const ownMsg = msg === void 0;
|
|
3641
|
+
const m = ownMsg ? randomBytes3(msgLen) : msg;
|
|
3642
|
+
let kr;
|
|
3643
|
+
try {
|
|
3644
|
+
abytesDoc(m, msgLen, "message");
|
|
3645
|
+
kr = HASH512.create().update(m).update(publicKeyHash).digest();
|
|
3646
|
+
const cipherText = cached.encrypt(m, kr.subarray(32, 64));
|
|
3647
|
+
return {
|
|
3648
|
+
cipherText,
|
|
3649
|
+
sharedSecret: kr.subarray(0, 32)
|
|
3650
|
+
};
|
|
3651
|
+
} finally {
|
|
3652
|
+
if (kr !== void 0)
|
|
3653
|
+
cleanBytes(kr.subarray(32));
|
|
3654
|
+
if (ownMsg)
|
|
3655
|
+
cleanBytes(m);
|
|
3656
|
+
}
|
|
3657
|
+
},
|
|
3658
|
+
decapsulate: (cipherText, secretKey) => {
|
|
3659
|
+
abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
|
|
3660
|
+
abytesDoc(cipherText, lengths.cipherText, "cipherText");
|
|
3661
|
+
const [sk, ekEmbedded, storedHash, z] = secretCoder.decode(secretKey);
|
|
3662
|
+
if (!equalBytes2(ekEmbedded, ek) || !equalBytes2(storedHash, publicKeyHash))
|
|
3663
|
+
throw new Error("ML-KEM.decapsulate: secretKey does not match prepared publicKey");
|
|
3664
|
+
const msg = KPKE.decrypt(cipherText, sk);
|
|
3665
|
+
const kr = HASH512.create().update(msg).update(publicKeyHash).digest();
|
|
3666
|
+
const Khat = kr.subarray(0, 32);
|
|
3667
|
+
const cipherText2 = cached.encrypt(msg, kr.subarray(32, 64));
|
|
3668
|
+
const isValid = equalBytes2(cipherText, cipherText2);
|
|
3669
|
+
const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
|
|
3670
|
+
cleanBytes(msg, cipherText2, kr.subarray(32), !isValid ? Khat : Kbar);
|
|
3671
|
+
return isValid ? Khat : Kbar;
|
|
3672
|
+
},
|
|
3673
|
+
clean: cached.clean
|
|
3674
|
+
});
|
|
2875
3675
|
}
|
|
2876
3676
|
});
|
|
2877
3677
|
}
|
|
@@ -2892,7 +3692,21 @@ var mk = (params) => createKyber({
|
|
|
2892
3692
|
var ml_kem768 = /* @__PURE__ */ (() => mk(PARAMS[768]))();
|
|
2893
3693
|
|
|
2894
3694
|
// ../../node_modules/@noble/post-quantum/hybrid.js
|
|
3695
|
+
var validateKEM = (kem, title) => {
|
|
3696
|
+
const k = aobject3(kem, title);
|
|
3697
|
+
aobject3(k.lengths, `${title}.lengths`);
|
|
3698
|
+
afunction(k.keygen, `${title}.keygen`);
|
|
3699
|
+
afunction(k.getPublicKey, `${title}.getPublicKey`);
|
|
3700
|
+
afunction(k.encapsulate, `${title}.encapsulate`);
|
|
3701
|
+
afunction(k.decapsulate, `${title}.decapsulate`);
|
|
3702
|
+
return k;
|
|
3703
|
+
};
|
|
2895
3704
|
function ecKeygen(curve, allowZeroKey = false) {
|
|
3705
|
+
const c = aobject3(curve, "curve");
|
|
3706
|
+
aobject3(c.lengths, "curve.lengths");
|
|
3707
|
+
afunction(c.keygen, "curve.keygen");
|
|
3708
|
+
afunction(c.getPublicKey, "curve.getPublicKey");
|
|
3709
|
+
abool2(allowZeroKey, "allowZeroKey");
|
|
2896
3710
|
const lengths = curve.lengths;
|
|
2897
3711
|
let keygen = curve.keygen;
|
|
2898
3712
|
if (allowZeroKey) {
|
|
@@ -2916,10 +3730,14 @@ function ecKeygen(curve, allowZeroKey = false) {
|
|
|
2916
3730
|
getPublicKey: (secretKey) => curve.getPublicKey(secretKey)
|
|
2917
3731
|
};
|
|
2918
3732
|
}
|
|
2919
|
-
function
|
|
3733
|
+
function _ecdhKem(curve, allowZeroKey = false) {
|
|
2920
3734
|
const kg = ecKeygen(curve, allowZeroKey);
|
|
2921
3735
|
if (!curve.getSharedSecret)
|
|
2922
3736
|
throw new Error("wrong curve");
|
|
3737
|
+
const decapsulate = (cipherText, secretKey) => {
|
|
3738
|
+
const res = curve.getSharedSecret(secretKey, cipherText);
|
|
3739
|
+
return curve.lengths.publicKeyHasPrefix ? res.subarray(1) : res;
|
|
3740
|
+
};
|
|
2923
3741
|
return {
|
|
2924
3742
|
lengths: { ...kg.lengths, msg: kg.lengths.seed, cipherText: kg.lengths.publicKey },
|
|
2925
3743
|
keygen: kg.keygen,
|
|
@@ -2928,8 +3746,8 @@ function ecdhKem(curve, allowZeroKey = false) {
|
|
|
2928
3746
|
const seed = copyBytes2(rand);
|
|
2929
3747
|
let ek = void 0;
|
|
2930
3748
|
try {
|
|
2931
|
-
ek =
|
|
2932
|
-
const sharedSecret =
|
|
3749
|
+
ek = kg.keygen(seed).secretKey;
|
|
3750
|
+
const sharedSecret = decapsulate(publicKey, ek);
|
|
2933
3751
|
const cipherText = curve.getPublicKey(ek);
|
|
2934
3752
|
return { sharedSecret, cipherText };
|
|
2935
3753
|
} finally {
|
|
@@ -2938,18 +3756,23 @@ function ecdhKem(curve, allowZeroKey = false) {
|
|
|
2938
3756
|
cleanBytes(ek);
|
|
2939
3757
|
}
|
|
2940
3758
|
},
|
|
2941
|
-
decapsulate
|
|
2942
|
-
const res = curve.getSharedSecret(secretKey, cipherText);
|
|
2943
|
-
return curve.lengths.publicKeyHasPrefix ? res.subarray(1) : res;
|
|
2944
|
-
}
|
|
3759
|
+
decapsulate
|
|
2945
3760
|
};
|
|
2946
3761
|
}
|
|
3762
|
+
function positiveLength(value, title) {
|
|
3763
|
+
const length = anumber(value, title);
|
|
3764
|
+
if (length === 0)
|
|
3765
|
+
throw new RangeError(`"${title}" expected integer greater than 0, got 0`);
|
|
3766
|
+
return length;
|
|
3767
|
+
}
|
|
2947
3768
|
function splitLengths(lst, name) {
|
|
2948
|
-
|
|
3769
|
+
const coder = splitCoder(name, ...lst.map((i) => {
|
|
2949
3770
|
if (typeof i.lengths[name] !== "number")
|
|
2950
3771
|
throw new Error("wrong length: " + name);
|
|
2951
|
-
return i.lengths[name];
|
|
3772
|
+
return positiveLength(i.lengths[name], name);
|
|
2952
3773
|
}));
|
|
3774
|
+
positiveLength(coder.bytesLen, name);
|
|
3775
|
+
return coder;
|
|
2953
3776
|
}
|
|
2954
3777
|
function expandSeedXof(xof) {
|
|
2955
3778
|
return ((seed, seedLen) => xof(seed, { dkLen: seedLen }));
|
|
@@ -2959,11 +3782,9 @@ function combineKeys(realSeedLen, expandSeed_, ...ck_) {
|
|
|
2959
3782
|
const ck = ck_;
|
|
2960
3783
|
const seedCoder = splitLengths(ck, "seed");
|
|
2961
3784
|
const pkCoder = splitLengths(ck, "publicKey");
|
|
2962
|
-
|
|
2963
|
-
realSeedLen = seedCoder.bytesLen;
|
|
2964
|
-
anumber(realSeedLen);
|
|
3785
|
+
const rootSeedLen = positiveLength(realSeedLen === void 0 ? seedCoder.bytesLen : realSeedLen, "realSeedLen");
|
|
2965
3786
|
function expandDecapsulationKey(seed) {
|
|
2966
|
-
abytes(seed,
|
|
3787
|
+
abytes(seed, rootSeedLen);
|
|
2967
3788
|
const expandedRaw = expandSeed(seed, seedCoder.bytesLen);
|
|
2968
3789
|
const expandedSeed = expandedRaw.buffer === seed.buffer ? copyBytes2(expandedRaw) : expandedRaw;
|
|
2969
3790
|
const expanded = [];
|
|
@@ -2988,41 +3809,79 @@ function combineKeys(realSeedLen, expandSeed_, ...ck_) {
|
|
|
2988
3809
|
cleanBytes(secretKey);
|
|
2989
3810
|
}
|
|
2990
3811
|
}
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
keygen(seed = randomBytes3(realSeedLen)) {
|
|
2997
|
-
const { publicKey: pk, secretKey } = expandDecapsulationKey(seed);
|
|
3812
|
+
const keygen = (seed) => {
|
|
3813
|
+
const root = seed === void 0 ? randomBytes3(rootSeedLen) : copyBytes2(seed);
|
|
3814
|
+
let res;
|
|
3815
|
+
try {
|
|
3816
|
+
const { publicKey: pk, secretKey } = expandDecapsulationKey(root);
|
|
2998
3817
|
try {
|
|
2999
|
-
|
|
3000
|
-
|
|
3818
|
+
res = {
|
|
3819
|
+
secretKey: root,
|
|
3820
|
+
publicKey: pkCoder.encode(pk)
|
|
3821
|
+
};
|
|
3001
3822
|
} finally {
|
|
3002
|
-
cleanBytes(pk);
|
|
3003
|
-
cleanBytes(secretKey);
|
|
3823
|
+
cleanBytes(pk, secretKey);
|
|
3004
3824
|
}
|
|
3825
|
+
return res;
|
|
3826
|
+
} finally {
|
|
3827
|
+
if (!res)
|
|
3828
|
+
cleanBytes(root);
|
|
3829
|
+
}
|
|
3830
|
+
};
|
|
3831
|
+
return {
|
|
3832
|
+
info: { lengths: { seed: rootSeedLen, publicKey: pkCoder.bytesLen, secretKey: rootSeedLen } },
|
|
3833
|
+
// Composite secret keys are root seeds, so public-key derivation reruns key expansion from
|
|
3834
|
+
// that seed instead of decoding a packed child-secret-key structure.
|
|
3835
|
+
getPublicKey: (secretKey) => {
|
|
3836
|
+
const keys = keygen(secretKey);
|
|
3837
|
+
cleanBytes(keys.secretKey);
|
|
3838
|
+
return keys.publicKey;
|
|
3005
3839
|
},
|
|
3840
|
+
keygen,
|
|
3006
3841
|
expandDecapsulationKey,
|
|
3007
|
-
realSeedLen
|
|
3842
|
+
realSeedLen: rootSeedLen
|
|
3008
3843
|
};
|
|
3009
3844
|
}
|
|
3010
3845
|
function combineKEMS(realSeedLen, realMsgLen, expandSeed, combiner, ...kems) {
|
|
3846
|
+
if (realSeedLen !== void 0)
|
|
3847
|
+
positiveLength(realSeedLen, "realSeedLen");
|
|
3848
|
+
if (realMsgLen !== void 0)
|
|
3849
|
+
positiveLength(realMsgLen, "realMsgLen");
|
|
3850
|
+
if (typeof expandSeed !== "function")
|
|
3851
|
+
throw new TypeError('"expandSeed" expected function, got type=' + typeof expandSeed);
|
|
3852
|
+
if (typeof combiner !== "function")
|
|
3853
|
+
throw new TypeError('"combiner" expected function, got type=' + typeof combiner);
|
|
3011
3854
|
const rawCombiner = combiner;
|
|
3012
3855
|
const rawKems = kems;
|
|
3856
|
+
if (rawKems.length === 0)
|
|
3857
|
+
throw new RangeError("combineKEMS requires at least one KEM");
|
|
3858
|
+
for (let i = 0; i < rawKems.length; i++)
|
|
3859
|
+
validateKEM(rawKems[i], `kems[${i}]`);
|
|
3013
3860
|
const keys = combineKeys(realSeedLen, expandSeed, ...rawKems);
|
|
3014
3861
|
const ctCoder = splitLengths(rawKems, "cipherText");
|
|
3015
3862
|
const pkCoder = splitLengths(rawKems, "publicKey");
|
|
3016
3863
|
const msgCoder = splitLengths(rawKems, "msg");
|
|
3017
|
-
|
|
3018
|
-
realMsgLen = msgCoder.bytesLen;
|
|
3019
|
-
anumber(realMsgLen);
|
|
3864
|
+
const sharedSecretLen = positiveLength(realMsgLen === void 0 ? msgCoder.bytesLen : realMsgLen, "realMsgLen");
|
|
3020
3865
|
const lengths = Object.freeze({
|
|
3021
3866
|
...keys.info.lengths,
|
|
3022
|
-
msg:
|
|
3867
|
+
msg: sharedSecretLen,
|
|
3023
3868
|
msgRand: msgCoder.bytesLen,
|
|
3024
3869
|
cipherText: ctCoder.bytesLen
|
|
3025
3870
|
});
|
|
3871
|
+
const combine = (publicKeys, cipherTexts, sharedSecrets) => {
|
|
3872
|
+
const combined = rawCombiner(publicKeys, cipherTexts, sharedSecrets);
|
|
3873
|
+
try {
|
|
3874
|
+
return copyBytes2(abytes(combined, sharedSecretLen, "sharedSecret"));
|
|
3875
|
+
} catch (error) {
|
|
3876
|
+
if (isBytes(combined)) {
|
|
3877
|
+
const overlaps = (value) => combined.buffer === value.buffer && combined.byteOffset < value.byteOffset + value.byteLength && value.byteOffset < combined.byteOffset + combined.byteLength;
|
|
3878
|
+
const aliasesPublicInput = publicKeys.some(overlaps) || cipherTexts.some(overlaps);
|
|
3879
|
+
if (!aliasesPublicInput)
|
|
3880
|
+
cleanBytes(combined);
|
|
3881
|
+
}
|
|
3882
|
+
throw error;
|
|
3883
|
+
}
|
|
3884
|
+
};
|
|
3026
3885
|
return Object.freeze({
|
|
3027
3886
|
lengths,
|
|
3028
3887
|
getPublicKey: keys.getPublicKey,
|
|
@@ -3034,15 +3893,16 @@ function combineKEMS(realSeedLen, realMsgLen, expandSeed, combiner, ...kems) {
|
|
|
3034
3893
|
const cipherText = [];
|
|
3035
3894
|
try {
|
|
3036
3895
|
for (let i = 0; i < rawKems.length; i++) {
|
|
3037
|
-
const
|
|
3038
|
-
sharedSecret.push(
|
|
3039
|
-
cipherText.push(
|
|
3896
|
+
const enc2 = rawKems[i].encapsulate(pks[i], rand[i]);
|
|
3897
|
+
sharedSecret.push(enc2.sharedSecret);
|
|
3898
|
+
cipherText.push(enc2.cipherText);
|
|
3040
3899
|
}
|
|
3900
|
+
const encodedCipherText = ctCoder.encode(cipherText);
|
|
3041
3901
|
return {
|
|
3042
3902
|
// Detach the combiner result before cleanup: a caller-provided combiner may alias one of
|
|
3043
3903
|
// the child sharedSecret buffers, and those child buffers are zeroized immediately below.
|
|
3044
|
-
sharedSecret:
|
|
3045
|
-
cipherText:
|
|
3904
|
+
sharedSecret: combine(pks, cipherText, sharedSecret),
|
|
3905
|
+
cipherText: encodedCipherText
|
|
3046
3906
|
};
|
|
3047
3907
|
} finally {
|
|
3048
3908
|
cleanBytes(sharedSecret, cipherText);
|
|
@@ -3051,16 +3911,18 @@ function combineKEMS(realSeedLen, realMsgLen, expandSeed, combiner, ...kems) {
|
|
|
3051
3911
|
decapsulate(ct, seed) {
|
|
3052
3912
|
const cts = ctCoder.decode(ct);
|
|
3053
3913
|
const { publicKey, secretKey } = keys.expandDecapsulationKey(seed);
|
|
3054
|
-
const sharedSecret =
|
|
3914
|
+
const sharedSecret = [];
|
|
3055
3915
|
try {
|
|
3056
|
-
|
|
3916
|
+
for (let i = 0; i < rawKems.length; i++)
|
|
3917
|
+
sharedSecret.push(rawKems[i].decapsulate(cts[i], secretKey[i]));
|
|
3918
|
+
return combine(publicKey, cts, sharedSecret);
|
|
3057
3919
|
} finally {
|
|
3058
3920
|
cleanBytes(secretKey, sharedSecret);
|
|
3059
3921
|
}
|
|
3060
3922
|
}
|
|
3061
3923
|
});
|
|
3062
3924
|
}
|
|
3063
|
-
var x25519kem = /* @__PURE__ */
|
|
3925
|
+
var x25519kem = /* @__PURE__ */ _ecdhKem(x25519);
|
|
3064
3926
|
var ml_kem768_x25519 = /* @__PURE__ */ (() => combineKEMS(
|
|
3065
3927
|
32,
|
|
3066
3928
|
32,
|
|
@@ -3070,7 +3932,23 @@ var ml_kem768_x25519 = /* @__PURE__ */ (() => combineKEMS(
|
|
|
3070
3932
|
ml_kem768,
|
|
3071
3933
|
x25519kem
|
|
3072
3934
|
))();
|
|
3073
|
-
|
|
3935
|
+
|
|
3936
|
+
// ../crypto/src/endpoint-health.ts
|
|
3937
|
+
var ENDPOINT_PROBE_INTERVAL_MINUTES = 15;
|
|
3938
|
+
var ENDPOINT_STALE_AFTER_MINUTES = ENDPOINT_PROBE_INTERVAL_MINUTES * 4;
|
|
3939
|
+
var ENDPOINT_UNUSABLE_AFTER_MINUTES = ENDPOINT_PROBE_INTERVAL_MINUTES * 24;
|
|
3940
|
+
|
|
3941
|
+
// ../crypto/src/registry-root-pin.ts
|
|
3942
|
+
var PINNED_REGISTRY_ROOTS = Object.freeze([
|
|
3943
|
+
Object.freeze({
|
|
3944
|
+
root_key_id: "rk1_251368ec9b0a84ae",
|
|
3945
|
+
spki_sha256: "251368ec9b0a84ae9881533bd1687efb7d439edd927aef47c1bb3bb069b3dc1d"
|
|
3946
|
+
})
|
|
3947
|
+
]);
|
|
3948
|
+
|
|
3949
|
+
// ../crypto/src/vault-pin-hardening.ts
|
|
3950
|
+
var oprf = ristretto255_oprf.oprf;
|
|
3951
|
+
var enc = new TextEncoder();
|
|
3074
3952
|
|
|
3075
3953
|
// ../crypto/src/index.ts
|
|
3076
3954
|
function b64ToBytes(b64) {
|
|
@@ -3164,7 +4042,7 @@ async function encryptEnvelopeForPartner(payload, partner) {
|
|
|
3164
4042
|
const session_envelope = bytesToB64Url(concat(sessionIv, sessionCt));
|
|
3165
4043
|
const skRawForWrap = new Uint8Array(await subtle().exportKey("raw", sk));
|
|
3166
4044
|
if (isHybridScheme(partner.algorithm)) {
|
|
3167
|
-
const { cipherText: kemCt, sharedSecret: sharedSecret2 } =
|
|
4045
|
+
const { cipherText: kemCt, sharedSecret: sharedSecret2 } = ml_kem768_x25519.encapsulate(b64ToBytes(partner.public_key_b64));
|
|
3168
4046
|
const wrapSalt2 = globalThis.crypto.getRandomValues(new Uint8Array(WRAP_SALT_LENGTH));
|
|
3169
4047
|
const wrapKeyRaw2 = await subtle().deriveBits(
|
|
3170
4048
|
{ name: "HKDF", hash: "SHA-256", salt: wrapSalt2, info: sessionWrapInfoPq(partner.partner_id) },
|
|
@@ -3343,10 +4221,12 @@ var D5_BODY_CONDITIONAL = {
|
|
|
3343
4221
|
crossover_key_ids: "key-crossover copy only"
|
|
3344
4222
|
};
|
|
3345
4223
|
var D5_BODY_DRILL_ONLY = ["conformance"];
|
|
4224
|
+
var D5_BODY_ADDED_AT_SEND = ["dispatch_id"];
|
|
3346
4225
|
var D5_BODY_ALL_ALLOWED = [
|
|
3347
4226
|
...D5_BODY_ALWAYS,
|
|
3348
4227
|
...Object.keys(D5_BODY_CONDITIONAL),
|
|
3349
|
-
...D5_BODY_DRILL_ONLY
|
|
4228
|
+
...D5_BODY_DRILL_ONLY,
|
|
4229
|
+
...D5_BODY_ADDED_AT_SEND
|
|
3350
4230
|
];
|
|
3351
4231
|
|
|
3352
4232
|
// ../protocol/src/retention.ts
|
|
@@ -3580,8 +4460,9 @@ async function post(url, body, headers, timeoutMs = 12e3) {
|
|
|
3580
4460
|
clearTimeout(timer);
|
|
3581
4461
|
}
|
|
3582
4462
|
}
|
|
3583
|
-
function
|
|
3584
|
-
|
|
4463
|
+
function signPullV2(timestamp, secret, url, method = "GET", body = "") {
|
|
4464
|
+
const u = new URL(url);
|
|
4465
|
+
return import_node_crypto.default.createHmac("sha256", secret).update(`v2.${timestamp}.${method.toUpperCase()}.${u.pathname}${u.search}.${body}`).digest("hex");
|
|
3585
4466
|
}
|
|
3586
4467
|
async function get(url, headers, timeoutMs = 12e3) {
|
|
3587
4468
|
const controller = new AbortController();
|
|
@@ -3759,6 +4640,7 @@ async function runChecksDetailed(urlOrOptions, secret = TEST_WEBHOOK_SECRET, par
|
|
|
3759
4640
|
const ts2 = String(Math.floor(Date.now() / 1e3));
|
|
3760
4641
|
const sig2 = sign(sharedBody, ts2, secret);
|
|
3761
4642
|
const res2 = await fetch(targetUrl, {
|
|
4643
|
+
redirect: "error",
|
|
3762
4644
|
method: "POST",
|
|
3763
4645
|
headers: buildHeaders(partnerId, ts2, sig2, `dedup-${dedupId}`),
|
|
3764
4646
|
body: sharedBody
|
|
@@ -3831,6 +4713,7 @@ async function runChecksDetailed(urlOrOptions, secret = TEST_WEBHOOK_SECRET, par
|
|
|
3831
4713
|
const ts = String(Math.floor(Date.now() / 1e3));
|
|
3832
4714
|
const sig = sign(foreignBody, ts, secret);
|
|
3833
4715
|
const res = await fetch(targetUrl, {
|
|
4716
|
+
redirect: "error",
|
|
3834
4717
|
method: "POST",
|
|
3835
4718
|
headers: buildHeaders(partnerId, ts, sig, `undecryptable-${import_node_crypto.default.randomUUID().slice(0, 8)}`),
|
|
3836
4719
|
body: foreignBody,
|
|
@@ -3865,6 +4748,7 @@ async function runChecksDetailed(urlOrOptions, secret = TEST_WEBHOOK_SECRET, par
|
|
|
3865
4748
|
const ts = String(Math.floor(Date.now() / 1e3));
|
|
3866
4749
|
const sig = sign(body, ts, secret);
|
|
3867
4750
|
const res = await fetch(targetUrl, {
|
|
4751
|
+
redirect: "error",
|
|
3868
4752
|
method: "POST",
|
|
3869
4753
|
headers: buildHeaders(partnerId, ts, sig, `nomatch-${nonce.slice(0, 8)}`),
|
|
3870
4754
|
body,
|
|
@@ -3906,6 +4790,7 @@ async function runChecksDetailed(urlOrOptions, secret = TEST_WEBHOOK_SECRET, par
|
|
|
3906
4790
|
const ts = String(Math.floor(Date.now() / 1e3));
|
|
3907
4791
|
const sig = sign(body, ts, secret);
|
|
3908
4792
|
const res = await fetch(targetUrl, {
|
|
4793
|
+
redirect: "error",
|
|
3909
4794
|
method: "POST",
|
|
3910
4795
|
// `contact.updated`, matching what the relay now sends for a dispatch
|
|
3911
4796
|
// carrying no address (`dispatchEventName`). This line previously said
|
|
@@ -4082,11 +4967,12 @@ async function runChecksDetailed(urlOrOptions, secret = TEST_WEBHOOK_SECRET, par
|
|
|
4082
4967
|
const pullBase = customerUrl.replace(/\/$/, "");
|
|
4083
4968
|
{
|
|
4084
4969
|
const ts = String(Math.floor(Date.now() / 1e3));
|
|
4085
|
-
const sig = signGet(ts, secret);
|
|
4086
4970
|
const url = `${pullBase}/api/partners/${partnerId}/updates`;
|
|
4971
|
+
const sig = signPullV2(ts, secret, url);
|
|
4087
4972
|
const { status, error } = await get(url, {
|
|
4088
4973
|
"X-OneAddress-Timestamp": ts,
|
|
4089
|
-
"X-OneAddress-Signature": sig
|
|
4974
|
+
"X-OneAddress-Signature": sig,
|
|
4975
|
+
"X-OneAddress-Signature-Version": "2"
|
|
4090
4976
|
});
|
|
4091
4977
|
const passed = !error && status === 200;
|
|
4092
4978
|
results.push({
|
|
@@ -4099,11 +4985,12 @@ async function runChecksDetailed(urlOrOptions, secret = TEST_WEBHOOK_SECRET, par
|
|
|
4099
4985
|
}
|
|
4100
4986
|
{
|
|
4101
4987
|
const ts = String(Math.floor(Date.now() / 1e3));
|
|
4102
|
-
const sig = signGet(ts, secret);
|
|
4103
4988
|
const url = `${pullBase}/api/partners/${partnerId}/audit`;
|
|
4989
|
+
const sig = signPullV2(ts, secret, url);
|
|
4104
4990
|
const { status, headers: resHdrs, body: resBody, error } = await get(url, {
|
|
4105
4991
|
"X-OneAddress-Timestamp": ts,
|
|
4106
|
-
"X-OneAddress-Signature": sig
|
|
4992
|
+
"X-OneAddress-Signature": sig,
|
|
4993
|
+
"X-OneAddress-Signature-Version": "2"
|
|
4107
4994
|
});
|
|
4108
4995
|
let passed = false;
|
|
4109
4996
|
let detail;
|
|
@@ -4171,7 +5058,8 @@ function markUnprovenChecks(results) {
|
|
|
4171
5058
|
markUnprovenChecks,
|
|
4172
5059
|
plannedCoreCheckIds,
|
|
4173
5060
|
runChecks,
|
|
4174
|
-
runChecksDetailed
|
|
5061
|
+
runChecksDetailed,
|
|
5062
|
+
signPullV2
|
|
4175
5063
|
});
|
|
4176
5064
|
/*! Bundled license information:
|
|
4177
5065
|
|
|
@@ -4180,6 +5068,7 @@ function markUnprovenChecks(results) {
|
|
|
4180
5068
|
@noble/curves/abstract/curve.js:
|
|
4181
5069
|
@noble/curves/abstract/edwards.js:
|
|
4182
5070
|
@noble/curves/abstract/montgomery.js:
|
|
5071
|
+
@noble/curves/abstract/oprf.js:
|
|
4183
5072
|
@noble/curves/ed25519.js:
|
|
4184
5073
|
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
4185
5074
|
|