@blockcast/mmt-container 0.4.1 → 0.5.0-main.2c1f81817995
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/README.md +2 -2
- package/dist/browser-runtime.js +3458 -356
- package/dist/browser-runtime.js.map +4 -4
- package/dist/cmcd-producer.d.ts +5 -1
- package/dist/cmcd-producer.d.ts.map +1 -1
- package/dist/cmcd-producer.js.map +2 -2
- package/dist/delivery-catalog.js +18 -2
- package/dist/delivery-catalog.js.map +2 -2
- package/dist/fec-manager.d.ts +21 -16
- package/dist/fec-manager.d.ts.map +1 -1
- package/dist/fec-manager.js +177 -80
- package/dist/fec-manager.js.map +3 -3
- package/dist/held-layer-admission.d.ts +58 -0
- package/dist/held-layer-admission.d.ts.map +1 -0
- package/dist/held-layer-admission.js +105 -0
- package/dist/held-layer-admission.js.map +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +2 -2
- package/dist/mmtp-container-parser.no-modules.js +2682 -1023
- package/dist/mmtp-container-parser.no-modules.js.map +4 -4
- package/dist/mmtp-fec-session.d.ts +36 -3
- package/dist/mmtp-fec-session.d.ts.map +1 -1
- package/dist/mmtp-fec-session.js +114 -5
- package/dist/mmtp-fec-session.js.map +3 -3
- package/dist/mmtp-router.d.ts +72 -28
- package/dist/mmtp-router.d.ts.map +1 -1
- package/dist/mmtp-router.js +321 -203
- package/dist/mmtp-router.js.map +2 -2
- package/dist/multicast-auth-catalog.d.ts +10 -2
- package/dist/multicast-auth-catalog.d.ts.map +1 -1
- package/dist/multicast-auth-catalog.js +422 -22
- package/dist/multicast-auth-catalog.js.map +4 -4
- package/dist/multicast-manager.d.ts +48 -9
- package/dist/multicast-manager.d.ts.map +1 -1
- package/dist/multicast-manager.js +3406 -340
- package/dist/multicast-manager.js.map +4 -4
- package/dist/multicast-transport.d.ts +4 -1
- package/dist/multicast-transport.d.ts.map +1 -1
- package/dist/multicast-transport.js +20 -3
- package/dist/multicast-transport.js.map +2 -2
- package/dist/no-modules.d.ts +1 -2
- package/dist/no-modules.d.ts.map +1 -1
- package/dist/no-modules.js +2668 -1007
- package/dist/no-modules.js.map +4 -4
- package/dist/repair-fetch.d.ts +10 -1
- package/dist/repair-fetch.d.ts.map +1 -1
- package/dist/repair-fetch.js +5 -2
- package/dist/repair-fetch.js.map +2 -2
- package/package.json +5 -14
- package/src/cmcd-producer.ts +6 -1
- package/src/fec-manager.ts +121 -131
- package/src/held-layer-admission.ts +161 -0
- package/src/index.ts +5 -9
- package/src/mmtp-fec-session.ts +76 -3
- package/src/mmtp-router.ts +390 -272
- package/src/multicast-auth-catalog.ts +81 -8
- package/src/multicast-manager.ts +189 -48
- package/src/multicast-transport.ts +6 -2
- package/src/no-modules.ts +1 -6
- package/src/repair-fetch.ts +24 -4
- package/dist/manifest-auth-plan.d.ts +0 -31
- package/dist/manifest-auth-plan.d.ts.map +0 -1
- package/dist/manifest-auth-plan.js +0 -426
- package/dist/manifest-auth-plan.js.map +0 -7
- package/dist/source-auth.d.ts +0 -2
- package/dist/source-auth.d.ts.map +0 -1
- package/dist/source-auth.js +0 -3912
- package/dist/source-auth.js.map +0 -7
- package/src/manifest-auth-plan.ts +0 -55
- package/src/source-auth.ts +0 -1
- package/test-vectors/generate-source-auth-fixture.mjs +0 -114
- package/test-vectors/source-auth-v1.json +0 -61
package/dist/no-modules.js
CHANGED
|
@@ -8079,6 +8079,81 @@ function resolveFecState(evidence) {
|
|
|
8079
8079
|
|
|
8080
8080
|
// src/mmtp-fec-session.ts
|
|
8081
8081
|
import { deriveFecRetentionBlocks, fecBlockBudgetMs } from "@blockcast/mmt-fec";
|
|
8082
|
+
|
|
8083
|
+
// src/held-layer-admission.ts
|
|
8084
|
+
var HORIZON_CAP = 1073741824;
|
|
8085
|
+
function heldLayerAnchorHorizon(retentionBlocks, k) {
|
|
8086
|
+
return retentionBlocks === void 0 || k <= 0 ? HORIZON_CAP : Math.min(HORIZON_CAP, retentionBlocks * k);
|
|
8087
|
+
}
|
|
8088
|
+
var HeldLayerAdmission = class {
|
|
8089
|
+
#layerNames;
|
|
8090
|
+
#states;
|
|
8091
|
+
constructor(layers, heldLayers = [0]) {
|
|
8092
|
+
const held = new Set(heldLayers);
|
|
8093
|
+
this.#layerNames = new Set(layers.map((l) => l.repairTrackName));
|
|
8094
|
+
this.#states = layers.filter((l) => held.has(l.layer)).map((l) => ({ layer: l.layer, repairTrackName: l.repairTrackName, total: l.rsIdOffset + l.repairSymbols }));
|
|
8095
|
+
}
|
|
8096
|
+
/**
|
|
8097
|
+
* Judges one repair packet, after the 4.5 discards and the size check and
|
|
8098
|
+
* before it places a block. `repairTrackName` is the subscribed repair
|
|
8099
|
+
* track, never inferred from RS_ID; without it only (b) runs. A packet on a
|
|
8100
|
+
* repair track the catalog does not list is `'discard'` and anchors nothing.
|
|
8101
|
+
* `sourceFrontier` is the newest source SS_ID, if any: a repair more than
|
|
8102
|
+
* `horizon` past it is judged without moving the anchored state.
|
|
8103
|
+
*
|
|
8104
|
+
* @returns undefined to admit, `'discard'`, or the fail-closed reason.
|
|
8105
|
+
*/
|
|
8106
|
+
admit(repairTrackName, ssStart, rsbLength, horizon, sourceFrontier) {
|
|
8107
|
+
if (repairTrackName !== void 0 && !this.#layerNames.has(repairTrackName)) return "discard";
|
|
8108
|
+
const farAhead = sourceFrontier !== void 0 && serial32Precedes(sourceFrontier, ssStart) && serial32Distance(sourceFrontier, ssStart) > horizon;
|
|
8109
|
+
for (const state of this.#states) {
|
|
8110
|
+
let code;
|
|
8111
|
+
if (farAhead) {
|
|
8112
|
+
if (rsbLength < state.total) {
|
|
8113
|
+
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
8114
|
+
else if (state.anchor !== void 0 || state.settled) code = "below_anchored_total";
|
|
8115
|
+
}
|
|
8116
|
+
} else {
|
|
8117
|
+
if (state.newest === void 0 || serial32Precedes(state.newest, ssStart)) state.newest = ssStart;
|
|
8118
|
+
const stale = (s) => serial32Distance(s, state.newest) > horizon;
|
|
8119
|
+
if (state.latestBelow !== void 0 && stale(state.latestBelow)) {
|
|
8120
|
+
delete state.latestBelow;
|
|
8121
|
+
delete state.latestBelowRsb;
|
|
8122
|
+
}
|
|
8123
|
+
if (state.anchor !== void 0 && stale(state.anchor)) state.settled = true;
|
|
8124
|
+
if (rsbLength < state.total) {
|
|
8125
|
+
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
8126
|
+
else if (stale(ssStart)) continue;
|
|
8127
|
+
else if (state.settled || state.anchor !== void 0 && !serial32Precedes(ssStart, state.anchor)) code = "below_anchored_total";
|
|
8128
|
+
else if (state.latestBelow === void 0 || serial32Precedes(state.latestBelow, ssStart)) {
|
|
8129
|
+
state.latestBelow = ssStart;
|
|
8130
|
+
state.latestBelowRsb = rsbLength;
|
|
8131
|
+
}
|
|
8132
|
+
} else if (!state.settled && !stale(ssStart)) {
|
|
8133
|
+
if (state.anchor === void 0 || serial32Precedes(ssStart, state.anchor)) state.anchor = ssStart;
|
|
8134
|
+
if (state.latestBelow !== void 0 && !serial32Precedes(state.latestBelow, state.anchor)) {
|
|
8135
|
+
code = "below_anchored_total";
|
|
8136
|
+
ssStart = state.latestBelow;
|
|
8137
|
+
rsbLength = state.latestBelowRsb;
|
|
8138
|
+
}
|
|
8139
|
+
}
|
|
8140
|
+
}
|
|
8141
|
+
if (code !== void 0) {
|
|
8142
|
+
return {
|
|
8143
|
+
code,
|
|
8144
|
+
layer: state.layer,
|
|
8145
|
+
total: state.total,
|
|
8146
|
+
rsbLength,
|
|
8147
|
+
ssStart,
|
|
8148
|
+
...state.anchor === void 0 ? {} : { anchor: state.anchor }
|
|
8149
|
+
};
|
|
8150
|
+
}
|
|
8151
|
+
}
|
|
8152
|
+
return void 0;
|
|
8153
|
+
}
|
|
8154
|
+
};
|
|
8155
|
+
|
|
8156
|
+
// src/mmtp-fec-session.ts
|
|
8082
8157
|
var PATH_BITS = { moq: 1, multicast: 2 };
|
|
8083
8158
|
var MIXED_PATHS = PATH_BITS.moq | PATH_BITS.multicast;
|
|
8084
8159
|
var MmtpFecSession = class {
|
|
@@ -8096,6 +8171,9 @@ var MmtpFecSession = class {
|
|
|
8096
8171
|
/** The newest SS_ID when the remembered SS_IDs were last pruned. */
|
|
8097
8172
|
#prunedAt;
|
|
8098
8173
|
#repairPacketId;
|
|
8174
|
+
#newestSource;
|
|
8175
|
+
#heldLayers;
|
|
8176
|
+
#anchorHorizon;
|
|
8099
8177
|
#stats = {
|
|
8100
8178
|
sourceSymbols: 0,
|
|
8101
8179
|
repairSymbols: 0,
|
|
@@ -8108,11 +8186,12 @@ var MmtpFecSession = class {
|
|
|
8108
8186
|
dropped: {},
|
|
8109
8187
|
sourceSymbolsByPath: { moq: 0, multicast: 0 },
|
|
8110
8188
|
repairSymbolsByPath: { moq: 0, multicast: 0 },
|
|
8111
|
-
mixedPath: { clean: 0, recovered: 0, failed: 0, released: 0 }
|
|
8189
|
+
mixedPath: { clean: 0, recovered: 0, failed: 0, released: 0 },
|
|
8190
|
+
failClosed: null
|
|
8112
8191
|
};
|
|
8113
8192
|
/**
|
|
8114
8193
|
* @throws when the catalog block lacks or misstates a field the session
|
|
8115
|
-
* needs: the block profile, K, T, `interleaveDepthMs` or
|
|
8194
|
+
* needs: the block profile, K, P_0 (`repairSymbols`), T, `interleaveDepthMs` or
|
|
8116
8195
|
* `reorderToleranceMs`. None is defaulted.
|
|
8117
8196
|
*/
|
|
8118
8197
|
constructor(config) {
|
|
@@ -8126,6 +8205,7 @@ var MmtpFecSession = class {
|
|
|
8126
8205
|
}
|
|
8127
8206
|
this.algorithm = fec.algorithm === "reed-solomon" ? "reed-solomon" : "raptorq";
|
|
8128
8207
|
this.sourceSymbols = requirePositiveInteger(fec.sourceSymbols, "fec.sourceSymbols");
|
|
8208
|
+
this.repairSymbols = requirePositiveInteger(fec.repairSymbols, "fec.repairSymbols");
|
|
8129
8209
|
this.symbolSize = requirePositiveInteger(fec.symbolSize, "fec.symbolSize");
|
|
8130
8210
|
const interleaveDepthMs = requireNumber(fec.interleaveDepthMs, "fec.interleaveDepthMs");
|
|
8131
8211
|
const reorderToleranceMs = requireNumber(fec.reorderToleranceMs, "fec.reorderToleranceMs");
|
|
@@ -8133,6 +8213,18 @@ var MmtpFecSession = class {
|
|
|
8133
8213
|
fecBlockBudgetMs(interleaveDepthMs, reorderToleranceMs),
|
|
8134
8214
|
interleaveDepthMs
|
|
8135
8215
|
);
|
|
8216
|
+
this.#heldLayers = new HeldLayerAdmission(
|
|
8217
|
+
config.repairLayers ?? [
|
|
8218
|
+
{
|
|
8219
|
+
layer: 0,
|
|
8220
|
+
repairTrackName: typeof fec.repairTrack === "string" ? fec.repairTrack : "",
|
|
8221
|
+
rsIdOffset: 0,
|
|
8222
|
+
repairSymbols: this.repairSymbols
|
|
8223
|
+
}
|
|
8224
|
+
],
|
|
8225
|
+
config.heldLayers
|
|
8226
|
+
);
|
|
8227
|
+
this.#anchorHorizon = heldLayerAnchorHorizon(this.retentionBlocks, this.sourceSymbols);
|
|
8136
8228
|
if (typeof config.Decoder !== "function") {
|
|
8137
8229
|
throw new Error("MmtpFecSession: a decoder constructor (wasm_bindgen.MmtFecDecoder) is required");
|
|
8138
8230
|
}
|
|
@@ -8155,7 +8247,8 @@ var MmtpFecSession = class {
|
|
|
8155
8247
|
dropped: { ...this.#stats.dropped },
|
|
8156
8248
|
sourceSymbolsByPath: { ...this.#stats.sourceSymbolsByPath },
|
|
8157
8249
|
repairSymbolsByPath: { ...this.#stats.repairSymbolsByPath },
|
|
8158
|
-
mixedPath: { ...this.#stats.mixedPath }
|
|
8250
|
+
mixedPath: { ...this.#stats.mixedPath },
|
|
8251
|
+
failClosed: this.#stats.failClosed && { ...this.#stats.failClosed }
|
|
8159
8252
|
};
|
|
8160
8253
|
}
|
|
8161
8254
|
/**
|
|
@@ -8180,6 +8273,7 @@ var MmtpFecSession = class {
|
|
|
8180
8273
|
this.#stats.sourceSymbols++;
|
|
8181
8274
|
this.#stats.sourceSymbolsByPath[path]++;
|
|
8182
8275
|
this.#noteNewest(ssId);
|
|
8276
|
+
if (this.#newestSource === void 0 || serial32Precedes(this.#newestSource, ssId)) this.#newestSource = ssId;
|
|
8183
8277
|
const start = this.#blockStartOf(ssId);
|
|
8184
8278
|
const block = start === void 0 ? void 0 : this.#blocks.get(start);
|
|
8185
8279
|
if (block) {
|
|
@@ -8208,14 +8302,17 @@ var MmtpFecSession = class {
|
|
|
8208
8302
|
* Feeds one object of the repair track: a whole MMTP repair packet (ISO/IEC
|
|
8209
8303
|
* 23008-1 C.5.3). A repair track carries one repair flow, so the first
|
|
8210
8304
|
* repair packet fixes its packet ID and a packet of any other is refused.
|
|
8211
|
-
* `path` is where it was received, as for addSource().
|
|
8305
|
+
* `path` is where it was received, as for addSource(). `repairTrackName`
|
|
8306
|
+
* is the repair track it was received on, for the 6.3.9 own-track rule; a
|
|
8307
|
+
* track the layers do not list is refused as `repair:unlisted-track`.
|
|
8212
8308
|
*
|
|
8213
8309
|
* @returns the protected packets this repair symbol recovered, as for
|
|
8214
8310
|
* addSource().
|
|
8215
8311
|
*/
|
|
8216
|
-
addRepair(packet, path = "moq") {
|
|
8312
|
+
addRepair(packet, path = "moq", repairTrackName) {
|
|
8217
8313
|
const decoder = this.#decoder;
|
|
8218
8314
|
if (!decoder) return this.#drop("repair:released");
|
|
8315
|
+
if (this.#stats.failClosed) return this.#drop("repair:fail-closed");
|
|
8219
8316
|
const pathBit = PATH_BITS[path];
|
|
8220
8317
|
if (!pathBit) return this.#drop("repair:unknown-path");
|
|
8221
8318
|
let header;
|
|
@@ -8236,6 +8333,18 @@ var MmtpFecSession = class {
|
|
|
8236
8333
|
if (this.algorithm === "reed-solomon" && frame.ssbLength + frame.rsbLength > 255) {
|
|
8237
8334
|
return this.#drop("repair:rs-block-length");
|
|
8238
8335
|
}
|
|
8336
|
+
const verdict = this.#heldLayers.admit(
|
|
8337
|
+
repairTrackName,
|
|
8338
|
+
frame.ssStart,
|
|
8339
|
+
frame.rsbLength,
|
|
8340
|
+
this.#anchorHorizon,
|
|
8341
|
+
this.#newestSource
|
|
8342
|
+
);
|
|
8343
|
+
if (verdict === "discard") return this.#drop("repair:unlisted-track");
|
|
8344
|
+
if (verdict !== void 0) {
|
|
8345
|
+
this.#stats.failClosed = verdict;
|
|
8346
|
+
return this.#drop("repair:fail-closed");
|
|
8347
|
+
}
|
|
8239
8348
|
const start = frame.ssStart;
|
|
8240
8349
|
let block = this.#blocks.get(start);
|
|
8241
8350
|
if (block && block.ssbLength !== frame.ssbLength) return this.#drop("repair:conflicting-ssb-length");
|
|
@@ -9123,6 +9232,9 @@ var MmtpToCmaf = class {
|
|
|
9123
9232
|
}
|
|
9124
9233
|
};
|
|
9125
9234
|
|
|
9235
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/crypto.js
|
|
9236
|
+
var crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
9237
|
+
|
|
9126
9238
|
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/utils.js
|
|
9127
9239
|
function isBytes(a) {
|
|
9128
9240
|
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
@@ -9150,32 +9262,63 @@ function aoutput(out, instance) {
|
|
|
9150
9262
|
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
9151
9263
|
}
|
|
9152
9264
|
}
|
|
9153
|
-
function u8(arr) {
|
|
9154
|
-
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
9155
|
-
}
|
|
9156
|
-
function u322(arr) {
|
|
9157
|
-
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
9158
|
-
}
|
|
9159
9265
|
function clean(...arrays) {
|
|
9160
9266
|
for (let i = 0; i < arrays.length; i++) {
|
|
9161
9267
|
arrays[i].fill(0);
|
|
9162
9268
|
}
|
|
9163
9269
|
}
|
|
9270
|
+
function createView(arr) {
|
|
9271
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
9272
|
+
}
|
|
9164
9273
|
function rotr(word, shift) {
|
|
9165
9274
|
return word << 32 - shift | word >>> shift;
|
|
9166
9275
|
}
|
|
9167
|
-
var
|
|
9168
|
-
|
|
9169
|
-
|
|
9170
|
-
|
|
9171
|
-
var
|
|
9172
|
-
function
|
|
9173
|
-
|
|
9174
|
-
|
|
9175
|
-
|
|
9176
|
-
|
|
9276
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
9277
|
+
// @ts-ignore
|
|
9278
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
9279
|
+
))();
|
|
9280
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
9281
|
+
function bytesToHex(bytes) {
|
|
9282
|
+
abytes(bytes);
|
|
9283
|
+
if (hasHexBuiltin)
|
|
9284
|
+
return bytes.toHex();
|
|
9285
|
+
let hex2 = "";
|
|
9286
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
9287
|
+
hex2 += hexes[bytes[i]];
|
|
9288
|
+
}
|
|
9289
|
+
return hex2;
|
|
9290
|
+
}
|
|
9291
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
9292
|
+
function asciiToBase16(ch) {
|
|
9293
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
9294
|
+
return ch - asciis._0;
|
|
9295
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
9296
|
+
return ch - (asciis.A - 10);
|
|
9297
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
9298
|
+
return ch - (asciis.a - 10);
|
|
9299
|
+
return;
|
|
9300
|
+
}
|
|
9301
|
+
function hexToBytes(hex2) {
|
|
9302
|
+
if (typeof hex2 !== "string")
|
|
9303
|
+
throw new Error("hex string expected, got " + typeof hex2);
|
|
9304
|
+
if (hasHexBuiltin)
|
|
9305
|
+
return Uint8Array.fromHex(hex2);
|
|
9306
|
+
const hl = hex2.length;
|
|
9307
|
+
const al = hl / 2;
|
|
9308
|
+
if (hl % 2)
|
|
9309
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
9310
|
+
const array = new Uint8Array(al);
|
|
9311
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
9312
|
+
const n1 = asciiToBase16(hex2.charCodeAt(hi));
|
|
9313
|
+
const n2 = asciiToBase16(hex2.charCodeAt(hi + 1));
|
|
9314
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
9315
|
+
const char = hex2[hi] + hex2[hi + 1];
|
|
9316
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
9317
|
+
}
|
|
9318
|
+
array[ai] = n1 * 16 + n2;
|
|
9319
|
+
}
|
|
9320
|
+
return array;
|
|
9177
9321
|
}
|
|
9178
|
-
var swap32IfBE = isLE ? (u) => u : byteSwap32;
|
|
9179
9322
|
function utf8ToBytes(str) {
|
|
9180
9323
|
if (typeof str !== "string")
|
|
9181
9324
|
throw new Error("string expected");
|
|
@@ -9187,114 +9330,126 @@ function toBytes(data) {
|
|
|
9187
9330
|
abytes(data);
|
|
9188
9331
|
return data;
|
|
9189
9332
|
}
|
|
9333
|
+
function concatBytes(...arrays) {
|
|
9334
|
+
let sum = 0;
|
|
9335
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
9336
|
+
const a = arrays[i];
|
|
9337
|
+
abytes(a);
|
|
9338
|
+
sum += a.length;
|
|
9339
|
+
}
|
|
9340
|
+
const res = new Uint8Array(sum);
|
|
9341
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
9342
|
+
const a = arrays[i];
|
|
9343
|
+
res.set(a, pad);
|
|
9344
|
+
pad += a.length;
|
|
9345
|
+
}
|
|
9346
|
+
return res;
|
|
9347
|
+
}
|
|
9190
9348
|
var Hash = class {
|
|
9191
9349
|
};
|
|
9192
|
-
function
|
|
9193
|
-
const hashC = (msg
|
|
9194
|
-
const tmp = hashCons(
|
|
9350
|
+
function createHasher(hashCons) {
|
|
9351
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
9352
|
+
const tmp = hashCons();
|
|
9195
9353
|
hashC.outputLen = tmp.outputLen;
|
|
9196
9354
|
hashC.blockLen = tmp.blockLen;
|
|
9197
|
-
hashC.create = (
|
|
9355
|
+
hashC.create = () => hashCons();
|
|
9198
9356
|
return hashC;
|
|
9199
9357
|
}
|
|
9200
|
-
|
|
9201
|
-
|
|
9202
|
-
|
|
9203
|
-
|
|
9204
|
-
|
|
9205
|
-
|
|
9206
|
-
|
|
9207
|
-
|
|
9208
|
-
2600822924,
|
|
9209
|
-
528734635,
|
|
9210
|
-
1541459225
|
|
9211
|
-
]);
|
|
9212
|
-
|
|
9213
|
-
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_u64.js
|
|
9214
|
-
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
9215
|
-
var _32n = /* @__PURE__ */ BigInt(32);
|
|
9216
|
-
function fromBig(n, le = false) {
|
|
9217
|
-
if (le)
|
|
9218
|
-
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
9219
|
-
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
9220
|
-
}
|
|
9221
|
-
|
|
9222
|
-
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_blake.js
|
|
9223
|
-
function G1s(a, b, c, d, x) {
|
|
9224
|
-
a = a + b + x | 0;
|
|
9225
|
-
d = rotr(d ^ a, 16);
|
|
9226
|
-
c = c + d | 0;
|
|
9227
|
-
b = rotr(b ^ c, 12);
|
|
9228
|
-
return { a, b, c, d };
|
|
9229
|
-
}
|
|
9230
|
-
function G2s(a, b, c, d, x) {
|
|
9231
|
-
a = a + b + x | 0;
|
|
9232
|
-
d = rotr(d ^ a, 8);
|
|
9233
|
-
c = c + d | 0;
|
|
9234
|
-
b = rotr(b ^ c, 7);
|
|
9235
|
-
return { a, b, c, d };
|
|
9358
|
+
function randomBytes(bytesLength = 32) {
|
|
9359
|
+
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
9360
|
+
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
9361
|
+
}
|
|
9362
|
+
if (crypto && typeof crypto.randomBytes === "function") {
|
|
9363
|
+
return Uint8Array.from(crypto.randomBytes(bytesLength));
|
|
9364
|
+
}
|
|
9365
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
9236
9366
|
}
|
|
9237
9367
|
|
|
9238
|
-
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/
|
|
9239
|
-
|
|
9240
|
-
|
|
9368
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_md.js
|
|
9369
|
+
function setBigUint64(view2, byteOffset, value, isLE) {
|
|
9370
|
+
if (typeof view2.setBigUint64 === "function")
|
|
9371
|
+
return view2.setBigUint64(byteOffset, value, isLE);
|
|
9372
|
+
const _32n2 = BigInt(32);
|
|
9373
|
+
const _u32_max = BigInt(4294967295);
|
|
9374
|
+
const wh = Number(value >> _32n2 & _u32_max);
|
|
9375
|
+
const wl = Number(value & _u32_max);
|
|
9376
|
+
const h = isLE ? 4 : 0;
|
|
9377
|
+
const l = isLE ? 0 : 4;
|
|
9378
|
+
view2.setUint32(byteOffset + h, wh, isLE);
|
|
9379
|
+
view2.setUint32(byteOffset + l, wl, isLE);
|
|
9380
|
+
}
|
|
9381
|
+
function Chi(a, b, c) {
|
|
9382
|
+
return a & b ^ ~a & c;
|
|
9383
|
+
}
|
|
9384
|
+
function Maj(a, b, c) {
|
|
9385
|
+
return a & b ^ a & c ^ b & c;
|
|
9386
|
+
}
|
|
9387
|
+
var HashMD = class extends Hash {
|
|
9388
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
9241
9389
|
super();
|
|
9242
9390
|
this.finished = false;
|
|
9243
|
-
this.destroyed = false;
|
|
9244
9391
|
this.length = 0;
|
|
9245
9392
|
this.pos = 0;
|
|
9246
|
-
|
|
9247
|
-
anumber(outputLen);
|
|
9393
|
+
this.destroyed = false;
|
|
9248
9394
|
this.blockLen = blockLen;
|
|
9249
9395
|
this.outputLen = outputLen;
|
|
9396
|
+
this.padOffset = padOffset;
|
|
9397
|
+
this.isLE = isLE;
|
|
9250
9398
|
this.buffer = new Uint8Array(blockLen);
|
|
9251
|
-
this.
|
|
9399
|
+
this.view = createView(this.buffer);
|
|
9252
9400
|
}
|
|
9253
9401
|
update(data) {
|
|
9254
9402
|
aexists(this);
|
|
9255
9403
|
data = toBytes(data);
|
|
9256
9404
|
abytes(data);
|
|
9257
|
-
const {
|
|
9405
|
+
const { view: view2, buffer, blockLen } = this;
|
|
9258
9406
|
const len = data.length;
|
|
9259
|
-
const offset = data.byteOffset;
|
|
9260
|
-
const buf = data.buffer;
|
|
9261
9407
|
for (let pos = 0; pos < len; ) {
|
|
9262
|
-
if (this.pos === blockLen) {
|
|
9263
|
-
swap32IfBE(buffer32);
|
|
9264
|
-
this.compress(buffer32, 0, false);
|
|
9265
|
-
swap32IfBE(buffer32);
|
|
9266
|
-
this.pos = 0;
|
|
9267
|
-
}
|
|
9268
9408
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
9269
|
-
|
|
9270
|
-
|
|
9271
|
-
|
|
9272
|
-
|
|
9273
|
-
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) {
|
|
9274
|
-
this.length += blockLen;
|
|
9275
|
-
this.compress(data32, pos32, false);
|
|
9276
|
-
}
|
|
9277
|
-
swap32IfBE(data32);
|
|
9409
|
+
if (take === blockLen) {
|
|
9410
|
+
const dataView = createView(data);
|
|
9411
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
9412
|
+
this.process(dataView, pos);
|
|
9278
9413
|
continue;
|
|
9279
9414
|
}
|
|
9280
9415
|
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
9281
9416
|
this.pos += take;
|
|
9282
|
-
this.length += take;
|
|
9283
9417
|
pos += take;
|
|
9418
|
+
if (this.pos === blockLen) {
|
|
9419
|
+
this.process(view2, 0);
|
|
9420
|
+
this.pos = 0;
|
|
9421
|
+
}
|
|
9284
9422
|
}
|
|
9423
|
+
this.length += data.length;
|
|
9424
|
+
this.roundClean();
|
|
9285
9425
|
return this;
|
|
9286
9426
|
}
|
|
9287
9427
|
digestInto(out) {
|
|
9288
9428
|
aexists(this);
|
|
9289
9429
|
aoutput(out, this);
|
|
9290
|
-
const { pos, buffer32 } = this;
|
|
9291
9430
|
this.finished = true;
|
|
9431
|
+
const { buffer, view: view2, blockLen, isLE } = this;
|
|
9432
|
+
let { pos } = this;
|
|
9433
|
+
buffer[pos++] = 128;
|
|
9292
9434
|
clean(this.buffer.subarray(pos));
|
|
9293
|
-
|
|
9294
|
-
|
|
9295
|
-
|
|
9296
|
-
|
|
9297
|
-
|
|
9435
|
+
if (this.padOffset > blockLen - pos) {
|
|
9436
|
+
this.process(view2, 0);
|
|
9437
|
+
pos = 0;
|
|
9438
|
+
}
|
|
9439
|
+
for (let i = pos; i < blockLen; i++)
|
|
9440
|
+
buffer[i] = 0;
|
|
9441
|
+
setBigUint64(view2, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
9442
|
+
this.process(view2, 0);
|
|
9443
|
+
const oview = createView(out);
|
|
9444
|
+
const len = this.outputLen;
|
|
9445
|
+
if (len % 4)
|
|
9446
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
9447
|
+
const outLen = len / 4;
|
|
9448
|
+
const state = this.get();
|
|
9449
|
+
if (outLen > state.length)
|
|
9450
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
9451
|
+
for (let i = 0; i < outLen; i++)
|
|
9452
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
9298
9453
|
}
|
|
9299
9454
|
digest() {
|
|
9300
9455
|
const { buffer, outputLen } = this;
|
|
@@ -9304,1000 +9459,2502 @@ var BLAKE2 = class extends Hash {
|
|
|
9304
9459
|
return res;
|
|
9305
9460
|
}
|
|
9306
9461
|
_cloneInto(to) {
|
|
9307
|
-
|
|
9308
|
-
to || (to = new this.constructor({ dkLen: outputLen }));
|
|
9462
|
+
to || (to = new this.constructor());
|
|
9309
9463
|
to.set(...this.get());
|
|
9310
|
-
|
|
9464
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
9311
9465
|
to.destroyed = destroyed;
|
|
9312
9466
|
to.finished = finished;
|
|
9313
9467
|
to.length = length;
|
|
9314
9468
|
to.pos = pos;
|
|
9315
|
-
|
|
9469
|
+
if (length % blockLen)
|
|
9470
|
+
to.buffer.set(buffer);
|
|
9316
9471
|
return to;
|
|
9317
9472
|
}
|
|
9318
9473
|
clone() {
|
|
9319
9474
|
return this._cloneInto();
|
|
9320
9475
|
}
|
|
9321
9476
|
};
|
|
9322
|
-
|
|
9323
|
-
|
|
9324
|
-
|
|
9325
|
-
|
|
9326
|
-
|
|
9327
|
-
|
|
9328
|
-
|
|
9329
|
-
|
|
9330
|
-
|
|
9331
|
-
|
|
9332
|
-
|
|
9333
|
-
|
|
9334
|
-
|
|
9335
|
-
|
|
9336
|
-
|
|
9337
|
-
|
|
9338
|
-
|
|
9339
|
-
|
|
9340
|
-
|
|
9341
|
-
|
|
9342
|
-
|
|
9477
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
9478
|
+
1779033703,
|
|
9479
|
+
3144134277,
|
|
9480
|
+
1013904242,
|
|
9481
|
+
2773480762,
|
|
9482
|
+
1359893119,
|
|
9483
|
+
2600822924,
|
|
9484
|
+
528734635,
|
|
9485
|
+
1541459225
|
|
9486
|
+
]);
|
|
9487
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
9488
|
+
1779033703,
|
|
9489
|
+
4089235720,
|
|
9490
|
+
3144134277,
|
|
9491
|
+
2227873595,
|
|
9492
|
+
1013904242,
|
|
9493
|
+
4271175723,
|
|
9494
|
+
2773480762,
|
|
9495
|
+
1595750129,
|
|
9496
|
+
1359893119,
|
|
9497
|
+
2917565137,
|
|
9498
|
+
2600822924,
|
|
9499
|
+
725511199,
|
|
9500
|
+
528734635,
|
|
9501
|
+
4215389547,
|
|
9502
|
+
1541459225,
|
|
9503
|
+
327033209
|
|
9504
|
+
]);
|
|
9505
|
+
|
|
9506
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_u64.js
|
|
9507
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
9508
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
9509
|
+
function fromBig(n, le = false) {
|
|
9510
|
+
if (le)
|
|
9511
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
9512
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
9343
9513
|
}
|
|
9514
|
+
function split(lst, le = false) {
|
|
9515
|
+
const len = lst.length;
|
|
9516
|
+
let Ah = new Uint32Array(len);
|
|
9517
|
+
let Al = new Uint32Array(len);
|
|
9518
|
+
for (let i = 0; i < len; i++) {
|
|
9519
|
+
const { h, l } = fromBig(lst[i], le);
|
|
9520
|
+
[Ah[i], Al[i]] = [h, l];
|
|
9521
|
+
}
|
|
9522
|
+
return [Ah, Al];
|
|
9523
|
+
}
|
|
9524
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
9525
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
9526
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
9527
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
9528
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
9529
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
9530
|
+
function add(Ah, Al, Bh, Bl) {
|
|
9531
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
9532
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
9533
|
+
}
|
|
9534
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
9535
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
9536
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
9537
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
9538
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
9539
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
9344
9540
|
|
|
9345
|
-
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/
|
|
9346
|
-
var
|
|
9347
|
-
|
|
9348
|
-
|
|
9349
|
-
|
|
9350
|
-
|
|
9351
|
-
|
|
9352
|
-
|
|
9353
|
-
|
|
9354
|
-
|
|
9355
|
-
|
|
9356
|
-
|
|
9357
|
-
|
|
9358
|
-
|
|
9359
|
-
|
|
9360
|
-
|
|
9361
|
-
|
|
9362
|
-
|
|
9363
|
-
|
|
9364
|
-
|
|
9365
|
-
|
|
9366
|
-
|
|
9367
|
-
|
|
9368
|
-
|
|
9369
|
-
|
|
9370
|
-
|
|
9371
|
-
|
|
9372
|
-
|
|
9373
|
-
|
|
9374
|
-
|
|
9375
|
-
|
|
9376
|
-
|
|
9377
|
-
|
|
9378
|
-
|
|
9379
|
-
|
|
9380
|
-
|
|
9381
|
-
|
|
9382
|
-
|
|
9383
|
-
|
|
9384
|
-
|
|
9385
|
-
|
|
9386
|
-
|
|
9387
|
-
|
|
9388
|
-
|
|
9389
|
-
|
|
9390
|
-
|
|
9391
|
-
|
|
9392
|
-
|
|
9393
|
-
|
|
9394
|
-
|
|
9395
|
-
|
|
9396
|
-
|
|
9541
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha2.js
|
|
9542
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
9543
|
+
1116352408,
|
|
9544
|
+
1899447441,
|
|
9545
|
+
3049323471,
|
|
9546
|
+
3921009573,
|
|
9547
|
+
961987163,
|
|
9548
|
+
1508970993,
|
|
9549
|
+
2453635748,
|
|
9550
|
+
2870763221,
|
|
9551
|
+
3624381080,
|
|
9552
|
+
310598401,
|
|
9553
|
+
607225278,
|
|
9554
|
+
1426881987,
|
|
9555
|
+
1925078388,
|
|
9556
|
+
2162078206,
|
|
9557
|
+
2614888103,
|
|
9558
|
+
3248222580,
|
|
9559
|
+
3835390401,
|
|
9560
|
+
4022224774,
|
|
9561
|
+
264347078,
|
|
9562
|
+
604807628,
|
|
9563
|
+
770255983,
|
|
9564
|
+
1249150122,
|
|
9565
|
+
1555081692,
|
|
9566
|
+
1996064986,
|
|
9567
|
+
2554220882,
|
|
9568
|
+
2821834349,
|
|
9569
|
+
2952996808,
|
|
9570
|
+
3210313671,
|
|
9571
|
+
3336571891,
|
|
9572
|
+
3584528711,
|
|
9573
|
+
113926993,
|
|
9574
|
+
338241895,
|
|
9575
|
+
666307205,
|
|
9576
|
+
773529912,
|
|
9577
|
+
1294757372,
|
|
9578
|
+
1396182291,
|
|
9579
|
+
1695183700,
|
|
9580
|
+
1986661051,
|
|
9581
|
+
2177026350,
|
|
9582
|
+
2456956037,
|
|
9583
|
+
2730485921,
|
|
9584
|
+
2820302411,
|
|
9585
|
+
3259730800,
|
|
9586
|
+
3345764771,
|
|
9587
|
+
3516065817,
|
|
9588
|
+
3600352804,
|
|
9589
|
+
4094571909,
|
|
9590
|
+
275423344,
|
|
9591
|
+
430227734,
|
|
9592
|
+
506948616,
|
|
9593
|
+
659060556,
|
|
9594
|
+
883997877,
|
|
9595
|
+
958139571,
|
|
9596
|
+
1322822218,
|
|
9597
|
+
1537002063,
|
|
9598
|
+
1747873779,
|
|
9599
|
+
1955562222,
|
|
9600
|
+
2024104815,
|
|
9601
|
+
2227730452,
|
|
9602
|
+
2361852424,
|
|
9603
|
+
2428436474,
|
|
9604
|
+
2756734187,
|
|
9605
|
+
3204031479,
|
|
9606
|
+
3329325298
|
|
9607
|
+
]);
|
|
9608
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
9609
|
+
var SHA256 = class extends HashMD {
|
|
9610
|
+
constructor(outputLen = 32) {
|
|
9611
|
+
super(64, outputLen, 8, false);
|
|
9612
|
+
this.A = SHA256_IV[0] | 0;
|
|
9613
|
+
this.B = SHA256_IV[1] | 0;
|
|
9614
|
+
this.C = SHA256_IV[2] | 0;
|
|
9615
|
+
this.D = SHA256_IV[3] | 0;
|
|
9616
|
+
this.E = SHA256_IV[4] | 0;
|
|
9617
|
+
this.F = SHA256_IV[5] | 0;
|
|
9618
|
+
this.G = SHA256_IV[6] | 0;
|
|
9619
|
+
this.H = SHA256_IV[7] | 0;
|
|
9397
9620
|
}
|
|
9398
|
-
// Unused
|
|
9399
9621
|
get() {
|
|
9400
|
-
|
|
9622
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
9623
|
+
return [A, B, C, D, E, F, G, H];
|
|
9624
|
+
}
|
|
9625
|
+
// prettier-ignore
|
|
9626
|
+
set(A, B, C, D, E, F, G, H) {
|
|
9627
|
+
this.A = A | 0;
|
|
9628
|
+
this.B = B | 0;
|
|
9629
|
+
this.C = C | 0;
|
|
9630
|
+
this.D = D | 0;
|
|
9631
|
+
this.E = E | 0;
|
|
9632
|
+
this.F = F | 0;
|
|
9633
|
+
this.G = G | 0;
|
|
9634
|
+
this.H = H | 0;
|
|
9635
|
+
}
|
|
9636
|
+
process(view2, offset) {
|
|
9637
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
9638
|
+
SHA256_W[i] = view2.getUint32(offset, false);
|
|
9639
|
+
for (let i = 16; i < 64; i++) {
|
|
9640
|
+
const W15 = SHA256_W[i - 15];
|
|
9641
|
+
const W2 = SHA256_W[i - 2];
|
|
9642
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
9643
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
9644
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
9645
|
+
}
|
|
9646
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
9647
|
+
for (let i = 0; i < 64; i++) {
|
|
9648
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
9649
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
9650
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
9651
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
9652
|
+
H = G;
|
|
9653
|
+
G = F;
|
|
9654
|
+
F = E;
|
|
9655
|
+
E = D + T1 | 0;
|
|
9656
|
+
D = C;
|
|
9657
|
+
C = B;
|
|
9658
|
+
B = A;
|
|
9659
|
+
A = T1 + T2 | 0;
|
|
9660
|
+
}
|
|
9661
|
+
A = A + this.A | 0;
|
|
9662
|
+
B = B + this.B | 0;
|
|
9663
|
+
C = C + this.C | 0;
|
|
9664
|
+
D = D + this.D | 0;
|
|
9665
|
+
E = E + this.E | 0;
|
|
9666
|
+
F = F + this.F | 0;
|
|
9667
|
+
G = G + this.G | 0;
|
|
9668
|
+
H = H + this.H | 0;
|
|
9669
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
9670
|
+
}
|
|
9671
|
+
roundClean() {
|
|
9672
|
+
clean(SHA256_W);
|
|
9401
9673
|
}
|
|
9402
|
-
|
|
9403
|
-
|
|
9404
|
-
|
|
9405
|
-
const { state: s, pos } = this;
|
|
9406
|
-
const { h, l } = fromBig(BigInt(counter), true);
|
|
9407
|
-
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(B3_SIGMA, bufPos, buf, 7, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], h, l, pos, flags);
|
|
9408
|
-
s[0] = v0 ^ v8;
|
|
9409
|
-
s[1] = v1 ^ v9;
|
|
9410
|
-
s[2] = v2 ^ v10;
|
|
9411
|
-
s[3] = v3 ^ v11;
|
|
9412
|
-
s[4] = v4 ^ v12;
|
|
9413
|
-
s[5] = v5 ^ v13;
|
|
9414
|
-
s[6] = v6 ^ v14;
|
|
9415
|
-
s[7] = v7 ^ v15;
|
|
9416
|
-
}
|
|
9417
|
-
compress(buf, bufPos = 0, isLast = false) {
|
|
9418
|
-
let flags = this.flags;
|
|
9419
|
-
if (!this.chunkPos)
|
|
9420
|
-
flags |= B3_Flags.CHUNK_START;
|
|
9421
|
-
if (this.chunkPos === 15 || isLast)
|
|
9422
|
-
flags |= B3_Flags.CHUNK_END;
|
|
9423
|
-
if (!isLast)
|
|
9424
|
-
this.pos = this.blockLen;
|
|
9425
|
-
this.b2Compress(this.chunksDone, flags, buf, bufPos);
|
|
9426
|
-
this.chunkPos += 1;
|
|
9427
|
-
if (this.chunkPos === 16 || isLast) {
|
|
9428
|
-
let chunk = this.state;
|
|
9429
|
-
this.state = this.IV.slice();
|
|
9430
|
-
for (let last, chunks = this.chunksDone + 1; isLast || !(chunks & 1); chunks >>= 1) {
|
|
9431
|
-
if (!(last = this.stack.pop()))
|
|
9432
|
-
break;
|
|
9433
|
-
this.buffer32.set(last, 0);
|
|
9434
|
-
this.buffer32.set(chunk, 8);
|
|
9435
|
-
this.pos = this.blockLen;
|
|
9436
|
-
this.b2Compress(0, this.flags | B3_Flags.PARENT, this.buffer32, 0);
|
|
9437
|
-
chunk = this.state;
|
|
9438
|
-
this.state = this.IV.slice();
|
|
9439
|
-
}
|
|
9440
|
-
this.chunksDone++;
|
|
9441
|
-
this.chunkPos = 0;
|
|
9442
|
-
this.stack.push(chunk);
|
|
9443
|
-
}
|
|
9444
|
-
this.pos = 0;
|
|
9674
|
+
destroy() {
|
|
9675
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
9676
|
+
clean(this.buffer);
|
|
9445
9677
|
}
|
|
9446
|
-
|
|
9447
|
-
|
|
9448
|
-
|
|
9449
|
-
|
|
9450
|
-
|
|
9451
|
-
|
|
9452
|
-
|
|
9453
|
-
|
|
9454
|
-
|
|
9455
|
-
|
|
9456
|
-
|
|
9457
|
-
|
|
9458
|
-
|
|
9459
|
-
|
|
9678
|
+
};
|
|
9679
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
9680
|
+
"0x428a2f98d728ae22",
|
|
9681
|
+
"0x7137449123ef65cd",
|
|
9682
|
+
"0xb5c0fbcfec4d3b2f",
|
|
9683
|
+
"0xe9b5dba58189dbbc",
|
|
9684
|
+
"0x3956c25bf348b538",
|
|
9685
|
+
"0x59f111f1b605d019",
|
|
9686
|
+
"0x923f82a4af194f9b",
|
|
9687
|
+
"0xab1c5ed5da6d8118",
|
|
9688
|
+
"0xd807aa98a3030242",
|
|
9689
|
+
"0x12835b0145706fbe",
|
|
9690
|
+
"0x243185be4ee4b28c",
|
|
9691
|
+
"0x550c7dc3d5ffb4e2",
|
|
9692
|
+
"0x72be5d74f27b896f",
|
|
9693
|
+
"0x80deb1fe3b1696b1",
|
|
9694
|
+
"0x9bdc06a725c71235",
|
|
9695
|
+
"0xc19bf174cf692694",
|
|
9696
|
+
"0xe49b69c19ef14ad2",
|
|
9697
|
+
"0xefbe4786384f25e3",
|
|
9698
|
+
"0x0fc19dc68b8cd5b5",
|
|
9699
|
+
"0x240ca1cc77ac9c65",
|
|
9700
|
+
"0x2de92c6f592b0275",
|
|
9701
|
+
"0x4a7484aa6ea6e483",
|
|
9702
|
+
"0x5cb0a9dcbd41fbd4",
|
|
9703
|
+
"0x76f988da831153b5",
|
|
9704
|
+
"0x983e5152ee66dfab",
|
|
9705
|
+
"0xa831c66d2db43210",
|
|
9706
|
+
"0xb00327c898fb213f",
|
|
9707
|
+
"0xbf597fc7beef0ee4",
|
|
9708
|
+
"0xc6e00bf33da88fc2",
|
|
9709
|
+
"0xd5a79147930aa725",
|
|
9710
|
+
"0x06ca6351e003826f",
|
|
9711
|
+
"0x142929670a0e6e70",
|
|
9712
|
+
"0x27b70a8546d22ffc",
|
|
9713
|
+
"0x2e1b21385c26c926",
|
|
9714
|
+
"0x4d2c6dfc5ac42aed",
|
|
9715
|
+
"0x53380d139d95b3df",
|
|
9716
|
+
"0x650a73548baf63de",
|
|
9717
|
+
"0x766a0abb3c77b2a8",
|
|
9718
|
+
"0x81c2c92e47edaee6",
|
|
9719
|
+
"0x92722c851482353b",
|
|
9720
|
+
"0xa2bfe8a14cf10364",
|
|
9721
|
+
"0xa81a664bbc423001",
|
|
9722
|
+
"0xc24b8b70d0f89791",
|
|
9723
|
+
"0xc76c51a30654be30",
|
|
9724
|
+
"0xd192e819d6ef5218",
|
|
9725
|
+
"0xd69906245565a910",
|
|
9726
|
+
"0xf40e35855771202a",
|
|
9727
|
+
"0x106aa07032bbd1b8",
|
|
9728
|
+
"0x19a4c116b8d2d0c8",
|
|
9729
|
+
"0x1e376c085141ab53",
|
|
9730
|
+
"0x2748774cdf8eeb99",
|
|
9731
|
+
"0x34b0bcb5e19b48a8",
|
|
9732
|
+
"0x391c0cb3c5c95a63",
|
|
9733
|
+
"0x4ed8aa4ae3418acb",
|
|
9734
|
+
"0x5b9cca4f7763e373",
|
|
9735
|
+
"0x682e6ff3d6b2b8a3",
|
|
9736
|
+
"0x748f82ee5defb2fc",
|
|
9737
|
+
"0x78a5636f43172f60",
|
|
9738
|
+
"0x84c87814a1f0ab72",
|
|
9739
|
+
"0x8cc702081a6439ec",
|
|
9740
|
+
"0x90befffa23631e28",
|
|
9741
|
+
"0xa4506cebde82bde9",
|
|
9742
|
+
"0xbef9a3f7b2c67915",
|
|
9743
|
+
"0xc67178f2e372532b",
|
|
9744
|
+
"0xca273eceea26619c",
|
|
9745
|
+
"0xd186b8c721c0c207",
|
|
9746
|
+
"0xeada7dd6cde0eb1e",
|
|
9747
|
+
"0xf57d4f7fee6ed178",
|
|
9748
|
+
"0x06f067aa72176fba",
|
|
9749
|
+
"0x0a637dc5a2c898a6",
|
|
9750
|
+
"0x113f9804bef90dae",
|
|
9751
|
+
"0x1b710b35131c471b",
|
|
9752
|
+
"0x28db77f523047d84",
|
|
9753
|
+
"0x32caab7b40c72493",
|
|
9754
|
+
"0x3c9ebe0a15c9bebc",
|
|
9755
|
+
"0x431d67c49c100d4c",
|
|
9756
|
+
"0x4cc5d4becb3e42b6",
|
|
9757
|
+
"0x597f299cfc657e2a",
|
|
9758
|
+
"0x5fcb6fab3ad6faec",
|
|
9759
|
+
"0x6c44198c4a475817"
|
|
9760
|
+
].map((n) => BigInt(n))))();
|
|
9761
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
9762
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
9763
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
9764
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
9765
|
+
var SHA512 = class extends HashMD {
|
|
9766
|
+
constructor(outputLen = 64) {
|
|
9767
|
+
super(128, outputLen, 16, false);
|
|
9768
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
9769
|
+
this.Al = SHA512_IV[1] | 0;
|
|
9770
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
9771
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
9772
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
9773
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
9774
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
9775
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
9776
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
9777
|
+
this.El = SHA512_IV[9] | 0;
|
|
9778
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
9779
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
9780
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
9781
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
9782
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
9783
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
9784
|
+
}
|
|
9785
|
+
// prettier-ignore
|
|
9786
|
+
get() {
|
|
9787
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
9788
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
9789
|
+
}
|
|
9790
|
+
// prettier-ignore
|
|
9791
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
9792
|
+
this.Ah = Ah | 0;
|
|
9793
|
+
this.Al = Al | 0;
|
|
9794
|
+
this.Bh = Bh | 0;
|
|
9795
|
+
this.Bl = Bl | 0;
|
|
9796
|
+
this.Ch = Ch | 0;
|
|
9797
|
+
this.Cl = Cl | 0;
|
|
9798
|
+
this.Dh = Dh | 0;
|
|
9799
|
+
this.Dl = Dl | 0;
|
|
9800
|
+
this.Eh = Eh | 0;
|
|
9801
|
+
this.El = El | 0;
|
|
9802
|
+
this.Fh = Fh | 0;
|
|
9803
|
+
this.Fl = Fl | 0;
|
|
9804
|
+
this.Gh = Gh | 0;
|
|
9805
|
+
this.Gl = Gl | 0;
|
|
9806
|
+
this.Hh = Hh | 0;
|
|
9807
|
+
this.Hl = Hl | 0;
|
|
9808
|
+
}
|
|
9809
|
+
process(view2, offset) {
|
|
9810
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
9811
|
+
SHA512_W_H[i] = view2.getUint32(offset);
|
|
9812
|
+
SHA512_W_L[i] = view2.getUint32(offset += 4);
|
|
9813
|
+
}
|
|
9814
|
+
for (let i = 16; i < 80; i++) {
|
|
9815
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
9816
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
9817
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
9818
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
9819
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
9820
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
9821
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
9822
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
9823
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
9824
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
9825
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
9826
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
9827
|
+
}
|
|
9828
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
9829
|
+
for (let i = 0; i < 80; i++) {
|
|
9830
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
9831
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
9832
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
9833
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
9834
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
9835
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
9836
|
+
const T1l = T1ll | 0;
|
|
9837
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
9838
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
9839
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
9840
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
9841
|
+
Hh = Gh | 0;
|
|
9842
|
+
Hl = Gl | 0;
|
|
9843
|
+
Gh = Fh | 0;
|
|
9844
|
+
Gl = Fl | 0;
|
|
9845
|
+
Fh = Eh | 0;
|
|
9846
|
+
Fl = El | 0;
|
|
9847
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
9848
|
+
Dh = Ch | 0;
|
|
9849
|
+
Dl = Cl | 0;
|
|
9850
|
+
Ch = Bh | 0;
|
|
9851
|
+
Cl = Bl | 0;
|
|
9852
|
+
Bh = Ah | 0;
|
|
9853
|
+
Bl = Al | 0;
|
|
9854
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
9855
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
9856
|
+
Al = All | 0;
|
|
9857
|
+
}
|
|
9858
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
9859
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
9860
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
9861
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
9862
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
9863
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
9864
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
9865
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
9866
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
9867
|
+
}
|
|
9868
|
+
roundClean() {
|
|
9869
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
9460
9870
|
}
|
|
9461
9871
|
destroy() {
|
|
9462
|
-
this.
|
|
9463
|
-
|
|
9464
|
-
clean(...this.stack);
|
|
9465
|
-
}
|
|
9466
|
-
// Same as b2Compress, but doesn't modify state and returns 16 u32 array (instead of 8)
|
|
9467
|
-
b2CompressOut() {
|
|
9468
|
-
const { state: s, pos, flags, buffer32, bufferOut32: out32 } = this;
|
|
9469
|
-
const { h, l } = fromBig(BigInt(this.chunkOut++));
|
|
9470
|
-
swap32IfBE(buffer32);
|
|
9471
|
-
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(B3_SIGMA, 0, buffer32, 7, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], l, h, pos, flags);
|
|
9472
|
-
out32[0] = v0 ^ v8;
|
|
9473
|
-
out32[1] = v1 ^ v9;
|
|
9474
|
-
out32[2] = v2 ^ v10;
|
|
9475
|
-
out32[3] = v3 ^ v11;
|
|
9476
|
-
out32[4] = v4 ^ v12;
|
|
9477
|
-
out32[5] = v5 ^ v13;
|
|
9478
|
-
out32[6] = v6 ^ v14;
|
|
9479
|
-
out32[7] = v7 ^ v15;
|
|
9480
|
-
out32[8] = s[0] ^ v8;
|
|
9481
|
-
out32[9] = s[1] ^ v9;
|
|
9482
|
-
out32[10] = s[2] ^ v10;
|
|
9483
|
-
out32[11] = s[3] ^ v11;
|
|
9484
|
-
out32[12] = s[4] ^ v12;
|
|
9485
|
-
out32[13] = s[5] ^ v13;
|
|
9486
|
-
out32[14] = s[6] ^ v14;
|
|
9487
|
-
out32[15] = s[7] ^ v15;
|
|
9488
|
-
swap32IfBE(buffer32);
|
|
9489
|
-
swap32IfBE(out32);
|
|
9490
|
-
this.posOut = 0;
|
|
9491
|
-
}
|
|
9492
|
-
finish() {
|
|
9493
|
-
if (this.finished)
|
|
9494
|
-
return;
|
|
9495
|
-
this.finished = true;
|
|
9496
|
-
clean(this.buffer.subarray(this.pos));
|
|
9497
|
-
let flags = this.flags | B3_Flags.ROOT;
|
|
9498
|
-
if (this.stack.length) {
|
|
9499
|
-
flags |= B3_Flags.PARENT;
|
|
9500
|
-
swap32IfBE(this.buffer32);
|
|
9501
|
-
this.compress(this.buffer32, 0, true);
|
|
9502
|
-
swap32IfBE(this.buffer32);
|
|
9503
|
-
this.chunksDone = 0;
|
|
9504
|
-
this.pos = this.blockLen;
|
|
9505
|
-
} else {
|
|
9506
|
-
flags |= (!this.chunkPos ? B3_Flags.CHUNK_START : 0) | B3_Flags.CHUNK_END;
|
|
9507
|
-
}
|
|
9508
|
-
this.flags = flags;
|
|
9509
|
-
this.b2CompressOut();
|
|
9510
|
-
}
|
|
9511
|
-
writeInto(out) {
|
|
9512
|
-
aexists(this, false);
|
|
9513
|
-
abytes(out);
|
|
9514
|
-
this.finish();
|
|
9515
|
-
const { blockLen, bufferOut } = this;
|
|
9516
|
-
for (let pos = 0, len = out.length; pos < len; ) {
|
|
9517
|
-
if (this.posOut >= blockLen)
|
|
9518
|
-
this.b2CompressOut();
|
|
9519
|
-
const take = Math.min(blockLen - this.posOut, len - pos);
|
|
9520
|
-
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
|
9521
|
-
this.posOut += take;
|
|
9522
|
-
pos += take;
|
|
9523
|
-
}
|
|
9524
|
-
return out;
|
|
9872
|
+
clean(this.buffer);
|
|
9873
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
9525
9874
|
}
|
|
9526
|
-
|
|
9527
|
-
|
|
9528
|
-
|
|
9529
|
-
|
|
9875
|
+
};
|
|
9876
|
+
var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
9877
|
+
var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
|
|
9878
|
+
|
|
9879
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha256.js
|
|
9880
|
+
var sha2562 = sha256;
|
|
9881
|
+
|
|
9882
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2.js
|
|
9883
|
+
var BCPV2_HEADER_LENGTH = 68;
|
|
9884
|
+
var BCPV2_SIGNATURE_LENGTH = 64;
|
|
9885
|
+
var BCPV2_MIN_RECORD_LENGTH = BCPV2_HEADER_LENGTH + 32 + BCPV2_SIGNATURE_LENGTH;
|
|
9886
|
+
var BCPV2_MAX_RECORD_LENGTH = 1316;
|
|
9887
|
+
var BCPV2_MAX_DIGESTS = 37;
|
|
9888
|
+
var BCPV2_HASH_ALG_SHA256 = 1;
|
|
9889
|
+
var text = new TextEncoder();
|
|
9890
|
+
var MAGIC = Uint8Array.from([77, 72, 77, 50]);
|
|
9891
|
+
var SIGNING_DOMAIN = text.encode("mcast-manifest-v2\0");
|
|
9892
|
+
function equalBytes(left, right) {
|
|
9893
|
+
if (left.byteLength !== right.byteLength)
|
|
9894
|
+
return false;
|
|
9895
|
+
let different = 0;
|
|
9896
|
+
for (let index = 0; index < left.byteLength; index++)
|
|
9897
|
+
different |= left[index] ^ right[index];
|
|
9898
|
+
return different === 0;
|
|
9899
|
+
}
|
|
9900
|
+
function u16(value) {
|
|
9901
|
+
const result = new Uint8Array(2);
|
|
9902
|
+
new DataView(result.buffer).setUint16(0, value);
|
|
9903
|
+
return result;
|
|
9904
|
+
}
|
|
9905
|
+
function u322(value) {
|
|
9906
|
+
const result = new Uint8Array(4);
|
|
9907
|
+
new DataView(result.buffer).setUint32(0, value);
|
|
9908
|
+
return result;
|
|
9909
|
+
}
|
|
9910
|
+
function validU32(value) {
|
|
9911
|
+
return Number.isInteger(value) && value >= 0 && value <= 4294967295;
|
|
9912
|
+
}
|
|
9913
|
+
function validPort(value) {
|
|
9914
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535;
|
|
9915
|
+
}
|
|
9916
|
+
function parseIpv4(address) {
|
|
9917
|
+
const fields = address.split(".");
|
|
9918
|
+
if (fields.length !== 4 || fields.some((field) => !/^\d+$/.test(field))) {
|
|
9919
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
9920
|
+
}
|
|
9921
|
+
const values = fields.map(Number);
|
|
9922
|
+
if (values.some((value, index) => value > 255 || String(value) !== fields[index])) {
|
|
9923
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
9924
|
+
}
|
|
9925
|
+
return Uint8Array.from(values);
|
|
9926
|
+
}
|
|
9927
|
+
function parseIpv6(address) {
|
|
9928
|
+
const compressed = address.includes("::");
|
|
9929
|
+
if ((address.match(/::/g) ?? []).length > 1)
|
|
9930
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
9931
|
+
const [leftText, rightText = ""] = address.split("::");
|
|
9932
|
+
const left = leftText ? leftText.split(":") : [];
|
|
9933
|
+
const right = rightText ? rightText.split(":") : [];
|
|
9934
|
+
if (left.some((group) => group.includes(".")) || right.some((group) => group.includes("."))) {
|
|
9935
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
9936
|
+
}
|
|
9937
|
+
const missing = 8 - left.length - right.length;
|
|
9938
|
+
if (missing < 0 || !compressed && missing !== 0 || compressed && missing < 1) {
|
|
9939
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
9940
|
+
}
|
|
9941
|
+
const groups = [...left, ...Array.from({ length: missing }, () => "0"), ...right];
|
|
9942
|
+
if (groups.length !== 8 || groups.some((group) => !/^[0-9a-f]{1,4}$/i.test(group))) {
|
|
9943
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
9944
|
+
}
|
|
9945
|
+
const result = new Uint8Array(16);
|
|
9946
|
+
const view2 = new DataView(result.buffer);
|
|
9947
|
+
groups.forEach((group, index) => view2.setUint16(index * 2, Number.parseInt(group, 16)));
|
|
9948
|
+
return result;
|
|
9949
|
+
}
|
|
9950
|
+
function parseBcProvenanceV2Address(address) {
|
|
9951
|
+
if (address.includes(":"))
|
|
9952
|
+
return parseIpv6(address);
|
|
9953
|
+
return parseIpv4(address);
|
|
9954
|
+
}
|
|
9955
|
+
function endpointBytes(endpoint) {
|
|
9956
|
+
if (!validPort(endpoint.port) || !validPort(endpoint.manifestPort) || !validU32(endpoint.streamId)) {
|
|
9957
|
+
throw new Error("invalid BCPV2 endpoint");
|
|
9530
9958
|
}
|
|
9531
|
-
|
|
9532
|
-
|
|
9533
|
-
|
|
9959
|
+
const source = parseBcProvenanceV2Address(endpoint.sourceAddress);
|
|
9960
|
+
const group = parseBcProvenanceV2Address(endpoint.groupAddress);
|
|
9961
|
+
if (source.byteLength !== group.byteLength)
|
|
9962
|
+
throw new Error("endpoint address families differ");
|
|
9963
|
+
return { source, group };
|
|
9964
|
+
}
|
|
9965
|
+
function readU32(view2, offset) {
|
|
9966
|
+
return view2.getUint32(offset);
|
|
9967
|
+
}
|
|
9968
|
+
function readU642(view2, offset) {
|
|
9969
|
+
return view2.getBigUint64(offset);
|
|
9970
|
+
}
|
|
9971
|
+
function decodeBcProvenanceV2Record(input) {
|
|
9972
|
+
const bytes = Uint8Array.from(input);
|
|
9973
|
+
if (bytes.byteLength < BCPV2_MIN_RECORD_LENGTH || bytes.byteLength > BCPV2_MAX_RECORD_LENGTH || (bytes.byteLength - 132) % 32 !== 0) {
|
|
9974
|
+
throw new Error("invalid BCPV2 record length");
|
|
9534
9975
|
}
|
|
9535
|
-
|
|
9536
|
-
|
|
9537
|
-
|
|
9538
|
-
|
|
9539
|
-
|
|
9540
|
-
|
|
9541
|
-
this.destroy();
|
|
9542
|
-
return out;
|
|
9976
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
9977
|
+
if (!equalBytes(bytes.subarray(0, 4), MAGIC))
|
|
9978
|
+
throw new Error("invalid BCPV2 record magic");
|
|
9979
|
+
const count = view2.getUint16(28);
|
|
9980
|
+
if (count < 1 || count > BCPV2_MAX_DIGESTS || bytes.byteLength !== 132 + count * 32) {
|
|
9981
|
+
throw new Error("invalid BCPV2 record count");
|
|
9543
9982
|
}
|
|
9544
|
-
|
|
9545
|
-
|
|
9983
|
+
const body = bytes.slice(0, bytes.byteLength - BCPV2_SIGNATURE_LENGTH);
|
|
9984
|
+
const digests = [];
|
|
9985
|
+
for (let index = 0; index < count; index++) {
|
|
9986
|
+
digests.push(bytes.slice(BCPV2_HEADER_LENGTH + index * 32, BCPV2_HEADER_LENGTH + (index + 1) * 32));
|
|
9546
9987
|
}
|
|
9547
|
-
|
|
9548
|
-
|
|
9549
|
-
|
|
9550
|
-
|
|
9551
|
-
|
|
9552
|
-
|
|
9553
|
-
|
|
9554
|
-
|
|
9555
|
-
|
|
9556
|
-
|
|
9557
|
-
|
|
9558
|
-
|
|
9559
|
-
|
|
9560
|
-
|
|
9561
|
-
if (value.byteLength !== length) {
|
|
9562
|
-
throw new Error(`${name} must be ${length} bytes, got ${value.byteLength}`);
|
|
9563
|
-
}
|
|
9564
|
-
}
|
|
9565
|
-
function requireU32(name, value) {
|
|
9566
|
-
if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
|
|
9567
|
-
throw new Error(`${name} must be an unsigned 32-bit integer`);
|
|
9568
|
-
}
|
|
9569
|
-
}
|
|
9570
|
-
function requireU64(name, value) {
|
|
9571
|
-
if (value < 0n || value > 0xffffffffffffffffn) {
|
|
9572
|
-
throw new Error(`${name} must be an unsigned 64-bit integer`);
|
|
9573
|
-
}
|
|
9574
|
-
}
|
|
9575
|
-
function requireTrueLength(value) {
|
|
9576
|
-
requireU32("trueLength", value);
|
|
9577
|
-
if (value > SOURCE_AUTH_MAX_TRUE_LENGTH) {
|
|
9578
|
-
throw new Error(`trueLength exceeds the ${SOURCE_AUTH_MAX_TRUE_LENGTH}-byte profile maximum`);
|
|
9579
|
-
}
|
|
9580
|
-
}
|
|
9581
|
-
function sourceAuthObjectDigest(object) {
|
|
9582
|
-
return blake3(object);
|
|
9583
|
-
}
|
|
9584
|
-
function sourceAuthLeafHash(groupId, objectId, digest, trueLength) {
|
|
9585
|
-
requireU64("groupId", groupId);
|
|
9586
|
-
requireU32("objectId", objectId);
|
|
9587
|
-
requireBytes("digest", digest, 32);
|
|
9588
|
-
requireTrueLength(trueLength);
|
|
9589
|
-
const preimage = new Uint8Array(49);
|
|
9590
|
-
const view2 = new DataView(preimage.buffer);
|
|
9591
|
-
view2.setBigUint64(1, groupId);
|
|
9592
|
-
view2.setUint32(9, objectId);
|
|
9593
|
-
preimage.set(digest, 13);
|
|
9594
|
-
view2.setUint32(45, trueLength);
|
|
9595
|
-
return blake3(preimage);
|
|
9596
|
-
}
|
|
9597
|
-
function sourceAuthMerkleRoot(entries) {
|
|
9598
|
-
if (entries.length === 0)
|
|
9599
|
-
throw new Error("Merkle tree requires at least one object");
|
|
9600
|
-
if (entries.length > SOURCE_AUTH_MAX_OBJECTS_PER_GROUP) {
|
|
9601
|
-
throw new Error(`group exceeds ${SOURCE_AUTH_MAX_OBJECTS_PER_GROUP} objects`);
|
|
9602
|
-
}
|
|
9603
|
-
const groupId = entries[0].groupId;
|
|
9604
|
-
const objectIds = /* @__PURE__ */ new Set();
|
|
9605
|
-
const ordered = [...entries].sort((left, right) => left.objectId - right.objectId);
|
|
9606
|
-
let level = ordered.map((entry) => {
|
|
9607
|
-
if (entry.groupId !== groupId)
|
|
9608
|
-
throw new Error("Merkle entries must belong to one group");
|
|
9609
|
-
if (objectIds.has(entry.objectId))
|
|
9610
|
-
throw new Error(`duplicate objectId ${entry.objectId}`);
|
|
9611
|
-
objectIds.add(entry.objectId);
|
|
9612
|
-
return sourceAuthLeafHash(entry.groupId, entry.objectId, entry.digest, entry.trueLength);
|
|
9613
|
-
});
|
|
9614
|
-
while (level.length > 1) {
|
|
9615
|
-
const parents = [];
|
|
9616
|
-
for (let index = 0; index < level.length; index += 2) {
|
|
9617
|
-
const left = level[index];
|
|
9618
|
-
const right = level[index + 1] ?? left;
|
|
9619
|
-
const preimage = new Uint8Array(65);
|
|
9620
|
-
preimage[0] = 1;
|
|
9621
|
-
preimage.set(left, 1);
|
|
9622
|
-
preimage.set(right, 33);
|
|
9623
|
-
parents.push(blake3(preimage));
|
|
9624
|
-
}
|
|
9625
|
-
level = parents;
|
|
9626
|
-
}
|
|
9627
|
-
return level[0];
|
|
9628
|
-
}
|
|
9629
|
-
function sourceAuthSignaturePreimage(authScope, groupId, objectCount, root) {
|
|
9630
|
-
requireU32("objectCount", objectCount);
|
|
9631
|
-
if (objectCount === 0 || objectCount > SOURCE_AUTH_MAX_OBJECTS_PER_GROUP) {
|
|
9632
|
-
throw new Error(`objectCount must be between 1 and ${SOURCE_AUTH_MAX_OBJECTS_PER_GROUP}`);
|
|
9633
|
-
}
|
|
9634
|
-
requireU64("groupId", groupId);
|
|
9635
|
-
requireBytes("authScope", authScope, SOURCE_AUTH_SCOPE_LENGTH);
|
|
9636
|
-
requireBytes("root", root, 32);
|
|
9637
|
-
const preimage = new Uint8Array(100);
|
|
9638
|
-
const view2 = new DataView(preimage.buffer);
|
|
9639
|
-
preimage.set(SIGNATURE_DOMAIN);
|
|
9640
|
-
preimage.set(authScope, 24);
|
|
9641
|
-
view2.setBigUint64(56, groupId);
|
|
9642
|
-
view2.setUint32(64, objectCount);
|
|
9643
|
-
preimage.set(root, 68);
|
|
9644
|
-
return preimage;
|
|
9645
|
-
}
|
|
9646
|
-
function decodeSourceAuthManifestFrame(frame) {
|
|
9647
|
-
const frameType = frame[0];
|
|
9648
|
-
const expectedLength = frameType === SOURCE_AUTH_OBJECT_ENTRY_FRAME ? SOURCE_AUTH_OBJECT_ENTRY_LENGTH : frameType === SOURCE_AUTH_GROUP_CLOSE_FRAME ? SOURCE_AUTH_GROUP_CLOSE_LENGTH : 0;
|
|
9649
|
-
if (expectedLength === 0)
|
|
9650
|
-
throw new Error(`unsupported manifest frame type ${frameType}`);
|
|
9651
|
-
if (frame.byteLength !== expectedLength) {
|
|
9652
|
-
throw new Error(`manifest frame has invalid length ${frame.byteLength} for type ${frameType}`);
|
|
9653
|
-
}
|
|
9654
|
-
if (frame[1] !== SOURCE_AUTH_MANIFEST_VERSION) {
|
|
9655
|
-
throw new Error(`unsupported manifest version ${frame[1]}`);
|
|
9656
|
-
}
|
|
9657
|
-
if (frame[2] !== 0 || frame[3] !== 0)
|
|
9658
|
-
throw new Error("manifest reserved bits must be zero");
|
|
9659
|
-
const view2 = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
9660
|
-
const groupId = view2.getBigUint64(4);
|
|
9661
|
-
if (frameType === SOURCE_AUTH_OBJECT_ENTRY_FRAME) {
|
|
9662
|
-
const entry = {
|
|
9663
|
-
kind: "object",
|
|
9664
|
-
groupId,
|
|
9665
|
-
objectId: view2.getUint32(12),
|
|
9666
|
-
trueLength: view2.getUint32(16),
|
|
9667
|
-
digest: frame.slice(20, 52)
|
|
9668
|
-
};
|
|
9669
|
-
requireTrueLength(entry.trueLength);
|
|
9670
|
-
return entry;
|
|
9671
|
-
}
|
|
9672
|
-
const close = {
|
|
9673
|
-
kind: "close",
|
|
9674
|
-
groupId,
|
|
9675
|
-
objectCount: view2.getUint32(12),
|
|
9676
|
-
root: frame.slice(16, 48),
|
|
9677
|
-
signature: frame.slice(48, 112)
|
|
9988
|
+
return {
|
|
9989
|
+
kid: readU32(view2, 4),
|
|
9990
|
+
epoch: readU32(view2, 8),
|
|
9991
|
+
mseq: readU642(view2, 12),
|
|
9992
|
+
firstSeq: readU642(view2, 20),
|
|
9993
|
+
count,
|
|
9994
|
+
hashAlg: bytes[30],
|
|
9995
|
+
flags: bytes[31],
|
|
9996
|
+
sent: readU32(view2, 32),
|
|
9997
|
+
ctx: bytes.slice(36, 68),
|
|
9998
|
+
digests,
|
|
9999
|
+
signature: bytes.slice(bytes.byteLength - BCPV2_SIGNATURE_LENGTH),
|
|
10000
|
+
body,
|
|
10001
|
+
raw: bytes
|
|
9678
10002
|
};
|
|
9679
|
-
|
|
9680
|
-
|
|
9681
|
-
}
|
|
9682
|
-
|
|
10003
|
+
}
|
|
10004
|
+
function bcProvenanceV2SignedMessage(record, endpoint) {
|
|
10005
|
+
const { source, group } = endpointBytes(endpoint);
|
|
10006
|
+
const prefix = new Uint8Array(SIGNING_DOMAIN.byteLength + 4 + source.byteLength + group.byteLength + 2);
|
|
10007
|
+
let offset = 0;
|
|
10008
|
+
prefix.set(SIGNING_DOMAIN, offset);
|
|
10009
|
+
offset += SIGNING_DOMAIN.byteLength;
|
|
10010
|
+
prefix.set(u322(endpoint.streamId), offset);
|
|
10011
|
+
offset += 4;
|
|
10012
|
+
prefix.set(source, offset);
|
|
10013
|
+
offset += source.byteLength;
|
|
10014
|
+
prefix.set(group, offset);
|
|
10015
|
+
offset += group.byteLength;
|
|
10016
|
+
prefix.set(u16(endpoint.manifestPort), offset);
|
|
10017
|
+
const message = new Uint8Array(prefix.byteLength + record.body.byteLength);
|
|
10018
|
+
message.set(prefix);
|
|
10019
|
+
message.set(record.body, prefix.byteLength);
|
|
10020
|
+
return message;
|
|
10021
|
+
}
|
|
10022
|
+
function bcProvenanceV2DatagramDigest(endpoint, payload) {
|
|
10023
|
+
if (payload.byteLength > 65535)
|
|
10024
|
+
throw new Error("datagram payload exceeds u16 length");
|
|
10025
|
+
const { source, group } = endpointBytes(endpoint);
|
|
10026
|
+
const pseudoheader = new Uint8Array(source.byteLength + group.byteLength + 2 + 2 + 2 + 2 + 4);
|
|
10027
|
+
let offset = 0;
|
|
10028
|
+
pseudoheader.set(source, offset);
|
|
10029
|
+
offset += source.byteLength;
|
|
10030
|
+
pseudoheader.set(group, offset);
|
|
10031
|
+
offset += group.byteLength;
|
|
10032
|
+
pseudoheader.set([0, 17], offset);
|
|
10033
|
+
offset += 2;
|
|
10034
|
+
pseudoheader.set(u16(payload.byteLength), offset);
|
|
10035
|
+
offset += 2;
|
|
10036
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
10037
|
+
offset += 2;
|
|
10038
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
10039
|
+
offset += 2;
|
|
10040
|
+
pseudoheader.set(u322(endpoint.streamId), offset);
|
|
10041
|
+
const input = new Uint8Array(pseudoheader.byteLength + payload.byteLength);
|
|
10042
|
+
input.set(pseudoheader);
|
|
10043
|
+
input.set(payload, pseudoheader.byteLength);
|
|
10044
|
+
return sha2562(input);
|
|
9683
10045
|
}
|
|
9684
10046
|
|
|
9685
|
-
// ../mmt-manifest-verify/
|
|
9686
|
-
|
|
9687
|
-
|
|
9688
|
-
|
|
9689
|
-
if (
|
|
9690
|
-
|
|
9691
|
-
|
|
9692
|
-
for (const key of keys) {
|
|
9693
|
-
if (key.firstEpoch <= epoch && (signer === void 0 || key.firstEpoch > signer.firstEpoch))
|
|
9694
|
-
signer = key;
|
|
10047
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/utils.js
|
|
10048
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
10049
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
10050
|
+
function _abool2(value, title = "") {
|
|
10051
|
+
if (typeof value !== "boolean") {
|
|
10052
|
+
const prefix = title && `"${title}"`;
|
|
10053
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
9695
10054
|
}
|
|
9696
|
-
|
|
9697
|
-
return void 0;
|
|
9698
|
-
return signer.publicKey;
|
|
9699
|
-
}
|
|
9700
|
-
function manifestBindingId(trackName, sourceSymbolFormat) {
|
|
9701
|
-
return JSON.stringify([trackName, sourceSymbolFormat]);
|
|
9702
|
-
}
|
|
9703
|
-
function createWebCryptoEd25519Verifier(subtle = globalThis.crypto.subtle) {
|
|
9704
|
-
return async (publicKey, message, signature) => {
|
|
9705
|
-
const keyBytes = Uint8Array.from(publicKey);
|
|
9706
|
-
const messageBytes = Uint8Array.from(message);
|
|
9707
|
-
const signatureBytes = Uint8Array.from(signature);
|
|
9708
|
-
const key = await subtle.importKey("raw", keyBytes.buffer, "Ed25519", false, ["verify"]);
|
|
9709
|
-
return subtle.verify("Ed25519", key, signatureBytes.buffer, messageBytes.buffer);
|
|
9710
|
-
};
|
|
10055
|
+
return value;
|
|
9711
10056
|
}
|
|
9712
|
-
|
|
9713
|
-
|
|
9714
|
-
|
|
9715
|
-
|
|
9716
|
-
|
|
9717
|
-
|
|
9718
|
-
|
|
9719
|
-
|
|
9720
|
-
|
|
10057
|
+
function _abytes2(value, length, title = "") {
|
|
10058
|
+
const bytes = isBytes(value);
|
|
10059
|
+
const len = value?.length;
|
|
10060
|
+
const needsLen = length !== void 0;
|
|
10061
|
+
if (!bytes || needsLen && len !== length) {
|
|
10062
|
+
const prefix = title && `"${title}" `;
|
|
10063
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
10064
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
10065
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
10066
|
+
}
|
|
10067
|
+
return value;
|
|
10068
|
+
}
|
|
10069
|
+
function hexToNumber(hex2) {
|
|
10070
|
+
if (typeof hex2 !== "string")
|
|
10071
|
+
throw new Error("hex string expected, got " + typeof hex2);
|
|
10072
|
+
return hex2 === "" ? _0n : BigInt("0x" + hex2);
|
|
10073
|
+
}
|
|
10074
|
+
function bytesToNumberBE(bytes) {
|
|
10075
|
+
return hexToNumber(bytesToHex(bytes));
|
|
10076
|
+
}
|
|
10077
|
+
function bytesToNumberLE(bytes) {
|
|
10078
|
+
abytes(bytes);
|
|
10079
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
10080
|
+
}
|
|
10081
|
+
function numberToBytesBE(n, len) {
|
|
10082
|
+
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
10083
|
+
}
|
|
10084
|
+
function numberToBytesLE(n, len) {
|
|
10085
|
+
return numberToBytesBE(n, len).reverse();
|
|
10086
|
+
}
|
|
10087
|
+
function ensureBytes(title, hex2, expectedLength) {
|
|
10088
|
+
let res;
|
|
10089
|
+
if (typeof hex2 === "string") {
|
|
10090
|
+
try {
|
|
10091
|
+
res = hexToBytes(hex2);
|
|
10092
|
+
} catch (e) {
|
|
10093
|
+
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
10094
|
+
}
|
|
10095
|
+
} else if (isBytes(hex2)) {
|
|
10096
|
+
res = Uint8Array.from(hex2);
|
|
10097
|
+
} else {
|
|
10098
|
+
throw new Error(title + " must be hex string or Uint8Array");
|
|
10099
|
+
}
|
|
10100
|
+
const len = res.length;
|
|
10101
|
+
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
10102
|
+
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
10103
|
+
return res;
|
|
10104
|
+
}
|
|
10105
|
+
function equalBytes2(a, b) {
|
|
10106
|
+
if (a.length !== b.length)
|
|
9721
10107
|
return false;
|
|
9722
10108
|
let diff = 0;
|
|
9723
|
-
for (let
|
|
9724
|
-
diff |=
|
|
10109
|
+
for (let i = 0; i < a.length; i++)
|
|
10110
|
+
diff |= a[i] ^ b[i];
|
|
9725
10111
|
return diff === 0;
|
|
10112
|
+
}
|
|
10113
|
+
function copyBytes(bytes) {
|
|
10114
|
+
return Uint8Array.from(bytes);
|
|
10115
|
+
}
|
|
10116
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
10117
|
+
function inRange(n, min, max) {
|
|
10118
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
10119
|
+
}
|
|
10120
|
+
function aInRange(title, n, min, max) {
|
|
10121
|
+
if (!inRange(n, min, max))
|
|
10122
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
10123
|
+
}
|
|
10124
|
+
function bitLen(n) {
|
|
10125
|
+
let len;
|
|
10126
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
10127
|
+
;
|
|
10128
|
+
return len;
|
|
10129
|
+
}
|
|
10130
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
10131
|
+
function _validateObject(object, fields, optFields = {}) {
|
|
10132
|
+
if (!object || typeof object !== "object")
|
|
10133
|
+
throw new Error("expected valid options object");
|
|
10134
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
10135
|
+
const val = object[fieldName];
|
|
10136
|
+
if (isOpt && val === void 0)
|
|
10137
|
+
return;
|
|
10138
|
+
const current = typeof val;
|
|
10139
|
+
if (current !== expectedType || val === null)
|
|
10140
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
10141
|
+
}
|
|
10142
|
+
Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));
|
|
10143
|
+
Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));
|
|
10144
|
+
}
|
|
10145
|
+
var notImplemented = () => {
|
|
10146
|
+
throw new Error("not implemented");
|
|
9726
10147
|
};
|
|
9727
|
-
|
|
9728
|
-
|
|
9729
|
-
|
|
9730
|
-
|
|
9731
|
-
|
|
9732
|
-
|
|
9733
|
-
|
|
9734
|
-
|
|
9735
|
-
|
|
9736
|
-
quarantinedBytes: 0
|
|
10148
|
+
function memoized(fn) {
|
|
10149
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
10150
|
+
return (arg, ...args) => {
|
|
10151
|
+
const val = map.get(arg);
|
|
10152
|
+
if (val !== void 0)
|
|
10153
|
+
return val;
|
|
10154
|
+
const computed = fn(arg, ...args);
|
|
10155
|
+
map.set(arg, computed);
|
|
10156
|
+
return computed;
|
|
9737
10157
|
};
|
|
9738
|
-
|
|
9739
|
-
|
|
9740
|
-
|
|
9741
|
-
|
|
9742
|
-
|
|
9743
|
-
|
|
9744
|
-
|
|
9745
|
-
|
|
9746
|
-
|
|
9747
|
-
|
|
9748
|
-
|
|
9749
|
-
|
|
9750
|
-
|
|
9751
|
-
|
|
9752
|
-
|
|
9753
|
-
|
|
9754
|
-
|
|
9755
|
-
|
|
9756
|
-
|
|
9757
|
-
|
|
9758
|
-
|
|
9759
|
-
|
|
9760
|
-
|
|
9761
|
-
|
|
9762
|
-
|
|
9763
|
-
|
|
9764
|
-
|
|
9765
|
-
|
|
9766
|
-
|
|
9767
|
-
|
|
9768
|
-
|
|
9769
|
-
|
|
9770
|
-
|
|
9771
|
-
|
|
9772
|
-
|
|
9773
|
-
|
|
9774
|
-
|
|
9775
|
-
|
|
9776
|
-
|
|
9777
|
-
|
|
9778
|
-
|
|
9779
|
-
|
|
9780
|
-
|
|
9781
|
-
|
|
9782
|
-
|
|
9783
|
-
|
|
9784
|
-
|
|
9785
|
-
|
|
9786
|
-
|
|
9787
|
-
|
|
9788
|
-
|
|
9789
|
-
|
|
9790
|
-
|
|
9791
|
-
|
|
9792
|
-
|
|
9793
|
-
|
|
9794
|
-
|
|
9795
|
-
|
|
9796
|
-
|
|
9797
|
-
|
|
9798
|
-
|
|
9799
|
-
|
|
9800
|
-
|
|
9801
|
-
|
|
9802
|
-
|
|
9803
|
-
|
|
9804
|
-
|
|
9805
|
-
|
|
9806
|
-
|
|
9807
|
-
|
|
9808
|
-
|
|
9809
|
-
|
|
9810
|
-
|
|
9811
|
-
|
|
9812
|
-
|
|
9813
|
-
|
|
9814
|
-
|
|
9815
|
-
|
|
9816
|
-
|
|
9817
|
-
|
|
9818
|
-
|
|
9819
|
-
|
|
9820
|
-
|
|
9821
|
-
|
|
9822
|
-
|
|
9823
|
-
|
|
9824
|
-
|
|
9825
|
-
|
|
9826
|
-
|
|
9827
|
-
|
|
9828
|
-
|
|
9829
|
-
|
|
9830
|
-
|
|
9831
|
-
|
|
9832
|
-
|
|
9833
|
-
|
|
9834
|
-
|
|
9835
|
-
|
|
9836
|
-
|
|
9837
|
-
|
|
9838
|
-
|
|
9839
|
-
|
|
9840
|
-
|
|
9841
|
-
|
|
9842
|
-
|
|
9843
|
-
|
|
9844
|
-
|
|
9845
|
-
|
|
9846
|
-
|
|
9847
|
-
|
|
9848
|
-
|
|
9849
|
-
|
|
9850
|
-
|
|
9851
|
-
|
|
9852
|
-
|
|
9853
|
-
|
|
9854
|
-
if (
|
|
9855
|
-
return
|
|
10158
|
+
}
|
|
10159
|
+
|
|
10160
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/modular.js
|
|
10161
|
+
var _0n2 = BigInt(0);
|
|
10162
|
+
var _1n2 = BigInt(1);
|
|
10163
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
10164
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
10165
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
10166
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
10167
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
10168
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
10169
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
10170
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
10171
|
+
function mod(a, b) {
|
|
10172
|
+
const result = a % b;
|
|
10173
|
+
return result >= _0n2 ? result : b + result;
|
|
10174
|
+
}
|
|
10175
|
+
function pow2(x, power, modulo) {
|
|
10176
|
+
let res = x;
|
|
10177
|
+
while (power-- > _0n2) {
|
|
10178
|
+
res *= res;
|
|
10179
|
+
res %= modulo;
|
|
10180
|
+
}
|
|
10181
|
+
return res;
|
|
10182
|
+
}
|
|
10183
|
+
function invert(number, modulo) {
|
|
10184
|
+
if (number === _0n2)
|
|
10185
|
+
throw new Error("invert: expected non-zero number");
|
|
10186
|
+
if (modulo <= _0n2)
|
|
10187
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
10188
|
+
let a = mod(number, modulo);
|
|
10189
|
+
let b = modulo;
|
|
10190
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
10191
|
+
while (a !== _0n2) {
|
|
10192
|
+
const q = b / a;
|
|
10193
|
+
const r = b % a;
|
|
10194
|
+
const m = x - u * q;
|
|
10195
|
+
const n = y - v * q;
|
|
10196
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
10197
|
+
}
|
|
10198
|
+
const gcd = b;
|
|
10199
|
+
if (gcd !== _1n2)
|
|
10200
|
+
throw new Error("invert: does not exist");
|
|
10201
|
+
return mod(x, modulo);
|
|
10202
|
+
}
|
|
10203
|
+
function assertIsSquare(Fp2, root, n) {
|
|
10204
|
+
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
10205
|
+
throw new Error("Cannot find square root");
|
|
10206
|
+
}
|
|
10207
|
+
function sqrt3mod4(Fp2, n) {
|
|
10208
|
+
const p1div4 = (Fp2.ORDER + _1n2) / _4n;
|
|
10209
|
+
const root = Fp2.pow(n, p1div4);
|
|
10210
|
+
assertIsSquare(Fp2, root, n);
|
|
10211
|
+
return root;
|
|
10212
|
+
}
|
|
10213
|
+
function sqrt5mod8(Fp2, n) {
|
|
10214
|
+
const p5div8 = (Fp2.ORDER - _5n) / _8n;
|
|
10215
|
+
const n2 = Fp2.mul(n, _2n);
|
|
10216
|
+
const v = Fp2.pow(n2, p5div8);
|
|
10217
|
+
const nv = Fp2.mul(n, v);
|
|
10218
|
+
const i = Fp2.mul(Fp2.mul(nv, _2n), v);
|
|
10219
|
+
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
10220
|
+
assertIsSquare(Fp2, root, n);
|
|
10221
|
+
return root;
|
|
10222
|
+
}
|
|
10223
|
+
function sqrt9mod16(P) {
|
|
10224
|
+
const Fp_ = Field(P);
|
|
10225
|
+
const tn = tonelliShanks(P);
|
|
10226
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
10227
|
+
const c2 = tn(Fp_, c1);
|
|
10228
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
10229
|
+
const c4 = (P + _7n) / _16n;
|
|
10230
|
+
return (Fp2, n) => {
|
|
10231
|
+
let tv1 = Fp2.pow(n, c4);
|
|
10232
|
+
let tv2 = Fp2.mul(tv1, c1);
|
|
10233
|
+
const tv3 = Fp2.mul(tv1, c2);
|
|
10234
|
+
const tv4 = Fp2.mul(tv1, c3);
|
|
10235
|
+
const e1 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
10236
|
+
const e2 = Fp2.eql(Fp2.sqr(tv3), n);
|
|
10237
|
+
tv1 = Fp2.cmov(tv1, tv2, e1);
|
|
10238
|
+
tv2 = Fp2.cmov(tv4, tv3, e2);
|
|
10239
|
+
const e3 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
10240
|
+
const root = Fp2.cmov(tv1, tv2, e3);
|
|
10241
|
+
assertIsSquare(Fp2, root, n);
|
|
10242
|
+
return root;
|
|
10243
|
+
};
|
|
10244
|
+
}
|
|
10245
|
+
function tonelliShanks(P) {
|
|
10246
|
+
if (P < _3n)
|
|
10247
|
+
throw new Error("sqrt is not defined for small field");
|
|
10248
|
+
let Q = P - _1n2;
|
|
10249
|
+
let S = 0;
|
|
10250
|
+
while (Q % _2n === _0n2) {
|
|
10251
|
+
Q /= _2n;
|
|
10252
|
+
S++;
|
|
10253
|
+
}
|
|
10254
|
+
let Z = _2n;
|
|
10255
|
+
const _Fp = Field(P);
|
|
10256
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
10257
|
+
if (Z++ > 1e3)
|
|
10258
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
10259
|
+
}
|
|
10260
|
+
if (S === 1)
|
|
10261
|
+
return sqrt3mod4;
|
|
10262
|
+
let cc = _Fp.pow(Z, Q);
|
|
10263
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
10264
|
+
return function tonelliSlow(Fp2, n) {
|
|
10265
|
+
if (Fp2.is0(n))
|
|
10266
|
+
return n;
|
|
10267
|
+
if (FpLegendre(Fp2, n) !== 1)
|
|
10268
|
+
throw new Error("Cannot find square root");
|
|
10269
|
+
let M = S;
|
|
10270
|
+
let c = Fp2.mul(Fp2.ONE, cc);
|
|
10271
|
+
let t = Fp2.pow(n, Q);
|
|
10272
|
+
let R = Fp2.pow(n, Q1div2);
|
|
10273
|
+
while (!Fp2.eql(t, Fp2.ONE)) {
|
|
10274
|
+
if (Fp2.is0(t))
|
|
10275
|
+
return Fp2.ZERO;
|
|
10276
|
+
let i = 1;
|
|
10277
|
+
let t_tmp = Fp2.sqr(t);
|
|
10278
|
+
while (!Fp2.eql(t_tmp, Fp2.ONE)) {
|
|
10279
|
+
i++;
|
|
10280
|
+
t_tmp = Fp2.sqr(t_tmp);
|
|
10281
|
+
if (i === M)
|
|
10282
|
+
throw new Error("Cannot find square root");
|
|
9856
10283
|
}
|
|
9857
|
-
const
|
|
9858
|
-
|
|
9859
|
-
|
|
10284
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
10285
|
+
const b = Fp2.pow(c, exponent);
|
|
10286
|
+
M = i;
|
|
10287
|
+
c = Fp2.sqr(b);
|
|
10288
|
+
t = Fp2.mul(t, c);
|
|
10289
|
+
R = Fp2.mul(R, b);
|
|
10290
|
+
}
|
|
10291
|
+
return R;
|
|
10292
|
+
};
|
|
10293
|
+
}
|
|
10294
|
+
function FpSqrt(P) {
|
|
10295
|
+
if (P % _4n === _3n)
|
|
10296
|
+
return sqrt3mod4;
|
|
10297
|
+
if (P % _8n === _5n)
|
|
10298
|
+
return sqrt5mod8;
|
|
10299
|
+
if (P % _16n === _9n)
|
|
10300
|
+
return sqrt9mod16(P);
|
|
10301
|
+
return tonelliShanks(P);
|
|
10302
|
+
}
|
|
10303
|
+
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
10304
|
+
var FIELD_FIELDS = [
|
|
10305
|
+
"create",
|
|
10306
|
+
"isValid",
|
|
10307
|
+
"is0",
|
|
10308
|
+
"neg",
|
|
10309
|
+
"inv",
|
|
10310
|
+
"sqrt",
|
|
10311
|
+
"sqr",
|
|
10312
|
+
"eql",
|
|
10313
|
+
"add",
|
|
10314
|
+
"sub",
|
|
10315
|
+
"mul",
|
|
10316
|
+
"pow",
|
|
10317
|
+
"div",
|
|
10318
|
+
"addN",
|
|
10319
|
+
"subN",
|
|
10320
|
+
"mulN",
|
|
10321
|
+
"sqrN"
|
|
10322
|
+
];
|
|
10323
|
+
function validateField(field) {
|
|
10324
|
+
const initial = {
|
|
10325
|
+
ORDER: "bigint",
|
|
10326
|
+
MASK: "bigint",
|
|
10327
|
+
BYTES: "number",
|
|
10328
|
+
BITS: "number"
|
|
10329
|
+
};
|
|
10330
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
10331
|
+
map[val] = "function";
|
|
10332
|
+
return map;
|
|
10333
|
+
}, initial);
|
|
10334
|
+
_validateObject(field, opts);
|
|
10335
|
+
return field;
|
|
10336
|
+
}
|
|
10337
|
+
function FpPow(Fp2, num, power) {
|
|
10338
|
+
if (power < _0n2)
|
|
10339
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
10340
|
+
if (power === _0n2)
|
|
10341
|
+
return Fp2.ONE;
|
|
10342
|
+
if (power === _1n2)
|
|
10343
|
+
return num;
|
|
10344
|
+
let p = Fp2.ONE;
|
|
10345
|
+
let d = num;
|
|
10346
|
+
while (power > _0n2) {
|
|
10347
|
+
if (power & _1n2)
|
|
10348
|
+
p = Fp2.mul(p, d);
|
|
10349
|
+
d = Fp2.sqr(d);
|
|
10350
|
+
power >>= _1n2;
|
|
10351
|
+
}
|
|
10352
|
+
return p;
|
|
10353
|
+
}
|
|
10354
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
10355
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
|
|
10356
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
10357
|
+
if (Fp2.is0(num))
|
|
10358
|
+
return acc;
|
|
10359
|
+
inverted[i] = acc;
|
|
10360
|
+
return Fp2.mul(acc, num);
|
|
10361
|
+
}, Fp2.ONE);
|
|
10362
|
+
const invertedAcc = Fp2.inv(multipliedAcc);
|
|
10363
|
+
nums.reduceRight((acc, num, i) => {
|
|
10364
|
+
if (Fp2.is0(num))
|
|
10365
|
+
return acc;
|
|
10366
|
+
inverted[i] = Fp2.mul(acc, inverted[i]);
|
|
10367
|
+
return Fp2.mul(acc, num);
|
|
10368
|
+
}, invertedAcc);
|
|
10369
|
+
return inverted;
|
|
10370
|
+
}
|
|
10371
|
+
function FpLegendre(Fp2, n) {
|
|
10372
|
+
const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
|
|
10373
|
+
const powered = Fp2.pow(n, p1mod2);
|
|
10374
|
+
const yes = Fp2.eql(powered, Fp2.ONE);
|
|
10375
|
+
const zero = Fp2.eql(powered, Fp2.ZERO);
|
|
10376
|
+
const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
|
|
10377
|
+
if (!yes && !zero && !no)
|
|
10378
|
+
throw new Error("invalid Legendre symbol result");
|
|
10379
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
10380
|
+
}
|
|
10381
|
+
function nLength(n, nBitLength) {
|
|
10382
|
+
if (nBitLength !== void 0)
|
|
10383
|
+
anumber(nBitLength);
|
|
10384
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
10385
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
10386
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
10387
|
+
}
|
|
10388
|
+
function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
|
|
10389
|
+
if (ORDER <= _0n2)
|
|
10390
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
10391
|
+
let _nbitLength = void 0;
|
|
10392
|
+
let _sqrt = void 0;
|
|
10393
|
+
let modFromBytes = false;
|
|
10394
|
+
let allowedLengths = void 0;
|
|
10395
|
+
if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
|
|
10396
|
+
if (opts.sqrt || isLE)
|
|
10397
|
+
throw new Error("cannot specify opts in two arguments");
|
|
10398
|
+
const _opts = bitLenOrOpts;
|
|
10399
|
+
if (_opts.BITS)
|
|
10400
|
+
_nbitLength = _opts.BITS;
|
|
10401
|
+
if (_opts.sqrt)
|
|
10402
|
+
_sqrt = _opts.sqrt;
|
|
10403
|
+
if (typeof _opts.isLE === "boolean")
|
|
10404
|
+
isLE = _opts.isLE;
|
|
10405
|
+
if (typeof _opts.modFromBytes === "boolean")
|
|
10406
|
+
modFromBytes = _opts.modFromBytes;
|
|
10407
|
+
allowedLengths = _opts.allowedLengths;
|
|
10408
|
+
} else {
|
|
10409
|
+
if (typeof bitLenOrOpts === "number")
|
|
10410
|
+
_nbitLength = bitLenOrOpts;
|
|
10411
|
+
if (opts.sqrt)
|
|
10412
|
+
_sqrt = opts.sqrt;
|
|
10413
|
+
}
|
|
10414
|
+
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);
|
|
10415
|
+
if (BYTES > 2048)
|
|
10416
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
10417
|
+
let sqrtP;
|
|
10418
|
+
const f = Object.freeze({
|
|
10419
|
+
ORDER,
|
|
10420
|
+
isLE,
|
|
10421
|
+
BITS,
|
|
10422
|
+
BYTES,
|
|
10423
|
+
MASK: bitMask(BITS),
|
|
10424
|
+
ZERO: _0n2,
|
|
10425
|
+
ONE: _1n2,
|
|
10426
|
+
allowedLengths,
|
|
10427
|
+
create: (num) => mod(num, ORDER),
|
|
10428
|
+
isValid: (num) => {
|
|
10429
|
+
if (typeof num !== "bigint")
|
|
10430
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
10431
|
+
return _0n2 <= num && num < ORDER;
|
|
10432
|
+
},
|
|
10433
|
+
is0: (num) => num === _0n2,
|
|
10434
|
+
// is valid and invertible
|
|
10435
|
+
isValidNot0: (num) => !f.is0(num) && f.isValid(num),
|
|
10436
|
+
isOdd: (num) => (num & _1n2) === _1n2,
|
|
10437
|
+
neg: (num) => mod(-num, ORDER),
|
|
10438
|
+
eql: (lhs, rhs) => lhs === rhs,
|
|
10439
|
+
sqr: (num) => mod(num * num, ORDER),
|
|
10440
|
+
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
10441
|
+
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
10442
|
+
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
10443
|
+
pow: (num, power) => FpPow(f, num, power),
|
|
10444
|
+
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
10445
|
+
// Same as above, but doesn't normalize
|
|
10446
|
+
sqrN: (num) => num * num,
|
|
10447
|
+
addN: (lhs, rhs) => lhs + rhs,
|
|
10448
|
+
subN: (lhs, rhs) => lhs - rhs,
|
|
10449
|
+
mulN: (lhs, rhs) => lhs * rhs,
|
|
10450
|
+
inv: (num) => invert(num, ORDER),
|
|
10451
|
+
sqrt: _sqrt || ((n) => {
|
|
10452
|
+
if (!sqrtP)
|
|
10453
|
+
sqrtP = FpSqrt(ORDER);
|
|
10454
|
+
return sqrtP(f, n);
|
|
10455
|
+
}),
|
|
10456
|
+
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
10457
|
+
fromBytes: (bytes, skipValidation = true) => {
|
|
10458
|
+
if (allowedLengths) {
|
|
10459
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
10460
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
10461
|
+
}
|
|
10462
|
+
const padded = new Uint8Array(BYTES);
|
|
10463
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
10464
|
+
bytes = padded;
|
|
9860
10465
|
}
|
|
9861
|
-
|
|
9862
|
-
|
|
9863
|
-
|
|
9864
|
-
if (
|
|
9865
|
-
|
|
9866
|
-
|
|
9867
|
-
|
|
10466
|
+
if (bytes.length !== BYTES)
|
|
10467
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
10468
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
10469
|
+
if (modFromBytes)
|
|
10470
|
+
scalar = mod(scalar, ORDER);
|
|
10471
|
+
if (!skipValidation) {
|
|
10472
|
+
if (!f.isValid(scalar))
|
|
10473
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
9868
10474
|
}
|
|
9869
|
-
|
|
9870
|
-
|
|
10475
|
+
return scalar;
|
|
10476
|
+
},
|
|
10477
|
+
// TODO: we don't need it here, move out to separate fn
|
|
10478
|
+
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
10479
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
10480
|
+
// and it's unclear what to return in there.
|
|
10481
|
+
cmov: (a, b, c) => c ? b : a
|
|
10482
|
+
});
|
|
10483
|
+
return Object.freeze(f);
|
|
10484
|
+
}
|
|
10485
|
+
|
|
10486
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/curve.js
|
|
10487
|
+
var _0n3 = BigInt(0);
|
|
10488
|
+
var _1n3 = BigInt(1);
|
|
10489
|
+
function negateCt(condition, item) {
|
|
10490
|
+
const neg = item.negate();
|
|
10491
|
+
return condition ? neg : item;
|
|
10492
|
+
}
|
|
10493
|
+
function normalizeZ(c, points) {
|
|
10494
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
10495
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
10496
|
+
}
|
|
10497
|
+
function validateW(W, bits) {
|
|
10498
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
10499
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
10500
|
+
}
|
|
10501
|
+
function calcWOpts(W, scalarBits) {
|
|
10502
|
+
validateW(W, scalarBits);
|
|
10503
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
10504
|
+
const windowSize = 2 ** (W - 1);
|
|
10505
|
+
const maxNumber = 2 ** W;
|
|
10506
|
+
const mask = bitMask(W);
|
|
10507
|
+
const shiftBy = BigInt(W);
|
|
10508
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
10509
|
+
}
|
|
10510
|
+
function calcOffsets(n, window, wOpts) {
|
|
10511
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
10512
|
+
let wbits = Number(n & mask);
|
|
10513
|
+
let nextN = n >> shiftBy;
|
|
10514
|
+
if (wbits > windowSize) {
|
|
10515
|
+
wbits -= maxNumber;
|
|
10516
|
+
nextN += _1n3;
|
|
10517
|
+
}
|
|
10518
|
+
const offsetStart = window * windowSize;
|
|
10519
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
10520
|
+
const isZero = wbits === 0;
|
|
10521
|
+
const isNeg = wbits < 0;
|
|
10522
|
+
const isNegF = window % 2 !== 0;
|
|
10523
|
+
const offsetF = offsetStart;
|
|
10524
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
10525
|
+
}
|
|
10526
|
+
function validateMSMPoints(points, c) {
|
|
10527
|
+
if (!Array.isArray(points))
|
|
10528
|
+
throw new Error("array expected");
|
|
10529
|
+
points.forEach((p, i) => {
|
|
10530
|
+
if (!(p instanceof c))
|
|
10531
|
+
throw new Error("invalid point at index " + i);
|
|
10532
|
+
});
|
|
10533
|
+
}
|
|
10534
|
+
function validateMSMScalars(scalars, field) {
|
|
10535
|
+
if (!Array.isArray(scalars))
|
|
10536
|
+
throw new Error("array of scalars expected");
|
|
10537
|
+
scalars.forEach((s, i) => {
|
|
10538
|
+
if (!field.isValid(s))
|
|
10539
|
+
throw new Error("invalid scalar at index " + i);
|
|
10540
|
+
});
|
|
10541
|
+
}
|
|
10542
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
10543
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
10544
|
+
function getW(P) {
|
|
10545
|
+
return pointWindowSizes.get(P) || 1;
|
|
10546
|
+
}
|
|
10547
|
+
function assert0(n) {
|
|
10548
|
+
if (n !== _0n3)
|
|
10549
|
+
throw new Error("invalid wNAF");
|
|
10550
|
+
}
|
|
10551
|
+
var wNAF = class {
|
|
10552
|
+
// Parametrized with a given Point class (not individual point)
|
|
10553
|
+
constructor(Point, bits) {
|
|
10554
|
+
this.BASE = Point.BASE;
|
|
10555
|
+
this.ZERO = Point.ZERO;
|
|
10556
|
+
this.Fn = Point.Fn;
|
|
10557
|
+
this.bits = bits;
|
|
10558
|
+
}
|
|
10559
|
+
// non-const time multiplication ladder
|
|
10560
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
10561
|
+
let d = elm;
|
|
10562
|
+
while (n > _0n3) {
|
|
10563
|
+
if (n & _1n3)
|
|
10564
|
+
p = p.add(d);
|
|
10565
|
+
d = d.double();
|
|
10566
|
+
n >>= _1n3;
|
|
10567
|
+
}
|
|
10568
|
+
return p;
|
|
10569
|
+
}
|
|
10570
|
+
/**
|
|
10571
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
10572
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
10573
|
+
* Number of precomputed points depends on the curve size:
|
|
10574
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
10575
|
+
* - 𝑊 is the window size
|
|
10576
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
10577
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
10578
|
+
* @param point Point instance
|
|
10579
|
+
* @param W window size
|
|
10580
|
+
* @returns precomputed point tables flattened to a single array
|
|
10581
|
+
*/
|
|
10582
|
+
precomputeWindow(point, W) {
|
|
10583
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
10584
|
+
const points = [];
|
|
10585
|
+
let p = point;
|
|
10586
|
+
let base = p;
|
|
10587
|
+
for (let window = 0; window < windows; window++) {
|
|
10588
|
+
base = p;
|
|
10589
|
+
points.push(base);
|
|
10590
|
+
for (let i = 1; i < windowSize; i++) {
|
|
10591
|
+
base = base.add(p);
|
|
10592
|
+
points.push(base);
|
|
9871
10593
|
}
|
|
9872
|
-
|
|
9873
|
-
} catch {
|
|
9874
|
-
release();
|
|
9875
|
-
this.#failure(identity, "verifierError");
|
|
9876
|
-
return { action: "play", verified: false, reason: "verifierError" };
|
|
10594
|
+
p = base.double();
|
|
9877
10595
|
}
|
|
10596
|
+
return points;
|
|
9878
10597
|
}
|
|
9879
|
-
|
|
9880
|
-
|
|
9881
|
-
|
|
9882
|
-
|
|
9883
|
-
|
|
9884
|
-
|
|
9885
|
-
|
|
9886
|
-
|
|
9887
|
-
|
|
9888
|
-
|
|
9889
|
-
|
|
9890
|
-
|
|
9891
|
-
|
|
9892
|
-
|
|
9893
|
-
|
|
9894
|
-
|
|
9895
|
-
|
|
9896
|
-
|
|
9897
|
-
|
|
9898
|
-
const oldest = track.groups.entries().next().value;
|
|
9899
|
-
if (!oldest)
|
|
9900
|
-
break;
|
|
9901
|
-
const [oldestId, oldestGroup] = oldest;
|
|
9902
|
-
for (const pending of [...oldestGroup.pending.values()]) {
|
|
9903
|
-
this.#removePending(track, oldestGroup, pending);
|
|
9904
|
-
this.#giveUpOnPending(pending);
|
|
9905
|
-
this.#failure(pending.identity, "manifestAbsent");
|
|
9906
|
-
}
|
|
9907
|
-
track.manifestEntries -= oldestGroup.entries.size;
|
|
9908
|
-
track.groups.delete(oldestId);
|
|
10598
|
+
/**
|
|
10599
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
10600
|
+
* More compact implementation:
|
|
10601
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
10602
|
+
* @returns real and fake (for const-time) points
|
|
10603
|
+
*/
|
|
10604
|
+
wNAF(W, precomputes, n) {
|
|
10605
|
+
if (!this.Fn.isValid(n))
|
|
10606
|
+
throw new Error("invalid scalar");
|
|
10607
|
+
let p = this.ZERO;
|
|
10608
|
+
let f = this.BASE;
|
|
10609
|
+
const wo = calcWOpts(W, this.bits);
|
|
10610
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
10611
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
10612
|
+
n = nextN;
|
|
10613
|
+
if (isZero) {
|
|
10614
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
10615
|
+
} else {
|
|
10616
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
9909
10617
|
}
|
|
9910
10618
|
}
|
|
9911
|
-
|
|
9912
|
-
|
|
9913
|
-
#validateIdentity(track, identity) {
|
|
9914
|
-
const coordinates = track.context.mapper.derive(Number(identity.groupId), identity.objectId);
|
|
9915
|
-
return identity.ssId === void 0 || coordinates.ssId === identity.ssId;
|
|
10619
|
+
assert0(n);
|
|
10620
|
+
return { p, f };
|
|
9916
10621
|
}
|
|
9917
|
-
|
|
9918
|
-
|
|
9919
|
-
|
|
9920
|
-
|
|
9921
|
-
|
|
9922
|
-
|
|
9923
|
-
|
|
9924
|
-
|
|
9925
|
-
if (
|
|
9926
|
-
|
|
9927
|
-
|
|
10622
|
+
/**
|
|
10623
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
10624
|
+
* @param acc accumulator point to add result of multiplication
|
|
10625
|
+
* @returns point
|
|
10626
|
+
*/
|
|
10627
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
10628
|
+
const wo = calcWOpts(W, this.bits);
|
|
10629
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
10630
|
+
if (n === _0n3)
|
|
10631
|
+
break;
|
|
10632
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
10633
|
+
n = nextN;
|
|
10634
|
+
if (isZero) {
|
|
10635
|
+
continue;
|
|
10636
|
+
} else {
|
|
10637
|
+
const item = precomputes[offset];
|
|
10638
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
9928
10639
|
}
|
|
9929
|
-
|
|
9930
|
-
|
|
10640
|
+
}
|
|
10641
|
+
assert0(n);
|
|
10642
|
+
return acc;
|
|
10643
|
+
}
|
|
10644
|
+
getPrecomputes(W, point, transform) {
|
|
10645
|
+
let comp = pointPrecomputes.get(point);
|
|
10646
|
+
if (!comp) {
|
|
10647
|
+
comp = this.precomputeWindow(point, W);
|
|
10648
|
+
if (W !== 1) {
|
|
10649
|
+
if (typeof transform === "function")
|
|
10650
|
+
comp = transform(comp);
|
|
10651
|
+
pointPrecomputes.set(point, comp);
|
|
9931
10652
|
}
|
|
9932
|
-
return { action: "stall", verified: false };
|
|
9933
10653
|
}
|
|
9934
|
-
|
|
9935
|
-
this.#stats.verified++;
|
|
9936
|
-
return { action: "play", verified: true };
|
|
10654
|
+
return comp;
|
|
9937
10655
|
}
|
|
9938
|
-
|
|
9939
|
-
|
|
9940
|
-
|
|
9941
|
-
console.warn(JSON.stringify({ event: "manifest-enforce-drop", track: identity.trackName, group: String(identity.groupId), object: identity.objectId, reason }));
|
|
9942
|
-
return { action: "drop", verified: false, reason };
|
|
9943
|
-
}
|
|
9944
|
-
release();
|
|
9945
|
-
return { action: "play", verified: false, reason };
|
|
10656
|
+
cached(point, scalar, transform) {
|
|
10657
|
+
const W = getW(point);
|
|
10658
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
9946
10659
|
}
|
|
9947
|
-
|
|
9948
|
-
|
|
9949
|
-
|
|
10660
|
+
unsafe(point, scalar, transform, prev) {
|
|
10661
|
+
const W = getW(point);
|
|
10662
|
+
if (W === 1)
|
|
10663
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
10664
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
9950
10665
|
}
|
|
9951
|
-
|
|
9952
|
-
|
|
9953
|
-
|
|
9954
|
-
|
|
9955
|
-
|
|
9956
|
-
|
|
9957
|
-
|
|
9958
|
-
|
|
9959
|
-
|
|
9960
|
-
|
|
9961
|
-
|
|
9962
|
-
|
|
9963
|
-
|
|
9964
|
-
|
|
9965
|
-
|
|
9966
|
-
|
|
9967
|
-
|
|
9968
|
-
|
|
9969
|
-
|
|
9970
|
-
|
|
9971
|
-
|
|
9972
|
-
|
|
9973
|
-
|
|
9974
|
-
|
|
9975
|
-
|
|
9976
|
-
|
|
9977
|
-
|
|
10666
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
10667
|
+
// using windowed method. This specifies window size and
|
|
10668
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
10669
|
+
createCache(P, W) {
|
|
10670
|
+
validateW(W, this.bits);
|
|
10671
|
+
pointWindowSizes.set(P, W);
|
|
10672
|
+
pointPrecomputes.delete(P);
|
|
10673
|
+
}
|
|
10674
|
+
hasCache(elm) {
|
|
10675
|
+
return getW(elm) !== 1;
|
|
10676
|
+
}
|
|
10677
|
+
};
|
|
10678
|
+
function pippenger(c, fieldN, points, scalars) {
|
|
10679
|
+
validateMSMPoints(points, c);
|
|
10680
|
+
validateMSMScalars(scalars, fieldN);
|
|
10681
|
+
const plength = points.length;
|
|
10682
|
+
const slength = scalars.length;
|
|
10683
|
+
if (plength !== slength)
|
|
10684
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
10685
|
+
const zero = c.ZERO;
|
|
10686
|
+
const wbits = bitLen(BigInt(plength));
|
|
10687
|
+
let windowSize = 1;
|
|
10688
|
+
if (wbits > 12)
|
|
10689
|
+
windowSize = wbits - 3;
|
|
10690
|
+
else if (wbits > 4)
|
|
10691
|
+
windowSize = wbits - 2;
|
|
10692
|
+
else if (wbits > 0)
|
|
10693
|
+
windowSize = 2;
|
|
10694
|
+
const MASK = bitMask(windowSize);
|
|
10695
|
+
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
10696
|
+
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
10697
|
+
let sum = zero;
|
|
10698
|
+
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
10699
|
+
buckets.fill(zero);
|
|
10700
|
+
for (let j = 0; j < slength; j++) {
|
|
10701
|
+
const scalar = scalars[j];
|
|
10702
|
+
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
|
10703
|
+
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
10704
|
+
}
|
|
10705
|
+
let resI = zero;
|
|
10706
|
+
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
10707
|
+
sumI = sumI.add(buckets[j]);
|
|
10708
|
+
resI = resI.add(sumI);
|
|
10709
|
+
}
|
|
10710
|
+
sum = sum.add(resI);
|
|
10711
|
+
if (i !== 0)
|
|
10712
|
+
for (let j = 0; j < windowSize; j++)
|
|
10713
|
+
sum = sum.double();
|
|
10714
|
+
}
|
|
10715
|
+
return sum;
|
|
10716
|
+
}
|
|
10717
|
+
function createField(order, field, isLE) {
|
|
10718
|
+
if (field) {
|
|
10719
|
+
if (field.ORDER !== order)
|
|
10720
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
10721
|
+
validateField(field);
|
|
10722
|
+
return field;
|
|
10723
|
+
} else {
|
|
10724
|
+
return Field(order, { isLE });
|
|
10725
|
+
}
|
|
10726
|
+
}
|
|
10727
|
+
function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
10728
|
+
if (FpFnLE === void 0)
|
|
10729
|
+
FpFnLE = type === "edwards";
|
|
10730
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
10731
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
10732
|
+
for (const p of ["p", "n", "h"]) {
|
|
10733
|
+
const val = CURVE[p];
|
|
10734
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
10735
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
10736
|
+
}
|
|
10737
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
10738
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
10739
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
10740
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
10741
|
+
for (const p of params) {
|
|
10742
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
10743
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
10744
|
+
}
|
|
10745
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
10746
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
10747
|
+
}
|
|
10748
|
+
|
|
10749
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/edwards.js
|
|
10750
|
+
var _0n4 = BigInt(0);
|
|
10751
|
+
var _1n4 = BigInt(1);
|
|
10752
|
+
var _2n2 = BigInt(2);
|
|
10753
|
+
var _8n2 = BigInt(8);
|
|
10754
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
10755
|
+
const x2 = Fp2.sqr(x);
|
|
10756
|
+
const y2 = Fp2.sqr(y);
|
|
10757
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
10758
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
10759
|
+
return Fp2.eql(left, right);
|
|
10760
|
+
}
|
|
10761
|
+
function edwards(params, extraOpts = {}) {
|
|
10762
|
+
const validated = _createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
10763
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
10764
|
+
let CURVE = validated.CURVE;
|
|
10765
|
+
const { h: cofactor } = CURVE;
|
|
10766
|
+
_validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
10767
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
10768
|
+
const modP = (n) => Fp2.create(n);
|
|
10769
|
+
const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
|
|
10770
|
+
try {
|
|
10771
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
10772
|
+
} catch (e) {
|
|
10773
|
+
return { isValid: false, value: _0n4 };
|
|
9978
10774
|
}
|
|
9979
|
-
|
|
9980
|
-
|
|
10775
|
+
});
|
|
10776
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
10777
|
+
throw new Error("bad curve params: generator point");
|
|
10778
|
+
function acoord(title, n, banZero = false) {
|
|
10779
|
+
const min = banZero ? _1n4 : _0n4;
|
|
10780
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
10781
|
+
return n;
|
|
10782
|
+
}
|
|
10783
|
+
function aextpoint(other) {
|
|
10784
|
+
if (!(other instanceof Point))
|
|
10785
|
+
throw new Error("ExtendedPoint expected");
|
|
10786
|
+
}
|
|
10787
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
10788
|
+
const { X, Y, Z } = p;
|
|
10789
|
+
const is0 = p.is0();
|
|
10790
|
+
if (iz == null)
|
|
10791
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
10792
|
+
const x = modP(X * iz);
|
|
10793
|
+
const y = modP(Y * iz);
|
|
10794
|
+
const zz = Fp2.mul(Z, iz);
|
|
10795
|
+
if (is0)
|
|
10796
|
+
return { x: _0n4, y: _1n4 };
|
|
10797
|
+
if (zz !== _1n4)
|
|
10798
|
+
throw new Error("invZ was invalid");
|
|
10799
|
+
return { x, y };
|
|
10800
|
+
});
|
|
10801
|
+
const assertValidMemo = memoized((p) => {
|
|
10802
|
+
const { a, d } = CURVE;
|
|
10803
|
+
if (p.is0())
|
|
10804
|
+
throw new Error("bad point: ZERO");
|
|
10805
|
+
const { X, Y, Z, T } = p;
|
|
10806
|
+
const X2 = modP(X * X);
|
|
10807
|
+
const Y2 = modP(Y * Y);
|
|
10808
|
+
const Z2 = modP(Z * Z);
|
|
10809
|
+
const Z4 = modP(Z2 * Z2);
|
|
10810
|
+
const aX2 = modP(X2 * a);
|
|
10811
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
10812
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
10813
|
+
if (left !== right)
|
|
10814
|
+
throw new Error("bad point: equation left != right (1)");
|
|
10815
|
+
const XY = modP(X * Y);
|
|
10816
|
+
const ZT = modP(Z * T);
|
|
10817
|
+
if (XY !== ZT)
|
|
10818
|
+
throw new Error("bad point: equation left != right (2)");
|
|
10819
|
+
return true;
|
|
10820
|
+
});
|
|
10821
|
+
class Point {
|
|
10822
|
+
constructor(X, Y, Z, T) {
|
|
10823
|
+
this.X = acoord("x", X);
|
|
10824
|
+
this.Y = acoord("y", Y);
|
|
10825
|
+
this.Z = acoord("z", Z, true);
|
|
10826
|
+
this.T = acoord("t", T);
|
|
10827
|
+
Object.freeze(this);
|
|
10828
|
+
}
|
|
10829
|
+
static CURVE() {
|
|
10830
|
+
return CURVE;
|
|
10831
|
+
}
|
|
10832
|
+
static fromAffine(p) {
|
|
10833
|
+
if (p instanceof Point)
|
|
10834
|
+
throw new Error("extended point not allowed");
|
|
10835
|
+
const { x, y } = p || {};
|
|
10836
|
+
acoord("x", x);
|
|
10837
|
+
acoord("y", y);
|
|
10838
|
+
return new Point(x, y, _1n4, modP(x * y));
|
|
10839
|
+
}
|
|
10840
|
+
// Uses algo from RFC8032 5.1.3.
|
|
10841
|
+
static fromBytes(bytes, zip215 = false) {
|
|
10842
|
+
const len = Fp2.BYTES;
|
|
10843
|
+
const { a, d } = CURVE;
|
|
10844
|
+
bytes = copyBytes(_abytes2(bytes, len, "point"));
|
|
10845
|
+
_abool2(zip215, "zip215");
|
|
10846
|
+
const normed = copyBytes(bytes);
|
|
10847
|
+
const lastByte = bytes[len - 1];
|
|
10848
|
+
normed[len - 1] = lastByte & ~128;
|
|
10849
|
+
const y = bytesToNumberLE(normed);
|
|
10850
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
10851
|
+
aInRange("point.y", y, _0n4, max);
|
|
10852
|
+
const y2 = modP(y * y);
|
|
10853
|
+
const u = modP(y2 - _1n4);
|
|
10854
|
+
const v = modP(d * y2 - a);
|
|
10855
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
10856
|
+
if (!isValid)
|
|
10857
|
+
throw new Error("bad point: invalid y coordinate");
|
|
10858
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
10859
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
10860
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
10861
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
10862
|
+
if (isLastByteOdd !== isXOdd)
|
|
10863
|
+
x = modP(-x);
|
|
10864
|
+
return Point.fromAffine({ x, y });
|
|
10865
|
+
}
|
|
10866
|
+
static fromHex(bytes, zip215 = false) {
|
|
10867
|
+
return Point.fromBytes(ensureBytes("point", bytes), zip215);
|
|
10868
|
+
}
|
|
10869
|
+
get x() {
|
|
10870
|
+
return this.toAffine().x;
|
|
10871
|
+
}
|
|
10872
|
+
get y() {
|
|
10873
|
+
return this.toAffine().y;
|
|
10874
|
+
}
|
|
10875
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
10876
|
+
wnaf.createCache(this, windowSize);
|
|
10877
|
+
if (!isLazy)
|
|
10878
|
+
this.multiply(_2n2);
|
|
10879
|
+
return this;
|
|
10880
|
+
}
|
|
10881
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
10882
|
+
assertValidity() {
|
|
10883
|
+
assertValidMemo(this);
|
|
10884
|
+
}
|
|
10885
|
+
// Compare one point to another.
|
|
10886
|
+
equals(other) {
|
|
10887
|
+
aextpoint(other);
|
|
10888
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
10889
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
10890
|
+
const X1Z2 = modP(X1 * Z2);
|
|
10891
|
+
const X2Z1 = modP(X2 * Z1);
|
|
10892
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
10893
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
10894
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
10895
|
+
}
|
|
10896
|
+
is0() {
|
|
10897
|
+
return this.equals(Point.ZERO);
|
|
10898
|
+
}
|
|
10899
|
+
negate() {
|
|
10900
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
10901
|
+
}
|
|
10902
|
+
// Fast algo for doubling Extended Point.
|
|
10903
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
10904
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
10905
|
+
double() {
|
|
10906
|
+
const { a } = CURVE;
|
|
10907
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
10908
|
+
const A = modP(X1 * X1);
|
|
10909
|
+
const B = modP(Y1 * Y1);
|
|
10910
|
+
const C = modP(_2n2 * modP(Z1 * Z1));
|
|
10911
|
+
const D = modP(a * A);
|
|
10912
|
+
const x1y1 = X1 + Y1;
|
|
10913
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
10914
|
+
const G = D + B;
|
|
10915
|
+
const F = G - C;
|
|
10916
|
+
const H = D - B;
|
|
10917
|
+
const X3 = modP(E * F);
|
|
10918
|
+
const Y3 = modP(G * H);
|
|
10919
|
+
const T3 = modP(E * H);
|
|
10920
|
+
const Z3 = modP(F * G);
|
|
10921
|
+
return new Point(X3, Y3, Z3, T3);
|
|
10922
|
+
}
|
|
10923
|
+
// Fast algo for adding 2 Extended Points.
|
|
10924
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
10925
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
10926
|
+
add(other) {
|
|
10927
|
+
aextpoint(other);
|
|
10928
|
+
const { a, d } = CURVE;
|
|
10929
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
10930
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
10931
|
+
const A = modP(X1 * X2);
|
|
10932
|
+
const B = modP(Y1 * Y2);
|
|
10933
|
+
const C = modP(T1 * d * T2);
|
|
10934
|
+
const D = modP(Z1 * Z2);
|
|
10935
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
10936
|
+
const F = D - C;
|
|
10937
|
+
const G = D + C;
|
|
10938
|
+
const H = modP(B - a * A);
|
|
10939
|
+
const X3 = modP(E * F);
|
|
10940
|
+
const Y3 = modP(G * H);
|
|
10941
|
+
const T3 = modP(E * H);
|
|
10942
|
+
const Z3 = modP(F * G);
|
|
10943
|
+
return new Point(X3, Y3, Z3, T3);
|
|
10944
|
+
}
|
|
10945
|
+
subtract(other) {
|
|
10946
|
+
return this.add(other.negate());
|
|
10947
|
+
}
|
|
10948
|
+
// Constant-time multiplication.
|
|
10949
|
+
multiply(scalar) {
|
|
10950
|
+
if (!Fn2.isValidNot0(scalar))
|
|
10951
|
+
throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
10952
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
|
|
10953
|
+
return normalizeZ(Point, [p, f])[0];
|
|
10954
|
+
}
|
|
10955
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
10956
|
+
// It's faster, but should only be used when you don't care about
|
|
10957
|
+
// an exposed private key e.g. sig verification.
|
|
10958
|
+
// Does NOT allow scalars higher than CURVE.n.
|
|
10959
|
+
// Accepts optional accumulator to merge with multiply (important for sparse scalars)
|
|
10960
|
+
multiplyUnsafe(scalar, acc = Point.ZERO) {
|
|
10961
|
+
if (!Fn2.isValid(scalar))
|
|
10962
|
+
throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
10963
|
+
if (scalar === _0n4)
|
|
10964
|
+
return Point.ZERO;
|
|
10965
|
+
if (this.is0() || scalar === _1n4)
|
|
10966
|
+
return this;
|
|
10967
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
|
|
10968
|
+
}
|
|
10969
|
+
// Checks if point is of small order.
|
|
10970
|
+
// If you add something to small order point, you will have "dirty"
|
|
10971
|
+
// point with torsion component.
|
|
10972
|
+
// Multiplies point by cofactor and checks if the result is 0.
|
|
10973
|
+
isSmallOrder() {
|
|
10974
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
10975
|
+
}
|
|
10976
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
10977
|
+
// Returns `false` is the point is dirty.
|
|
10978
|
+
isTorsionFree() {
|
|
10979
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
10980
|
+
}
|
|
10981
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
10982
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
10983
|
+
toAffine(invertedZ) {
|
|
10984
|
+
return toAffineMemo(this, invertedZ);
|
|
10985
|
+
}
|
|
10986
|
+
clearCofactor() {
|
|
10987
|
+
if (cofactor === _1n4)
|
|
10988
|
+
return this;
|
|
10989
|
+
return this.multiplyUnsafe(cofactor);
|
|
10990
|
+
}
|
|
10991
|
+
toBytes() {
|
|
10992
|
+
const { x, y } = this.toAffine();
|
|
10993
|
+
const bytes = Fp2.toBytes(y);
|
|
10994
|
+
bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
|
|
10995
|
+
return bytes;
|
|
10996
|
+
}
|
|
10997
|
+
toHex() {
|
|
10998
|
+
return bytesToHex(this.toBytes());
|
|
10999
|
+
}
|
|
11000
|
+
toString() {
|
|
11001
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
11002
|
+
}
|
|
11003
|
+
// TODO: remove
|
|
11004
|
+
get ex() {
|
|
11005
|
+
return this.X;
|
|
11006
|
+
}
|
|
11007
|
+
get ey() {
|
|
11008
|
+
return this.Y;
|
|
11009
|
+
}
|
|
11010
|
+
get ez() {
|
|
11011
|
+
return this.Z;
|
|
11012
|
+
}
|
|
11013
|
+
get et() {
|
|
11014
|
+
return this.T;
|
|
11015
|
+
}
|
|
11016
|
+
static normalizeZ(points) {
|
|
11017
|
+
return normalizeZ(Point, points);
|
|
11018
|
+
}
|
|
11019
|
+
static msm(points, scalars) {
|
|
11020
|
+
return pippenger(Point, Fn2, points, scalars);
|
|
11021
|
+
}
|
|
11022
|
+
_setWindowSize(windowSize) {
|
|
11023
|
+
this.precompute(windowSize);
|
|
11024
|
+
}
|
|
11025
|
+
toRawBytes() {
|
|
11026
|
+
return this.toBytes();
|
|
11027
|
+
}
|
|
11028
|
+
}
|
|
11029
|
+
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
11030
|
+
Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
11031
|
+
Point.Fp = Fp2;
|
|
11032
|
+
Point.Fn = Fn2;
|
|
11033
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
11034
|
+
Point.BASE.precompute(8);
|
|
11035
|
+
return Point;
|
|
11036
|
+
}
|
|
11037
|
+
var PrimeEdwardsPoint = class {
|
|
11038
|
+
constructor(ep) {
|
|
11039
|
+
this.ep = ep;
|
|
11040
|
+
}
|
|
11041
|
+
// Static methods that must be implemented by subclasses
|
|
11042
|
+
static fromBytes(_bytes) {
|
|
11043
|
+
notImplemented();
|
|
11044
|
+
}
|
|
11045
|
+
static fromHex(_hex) {
|
|
11046
|
+
notImplemented();
|
|
11047
|
+
}
|
|
11048
|
+
get x() {
|
|
11049
|
+
return this.toAffine().x;
|
|
11050
|
+
}
|
|
11051
|
+
get y() {
|
|
11052
|
+
return this.toAffine().y;
|
|
11053
|
+
}
|
|
11054
|
+
// Common implementations
|
|
11055
|
+
clearCofactor() {
|
|
11056
|
+
return this;
|
|
11057
|
+
}
|
|
11058
|
+
assertValidity() {
|
|
11059
|
+
this.ep.assertValidity();
|
|
11060
|
+
}
|
|
11061
|
+
toAffine(invertedZ) {
|
|
11062
|
+
return this.ep.toAffine(invertedZ);
|
|
11063
|
+
}
|
|
11064
|
+
toHex() {
|
|
11065
|
+
return bytesToHex(this.toBytes());
|
|
11066
|
+
}
|
|
11067
|
+
toString() {
|
|
11068
|
+
return this.toHex();
|
|
11069
|
+
}
|
|
11070
|
+
isTorsionFree() {
|
|
11071
|
+
return true;
|
|
11072
|
+
}
|
|
11073
|
+
isSmallOrder() {
|
|
11074
|
+
return false;
|
|
11075
|
+
}
|
|
11076
|
+
add(other) {
|
|
11077
|
+
this.assertSame(other);
|
|
11078
|
+
return this.init(this.ep.add(other.ep));
|
|
11079
|
+
}
|
|
11080
|
+
subtract(other) {
|
|
11081
|
+
this.assertSame(other);
|
|
11082
|
+
return this.init(this.ep.subtract(other.ep));
|
|
11083
|
+
}
|
|
11084
|
+
multiply(scalar) {
|
|
11085
|
+
return this.init(this.ep.multiply(scalar));
|
|
11086
|
+
}
|
|
11087
|
+
multiplyUnsafe(scalar) {
|
|
11088
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
11089
|
+
}
|
|
11090
|
+
double() {
|
|
11091
|
+
return this.init(this.ep.double());
|
|
11092
|
+
}
|
|
11093
|
+
negate() {
|
|
11094
|
+
return this.init(this.ep.negate());
|
|
11095
|
+
}
|
|
11096
|
+
precompute(windowSize, isLazy) {
|
|
11097
|
+
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
11098
|
+
}
|
|
11099
|
+
/** @deprecated use `toBytes` */
|
|
11100
|
+
toRawBytes() {
|
|
11101
|
+
return this.toBytes();
|
|
11102
|
+
}
|
|
11103
|
+
};
|
|
11104
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
11105
|
+
if (typeof cHash !== "function")
|
|
11106
|
+
throw new Error('"hash" function param is required');
|
|
11107
|
+
_validateObject(eddsaOpts, {}, {
|
|
11108
|
+
adjustScalarBytes: "function",
|
|
11109
|
+
randomBytes: "function",
|
|
11110
|
+
domain: "function",
|
|
11111
|
+
prehash: "function",
|
|
11112
|
+
mapToCurve: "function"
|
|
11113
|
+
});
|
|
11114
|
+
const { prehash } = eddsaOpts;
|
|
11115
|
+
const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
|
|
11116
|
+
const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
|
|
11117
|
+
const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
|
|
11118
|
+
const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
|
|
11119
|
+
_abool2(phflag, "phflag");
|
|
11120
|
+
if (ctx.length || phflag)
|
|
11121
|
+
throw new Error("Contexts/pre-hash are not supported");
|
|
11122
|
+
return data;
|
|
11123
|
+
});
|
|
11124
|
+
function modN_LE(hash) {
|
|
11125
|
+
return Fn2.create(bytesToNumberLE(hash));
|
|
11126
|
+
}
|
|
11127
|
+
function getPrivateScalar(key) {
|
|
11128
|
+
const len = lengths.secretKey;
|
|
11129
|
+
key = ensureBytes("private key", key, len);
|
|
11130
|
+
const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
|
|
11131
|
+
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
11132
|
+
const prefix = hashed.slice(len, 2 * len);
|
|
11133
|
+
const scalar = modN_LE(head);
|
|
11134
|
+
return { head, prefix, scalar };
|
|
11135
|
+
}
|
|
11136
|
+
function getExtendedPublicKey(secretKey) {
|
|
11137
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
11138
|
+
const point = BASE.multiply(scalar);
|
|
11139
|
+
const pointBytes = point.toBytes();
|
|
11140
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
11141
|
+
}
|
|
11142
|
+
function getPublicKey(secretKey) {
|
|
11143
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
11144
|
+
}
|
|
11145
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
11146
|
+
const msg = concatBytes(...msgs);
|
|
11147
|
+
return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
|
|
11148
|
+
}
|
|
11149
|
+
function sign(msg, secretKey, options = {}) {
|
|
11150
|
+
msg = ensureBytes("message", msg);
|
|
11151
|
+
if (prehash)
|
|
11152
|
+
msg = prehash(msg);
|
|
11153
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
11154
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
11155
|
+
const R = BASE.multiply(r).toBytes();
|
|
11156
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
11157
|
+
const s = Fn2.create(r + k * scalar);
|
|
11158
|
+
if (!Fn2.isValid(s))
|
|
11159
|
+
throw new Error("sign failed: invalid s");
|
|
11160
|
+
const rs = concatBytes(R, Fn2.toBytes(s));
|
|
11161
|
+
return _abytes2(rs, lengths.signature, "result");
|
|
11162
|
+
}
|
|
11163
|
+
const verifyOpts = { zip215: true };
|
|
11164
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
11165
|
+
const { context, zip215 } = options;
|
|
11166
|
+
const len = lengths.signature;
|
|
11167
|
+
sig = ensureBytes("signature", sig, len);
|
|
11168
|
+
msg = ensureBytes("message", msg);
|
|
11169
|
+
publicKey = ensureBytes("publicKey", publicKey, lengths.publicKey);
|
|
11170
|
+
if (zip215 !== void 0)
|
|
11171
|
+
_abool2(zip215, "zip215");
|
|
11172
|
+
if (prehash)
|
|
11173
|
+
msg = prehash(msg);
|
|
11174
|
+
const mid = len / 2;
|
|
11175
|
+
const r = sig.subarray(0, mid);
|
|
11176
|
+
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
11177
|
+
let A, R, SB;
|
|
9981
11178
|
try {
|
|
9982
|
-
|
|
9983
|
-
|
|
9984
|
-
|
|
9985
|
-
|
|
11179
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
11180
|
+
R = Point.fromBytes(r, zip215);
|
|
11181
|
+
SB = BASE.multiplyUnsafe(s);
|
|
11182
|
+
} catch (error) {
|
|
11183
|
+
return false;
|
|
9986
11184
|
}
|
|
9987
|
-
if (
|
|
9988
|
-
|
|
9989
|
-
|
|
9990
|
-
|
|
9991
|
-
|
|
9992
|
-
|
|
9993
|
-
|
|
9994
|
-
|
|
9995
|
-
|
|
9996
|
-
|
|
9997
|
-
|
|
9998
|
-
|
|
9999
|
-
|
|
10000
|
-
|
|
11185
|
+
if (!zip215 && A.isSmallOrder())
|
|
11186
|
+
return false;
|
|
11187
|
+
const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
|
|
11188
|
+
const RkA = R.add(A.multiplyUnsafe(k));
|
|
11189
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
11190
|
+
}
|
|
11191
|
+
const _size = Fp2.BYTES;
|
|
11192
|
+
const lengths = {
|
|
11193
|
+
secretKey: _size,
|
|
11194
|
+
publicKey: _size,
|
|
11195
|
+
signature: 2 * _size,
|
|
11196
|
+
seed: _size
|
|
11197
|
+
};
|
|
11198
|
+
function randomSecretKey(seed = randomBytes2(lengths.seed)) {
|
|
11199
|
+
return _abytes2(seed, lengths.seed, "seed");
|
|
11200
|
+
}
|
|
11201
|
+
function keygen(seed) {
|
|
11202
|
+
const secretKey = utils.randomSecretKey(seed);
|
|
11203
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
11204
|
+
}
|
|
11205
|
+
function isValidSecretKey(key) {
|
|
11206
|
+
return isBytes(key) && key.length === Fn2.BYTES;
|
|
11207
|
+
}
|
|
11208
|
+
function isValidPublicKey(key, zip215) {
|
|
11209
|
+
try {
|
|
11210
|
+
return !!Point.fromBytes(key, zip215);
|
|
11211
|
+
} catch (error) {
|
|
11212
|
+
return false;
|
|
10001
11213
|
}
|
|
10002
|
-
this.#finishGroupSignature(track, groupId, group, close, signatureValid);
|
|
10003
11214
|
}
|
|
10004
|
-
|
|
10005
|
-
|
|
10006
|
-
|
|
10007
|
-
|
|
10008
|
-
|
|
11215
|
+
const utils = {
|
|
11216
|
+
getExtendedPublicKey,
|
|
11217
|
+
randomSecretKey,
|
|
11218
|
+
isValidSecretKey,
|
|
11219
|
+
isValidPublicKey,
|
|
11220
|
+
/**
|
|
11221
|
+
* Converts ed public key to x public key. Uses formula:
|
|
11222
|
+
* - ed25519:
|
|
11223
|
+
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
11224
|
+
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
11225
|
+
* - ed448:
|
|
11226
|
+
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
11227
|
+
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
11228
|
+
*/
|
|
11229
|
+
toMontgomery(publicKey) {
|
|
11230
|
+
const { y } = Point.fromBytes(publicKey);
|
|
11231
|
+
const size = lengths.publicKey;
|
|
11232
|
+
const is25519 = size === 32;
|
|
11233
|
+
if (!is25519 && size !== 57)
|
|
11234
|
+
throw new Error("only defined for 25519 and 448");
|
|
11235
|
+
const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
|
|
11236
|
+
return Fp2.toBytes(u);
|
|
11237
|
+
},
|
|
11238
|
+
toMontgomerySecret(secretKey) {
|
|
11239
|
+
const size = lengths.secretKey;
|
|
11240
|
+
_abytes2(secretKey, size);
|
|
11241
|
+
const hashed = cHash(secretKey.subarray(0, size));
|
|
11242
|
+
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
11243
|
+
},
|
|
11244
|
+
/** @deprecated */
|
|
11245
|
+
randomPrivateKey: randomSecretKey,
|
|
11246
|
+
/** @deprecated */
|
|
11247
|
+
precompute(windowSize = 8, point = Point.BASE) {
|
|
11248
|
+
return point.precompute(windowSize, false);
|
|
10009
11249
|
}
|
|
10010
|
-
|
|
10011
|
-
|
|
10012
|
-
|
|
10013
|
-
|
|
10014
|
-
|
|
10015
|
-
|
|
10016
|
-
|
|
10017
|
-
|
|
10018
|
-
|
|
10019
|
-
|
|
10020
|
-
|
|
10021
|
-
|
|
10022
|
-
|
|
11250
|
+
};
|
|
11251
|
+
return Object.freeze({
|
|
11252
|
+
keygen,
|
|
11253
|
+
getPublicKey,
|
|
11254
|
+
sign,
|
|
11255
|
+
verify,
|
|
11256
|
+
utils,
|
|
11257
|
+
Point,
|
|
11258
|
+
lengths
|
|
11259
|
+
});
|
|
11260
|
+
}
|
|
11261
|
+
function _eddsa_legacy_opts_to_new(c) {
|
|
11262
|
+
const CURVE = {
|
|
11263
|
+
a: c.a,
|
|
11264
|
+
d: c.d,
|
|
11265
|
+
p: c.Fp.ORDER,
|
|
11266
|
+
n: c.n,
|
|
11267
|
+
h: c.h,
|
|
11268
|
+
Gx: c.Gx,
|
|
11269
|
+
Gy: c.Gy
|
|
11270
|
+
};
|
|
11271
|
+
const Fp2 = c.Fp;
|
|
11272
|
+
const Fn2 = Field(CURVE.n, c.nBitLength, true);
|
|
11273
|
+
const curveOpts = { Fp: Fp2, Fn: Fn2, uvRatio: c.uvRatio };
|
|
11274
|
+
const eddsaOpts = {
|
|
11275
|
+
randomBytes: c.randomBytes,
|
|
11276
|
+
adjustScalarBytes: c.adjustScalarBytes,
|
|
11277
|
+
domain: c.domain,
|
|
11278
|
+
prehash: c.prehash,
|
|
11279
|
+
mapToCurve: c.mapToCurve
|
|
11280
|
+
};
|
|
11281
|
+
return { CURVE, curveOpts, hash: c.hash, eddsaOpts };
|
|
11282
|
+
}
|
|
11283
|
+
function _eddsa_new_output_to_legacy(c, eddsa2) {
|
|
11284
|
+
const Point = eddsa2.Point;
|
|
11285
|
+
const legacy = Object.assign({}, eddsa2, {
|
|
11286
|
+
ExtendedPoint: Point,
|
|
11287
|
+
CURVE: c,
|
|
11288
|
+
nBitLength: Point.Fn.BITS,
|
|
11289
|
+
nByteLength: Point.Fn.BYTES
|
|
11290
|
+
});
|
|
11291
|
+
return legacy;
|
|
11292
|
+
}
|
|
11293
|
+
function twistedEdwards(c) {
|
|
11294
|
+
const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
|
|
11295
|
+
const Point = edwards(CURVE, curveOpts);
|
|
11296
|
+
const EDDSA = eddsa(Point, hash, eddsaOpts);
|
|
11297
|
+
return _eddsa_new_output_to_legacy(c, EDDSA);
|
|
11298
|
+
}
|
|
11299
|
+
|
|
11300
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/ed25519.js
|
|
11301
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
11302
|
+
var _1n5 = BigInt(1);
|
|
11303
|
+
var _2n3 = BigInt(2);
|
|
11304
|
+
var _3n2 = BigInt(3);
|
|
11305
|
+
var _5n2 = BigInt(5);
|
|
11306
|
+
var _8n3 = BigInt(8);
|
|
11307
|
+
var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
11308
|
+
var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
11309
|
+
p: ed25519_CURVE_p,
|
|
11310
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
11311
|
+
h: _8n3,
|
|
11312
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
11313
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
11314
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
11315
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
11316
|
+
}))();
|
|
11317
|
+
function ed25519_pow_2_252_3(x) {
|
|
11318
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
11319
|
+
const P = ed25519_CURVE_p;
|
|
11320
|
+
const x2 = x * x % P;
|
|
11321
|
+
const b2 = x2 * x % P;
|
|
11322
|
+
const b4 = pow2(b2, _2n3, P) * b2 % P;
|
|
11323
|
+
const b5 = pow2(b4, _1n5, P) * x % P;
|
|
11324
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
11325
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
11326
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
11327
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
11328
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
11329
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
11330
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
11331
|
+
const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
|
|
11332
|
+
return { pow_p_5_8, b2 };
|
|
11333
|
+
}
|
|
11334
|
+
function adjustScalarBytes(bytes) {
|
|
11335
|
+
bytes[0] &= 248;
|
|
11336
|
+
bytes[31] &= 127;
|
|
11337
|
+
bytes[31] |= 64;
|
|
11338
|
+
return bytes;
|
|
11339
|
+
}
|
|
11340
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
11341
|
+
function uvRatio(u, v) {
|
|
11342
|
+
const P = ed25519_CURVE_p;
|
|
11343
|
+
const v3 = mod(v * v * v, P);
|
|
11344
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
11345
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
11346
|
+
let x = mod(u * v3 * pow, P);
|
|
11347
|
+
const vx2 = mod(v * x * x, P);
|
|
11348
|
+
const root1 = x;
|
|
11349
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
11350
|
+
const useRoot1 = vx2 === u;
|
|
11351
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
11352
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
11353
|
+
if (useRoot1)
|
|
11354
|
+
x = root1;
|
|
11355
|
+
if (useRoot2 || noRoot)
|
|
11356
|
+
x = root2;
|
|
11357
|
+
if (isNegativeLE(x, P))
|
|
11358
|
+
x = mod(-x, P);
|
|
11359
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
11360
|
+
}
|
|
11361
|
+
var Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
|
|
11362
|
+
var Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
|
|
11363
|
+
var ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
11364
|
+
...ed25519_CURVE,
|
|
11365
|
+
Fp,
|
|
11366
|
+
hash: sha512,
|
|
11367
|
+
adjustScalarBytes,
|
|
11368
|
+
// dom2
|
|
11369
|
+
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
11370
|
+
// Constant-time, u/√v
|
|
11371
|
+
uvRatio
|
|
11372
|
+
}))();
|
|
11373
|
+
var ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
|
11374
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
11375
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
11376
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
11377
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
11378
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
11379
|
+
var invertSqrt = (number) => uvRatio(_1n5, number);
|
|
11380
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
11381
|
+
var bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
11382
|
+
function calcElligatorRistrettoMap(r0) {
|
|
11383
|
+
const { d } = ed25519_CURVE;
|
|
11384
|
+
const P = ed25519_CURVE_p;
|
|
11385
|
+
const mod2 = (n) => Fp.create(n);
|
|
11386
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
11387
|
+
const Ns = mod2((r + _1n5) * ONE_MINUS_D_SQ);
|
|
11388
|
+
let c = BigInt(-1);
|
|
11389
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
11390
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
11391
|
+
let s_ = mod2(s * r0);
|
|
11392
|
+
if (!isNegativeLE(s_, P))
|
|
11393
|
+
s_ = mod2(-s_);
|
|
11394
|
+
if (!Ns_D_is_sq)
|
|
11395
|
+
s = s_;
|
|
11396
|
+
if (!Ns_D_is_sq)
|
|
11397
|
+
c = r;
|
|
11398
|
+
const Nt = mod2(c * (r - _1n5) * D_MINUS_ONE_SQ - D);
|
|
11399
|
+
const s2 = s * s;
|
|
11400
|
+
const W0 = mod2((s + s) * D);
|
|
11401
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
11402
|
+
const W2 = mod2(_1n5 - s2);
|
|
11403
|
+
const W3 = mod2(_1n5 + s2);
|
|
11404
|
+
return new ed25519.Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
11405
|
+
}
|
|
11406
|
+
function ristretto255_map(bytes) {
|
|
11407
|
+
abytes(bytes, 64);
|
|
11408
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
11409
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
11410
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
11411
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
11412
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
11413
|
+
}
|
|
11414
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
11415
|
+
constructor(ep) {
|
|
11416
|
+
super(ep);
|
|
11417
|
+
}
|
|
11418
|
+
static fromAffine(ap) {
|
|
11419
|
+
return new __RistrettoPoint(ed25519.Point.fromAffine(ap));
|
|
11420
|
+
}
|
|
11421
|
+
assertSame(other) {
|
|
11422
|
+
if (!(other instanceof __RistrettoPoint))
|
|
11423
|
+
throw new Error("RistrettoPoint expected");
|
|
11424
|
+
}
|
|
11425
|
+
init(ep) {
|
|
11426
|
+
return new __RistrettoPoint(ep);
|
|
11427
|
+
}
|
|
11428
|
+
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
11429
|
+
static hashToCurve(hex2) {
|
|
11430
|
+
return ristretto255_map(ensureBytes("ristrettoHash", hex2, 64));
|
|
11431
|
+
}
|
|
11432
|
+
static fromBytes(bytes) {
|
|
11433
|
+
abytes(bytes, 32);
|
|
11434
|
+
const { a, d } = ed25519_CURVE;
|
|
11435
|
+
const P = ed25519_CURVE_p;
|
|
11436
|
+
const mod2 = (n) => Fp.create(n);
|
|
11437
|
+
const s = bytes255ToNumberLE(bytes);
|
|
11438
|
+
if (!equalBytes2(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
11439
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
11440
|
+
const s2 = mod2(s * s);
|
|
11441
|
+
const u1 = mod2(_1n5 + a * s2);
|
|
11442
|
+
const u2 = mod2(_1n5 - a * s2);
|
|
11443
|
+
const u1_2 = mod2(u1 * u1);
|
|
11444
|
+
const u2_2 = mod2(u2 * u2);
|
|
11445
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
11446
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
11447
|
+
const Dx = mod2(I * u2);
|
|
11448
|
+
const Dy = mod2(I * Dx * v);
|
|
11449
|
+
let x = mod2((s + s) * Dx);
|
|
11450
|
+
if (isNegativeLE(x, P))
|
|
11451
|
+
x = mod2(-x);
|
|
11452
|
+
const y = mod2(u1 * Dy);
|
|
11453
|
+
const t = mod2(x * y);
|
|
11454
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n5)
|
|
11455
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
11456
|
+
return new __RistrettoPoint(new ed25519.Point(x, y, _1n5, t));
|
|
11457
|
+
}
|
|
11458
|
+
/**
|
|
11459
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
11460
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
11461
|
+
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
11462
|
+
*/
|
|
11463
|
+
static fromHex(hex2) {
|
|
11464
|
+
return __RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex2, 32));
|
|
11465
|
+
}
|
|
11466
|
+
static msm(points, scalars) {
|
|
11467
|
+
return pippenger(__RistrettoPoint, ed25519.Point.Fn, points, scalars);
|
|
11468
|
+
}
|
|
11469
|
+
/**
|
|
11470
|
+
* Encodes ristretto point to Uint8Array.
|
|
11471
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
11472
|
+
*/
|
|
11473
|
+
toBytes() {
|
|
11474
|
+
let { X, Y, Z, T } = this.ep;
|
|
11475
|
+
const P = ed25519_CURVE_p;
|
|
11476
|
+
const mod2 = (n) => Fp.create(n);
|
|
11477
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
11478
|
+
const u2 = mod2(X * Y);
|
|
11479
|
+
const u2sq = mod2(u2 * u2);
|
|
11480
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
11481
|
+
const D1 = mod2(invsqrt * u1);
|
|
11482
|
+
const D2 = mod2(invsqrt * u2);
|
|
11483
|
+
const zInv = mod2(D1 * D2 * T);
|
|
11484
|
+
let D;
|
|
11485
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
11486
|
+
let _x = mod2(Y * SQRT_M1);
|
|
11487
|
+
let _y = mod2(X * SQRT_M1);
|
|
11488
|
+
X = _x;
|
|
11489
|
+
Y = _y;
|
|
11490
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
11491
|
+
} else {
|
|
11492
|
+
D = D2;
|
|
10023
11493
|
}
|
|
11494
|
+
if (isNegativeLE(X * zInv, P))
|
|
11495
|
+
Y = mod2(-Y);
|
|
11496
|
+
let s = mod2((Z - Y) * D);
|
|
11497
|
+
if (isNegativeLE(s, P))
|
|
11498
|
+
s = mod2(-s);
|
|
11499
|
+
return Fp.toBytes(s);
|
|
11500
|
+
}
|
|
11501
|
+
/**
|
|
11502
|
+
* Compares two Ristretto points.
|
|
11503
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
11504
|
+
*/
|
|
11505
|
+
equals(other) {
|
|
11506
|
+
this.assertSame(other);
|
|
11507
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
11508
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
11509
|
+
const mod2 = (n) => Fp.create(n);
|
|
11510
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
11511
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
11512
|
+
return one || two;
|
|
11513
|
+
}
|
|
11514
|
+
is0() {
|
|
11515
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
10024
11516
|
}
|
|
10025
|
-
|
|
10026
|
-
|
|
10027
|
-
|
|
10028
|
-
|
|
10029
|
-
|
|
11517
|
+
};
|
|
11518
|
+
_RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
|
|
11519
|
+
_RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
|
|
11520
|
+
_RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
|
|
11521
|
+
_RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
|
|
11522
|
+
|
|
11523
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2-receiver.js
|
|
11524
|
+
var BCPV2_LEAD_BOUND_MS = 5e3;
|
|
11525
|
+
var REPLAY_WINDOW = 64n;
|
|
11526
|
+
var BUCKET_DEPTH = 64;
|
|
11527
|
+
var BUCKET_RATE_PER_MS = 1;
|
|
11528
|
+
var hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
11529
|
+
function sameBytes(a, b) {
|
|
11530
|
+
if (a.byteLength !== b.byteLength)
|
|
11531
|
+
return false;
|
|
11532
|
+
let diff = 0;
|
|
11533
|
+
for (let i = 0; i < a.byteLength; i++)
|
|
11534
|
+
diff |= a[i] ^ b[i];
|
|
11535
|
+
return diff === 0;
|
|
11536
|
+
}
|
|
11537
|
+
var BcProvenanceV2Receiver = class {
|
|
11538
|
+
maxAuthDelayMs;
|
|
11539
|
+
manifestTimerMs;
|
|
11540
|
+
widenMs;
|
|
11541
|
+
/** Datagrams that left a hold queue at expiry, per path: the loss signal for failover. */
|
|
11542
|
+
expired = { multicast: 0, unicast: 0 };
|
|
11543
|
+
/**
|
|
11544
|
+
* Unicast repair (Section 7.2.7 step 2): on a receiver that can request it, how
|
|
11545
|
+
* long ago (ms) the first packet admitted into the oldest open block with an FEC
|
|
11546
|
+
* deadline was admitted; undefined with none open. A pending entry still live
|
|
11547
|
+
* then is kept instead of expiring, and is released with the block, under the
|
|
11548
|
+
* open-block bound of [MOQ-FEC] Section 9.
|
|
11549
|
+
*/
|
|
11550
|
+
keepPendingAgeMs = null;
|
|
11551
|
+
cfg;
|
|
11552
|
+
now;
|
|
11553
|
+
verifySig;
|
|
11554
|
+
accepted;
|
|
11555
|
+
queueLimit;
|
|
11556
|
+
current = null;
|
|
11557
|
+
previous = null;
|
|
11558
|
+
graceStart = {};
|
|
11559
|
+
hashAlg = null;
|
|
11560
|
+
verified = /* @__PURE__ */ new Map();
|
|
11561
|
+
highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
11562
|
+
closedAt = /* @__PURE__ */ new Map();
|
|
11563
|
+
lead = null;
|
|
11564
|
+
pending = /* @__PURE__ */ new Map();
|
|
11565
|
+
admittedUntil = /* @__PURE__ */ new Map();
|
|
11566
|
+
hold = { multicast: [], unicast: [] };
|
|
11567
|
+
holdBytes = { multicast: 0, unicast: 0 };
|
|
11568
|
+
order = 0;
|
|
11569
|
+
buckets;
|
|
11570
|
+
constructor(cfg) {
|
|
11571
|
+
this.maxAuthDelayMs = cfg.maxAuthDelayMs ?? 200;
|
|
11572
|
+
this.manifestTimerMs = cfg.manifestTimerMs ?? 20;
|
|
11573
|
+
if (!(this.maxAuthDelayMs >= 50 && this.maxAuthDelayMs <= 1e3) || !(this.manifestTimerMs >= 1)) {
|
|
11574
|
+
throw new Error("BCPV2 endpoint ignored: maxAuthDelayMs or manifestTimerMs out of range");
|
|
11575
|
+
}
|
|
11576
|
+
if (!(cfg.bandwidth > 0))
|
|
11577
|
+
throw new Error("BCPV2 endpoint ignored: protected endpoint without bandwidth");
|
|
11578
|
+
const kids = new Set(cfg.keys.map((k) => k.kid));
|
|
11579
|
+
const firsts = new Set(cfg.keys.map((k) => k.firstEpoch));
|
|
11580
|
+
if (cfg.keys.length < 1 || kids.size !== cfg.keys.length || firsts.size !== cfg.keys.length) {
|
|
11581
|
+
throw new Error("BCPV2 key set invalid: no keys, or a repeated kid or firstEpoch");
|
|
11582
|
+
}
|
|
11583
|
+
this.cfg = cfg;
|
|
11584
|
+
this.widenMs = Math.min(Math.max(cfg.widenMs ?? 0, 0), BCPV2_LEAD_BOUND_MS);
|
|
11585
|
+
this.now = cfg.now ?? Date.now;
|
|
11586
|
+
this.verifySig = cfg.verifySignature ?? ((pk, msg, sig) => {
|
|
10030
11587
|
try {
|
|
10031
|
-
|
|
10032
|
-
if (isPromiseLike(result)) {
|
|
10033
|
-
pending.push(Promise.resolve(result).then((valid) => valid, () => "error"));
|
|
10034
|
-
} else if (result) {
|
|
10035
|
-
return true;
|
|
10036
|
-
}
|
|
11588
|
+
return ed25519.verify(sig, msg, pk, { zip215: false });
|
|
10037
11589
|
} catch {
|
|
10038
|
-
|
|
10039
|
-
}
|
|
10040
|
-
}
|
|
10041
|
-
if (pending.length === 0) {
|
|
10042
|
-
if (hadSynchronousError)
|
|
10043
|
-
throw new Error("manifest signature verifier unavailable");
|
|
10044
|
-
return false;
|
|
10045
|
-
}
|
|
10046
|
-
return Promise.all(pending).then((results) => {
|
|
10047
|
-
if (results.some((valid) => valid === true))
|
|
10048
|
-
return true;
|
|
10049
|
-
if (hadSynchronousError || results.some((valid) => valid === "error")) {
|
|
10050
|
-
throw new Error("manifest signature verifier unavailable");
|
|
11590
|
+
return false;
|
|
10051
11591
|
}
|
|
10052
|
-
return false;
|
|
10053
11592
|
});
|
|
11593
|
+
this.accepted = cfg.acceptedHashAlgs ?? [BCPV2_HASH_ALG_SHA256];
|
|
11594
|
+
const bandwidth = Math.min(cfg.bandwidth, 1e10);
|
|
11595
|
+
this.queueLimit = {
|
|
11596
|
+
multicast: this.maxAuthDelayMs * bandwidth / 8e3,
|
|
11597
|
+
unicast: (this.maxAuthDelayMs + this.widenMs) * bandwidth / 8e3
|
|
11598
|
+
};
|
|
11599
|
+
const t = this.now();
|
|
11600
|
+
this.buckets = {
|
|
11601
|
+
multicast: { tokens: BUCKET_DEPTH, last: t, dropped: 0 },
|
|
11602
|
+
mirror: { tokens: BUCKET_DEPTH, last: t, dropped: 0 }
|
|
11603
|
+
};
|
|
10054
11604
|
}
|
|
10055
|
-
|
|
10056
|
-
|
|
10057
|
-
|
|
10058
|
-
|
|
10059
|
-
|
|
10060
|
-
|
|
10061
|
-
|
|
10062
|
-
|
|
10063
|
-
|
|
11605
|
+
get grace() {
|
|
11606
|
+
return this.maxAuthDelayMs + 2 * this.manifestTimerMs;
|
|
11607
|
+
}
|
|
11608
|
+
/** The mirror's lead in force: 0 without a manifest track, the bound until one is measured. */
|
|
11609
|
+
get leadMs() {
|
|
11610
|
+
if (!this.cfg.applyLead)
|
|
11611
|
+
return 0;
|
|
11612
|
+
return this.lead ?? BCPV2_LEAD_BOUND_MS;
|
|
11613
|
+
}
|
|
11614
|
+
/** Step 1: a record datagram on `manifestPort`, or a record object of the manifest track. */
|
|
11615
|
+
onRecord(bytes, path) {
|
|
11616
|
+
const now = this.now();
|
|
11617
|
+
this.expire(now);
|
|
11618
|
+
const drop = (verdict) => ({ verdict, admitted: [] });
|
|
11619
|
+
const len = bytes.byteLength;
|
|
11620
|
+
if (len < BCPV2_MIN_RECORD_LENGTH || len > BCPV2_MAX_RECORD_LENGTH || (len - 132) % 32 !== 0)
|
|
11621
|
+
return drop("length");
|
|
11622
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, len);
|
|
11623
|
+
if (view2.getUint32(0) !== 1296583986)
|
|
11624
|
+
return drop("magic");
|
|
11625
|
+
const kid = view2.getUint32(4);
|
|
11626
|
+
const epoch = view2.getUint32(8);
|
|
11627
|
+
const mseq = view2.getBigUint64(12);
|
|
11628
|
+
const count = view2.getUint16(28);
|
|
11629
|
+
const hashAlg = bytes[30];
|
|
11630
|
+
const sent = view2.getUint32(32);
|
|
11631
|
+
const key = this.cfg.keys.find((k) => k.kid === kid);
|
|
11632
|
+
if (!key)
|
|
11633
|
+
return drop("kid");
|
|
11634
|
+
const next = Math.min(...this.cfg.keys.map((k) => k.firstEpoch).filter((f) => f > key.firstEpoch), Infinity);
|
|
11635
|
+
if (epoch < key.firstEpoch || epoch >= next)
|
|
11636
|
+
return drop("epoch");
|
|
11637
|
+
const id = `${kid}:${epoch}:${mseq}`;
|
|
11638
|
+
const seen = this.verified.get(id);
|
|
11639
|
+
if (seen) {
|
|
11640
|
+
const admitted2 = hex(sha2562(bytes)) === seen.digest ? this.arrival(seen, path, now) : [];
|
|
11641
|
+
return { verdict: "duplicate", admitted: admitted2 };
|
|
11642
|
+
}
|
|
11643
|
+
const ke = `${kid}:${epoch}`;
|
|
11644
|
+
if (this.current === null) {
|
|
11645
|
+
if (epoch < this.cfg.minEpoch || epoch < (this.cfg.pinnedEpochFloor?.get(kid) ?? 0))
|
|
11646
|
+
return drop("epoch");
|
|
11647
|
+
} else if (epoch < this.current.epoch && !this.previousAdmitted(epoch, path, now)) {
|
|
11648
|
+
return drop("epoch");
|
|
11649
|
+
}
|
|
11650
|
+
const floor = this.floor(ke, path);
|
|
11651
|
+
if (floor !== void 0 && mseq <= floor - REPLAY_WINDOW)
|
|
11652
|
+
return drop("mseq");
|
|
11653
|
+
const closed = this.closedAt.get(ke);
|
|
11654
|
+
if (closed !== void 0 && mseq > closed)
|
|
11655
|
+
return drop("closed");
|
|
11656
|
+
if (this.cfg.checkFreshness !== false) {
|
|
11657
|
+
const behind = 7e3 + this.maxAuthDelayMs + (path === "mirror" ? this.widenMs : 0);
|
|
11658
|
+
if (sent * 1e3 < now - behind || sent * 1e3 > now + 6e3)
|
|
11659
|
+
return drop("sent");
|
|
11660
|
+
}
|
|
11661
|
+
if (count !== (len - 132) / 32)
|
|
11662
|
+
return drop("count");
|
|
11663
|
+
if (!this.accepted.includes(hashAlg) || this.hashAlg !== null && hashAlg !== this.hashAlg)
|
|
11664
|
+
return drop("hash_alg");
|
|
11665
|
+
if (this.cfg.catalogCtx && !sameBytes(bytes.subarray(36, 68), this.cfg.catalogCtx))
|
|
11666
|
+
return drop("ctx");
|
|
11667
|
+
if (!this.take(path, now))
|
|
11668
|
+
return drop("budget");
|
|
11669
|
+
const record = decodeBcProvenanceV2Record(bytes);
|
|
11670
|
+
if (!this.verifySig(key.publicKey, bcProvenanceV2SignedMessage(record, this.cfg.endpoint), record.signature)) {
|
|
11671
|
+
return drop("signature");
|
|
11672
|
+
}
|
|
11673
|
+
if (this.current === null || epoch > this.current.epoch) {
|
|
11674
|
+
if (this.current !== null) {
|
|
11675
|
+
this.previous = { epoch: this.current.epoch, deadline: now + BCPV2_LEAD_BOUND_MS + this.grace };
|
|
10064
11676
|
}
|
|
11677
|
+
this.current = { epoch, since: now };
|
|
11678
|
+
this.graceStart = { [path]: now };
|
|
11679
|
+
if (this.hashAlg === null) {
|
|
11680
|
+
this.hashAlg = hashAlg;
|
|
11681
|
+
for (const queue of Object.values(this.hold)) {
|
|
11682
|
+
for (const held of queue)
|
|
11683
|
+
held.digest ??= this.digest(held.datagram);
|
|
11684
|
+
}
|
|
11685
|
+
}
|
|
11686
|
+
} else if (epoch === this.current.epoch) {
|
|
11687
|
+
this.graceStart[path] ??= now;
|
|
10065
11688
|
}
|
|
11689
|
+
const entry = {
|
|
11690
|
+
kid,
|
|
11691
|
+
epoch,
|
|
11692
|
+
mseq,
|
|
11693
|
+
path,
|
|
11694
|
+
arrival: now,
|
|
11695
|
+
sent,
|
|
11696
|
+
digest: hex(sha2562(bytes)),
|
|
11697
|
+
pairs: record.digests.map((d, i) => ({ digest: hex(d), seq: record.firstSeq + BigInt(i), admitted: false })),
|
|
11698
|
+
awaitingMulticast: path === "mirror"
|
|
11699
|
+
};
|
|
11700
|
+
this.verified.set(id, entry);
|
|
11701
|
+
this.raise(ke, path, mseq);
|
|
11702
|
+
if (record.flags & 1)
|
|
11703
|
+
this.closedAt.set(ke, mseq);
|
|
11704
|
+
const widen = path === "mirror" ? this.cfg.applyLead ? this.leadMs : this.widenMs : 0;
|
|
11705
|
+
const admitted = [];
|
|
11706
|
+
for (const pair of entry.pairs)
|
|
11707
|
+
this.handlePair(pair, now, now + this.grace + widen, now, admitted);
|
|
11708
|
+
this.dropAdmittedHeld();
|
|
11709
|
+
this.prune(now);
|
|
11710
|
+
return { verdict: "verified", admitted };
|
|
11711
|
+
}
|
|
11712
|
+
/** Step 2: a data datagram on `port` (or a packet of a listed track over MoQ unicast). */
|
|
11713
|
+
onDatagram(datagram, path) {
|
|
11714
|
+
const now = this.now();
|
|
11715
|
+
this.expire(now);
|
|
11716
|
+
if (this.hashAlg !== null) {
|
|
11717
|
+
const d = this.digest(datagram);
|
|
11718
|
+
if (this.consumePending(d, now))
|
|
11719
|
+
return { verdict: "admitted", admitted: [datagram] };
|
|
11720
|
+
if (this.admittedUntil.has(d))
|
|
11721
|
+
return { verdict: "duplicate", admitted: [] };
|
|
11722
|
+
return this.enqueue({ digest: d, datagram, expiry: 0, order: 0 }, path, now);
|
|
11723
|
+
}
|
|
11724
|
+
return this.enqueue({ datagram, expiry: 0, order: 0 }, path, now);
|
|
10066
11725
|
}
|
|
10067
|
-
|
|
10068
|
-
|
|
10069
|
-
|
|
10070
|
-
|
|
10071
|
-
|
|
10072
|
-
|
|
10073
|
-
|
|
10074
|
-
|
|
10075
|
-
this
|
|
11726
|
+
/**
|
|
11727
|
+
* A source packet recovered by FEC decode, its base SS_ID (SS_Start + ESI)
|
|
11728
|
+
* appended: admitted only on a pending entry it consumes, never held. One no
|
|
11729
|
+
* verified record lists, or a copy of an admitted packet, is dropped.
|
|
11730
|
+
*/
|
|
11731
|
+
admitRecovered(datagram) {
|
|
11732
|
+
const now = this.now();
|
|
11733
|
+
this.expire(now);
|
|
11734
|
+
return this.hashAlg !== null && this.consumePending(this.digest(datagram), now);
|
|
10076
11735
|
}
|
|
10077
|
-
|
|
10078
|
-
|
|
11736
|
+
/**
|
|
11737
|
+
* Discard the per-flow state on a `streamId` change or a key-set change that fails the
|
|
11738
|
+
* flow's current (kid, epoch): budgets and hold queues survive, held digests are cleared.
|
|
11739
|
+
*/
|
|
11740
|
+
resetGeneration() {
|
|
11741
|
+
this.current = null;
|
|
11742
|
+
this.previous = null;
|
|
11743
|
+
this.graceStart = {};
|
|
11744
|
+
this.hashAlg = null;
|
|
11745
|
+
this.verified.clear();
|
|
11746
|
+
this.highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
11747
|
+
this.closedAt.clear();
|
|
11748
|
+
this.lead = null;
|
|
11749
|
+
this.pending.clear();
|
|
11750
|
+
this.admittedUntil.clear();
|
|
11751
|
+
for (const queue of Object.values(this.hold))
|
|
11752
|
+
for (const held of queue)
|
|
11753
|
+
held.digest = void 0;
|
|
11754
|
+
}
|
|
11755
|
+
/** The receiver ceased to hold the endpoint: drop everything; no drop here is an expiry. */
|
|
11756
|
+
release() {
|
|
11757
|
+
this.resetGeneration();
|
|
11758
|
+
this.hold = { multicast: [], unicast: [] };
|
|
11759
|
+
this.holdBytes = { multicast: 0, unicast: 0 };
|
|
11760
|
+
}
|
|
11761
|
+
digest(datagram) {
|
|
11762
|
+
return hex(bcProvenanceV2DatagramDigest(this.cfg.endpoint, datagram));
|
|
11763
|
+
}
|
|
11764
|
+
/** Consume the lowest-seq pending entry for `digest`, admitting it. */
|
|
11765
|
+
consumePending(digest, now) {
|
|
11766
|
+
const list = this.pending.get(digest);
|
|
11767
|
+
if (!list?.length)
|
|
10079
11768
|
return false;
|
|
10080
|
-
|
|
10081
|
-
|
|
10082
|
-
|
|
10083
|
-
|
|
10084
|
-
|
|
11769
|
+
list.sort((a, b) => a.pair.seq < b.pair.seq ? -1 : a.pair.seq > b.pair.seq ? 1 : 0);
|
|
11770
|
+
const hit = list.shift();
|
|
11771
|
+
if (list.length === 0)
|
|
11772
|
+
this.pending.delete(digest);
|
|
11773
|
+
hit.pair.pending = void 0;
|
|
11774
|
+
hit.pair.admitted = true;
|
|
11775
|
+
this.admittedUntil.set(digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
10085
11776
|
return true;
|
|
10086
11777
|
}
|
|
10087
|
-
|
|
10088
|
-
|
|
10089
|
-
|
|
10090
|
-
|
|
10091
|
-
|
|
10092
|
-
|
|
10093
|
-
|
|
10094
|
-
|
|
10095
|
-
|
|
10096
|
-
|
|
10097
|
-
|
|
10098
|
-
|
|
10099
|
-
|
|
10100
|
-
|
|
10101
|
-
|
|
10102
|
-
|
|
10103
|
-
|
|
10104
|
-
|
|
10105
|
-
*
|
|
10106
|
-
* Deliberately NOT used by `#failOpenGroup`/`#failOpenTrack` — failing open on
|
|
10107
|
-
* a genuine verifier fault is correct and stays untouched.
|
|
10108
|
-
*/
|
|
10109
|
-
#giveUpOnPending(pending) {
|
|
10110
|
-
if (this.#config.mode !== "enforce") {
|
|
10111
|
-
pending.release();
|
|
10112
|
-
return;
|
|
11778
|
+
enqueue(held, path, now) {
|
|
11779
|
+
if (this.holdBytes[path] + held.datagram.byteLength > this.queueLimit[path]) {
|
|
11780
|
+
return { verdict: "overflow", admitted: [] };
|
|
11781
|
+
}
|
|
11782
|
+
held.expiry = now + this.maxAuthDelayMs + (path === "unicast" ? this.widenMs : 0);
|
|
11783
|
+
held.order = this.order++;
|
|
11784
|
+
this.hold[path].push(held);
|
|
11785
|
+
this.holdBytes[path] += held.datagram.byteLength;
|
|
11786
|
+
return { verdict: "held", admitted: [] };
|
|
11787
|
+
}
|
|
11788
|
+
/** Remove and return the oldest held datagram with `digest`, across both queues. */
|
|
11789
|
+
takeHeld(digest) {
|
|
11790
|
+
let best;
|
|
11791
|
+
for (const path of ["multicast", "unicast"]) {
|
|
11792
|
+
const index = this.hold[path].findIndex((h) => h.digest === digest);
|
|
11793
|
+
if (index >= 0 && (!best || this.hold[path][index].order < best.order)) {
|
|
11794
|
+
best = { path, index, order: this.hold[path][index].order };
|
|
11795
|
+
}
|
|
10113
11796
|
}
|
|
10114
|
-
|
|
10115
|
-
|
|
10116
|
-
|
|
10117
|
-
|
|
10118
|
-
|
|
10119
|
-
|
|
10120
|
-
|
|
10121
|
-
|
|
10122
|
-
this
|
|
11797
|
+
if (!best)
|
|
11798
|
+
return void 0;
|
|
11799
|
+
const [held] = this.hold[best.path].splice(best.index, 1);
|
|
11800
|
+
this.holdBytes[best.path] -= held.datagram.byteLength;
|
|
11801
|
+
return held.datagram;
|
|
11802
|
+
}
|
|
11803
|
+
dropAdmittedHeld() {
|
|
11804
|
+
for (const path of ["multicast", "unicast"]) {
|
|
11805
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
11806
|
+
const keep = h.digest === void 0 || !this.admittedUntil.has(h.digest);
|
|
11807
|
+
if (!keep)
|
|
11808
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
11809
|
+
return keep;
|
|
11810
|
+
});
|
|
10123
11811
|
}
|
|
10124
11812
|
}
|
|
10125
|
-
|
|
10126
|
-
|
|
10127
|
-
|
|
10128
|
-
|
|
11813
|
+
/** Step 1 pair handling: admit a held copy, or insert a pending entry. */
|
|
11814
|
+
handlePair(pair, now, expiry, inserted, out) {
|
|
11815
|
+
const datagram = this.takeHeld(pair.digest);
|
|
11816
|
+
if (datagram) {
|
|
11817
|
+
pair.admitted = true;
|
|
11818
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
11819
|
+
out.push(datagram);
|
|
10129
11820
|
return;
|
|
10130
11821
|
}
|
|
10131
|
-
|
|
10132
|
-
|
|
10133
|
-
|
|
10134
|
-
|
|
10135
|
-
|
|
10136
|
-
|
|
10137
|
-
|
|
10138
|
-
|
|
10139
|
-
|
|
10140
|
-
|
|
10141
|
-
|
|
10142
|
-
|
|
10143
|
-
|
|
10144
|
-
|
|
10145
|
-
|
|
11822
|
+
const entry = { pair, inserted, expiry };
|
|
11823
|
+
pair.pending = entry;
|
|
11824
|
+
const list = this.pending.get(pair.digest);
|
|
11825
|
+
if (list)
|
|
11826
|
+
list.push(entry);
|
|
11827
|
+
else
|
|
11828
|
+
this.pending.set(pair.digest, [entry]);
|
|
11829
|
+
}
|
|
11830
|
+
/** A byte-identical copy dropped by the already-verified lookup. */
|
|
11831
|
+
arrival(seen, path, now) {
|
|
11832
|
+
const ke = `${seen.kid}:${seen.epoch}`;
|
|
11833
|
+
this.raise(ke, path, seen.mseq);
|
|
11834
|
+
if (this.current && seen.epoch === this.current.epoch)
|
|
11835
|
+
this.graceStart[path] ??= now;
|
|
11836
|
+
if (path !== "multicast" || !seen.awaitingMulticast)
|
|
11837
|
+
return [];
|
|
11838
|
+
seen.awaitingMulticast = false;
|
|
11839
|
+
if (!this.cfg.applyLead)
|
|
11840
|
+
return [];
|
|
11841
|
+
const t = this.manifestTimerMs;
|
|
11842
|
+
const interval = Math.min(Math.ceil((now - seen.arrival) / t) * t, BCPV2_LEAD_BOUND_MS);
|
|
11843
|
+
if (this.lead === null) {
|
|
11844
|
+
this.lead = interval;
|
|
11845
|
+
return [];
|
|
11846
|
+
}
|
|
11847
|
+
if (interval <= this.lead)
|
|
11848
|
+
return [];
|
|
11849
|
+
this.lead = interval;
|
|
11850
|
+
const out = [];
|
|
11851
|
+
const records = [seen, ...[...this.verified.values()].filter((v) => v.awaitingMulticast && v.arrival >= now - interval)];
|
|
11852
|
+
for (const v of records) {
|
|
11853
|
+
for (const pair of v.pairs) {
|
|
11854
|
+
if (pair.admitted) {
|
|
11855
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + interval);
|
|
11856
|
+
} else if (pair.pending) {
|
|
11857
|
+
pair.pending.expiry = pair.pending.inserted + this.grace + interval;
|
|
11858
|
+
} else {
|
|
11859
|
+
this.handlePair(pair, now, now + this.grace + interval, now, out);
|
|
11860
|
+
}
|
|
10146
11861
|
}
|
|
10147
11862
|
}
|
|
11863
|
+
this.dropAdmittedHeld();
|
|
11864
|
+
return out;
|
|
10148
11865
|
}
|
|
10149
|
-
|
|
10150
|
-
this
|
|
10151
|
-
|
|
10152
|
-
|
|
10153
|
-
|
|
10154
|
-
|
|
10155
|
-
|
|
10156
|
-
|
|
10157
|
-
|
|
10158
|
-
|
|
10159
|
-
|
|
10160
|
-
|
|
10161
|
-
|
|
10162
|
-
|
|
10163
|
-
|
|
10164
|
-
|
|
10165
|
-
|
|
10166
|
-
|
|
10167
|
-
|
|
10168
|
-
|
|
10169
|
-
|
|
10170
|
-
|
|
10171
|
-
|
|
10172
|
-
|
|
10173
|
-
|
|
10174
|
-
|
|
10175
|
-
|
|
10176
|
-
|
|
10177
|
-
|
|
10178
|
-
|
|
10179
|
-
|
|
10180
|
-
|
|
10181
|
-
|
|
10182
|
-
|
|
10183
|
-
output += BASE64URL_ALPHABET[(b1 & 15) << 2 | (b2 === void 0 ? 0 : b2 >> 6)];
|
|
10184
|
-
if (b2 === void 0) break;
|
|
10185
|
-
output += BASE64URL_ALPHABET[b2 & 63];
|
|
10186
|
-
}
|
|
10187
|
-
return output;
|
|
10188
|
-
}
|
|
10189
|
-
var BASE64URL_DECODE_TABLE = (() => {
|
|
10190
|
-
const table = new Int8Array(128).fill(-1);
|
|
10191
|
-
for (let i = 0; i < BASE64URL_ALPHABET.length; i++) {
|
|
10192
|
-
table[BASE64URL_ALPHABET.charCodeAt(i)] = i;
|
|
10193
|
-
}
|
|
10194
|
-
return table;
|
|
10195
|
-
})();
|
|
10196
|
-
function decodeBase64Url(text2) {
|
|
10197
|
-
if (text2.length % 4 === 1) {
|
|
10198
|
-
throw new Error(`[multicast-auth-catalog] invalid unpadded base64url length for "${text2}"`);
|
|
10199
|
-
}
|
|
10200
|
-
const bytes = [];
|
|
10201
|
-
let buffer = 0;
|
|
10202
|
-
let bits = 0;
|
|
10203
|
-
for (let i = 0; i < text2.length; i++) {
|
|
10204
|
-
const code = text2.charCodeAt(i);
|
|
10205
|
-
const value = code < 128 ? BASE64URL_DECODE_TABLE[code] : -1;
|
|
10206
|
-
if (value === void 0 || value < 0) {
|
|
10207
|
-
throw new Error(`[multicast-auth-catalog] invalid base64url character in "${text2}"`);
|
|
10208
|
-
}
|
|
10209
|
-
buffer = buffer << 6 | value;
|
|
10210
|
-
bits += 6;
|
|
10211
|
-
if (bits >= 8) {
|
|
10212
|
-
bits -= 8;
|
|
10213
|
-
bytes.push(buffer >> bits & 255);
|
|
10214
|
-
}
|
|
10215
|
-
}
|
|
10216
|
-
const decoded = Uint8Array.from(bytes);
|
|
10217
|
-
if (encodeBase64Url(decoded) !== text2) {
|
|
10218
|
-
throw new Error(`[multicast-auth-catalog] non-canonical base64url encoding "${text2}"`);
|
|
10219
|
-
}
|
|
10220
|
-
return decoded;
|
|
10221
|
-
}
|
|
10222
|
-
function decodeEd25519PublicKey(publicKey) {
|
|
10223
|
-
const bytes = decodeBase64Url(publicKey);
|
|
10224
|
-
if (bytes.byteLength !== RAW_ED25519_KEY_LENGTH) {
|
|
10225
|
-
throw new Error(
|
|
10226
|
-
`[multicast-auth-catalog] public key must decode to ${RAW_ED25519_KEY_LENGTH} raw bytes, got ${bytes.byteLength}`
|
|
10227
|
-
);
|
|
10228
|
-
}
|
|
10229
|
-
return bytes;
|
|
10230
|
-
}
|
|
10231
|
-
function resolveMulticastAuthCatalog(catalog, options = {}) {
|
|
10232
|
-
if (typeof catalog !== "object" || catalog === null || Array.isArray(catalog)) {
|
|
10233
|
-
throw new Error("[multicast-auth-catalog] catalog must be an object");
|
|
11866
|
+
previousAdmitted(epoch, path, now) {
|
|
11867
|
+
if (!this.previous || epoch !== this.previous.epoch)
|
|
11868
|
+
return false;
|
|
11869
|
+
const m = this.graceStart.multicast;
|
|
11870
|
+
if (path === "multicast")
|
|
11871
|
+
return m === void 0 || now <= m + this.grace;
|
|
11872
|
+
const starts = [m, this.graceStart.mirror].filter((s) => s !== void 0);
|
|
11873
|
+
return starts.length > 0 && now <= Math.min(...starts) + this.grace;
|
|
11874
|
+
}
|
|
11875
|
+
/** The replay floor this record meets: its path's, or the higher of both from the mirror. */
|
|
11876
|
+
floor(ke, path) {
|
|
11877
|
+
const m = this.highest.multicast.get(ke);
|
|
11878
|
+
if (path === "multicast")
|
|
11879
|
+
return m;
|
|
11880
|
+
const r = this.highest.mirror.get(ke);
|
|
11881
|
+
if (m === void 0)
|
|
11882
|
+
return r;
|
|
11883
|
+
return r === void 0 || m > r ? m : r;
|
|
11884
|
+
}
|
|
11885
|
+
raise(ke, path, mseq) {
|
|
11886
|
+
const h = this.highest[path].get(ke);
|
|
11887
|
+
if (h === void 0 || mseq > h)
|
|
11888
|
+
this.highest[path].set(ke, mseq);
|
|
11889
|
+
}
|
|
11890
|
+
take(path, now) {
|
|
11891
|
+
const b = this.buckets[path];
|
|
11892
|
+
b.tokens = Math.min(BUCKET_DEPTH, b.tokens + (now - b.last) * BUCKET_RATE_PER_MS);
|
|
11893
|
+
b.last = now;
|
|
11894
|
+
if (b.tokens < 1) {
|
|
11895
|
+
b.dropped++;
|
|
11896
|
+
return false;
|
|
11897
|
+
}
|
|
11898
|
+
b.tokens -= 1;
|
|
11899
|
+
return true;
|
|
10234
11900
|
}
|
|
10235
|
-
|
|
10236
|
-
|
|
10237
|
-
|
|
10238
|
-
|
|
10239
|
-
|
|
10240
|
-
|
|
10241
|
-
|
|
10242
|
-
|
|
11901
|
+
expire(now) {
|
|
11902
|
+
for (const path of ["multicast", "unicast"]) {
|
|
11903
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
11904
|
+
if (h.expiry > now)
|
|
11905
|
+
return true;
|
|
11906
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
11907
|
+
this.expired[path]++;
|
|
11908
|
+
return false;
|
|
11909
|
+
});
|
|
10243
11910
|
}
|
|
10244
|
-
|
|
11911
|
+
let keepFrom = null;
|
|
11912
|
+
for (const [d, list] of this.pending) {
|
|
11913
|
+
const live = list.filter((p) => {
|
|
11914
|
+
if (p.expiry > now)
|
|
11915
|
+
return true;
|
|
11916
|
+
if (keepFrom === null) {
|
|
11917
|
+
const age = this.keepPendingAgeMs?.();
|
|
11918
|
+
keepFrom = age === void 0 ? void 0 : now - age;
|
|
11919
|
+
}
|
|
11920
|
+
if (keepFrom !== void 0 && keepFrom !== null && p.expiry > keepFrom)
|
|
11921
|
+
return true;
|
|
11922
|
+
p.pair.pending = void 0;
|
|
11923
|
+
return false;
|
|
11924
|
+
});
|
|
11925
|
+
if (live.length)
|
|
11926
|
+
this.pending.set(d, live);
|
|
11927
|
+
else
|
|
11928
|
+
this.pending.delete(d);
|
|
11929
|
+
}
|
|
11930
|
+
for (const [d, until] of this.admittedUntil)
|
|
11931
|
+
if (until <= now)
|
|
11932
|
+
this.admittedUntil.delete(d);
|
|
11933
|
+
if (this.previous && now > this.previous.deadline)
|
|
11934
|
+
this.previous = null;
|
|
10245
11935
|
}
|
|
10246
|
-
|
|
10247
|
-
|
|
10248
|
-
|
|
10249
|
-
|
|
10250
|
-
|
|
10251
|
-
|
|
10252
|
-
|
|
10253
|
-
|
|
10254
|
-
|
|
10255
|
-
|
|
10256
|
-
|
|
10257
|
-
|
|
10258
|
-
|
|
10259
|
-
|
|
10260
|
-
|
|
10261
|
-
|
|
10262
|
-
...flowAuth.manifestTrack === void 0 ? {} : { manifestTrack: flowAuth.manifestTrack },
|
|
10263
|
-
tracks: (endpoint["tracks"] ?? []).map(({ name, packetId }) => ({
|
|
10264
|
-
name,
|
|
10265
|
-
packetId
|
|
10266
|
-
}))
|
|
10267
|
-
});
|
|
10268
|
-
});
|
|
10269
|
-
return {
|
|
10270
|
-
keys: auth.keys.map((key) => ({ kid: key.kid, publicKey: decodeEd25519PublicKey(key.publicKey), firstEpoch: key.firstEpoch })).sort((a, b) => a.firstEpoch - b.firstEpoch),
|
|
10271
|
-
minEpoch: auth.minEpoch,
|
|
10272
|
-
flows
|
|
10273
|
-
};
|
|
10274
|
-
}
|
|
10275
|
-
function multicastAuthKeyFor(resolved, kid, epoch) {
|
|
10276
|
-
return manifestKeyFor(resolved.keys, epoch, kid);
|
|
10277
|
-
}
|
|
10278
|
-
|
|
10279
|
-
// src/manifest-auth-plan.ts
|
|
10280
|
-
function createManifestAuthPlan(catalog, options = {}) {
|
|
10281
|
-
const resolved = resolveMulticastAuthCatalog(catalog, options);
|
|
10282
|
-
if (!resolved) return void 0;
|
|
10283
|
-
const byManifestTrack = /* @__PURE__ */ new Map();
|
|
10284
|
-
for (const flow of resolved.flows) {
|
|
10285
|
-
if (flow.manifestTrack !== void 0) byManifestTrack.set(flow.manifestTrack, flow);
|
|
11936
|
+
/**
|
|
11937
|
+
* Retire verified entries below their arrival path's replay floor, keeping a mirrored entry
|
|
11938
|
+
* while the freshness check would still pass its copy on `manifestPort`.
|
|
11939
|
+
* ponytail: linear scan per record; index by (kid, epoch) if retained sets grow past a few thousand.
|
|
11940
|
+
*/
|
|
11941
|
+
prune(now) {
|
|
11942
|
+
const retention = 7e3 + this.maxAuthDelayMs;
|
|
11943
|
+
for (const [id, v] of this.verified) {
|
|
11944
|
+
const h = this.highest[v.path].get(`${v.kid}:${v.epoch}`);
|
|
11945
|
+
const aboveFloor = h === void 0 || v.mseq > h - REPLAY_WINDOW;
|
|
11946
|
+
const fresh = this.cfg.checkFreshness === false ? v.arrival + retention >= now : v.sent * 1e3 + retention >= now;
|
|
11947
|
+
const current = this.current && (v.epoch === this.current.epoch || v.epoch === this.previous?.epoch);
|
|
11948
|
+
if (current && (aboveFloor || v.path === "mirror" && fresh))
|
|
11949
|
+
continue;
|
|
11950
|
+
this.verified.delete(id);
|
|
11951
|
+
}
|
|
10286
11952
|
}
|
|
10287
|
-
|
|
10288
|
-
keys: resolved.keys,
|
|
10289
|
-
minEpoch: resolved.minEpoch,
|
|
10290
|
-
flows: resolved.flows,
|
|
10291
|
-
manifestTracks: [...byManifestTrack.keys()],
|
|
10292
|
-
keyFor: (kid, epoch) => multicastAuthKeyFor(resolved, kid, epoch),
|
|
10293
|
-
flowForManifestPort: (source, groupAddress, manifestPort) => resolved.flows.find((flow) => flow.source === source && flow.groupAddress === groupAddress && flow.manifestPort === manifestPort),
|
|
10294
|
-
flowForManifestTrack: (manifestTrack) => byManifestTrack.get(manifestTrack)
|
|
10295
|
-
};
|
|
10296
|
-
}
|
|
11953
|
+
};
|
|
10297
11954
|
export {
|
|
11955
|
+
BcProvenanceV2Receiver,
|
|
10298
11956
|
CmsfCatalogValidationError,
|
|
10299
11957
|
FEC_INACTIVE_REASONS,
|
|
10300
|
-
ManifestVerifier,
|
|
10301
11958
|
MmtpContainerParser,
|
|
10302
11959
|
MmtpFecSession,
|
|
10303
11960
|
MmtpToCmaf,
|
|
@@ -10305,14 +11962,11 @@ export {
|
|
|
10305
11962
|
catalogAtsc3NetworkSource,
|
|
10306
11963
|
catalogDeclaresFec,
|
|
10307
11964
|
catalogFecInterleaveMs,
|
|
10308
|
-
createManifestAuthPlan,
|
|
10309
11965
|
createWasmMmtpContainerParser,
|
|
10310
|
-
createWebCryptoEd25519Verifier,
|
|
10311
11966
|
datagramMfuReassembly,
|
|
10312
11967
|
fecBlockBudgetMs2 as fecBlockBudgetMs,
|
|
10313
11968
|
fecBlockDeadlineMs,
|
|
10314
11969
|
isFmp4InitSegment,
|
|
10315
|
-
manifestBindingId,
|
|
10316
11970
|
mmtpFrameReorderWindowFrames,
|
|
10317
11971
|
reliableMoqMfuReassembly,
|
|
10318
11972
|
resolveFecState,
|
|
@@ -10323,5 +11977,12 @@ export {
|
|
|
10323
11977
|
|
|
10324
11978
|
@noble/hashes/esm/utils.js:
|
|
10325
11979
|
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
11980
|
+
|
|
11981
|
+
@noble/curves/esm/utils.js:
|
|
11982
|
+
@noble/curves/esm/abstract/modular.js:
|
|
11983
|
+
@noble/curves/esm/abstract/curve.js:
|
|
11984
|
+
@noble/curves/esm/abstract/edwards.js:
|
|
11985
|
+
@noble/curves/esm/ed25519.js:
|
|
11986
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
10326
11987
|
*/
|
|
10327
11988
|
//# sourceMappingURL=no-modules.js.map
|