@oneaddress/partner-sdk 2.6.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +170 -0
- package/README.md +184 -61
- package/dist/{chunk-XPQERNNN.mjs → chunk-BFSYMDJI.mjs} +545 -53
- package/dist/{chunk-QAOGKPD3.mjs → chunk-PP2TM7HN.mjs} +1263 -112
- package/dist/edge.d.mts +1 -1
- package/dist/edge.d.ts +1 -1
- package/dist/edge.js +1782 -160
- package/dist/edge.mjs +28 -2
- package/dist/index.d.mts +415 -52
- package/dist/index.d.ts +415 -52
- package/dist/index.js +1881 -167
- package/dist/index.mjs +50 -2
- package/dist/{src-BPES2DKY.mjs → src-P3LR7F4A.mjs} +13 -1
- package/package.json +1 -1
|
@@ -65,6 +65,9 @@ function clean(...arrays) {
|
|
|
65
65
|
arrays[i].fill(0);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
+
function createView(arr) {
|
|
69
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
70
|
+
}
|
|
68
71
|
var isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
|
|
69
72
|
function byteSwap(word) {
|
|
70
73
|
return word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
|
|
@@ -249,6 +252,16 @@ function numberToBytesBE(n, len) {
|
|
|
249
252
|
function numberToBytesLE(n, len) {
|
|
250
253
|
return numberToBytesBE(n, len).reverse();
|
|
251
254
|
}
|
|
255
|
+
function equalBytes(a, b) {
|
|
256
|
+
a = abytes2(a);
|
|
257
|
+
b = abytes2(b);
|
|
258
|
+
if (a.length !== b.length)
|
|
259
|
+
return false;
|
|
260
|
+
let diff = 0;
|
|
261
|
+
for (let i = 0; i < a.length; i++)
|
|
262
|
+
diff |= a[i] ^ b[i];
|
|
263
|
+
return diff === 0;
|
|
264
|
+
}
|
|
252
265
|
function copyBytes(bytes) {
|
|
253
266
|
return Uint8Array.from(abytes2(bytes));
|
|
254
267
|
}
|
|
@@ -302,6 +315,9 @@ function validateObject(object, fields = {}, optFields = {}, title = "object") {
|
|
|
302
315
|
iter(fields, false);
|
|
303
316
|
iter(optFields, true);
|
|
304
317
|
}
|
|
318
|
+
var notImplemented = () => {
|
|
319
|
+
throw new Error("not implemented");
|
|
320
|
+
};
|
|
305
321
|
|
|
306
322
|
// ../../node_modules/@noble/curves/abstract/modular.js
|
|
307
323
|
var _0n2 = /* @__PURE__ */ BigInt(0);
|
|
@@ -399,8 +415,19 @@ function invert(number, modulo) {
|
|
|
399
415
|
throw new Error("invert: does not exist");
|
|
400
416
|
return mod(x, modulo);
|
|
401
417
|
}
|
|
402
|
-
function
|
|
403
|
-
|
|
418
|
+
function invertCt(a, prime) {
|
|
419
|
+
if (prime <= _1n2)
|
|
420
|
+
throw new Error("invertCt: expected prime modulus > 1, got " + prime);
|
|
421
|
+
const an = mod(a, prime);
|
|
422
|
+
if (an === _0n2)
|
|
423
|
+
throw new Error("invertCt: expected non-zero number");
|
|
424
|
+
const inverse = pow(an, prime - _2n, prime);
|
|
425
|
+
if (mod(an * inverse, prime) !== _1n2)
|
|
426
|
+
throw new Error("invertCt: does not exist");
|
|
427
|
+
return inverse;
|
|
428
|
+
}
|
|
429
|
+
function assertIsSquare(Fp2, root, n) {
|
|
430
|
+
const F2 = Fp2;
|
|
404
431
|
if (!F2.eql(F2.sqr(root), n))
|
|
405
432
|
throw new Error("Cannot find square root");
|
|
406
433
|
}
|
|
@@ -408,15 +435,15 @@ function aoddModulus(order, fnName) {
|
|
|
408
435
|
if ((order & _1n2) === _0n2)
|
|
409
436
|
throw new Error(fnName + ": expected odd modulus, got " + order);
|
|
410
437
|
}
|
|
411
|
-
function sqrt3mod4(
|
|
412
|
-
const F2 =
|
|
438
|
+
function sqrt3mod4(Fp2, n) {
|
|
439
|
+
const F2 = Fp2;
|
|
413
440
|
const p1div4 = (F2.ORDER + _1n2) / _4n;
|
|
414
441
|
const root = F2.pow(n, p1div4);
|
|
415
442
|
assertIsSquare(F2, root, n);
|
|
416
443
|
return root;
|
|
417
444
|
}
|
|
418
|
-
function sqrt5mod8(
|
|
419
|
-
const F2 =
|
|
445
|
+
function sqrt5mod8(Fp2, n) {
|
|
446
|
+
const F2 = Fp2;
|
|
420
447
|
const p5div8 = (F2.ORDER - _5n) / _8n;
|
|
421
448
|
const n2 = F2.mul(n, _2n);
|
|
422
449
|
const v = F2.pow(n2, p5div8);
|
|
@@ -433,8 +460,8 @@ function sqrt9mod16(P) {
|
|
|
433
460
|
const c2 = tn(Fp_, c1);
|
|
434
461
|
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
435
462
|
const c4 = (P + _7n) / _16n;
|
|
436
|
-
return ((
|
|
437
|
-
const F2 =
|
|
463
|
+
return ((Fp2, n) => {
|
|
464
|
+
const F2 = Fp2;
|
|
438
465
|
let tv1 = F2.pow(n, c4);
|
|
439
466
|
let tv2 = F2.mul(tv1, c1);
|
|
440
467
|
const tv3 = F2.mul(tv1, c2);
|
|
@@ -469,8 +496,8 @@ function tonelliShanks(P) {
|
|
|
469
496
|
return sqrt3mod4;
|
|
470
497
|
let cc = _Fp.pow(Z, Q2);
|
|
471
498
|
const Q1div2 = (Q2 + _1n2) / _2n;
|
|
472
|
-
return function tonelliSlow(
|
|
473
|
-
const F2 =
|
|
499
|
+
return function tonelliSlow(Fp2, n) {
|
|
500
|
+
const F2 = Fp2;
|
|
474
501
|
if (F2.is0(n))
|
|
475
502
|
return n;
|
|
476
503
|
if (FpLegendre(F2, n) !== 1)
|
|
@@ -544,11 +571,11 @@ function validateField(field) {
|
|
|
544
571
|
throw new Error("invalid field: expected ORDER > 1, got " + field.ORDER);
|
|
545
572
|
return field;
|
|
546
573
|
}
|
|
547
|
-
function FpInvertBatch(
|
|
548
|
-
validateField(
|
|
574
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
575
|
+
validateField(Fp2);
|
|
549
576
|
aarray(nums, "nums");
|
|
550
577
|
abool2(passZero, "passZero");
|
|
551
|
-
const F2 =
|
|
578
|
+
const F2 = Fp2;
|
|
552
579
|
const inverted = new Array(nums.length).fill(passZero ? F2.ZERO : void 0);
|
|
553
580
|
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
554
581
|
if (F2.is0(num))
|
|
@@ -565,9 +592,9 @@ function FpInvertBatch(Fp, nums, passZero = false) {
|
|
|
565
592
|
}, invertedAcc);
|
|
566
593
|
return inverted;
|
|
567
594
|
}
|
|
568
|
-
function FpLegendre(
|
|
569
|
-
validateField(
|
|
570
|
-
const F2 =
|
|
595
|
+
function FpLegendre(Fp2, n) {
|
|
596
|
+
validateField(Fp2);
|
|
597
|
+
const F2 = Fp2;
|
|
571
598
|
aoddModulus(F2.ORDER, "FpLegendre");
|
|
572
599
|
const p1mod2 = (F2.ORDER - _1n2) / _2n;
|
|
573
600
|
const powered = F2.pow(n, p1mod2);
|
|
@@ -731,6 +758,29 @@ function Field(ORDER, opts2 = {}) {
|
|
|
731
758
|
Object.freeze(_Field.prototype);
|
|
732
759
|
return new _Field(ORDER, opts2);
|
|
733
760
|
}
|
|
761
|
+
function getFieldBytesLength(fieldOrder) {
|
|
762
|
+
if (typeof fieldOrder !== "bigint")
|
|
763
|
+
throw new Error("field order must be bigint");
|
|
764
|
+
if (fieldOrder <= _1n2)
|
|
765
|
+
throw new Error("field order must be greater than 1");
|
|
766
|
+
const bitLength = bitLen(fieldOrder - _1n2);
|
|
767
|
+
return Math.ceil(bitLength / 8);
|
|
768
|
+
}
|
|
769
|
+
function getMinHashLength(fieldOrder) {
|
|
770
|
+
const length = getFieldBytesLength(fieldOrder);
|
|
771
|
+
return length + Math.ceil(length / 2);
|
|
772
|
+
}
|
|
773
|
+
function mapHashToField(key, fieldOrder, isLE2 = false) {
|
|
774
|
+
abytes2(key);
|
|
775
|
+
const len = key.length;
|
|
776
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
777
|
+
const minLen = Math.max(getMinHashLength(fieldOrder), 16);
|
|
778
|
+
if (len < minLen || len > 1024)
|
|
779
|
+
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
780
|
+
const num = isLE2 ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
|
781
|
+
const reduced = mod(num, fieldOrder - _1n2) + _1n2;
|
|
782
|
+
return isLE2 ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
783
|
+
}
|
|
734
784
|
|
|
735
785
|
// ../../node_modules/@noble/curves/abstract/curve.js
|
|
736
786
|
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
@@ -1101,16 +1151,16 @@ function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
|
1101
1151
|
if (!(isPosBig(val) && val !== _0n3))
|
|
1102
1152
|
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
1103
1153
|
}
|
|
1104
|
-
const
|
|
1105
|
-
const
|
|
1154
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
1155
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
1106
1156
|
const _b = type === "weierstrass" ? "b" : "d";
|
|
1107
1157
|
const params = ["Gx", "Gy", "a", _b];
|
|
1108
1158
|
for (const p of params) {
|
|
1109
|
-
if (!
|
|
1159
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
1110
1160
|
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
1111
1161
|
}
|
|
1112
1162
|
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
1113
|
-
return { CURVE, Fp, Fn };
|
|
1163
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
1114
1164
|
}
|
|
1115
1165
|
function createKeygen(randomSecretKey, getPublicKey) {
|
|
1116
1166
|
return function keygen(seed) {
|
|
@@ -1125,42 +1175,42 @@ var _1n4 = /* @__PURE__ */ BigInt(1);
|
|
|
1125
1175
|
var _2n2 = /* @__PURE__ */ BigInt(2);
|
|
1126
1176
|
var _4n3 = /* @__PURE__ */ BigInt(4);
|
|
1127
1177
|
var _8n2 = /* @__PURE__ */ BigInt(8);
|
|
1128
|
-
function isEdValidXY(
|
|
1129
|
-
const x2 =
|
|
1130
|
-
const y2 =
|
|
1131
|
-
const left =
|
|
1132
|
-
const right =
|
|
1133
|
-
return
|
|
1178
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
1179
|
+
const x2 = Fp2.sqr(x);
|
|
1180
|
+
const y2 = Fp2.sqr(y);
|
|
1181
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
1182
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
1183
|
+
return Fp2.eql(left, right);
|
|
1134
1184
|
}
|
|
1135
1185
|
function edwards(params, extraOpts = {}) {
|
|
1136
1186
|
validateObject(extraOpts, {}, {}, "extraOpts");
|
|
1137
1187
|
const opts2 = extraOpts;
|
|
1138
1188
|
const validated = createCurveFields("edwards", params, opts2, opts2.FpFnLE);
|
|
1139
|
-
const { Fp, Fn } = validated;
|
|
1189
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
1140
1190
|
let CURVE = validated.CURVE;
|
|
1141
1191
|
const { h: cofactor } = CURVE;
|
|
1142
|
-
if (FpLegendre(
|
|
1192
|
+
if (FpLegendre(Fp2, CURVE.a) !== 1)
|
|
1143
1193
|
throw new Error("edwards: CURVE.a must be a square in Fp for complete addition formulas");
|
|
1144
|
-
if (FpLegendre(
|
|
1194
|
+
if (FpLegendre(Fp2, CURVE.d) !== -1)
|
|
1145
1195
|
throw new Error("edwards: CURVE.d must be a non-square in Fp for complete addition formulas");
|
|
1146
1196
|
validateObject(opts2, {}, { uvRatio: "function", randomBytes: "function" });
|
|
1147
1197
|
const randomBytes4 = opts2.randomBytes === void 0 ? randomBytes2 : opts2.randomBytes;
|
|
1148
|
-
const MASK = _2n2 << BigInt(
|
|
1198
|
+
const MASK = _2n2 << BigInt(Fp2.BYTES * 8) - _1n4;
|
|
1149
1199
|
function isOdd(n) {
|
|
1150
|
-
if (!
|
|
1200
|
+
if (!Fp2.isOdd)
|
|
1151
1201
|
throw new Error("Field does not have .isOdd()");
|
|
1152
|
-
return
|
|
1202
|
+
return Fp2.isOdd(n);
|
|
1153
1203
|
}
|
|
1154
1204
|
const uvRatio2 = opts2.uvRatio === void 0 ? (u, v) => {
|
|
1155
1205
|
try {
|
|
1156
|
-
return { isValid: true, value:
|
|
1206
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
1157
1207
|
} catch (e) {
|
|
1158
1208
|
return { isValid: false, value: _0n4 };
|
|
1159
1209
|
}
|
|
1160
1210
|
} : opts2.uvRatio;
|
|
1161
|
-
if (!isEdValidXY(
|
|
1211
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
1162
1212
|
throw new Error("bad curve params: generator point");
|
|
1163
|
-
const mulA =
|
|
1213
|
+
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);
|
|
1164
1214
|
function acoord(title, n, banZero = false) {
|
|
1165
1215
|
const min = banZero ? _1n4 : _0n4;
|
|
1166
1216
|
aInRange("coordinate " + title, n, min, MASK);
|
|
@@ -1196,11 +1246,11 @@ function edwards(params, extraOpts = {}) {
|
|
|
1196
1246
|
const { x, y } = p || {};
|
|
1197
1247
|
acoord("x", x);
|
|
1198
1248
|
acoord("y", y);
|
|
1199
|
-
return new _Point(x, y,
|
|
1249
|
+
return new _Point(x, y, Fp2.ONE, Fp2.mul(x, y));
|
|
1200
1250
|
}
|
|
1201
1251
|
// Uses algo from RFC8032 5.1.3.
|
|
1202
1252
|
static fromBytes(bytes, zip215 = false) {
|
|
1203
|
-
const len =
|
|
1253
|
+
const len = Fp2.BYTES;
|
|
1204
1254
|
const { a, d } = CURVE;
|
|
1205
1255
|
bytes = copyBytes(abytes2(bytes, len, "point"));
|
|
1206
1256
|
abool2(zip215, "zip215");
|
|
@@ -1208,20 +1258,20 @@ function edwards(params, extraOpts = {}) {
|
|
|
1208
1258
|
const lastByte = bytes[len - 1];
|
|
1209
1259
|
normed[len - 1] = lastByte & ~128;
|
|
1210
1260
|
const y = bytesToNumberLE(normed);
|
|
1211
|
-
const max = zip215 ? MASK :
|
|
1261
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
1212
1262
|
aInRange("point.y", y, _0n4, max);
|
|
1213
|
-
const y2 =
|
|
1214
|
-
const u =
|
|
1215
|
-
const v =
|
|
1263
|
+
const y2 = Fp2.sqr(y);
|
|
1264
|
+
const u = Fp2.sub(y2, Fp2.ONE);
|
|
1265
|
+
const v = Fp2.sub(Fp2.mulN(d, y2), a);
|
|
1216
1266
|
let { isValid, value: x } = uvRatio2(u, v);
|
|
1217
1267
|
if (!isValid)
|
|
1218
1268
|
throw new Error("bad point: invalid y coordinate");
|
|
1219
1269
|
const isXOdd = isOdd(x);
|
|
1220
1270
|
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
1221
|
-
if (!zip215 &&
|
|
1271
|
+
if (!zip215 && Fp2.is0(x) && isLastByteOdd)
|
|
1222
1272
|
throw new Error("bad point: x=0 and x_0=1");
|
|
1223
1273
|
if (isLastByteOdd !== isXOdd)
|
|
1224
|
-
x =
|
|
1274
|
+
x = Fp2.neg(x);
|
|
1225
1275
|
return _Point.fromAffine({ x, y });
|
|
1226
1276
|
}
|
|
1227
1277
|
static fromHex(hex, zip215 = false) {
|
|
@@ -1246,18 +1296,18 @@ function edwards(params, extraOpts = {}) {
|
|
|
1246
1296
|
if (p.is0())
|
|
1247
1297
|
throw new Error("bad point: ZERO");
|
|
1248
1298
|
const { X, Y, Z, T } = p;
|
|
1249
|
-
const X2 =
|
|
1250
|
-
const Y2 =
|
|
1251
|
-
const Z2 =
|
|
1252
|
-
const Z4 =
|
|
1253
|
-
const aX2 =
|
|
1254
|
-
const left =
|
|
1255
|
-
const right =
|
|
1256
|
-
if (!
|
|
1299
|
+
const X2 = Fp2.sqr(X);
|
|
1300
|
+
const Y2 = Fp2.sqr(Y);
|
|
1301
|
+
const Z2 = Fp2.sqr(Z);
|
|
1302
|
+
const Z4 = Fp2.sqr(Z2);
|
|
1303
|
+
const aX2 = Fp2.mul(X2, a);
|
|
1304
|
+
const left = Fp2.mul(Fp2.add(aX2, Y2), Z2);
|
|
1305
|
+
const right = Fp2.add(Z4, Fp2.mul(d, Fp2.mul(X2, Y2)));
|
|
1306
|
+
if (!Fp2.eql(left, right))
|
|
1257
1307
|
throw new Error("bad point: equation left != right (1)");
|
|
1258
|
-
const XY =
|
|
1259
|
-
const ZT =
|
|
1260
|
-
if (!
|
|
1308
|
+
const XY = Fp2.mul(X, Y);
|
|
1309
|
+
const ZT = Fp2.mul(Z, T);
|
|
1310
|
+
if (!Fp2.eql(XY, ZT))
|
|
1261
1311
|
throw new Error("bad point: equation left != right (2)");
|
|
1262
1312
|
}
|
|
1263
1313
|
// Compare one point to another.
|
|
@@ -1265,36 +1315,36 @@ function edwards(params, extraOpts = {}) {
|
|
|
1265
1315
|
aedpoint(other);
|
|
1266
1316
|
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1267
1317
|
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
1268
|
-
const X1Z2 =
|
|
1269
|
-
const X2Z1 =
|
|
1270
|
-
const Y1Z2 =
|
|
1271
|
-
const Y2Z1 =
|
|
1272
|
-
return
|
|
1318
|
+
const X1Z2 = Fp2.mul(X1, Z2);
|
|
1319
|
+
const X2Z1 = Fp2.mul(X2, Z1);
|
|
1320
|
+
const Y1Z2 = Fp2.mul(Y1, Z2);
|
|
1321
|
+
const Y2Z1 = Fp2.mul(Y2, Z1);
|
|
1322
|
+
return Fp2.eql(X1Z2, X2Z1) && Fp2.eql(Y1Z2, Y2Z1);
|
|
1273
1323
|
}
|
|
1274
1324
|
is0() {
|
|
1275
1325
|
return this.equals(_Point.ZERO);
|
|
1276
1326
|
}
|
|
1277
1327
|
negate() {
|
|
1278
|
-
return new _Point(
|
|
1328
|
+
return new _Point(Fp2.neg(this.X), this.Y, this.Z, Fp2.neg(this.T));
|
|
1279
1329
|
}
|
|
1280
1330
|
// Fast algo for doubling Extended Point.
|
|
1281
1331
|
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
1282
1332
|
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
1283
1333
|
double() {
|
|
1284
1334
|
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1285
|
-
const A =
|
|
1286
|
-
const B2 =
|
|
1287
|
-
const C =
|
|
1335
|
+
const A = Fp2.sqr(X1);
|
|
1336
|
+
const B2 = Fp2.sqr(Y1);
|
|
1337
|
+
const C = Fp2.mul(Fp2.sqr(Z1), _2n2);
|
|
1288
1338
|
const D = mulA(A);
|
|
1289
|
-
const x1y1 =
|
|
1290
|
-
const E =
|
|
1291
|
-
const G =
|
|
1292
|
-
const F2 =
|
|
1293
|
-
const H =
|
|
1294
|
-
const X3 =
|
|
1295
|
-
const Y3 =
|
|
1296
|
-
const T3 =
|
|
1297
|
-
const Z3 =
|
|
1339
|
+
const x1y1 = Fp2.addN(X1, Y1);
|
|
1340
|
+
const E = Fp2.sub(Fp2.subN(Fp2.sqr(x1y1), A), B2);
|
|
1341
|
+
const G = Fp2.addN(D, B2);
|
|
1342
|
+
const F2 = Fp2.subN(G, C);
|
|
1343
|
+
const H = Fp2.subN(D, B2);
|
|
1344
|
+
const X3 = Fp2.mul(E, F2);
|
|
1345
|
+
const Y3 = Fp2.mul(G, H);
|
|
1346
|
+
const T3 = Fp2.mul(E, H);
|
|
1347
|
+
const Z3 = Fp2.mul(F2, G);
|
|
1298
1348
|
return new _Point(X3, Y3, Z3, T3);
|
|
1299
1349
|
}
|
|
1300
1350
|
// Fast algo for adding 2 Extended Points.
|
|
@@ -1305,18 +1355,18 @@ function edwards(params, extraOpts = {}) {
|
|
|
1305
1355
|
const { d } = CURVE;
|
|
1306
1356
|
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
1307
1357
|
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
1308
|
-
const A =
|
|
1309
|
-
const B2 =
|
|
1310
|
-
const C =
|
|
1311
|
-
const D =
|
|
1312
|
-
const E =
|
|
1313
|
-
const F2 =
|
|
1314
|
-
const G =
|
|
1315
|
-
const H =
|
|
1316
|
-
const X3 =
|
|
1317
|
-
const Y3 =
|
|
1318
|
-
const T3 =
|
|
1319
|
-
const Z3 =
|
|
1358
|
+
const A = Fp2.mul(X1, X2);
|
|
1359
|
+
const B2 = Fp2.mul(Y1, Y2);
|
|
1360
|
+
const C = Fp2.mul(Fp2.mulN(T1, d), T2);
|
|
1361
|
+
const D = Fp2.mul(Z1, Z2);
|
|
1362
|
+
const E = Fp2.sub(Fp2.subN(Fp2.mulN(Fp2.addN(X1, Y1), Fp2.addN(X2, Y2)), A), B2);
|
|
1363
|
+
const F2 = Fp2.subN(D, C);
|
|
1364
|
+
const G = Fp2.addN(D, C);
|
|
1365
|
+
const H = Fp2.sub(B2, mulA(A));
|
|
1366
|
+
const X3 = Fp2.mul(E, F2);
|
|
1367
|
+
const Y3 = Fp2.mul(G, H);
|
|
1368
|
+
const T3 = Fp2.mul(E, H);
|
|
1369
|
+
const Z3 = Fp2.mul(F2, G);
|
|
1320
1370
|
return new _Point(X3, Y3, Z3, T3);
|
|
1321
1371
|
}
|
|
1322
1372
|
subtract(other) {
|
|
@@ -1325,7 +1375,7 @@ function edwards(params, extraOpts = {}) {
|
|
|
1325
1375
|
}
|
|
1326
1376
|
// Constant-time multiplication.
|
|
1327
1377
|
multiply(scalar) {
|
|
1328
|
-
if (!
|
|
1378
|
+
if (!Fn2.isValidNot0(scalar))
|
|
1329
1379
|
throw new RangeError("invalid scalar: expected 1 <= sc < curve.n");
|
|
1330
1380
|
const { p, f } = wnaf.mulSecret(this, scalar, cofactor, normalize);
|
|
1331
1381
|
return normalize([p, f])[0];
|
|
@@ -1336,7 +1386,7 @@ function edwards(params, extraOpts = {}) {
|
|
|
1336
1386
|
// Keeps the same subgroup-scalar contract: 0 is allowed for public-scalar callers, but
|
|
1337
1387
|
// n and larger values are rejected instead of being reduced mod n to the identity point.
|
|
1338
1388
|
multiplyUnsafe(scalar) {
|
|
1339
|
-
if (!
|
|
1389
|
+
if (!Fn2.isValid(scalar))
|
|
1340
1390
|
throw new RangeError("invalid scalar: expected 0 <= sc < curve.n");
|
|
1341
1391
|
if (scalar === _0n4)
|
|
1342
1392
|
return _Point.ZERO;
|
|
@@ -1366,13 +1416,13 @@ function edwards(params, extraOpts = {}) {
|
|
|
1366
1416
|
const { X, Y, Z } = p;
|
|
1367
1417
|
const is0 = p.is0();
|
|
1368
1418
|
if (iz == null)
|
|
1369
|
-
iz = is0 ?
|
|
1370
|
-
const x =
|
|
1371
|
-
const y =
|
|
1372
|
-
const zz =
|
|
1419
|
+
iz = is0 ? Fp2.create(_8n2) : Fp2.inv(Z);
|
|
1420
|
+
const x = Fp2.mul(X, iz);
|
|
1421
|
+
const y = Fp2.mul(Y, iz);
|
|
1422
|
+
const zz = Fp2.mul(Z, iz);
|
|
1373
1423
|
if (is0)
|
|
1374
|
-
return { x:
|
|
1375
|
-
if (!
|
|
1424
|
+
return { x: Fp2.ZERO, y: Fp2.ONE };
|
|
1425
|
+
if (!Fp2.eql(zz, Fp2.ONE))
|
|
1376
1426
|
throw new Error("invZ was invalid");
|
|
1377
1427
|
return { x, y };
|
|
1378
1428
|
}
|
|
@@ -1389,7 +1439,7 @@ function edwards(params, extraOpts = {}) {
|
|
|
1389
1439
|
}
|
|
1390
1440
|
toBytes() {
|
|
1391
1441
|
const { x, y } = this.toAffine();
|
|
1392
|
-
const bytes =
|
|
1442
|
+
const bytes = Fp2.toBytes(y);
|
|
1393
1443
|
bytes[bytes.length - 1] |= isOdd(x) ? 128 : 0;
|
|
1394
1444
|
return bytes;
|
|
1395
1445
|
}
|
|
@@ -1400,10 +1450,10 @@ function edwards(params, extraOpts = {}) {
|
|
|
1400
1450
|
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1401
1451
|
}
|
|
1402
1452
|
};
|
|
1403
|
-
__publicField(_Point, "BASE", new _Point(CURVE.Gx, CURVE.Gy,
|
|
1404
|
-
__publicField(_Point, "ZERO", new _Point(
|
|
1405
|
-
__publicField(_Point, "Fp",
|
|
1406
|
-
__publicField(_Point, "Fn",
|
|
1453
|
+
__publicField(_Point, "BASE", new _Point(CURVE.Gx, CURVE.Gy, Fp2.ONE, Fp2.mul(CURVE.Gx, CURVE.Gy)));
|
|
1454
|
+
__publicField(_Point, "ZERO", new _Point(Fp2.ZERO, Fp2.ONE, Fp2.ONE, Fp2.ZERO));
|
|
1455
|
+
__publicField(_Point, "Fp", Fp2);
|
|
1456
|
+
__publicField(_Point, "Fn", Fn2);
|
|
1407
1457
|
let Point = _Point;
|
|
1408
1458
|
const normalize = (points) => normalizeZ(Point, points);
|
|
1409
1459
|
const wnaf = new ScalarMultiplier(Point, randomBytes4);
|
|
@@ -1413,6 +1463,86 @@ function edwards(params, extraOpts = {}) {
|
|
|
1413
1463
|
Object.freeze(Point);
|
|
1414
1464
|
return Point;
|
|
1415
1465
|
}
|
|
1466
|
+
var PrimeEdwardsPoint = class {
|
|
1467
|
+
/**
|
|
1468
|
+
* Wrap one internal Edwards representative directly.
|
|
1469
|
+
* This is not a canonical encoding boundary: alternate Edwards
|
|
1470
|
+
* representatives may still describe the same abstract wrapper element.
|
|
1471
|
+
*/
|
|
1472
|
+
constructor(ep) {
|
|
1473
|
+
__publicField(this, "ep");
|
|
1474
|
+
this.ep = ep;
|
|
1475
|
+
}
|
|
1476
|
+
// Static methods that must be implemented by subclasses
|
|
1477
|
+
static fromBytes(_bytes) {
|
|
1478
|
+
notImplemented();
|
|
1479
|
+
}
|
|
1480
|
+
static fromHex(_hex) {
|
|
1481
|
+
notImplemented();
|
|
1482
|
+
}
|
|
1483
|
+
get x() {
|
|
1484
|
+
return this.toAffine().x;
|
|
1485
|
+
}
|
|
1486
|
+
get y() {
|
|
1487
|
+
return this.toAffine().y;
|
|
1488
|
+
}
|
|
1489
|
+
// Common implementations
|
|
1490
|
+
clearCofactor() {
|
|
1491
|
+
return this;
|
|
1492
|
+
}
|
|
1493
|
+
assertValidity() {
|
|
1494
|
+
this.ep.assertValidity();
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Return affine coordinates of the current internal Edwards representative.
|
|
1498
|
+
* This is a convenience helper, not a canonical Ristretto/Decaf encoding.
|
|
1499
|
+
* Equal abstract elements may expose different `x` / `y`; use
|
|
1500
|
+
* `toBytes()` / `fromBytes()` for canonical roundtrips.
|
|
1501
|
+
*/
|
|
1502
|
+
toAffine(invertedZ) {
|
|
1503
|
+
return this.ep.toAffine(invertedZ);
|
|
1504
|
+
}
|
|
1505
|
+
toHex() {
|
|
1506
|
+
return bytesToHex2(this.toBytes());
|
|
1507
|
+
}
|
|
1508
|
+
toString() {
|
|
1509
|
+
return this.toHex();
|
|
1510
|
+
}
|
|
1511
|
+
isTorsionFree() {
|
|
1512
|
+
return true;
|
|
1513
|
+
}
|
|
1514
|
+
isSmallOrder() {
|
|
1515
|
+
return false;
|
|
1516
|
+
}
|
|
1517
|
+
add(other) {
|
|
1518
|
+
this.assertSame(other);
|
|
1519
|
+
return this.init(this.ep.add(other.ep));
|
|
1520
|
+
}
|
|
1521
|
+
subtract(other) {
|
|
1522
|
+
this.assertSame(other);
|
|
1523
|
+
return this.init(this.ep.subtract(other.ep));
|
|
1524
|
+
}
|
|
1525
|
+
multiply(scalar) {
|
|
1526
|
+
return this.init(this.ep.multiply(scalar));
|
|
1527
|
+
}
|
|
1528
|
+
multiplyUnsafe(scalar) {
|
|
1529
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
1530
|
+
}
|
|
1531
|
+
double() {
|
|
1532
|
+
return this.init(this.ep.double());
|
|
1533
|
+
}
|
|
1534
|
+
negate() {
|
|
1535
|
+
return this.init(this.ep.negate());
|
|
1536
|
+
}
|
|
1537
|
+
precompute(windowSize, isLazy) {
|
|
1538
|
+
this.ep.precompute(windowSize, isLazy);
|
|
1539
|
+
return this;
|
|
1540
|
+
}
|
|
1541
|
+
};
|
|
1542
|
+
__publicField(PrimeEdwardsPoint, "BASE");
|
|
1543
|
+
__publicField(PrimeEdwardsPoint, "ZERO");
|
|
1544
|
+
__publicField(PrimeEdwardsPoint, "Fp");
|
|
1545
|
+
__publicField(PrimeEdwardsPoint, "Fn");
|
|
1416
1546
|
|
|
1417
1547
|
// ../../node_modules/@noble/curves/abstract/montgomery.js
|
|
1418
1548
|
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
@@ -1579,6 +1709,379 @@ function split(lst, le = false) {
|
|
|
1579
1709
|
}
|
|
1580
1710
|
return [Ah, Al];
|
|
1581
1711
|
}
|
|
1712
|
+
var fromNumH = (n) => n / 2 ** 32 | 0;
|
|
1713
|
+
var fromNumL = (n) => n >>> 0;
|
|
1714
|
+
function setU64FromNum(view, byteOffset, n, isLE2) {
|
|
1715
|
+
const h = fromNumH(n);
|
|
1716
|
+
const l = fromNumL(n);
|
|
1717
|
+
view.setUint32(byteOffset, isLE2 ? l : h, isLE2);
|
|
1718
|
+
view.setUint32(byteOffset + 4, isLE2 ? h : l, isLE2);
|
|
1719
|
+
}
|
|
1720
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
1721
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1722
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
1723
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1724
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
1725
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
1726
|
+
function add(Ah, Al, Bh, Bl) {
|
|
1727
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1728
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
1729
|
+
}
|
|
1730
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1731
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
1732
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1733
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
1734
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1735
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
1736
|
+
|
|
1737
|
+
// ../../node_modules/@noble/hashes/_md.js
|
|
1738
|
+
var HashMD = class {
|
|
1739
|
+
constructor(blockLen, outputLen, padOffset, isLE2) {
|
|
1740
|
+
__publicField(this, "blockLen");
|
|
1741
|
+
__publicField(this, "outputLen");
|
|
1742
|
+
__publicField(this, "canXOF", false);
|
|
1743
|
+
__publicField(this, "padOffset");
|
|
1744
|
+
__publicField(this, "isLE");
|
|
1745
|
+
// For partial updates less than block size
|
|
1746
|
+
__publicField(this, "buffer");
|
|
1747
|
+
__publicField(this, "view");
|
|
1748
|
+
__publicField(this, "finished", false);
|
|
1749
|
+
__publicField(this, "length", 0);
|
|
1750
|
+
__publicField(this, "pos", 0);
|
|
1751
|
+
__publicField(this, "destroyed", false);
|
|
1752
|
+
this.blockLen = blockLen;
|
|
1753
|
+
this.outputLen = outputLen;
|
|
1754
|
+
this.padOffset = padOffset;
|
|
1755
|
+
this.isLE = isLE2;
|
|
1756
|
+
this.buffer = new Uint8Array(blockLen);
|
|
1757
|
+
this.view = createView(this.buffer);
|
|
1758
|
+
}
|
|
1759
|
+
update(data) {
|
|
1760
|
+
aexists(this);
|
|
1761
|
+
abytes(data);
|
|
1762
|
+
const { view, buffer, blockLen } = this;
|
|
1763
|
+
const len = data.length;
|
|
1764
|
+
let processed = false;
|
|
1765
|
+
for (let pos = 0; pos < len; ) {
|
|
1766
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
1767
|
+
if (take === blockLen) {
|
|
1768
|
+
const dataView = createView(data);
|
|
1769
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
1770
|
+
this.process(dataView, pos);
|
|
1771
|
+
processed = true;
|
|
1772
|
+
continue;
|
|
1773
|
+
}
|
|
1774
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
1775
|
+
this.pos += take;
|
|
1776
|
+
pos += take;
|
|
1777
|
+
if (this.pos === blockLen) {
|
|
1778
|
+
this.process(view, 0);
|
|
1779
|
+
this.pos = 0;
|
|
1780
|
+
processed = true;
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
this.length += data.length;
|
|
1784
|
+
if (processed)
|
|
1785
|
+
this.roundClean();
|
|
1786
|
+
return this;
|
|
1787
|
+
}
|
|
1788
|
+
digestInto(out) {
|
|
1789
|
+
aexists(this);
|
|
1790
|
+
aoutput(out, this);
|
|
1791
|
+
this.finished = true;
|
|
1792
|
+
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
|
1793
|
+
let { pos } = this;
|
|
1794
|
+
buffer[pos++] = 128;
|
|
1795
|
+
buffer.fill(0, pos);
|
|
1796
|
+
if (this.padOffset > blockLen - pos) {
|
|
1797
|
+
this.process(view, 0);
|
|
1798
|
+
buffer.fill(0);
|
|
1799
|
+
}
|
|
1800
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE2);
|
|
1801
|
+
this.process(view, 0);
|
|
1802
|
+
this.roundClean();
|
|
1803
|
+
const oview = out === buffer ? view : createView(out);
|
|
1804
|
+
const len = this.outputLen;
|
|
1805
|
+
const outLen = len / 4;
|
|
1806
|
+
const state = this.get();
|
|
1807
|
+
if (len % 4 || outLen > state.length)
|
|
1808
|
+
throw new Error("invalid outputLen");
|
|
1809
|
+
for (let i = 0; i < outLen; i++)
|
|
1810
|
+
oview.setUint32(4 * i, state[i], isLE2);
|
|
1811
|
+
}
|
|
1812
|
+
digest() {
|
|
1813
|
+
const { buffer, outputLen } = this;
|
|
1814
|
+
this.digestInto(buffer);
|
|
1815
|
+
const res = buffer.slice(0, outputLen);
|
|
1816
|
+
this.destroy();
|
|
1817
|
+
return res;
|
|
1818
|
+
}
|
|
1819
|
+
_cloneIntoMeta(to) {
|
|
1820
|
+
const { buffer, length, finished, destroyed, pos } = this;
|
|
1821
|
+
to.destroyed = destroyed;
|
|
1822
|
+
to.finished = finished;
|
|
1823
|
+
to.length = length;
|
|
1824
|
+
to.pos = pos;
|
|
1825
|
+
if (pos)
|
|
1826
|
+
to.buffer.set(buffer);
|
|
1827
|
+
return to;
|
|
1828
|
+
}
|
|
1829
|
+
clone() {
|
|
1830
|
+
return this._cloneInto();
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
1834
|
+
1779033703,
|
|
1835
|
+
4089235720,
|
|
1836
|
+
3144134277,
|
|
1837
|
+
2227873595,
|
|
1838
|
+
1013904242,
|
|
1839
|
+
4271175723,
|
|
1840
|
+
2773480762,
|
|
1841
|
+
1595750129,
|
|
1842
|
+
1359893119,
|
|
1843
|
+
2917565137,
|
|
1844
|
+
2600822924,
|
|
1845
|
+
725511199,
|
|
1846
|
+
528734635,
|
|
1847
|
+
4215389547,
|
|
1848
|
+
1541459225,
|
|
1849
|
+
327033209
|
|
1850
|
+
]);
|
|
1851
|
+
|
|
1852
|
+
// ../../node_modules/@noble/hashes/sha2.js
|
|
1853
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
1854
|
+
"0x428a2f98d728ae22",
|
|
1855
|
+
"0x7137449123ef65cd",
|
|
1856
|
+
"0xb5c0fbcfec4d3b2f",
|
|
1857
|
+
"0xe9b5dba58189dbbc",
|
|
1858
|
+
"0x3956c25bf348b538",
|
|
1859
|
+
"0x59f111f1b605d019",
|
|
1860
|
+
"0x923f82a4af194f9b",
|
|
1861
|
+
"0xab1c5ed5da6d8118",
|
|
1862
|
+
"0xd807aa98a3030242",
|
|
1863
|
+
"0x12835b0145706fbe",
|
|
1864
|
+
"0x243185be4ee4b28c",
|
|
1865
|
+
"0x550c7dc3d5ffb4e2",
|
|
1866
|
+
"0x72be5d74f27b896f",
|
|
1867
|
+
"0x80deb1fe3b1696b1",
|
|
1868
|
+
"0x9bdc06a725c71235",
|
|
1869
|
+
"0xc19bf174cf692694",
|
|
1870
|
+
"0xe49b69c19ef14ad2",
|
|
1871
|
+
"0xefbe4786384f25e3",
|
|
1872
|
+
"0x0fc19dc68b8cd5b5",
|
|
1873
|
+
"0x240ca1cc77ac9c65",
|
|
1874
|
+
"0x2de92c6f592b0275",
|
|
1875
|
+
"0x4a7484aa6ea6e483",
|
|
1876
|
+
"0x5cb0a9dcbd41fbd4",
|
|
1877
|
+
"0x76f988da831153b5",
|
|
1878
|
+
"0x983e5152ee66dfab",
|
|
1879
|
+
"0xa831c66d2db43210",
|
|
1880
|
+
"0xb00327c898fb213f",
|
|
1881
|
+
"0xbf597fc7beef0ee4",
|
|
1882
|
+
"0xc6e00bf33da88fc2",
|
|
1883
|
+
"0xd5a79147930aa725",
|
|
1884
|
+
"0x06ca6351e003826f",
|
|
1885
|
+
"0x142929670a0e6e70",
|
|
1886
|
+
"0x27b70a8546d22ffc",
|
|
1887
|
+
"0x2e1b21385c26c926",
|
|
1888
|
+
"0x4d2c6dfc5ac42aed",
|
|
1889
|
+
"0x53380d139d95b3df",
|
|
1890
|
+
"0x650a73548baf63de",
|
|
1891
|
+
"0x766a0abb3c77b2a8",
|
|
1892
|
+
"0x81c2c92e47edaee6",
|
|
1893
|
+
"0x92722c851482353b",
|
|
1894
|
+
"0xa2bfe8a14cf10364",
|
|
1895
|
+
"0xa81a664bbc423001",
|
|
1896
|
+
"0xc24b8b70d0f89791",
|
|
1897
|
+
"0xc76c51a30654be30",
|
|
1898
|
+
"0xd192e819d6ef5218",
|
|
1899
|
+
"0xd69906245565a910",
|
|
1900
|
+
"0xf40e35855771202a",
|
|
1901
|
+
"0x106aa07032bbd1b8",
|
|
1902
|
+
"0x19a4c116b8d2d0c8",
|
|
1903
|
+
"0x1e376c085141ab53",
|
|
1904
|
+
"0x2748774cdf8eeb99",
|
|
1905
|
+
"0x34b0bcb5e19b48a8",
|
|
1906
|
+
"0x391c0cb3c5c95a63",
|
|
1907
|
+
"0x4ed8aa4ae3418acb",
|
|
1908
|
+
"0x5b9cca4f7763e373",
|
|
1909
|
+
"0x682e6ff3d6b2b8a3",
|
|
1910
|
+
"0x748f82ee5defb2fc",
|
|
1911
|
+
"0x78a5636f43172f60",
|
|
1912
|
+
"0x84c87814a1f0ab72",
|
|
1913
|
+
"0x8cc702081a6439ec",
|
|
1914
|
+
"0x90befffa23631e28",
|
|
1915
|
+
"0xa4506cebde82bde9",
|
|
1916
|
+
"0xbef9a3f7b2c67915",
|
|
1917
|
+
"0xc67178f2e372532b",
|
|
1918
|
+
"0xca273eceea26619c",
|
|
1919
|
+
"0xd186b8c721c0c207",
|
|
1920
|
+
"0xeada7dd6cde0eb1e",
|
|
1921
|
+
"0xf57d4f7fee6ed178",
|
|
1922
|
+
"0x06f067aa72176fba",
|
|
1923
|
+
"0x0a637dc5a2c898a6",
|
|
1924
|
+
"0x113f9804bef90dae",
|
|
1925
|
+
"0x1b710b35131c471b",
|
|
1926
|
+
"0x28db77f523047d84",
|
|
1927
|
+
"0x32caab7b40c72493",
|
|
1928
|
+
"0x3c9ebe0a15c9bebc",
|
|
1929
|
+
"0x431d67c49c100d4c",
|
|
1930
|
+
"0x4cc5d4becb3e42b6",
|
|
1931
|
+
"0x597f299cfc657e2a",
|
|
1932
|
+
"0x5fcb6fab3ad6faec",
|
|
1933
|
+
"0x6c44198c4a475817"
|
|
1934
|
+
].map((n) => BigInt(n))))();
|
|
1935
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
1936
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
1937
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1938
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1939
|
+
var SHA2_64B = class extends HashMD {
|
|
1940
|
+
constructor(outputLen, IV) {
|
|
1941
|
+
super(128, outputLen, 16, false);
|
|
1942
|
+
// We cannot use array here since array allows indexing by variable
|
|
1943
|
+
// which means optimizer/compiler cannot use registers.
|
|
1944
|
+
// h -- high 32 bits, l -- low 32 bits
|
|
1945
|
+
// Numeric initializers matter: starting the fields as `undefined` changes
|
|
1946
|
+
// V8's field representation and slows hashing down (measured on sha256).
|
|
1947
|
+
__publicField(this, "Ah", 0);
|
|
1948
|
+
__publicField(this, "Al", 0);
|
|
1949
|
+
__publicField(this, "Bh", 0);
|
|
1950
|
+
__publicField(this, "Bl", 0);
|
|
1951
|
+
__publicField(this, "Ch", 0);
|
|
1952
|
+
__publicField(this, "Cl", 0);
|
|
1953
|
+
__publicField(this, "Dh", 0);
|
|
1954
|
+
__publicField(this, "Dl", 0);
|
|
1955
|
+
__publicField(this, "Eh", 0);
|
|
1956
|
+
__publicField(this, "El", 0);
|
|
1957
|
+
__publicField(this, "Fh", 0);
|
|
1958
|
+
__publicField(this, "Fl", 0);
|
|
1959
|
+
__publicField(this, "Gh", 0);
|
|
1960
|
+
__publicField(this, "Gl", 0);
|
|
1961
|
+
__publicField(this, "Hh", 0);
|
|
1962
|
+
__publicField(this, "Hl", 0);
|
|
1963
|
+
this.Ah = IV[0] | 0;
|
|
1964
|
+
this.Al = IV[1] | 0;
|
|
1965
|
+
this.Bh = IV[2] | 0;
|
|
1966
|
+
this.Bl = IV[3] | 0;
|
|
1967
|
+
this.Ch = IV[4] | 0;
|
|
1968
|
+
this.Cl = IV[5] | 0;
|
|
1969
|
+
this.Dh = IV[6] | 0;
|
|
1970
|
+
this.Dl = IV[7] | 0;
|
|
1971
|
+
this.Eh = IV[8] | 0;
|
|
1972
|
+
this.El = IV[9] | 0;
|
|
1973
|
+
this.Fh = IV[10] | 0;
|
|
1974
|
+
this.Fl = IV[11] | 0;
|
|
1975
|
+
this.Gh = IV[12] | 0;
|
|
1976
|
+
this.Gl = IV[13] | 0;
|
|
1977
|
+
this.Hh = IV[14] | 0;
|
|
1978
|
+
this.Hl = IV[15] | 0;
|
|
1979
|
+
}
|
|
1980
|
+
// prettier-ignore
|
|
1981
|
+
get() {
|
|
1982
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1983
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
1984
|
+
}
|
|
1985
|
+
// prettier-ignore
|
|
1986
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
1987
|
+
this.Ah = Ah | 0;
|
|
1988
|
+
this.Al = Al | 0;
|
|
1989
|
+
this.Bh = Bh | 0;
|
|
1990
|
+
this.Bl = Bl | 0;
|
|
1991
|
+
this.Ch = Ch | 0;
|
|
1992
|
+
this.Cl = Cl | 0;
|
|
1993
|
+
this.Dh = Dh | 0;
|
|
1994
|
+
this.Dl = Dl | 0;
|
|
1995
|
+
this.Eh = Eh | 0;
|
|
1996
|
+
this.El = El | 0;
|
|
1997
|
+
this.Fh = Fh | 0;
|
|
1998
|
+
this.Fl = Fl | 0;
|
|
1999
|
+
this.Gh = Gh | 0;
|
|
2000
|
+
this.Gl = Gl | 0;
|
|
2001
|
+
this.Hh = Hh | 0;
|
|
2002
|
+
this.Hl = Hl | 0;
|
|
2003
|
+
}
|
|
2004
|
+
_cloneInto(to) {
|
|
2005
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
2006
|
+
return this._cloneIntoMeta(to);
|
|
2007
|
+
}
|
|
2008
|
+
process(view, offset) {
|
|
2009
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
2010
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
2011
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
2012
|
+
}
|
|
2013
|
+
for (let i = 16; i < 80; i++) {
|
|
2014
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
2015
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
2016
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
2017
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
2018
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
2019
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
2020
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
2021
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
2022
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
2023
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
2024
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
2025
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
2026
|
+
}
|
|
2027
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
2028
|
+
for (let i = 0; i < 80; i++) {
|
|
2029
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
2030
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
2031
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
2032
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
2033
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
2034
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
2035
|
+
const T1l = T1ll | 0;
|
|
2036
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
2037
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
2038
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
2039
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
2040
|
+
Hh = Gh | 0;
|
|
2041
|
+
Hl = Gl | 0;
|
|
2042
|
+
Gh = Fh | 0;
|
|
2043
|
+
Gl = Fl | 0;
|
|
2044
|
+
Fh = Eh | 0;
|
|
2045
|
+
Fl = El | 0;
|
|
2046
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
2047
|
+
Dh = Ch | 0;
|
|
2048
|
+
Dl = Cl | 0;
|
|
2049
|
+
Ch = Bh | 0;
|
|
2050
|
+
Cl = Bl | 0;
|
|
2051
|
+
Bh = Ah | 0;
|
|
2052
|
+
Bl = Al | 0;
|
|
2053
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
2054
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
2055
|
+
Al = All | 0;
|
|
2056
|
+
}
|
|
2057
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
2058
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
2059
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
2060
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
2061
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
2062
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
2063
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
2064
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
2065
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
2066
|
+
}
|
|
2067
|
+
roundClean() {
|
|
2068
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
2069
|
+
}
|
|
2070
|
+
destroy() {
|
|
2071
|
+
this.destroyed = true;
|
|
2072
|
+
clean(this.buffer);
|
|
2073
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
2074
|
+
}
|
|
2075
|
+
};
|
|
2076
|
+
var _SHA512 = class extends SHA2_64B {
|
|
2077
|
+
constructor() {
|
|
2078
|
+
super(64, SHA512_IV);
|
|
2079
|
+
}
|
|
2080
|
+
};
|
|
2081
|
+
var sha512 = /* @__PURE__ */ createHasher(
|
|
2082
|
+
() => new _SHA512(),
|
|
2083
|
+
/* @__PURE__ */ oidNist(3)
|
|
2084
|
+
);
|
|
1582
2085
|
|
|
1583
2086
|
// ../../node_modules/@noble/curves/abstract/fft.js
|
|
1584
2087
|
function checkU32(n, title = "n") {
|
|
@@ -1676,6 +2179,334 @@ var FFTCore = (F2, coreOpts) => {
|
|
|
1676
2179
|
};
|
|
1677
2180
|
};
|
|
1678
2181
|
|
|
2182
|
+
// ../../node_modules/@noble/curves/abstract/hash-to-curve.js
|
|
2183
|
+
function i2osp(value, length) {
|
|
2184
|
+
asafenumber(value);
|
|
2185
|
+
asafenumber(length);
|
|
2186
|
+
if (length < 0 || length > 4)
|
|
2187
|
+
throw new Error("invalid I2OSP length: " + length);
|
|
2188
|
+
if (value < 0 || value > 2 ** (8 * length) - 1)
|
|
2189
|
+
throw new Error("invalid I2OSP input: " + value);
|
|
2190
|
+
const res = Array.from({ length }).fill(0);
|
|
2191
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
2192
|
+
res[i] = value & 255;
|
|
2193
|
+
value >>>= 8;
|
|
2194
|
+
}
|
|
2195
|
+
return new Uint8Array(res);
|
|
2196
|
+
}
|
|
2197
|
+
function strxor(a, b) {
|
|
2198
|
+
const arr = new Uint8Array(a.length);
|
|
2199
|
+
for (let i = 0; i < a.length; i++) {
|
|
2200
|
+
arr[i] = a[i] ^ b[i];
|
|
2201
|
+
}
|
|
2202
|
+
return arr;
|
|
2203
|
+
}
|
|
2204
|
+
function normDST(DST) {
|
|
2205
|
+
if (!isBytes2(DST) && typeof DST !== "string")
|
|
2206
|
+
throw new Error("DST must be Uint8Array or ascii string");
|
|
2207
|
+
const dst = typeof DST === "string" ? asciiToBytes(DST) : DST;
|
|
2208
|
+
if (dst.length === 0)
|
|
2209
|
+
throw new Error("DST must be non-empty");
|
|
2210
|
+
return dst;
|
|
2211
|
+
}
|
|
2212
|
+
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
2213
|
+
abytes2(msg);
|
|
2214
|
+
asafenumber(lenInBytes);
|
|
2215
|
+
if (typeof H !== "function")
|
|
2216
|
+
throw new Error("expand_message_xmd: expected hash function");
|
|
2217
|
+
asafenumber(H.outputLen, "hash.outputLen");
|
|
2218
|
+
asafenumber(H.blockLen, "hash.blockLen");
|
|
2219
|
+
DST = normDST(DST);
|
|
2220
|
+
if (DST.length > 255)
|
|
2221
|
+
DST = H(concatBytes2(asciiToBytes("H2C-OVERSIZE-DST-"), DST));
|
|
2222
|
+
const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
|
|
2223
|
+
const ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
2224
|
+
if (lenInBytes > 65535 || ell > 255)
|
|
2225
|
+
throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
2226
|
+
const DST_prime = concatBytes2(DST, i2osp(DST.length, 1));
|
|
2227
|
+
const Z_pad = new Uint8Array(r_in_bytes);
|
|
2228
|
+
const l_i_b_str = i2osp(lenInBytes, 2);
|
|
2229
|
+
const b = new Array(ell);
|
|
2230
|
+
const b_0 = H(concatBytes2(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
|
|
2231
|
+
b[0] = H(concatBytes2(b_0, i2osp(1, 1), DST_prime));
|
|
2232
|
+
for (let i = 1; i < ell; i++) {
|
|
2233
|
+
const args = [strxor(b_0, b[i - 1]), i2osp(i + 1, 1), DST_prime];
|
|
2234
|
+
b[i] = H(concatBytes2(...args));
|
|
2235
|
+
}
|
|
2236
|
+
const pseudo_random_bytes = concatBytes2(...b);
|
|
2237
|
+
return pseudo_random_bytes.slice(0, lenInBytes);
|
|
2238
|
+
}
|
|
2239
|
+
var _DST_scalar = "HashToScalar-";
|
|
2240
|
+
|
|
2241
|
+
// ../../node_modules/@noble/curves/abstract/oprf.js
|
|
2242
|
+
var _DST_scalarBytes = /* @__PURE__ */ asciiToBytes(_DST_scalar);
|
|
2243
|
+
function createOPRF(opts2) {
|
|
2244
|
+
validateObject(opts2, {
|
|
2245
|
+
name: "string",
|
|
2246
|
+
hash: "function",
|
|
2247
|
+
hashToScalar: "function",
|
|
2248
|
+
hashToGroup: "function"
|
|
2249
|
+
});
|
|
2250
|
+
validatePointCons(opts2.Point);
|
|
2251
|
+
const { name, Point, hash, hashToGroup: hashToGroupHook, hashToScalar } = opts2;
|
|
2252
|
+
const { Fn: Fn2 } = Point;
|
|
2253
|
+
const invertSecret = (value) => invertCt(value, Fn2.ORDER);
|
|
2254
|
+
const hashToGroup = (msg, ctx) => hashToGroupHook(msg, {
|
|
2255
|
+
DST: concatBytes2(asciiToBytes("HashToGroup-"), ctx)
|
|
2256
|
+
});
|
|
2257
|
+
const hashToScalarPrefixed = (msg, ctx) => hashToScalar(msg, { DST: concatBytes2(_DST_scalarBytes, ctx) });
|
|
2258
|
+
const randomScalar = (rng = randomBytes2) => {
|
|
2259
|
+
if (typeof rng !== "function")
|
|
2260
|
+
throw new TypeError('"rng" expected function, got type=' + typeof rng);
|
|
2261
|
+
const t = mapHashToField(rng(getMinHashLength(Fn2.ORDER)), Fn2.ORDER, Fn2.isLE);
|
|
2262
|
+
return Fn2.isLE ? bytesToNumberLE(t) : bytesToNumberBE(t);
|
|
2263
|
+
};
|
|
2264
|
+
const msm = (points, scalars) => mulAddUnsafe(Point, points, scalars);
|
|
2265
|
+
const getCtx = (mode) => concatBytes2(asciiToBytes("OPRFV1-"), new Uint8Array([mode]), asciiToBytes("-" + name));
|
|
2266
|
+
const ctxOPRF = getCtx(0);
|
|
2267
|
+
const ctxVOPRF = getCtx(1);
|
|
2268
|
+
const ctxPOPRF = getCtx(2);
|
|
2269
|
+
function encode(...args) {
|
|
2270
|
+
const res2 = [];
|
|
2271
|
+
for (const a of args) {
|
|
2272
|
+
if (typeof a === "number")
|
|
2273
|
+
res2.push(numberToBytesBE(a, 2));
|
|
2274
|
+
else if (typeof a === "string")
|
|
2275
|
+
res2.push(asciiToBytes(a));
|
|
2276
|
+
else {
|
|
2277
|
+
abytes2(a);
|
|
2278
|
+
res2.push(numberToBytesBE(a.length, 2), a);
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
2281
|
+
return concatBytes2(...res2);
|
|
2282
|
+
}
|
|
2283
|
+
const inputBytes = (title, bytes) => {
|
|
2284
|
+
abytes2(bytes, void 0, title);
|
|
2285
|
+
if (bytes.length > 65535)
|
|
2286
|
+
throw new Error(`"${title}" expected Uint8Array of length <= 65535, got length=${bytes.length}`);
|
|
2287
|
+
return bytes;
|
|
2288
|
+
};
|
|
2289
|
+
const hashInput = (...bytes) => hash(encode(...bytes, "Finalize"));
|
|
2290
|
+
function getTranscripts(B2, C, D, ctx) {
|
|
2291
|
+
const Bm = B2.toBytes();
|
|
2292
|
+
const seed = hash(encode(Bm, concatBytes2(asciiToBytes("Seed-"), ctx)));
|
|
2293
|
+
const res2 = [];
|
|
2294
|
+
for (let i = 0; i < C.length; i++) {
|
|
2295
|
+
const Ci = C[i].toBytes();
|
|
2296
|
+
const Di = D[i].toBytes();
|
|
2297
|
+
const di = hashToScalarPrefixed(encode(seed, i, Ci, Di, "Composite"), ctx);
|
|
2298
|
+
res2.push(di);
|
|
2299
|
+
}
|
|
2300
|
+
return res2;
|
|
2301
|
+
}
|
|
2302
|
+
function computeComposites(B2, C, D, ctx) {
|
|
2303
|
+
const T = getTranscripts(B2, C, D, ctx);
|
|
2304
|
+
const M = msm(C, T);
|
|
2305
|
+
const Z = msm(D, T);
|
|
2306
|
+
return { M, Z };
|
|
2307
|
+
}
|
|
2308
|
+
function computeCompositesFast(k, B2, C, D, ctx) {
|
|
2309
|
+
const T = getTranscripts(B2, C, D, ctx);
|
|
2310
|
+
const M = msm(C, T);
|
|
2311
|
+
const Z = M.multiply(k);
|
|
2312
|
+
return { M, Z };
|
|
2313
|
+
}
|
|
2314
|
+
function challengeTranscript(B2, M, Z, t2, t3, ctx) {
|
|
2315
|
+
const [Bm, a0, a1, a2, a3] = [B2, M, Z, t2, t3].map((i) => i.toBytes());
|
|
2316
|
+
return hashToScalarPrefixed(encode(Bm, a0, a1, a2, a3, "Challenge"), ctx);
|
|
2317
|
+
}
|
|
2318
|
+
function generateProof(ctx, k, B2, C, D, rng) {
|
|
2319
|
+
const { M, Z } = computeCompositesFast(k, B2, C, D, ctx);
|
|
2320
|
+
const r = randomScalar(rng);
|
|
2321
|
+
const t2 = Point.BASE.multiply(r);
|
|
2322
|
+
const t3 = M.multiply(r);
|
|
2323
|
+
const c = challengeTranscript(B2, M, Z, t2, t3, ctx);
|
|
2324
|
+
const s = Fn2.sub(r, Fn2.mul(c, k));
|
|
2325
|
+
return concatBytes2(...[c, s].map((i) => Fn2.toBytes(i)));
|
|
2326
|
+
}
|
|
2327
|
+
function verifyProof(ctx, B2, C, D, proof) {
|
|
2328
|
+
abytes2(proof, 2 * Fn2.BYTES);
|
|
2329
|
+
const { M, Z } = computeComposites(B2, C, D, ctx);
|
|
2330
|
+
const [c, s] = [proof.subarray(0, Fn2.BYTES), proof.subarray(Fn2.BYTES)].map((f) => Fn2.fromBytes(f));
|
|
2331
|
+
const t2 = msm([Point.BASE, B2], [s, c]);
|
|
2332
|
+
const t3 = msm([M, Z], [s, c]);
|
|
2333
|
+
const expectedC = challengeTranscript(B2, M, Z, t2, t3, ctx);
|
|
2334
|
+
if (!Fn2.eql(c, expectedC))
|
|
2335
|
+
throw new Error("proof verification failed");
|
|
2336
|
+
}
|
|
2337
|
+
function generateKeyPair() {
|
|
2338
|
+
const skS = randomScalar();
|
|
2339
|
+
const pkS = Point.BASE.multiply(skS);
|
|
2340
|
+
return { secretKey: Fn2.toBytes(skS), publicKey: pkS.toBytes() };
|
|
2341
|
+
}
|
|
2342
|
+
function deriveKeyPair(ctx, seed, info) {
|
|
2343
|
+
abytes2(seed, 32, "seed");
|
|
2344
|
+
info = inputBytes("keyInfo", info);
|
|
2345
|
+
const dst = concatBytes2(asciiToBytes("DeriveKeyPair"), ctx);
|
|
2346
|
+
const msg = concatBytes2(seed, encode(info), Uint8Array.of(0));
|
|
2347
|
+
for (let counter = 0; counter <= 255; counter++) {
|
|
2348
|
+
msg[msg.length - 1] = counter;
|
|
2349
|
+
const skS = hashToScalar(msg, { DST: dst });
|
|
2350
|
+
if (Fn2.is0(skS))
|
|
2351
|
+
continue;
|
|
2352
|
+
return {
|
|
2353
|
+
secretKey: Fn2.toBytes(skS),
|
|
2354
|
+
publicKey: Point.BASE.multiply(skS).toBytes()
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
throw new Error("Cannot derive key");
|
|
2358
|
+
}
|
|
2359
|
+
const wirePoint = (label, bytes) => {
|
|
2360
|
+
const point = Point.fromBytes(bytes);
|
|
2361
|
+
if (point.equals(Point.ZERO))
|
|
2362
|
+
throw new Error(label + " point at infinity");
|
|
2363
|
+
return point;
|
|
2364
|
+
};
|
|
2365
|
+
function blind(ctx, input, rng = randomBytes2) {
|
|
2366
|
+
input = inputBytes("input", input);
|
|
2367
|
+
const blind2 = randomScalar(rng);
|
|
2368
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
2369
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2370
|
+
throw new Error("Input point at infinity");
|
|
2371
|
+
const blinded = inputPoint.multiply(blind2);
|
|
2372
|
+
return { blind: Fn2.toBytes(blind2), blinded: blinded.toBytes() };
|
|
2373
|
+
}
|
|
2374
|
+
function evaluate(ctx, secretKey, input) {
|
|
2375
|
+
input = inputBytes("input", input);
|
|
2376
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2377
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
2378
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2379
|
+
throw new Error("Input point at infinity");
|
|
2380
|
+
const unblinded = inputPoint.multiply(skS).toBytes();
|
|
2381
|
+
return hashInput(input, unblinded);
|
|
2382
|
+
}
|
|
2383
|
+
const oprf2 = Object.freeze({
|
|
2384
|
+
generateKeyPair,
|
|
2385
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxOPRF, seed, keyInfo),
|
|
2386
|
+
blind: (input, rng = randomBytes2) => blind(ctxOPRF, input, rng),
|
|
2387
|
+
blindEvaluate(secretKey, blindedPoint) {
|
|
2388
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2389
|
+
const elm = wirePoint("blinded", blindedPoint);
|
|
2390
|
+
return elm.multiply(skS).toBytes();
|
|
2391
|
+
},
|
|
2392
|
+
finalize(input, blindBytes, evaluatedBytes) {
|
|
2393
|
+
input = inputBytes("input", input);
|
|
2394
|
+
const blind2 = Fn2.fromBytes(blindBytes);
|
|
2395
|
+
const evalPoint = wirePoint("evaluated", evaluatedBytes);
|
|
2396
|
+
const unblinded = evalPoint.multiply(Fn2.inv(blind2)).toBytes();
|
|
2397
|
+
return hashInput(input, unblinded);
|
|
2398
|
+
},
|
|
2399
|
+
evaluate: (secretKey, input) => evaluate(ctxOPRF, secretKey, input)
|
|
2400
|
+
});
|
|
2401
|
+
const voprf = Object.freeze({
|
|
2402
|
+
generateKeyPair,
|
|
2403
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxVOPRF, seed, keyInfo),
|
|
2404
|
+
blind: (input, rng = randomBytes2) => blind(ctxVOPRF, input, rng),
|
|
2405
|
+
blindEvaluateBatch(secretKey, publicKey, blinded, rng = randomBytes2) {
|
|
2406
|
+
if (!Array.isArray(blinded))
|
|
2407
|
+
throw new Error("expected array");
|
|
2408
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2409
|
+
const pkS = wirePoint("public key", publicKey);
|
|
2410
|
+
const blindedPoints = blinded.map((i) => wirePoint("blinded", i));
|
|
2411
|
+
const evaluated = blindedPoints.map((i) => i.multiply(skS));
|
|
2412
|
+
const proof = generateProof(ctxVOPRF, skS, pkS, blindedPoints, evaluated, rng);
|
|
2413
|
+
return { evaluated: evaluated.map((i) => i.toBytes()), proof };
|
|
2414
|
+
},
|
|
2415
|
+
blindEvaluate(secretKey, publicKey, blinded, rng = randomBytes2) {
|
|
2416
|
+
const res2 = this.blindEvaluateBatch(secretKey, publicKey, [blinded], rng);
|
|
2417
|
+
return { evaluated: res2.evaluated[0], proof: res2.proof };
|
|
2418
|
+
},
|
|
2419
|
+
finalizeBatch(items, publicKey, proof) {
|
|
2420
|
+
if (!Array.isArray(items))
|
|
2421
|
+
throw new Error("expected array");
|
|
2422
|
+
const pkS = wirePoint("public key", publicKey);
|
|
2423
|
+
const blindedPoints = items.map((i) => wirePoint("blinded", i.blinded));
|
|
2424
|
+
const evalPoints = items.map((i) => wirePoint("evaluated", i.evaluated));
|
|
2425
|
+
verifyProof(ctxVOPRF, pkS, blindedPoints, evalPoints, proof);
|
|
2426
|
+
return items.map((i, j) => {
|
|
2427
|
+
const input = inputBytes("input", i.input);
|
|
2428
|
+
const blind2 = Fn2.fromBytes(i.blind);
|
|
2429
|
+
const unblinded = evalPoints[j].multiply(Fn2.inv(blind2)).toBytes();
|
|
2430
|
+
return hashInput(input, unblinded);
|
|
2431
|
+
});
|
|
2432
|
+
},
|
|
2433
|
+
finalize(input, blind2, evaluated, blinded, publicKey, proof) {
|
|
2434
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], publicKey, proof)[0];
|
|
2435
|
+
},
|
|
2436
|
+
evaluate: (secretKey, input) => evaluate(ctxVOPRF, secretKey, input)
|
|
2437
|
+
});
|
|
2438
|
+
const poprf = (info) => {
|
|
2439
|
+
info = copyBytes(inputBytes("info", info));
|
|
2440
|
+
const m = hashToScalarPrefixed(encode("Info", info), ctxPOPRF);
|
|
2441
|
+
const T = Point.BASE.multiply(m);
|
|
2442
|
+
return Object.freeze({
|
|
2443
|
+
generateKeyPair,
|
|
2444
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxPOPRF, seed, keyInfo),
|
|
2445
|
+
blind(input, publicKey, rng = randomBytes2) {
|
|
2446
|
+
input = inputBytes("input", input);
|
|
2447
|
+
const pkS = wirePoint("public key", publicKey);
|
|
2448
|
+
const tweakedKey = T.add(pkS);
|
|
2449
|
+
if (tweakedKey.equals(Point.ZERO))
|
|
2450
|
+
throw new Error("tweakedKey point at infinity");
|
|
2451
|
+
const blind2 = randomScalar(rng);
|
|
2452
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
2453
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2454
|
+
throw new Error("Input point at infinity");
|
|
2455
|
+
const blindedPoint = inputPoint.multiply(blind2);
|
|
2456
|
+
return {
|
|
2457
|
+
blind: Fn2.toBytes(blind2),
|
|
2458
|
+
blinded: blindedPoint.toBytes(),
|
|
2459
|
+
tweakedKey: tweakedKey.toBytes()
|
|
2460
|
+
};
|
|
2461
|
+
},
|
|
2462
|
+
blindEvaluateBatch(secretKey, blinded, rng = randomBytes2) {
|
|
2463
|
+
if (!Array.isArray(blinded))
|
|
2464
|
+
throw new Error("expected array");
|
|
2465
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2466
|
+
const t = Fn2.add(skS, m);
|
|
2467
|
+
const invT = invertSecret(t);
|
|
2468
|
+
const blindedPoints = blinded.map((i) => wirePoint("blinded", i));
|
|
2469
|
+
const evalPoints = blindedPoints.map((i) => i.multiply(invT));
|
|
2470
|
+
const tweakedKey = Point.BASE.multiply(t);
|
|
2471
|
+
const proof = generateProof(ctxPOPRF, t, tweakedKey, evalPoints, blindedPoints, rng);
|
|
2472
|
+
return { evaluated: evalPoints.map((i) => i.toBytes()), proof };
|
|
2473
|
+
},
|
|
2474
|
+
blindEvaluate(secretKey, blinded, rng = randomBytes2) {
|
|
2475
|
+
const res2 = this.blindEvaluateBatch(secretKey, [blinded], rng);
|
|
2476
|
+
return { evaluated: res2.evaluated[0], proof: res2.proof };
|
|
2477
|
+
},
|
|
2478
|
+
finalizeBatch(items, proof, tweakedKey) {
|
|
2479
|
+
if (!Array.isArray(items))
|
|
2480
|
+
throw new Error("expected array");
|
|
2481
|
+
const inputs = items.map((i) => inputBytes("input", i.input));
|
|
2482
|
+
const evalPoints = items.map((i) => wirePoint("evaluated", i.evaluated));
|
|
2483
|
+
verifyProof(ctxPOPRF, wirePoint("tweakedKey", tweakedKey), evalPoints, items.map((i) => wirePoint("blinded", i.blinded)), proof);
|
|
2484
|
+
return items.map((i, j) => {
|
|
2485
|
+
const blind2 = Fn2.fromBytes(i.blind);
|
|
2486
|
+
const point = evalPoints[j].multiply(Fn2.inv(blind2)).toBytes();
|
|
2487
|
+
return hashInput(inputs[j], info, point);
|
|
2488
|
+
});
|
|
2489
|
+
},
|
|
2490
|
+
finalize(input, blind2, evaluated, blinded, proof, tweakedKey) {
|
|
2491
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], proof, tweakedKey)[0];
|
|
2492
|
+
},
|
|
2493
|
+
evaluate(secretKey, input) {
|
|
2494
|
+
input = inputBytes("input", input);
|
|
2495
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
2496
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
2497
|
+
if (inputPoint.equals(Point.ZERO))
|
|
2498
|
+
throw new Error("Input point at infinity");
|
|
2499
|
+
const t = Fn2.add(skS, m);
|
|
2500
|
+
const invT = invertSecret(t);
|
|
2501
|
+
const unblinded = inputPoint.multiply(invT).toBytes();
|
|
2502
|
+
return hashInput(input, info, unblinded);
|
|
2503
|
+
}
|
|
2504
|
+
});
|
|
2505
|
+
};
|
|
2506
|
+
const res = { name, oprf: oprf2, voprf, poprf, __tests: Object.freeze({ Fn: Fn2, invertSecret }) };
|
|
2507
|
+
return Object.freeze(res);
|
|
2508
|
+
}
|
|
2509
|
+
|
|
1679
2510
|
// ../../node_modules/@noble/curves/ed25519.js
|
|
1680
2511
|
var _0n6 = /* @__PURE__ */ BigInt(0);
|
|
1681
2512
|
var _1n6 = /* @__PURE__ */ BigInt(1);
|
|
@@ -1738,6 +2569,8 @@ function uvRatio(u, v) {
|
|
|
1738
2569
|
return { isValid: useRoot1 || useRoot2, value: x };
|
|
1739
2570
|
}
|
|
1740
2571
|
var ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
|
|
2572
|
+
var Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
2573
|
+
var Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
|
|
1741
2574
|
var x25519 = /* @__PURE__ */ (() => {
|
|
1742
2575
|
const P = ed25519_CURVE_p;
|
|
1743
2576
|
const powPminus2 = (x) => {
|
|
@@ -1761,6 +2594,196 @@ var x25519 = /* @__PURE__ */ (() => {
|
|
|
1761
2594
|
}
|
|
1762
2595
|
});
|
|
1763
2596
|
})();
|
|
2597
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
2598
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
2599
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
2600
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
2601
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
2602
|
+
var invertSqrt = (number) => uvRatio(_1n6, number);
|
|
2603
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
2604
|
+
var bytes255ToNumberLE = (bytes) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
2605
|
+
function calcElligatorRistrettoMap(r0) {
|
|
2606
|
+
const { d } = ed25519_CURVE;
|
|
2607
|
+
const r = Fp.mul(Fp.mulN(SQRT_M1, r0), r0);
|
|
2608
|
+
const Ns = Fp.mul(Fp.addN(r, _1n6), ONE_MINUS_D_SQ);
|
|
2609
|
+
let c = BigInt(-1);
|
|
2610
|
+
const D = Fp.mul(Fp.subN(c, Fp.mulN(d, r)), Fp.add(r, d));
|
|
2611
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
2612
|
+
let s_ = Fp.mul(s, r0);
|
|
2613
|
+
if (!Fp.isOdd(s_))
|
|
2614
|
+
s_ = Fp.neg(s_);
|
|
2615
|
+
if (!Ns_D_is_sq)
|
|
2616
|
+
s = s_;
|
|
2617
|
+
if (!Ns_D_is_sq)
|
|
2618
|
+
c = r;
|
|
2619
|
+
const Nt = Fp.sub(Fp.mulN(Fp.mulN(c, Fp.subN(r, _1n6)), D_MINUS_ONE_SQ), D);
|
|
2620
|
+
const s2 = Fp.sqrN(s);
|
|
2621
|
+
const W0 = Fp.mul(Fp.addN(s, s), D);
|
|
2622
|
+
const W1 = Fp.mul(Nt, SQRT_AD_MINUS_ONE);
|
|
2623
|
+
const W2 = Fp.sub(_1n6, s2);
|
|
2624
|
+
const W3 = Fp.add(_1n6, s2);
|
|
2625
|
+
return new ed25519_Point(Fp.mul(W0, W3), Fp.mul(W2, W1), Fp.mul(W1, W3), Fp.mul(W0, W2));
|
|
2626
|
+
}
|
|
2627
|
+
var __RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
2628
|
+
constructor(ep) {
|
|
2629
|
+
super(ep);
|
|
2630
|
+
}
|
|
2631
|
+
/**
|
|
2632
|
+
* Create one Ristretto255 point from affine Edwards coordinates.
|
|
2633
|
+
* This wraps the internal Edwards representative directly and is not a
|
|
2634
|
+
* canonical ristretto255 decoding path.
|
|
2635
|
+
* Use `toBytes()` / `fromBytes()` if canonical ristretto255 bytes matter.
|
|
2636
|
+
*/
|
|
2637
|
+
static fromAffine(ap) {
|
|
2638
|
+
return new __RistrettoPoint(ed25519_Point.fromAffine(ap));
|
|
2639
|
+
}
|
|
2640
|
+
assertSame(other) {
|
|
2641
|
+
if (!(other instanceof __RistrettoPoint))
|
|
2642
|
+
throw new Error("RistrettoPoint expected");
|
|
2643
|
+
}
|
|
2644
|
+
init(ep) {
|
|
2645
|
+
return new __RistrettoPoint(ep);
|
|
2646
|
+
}
|
|
2647
|
+
static fromBytes(bytes) {
|
|
2648
|
+
abytes(bytes, 32);
|
|
2649
|
+
const { a, d } = ed25519_CURVE;
|
|
2650
|
+
const s = bytes255ToNumberLE(bytes);
|
|
2651
|
+
if (!equalBytes(Fp.toBytes(s), bytes) || Fp.isOdd(s))
|
|
2652
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
2653
|
+
const s2 = Fp.sqr(s);
|
|
2654
|
+
const u1 = Fp.add(_1n6, Fp.mulN(a, s2));
|
|
2655
|
+
const u2 = Fp.sub(_1n6, Fp.mulN(a, s2));
|
|
2656
|
+
const u1_2 = Fp.sqr(u1);
|
|
2657
|
+
const u2_2 = Fp.sqr(u2);
|
|
2658
|
+
const v = Fp.sub(Fp.mulN(Fp.mulN(a, d), u1_2), u2_2);
|
|
2659
|
+
const { isValid, value: I } = invertSqrt(Fp.mul(v, u2_2));
|
|
2660
|
+
const Dx = Fp.mul(I, u2);
|
|
2661
|
+
const Dy = Fp.mul(Fp.mulN(I, Dx), v);
|
|
2662
|
+
let x = Fp.mul(Fp.addN(s, s), Dx);
|
|
2663
|
+
if (Fp.isOdd(x))
|
|
2664
|
+
x = Fp.neg(x);
|
|
2665
|
+
const y = Fp.mul(u1, Dy);
|
|
2666
|
+
const t = Fp.mul(x, y);
|
|
2667
|
+
if (!isValid || Fp.isOdd(t) || Fp.is0(y))
|
|
2668
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
2669
|
+
return new __RistrettoPoint(new ed25519_Point(x, y, Fp.ONE, t));
|
|
2670
|
+
}
|
|
2671
|
+
/**
|
|
2672
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
2673
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
2674
|
+
* @param hex - Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
2675
|
+
*/
|
|
2676
|
+
static fromHex(hex) {
|
|
2677
|
+
return __RistrettoPoint.fromBytes(hexToBytes(hex));
|
|
2678
|
+
}
|
|
2679
|
+
/**
|
|
2680
|
+
* Encodes ristretto point to Uint8Array.
|
|
2681
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
2682
|
+
*/
|
|
2683
|
+
toBytes() {
|
|
2684
|
+
let { X, Y, Z, T } = this.ep;
|
|
2685
|
+
const u1 = Fp.mul(Fp.add(Z, Y), Fp.sub(Z, Y));
|
|
2686
|
+
const u2 = Fp.mul(X, Y);
|
|
2687
|
+
const u2sq = Fp.sqr(u2);
|
|
2688
|
+
const { value: invsqrt } = invertSqrt(Fp.mul(u1, u2sq));
|
|
2689
|
+
const D1 = Fp.mul(invsqrt, u1);
|
|
2690
|
+
const D2 = Fp.mul(invsqrt, u2);
|
|
2691
|
+
const zInv = Fp.mul(Fp.mulN(D1, D2), T);
|
|
2692
|
+
let D;
|
|
2693
|
+
if (Fp.isOdd(Fp.mul(T, zInv))) {
|
|
2694
|
+
let _x = Fp.mul(Y, SQRT_M1);
|
|
2695
|
+
let _y = Fp.mul(X, SQRT_M1);
|
|
2696
|
+
X = _x;
|
|
2697
|
+
Y = _y;
|
|
2698
|
+
D = Fp.mul(D1, INVSQRT_A_MINUS_D);
|
|
2699
|
+
} else {
|
|
2700
|
+
D = D2;
|
|
2701
|
+
}
|
|
2702
|
+
if (Fp.isOdd(Fp.mul(X, zInv)))
|
|
2703
|
+
Y = Fp.neg(Y);
|
|
2704
|
+
let s = Fp.mul(Fp.subN(Z, Y), D);
|
|
2705
|
+
if (Fp.isOdd(s))
|
|
2706
|
+
s = Fp.neg(s);
|
|
2707
|
+
return Fp.toBytes(s);
|
|
2708
|
+
}
|
|
2709
|
+
/**
|
|
2710
|
+
* Compares two Ristretto points.
|
|
2711
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
2712
|
+
*/
|
|
2713
|
+
equals(other) {
|
|
2714
|
+
this.assertSame(other);
|
|
2715
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
2716
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
2717
|
+
const one = Fp.eql(Fp.mul(X1, Y2), Fp.mul(Y1, X2));
|
|
2718
|
+
const two = Fp.eql(Fp.mul(Y1, Y2), Fp.mul(X1, X2));
|
|
2719
|
+
return one || two;
|
|
2720
|
+
}
|
|
2721
|
+
is0() {
|
|
2722
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
2723
|
+
}
|
|
2724
|
+
};
|
|
2725
|
+
// Do NOT change syntax: the following gymnastics is done,
|
|
2726
|
+
// because typescript strips comments, which makes bundlers disable tree-shaking.
|
|
2727
|
+
// prettier-ignore
|
|
2728
|
+
__publicField(__RistrettoPoint, "BASE", /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.BASE))());
|
|
2729
|
+
// prettier-ignore
|
|
2730
|
+
__publicField(__RistrettoPoint, "ZERO", /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.ZERO))());
|
|
2731
|
+
// prettier-ignore
|
|
2732
|
+
__publicField(__RistrettoPoint, "Fp", /* @__PURE__ */ (() => Fp)());
|
|
2733
|
+
// prettier-ignore
|
|
2734
|
+
__publicField(__RistrettoPoint, "Fn", /* @__PURE__ */ (() => Fn)());
|
|
2735
|
+
var _RistrettoPoint = __RistrettoPoint;
|
|
2736
|
+
var ristretto255_hasher = /* @__PURE__ */ Object.freeze({
|
|
2737
|
+
Point: _RistrettoPoint,
|
|
2738
|
+
/**
|
|
2739
|
+
* Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
|
|
2740
|
+
* * There are no test vectors
|
|
2741
|
+
* * encodeToCurve / mapToCurve is undefined
|
|
2742
|
+
* * mapToCurve would be `calcElligatorRistrettoMap(scalars[0])`, not ristretto255_map!
|
|
2743
|
+
* * hashToScalar is undefined too, so we just use OPRF implementation
|
|
2744
|
+
* * We cannot re-use 'createHasher', because ristretto255_map is different algorithm/RFC
|
|
2745
|
+
(os2ip -> bytes255ToNumberLE)
|
|
2746
|
+
* * mapToCurve == calcElligatorRistrettoMap, hashToCurve == ristretto255_map
|
|
2747
|
+
* * hashToScalar is undefined in RFC9380 for ristretto, so we use the OPRF
|
|
2748
|
+
version here. Using `bytes255ToNumblerLE` will create a different result
|
|
2749
|
+
if we use `bytes255ToNumberLE` as os2ip
|
|
2750
|
+
* * current version is closest to spec.
|
|
2751
|
+
*/
|
|
2752
|
+
hashToCurve(msg, options) {
|
|
2753
|
+
const DST = options?.DST === void 0 ? "ristretto255_XMD:SHA-512_R255MAP_RO_" : options.DST;
|
|
2754
|
+
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
2755
|
+
return ristretto255_hasher.deriveToCurve(xmd);
|
|
2756
|
+
},
|
|
2757
|
+
hashToScalar(msg, options) {
|
|
2758
|
+
const DST = options?.DST === void 0 ? _DST_scalar : options.DST;
|
|
2759
|
+
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
2760
|
+
return Fn.create(bytesToNumberLE(xmd));
|
|
2761
|
+
},
|
|
2762
|
+
/**
|
|
2763
|
+
* HashToCurve-like construction based on RFC 9496 (Element Derivation).
|
|
2764
|
+
* Converts 64 uniform random bytes into a curve point.
|
|
2765
|
+
*
|
|
2766
|
+
* WARNING: This represents an older hash-to-curve construction from before
|
|
2767
|
+
* RFC 9380 was finalized.
|
|
2768
|
+
* It was later reused as a component in the newer
|
|
2769
|
+
* `hash_to_ristretto255` function defined in RFC 9380.
|
|
2770
|
+
*/
|
|
2771
|
+
deriveToCurve(bytes) {
|
|
2772
|
+
abytes(bytes, 64);
|
|
2773
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
2774
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
2775
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
2776
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
2777
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
2778
|
+
}
|
|
2779
|
+
});
|
|
2780
|
+
var ristretto255_oprf = /* @__PURE__ */ (() => createOPRF({
|
|
2781
|
+
name: "ristretto255-SHA512",
|
|
2782
|
+
Point: _RistrettoPoint,
|
|
2783
|
+
hash: sha512,
|
|
2784
|
+
hashToGroup: ristretto255_hasher.hashToCurve,
|
|
2785
|
+
hashToScalar: ristretto255_hasher.hashToScalar
|
|
2786
|
+
}))();
|
|
1764
2787
|
|
|
1765
2788
|
// ../../node_modules/@noble/hashes/sha3.js
|
|
1766
2789
|
var _0n7 = BigInt(0);
|
|
@@ -2018,7 +3041,7 @@ function aobject3(value, title = "object") {
|
|
|
2018
3041
|
throw new TypeError(title === "object" ? "expected valid options object" : `"${title}" expected object, got type=${typeof value}`);
|
|
2019
3042
|
return value;
|
|
2020
3043
|
}
|
|
2021
|
-
function
|
|
3044
|
+
function equalBytes2(a, b) {
|
|
2022
3045
|
a = abytes(a);
|
|
2023
3046
|
b = abytes(b);
|
|
2024
3047
|
if (a.length !== b.length)
|
|
@@ -2474,7 +3497,7 @@ function createKyber(opts2) {
|
|
|
2474
3497
|
const validateModulus = (publicKey, fn) => {
|
|
2475
3498
|
const eke = publicKey.subarray(0, 384 * rawOpts.K);
|
|
2476
3499
|
const ek = KPKESecretCoder.encode(KPKESecretCoder.decode(copyBytes2(eke)));
|
|
2477
|
-
const ok =
|
|
3500
|
+
const ok = equalBytes2(ek, eke);
|
|
2478
3501
|
cleanBytes(ek);
|
|
2479
3502
|
if (!ok)
|
|
2480
3503
|
throw new Error(`ML-KEM.${fn}: wrong publicKey modulus`);
|
|
@@ -2545,14 +3568,14 @@ function createKyber(opts2) {
|
|
|
2545
3568
|
const k768 = secretCoder.bytesLen - 96;
|
|
2546
3569
|
const start = k768 + 32;
|
|
2547
3570
|
const test = HASH256(secretKey.subarray(k768 / 2, start));
|
|
2548
|
-
if (!
|
|
3571
|
+
if (!equalBytes2(test, secretKey.subarray(start, start + 32)))
|
|
2549
3572
|
throw new Error("invalid secretKey: hash check failed");
|
|
2550
3573
|
const [sk, publicKey, publicKeyHash, z] = secretCoder.decode(secretKey);
|
|
2551
3574
|
const msg = KPKE.decrypt(cipherText, sk);
|
|
2552
3575
|
const kr = HASH512.create().update(msg).update(publicKeyHash).digest();
|
|
2553
3576
|
const Khat = kr.subarray(0, 32);
|
|
2554
3577
|
const cipherText2 = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
|
|
2555
|
-
const isValid =
|
|
3578
|
+
const isValid = equalBytes2(cipherText, cipherText2);
|
|
2556
3579
|
const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
|
|
2557
3580
|
cleanBytes(msg, cipherText2, kr.subarray(32), !isValid ? Khat : Kbar);
|
|
2558
3581
|
return isValid ? Khat : Kbar;
|
|
@@ -2593,13 +3616,13 @@ function createKyber(opts2) {
|
|
|
2593
3616
|
abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
|
|
2594
3617
|
abytesDoc(cipherText, lengths.cipherText, "cipherText");
|
|
2595
3618
|
const [sk, ekEmbedded, storedHash, z] = secretCoder.decode(secretKey);
|
|
2596
|
-
if (!
|
|
3619
|
+
if (!equalBytes2(ekEmbedded, ek) || !equalBytes2(storedHash, publicKeyHash))
|
|
2597
3620
|
throw new Error("ML-KEM.decapsulate: secretKey does not match prepared publicKey");
|
|
2598
3621
|
const msg = KPKE.decrypt(cipherText, sk);
|
|
2599
3622
|
const kr = HASH512.create().update(msg).update(publicKeyHash).digest();
|
|
2600
3623
|
const Khat = kr.subarray(0, 32);
|
|
2601
3624
|
const cipherText2 = cached.encrypt(msg, kr.subarray(32, 64));
|
|
2602
|
-
const isValid =
|
|
3625
|
+
const isValid = equalBytes2(cipherText, cipherText2);
|
|
2603
3626
|
const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
|
|
2604
3627
|
cleanBytes(msg, cipherText2, kr.subarray(32), !isValid ? Khat : Kbar);
|
|
2605
3628
|
return isValid ? Khat : Kbar;
|
|
@@ -2647,11 +3670,11 @@ function ecKeygen(curve, allowZeroKey = false) {
|
|
|
2647
3670
|
if (!("getSharedSecret" in curve && "sign" in curve && "verify" in curve))
|
|
2648
3671
|
throw new Error("allowZeroKey requires a Weierstrass curve");
|
|
2649
3672
|
const wCurve = curve;
|
|
2650
|
-
const
|
|
3673
|
+
const Fn2 = wCurve.Point.Fn;
|
|
2651
3674
|
keygen = (seed = randomBytes3(lengths.seed)) => {
|
|
2652
3675
|
abytes(seed, lengths.seed, "seed");
|
|
2653
|
-
const seedScalar =
|
|
2654
|
-
const secretKey =
|
|
3676
|
+
const seedScalar = Fn2.isLE ? bytesToNumberLE(seed) : bytesToNumberBE(seed);
|
|
3677
|
+
const secretKey = Fn2.toBytes(Fn2.create(seedScalar));
|
|
2655
3678
|
return {
|
|
2656
3679
|
secretKey,
|
|
2657
3680
|
publicKey: curve.getPublicKey(secretKey)
|
|
@@ -2827,9 +3850,9 @@ function combineKEMS(realSeedLen, realMsgLen, expandSeed, combiner, ...kems) {
|
|
|
2827
3850
|
const cipherText = [];
|
|
2828
3851
|
try {
|
|
2829
3852
|
for (let i = 0; i < rawKems.length; i++) {
|
|
2830
|
-
const
|
|
2831
|
-
sharedSecret.push(
|
|
2832
|
-
cipherText.push(
|
|
3853
|
+
const enc2 = rawKems[i].encapsulate(pks[i], rand[i]);
|
|
3854
|
+
sharedSecret.push(enc2.sharedSecret);
|
|
3855
|
+
cipherText.push(enc2.cipherText);
|
|
2833
3856
|
}
|
|
2834
3857
|
const encodedCipherText = ctCoder.encode(cipherText);
|
|
2835
3858
|
return {
|
|
@@ -2937,6 +3960,69 @@ function isBlockedIPv4(host) {
|
|
|
2937
3960
|
if (a === 192 && b === 168) return true;
|
|
2938
3961
|
if (a === 169 && b === 254) return true;
|
|
2939
3962
|
if (a === 100 && b >= 64 && b <= 127) return true;
|
|
3963
|
+
const c = octets[2];
|
|
3964
|
+
if (a === 192 && b === 0 && c === 0) return true;
|
|
3965
|
+
if (a === 198 && (b === 18 || b === 19)) return true;
|
|
3966
|
+
if (a >= 224 && a <= 239) return true;
|
|
3967
|
+
if (a >= 240) return true;
|
|
3968
|
+
return false;
|
|
3969
|
+
}
|
|
3970
|
+
function expandIPv6(clean2) {
|
|
3971
|
+
let text = clean2;
|
|
3972
|
+
const tail = [];
|
|
3973
|
+
const dotted = /^(.*:)(\d+\.\d+\.\d+\.\d+)$/.exec(text);
|
|
3974
|
+
if (dotted) {
|
|
3975
|
+
const parts = dotted[2].split(".").map((x) => Number.parseInt(x, 10));
|
|
3976
|
+
if (parts.some((x) => !Number.isInteger(x) || x > 255)) return null;
|
|
3977
|
+
tail.push(parts[0] << 8 | parts[1], parts[2] << 8 | parts[3]);
|
|
3978
|
+
text = dotted[1].endsWith("::") ? dotted[1] : dotted[1].slice(0, -1);
|
|
3979
|
+
}
|
|
3980
|
+
const halves = text.split("::");
|
|
3981
|
+
if (halves.length > 2) return null;
|
|
3982
|
+
const parse = (s) => {
|
|
3983
|
+
if (s === "") return [];
|
|
3984
|
+
const out = [];
|
|
3985
|
+
for (const g of s.split(":")) {
|
|
3986
|
+
if (!/^[0-9a-f]{1,4}$/.test(g)) return null;
|
|
3987
|
+
out.push(Number.parseInt(g, 16));
|
|
3988
|
+
}
|
|
3989
|
+
return out;
|
|
3990
|
+
};
|
|
3991
|
+
const head = parse(halves[0]);
|
|
3992
|
+
const rest = halves.length === 2 ? parse(halves[1]) : [];
|
|
3993
|
+
if (!head || !rest) return null;
|
|
3994
|
+
const known = head.length + rest.length + tail.length;
|
|
3995
|
+
if (halves.length === 1) {
|
|
3996
|
+
return known === 8 ? [...head, ...tail] : null;
|
|
3997
|
+
}
|
|
3998
|
+
if (known > 7) return null;
|
|
3999
|
+
return [...head, ...new Array(8 - known).fill(0), ...rest, ...tail];
|
|
4000
|
+
}
|
|
4001
|
+
function v4FromGroups(high, low) {
|
|
4002
|
+
return `${high >> 8 & 255}.${high & 255}.${low >> 8 & 255}.${low & 255}`;
|
|
4003
|
+
}
|
|
4004
|
+
function isBlockedIPv6Groups(g) {
|
|
4005
|
+
const first = g[0];
|
|
4006
|
+
if ((first & 65280) === 65280) return true;
|
|
4007
|
+
if ((first & 65472) === 65152) return true;
|
|
4008
|
+
if ((first & 65472) === 65216) return true;
|
|
4009
|
+
if ((first & 65024) === 64512) return true;
|
|
4010
|
+
if (first === 8194) return isBlockedIPv4(v4FromGroups(g[1], g[2]));
|
|
4011
|
+
if (first === 100 && g[1] === 65435) {
|
|
4012
|
+
if (g[2] === 0 && g[3] === 0 && g[4] === 0 && g[5] === 0) return isBlockedIPv4(v4FromGroups(g[6], g[7]));
|
|
4013
|
+
if (g[2] === 1) return isBlockedIPv4(v4FromGroups(g[6], g[7]));
|
|
4014
|
+
return true;
|
|
4015
|
+
}
|
|
4016
|
+
if (g[0] === 0 && g[1] === 0 && g[2] === 0 && g[3] === 0 && g[4] === 0) {
|
|
4017
|
+
if (g[5] === 65535 || g[5] === 0) {
|
|
4018
|
+
if (g[5] === 0 && g[6] === 0 && g[7] <= 1) return true;
|
|
4019
|
+
return isBlockedIPv4(v4FromGroups(g[6], g[7]));
|
|
4020
|
+
}
|
|
4021
|
+
return true;
|
|
4022
|
+
}
|
|
4023
|
+
if (g[0] === 0 && g[1] === 0 && g[2] === 0 && g[3] === 0 && g[4] === 65535 && g[5] === 0) {
|
|
4024
|
+
return isBlockedIPv4(v4FromGroups(g[6], g[7]));
|
|
4025
|
+
}
|
|
2940
4026
|
return false;
|
|
2941
4027
|
}
|
|
2942
4028
|
function v4FromHexPair(highHex, lowHex) {
|
|
@@ -2956,6 +4042,9 @@ function isBlockedIPv6(host) {
|
|
|
2956
4042
|
}
|
|
2957
4043
|
clean2 = clean2.split("%")[0];
|
|
2958
4044
|
if (!clean2.includes(":")) return false;
|
|
4045
|
+
const groups = expandIPv6(clean2);
|
|
4046
|
+
if (!groups) return true;
|
|
4047
|
+
if (isBlockedIPv6Groups(groups)) return true;
|
|
2959
4048
|
if (clean2 === "::1") return true;
|
|
2960
4049
|
if (clean2 === "::") return true;
|
|
2961
4050
|
if (clean2.startsWith("fe80:")) return true;
|
|
@@ -3304,12 +4393,26 @@ function hexToBytes4(hex) {
|
|
|
3304
4393
|
}
|
|
3305
4394
|
return out;
|
|
3306
4395
|
}
|
|
4396
|
+
function mapsAreCanonical(doc) {
|
|
4397
|
+
const canonical = (s) => typeof s === "string" && s === s.toLowerCase();
|
|
4398
|
+
for (const [partnerId, keyId] of Object.entries(doc.heads)) {
|
|
4399
|
+
if (!canonical(partnerId) || !canonical(keyId)) return false;
|
|
4400
|
+
}
|
|
4401
|
+
if (doc.staged === void 0) return true;
|
|
4402
|
+
if (!doc.staged || typeof doc.staged !== "object" || Array.isArray(doc.staged)) return false;
|
|
4403
|
+
for (const [partnerId, entry] of Object.entries(doc.staged)) {
|
|
4404
|
+
if (!canonical(partnerId) || !entry || typeof entry !== "object") return false;
|
|
4405
|
+
if (!canonical(entry.key_id) || !canonical(entry.key_sha256)) return false;
|
|
4406
|
+
}
|
|
4407
|
+
return true;
|
|
4408
|
+
}
|
|
3307
4409
|
async function verifyRegistryHead(doc, roots, now = /* @__PURE__ */ new Date()) {
|
|
3308
4410
|
try {
|
|
3309
4411
|
if (!doc || !KNOWN_VERSIONS.has(doc.version)) return "malformed";
|
|
3310
4412
|
if (doc.algorithm !== REGISTRY_HEAD_ALG) return "malformed";
|
|
3311
4413
|
if (typeof doc.signature !== "string" || !/^[0-9a-f]{128}$/i.test(doc.signature)) return "malformed";
|
|
3312
4414
|
if (!doc.heads || typeof doc.heads !== "object") return "malformed";
|
|
4415
|
+
if (!mapsAreCanonical(doc)) return "malformed";
|
|
3313
4416
|
const expires = Date.parse(doc.expires_at);
|
|
3314
4417
|
const issued = Date.parse(doc.issued_at);
|
|
3315
4418
|
if (Number.isNaN(expires) || Number.isNaN(issued)) return "malformed";
|
|
@@ -3665,6 +4768,47 @@ async function openSealedWithSenderHeldKey(share, envelope, partnerId, store) {
|
|
|
3665
4768
|
}
|
|
3666
4769
|
}
|
|
3667
4770
|
|
|
4771
|
+
// ../crypto/src/vault-pin-hardening.ts
|
|
4772
|
+
var HARDENED_PREFIX = "h1:";
|
|
4773
|
+
var PIN_HARDENING_VERSION = 1;
|
|
4774
|
+
var oprf = ristretto255_oprf.oprf;
|
|
4775
|
+
var enc = new TextEncoder();
|
|
4776
|
+
function toB642(bytes) {
|
|
4777
|
+
let s = "";
|
|
4778
|
+
for (const b of bytes) s += String.fromCharCode(b);
|
|
4779
|
+
return btoa(s);
|
|
4780
|
+
}
|
|
4781
|
+
function fromB64(b64) {
|
|
4782
|
+
const s = atob(b64);
|
|
4783
|
+
const out = new Uint8Array(s.length);
|
|
4784
|
+
for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i);
|
|
4785
|
+
return out;
|
|
4786
|
+
}
|
|
4787
|
+
function isHardenedSecret(value) {
|
|
4788
|
+
return value.startsWith(HARDENED_PREFIX);
|
|
4789
|
+
}
|
|
4790
|
+
async function hardenPin(pin, evaluate) {
|
|
4791
|
+
if (isHardenedSecret(pin)) throw new Error("hardenPin was given an already-hardened secret");
|
|
4792
|
+
const input = enc.encode(pin);
|
|
4793
|
+
const { blind, blinded } = oprf.blind(input);
|
|
4794
|
+
const evaluated = fromB64(await evaluate(toB642(blinded)));
|
|
4795
|
+
const output = oprf.finalize(input, blind, evaluated);
|
|
4796
|
+
return HARDENED_PREFIX + toB642(output);
|
|
4797
|
+
}
|
|
4798
|
+
function parseOprfSeed(hex) {
|
|
4799
|
+
if (!hex) return null;
|
|
4800
|
+
const clean2 = hex.trim();
|
|
4801
|
+
if (!/^[0-9a-fA-F]{64}$/.test(clean2)) return null;
|
|
4802
|
+
const out = new Uint8Array(32);
|
|
4803
|
+
for (let i = 0; i < 32; i++) out[i] = parseInt(clean2.slice(i * 2, i * 2 + 2), 16);
|
|
4804
|
+
return out;
|
|
4805
|
+
}
|
|
4806
|
+
function evaluateBlindedPin(seed, userId, blindedB64) {
|
|
4807
|
+
if (!userId) throw new Error("userId is required");
|
|
4808
|
+
const { secretKey } = oprf.deriveKeyPair(seed, enc.encode(`oa-vault-pin:v1:${userId}`));
|
|
4809
|
+
return toB642(oprf.blindEvaluate(secretKey, fromB64(blindedB64)));
|
|
4810
|
+
}
|
|
4811
|
+
|
|
3668
4812
|
// ../crypto/src/index.ts
|
|
3669
4813
|
function b64ToBytes4(b64) {
|
|
3670
4814
|
const binary = atob(b64);
|
|
@@ -4303,15 +5447,15 @@ async function verifyWebhookSignature(payload, signature, secret) {
|
|
|
4303
5447
|
if (!/^[0-9a-f]{64}$/i.test(signature)) return false;
|
|
4304
5448
|
const sigBytes = new Uint8Array(32);
|
|
4305
5449
|
for (let i = 0; i < 32; i++) sigBytes[i] = Number.parseInt(signature.slice(i * 2, i * 2 + 2), 16);
|
|
4306
|
-
const
|
|
5450
|
+
const enc2 = new TextEncoder();
|
|
4307
5451
|
const key = await subtle().importKey(
|
|
4308
5452
|
"raw",
|
|
4309
|
-
|
|
5453
|
+
enc2.encode(secret),
|
|
4310
5454
|
{ name: "HMAC", hash: "SHA-256" },
|
|
4311
5455
|
false,
|
|
4312
5456
|
["verify"]
|
|
4313
5457
|
);
|
|
4314
|
-
return subtle().verify("HMAC", key, sigBytes,
|
|
5458
|
+
return subtle().verify("HMAC", key, sigBytes, enc2.encode(payload));
|
|
4315
5459
|
}
|
|
4316
5460
|
|
|
4317
5461
|
export {
|
|
@@ -4355,6 +5499,12 @@ export {
|
|
|
4355
5499
|
keyIdForPublicKey,
|
|
4356
5500
|
createSenderHeldKeyBinder,
|
|
4357
5501
|
openSealedWithSenderHeldKey,
|
|
5502
|
+
HARDENED_PREFIX,
|
|
5503
|
+
PIN_HARDENING_VERSION,
|
|
5504
|
+
isHardenedSecret,
|
|
5505
|
+
hardenPin,
|
|
5506
|
+
parseOprfSeed,
|
|
5507
|
+
evaluateBlindedPin,
|
|
4358
5508
|
normaliseAddress,
|
|
4359
5509
|
generatePartnerKeyPair,
|
|
4360
5510
|
generateHybridPartnerKeyPair,
|
|
@@ -4381,6 +5531,7 @@ export {
|
|
|
4381
5531
|
@noble/curves/abstract/curve.js:
|
|
4382
5532
|
@noble/curves/abstract/edwards.js:
|
|
4383
5533
|
@noble/curves/abstract/montgomery.js:
|
|
5534
|
+
@noble/curves/abstract/oprf.js:
|
|
4384
5535
|
@noble/curves/ed25519.js:
|
|
4385
5536
|
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
4386
5537
|
|