@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
|
@@ -1361,6 +1361,79 @@ function decideGap(input) {
|
|
|
1361
1361
|
});
|
|
1362
1362
|
}
|
|
1363
1363
|
|
|
1364
|
+
// src/held-layer-admission.ts
|
|
1365
|
+
var HORIZON_CAP = 1073741824;
|
|
1366
|
+
function heldLayerAnchorHorizon(retentionBlocks, k) {
|
|
1367
|
+
return retentionBlocks === void 0 || k <= 0 ? HORIZON_CAP : Math.min(HORIZON_CAP, retentionBlocks * k);
|
|
1368
|
+
}
|
|
1369
|
+
var HeldLayerAdmission = class {
|
|
1370
|
+
#layerNames;
|
|
1371
|
+
#states;
|
|
1372
|
+
constructor(layers, heldLayers = [0]) {
|
|
1373
|
+
const held = new Set(heldLayers);
|
|
1374
|
+
this.#layerNames = new Set(layers.map((l) => l.repairTrackName));
|
|
1375
|
+
this.#states = layers.filter((l) => held.has(l.layer)).map((l) => ({ layer: l.layer, repairTrackName: l.repairTrackName, total: l.rsIdOffset + l.repairSymbols }));
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* Judges one repair packet, after the 4.5 discards and the size check and
|
|
1379
|
+
* before it places a block. `repairTrackName` is the subscribed repair
|
|
1380
|
+
* track, never inferred from RS_ID; without it only (b) runs. A packet on a
|
|
1381
|
+
* repair track the catalog does not list is `'discard'` and anchors nothing.
|
|
1382
|
+
* `sourceFrontier` is the newest source SS_ID, if any: a repair more than
|
|
1383
|
+
* `horizon` past it is judged without moving the anchored state.
|
|
1384
|
+
*
|
|
1385
|
+
* @returns undefined to admit, `'discard'`, or the fail-closed reason.
|
|
1386
|
+
*/
|
|
1387
|
+
admit(repairTrackName, ssStart, rsbLength, horizon, sourceFrontier) {
|
|
1388
|
+
if (repairTrackName !== void 0 && !this.#layerNames.has(repairTrackName)) return "discard";
|
|
1389
|
+
const farAhead = sourceFrontier !== void 0 && serial32Precedes(sourceFrontier, ssStart) && serial32Distance(sourceFrontier, ssStart) > horizon;
|
|
1390
|
+
for (const state of this.#states) {
|
|
1391
|
+
let code;
|
|
1392
|
+
if (farAhead) {
|
|
1393
|
+
if (rsbLength < state.total) {
|
|
1394
|
+
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
1395
|
+
else if (state.anchor !== void 0 || state.settled) code = "below_anchored_total";
|
|
1396
|
+
}
|
|
1397
|
+
} else {
|
|
1398
|
+
if (state.newest === void 0 || serial32Precedes(state.newest, ssStart)) state.newest = ssStart;
|
|
1399
|
+
const stale = (s) => serial32Distance(s, state.newest) > horizon;
|
|
1400
|
+
if (state.latestBelow !== void 0 && stale(state.latestBelow)) {
|
|
1401
|
+
delete state.latestBelow;
|
|
1402
|
+
delete state.latestBelowRsb;
|
|
1403
|
+
}
|
|
1404
|
+
if (state.anchor !== void 0 && stale(state.anchor)) state.settled = true;
|
|
1405
|
+
if (rsbLength < state.total) {
|
|
1406
|
+
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
1407
|
+
else if (stale(ssStart)) continue;
|
|
1408
|
+
else if (state.settled || state.anchor !== void 0 && !serial32Precedes(ssStart, state.anchor)) code = "below_anchored_total";
|
|
1409
|
+
else if (state.latestBelow === void 0 || serial32Precedes(state.latestBelow, ssStart)) {
|
|
1410
|
+
state.latestBelow = ssStart;
|
|
1411
|
+
state.latestBelowRsb = rsbLength;
|
|
1412
|
+
}
|
|
1413
|
+
} else if (!state.settled && !stale(ssStart)) {
|
|
1414
|
+
if (state.anchor === void 0 || serial32Precedes(ssStart, state.anchor)) state.anchor = ssStart;
|
|
1415
|
+
if (state.latestBelow !== void 0 && !serial32Precedes(state.latestBelow, state.anchor)) {
|
|
1416
|
+
code = "below_anchored_total";
|
|
1417
|
+
ssStart = state.latestBelow;
|
|
1418
|
+
rsbLength = state.latestBelowRsb;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
if (code !== void 0) {
|
|
1423
|
+
return {
|
|
1424
|
+
code,
|
|
1425
|
+
layer: state.layer,
|
|
1426
|
+
total: state.total,
|
|
1427
|
+
rsbLength,
|
|
1428
|
+
ssStart,
|
|
1429
|
+
...state.anchor === void 0 ? {} : { anchor: state.anchor }
|
|
1430
|
+
};
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
return void 0;
|
|
1434
|
+
}
|
|
1435
|
+
};
|
|
1436
|
+
|
|
1364
1437
|
// src/fec-manager.ts
|
|
1365
1438
|
var RS_MAX_BLOCK_SYMBOLS = 255;
|
|
1366
1439
|
var WORKER_ERROR_LOG_CAP = 5;
|
|
@@ -1517,6 +1590,9 @@ var FECManager = class _FECManager {
|
|
|
1517
1590
|
// Receiver-local first-accepted-symbol time per block, bounded like
|
|
1518
1591
|
// #blockPathCounts (see blockAnchorAtMs).
|
|
1519
1592
|
this.#blockAnchorsMs = /* @__PURE__ */ new Map();
|
|
1593
|
+
// The receipt time of the packet being fed, when it is not now: a datagram
|
|
1594
|
+
// held on a miss and admitted later (withReceiptTime).
|
|
1595
|
+
this.#receiptAtMs = void 0;
|
|
1520
1596
|
// Exact catalog identity observed on accepted symbols for each FEC block.
|
|
1521
1597
|
// Conflicting identities collapse to null so recovery fails closed.
|
|
1522
1598
|
this.#blockCatalogTrackNames = /* @__PURE__ */ new Map();
|
|
@@ -1564,6 +1640,9 @@ var FECManager = class _FECManager {
|
|
|
1564
1640
|
// Worker recovery emits all K source symbols. Track recovery once per block
|
|
1565
1641
|
// while routing only ESI slots that were absent from the direct source path.
|
|
1566
1642
|
this.#workerRecoveredBlocks = /* @__PURE__ */ new Set();
|
|
1643
|
+
// Worker-recovered blocks a recovered packet of which admission refused,
|
|
1644
|
+
// until the decode's final symbol (7.2.7: the block is unrecovered).
|
|
1645
|
+
this.#refusedRecoveries = /* @__PURE__ */ new Set();
|
|
1567
1646
|
// How the multicast leg delivers symbols (see MulticastSymbolDelivery). The
|
|
1568
1647
|
// provider declares it per transport join; absence keeps datagram semantics.
|
|
1569
1648
|
this.#multicastSymbolDelivery = "datagram";
|
|
@@ -1702,6 +1781,11 @@ var FECManager = class _FECManager {
|
|
|
1702
1781
|
/** A stream's SBN went backwards (or jumped implausibly): the source restarted. */
|
|
1703
1782
|
this.onSourceDiscontinuity = null;
|
|
1704
1783
|
// Worker-path callbacks — async recovery and stats from FecWorkerClient.
|
|
1784
|
+
/**
|
|
1785
|
+
* A recovered source symbol. Return false when admission refused the
|
|
1786
|
+
* packet it carries (bc-provenance v2, draft-ramadan-moq-multicast-01
|
|
1787
|
+
* 7.2.7): the block is then unrecovered and stays open to its FEC deadline.
|
|
1788
|
+
*/
|
|
1705
1789
|
this.onBlockRecovered = null;
|
|
1706
1790
|
this.onFecStatsUpdate = null;
|
|
1707
1791
|
}
|
|
@@ -1754,6 +1838,7 @@ var FECManager = class _FECManager {
|
|
|
1754
1838
|
#seenSymbols;
|
|
1755
1839
|
#blockPathCounts;
|
|
1756
1840
|
#blockAnchorsMs;
|
|
1841
|
+
#receiptAtMs;
|
|
1757
1842
|
#blockCatalogTrackNames;
|
|
1758
1843
|
#startupAlignedPackets;
|
|
1759
1844
|
#startupPartialBlocks;
|
|
@@ -1766,6 +1851,7 @@ var FECManager = class _FECManager {
|
|
|
1766
1851
|
#repairDeadlines;
|
|
1767
1852
|
#repairDeadlineAtMs;
|
|
1768
1853
|
#workerRecoveredBlocks;
|
|
1854
|
+
#refusedRecoveries;
|
|
1769
1855
|
#multicastSymbolDelivery;
|
|
1770
1856
|
#orderedFrontiers;
|
|
1771
1857
|
#orderedPendingBlocks;
|
|
@@ -1875,12 +1961,12 @@ var FECManager = class _FECManager {
|
|
|
1875
1961
|
* still be held behind it. Retirement is swept lazily here, on the stream's
|
|
1876
1962
|
* own terminal events, so it needs no timer.
|
|
1877
1963
|
*/
|
|
1878
|
-
#markTerminal(key, sbn, packetId,
|
|
1964
|
+
#markTerminal(key, sbn, packetId, clean2) {
|
|
1879
1965
|
const nowMs = performance.now();
|
|
1880
1966
|
this.#retireTerminalBlocks(packetId, nowMs);
|
|
1881
1967
|
this.#forgetOrderedPending(key);
|
|
1882
1968
|
this.#completedBlocks.add(key);
|
|
1883
|
-
if (
|
|
1969
|
+
if (clean2) this.#cleanlyCompletedBlocks.add(key);
|
|
1884
1970
|
this.#indexBlockKey(key, sbn, packetId);
|
|
1885
1971
|
const config = this.#trackConfig(packetId);
|
|
1886
1972
|
const expiresAtMs = nowMs + config.interleaveDepthMs + 2 * this.#blockLifetimeMs(config);
|
|
@@ -1905,6 +1991,7 @@ var FECManager = class _FECManager {
|
|
|
1905
1991
|
this.#completedBlocks.delete(record.key);
|
|
1906
1992
|
this.#cleanlyCompletedBlocks.delete(record.key);
|
|
1907
1993
|
this.#workerRecoveredBlocks.delete(record.key);
|
|
1994
|
+
this.#refusedRecoveries.delete(record.key);
|
|
1908
1995
|
this.#failedBlocks.delete(record.key);
|
|
1909
1996
|
this.#resolvedBlockClasses.delete(record.key);
|
|
1910
1997
|
this.#repairedBlocks.delete(record.key);
|
|
@@ -2243,7 +2330,7 @@ var FECManager = class _FECManager {
|
|
|
2243
2330
|
* remainder, so repeated source progress costs no timer operation.
|
|
2244
2331
|
*/
|
|
2245
2332
|
#armRepairDeadline(key, sbn, packetId, catalogTrackName, delayMs) {
|
|
2246
|
-
const deadlineMs =
|
|
2333
|
+
const deadlineMs = this.#notEarlierUnderReceipt(this.#receiptNow() + delayMs, this.#repairDeadlineAtMs.get(key));
|
|
2247
2334
|
this.#repairDeadlineAtMs.set(key, deadlineMs);
|
|
2248
2335
|
const armed = this.#repairDeadlines.get(key);
|
|
2249
2336
|
if (armed?.handle != null) {
|
|
@@ -2263,7 +2350,7 @@ var FECManager = class _FECManager {
|
|
|
2263
2350
|
timer.handle = null;
|
|
2264
2351
|
this.#requestRepair(sbn, packetId, catalogTrackName);
|
|
2265
2352
|
};
|
|
2266
|
-
timer.handle = setTimeout(onFire,
|
|
2353
|
+
timer.handle = setTimeout(onFire, Math.max(0, deadlineMs - performance.now()));
|
|
2267
2354
|
this.#repairDeadlines.set(key, timer);
|
|
2268
2355
|
}
|
|
2269
2356
|
#requestRepair(sbn, packetId, catalogTrackName, settledByOrder = false) {
|
|
@@ -2667,7 +2754,10 @@ var FECManager = class _FECManager {
|
|
|
2667
2754
|
}
|
|
2668
2755
|
#armBlockTimeout(sbn, packetId, catalogTrackName, delayMs) {
|
|
2669
2756
|
const key = this.#blockKey(sbn, packetId, catalogTrackName);
|
|
2670
|
-
const waitAtMs =
|
|
2757
|
+
const waitAtMs = this.#notEarlierUnderReceipt(
|
|
2758
|
+
this.#receiptNow() + Math.max(1, Math.ceil(delayMs)),
|
|
2759
|
+
this.#blockWaitDeadlinesMs.get(key)
|
|
2760
|
+
);
|
|
2671
2761
|
const holdAtMs = this.#repairHoldUntilMs.get(key);
|
|
2672
2762
|
this.#blockWaitDeadlinesMs.set(key, holdAtMs === void 0 ? waitAtMs : Math.min(waitAtMs, holdAtMs));
|
|
2673
2763
|
this.#applyBlockDeadline(key, sbn, packetId, catalogTrackName);
|
|
@@ -3056,6 +3146,7 @@ var FECManager = class _FECManager {
|
|
|
3056
3146
|
resetSet(this.#cleanlyCompletedBlocks);
|
|
3057
3147
|
resetSet(this.#repairRequestedBlocks);
|
|
3058
3148
|
resetSet(this.#workerRecoveredBlocks);
|
|
3149
|
+
resetSet(this.#refusedRecoveries);
|
|
3059
3150
|
resetSet(this.#startupAlignedPackets);
|
|
3060
3151
|
resetSet(this.#startupPartialBlocks);
|
|
3061
3152
|
resetSet(this.#startupRepairReadyPackets);
|
|
@@ -3182,6 +3273,7 @@ var FECManager = class _FECManager {
|
|
|
3182
3273
|
dropSet(this.#cleanlyCompletedBlocks);
|
|
3183
3274
|
dropSet(this.#repairRequestedBlocks);
|
|
3184
3275
|
dropSet(this.#workerRecoveredBlocks);
|
|
3276
|
+
dropSet(this.#refusedRecoveries);
|
|
3185
3277
|
dropSet(this.#startupAlignedPackets);
|
|
3186
3278
|
dropSet(this.#startupPartialBlocks);
|
|
3187
3279
|
dropSet(this.#startupRepairReadyPackets);
|
|
@@ -3295,7 +3387,9 @@ var FECManager = class _FECManager {
|
|
|
3295
3387
|
moqSymbols: 0,
|
|
3296
3388
|
complete: false
|
|
3297
3389
|
};
|
|
3298
|
-
|
|
3390
|
+
const receivedAtMs = this.#receiptNow();
|
|
3391
|
+
const anchorMs = this.#blockAnchorsMs.get(blockKey);
|
|
3392
|
+
if (anchorMs === void 0 || receivedAtMs < anchorMs) this.#blockAnchorsMs.set(blockKey, receivedAtMs);
|
|
3299
3393
|
if (source === "multicast") counts.multicastSymbols++;
|
|
3300
3394
|
else counts.moqSymbols++;
|
|
3301
3395
|
this.#blockPathCounts.set(blockKey, counts);
|
|
@@ -3456,6 +3550,18 @@ var FECManager = class _FECManager {
|
|
|
3456
3550
|
blockAnchorAtMs(sbn, packetId, catalogTrackName) {
|
|
3457
3551
|
return this.#blockAnchorsMs.get(this.#blockKey(sbn, packetId, catalogTrackName));
|
|
3458
3552
|
}
|
|
3553
|
+
/**
|
|
3554
|
+
* How long ago (ms) the oldest open block of the stream, one with an FEC
|
|
3555
|
+
* deadline, took its first accepted symbol; undefined with none open.
|
|
3556
|
+
*/
|
|
3557
|
+
oldestOpenBlockAgeMs(packetId, catalogTrackName) {
|
|
3558
|
+
let oldest = Infinity;
|
|
3559
|
+
for (const key of this.#activeBlocks.keys()) {
|
|
3560
|
+
if (!this.#belongsToStream(key, packetId, catalogTrackName)) continue;
|
|
3561
|
+
oldest = Math.min(oldest, this.#blockAnchorsMs.get(key) ?? Infinity);
|
|
3562
|
+
}
|
|
3563
|
+
return oldest === Infinity ? void 0 : performance.now() - oldest;
|
|
3564
|
+
}
|
|
3459
3565
|
/** Absolute fail deadline of a still-open block, or undefined. */
|
|
3460
3566
|
blockDeadlineAtMs(sbn, packetId, catalogTrackName) {
|
|
3461
3567
|
return this.#activeBlockDeadlinesMs.get(this.#blockKey(sbn, packetId, catalogTrackName));
|
|
@@ -3956,7 +4062,25 @@ var FECManager = class _FECManager {
|
|
|
3956
4062
|
const blockKey = this.#blockKey(meta.sbn, packetId, recoveredCatalogTrackName);
|
|
3957
4063
|
const cleanlyCompleted = this.#cleanlyCompletedBlocks.has(blockKey);
|
|
3958
4064
|
if (!cleanlyCompleted) {
|
|
3959
|
-
|
|
4065
|
+
const symbolKey = `${blockKey}:${esi}`;
|
|
4066
|
+
if (!this.#seenSymbols.has(symbolKey)) {
|
|
4067
|
+
this.#seenSymbols.add(symbolKey);
|
|
4068
|
+
const admitted = this.onBlockRecovered?.(
|
|
4069
|
+
meta.sbn,
|
|
4070
|
+
packetId,
|
|
4071
|
+
data,
|
|
4072
|
+
meta,
|
|
4073
|
+
recoveredCatalogTrackName
|
|
4074
|
+
);
|
|
4075
|
+
if (admitted === false) {
|
|
4076
|
+
this.#refusedRecoveries.add(blockKey);
|
|
4077
|
+
if (this.#refusedRecoveries.size > 1e3) dropOldest(this.#refusedRecoveries, 500);
|
|
4078
|
+
}
|
|
4079
|
+
}
|
|
4080
|
+
}
|
|
4081
|
+
const refused = esi >= meta.ssbLength - 1 && this.#refusedRecoveries.delete(blockKey);
|
|
4082
|
+
if (esi >= meta.ssbLength - 1 && !refused && !this.#completedBlocks.has(blockKey)) {
|
|
4083
|
+
if (!cleanlyCompleted && !this.#workerRecoveredBlocks.has(blockKey)) {
|
|
3960
4084
|
this.#traceLifecycle(
|
|
3961
4085
|
"recovered",
|
|
3962
4086
|
meta.sbn,
|
|
@@ -3971,19 +4095,6 @@ var FECManager = class _FECManager {
|
|
|
3971
4095
|
}
|
|
3972
4096
|
this.#noteBlockRepaired(blockKey, meta.sbn, packetId, recoveredCatalogTrackName);
|
|
3973
4097
|
}
|
|
3974
|
-
const symbolKey = `${blockKey}:${esi}`;
|
|
3975
|
-
if (!this.#seenSymbols.has(symbolKey)) {
|
|
3976
|
-
this.#seenSymbols.add(symbolKey);
|
|
3977
|
-
this.onBlockRecovered?.(
|
|
3978
|
-
meta.sbn,
|
|
3979
|
-
packetId,
|
|
3980
|
-
data,
|
|
3981
|
-
meta,
|
|
3982
|
-
recoveredCatalogTrackName
|
|
3983
|
-
);
|
|
3984
|
-
}
|
|
3985
|
-
}
|
|
3986
|
-
if (esi >= meta.ssbLength - 1 && !this.#completedBlocks.has(blockKey)) {
|
|
3987
4098
|
this.cancelBlockTimeout(meta.sbn, packetId, recoveredCatalogTrackName);
|
|
3988
4099
|
if (recoveredCatalogTrackName === void 0) {
|
|
3989
4100
|
this.onBlockComplete?.(meta.sbn, packetId);
|
|
@@ -4278,66 +4389,26 @@ var FECManager = class _FECManager {
|
|
|
4278
4389
|
const config = this.#trackConfigs.get(packetId);
|
|
4279
4390
|
const layers = config?.repairLayers;
|
|
4280
4391
|
if (layers === void 0 || layers.length === 0) return true;
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
if (states === void 0) {
|
|
4286
|
-
const held = new Set(config.heldLayers ?? [0]);
|
|
4287
|
-
states = layers.filter((l) => held.has(l.layer)).map((l) => ({ layer: l.layer, repairTrackName: l.repairTrackName, total: l.rsIdOffset + l.repairSymbols }));
|
|
4288
|
-
this.#heldLayerStates.set(streamKey, states);
|
|
4392
|
+
let admission = this.#heldLayerStates.get(streamKey);
|
|
4393
|
+
if (admission === void 0) {
|
|
4394
|
+
admission = new HeldLayerAdmission(layers, config.heldLayers);
|
|
4395
|
+
this.#heldLayerStates.set(streamKey, admission);
|
|
4289
4396
|
}
|
|
4290
|
-
const
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
delete state.latestBelowRsb;
|
|
4306
|
-
}
|
|
4307
|
-
if (state.anchor !== void 0 && stale(state.anchor)) state.settled = true;
|
|
4308
|
-
if (rsbLength < state.total) {
|
|
4309
|
-
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
4310
|
-
else if (stale(ssStart)) continue;
|
|
4311
|
-
else if (state.settled || state.anchor !== void 0 && !serial32Precedes(ssStart, state.anchor)) code = "below_anchored_total";
|
|
4312
|
-
else if (state.latestBelow === void 0 || serial32Precedes(state.latestBelow, ssStart)) {
|
|
4313
|
-
state.latestBelow = ssStart;
|
|
4314
|
-
state.latestBelowRsb = rsbLength;
|
|
4315
|
-
}
|
|
4316
|
-
} else if (!state.settled && !stale(ssStart)) {
|
|
4317
|
-
if (state.anchor === void 0 || serial32Precedes(ssStart, state.anchor)) state.anchor = ssStart;
|
|
4318
|
-
if (state.latestBelow !== void 0 && !serial32Precedes(state.latestBelow, state.anchor)) {
|
|
4319
|
-
code = "below_anchored_total";
|
|
4320
|
-
ssStart = state.latestBelow;
|
|
4321
|
-
rsbLength = state.latestBelowRsb;
|
|
4322
|
-
}
|
|
4323
|
-
}
|
|
4324
|
-
}
|
|
4325
|
-
if (code !== void 0) {
|
|
4326
|
-
const reason = {
|
|
4327
|
-
code,
|
|
4328
|
-
layer: state.layer,
|
|
4329
|
-
total: state.total,
|
|
4330
|
-
rsbLength,
|
|
4331
|
-
ssStart,
|
|
4332
|
-
...state.anchor === void 0 ? {} : { anchor: state.anchor }
|
|
4333
|
-
};
|
|
4334
|
-
this.#fecFailClosed.set(streamKey, reason);
|
|
4335
|
-
this.#onFecFailClosed?.({ ...reason, packetId, catalogTrackName });
|
|
4336
|
-
console.warn(
|
|
4337
|
-
`[FECManager] FEC failed closed (fec-01 6.3.9) packetId=${packetId} track=${catalogTrackName ?? "-"}: ${JSON.stringify(reason)}`
|
|
4338
|
-
);
|
|
4339
|
-
return false;
|
|
4340
|
-
}
|
|
4397
|
+
const verdict = admission.admit(
|
|
4398
|
+
repairTrackName,
|
|
4399
|
+
ssStart,
|
|
4400
|
+
rsbLength,
|
|
4401
|
+
this.#layerAnchorHorizon(packetId),
|
|
4402
|
+
this.#sourceFrontiers.get(streamKey)?.sbn
|
|
4403
|
+
);
|
|
4404
|
+
if (verdict === "discard") return false;
|
|
4405
|
+
if (verdict !== void 0) {
|
|
4406
|
+
this.#fecFailClosed.set(streamKey, verdict);
|
|
4407
|
+
this.#onFecFailClosed?.({ ...verdict, packetId, catalogTrackName });
|
|
4408
|
+
console.warn(
|
|
4409
|
+
`[FECManager] FEC failed closed (fec-01 6.3.9) packetId=${packetId} track=${catalogTrackName ?? "-"}: ${JSON.stringify(verdict)}`
|
|
4410
|
+
);
|
|
4411
|
+
return false;
|
|
4341
4412
|
}
|
|
4342
4413
|
return true;
|
|
4343
4414
|
}
|
|
@@ -4347,15 +4418,13 @@ var FECManager = class _FECManager {
|
|
|
4347
4418
|
* times K, capped at 2^30 so compared serials stay inside one half-window.
|
|
4348
4419
|
*/
|
|
4349
4420
|
#layerAnchorHorizon(packetId) {
|
|
4350
|
-
const cap = 1073741824;
|
|
4351
4421
|
let blocks;
|
|
4352
4422
|
try {
|
|
4353
4423
|
blocks = this.isoRetentionBlocksForPacket(packetId);
|
|
4354
4424
|
} catch {
|
|
4355
4425
|
blocks = void 0;
|
|
4356
4426
|
}
|
|
4357
|
-
|
|
4358
|
-
return blocks === void 0 || k <= 0 ? cap : Math.min(cap, blocks * k);
|
|
4427
|
+
return heldLayerAnchorHorizon(blocks, this.#trackConfigs.get(packetId)?.k ?? 0);
|
|
4359
4428
|
}
|
|
4360
4429
|
/**
|
|
4361
4430
|
* Report each stream whose FEC fails closed at runtime (fec-01 6.3.9), once,
|
|
@@ -4595,6 +4664,33 @@ var FECManager = class _FECManager {
|
|
|
4595
4664
|
}
|
|
4596
4665
|
queueMicrotask(() => this.#syncDecoderBlockOwners.delete(key));
|
|
4597
4666
|
}
|
|
4667
|
+
/**
|
|
4668
|
+
* Run `fn` with `receivedAtMs` (performance.now() clock) as the receipt
|
|
4669
|
+
* time of what it feeds. Within a consumer the bc-provenance profile
|
|
4670
|
+
* governs, a datagram held on a miss and admitted later keeps its receipt
|
|
4671
|
+
* time (fec-01 9): it anchors its block's ts and arms the block's
|
|
4672
|
+
* deadlines from then, never later than they are already armed.
|
|
4673
|
+
*/
|
|
4674
|
+
withReceiptTime(receivedAtMs, fn) {
|
|
4675
|
+
const outer = this.#receiptAtMs;
|
|
4676
|
+
this.#receiptAtMs = receivedAtMs;
|
|
4677
|
+
try {
|
|
4678
|
+
fn();
|
|
4679
|
+
} finally {
|
|
4680
|
+
this.#receiptAtMs = outer;
|
|
4681
|
+
}
|
|
4682
|
+
}
|
|
4683
|
+
#receiptNow() {
|
|
4684
|
+
return this.#receiptAtMs ?? performance.now();
|
|
4685
|
+
}
|
|
4686
|
+
/**
|
|
4687
|
+
* A held packet admitted late arms from its (earlier) receipt time; that
|
|
4688
|
+
* never pulls in a deadline later activity already armed. Outside a
|
|
4689
|
+
* receipt scope a re-arm replaces the deadline, as a narrower bound may.
|
|
4690
|
+
*/
|
|
4691
|
+
#notEarlierUnderReceipt(deadlineMs, armedMs) {
|
|
4692
|
+
return this.#receiptAtMs === void 0 || armedMs === void 0 ? deadlineMs : Math.max(deadlineMs, armedMs);
|
|
4693
|
+
}
|
|
4598
4694
|
/** Refresh deadlines after accepted block activity. */
|
|
4599
4695
|
noteBlockActivity(sbn, packetId, catalogTrackName, sourceProgress = false) {
|
|
4600
4696
|
if (!this.#trackConfigs.has(packetId)) return;
|
|
@@ -4773,6 +4869,7 @@ var FECManager = class _FECManager {
|
|
|
4773
4869
|
this.#cleanlyCompletedBlocks.clear();
|
|
4774
4870
|
this.#repairRequestedBlocks.clear();
|
|
4775
4871
|
this.#workerRecoveredBlocks.clear();
|
|
4872
|
+
this.#refusedRecoveries.clear();
|
|
4776
4873
|
this.#syncDecoderBlockOwners.clear();
|
|
4777
4874
|
this.#startupAlignedPackets.clear();
|
|
4778
4875
|
this.#startupPartialBlocks.clear();
|
|
@@ -7618,11 +7715,11 @@ function hevcNalUnitsContainIrap(nals) {
|
|
|
7618
7715
|
return sawNal ? false : void 0;
|
|
7619
7716
|
}
|
|
7620
7717
|
function prepareHevcCmafSample(data, sampleEntry) {
|
|
7621
|
-
const
|
|
7622
|
-
if (
|
|
7623
|
-
const keyframe = hevcNalUnitsContainIrap(
|
|
7718
|
+
const split2 = splitHevcSample(data);
|
|
7719
|
+
if (split2 === null) return null;
|
|
7720
|
+
const keyframe = hevcNalUnitsContainIrap(split2.nals);
|
|
7624
7721
|
if (keyframe === void 0) return null;
|
|
7625
|
-
const normalized = hevcNalUnitsToLengthPrefixed(data,
|
|
7722
|
+
const normalized = hevcNalUnitsToLengthPrefixed(data, split2.nals, split2.lengthPrefixed, sampleEntry);
|
|
7626
7723
|
return normalized === null ? null : { keyframe, data: normalized };
|
|
7627
7724
|
}
|
|
7628
7725
|
function hevcNalUnitsToLengthPrefixed(data, nals, lengthPrefixed, sampleEntry) {
|
|
@@ -9033,9 +9130,9 @@ var CmafBlockAssembler = class {
|
|
|
9033
9130
|
const sample = splitH264SampleNalUnits(data);
|
|
9034
9131
|
const nalTypes = describeH264NalTypes(sample.nals);
|
|
9035
9132
|
const classify = classifyH264Keyframe(data);
|
|
9036
|
-
const
|
|
9133
|
+
const hex2 = Array.from(data.subarray(0, Math.min(48, data.length))).map((b) => b.toString(16).padStart(2, "0")).join(" ");
|
|
9037
9134
|
console.log(
|
|
9038
|
-
`[L4-KF-GATE] reject#${this.#keyframeRejectDumpCount} len=${data.length} metadataKf=${metadataKeyframe} classify=${classify} format=${sample.format} nalTypes=[${nalTypes.join(",")}] first48=[${
|
|
9135
|
+
`[L4-KF-GATE] reject#${this.#keyframeRejectDumpCount} len=${data.length} metadataKf=${metadataKeyframe} classify=${classify} format=${sample.format} nalTypes=[${nalTypes.join(",")}] first48=[${hex2}]`
|
|
9039
9136
|
);
|
|
9040
9137
|
}
|
|
9041
9138
|
/** Mark video as already synchronized by an external decoder path.
|
|
@@ -12846,11 +12943,22 @@ var MmtpRouter = class {
|
|
|
12846
12943
|
#fecManager;
|
|
12847
12944
|
#defaultFecMapper;
|
|
12848
12945
|
#fecMappersByPacketId = /* @__PURE__ */ new Map();
|
|
12849
|
-
|
|
12850
|
-
#
|
|
12851
|
-
#
|
|
12852
|
-
#
|
|
12853
|
-
#
|
|
12946
|
+
/** Protected flows: one 7.2.7 receiver each, and the ingress its datagrams arrive with. */
|
|
12947
|
+
#provenance = /* @__PURE__ */ new Map();
|
|
12948
|
+
#onFlowProtected = null;
|
|
12949
|
+
#unadmittedListedDrops = 0;
|
|
12950
|
+
#unlistedRepairSetAside = 0;
|
|
12951
|
+
/** The ingress a held datagram arrived with; the receiver returns the same bytes on release. */
|
|
12952
|
+
#heldIngress = /* @__PURE__ */ new WeakMap();
|
|
12953
|
+
#unicastRepair = false;
|
|
12954
|
+
/** How to route a held unicast (MoQ) datagram once a record admits it. */
|
|
12955
|
+
#heldReplay = /* @__PURE__ */ new WeakMap();
|
|
12956
|
+
/** When a held datagram was received: admitted later, it keeps that receipt time (fec-01 9). */
|
|
12957
|
+
#heldAt = /* @__PURE__ */ new WeakMap();
|
|
12958
|
+
/** The flow whose receiver admitted a stream's source datagrams, by packetId then catalog track name: recovered packets answer to it. */
|
|
12959
|
+
#provenanceStreamFlow = /* @__PURE__ */ new Map();
|
|
12960
|
+
#unregisteredFlowDrops = 0;
|
|
12961
|
+
#ambiguousRecoveredDrops = 0;
|
|
12854
12962
|
// Temp state set by #processMMTP before calling parser.feed(),
|
|
12855
12963
|
// read by the parser's onFrame callback for CMAF assembly + dedup.
|
|
12856
12964
|
#currentFecInfo;
|
|
@@ -12987,10 +13095,6 @@ var MmtpRouter = class {
|
|
|
12987
13095
|
this.#fecManager = config?.fecManager ?? null;
|
|
12988
13096
|
this.#onFrameGap = config?.onFrameGap ?? null;
|
|
12989
13097
|
this.#defaultFecMapper = config?.fecMapper ?? null;
|
|
12990
|
-
this.#manifestVerifier = config?.manifestVerifier ?? null;
|
|
12991
|
-
this.#ownsManifestVerifier = config?.manifestVerifierOwnership !== "caller";
|
|
12992
|
-
this.#resolveManifestIdentity = config?.resolveManifestIdentity ?? null;
|
|
12993
|
-
this.#resolveManifestIdentityForRoute = config?.resolveManifestIdentityForRoute ?? null;
|
|
12994
13098
|
this.#parser = new MmtpContainerParser({
|
|
12995
13099
|
mfuTimeoutMs: config?.mfuTimeoutMs,
|
|
12996
13100
|
reassemblyPolicy: config?.reassemblyPolicy,
|
|
@@ -13522,29 +13626,189 @@ var MmtpRouter = class {
|
|
|
13522
13626
|
}
|
|
13523
13627
|
});
|
|
13524
13628
|
}
|
|
13525
|
-
|
|
13526
|
-
|
|
13527
|
-
|
|
13528
|
-
|
|
13529
|
-
|
|
13530
|
-
|
|
13531
|
-
|
|
13532
|
-
|
|
13533
|
-
this.#
|
|
13534
|
-
|
|
13535
|
-
|
|
13536
|
-
|
|
13537
|
-
|
|
13538
|
-
|
|
13539
|
-
|
|
13540
|
-
|
|
13541
|
-
|
|
13542
|
-
|
|
13629
|
+
/**
|
|
13630
|
+
* Protect a flow (draft-ramadan-moq-multicast-01 7.2.7): every multicast
|
|
13631
|
+
* datagram whose ingress names `ingress.flow`, source and repair alike, is
|
|
13632
|
+
* admitted by `receiver` before anything reads it, and an unadmitted one is
|
|
13633
|
+
* held or dropped. The caller owns the receiver's configuration; the router
|
|
13634
|
+
* releases it on `releaseProvenanceFlow` and on dispose.
|
|
13635
|
+
*/
|
|
13636
|
+
registerProvenanceFlow(ingress, receiver) {
|
|
13637
|
+
this.#provenance.get(ingress.flow)?.receiver.release();
|
|
13638
|
+
this.#provenance.set(ingress.flow, { receiver, ingress });
|
|
13639
|
+
receiver.keepPendingAgeMs = () => this.#oldestOpenBlockAgeMs(ingress.flow);
|
|
13640
|
+
}
|
|
13641
|
+
/**
|
|
13642
|
+
* Declare that this receiver can request unicast repair (a repair FETCH
|
|
13643
|
+
* scheduler is wired): a protected flow then keeps the pending entries of
|
|
13644
|
+
* its open blocks for the repair to consume (7.2.7 step 2).
|
|
13645
|
+
*/
|
|
13646
|
+
setUnicastRepair(enabled) {
|
|
13647
|
+
this.#unicastRepair = enabled;
|
|
13648
|
+
}
|
|
13649
|
+
#oldestOpenBlockAgeMs(flow) {
|
|
13650
|
+
const entry = this.#provenance.get(flow);
|
|
13651
|
+
if (!this.#unicastRepair || !entry) return void 0;
|
|
13652
|
+
let age;
|
|
13653
|
+
for (const { name, packetId } of entry.ingress.tracks) {
|
|
13654
|
+
const a = name ? this.#fecManager?.oldestOpenBlockAgeMs?.(packetId, name) : void 0;
|
|
13655
|
+
if (a !== void 0 && (age === void 0 || a > age)) age = a;
|
|
13656
|
+
}
|
|
13657
|
+
return age;
|
|
13658
|
+
}
|
|
13659
|
+
/**
|
|
13660
|
+
* Replace a registered flow's ingress (a rendition switch staged, committed
|
|
13661
|
+
* or cancelled) while keeping its receiver; a no-op for an unregistered flow.
|
|
13662
|
+
*/
|
|
13663
|
+
updateProvenanceFlowIngress(ingress) {
|
|
13664
|
+
const entry = this.#provenance.get(ingress.flow);
|
|
13665
|
+
if (!entry) return;
|
|
13666
|
+
this.#provenance.set(ingress.flow, { ...entry, ingress });
|
|
13667
|
+
if (entry.protected) {
|
|
13668
|
+
for (const { name, packetId } of ingress.tracks) {
|
|
13669
|
+
if (name && !entry.ingress.tracks.some((t) => t.name === name && t.packetId === packetId)) {
|
|
13670
|
+
this.#fecManager?.resetTrack?.(packetId, name);
|
|
13543
13671
|
}
|
|
13544
13672
|
}
|
|
13545
|
-
this.#manifestRecoveryQueues.clear();
|
|
13546
13673
|
}
|
|
13547
13674
|
}
|
|
13675
|
+
/**
|
|
13676
|
+
* Called once per protection of a flow: at the admission that makes this
|
|
13677
|
+
* router's consumers protected under it (7.2.7), after their unadmitted
|
|
13678
|
+
* holdings of its listed tracks are discarded. Again after an unprotect.
|
|
13679
|
+
*/
|
|
13680
|
+
onProvenanceFlowProtected(cb) {
|
|
13681
|
+
this.#onFlowProtected = cb;
|
|
13682
|
+
}
|
|
13683
|
+
/**
|
|
13684
|
+
* The receiver ceased to hold the flow's endpoint on failover (7.2.7): drop
|
|
13685
|
+
* its state and hold queues (no expiry counted) and un-protect its tracks,
|
|
13686
|
+
* which then take MoQ again. The registration stays, so multicast resuming
|
|
13687
|
+
* protects the flow anew at its next admission.
|
|
13688
|
+
*/
|
|
13689
|
+
unprotectProvenanceFlow(flow) {
|
|
13690
|
+
const entry = this.#provenance.get(flow);
|
|
13691
|
+
if (!entry) return;
|
|
13692
|
+
entry.receiver.release();
|
|
13693
|
+
entry.protected = false;
|
|
13694
|
+
}
|
|
13695
|
+
/** MoQ packets of a track a protected flow lists, dropped on its unicast path: duplicate, overflow, or listed by more than one flow. */
|
|
13696
|
+
get unadmittedListedDropCount() {
|
|
13697
|
+
return this.#unadmittedListedDrops;
|
|
13698
|
+
}
|
|
13699
|
+
/** MoQ repair symbols of an unlisted track kept out of a governed FEC instance (7.2.7 step 2). */
|
|
13700
|
+
get unlistedRepairSetAsideCount() {
|
|
13701
|
+
return this.#unlistedRepairSetAside;
|
|
13702
|
+
}
|
|
13703
|
+
/**
|
|
13704
|
+
* A MoQ repair symbol no protected flow lists, owned by a source track one
|
|
13705
|
+
* does: its FEC instance is governed (7.2.7 step 2) and takes only admitted
|
|
13706
|
+
* symbols, so the symbol is set aside. Un-listing needs no hook: an
|
|
13707
|
+
* unprotected or released flow lists nothing.
|
|
13708
|
+
* ponytail: set aside = dropped. 7.2.7 lets it enter a decode (MAY), only
|
|
13709
|
+
* for a shortfall; the unlisted-symbol pool (BLO-49773 item 2) adds that.
|
|
13710
|
+
*/
|
|
13711
|
+
#setAsideUnlistedRepair(ownerPacketId) {
|
|
13712
|
+
for (const entry of this.#provenance.values()) {
|
|
13713
|
+
if (entry.protected && ingressHasPacketId(entry.ingress, ownerPacketId)) {
|
|
13714
|
+
this.#unlistedRepairSetAside++;
|
|
13715
|
+
return true;
|
|
13716
|
+
}
|
|
13717
|
+
}
|
|
13718
|
+
return false;
|
|
13719
|
+
}
|
|
13720
|
+
/**
|
|
13721
|
+
* The admission that makes this router protected under `flow` (7.2.7):
|
|
13722
|
+
* its FEC instances discard what they hold of the flow's listed tracks so
|
|
13723
|
+
* no unadmitted symbol stands for an admitted one, then notify. Before it
|
|
13724
|
+
* nothing of the flow was admitted, so every such symbol is unadmitted.
|
|
13725
|
+
* The playback parse ledger is kept: it records MoQ copies already played,
|
|
13726
|
+
* not holdings, and clearing it re-emits that media (audio twice).
|
|
13727
|
+
* The FEC reset is the whole 7.2.7 discard here: no keyframe-overlay
|
|
13728
|
+
* instance exists to hold anything (repair-layers.ts is the catalog
|
|
13729
|
+
* contract only, no decoder), and packet_id 0 is never a listed track
|
|
13730
|
+
* (`ingressHasPacketId`), so nothing of it is held for the flow.
|
|
13731
|
+
*/
|
|
13732
|
+
#noteFlowAdmitted(flow) {
|
|
13733
|
+
const entry = this.#provenance.get(flow);
|
|
13734
|
+
if (!entry || entry.protected) return;
|
|
13735
|
+
entry.protected = true;
|
|
13736
|
+
for (const { name, packetId } of entry.ingress.tracks) {
|
|
13737
|
+
if (name) this.#fecManager?.resetTrack?.(packetId, name);
|
|
13738
|
+
}
|
|
13739
|
+
this.#onFlowProtected?.(flow);
|
|
13740
|
+
}
|
|
13741
|
+
/**
|
|
13742
|
+
* Admit a MoQ datagram of `packetId` on the unicast path of the protected
|
|
13743
|
+
* flow that lists it (7.2.7). `unlisted`: no protected flow lists it, so it
|
|
13744
|
+
* is read as before. A held datagram is routed by `replay` when a record
|
|
13745
|
+
* admits it; one more than one protected flow lists is dropped. So is an
|
|
13746
|
+
* overlap copy: it never enters FEC accounting, so admitting it would leave
|
|
13747
|
+
* its multicast twin a duplicate and the block one symbol short.
|
|
13748
|
+
*/
|
|
13749
|
+
#admitUnicast(packetId, packet, replay, overlapCopy = false) {
|
|
13750
|
+
let receiver;
|
|
13751
|
+
for (const entry of this.#provenance.values()) {
|
|
13752
|
+
if (!entry.protected || !ingressHasPacketId(entry.ingress, packetId)) continue;
|
|
13753
|
+
if (receiver || overlapCopy) {
|
|
13754
|
+
this.#unadmittedListedDrops++;
|
|
13755
|
+
return "dropped";
|
|
13756
|
+
}
|
|
13757
|
+
receiver = entry.receiver;
|
|
13758
|
+
}
|
|
13759
|
+
if (!receiver) return "unlisted";
|
|
13760
|
+
const { verdict } = receiver.onDatagram(packet, "unicast");
|
|
13761
|
+
if (verdict === "admitted") return "admitted";
|
|
13762
|
+
if (verdict === "held") {
|
|
13763
|
+
this.#heldReplay.set(packet, replay);
|
|
13764
|
+
this.#heldAt.set(packet, performance.now());
|
|
13765
|
+
return "held";
|
|
13766
|
+
}
|
|
13767
|
+
this.#unadmittedListedDrops++;
|
|
13768
|
+
return "dropped";
|
|
13769
|
+
}
|
|
13770
|
+
/**
|
|
13771
|
+
* Feed one bc-provenance record of `flow`: a datagram from `manifestPort`
|
|
13772
|
+
* (`multicast`) or an object of the manifest track (`mirror`). Held
|
|
13773
|
+
* datagrams the record admits are routed before this returns.
|
|
13774
|
+
*/
|
|
13775
|
+
feedProvenanceRecord(flow, record, path) {
|
|
13776
|
+
const entry = this.#provenance.get(flow);
|
|
13777
|
+
if (!entry || this.#disposed) return "unregistered";
|
|
13778
|
+
const result = entry.receiver.onRecord(record, path);
|
|
13779
|
+
if (result.admitted.length > 0) {
|
|
13780
|
+
this.#noteFlowAdmitted(flow);
|
|
13781
|
+
this.#withDeferredFecPokes(() => {
|
|
13782
|
+
for (const datagram of result.admitted) {
|
|
13783
|
+
const route = () => {
|
|
13784
|
+
const replay = this.#heldReplay.get(datagram);
|
|
13785
|
+
if (replay) replay();
|
|
13786
|
+
else this.#processMMTP(datagram, "multicast", true, this.#heldIngress.get(datagram) ?? entry.ingress, false, true);
|
|
13787
|
+
};
|
|
13788
|
+
const heldAt = this.#heldAt.get(datagram);
|
|
13789
|
+
if (heldAt !== void 0 && this.#fecManager?.withReceiptTime) this.#fecManager.withReceiptTime(heldAt, route);
|
|
13790
|
+
else route();
|
|
13791
|
+
}
|
|
13792
|
+
});
|
|
13793
|
+
}
|
|
13794
|
+
return result.verdict;
|
|
13795
|
+
}
|
|
13796
|
+
/** The receiver ceased to hold the flow: its hold queues are dropped, not expired. */
|
|
13797
|
+
releaseProvenanceFlow(flow) {
|
|
13798
|
+
this.#provenance.get(flow)?.receiver.release();
|
|
13799
|
+
this.#provenance.delete(flow);
|
|
13800
|
+
for (const byName of this.#provenanceStreamFlow.values()) {
|
|
13801
|
+
for (const [name, bound] of byName) if (bound === flow) byName.delete(name);
|
|
13802
|
+
}
|
|
13803
|
+
}
|
|
13804
|
+
/** Multicast datagrams dropped because their ingress names a flow with no registered receiver. */
|
|
13805
|
+
get unregisteredFlowDropCount() {
|
|
13806
|
+
return this.#unregisteredFlowDrops;
|
|
13807
|
+
}
|
|
13808
|
+
/** Recovered packets dropped because more than one protected flow could own their stream. */
|
|
13809
|
+
get ambiguousRecoveredDropCount() {
|
|
13810
|
+
return this.#ambiguousRecoveredDrops;
|
|
13811
|
+
}
|
|
13548
13812
|
/**
|
|
13549
13813
|
* Main entry point: feed a raw MMTP packet for processing.
|
|
13550
13814
|
*
|
|
@@ -13559,10 +13823,10 @@ var MmtpRouter = class {
|
|
|
13559
13823
|
* they may never have crossed — and that attribution is what the physical-
|
|
13560
13824
|
* delivery evidence path is built on. Absence is not multicast; say which.
|
|
13561
13825
|
*/
|
|
13562
|
-
feedPacket(packet, source,
|
|
13826
|
+
feedPacket(packet, source, ingress) {
|
|
13563
13827
|
if (this.#disposed) return;
|
|
13564
13828
|
this.#withDeferredFecPokes(() => {
|
|
13565
|
-
this.#processMMTP(packet, source,
|
|
13829
|
+
this.#processMMTP(packet, source, false, ingress);
|
|
13566
13830
|
});
|
|
13567
13831
|
}
|
|
13568
13832
|
/**
|
|
@@ -13597,7 +13861,7 @@ var MmtpRouter = class {
|
|
|
13597
13861
|
feedOverlapCopy(packet, ingress) {
|
|
13598
13862
|
if (this.#disposed) return;
|
|
13599
13863
|
this.#withDeferredFecPokes(() => {
|
|
13600
|
-
this.#processMMTP(packet, "moq",
|
|
13864
|
+
this.#processMMTP(packet, "moq", false, ingress, true);
|
|
13601
13865
|
});
|
|
13602
13866
|
}
|
|
13603
13867
|
/**
|
|
@@ -13610,7 +13874,7 @@ var MmtpRouter = class {
|
|
|
13610
13874
|
this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName);
|
|
13611
13875
|
});
|
|
13612
13876
|
}
|
|
13613
|
-
#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName) {
|
|
13877
|
+
#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName, admitted = false) {
|
|
13614
13878
|
let header;
|
|
13615
13879
|
try {
|
|
13616
13880
|
header = parseMMTPHeader(packet);
|
|
@@ -13627,6 +13891,11 @@ var MmtpRouter = class {
|
|
|
13627
13891
|
`[MmtpRouter] Dropping MoQ repair packet pid=${repairPacketId}: owner hint is required`
|
|
13628
13892
|
);
|
|
13629
13893
|
}
|
|
13894
|
+
if (ownerPacketId != null && !admitted) {
|
|
13895
|
+
const admit = this.#admitUnicast(repairPacketId, packet, () => this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName, true));
|
|
13896
|
+
if (admit === "held" || admit === "dropped") return;
|
|
13897
|
+
if (admit === "unlisted" && this.#setAsideUnlistedRepair(ownerPacketId)) return;
|
|
13898
|
+
}
|
|
13630
13899
|
if (ownerPacketId != null && this.#fecManager) {
|
|
13631
13900
|
this.#moqRepairPidCounts[repairPacketId] = (this.#moqRepairPidCounts[repairPacketId] || 0) + 1;
|
|
13632
13901
|
this.#moqRepairOwnerCounts[ownerPacketId] = (this.#moqRepairOwnerCounts[ownerPacketId] || 0) + 1;
|
|
@@ -13770,10 +14039,11 @@ var MmtpRouter = class {
|
|
|
13770
14039
|
catalogTrackName
|
|
13771
14040
|
);
|
|
13772
14041
|
if (recovered && !alreadyCompleted) {
|
|
13773
|
-
|
|
13774
|
-
|
|
13775
|
-
|
|
13776
|
-
|
|
14042
|
+
this.#routeRecoveredBlock(blockStart, recovered, ownerPacketId, catalogTrackName, () => {
|
|
14043
|
+
fec.cancelBlockTimeout(blockStart, ownerPacketId, catalogTrackName);
|
|
14044
|
+
fec.countRecovery(ownerPacketId, blockStart, catalogTrackName);
|
|
14045
|
+
fec.markBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
|
|
14046
|
+
});
|
|
13777
14047
|
}
|
|
13778
14048
|
}
|
|
13779
14049
|
/**
|
|
@@ -13814,10 +14084,11 @@ var MmtpRouter = class {
|
|
|
13814
14084
|
const alreadyCompleted = fec.isBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
|
|
13815
14085
|
const recovered = fec.feedSourceSymbol(ownerPacketId, ssId, payload.symbolData, catalogTrackName);
|
|
13816
14086
|
if (!recovered || alreadyCompleted) continue;
|
|
13817
|
-
|
|
13818
|
-
|
|
13819
|
-
|
|
13820
|
-
|
|
14087
|
+
this.#routeRecoveredBlock(blockStart, recovered, ownerPacketId, catalogTrackName, () => {
|
|
14088
|
+
fec.cancelBlockTimeout(blockStart, ownerPacketId, catalogTrackName);
|
|
14089
|
+
fec.countRecovery(ownerPacketId, blockStart, catalogTrackName);
|
|
14090
|
+
fec.markBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
|
|
14091
|
+
});
|
|
13821
14092
|
}
|
|
13822
14093
|
}
|
|
13823
14094
|
#resolveRepairOwnerPacketId(packetId, ingress) {
|
|
@@ -13840,14 +14111,18 @@ var MmtpRouter = class {
|
|
|
13840
14111
|
* Sync WASM decoders return the block's K' = SSB_length fixed-width
|
|
13841
14112
|
* ssbg_mode1 source symbols concatenated. */
|
|
13842
14113
|
routeRecoveredBlock(blockId, data, packetId, catalogTrackName) {
|
|
13843
|
-
|
|
14114
|
+
let recovered = false;
|
|
14115
|
+
this.#withDeferredFecPokes(() => {
|
|
14116
|
+
recovered = this.#routeRecoveredBlock(blockId, data, packetId, catalogTrackName);
|
|
14117
|
+
});
|
|
14118
|
+
return recovered;
|
|
13844
14119
|
}
|
|
13845
14120
|
/** Route a single recovered source symbol through the parser + dedup pipeline.
|
|
13846
14121
|
* Worker-backed decoders emit one padded source symbol per frame event. */
|
|
13847
14122
|
routeRecoveredSymbol(blockId, data, packetId, esi = 0, catalogTrackName) {
|
|
13848
14123
|
const blockProfile = this.#blockProfileFor(packetId);
|
|
13849
14124
|
const result = processFecSymbol(data, { ownerPacketId: packetId, index: esi, blockProfile });
|
|
13850
|
-
this.#routeVerifiedFecSymbols(blockId, result, packetId, catalogTrackName);
|
|
14125
|
+
return this.#routeVerifiedFecSymbols(blockId, result, packetId, catalogTrackName);
|
|
13851
14126
|
}
|
|
13852
14127
|
// ── Keyframe timer cancellation ─────────────────────────────────
|
|
13853
14128
|
/** Cancel the active keyframe loss timer.
|
|
@@ -13890,7 +14165,6 @@ var MmtpRouter = class {
|
|
|
13890
14165
|
this.#lastDirectSourceAudioCursorByStream.delete(streamKey);
|
|
13891
14166
|
this.#lastDirectEmittedAudioCursorByStream.delete(streamKey);
|
|
13892
14167
|
this.#pendingDirectRecoveredAudioByStream.delete(streamKey);
|
|
13893
|
-
this.#manifestRecoveryQueues.delete(streamKey);
|
|
13894
14168
|
this.#deliveredVideoHighWaterByStream.delete(streamKey);
|
|
13895
14169
|
this.#stream(packetId, catalogTrackName).duplicates.clear();
|
|
13896
14170
|
const assemblersToReset = /* @__PURE__ */ new Set();
|
|
@@ -13940,7 +14214,6 @@ var MmtpRouter = class {
|
|
|
13940
14214
|
this.#lastDirectSourceAudioCursorByStream.delete(key);
|
|
13941
14215
|
this.#lastDirectEmittedAudioCursorByStream.delete(key);
|
|
13942
14216
|
this.#pendingDirectRecoveredAudioByStream.delete(key);
|
|
13943
|
-
this.#manifestRecoveryQueues.delete(key);
|
|
13944
14217
|
this.#trackInitDelivered.delete(key);
|
|
13945
14218
|
this.#parserDiagnostics.delete(key);
|
|
13946
14219
|
this.#pendingFecPokes.delete(key);
|
|
@@ -14201,8 +14474,9 @@ var MmtpRouter = class {
|
|
|
14201
14474
|
this.#parserDiagnostics.clear();
|
|
14202
14475
|
this.#catalogTrackNamesByPacketId.clear();
|
|
14203
14476
|
this.#repairOwnersByPacketId.clear();
|
|
14204
|
-
|
|
14205
|
-
this.#
|
|
14477
|
+
for (const { receiver } of this.#provenance.values()) receiver.release();
|
|
14478
|
+
this.#provenance.clear();
|
|
14479
|
+
this.#provenanceStreamFlow.clear();
|
|
14206
14480
|
for (const assembler of this.#mseAssemblers.values()) assembler.dispose();
|
|
14207
14481
|
this.#mseAssemblers.clear();
|
|
14208
14482
|
}
|
|
@@ -14479,7 +14753,7 @@ var MmtpRouter = class {
|
|
|
14479
14753
|
* 1. parseHeader() → extract packetId + fecPayloadId for FEC feeding
|
|
14480
14754
|
* 2. feed() → MFU extraction, reassembly, keyframe detection, emit via callbacks
|
|
14481
14755
|
*/
|
|
14482
|
-
#processMMTP(packet, source,
|
|
14756
|
+
#processMMTP(packet, source, manifestAdmitted = false, ingress, overlapCopy = false, releasedFromQuarantine = false) {
|
|
14483
14757
|
const parsed = this.#parser.parsePacket(packet);
|
|
14484
14758
|
if (!parsed) return;
|
|
14485
14759
|
const info = parsed.info;
|
|
@@ -14504,43 +14778,32 @@ var MmtpRouter = class {
|
|
|
14504
14778
|
}
|
|
14505
14779
|
const fecUnreadable = !info.isRepair && info.fecPayloadId != null && this.#fecManager !== null && this.#classifyPacketId(info.packetId) !== null && this.#configuredBlockProfile(info.packetId) === void 0;
|
|
14506
14780
|
if (fecUnreadable && !manifestAdmitted) this.#noteUnreadableFecPayloadId(info.packetId, "source");
|
|
14507
|
-
if (!info.isRepair &&
|
|
14508
|
-
let
|
|
14509
|
-
if (!
|
|
14510
|
-
|
|
14511
|
-
|
|
14512
|
-
|
|
14513
|
-
|
|
14514
|
-
|
|
14515
|
-
|
|
14516
|
-
|
|
14517
|
-
recovered: false,
|
|
14518
|
-
sourceSymbolFormat: "full-mmtp-packet"
|
|
14519
|
-
}) : this.#resolveManifestIdentity(
|
|
14520
|
-
info.packetId,
|
|
14521
|
-
coordinates.sbn,
|
|
14522
|
-
coordinates.esi,
|
|
14523
|
-
catalogTrackName
|
|
14524
|
-
)) ?? void 0;
|
|
14525
|
-
}
|
|
14526
|
-
if (identity) {
|
|
14527
|
-
identity = {
|
|
14528
|
-
...identity,
|
|
14529
|
-
packetId: info.packetId,
|
|
14530
|
-
...info.fecPayloadId ? { ssId: info.fecPayloadId.ssId } : {}
|
|
14531
|
-
};
|
|
14532
|
-
const authenticated = info.fecPayloadId ? packet.subarray(0, packet.length - 4) : packet;
|
|
14533
|
-
let quarantined = false;
|
|
14534
|
-
const admission = this.#manifestVerifier.admitObject(identity, authenticated, () => {
|
|
14535
|
-
if (!this.#disposed) {
|
|
14536
|
-
this.#processMMTP(packet, source, identity, true, ingress, overlapCopy, quarantined);
|
|
14537
|
-
}
|
|
14538
|
-
});
|
|
14539
|
-
quarantined = admission.action === "stall";
|
|
14781
|
+
if (source === "multicast" && manifestAdmitted && !info.isRepair && ingress?.flow !== void 0) {
|
|
14782
|
+
let byName = this.#provenanceStreamFlow.get(info.packetId);
|
|
14783
|
+
if (!byName) this.#provenanceStreamFlow.set(info.packetId, byName = /* @__PURE__ */ new Map());
|
|
14784
|
+
byName.set(catalogTrackNameForPacket(ingress, info.packetId), ingress.flow);
|
|
14785
|
+
}
|
|
14786
|
+
if (source === "multicast" && !manifestAdmitted && ingress?.flow !== void 0) {
|
|
14787
|
+
const protectedFlow = this.#provenance.get(ingress.flow);
|
|
14788
|
+
if (!protectedFlow) {
|
|
14789
|
+
this.#unregisteredFlowDrops++;
|
|
14790
|
+
debugRouter(`[MmtpRouter] Dropping multicast packet pid=${info.packetId}: flow ${ingress.flow} has no receiver`);
|
|
14540
14791
|
return;
|
|
14541
14792
|
}
|
|
14542
|
-
|
|
14543
|
-
|
|
14793
|
+
const { verdict } = protectedFlow.receiver.onDatagram(packet, "multicast");
|
|
14794
|
+
if (verdict === "admitted") {
|
|
14795
|
+
this.#noteFlowAdmitted(ingress.flow);
|
|
14796
|
+
this.#processMMTP(packet, source, true, ingress, overlapCopy);
|
|
14797
|
+
} else if (verdict === "held") {
|
|
14798
|
+
this.#heldIngress.set(packet, ingress);
|
|
14799
|
+
this.#heldAt.set(packet, performance.now());
|
|
14800
|
+
}
|
|
14801
|
+
return;
|
|
14802
|
+
}
|
|
14803
|
+
if (source === "moq" && !manifestAdmitted) {
|
|
14804
|
+
const admit = this.#admitUnicast(info.packetId, packet, () => this.#processMMTP(packet, source, true, ingress, overlapCopy, true), overlapCopy);
|
|
14805
|
+
if (admit !== "unlisted") {
|
|
14806
|
+
if (admit === "admitted") this.#processMMTP(packet, source, true, ingress, overlapCopy);
|
|
14544
14807
|
return;
|
|
14545
14808
|
}
|
|
14546
14809
|
}
|
|
@@ -14550,6 +14813,7 @@ var MmtpRouter = class {
|
|
|
14550
14813
|
const ownerPacketId = this.#resolveRepairOwnerPacketId(info.packetId, ingress);
|
|
14551
14814
|
if (ownerPacketId != null && this.#fecManager) {
|
|
14552
14815
|
if (hasAmbiguousCatalogTrackName(ingress, ownerPacketId)) return;
|
|
14816
|
+
if (source === "moq" && !manifestAdmitted && this.#setAsideUnlistedRepair(ownerPacketId)) return;
|
|
14553
14817
|
this.#repairPidCounts[info.packetId] = (this.#repairPidCounts[info.packetId] || 0) + 1;
|
|
14554
14818
|
const count = this.#repairPidCounts[info.packetId];
|
|
14555
14819
|
if (count <= 2 || count % 500 === 0) {
|
|
@@ -14717,36 +14981,22 @@ var MmtpRouter = class {
|
|
|
14717
14981
|
sourceFec.symbolData,
|
|
14718
14982
|
catalogTrackName
|
|
14719
14983
|
);
|
|
14720
|
-
if (recovered) {
|
|
14721
|
-
|
|
14722
|
-
|
|
14723
|
-
|
|
14724
|
-
|
|
14725
|
-
|
|
14726
|
-
sourceFec.sbn,
|
|
14727
|
-
info.packetId,
|
|
14728
|
-
catalogTrackName
|
|
14729
|
-
);
|
|
14730
|
-
}
|
|
14731
|
-
}
|
|
14732
|
-
if (!alreadyCompleted) {
|
|
14733
|
-
this.#fecManager.countRecovery(info.packetId, sourceFec.sbn, catalogTrackName);
|
|
14984
|
+
if (recovered && !alreadyCompleted) {
|
|
14985
|
+
const fec = this.#fecManager;
|
|
14986
|
+
const sbn = sourceFec.sbn;
|
|
14987
|
+
const packetId = info.packetId;
|
|
14988
|
+
const tracked = this.#classifyPacketId(packetId) !== null;
|
|
14989
|
+
this.#routeRecoveredBlock(sbn, recovered, packetId, catalogTrackName, () => {
|
|
14734
14990
|
if (catalogTrackName === void 0) {
|
|
14735
|
-
|
|
14991
|
+
if (tracked) fec.cancelBlockTimeout(sbn, packetId);
|
|
14992
|
+
fec.countRecovery(packetId, sbn, catalogTrackName);
|
|
14993
|
+
fec.markBlockCompleted(sbn, packetId);
|
|
14736
14994
|
} else {
|
|
14737
|
-
|
|
14738
|
-
|
|
14739
|
-
|
|
14740
|
-
catalogTrackName
|
|
14741
|
-
);
|
|
14995
|
+
if (tracked) fec.cancelBlockTimeout(sbn, packetId, catalogTrackName);
|
|
14996
|
+
fec.countRecovery(packetId, sbn, catalogTrackName);
|
|
14997
|
+
fec.markBlockCompleted(sbn, packetId, catalogTrackName);
|
|
14742
14998
|
}
|
|
14743
|
-
|
|
14744
|
-
sourceFec.sbn,
|
|
14745
|
-
recovered,
|
|
14746
|
-
info.packetId,
|
|
14747
|
-
catalogTrackName
|
|
14748
|
-
);
|
|
14749
|
-
}
|
|
14999
|
+
});
|
|
14750
15000
|
}
|
|
14751
15001
|
}
|
|
14752
15002
|
}
|
|
@@ -15122,7 +15372,7 @@ var MmtpRouter = class {
|
|
|
15122
15372
|
}
|
|
15123
15373
|
// ── Private: FEC recovery routing ───────────────────────────────
|
|
15124
15374
|
/** Route WASM-recovered block through appropriate path based on container kind. */
|
|
15125
|
-
#routeRecoveredBlock(blockId, recovered, ownerPacketId, catalogTrackName) {
|
|
15375
|
+
#routeRecoveredBlock(blockId, recovered, ownerPacketId, catalogTrackName, complete) {
|
|
15126
15376
|
const packetSymbolSize = this.#symbolSizeForPacket(ownerPacketId);
|
|
15127
15377
|
if (ownerPacketId === void 0) {
|
|
15128
15378
|
throw new Error("[MmtpRouter] a recovered FEC block needs its owner packetId to read its fec.blockProfile");
|
|
@@ -15139,103 +15389,68 @@ var MmtpRouter = class {
|
|
|
15139
15389
|
ownerPacketId,
|
|
15140
15390
|
blockProfile
|
|
15141
15391
|
});
|
|
15142
|
-
this.#routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
|
|
15392
|
+
return this.#routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName, complete);
|
|
15143
15393
|
}
|
|
15144
|
-
|
|
15145
|
-
|
|
15146
|
-
|
|
15147
|
-
|
|
15148
|
-
|
|
15149
|
-
|
|
15150
|
-
|
|
15151
|
-
|
|
15152
|
-
|
|
15394
|
+
/**
|
|
15395
|
+
* A recovered source packet of a protected flow is admitted on its own
|
|
15396
|
+
* digest: its bytes with the SS_ID it was sent with (SS_Start + ESI)
|
|
15397
|
+
* appended are the datagram the record listed (7.2.7). One that no record
|
|
15398
|
+
* lists is dropped; holding it would let it out later as a multicast arrival.
|
|
15399
|
+
* Returns whether the decode recovered the block: every packet it yields
|
|
15400
|
+
* that this router has not parsed already is admitted. A block one of whose
|
|
15401
|
+
* recovered packets is refused is unrecovered (7.2.7): its caller leaves it
|
|
15402
|
+
* open, so it reaches its FEC deadline and fails by class. Always true
|
|
15403
|
+
* outside a protected flow.
|
|
15404
|
+
*/
|
|
15405
|
+
#routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName, complete) {
|
|
15406
|
+
const receiver = ownerPacketId == null ? void 0 : this.#provenanceFor(ownerPacketId, catalogTrackName);
|
|
15407
|
+
let recovered = true;
|
|
15408
|
+
if (receiver) {
|
|
15409
|
+
const parsed = this.#stream(ownerPacketId, catalogTrackName).duplicates.parsedSymbols;
|
|
15410
|
+
result = {
|
|
15411
|
+
...result,
|
|
15412
|
+
symbols: result.symbols.filter((sym) => {
|
|
15413
|
+
const ssId = this.#fecAnchorForRecovered(blockId, sym.index);
|
|
15414
|
+
if (parsed.has(ssId)) return false;
|
|
15415
|
+
let admitted = false;
|
|
15416
|
+
if (receiver === "ambiguous") {
|
|
15417
|
+
this.#ambiguousRecoveredDrops++;
|
|
15418
|
+
} else {
|
|
15419
|
+
const wire = new Uint8Array(sym.rawSymbol.byteLength + 4);
|
|
15420
|
+
wire.set(sym.rawSymbol);
|
|
15421
|
+
new DataView(wire.buffer).setUint32(sym.rawSymbol.byteLength, ssId);
|
|
15422
|
+
admitted = receiver.admitRecovered(wire);
|
|
15423
|
+
}
|
|
15424
|
+
if (!admitted) recovered = false;
|
|
15425
|
+
return admitted;
|
|
15426
|
+
})
|
|
15427
|
+
};
|
|
15153
15428
|
}
|
|
15154
|
-
|
|
15155
|
-
|
|
15156
|
-
|
|
15157
|
-
|
|
15158
|
-
|
|
15159
|
-
|
|
15160
|
-
|
|
15161
|
-
|
|
15162
|
-
|
|
15163
|
-
|
|
15429
|
+
if (recovered) complete?.();
|
|
15430
|
+
this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
|
|
15431
|
+
return recovered;
|
|
15432
|
+
}
|
|
15433
|
+
/**
|
|
15434
|
+
* The receiver a recovered packet of `packetId` answers to: the flow that
|
|
15435
|
+
* admitted the stream's source datagrams. Before any was admitted, the one
|
|
15436
|
+
* flow whose registration lists the stream; more than one (a repair flow
|
|
15437
|
+
* also lists its owners' source tracks) cannot be told apart, so none.
|
|
15438
|
+
*/
|
|
15439
|
+
#provenanceFor(packetId, catalogTrackName) {
|
|
15440
|
+
const byName = this.#provenanceStreamFlow.get(packetId);
|
|
15441
|
+
const flows = new Set(
|
|
15442
|
+
catalogTrackName === void 0 || !byName ? byName?.values() : byName.has(catalogTrackName) ? [byName.get(catalogTrackName)] : []
|
|
15443
|
+
);
|
|
15444
|
+
if (flows.size > 1) return "ambiguous";
|
|
15445
|
+
for (const bound of flows) return this.#provenance.get(bound)?.receiver ?? "ambiguous";
|
|
15446
|
+
let match;
|
|
15447
|
+
for (const { receiver, ingress } of this.#provenance.values()) {
|
|
15448
|
+
if (ingress.tracks.some((t) => t.packetId === packetId && (catalogTrackName === void 0 || t.name === catalogTrackName))) {
|
|
15449
|
+
if (match) return "ambiguous";
|
|
15450
|
+
match = receiver;
|
|
15164
15451
|
}
|
|
15165
|
-
this.#admitRecoveredResult(
|
|
15166
|
-
next.blockId,
|
|
15167
|
-
next.result,
|
|
15168
|
-
next.ownerPacketId,
|
|
15169
|
-
next.catalogTrackName,
|
|
15170
|
-
() => {
|
|
15171
|
-
queue.shift();
|
|
15172
|
-
drain();
|
|
15173
|
-
}
|
|
15174
|
-
);
|
|
15175
|
-
};
|
|
15176
|
-
drain();
|
|
15177
|
-
}
|
|
15178
|
-
#admitRecoveredResult(blockId, result, ownerPacketId, catalogTrackName, done) {
|
|
15179
|
-
const verifier = this.#manifestVerifier;
|
|
15180
|
-
const resolveIdentity = this.#resolveManifestIdentity;
|
|
15181
|
-
const resolveIdentityForRoute = this.#resolveManifestIdentityForRoute;
|
|
15182
|
-
if (!verifier) {
|
|
15183
|
-
this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
|
|
15184
|
-
done();
|
|
15185
|
-
return;
|
|
15186
15452
|
}
|
|
15187
|
-
|
|
15188
|
-
const symbols = result.symbols;
|
|
15189
|
-
const admitNext = (index) => {
|
|
15190
|
-
const symbol = symbols[index];
|
|
15191
|
-
if (!symbol) {
|
|
15192
|
-
done();
|
|
15193
|
-
return;
|
|
15194
|
-
}
|
|
15195
|
-
const identity = resolveIdentityForRoute ? resolveIdentityForRoute({
|
|
15196
|
-
packetId: ownerPacketId,
|
|
15197
|
-
sbn: blockId,
|
|
15198
|
-
esi: symbol.index,
|
|
15199
|
-
...catalogTrackName === void 0 ? {} : { catalogTrackName },
|
|
15200
|
-
recovered: true,
|
|
15201
|
-
sourceSymbolFormat: "fec-padded-recovery"
|
|
15202
|
-
}) : resolveIdentity(ownerPacketId, blockId, symbol.index, catalogTrackName);
|
|
15203
|
-
if (!identity) {
|
|
15204
|
-
console.warn(`[MmtpRouter] manifest identity unresolved; recovered symbol is fail-open for packetId=${ownerPacketId} block=${blockId} esi=${symbol.index}`);
|
|
15205
|
-
this.#routeProcessedFecSymbols(
|
|
15206
|
-
blockId,
|
|
15207
|
-
{ ...result, symbols: [symbol] },
|
|
15208
|
-
ownerPacketId,
|
|
15209
|
-
catalogTrackName
|
|
15210
|
-
);
|
|
15211
|
-
admitNext(index + 1);
|
|
15212
|
-
return;
|
|
15213
|
-
}
|
|
15214
|
-
const admission = verifier.admitObject(
|
|
15215
|
-
{
|
|
15216
|
-
...identity,
|
|
15217
|
-
packetId: ownerPacketId
|
|
15218
|
-
},
|
|
15219
|
-
symbol.rawSymbol,
|
|
15220
|
-
() => {
|
|
15221
|
-
if (!this.#disposed) {
|
|
15222
|
-
this.#routeProcessedFecSymbols(
|
|
15223
|
-
blockId,
|
|
15224
|
-
{ ...result, symbols: [symbol] },
|
|
15225
|
-
ownerPacketId,
|
|
15226
|
-
catalogTrackName
|
|
15227
|
-
);
|
|
15228
|
-
}
|
|
15229
|
-
admitNext(index + 1);
|
|
15230
|
-
},
|
|
15231
|
-
// A stalled symbol that is later discarded instead of played must still
|
|
15232
|
-
// advance this loop; enforce mode never calls the release callback for
|
|
15233
|
-
// it (BLO-30095).
|
|
15234
|
-
() => admitNext(index + 1)
|
|
15235
|
-
);
|
|
15236
|
-
if (admission.action === "drop") admitNext(index + 1);
|
|
15237
|
-
};
|
|
15238
|
-
admitNext(0);
|
|
15453
|
+
return match;
|
|
15239
15454
|
}
|
|
15240
15455
|
#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName) {
|
|
15241
15456
|
if (ownerPacketId == null) return;
|
|
@@ -16215,14 +16430,22 @@ function toWindowMulticastSubscribeConfig(input) {
|
|
|
16215
16430
|
if (input.packetIds?.some((id) => !isMulticastTrackPacketId(id)) || input.tracks?.some((track) => !isMulticastTrackPacketId(track.packetId))) {
|
|
16216
16431
|
throw new RangeError(`multicast packetId must be an integer from 1 to ${MMTP_PACKET_ID_MAX2}; packetId 0 is the MMTP signaling flow and is never a track's`);
|
|
16217
16432
|
}
|
|
16433
|
+
const source = input.source?.trim() || void 0;
|
|
16434
|
+
const senderAddress = input.senderAddress?.trim() || void 0;
|
|
16435
|
+
if (source && senderAddress) {
|
|
16436
|
+
throw new TypeError("multicast senderAddress is an ASM filter and never appears beside source");
|
|
16437
|
+
}
|
|
16218
16438
|
const base = {
|
|
16219
16439
|
group: input.group,
|
|
16220
16440
|
port: input.port,
|
|
16221
|
-
...
|
|
16441
|
+
...source ? { source } : {},
|
|
16222
16442
|
consume: "bytes",
|
|
16223
16443
|
...input.packetIds?.length ? { packetIds: [...input.packetIds] } : {}
|
|
16224
16444
|
};
|
|
16445
|
+
const filter = senderAddress ? { senderAddress } : {};
|
|
16225
16446
|
if (input.atsc3) {
|
|
16447
|
+
if (senderAddress)
|
|
16448
|
+
throw new TypeError("an ATSC3 tuner request carries no senderAddress");
|
|
16226
16449
|
const { frequency, plpId, serviceId } = input.atsc3;
|
|
16227
16450
|
if (!Number.isInteger(frequency) || frequency < 1) {
|
|
16228
16451
|
throw new TypeError(`ATSC3 tuning requires a positive integer frequency in kHz, got ${String(frequency)}`);
|
|
@@ -16242,6 +16465,7 @@ function toWindowMulticastSubscribeConfig(input) {
|
|
|
16242
16465
|
if (!wantsAmt) {
|
|
16243
16466
|
const native = {
|
|
16244
16467
|
...base,
|
|
16468
|
+
...filter,
|
|
16245
16469
|
origin: "ip-multicast",
|
|
16246
16470
|
...input.tracks?.length ? { tracks: input.tracks.map(({ name, packetId }) => ({ name, packetId })) } : {}
|
|
16247
16471
|
};
|
|
@@ -16267,6 +16491,7 @@ function toWindowMulticastSubscribeConfig(input) {
|
|
|
16267
16491
|
}
|
|
16268
16492
|
return {
|
|
16269
16493
|
...base,
|
|
16494
|
+
...filter,
|
|
16270
16495
|
origin: "amt",
|
|
16271
16496
|
relay,
|
|
16272
16497
|
relayPort
|
|
@@ -16292,6 +16517,9 @@ function isValidMulticastGroupAddress(address) {
|
|
|
16292
16517
|
function normalizeMulticastEndpoint(endpoint) {
|
|
16293
16518
|
const groupAddress = endpoint.groupAddress.trim().toLowerCase();
|
|
16294
16519
|
const sourceAddress = endpoint.sourceAddress?.trim().toLowerCase() || void 0;
|
|
16520
|
+
const senderAddress = endpoint.senderAddress?.trim().toLowerCase() || void 0;
|
|
16521
|
+
if (sourceAddress && senderAddress)
|
|
16522
|
+
return void 0;
|
|
16295
16523
|
const amtRelay = endpoint.amtRelay?.trim().toLowerCase() || void 0;
|
|
16296
16524
|
if (!isValidMulticastGroupAddress(groupAddress) || !isPositivePort(endpoint.port))
|
|
16297
16525
|
return void 0;
|
|
@@ -16302,6 +16530,7 @@ function normalizeMulticastEndpoint(endpoint) {
|
|
|
16302
16530
|
return {
|
|
16303
16531
|
groupAddress,
|
|
16304
16532
|
...sourceAddress ? { sourceAddress } : {},
|
|
16533
|
+
...senderAddress ? { senderAddress } : {},
|
|
16305
16534
|
port: endpoint.port,
|
|
16306
16535
|
...endpoint.mode ? { mode: endpoint.mode } : {},
|
|
16307
16536
|
...amtRelay ? { amtRelay } : {},
|
|
@@ -16321,7 +16550,9 @@ function multicastEndpointKey(endpoint) {
|
|
|
16321
16550
|
normalized.amtRelay ?? "",
|
|
16322
16551
|
normalized.amtRelayPort ?? "",
|
|
16323
16552
|
// A DRIAD join is a different gateway subscription from a native one.
|
|
16324
|
-
...normalized.amtDiscovery ? [normalized.amtDiscovery] : []
|
|
16553
|
+
...normalized.amtDiscovery ? [normalized.amtDiscovery] : [],
|
|
16554
|
+
// An ASM sender filter is a different subscription from the unfiltered join.
|
|
16555
|
+
...normalized.senderAddress ? [{ senderAddress: normalized.senderAddress }] : []
|
|
16325
16556
|
]);
|
|
16326
16557
|
}
|
|
16327
16558
|
function isMulticastTrackPacketId(value) {
|
|
@@ -17257,10 +17488,11 @@ var MulticastTransport = class {
|
|
|
17257
17488
|
try {
|
|
17258
17489
|
const packetIds = config.packetIds?.length ? new Set(config.packetIds) : null;
|
|
17259
17490
|
console.log(
|
|
17260
|
-
`[MulticastTransport] window.multicast.subscribe(${config.sourceAddress}, ${config.groupAddress}, ${config.port})`
|
|
17491
|
+
`[MulticastTransport] window.multicast.subscribe(${config.sourceAddress ?? config.senderAddress ?? "*"}, ${config.groupAddress}, ${config.port})`
|
|
17261
17492
|
);
|
|
17262
17493
|
const subscribeConfig = toWindowMulticastSubscribeConfig({
|
|
17263
17494
|
source: config.sourceAddress,
|
|
17495
|
+
senderAddress: config.senderAddress,
|
|
17264
17496
|
group: config.groupAddress,
|
|
17265
17497
|
port: config.port,
|
|
17266
17498
|
mode: config.mode,
|
|
@@ -17856,16 +18088,2736 @@ var MulticastTransport = class {
|
|
|
17856
18088
|
}
|
|
17857
18089
|
};
|
|
17858
18090
|
|
|
17859
|
-
//
|
|
17860
|
-
|
|
18091
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/crypto.js
|
|
18092
|
+
var crypto2 = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
17861
18093
|
|
|
17862
|
-
//
|
|
17863
|
-
|
|
17864
|
-
|
|
17865
|
-
|
|
17866
|
-
|
|
17867
|
-
|
|
17868
|
-
|
|
18094
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/utils.js
|
|
18095
|
+
function isBytes(a) {
|
|
18096
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
18097
|
+
}
|
|
18098
|
+
function anumber(n) {
|
|
18099
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
18100
|
+
throw new Error("positive integer expected, got " + n);
|
|
18101
|
+
}
|
|
18102
|
+
function abytes(b, ...lengths) {
|
|
18103
|
+
if (!isBytes(b))
|
|
18104
|
+
throw new Error("Uint8Array expected");
|
|
18105
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
18106
|
+
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
18107
|
+
}
|
|
18108
|
+
function aexists(instance, checkFinished = true) {
|
|
18109
|
+
if (instance.destroyed)
|
|
18110
|
+
throw new Error("Hash instance has been destroyed");
|
|
18111
|
+
if (checkFinished && instance.finished)
|
|
18112
|
+
throw new Error("Hash#digest() has already been called");
|
|
18113
|
+
}
|
|
18114
|
+
function aoutput(out, instance) {
|
|
18115
|
+
abytes(out);
|
|
18116
|
+
const min = instance.outputLen;
|
|
18117
|
+
if (out.length < min) {
|
|
18118
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
18119
|
+
}
|
|
18120
|
+
}
|
|
18121
|
+
function clean(...arrays) {
|
|
18122
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
18123
|
+
arrays[i].fill(0);
|
|
18124
|
+
}
|
|
18125
|
+
}
|
|
18126
|
+
function createView(arr) {
|
|
18127
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
18128
|
+
}
|
|
18129
|
+
function rotr(word, shift) {
|
|
18130
|
+
return word << 32 - shift | word >>> shift;
|
|
18131
|
+
}
|
|
18132
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
18133
|
+
// @ts-ignore
|
|
18134
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
18135
|
+
))();
|
|
18136
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
18137
|
+
function bytesToHex(bytes) {
|
|
18138
|
+
abytes(bytes);
|
|
18139
|
+
if (hasHexBuiltin)
|
|
18140
|
+
return bytes.toHex();
|
|
18141
|
+
let hex2 = "";
|
|
18142
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
18143
|
+
hex2 += hexes[bytes[i]];
|
|
18144
|
+
}
|
|
18145
|
+
return hex2;
|
|
18146
|
+
}
|
|
18147
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
18148
|
+
function asciiToBase16(ch) {
|
|
18149
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
18150
|
+
return ch - asciis._0;
|
|
18151
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
18152
|
+
return ch - (asciis.A - 10);
|
|
18153
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
18154
|
+
return ch - (asciis.a - 10);
|
|
18155
|
+
return;
|
|
18156
|
+
}
|
|
18157
|
+
function hexToBytes(hex2) {
|
|
18158
|
+
if (typeof hex2 !== "string")
|
|
18159
|
+
throw new Error("hex string expected, got " + typeof hex2);
|
|
18160
|
+
if (hasHexBuiltin)
|
|
18161
|
+
return Uint8Array.fromHex(hex2);
|
|
18162
|
+
const hl = hex2.length;
|
|
18163
|
+
const al = hl / 2;
|
|
18164
|
+
if (hl % 2)
|
|
18165
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
18166
|
+
const array = new Uint8Array(al);
|
|
18167
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
18168
|
+
const n1 = asciiToBase16(hex2.charCodeAt(hi));
|
|
18169
|
+
const n2 = asciiToBase16(hex2.charCodeAt(hi + 1));
|
|
18170
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
18171
|
+
const char = hex2[hi] + hex2[hi + 1];
|
|
18172
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
18173
|
+
}
|
|
18174
|
+
array[ai] = n1 * 16 + n2;
|
|
18175
|
+
}
|
|
18176
|
+
return array;
|
|
18177
|
+
}
|
|
18178
|
+
function utf8ToBytes(str) {
|
|
18179
|
+
if (typeof str !== "string")
|
|
18180
|
+
throw new Error("string expected");
|
|
18181
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
18182
|
+
}
|
|
18183
|
+
function toBytes(data) {
|
|
18184
|
+
if (typeof data === "string")
|
|
18185
|
+
data = utf8ToBytes(data);
|
|
18186
|
+
abytes(data);
|
|
18187
|
+
return data;
|
|
18188
|
+
}
|
|
18189
|
+
function concatBytes(...arrays) {
|
|
18190
|
+
let sum = 0;
|
|
18191
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
18192
|
+
const a = arrays[i];
|
|
18193
|
+
abytes(a);
|
|
18194
|
+
sum += a.length;
|
|
18195
|
+
}
|
|
18196
|
+
const res = new Uint8Array(sum);
|
|
18197
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
18198
|
+
const a = arrays[i];
|
|
18199
|
+
res.set(a, pad);
|
|
18200
|
+
pad += a.length;
|
|
18201
|
+
}
|
|
18202
|
+
return res;
|
|
18203
|
+
}
|
|
18204
|
+
var Hash = class {
|
|
18205
|
+
};
|
|
18206
|
+
function createHasher(hashCons) {
|
|
18207
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
18208
|
+
const tmp = hashCons();
|
|
18209
|
+
hashC.outputLen = tmp.outputLen;
|
|
18210
|
+
hashC.blockLen = tmp.blockLen;
|
|
18211
|
+
hashC.create = () => hashCons();
|
|
18212
|
+
return hashC;
|
|
18213
|
+
}
|
|
18214
|
+
function randomBytes(bytesLength = 32) {
|
|
18215
|
+
if (crypto2 && typeof crypto2.getRandomValues === "function") {
|
|
18216
|
+
return crypto2.getRandomValues(new Uint8Array(bytesLength));
|
|
18217
|
+
}
|
|
18218
|
+
if (crypto2 && typeof crypto2.randomBytes === "function") {
|
|
18219
|
+
return Uint8Array.from(crypto2.randomBytes(bytesLength));
|
|
18220
|
+
}
|
|
18221
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
18222
|
+
}
|
|
18223
|
+
|
|
18224
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_md.js
|
|
18225
|
+
function setBigUint64(view2, byteOffset, value, isLE) {
|
|
18226
|
+
if (typeof view2.setBigUint64 === "function")
|
|
18227
|
+
return view2.setBigUint64(byteOffset, value, isLE);
|
|
18228
|
+
const _32n2 = BigInt(32);
|
|
18229
|
+
const _u32_max = BigInt(4294967295);
|
|
18230
|
+
const wh = Number(value >> _32n2 & _u32_max);
|
|
18231
|
+
const wl = Number(value & _u32_max);
|
|
18232
|
+
const h = isLE ? 4 : 0;
|
|
18233
|
+
const l = isLE ? 0 : 4;
|
|
18234
|
+
view2.setUint32(byteOffset + h, wh, isLE);
|
|
18235
|
+
view2.setUint32(byteOffset + l, wl, isLE);
|
|
18236
|
+
}
|
|
18237
|
+
function Chi(a, b, c) {
|
|
18238
|
+
return a & b ^ ~a & c;
|
|
18239
|
+
}
|
|
18240
|
+
function Maj(a, b, c) {
|
|
18241
|
+
return a & b ^ a & c ^ b & c;
|
|
18242
|
+
}
|
|
18243
|
+
var HashMD = class extends Hash {
|
|
18244
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
18245
|
+
super();
|
|
18246
|
+
this.finished = false;
|
|
18247
|
+
this.length = 0;
|
|
18248
|
+
this.pos = 0;
|
|
18249
|
+
this.destroyed = false;
|
|
18250
|
+
this.blockLen = blockLen;
|
|
18251
|
+
this.outputLen = outputLen;
|
|
18252
|
+
this.padOffset = padOffset;
|
|
18253
|
+
this.isLE = isLE;
|
|
18254
|
+
this.buffer = new Uint8Array(blockLen);
|
|
18255
|
+
this.view = createView(this.buffer);
|
|
18256
|
+
}
|
|
18257
|
+
update(data) {
|
|
18258
|
+
aexists(this);
|
|
18259
|
+
data = toBytes(data);
|
|
18260
|
+
abytes(data);
|
|
18261
|
+
const { view: view2, buffer, blockLen } = this;
|
|
18262
|
+
const len = data.length;
|
|
18263
|
+
for (let pos = 0; pos < len; ) {
|
|
18264
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
18265
|
+
if (take === blockLen) {
|
|
18266
|
+
const dataView = createView(data);
|
|
18267
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
18268
|
+
this.process(dataView, pos);
|
|
18269
|
+
continue;
|
|
18270
|
+
}
|
|
18271
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
18272
|
+
this.pos += take;
|
|
18273
|
+
pos += take;
|
|
18274
|
+
if (this.pos === blockLen) {
|
|
18275
|
+
this.process(view2, 0);
|
|
18276
|
+
this.pos = 0;
|
|
18277
|
+
}
|
|
18278
|
+
}
|
|
18279
|
+
this.length += data.length;
|
|
18280
|
+
this.roundClean();
|
|
18281
|
+
return this;
|
|
18282
|
+
}
|
|
18283
|
+
digestInto(out) {
|
|
18284
|
+
aexists(this);
|
|
18285
|
+
aoutput(out, this);
|
|
18286
|
+
this.finished = true;
|
|
18287
|
+
const { buffer, view: view2, blockLen, isLE } = this;
|
|
18288
|
+
let { pos } = this;
|
|
18289
|
+
buffer[pos++] = 128;
|
|
18290
|
+
clean(this.buffer.subarray(pos));
|
|
18291
|
+
if (this.padOffset > blockLen - pos) {
|
|
18292
|
+
this.process(view2, 0);
|
|
18293
|
+
pos = 0;
|
|
18294
|
+
}
|
|
18295
|
+
for (let i = pos; i < blockLen; i++)
|
|
18296
|
+
buffer[i] = 0;
|
|
18297
|
+
setBigUint64(view2, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
18298
|
+
this.process(view2, 0);
|
|
18299
|
+
const oview = createView(out);
|
|
18300
|
+
const len = this.outputLen;
|
|
18301
|
+
if (len % 4)
|
|
18302
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
18303
|
+
const outLen = len / 4;
|
|
18304
|
+
const state = this.get();
|
|
18305
|
+
if (outLen > state.length)
|
|
18306
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
18307
|
+
for (let i = 0; i < outLen; i++)
|
|
18308
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
18309
|
+
}
|
|
18310
|
+
digest() {
|
|
18311
|
+
const { buffer, outputLen } = this;
|
|
18312
|
+
this.digestInto(buffer);
|
|
18313
|
+
const res = buffer.slice(0, outputLen);
|
|
18314
|
+
this.destroy();
|
|
18315
|
+
return res;
|
|
18316
|
+
}
|
|
18317
|
+
_cloneInto(to) {
|
|
18318
|
+
to || (to = new this.constructor());
|
|
18319
|
+
to.set(...this.get());
|
|
18320
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
18321
|
+
to.destroyed = destroyed;
|
|
18322
|
+
to.finished = finished;
|
|
18323
|
+
to.length = length;
|
|
18324
|
+
to.pos = pos;
|
|
18325
|
+
if (length % blockLen)
|
|
18326
|
+
to.buffer.set(buffer);
|
|
18327
|
+
return to;
|
|
18328
|
+
}
|
|
18329
|
+
clone() {
|
|
18330
|
+
return this._cloneInto();
|
|
18331
|
+
}
|
|
18332
|
+
};
|
|
18333
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
18334
|
+
1779033703,
|
|
18335
|
+
3144134277,
|
|
18336
|
+
1013904242,
|
|
18337
|
+
2773480762,
|
|
18338
|
+
1359893119,
|
|
18339
|
+
2600822924,
|
|
18340
|
+
528734635,
|
|
18341
|
+
1541459225
|
|
18342
|
+
]);
|
|
18343
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
18344
|
+
1779033703,
|
|
18345
|
+
4089235720,
|
|
18346
|
+
3144134277,
|
|
18347
|
+
2227873595,
|
|
18348
|
+
1013904242,
|
|
18349
|
+
4271175723,
|
|
18350
|
+
2773480762,
|
|
18351
|
+
1595750129,
|
|
18352
|
+
1359893119,
|
|
18353
|
+
2917565137,
|
|
18354
|
+
2600822924,
|
|
18355
|
+
725511199,
|
|
18356
|
+
528734635,
|
|
18357
|
+
4215389547,
|
|
18358
|
+
1541459225,
|
|
18359
|
+
327033209
|
|
18360
|
+
]);
|
|
18361
|
+
|
|
18362
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_u64.js
|
|
18363
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
18364
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
18365
|
+
function fromBig(n, le = false) {
|
|
18366
|
+
if (le)
|
|
18367
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
18368
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
18369
|
+
}
|
|
18370
|
+
function split(lst, le = false) {
|
|
18371
|
+
const len = lst.length;
|
|
18372
|
+
let Ah = new Uint32Array(len);
|
|
18373
|
+
let Al = new Uint32Array(len);
|
|
18374
|
+
for (let i = 0; i < len; i++) {
|
|
18375
|
+
const { h, l } = fromBig(lst[i], le);
|
|
18376
|
+
[Ah[i], Al[i]] = [h, l];
|
|
18377
|
+
}
|
|
18378
|
+
return [Ah, Al];
|
|
18379
|
+
}
|
|
18380
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
18381
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
18382
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
18383
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
18384
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
18385
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
18386
|
+
function add(Ah, Al, Bh, Bl) {
|
|
18387
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
18388
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
18389
|
+
}
|
|
18390
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
18391
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
18392
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
18393
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
18394
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
18395
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
18396
|
+
|
|
18397
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha2.js
|
|
18398
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
18399
|
+
1116352408,
|
|
18400
|
+
1899447441,
|
|
18401
|
+
3049323471,
|
|
18402
|
+
3921009573,
|
|
18403
|
+
961987163,
|
|
18404
|
+
1508970993,
|
|
18405
|
+
2453635748,
|
|
18406
|
+
2870763221,
|
|
18407
|
+
3624381080,
|
|
18408
|
+
310598401,
|
|
18409
|
+
607225278,
|
|
18410
|
+
1426881987,
|
|
18411
|
+
1925078388,
|
|
18412
|
+
2162078206,
|
|
18413
|
+
2614888103,
|
|
18414
|
+
3248222580,
|
|
18415
|
+
3835390401,
|
|
18416
|
+
4022224774,
|
|
18417
|
+
264347078,
|
|
18418
|
+
604807628,
|
|
18419
|
+
770255983,
|
|
18420
|
+
1249150122,
|
|
18421
|
+
1555081692,
|
|
18422
|
+
1996064986,
|
|
18423
|
+
2554220882,
|
|
18424
|
+
2821834349,
|
|
18425
|
+
2952996808,
|
|
18426
|
+
3210313671,
|
|
18427
|
+
3336571891,
|
|
18428
|
+
3584528711,
|
|
18429
|
+
113926993,
|
|
18430
|
+
338241895,
|
|
18431
|
+
666307205,
|
|
18432
|
+
773529912,
|
|
18433
|
+
1294757372,
|
|
18434
|
+
1396182291,
|
|
18435
|
+
1695183700,
|
|
18436
|
+
1986661051,
|
|
18437
|
+
2177026350,
|
|
18438
|
+
2456956037,
|
|
18439
|
+
2730485921,
|
|
18440
|
+
2820302411,
|
|
18441
|
+
3259730800,
|
|
18442
|
+
3345764771,
|
|
18443
|
+
3516065817,
|
|
18444
|
+
3600352804,
|
|
18445
|
+
4094571909,
|
|
18446
|
+
275423344,
|
|
18447
|
+
430227734,
|
|
18448
|
+
506948616,
|
|
18449
|
+
659060556,
|
|
18450
|
+
883997877,
|
|
18451
|
+
958139571,
|
|
18452
|
+
1322822218,
|
|
18453
|
+
1537002063,
|
|
18454
|
+
1747873779,
|
|
18455
|
+
1955562222,
|
|
18456
|
+
2024104815,
|
|
18457
|
+
2227730452,
|
|
18458
|
+
2361852424,
|
|
18459
|
+
2428436474,
|
|
18460
|
+
2756734187,
|
|
18461
|
+
3204031479,
|
|
18462
|
+
3329325298
|
|
18463
|
+
]);
|
|
18464
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
18465
|
+
var SHA256 = class extends HashMD {
|
|
18466
|
+
constructor(outputLen = 32) {
|
|
18467
|
+
super(64, outputLen, 8, false);
|
|
18468
|
+
this.A = SHA256_IV[0] | 0;
|
|
18469
|
+
this.B = SHA256_IV[1] | 0;
|
|
18470
|
+
this.C = SHA256_IV[2] | 0;
|
|
18471
|
+
this.D = SHA256_IV[3] | 0;
|
|
18472
|
+
this.E = SHA256_IV[4] | 0;
|
|
18473
|
+
this.F = SHA256_IV[5] | 0;
|
|
18474
|
+
this.G = SHA256_IV[6] | 0;
|
|
18475
|
+
this.H = SHA256_IV[7] | 0;
|
|
18476
|
+
}
|
|
18477
|
+
get() {
|
|
18478
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
18479
|
+
return [A, B, C, D, E, F, G, H];
|
|
18480
|
+
}
|
|
18481
|
+
// prettier-ignore
|
|
18482
|
+
set(A, B, C, D, E, F, G, H) {
|
|
18483
|
+
this.A = A | 0;
|
|
18484
|
+
this.B = B | 0;
|
|
18485
|
+
this.C = C | 0;
|
|
18486
|
+
this.D = D | 0;
|
|
18487
|
+
this.E = E | 0;
|
|
18488
|
+
this.F = F | 0;
|
|
18489
|
+
this.G = G | 0;
|
|
18490
|
+
this.H = H | 0;
|
|
18491
|
+
}
|
|
18492
|
+
process(view2, offset) {
|
|
18493
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
18494
|
+
SHA256_W[i] = view2.getUint32(offset, false);
|
|
18495
|
+
for (let i = 16; i < 64; i++) {
|
|
18496
|
+
const W15 = SHA256_W[i - 15];
|
|
18497
|
+
const W2 = SHA256_W[i - 2];
|
|
18498
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
18499
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
18500
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
18501
|
+
}
|
|
18502
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
18503
|
+
for (let i = 0; i < 64; i++) {
|
|
18504
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
18505
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
18506
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
18507
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
18508
|
+
H = G;
|
|
18509
|
+
G = F;
|
|
18510
|
+
F = E;
|
|
18511
|
+
E = D + T1 | 0;
|
|
18512
|
+
D = C;
|
|
18513
|
+
C = B;
|
|
18514
|
+
B = A;
|
|
18515
|
+
A = T1 + T2 | 0;
|
|
18516
|
+
}
|
|
18517
|
+
A = A + this.A | 0;
|
|
18518
|
+
B = B + this.B | 0;
|
|
18519
|
+
C = C + this.C | 0;
|
|
18520
|
+
D = D + this.D | 0;
|
|
18521
|
+
E = E + this.E | 0;
|
|
18522
|
+
F = F + this.F | 0;
|
|
18523
|
+
G = G + this.G | 0;
|
|
18524
|
+
H = H + this.H | 0;
|
|
18525
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
18526
|
+
}
|
|
18527
|
+
roundClean() {
|
|
18528
|
+
clean(SHA256_W);
|
|
18529
|
+
}
|
|
18530
|
+
destroy() {
|
|
18531
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
18532
|
+
clean(this.buffer);
|
|
18533
|
+
}
|
|
18534
|
+
};
|
|
18535
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
18536
|
+
"0x428a2f98d728ae22",
|
|
18537
|
+
"0x7137449123ef65cd",
|
|
18538
|
+
"0xb5c0fbcfec4d3b2f",
|
|
18539
|
+
"0xe9b5dba58189dbbc",
|
|
18540
|
+
"0x3956c25bf348b538",
|
|
18541
|
+
"0x59f111f1b605d019",
|
|
18542
|
+
"0x923f82a4af194f9b",
|
|
18543
|
+
"0xab1c5ed5da6d8118",
|
|
18544
|
+
"0xd807aa98a3030242",
|
|
18545
|
+
"0x12835b0145706fbe",
|
|
18546
|
+
"0x243185be4ee4b28c",
|
|
18547
|
+
"0x550c7dc3d5ffb4e2",
|
|
18548
|
+
"0x72be5d74f27b896f",
|
|
18549
|
+
"0x80deb1fe3b1696b1",
|
|
18550
|
+
"0x9bdc06a725c71235",
|
|
18551
|
+
"0xc19bf174cf692694",
|
|
18552
|
+
"0xe49b69c19ef14ad2",
|
|
18553
|
+
"0xefbe4786384f25e3",
|
|
18554
|
+
"0x0fc19dc68b8cd5b5",
|
|
18555
|
+
"0x240ca1cc77ac9c65",
|
|
18556
|
+
"0x2de92c6f592b0275",
|
|
18557
|
+
"0x4a7484aa6ea6e483",
|
|
18558
|
+
"0x5cb0a9dcbd41fbd4",
|
|
18559
|
+
"0x76f988da831153b5",
|
|
18560
|
+
"0x983e5152ee66dfab",
|
|
18561
|
+
"0xa831c66d2db43210",
|
|
18562
|
+
"0xb00327c898fb213f",
|
|
18563
|
+
"0xbf597fc7beef0ee4",
|
|
18564
|
+
"0xc6e00bf33da88fc2",
|
|
18565
|
+
"0xd5a79147930aa725",
|
|
18566
|
+
"0x06ca6351e003826f",
|
|
18567
|
+
"0x142929670a0e6e70",
|
|
18568
|
+
"0x27b70a8546d22ffc",
|
|
18569
|
+
"0x2e1b21385c26c926",
|
|
18570
|
+
"0x4d2c6dfc5ac42aed",
|
|
18571
|
+
"0x53380d139d95b3df",
|
|
18572
|
+
"0x650a73548baf63de",
|
|
18573
|
+
"0x766a0abb3c77b2a8",
|
|
18574
|
+
"0x81c2c92e47edaee6",
|
|
18575
|
+
"0x92722c851482353b",
|
|
18576
|
+
"0xa2bfe8a14cf10364",
|
|
18577
|
+
"0xa81a664bbc423001",
|
|
18578
|
+
"0xc24b8b70d0f89791",
|
|
18579
|
+
"0xc76c51a30654be30",
|
|
18580
|
+
"0xd192e819d6ef5218",
|
|
18581
|
+
"0xd69906245565a910",
|
|
18582
|
+
"0xf40e35855771202a",
|
|
18583
|
+
"0x106aa07032bbd1b8",
|
|
18584
|
+
"0x19a4c116b8d2d0c8",
|
|
18585
|
+
"0x1e376c085141ab53",
|
|
18586
|
+
"0x2748774cdf8eeb99",
|
|
18587
|
+
"0x34b0bcb5e19b48a8",
|
|
18588
|
+
"0x391c0cb3c5c95a63",
|
|
18589
|
+
"0x4ed8aa4ae3418acb",
|
|
18590
|
+
"0x5b9cca4f7763e373",
|
|
18591
|
+
"0x682e6ff3d6b2b8a3",
|
|
18592
|
+
"0x748f82ee5defb2fc",
|
|
18593
|
+
"0x78a5636f43172f60",
|
|
18594
|
+
"0x84c87814a1f0ab72",
|
|
18595
|
+
"0x8cc702081a6439ec",
|
|
18596
|
+
"0x90befffa23631e28",
|
|
18597
|
+
"0xa4506cebde82bde9",
|
|
18598
|
+
"0xbef9a3f7b2c67915",
|
|
18599
|
+
"0xc67178f2e372532b",
|
|
18600
|
+
"0xca273eceea26619c",
|
|
18601
|
+
"0xd186b8c721c0c207",
|
|
18602
|
+
"0xeada7dd6cde0eb1e",
|
|
18603
|
+
"0xf57d4f7fee6ed178",
|
|
18604
|
+
"0x06f067aa72176fba",
|
|
18605
|
+
"0x0a637dc5a2c898a6",
|
|
18606
|
+
"0x113f9804bef90dae",
|
|
18607
|
+
"0x1b710b35131c471b",
|
|
18608
|
+
"0x28db77f523047d84",
|
|
18609
|
+
"0x32caab7b40c72493",
|
|
18610
|
+
"0x3c9ebe0a15c9bebc",
|
|
18611
|
+
"0x431d67c49c100d4c",
|
|
18612
|
+
"0x4cc5d4becb3e42b6",
|
|
18613
|
+
"0x597f299cfc657e2a",
|
|
18614
|
+
"0x5fcb6fab3ad6faec",
|
|
18615
|
+
"0x6c44198c4a475817"
|
|
18616
|
+
].map((n) => BigInt(n))))();
|
|
18617
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
18618
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
18619
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
18620
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
18621
|
+
var SHA512 = class extends HashMD {
|
|
18622
|
+
constructor(outputLen = 64) {
|
|
18623
|
+
super(128, outputLen, 16, false);
|
|
18624
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
18625
|
+
this.Al = SHA512_IV[1] | 0;
|
|
18626
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
18627
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
18628
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
18629
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
18630
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
18631
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
18632
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
18633
|
+
this.El = SHA512_IV[9] | 0;
|
|
18634
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
18635
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
18636
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
18637
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
18638
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
18639
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
18640
|
+
}
|
|
18641
|
+
// prettier-ignore
|
|
18642
|
+
get() {
|
|
18643
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
18644
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
18645
|
+
}
|
|
18646
|
+
// prettier-ignore
|
|
18647
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
18648
|
+
this.Ah = Ah | 0;
|
|
18649
|
+
this.Al = Al | 0;
|
|
18650
|
+
this.Bh = Bh | 0;
|
|
18651
|
+
this.Bl = Bl | 0;
|
|
18652
|
+
this.Ch = Ch | 0;
|
|
18653
|
+
this.Cl = Cl | 0;
|
|
18654
|
+
this.Dh = Dh | 0;
|
|
18655
|
+
this.Dl = Dl | 0;
|
|
18656
|
+
this.Eh = Eh | 0;
|
|
18657
|
+
this.El = El | 0;
|
|
18658
|
+
this.Fh = Fh | 0;
|
|
18659
|
+
this.Fl = Fl | 0;
|
|
18660
|
+
this.Gh = Gh | 0;
|
|
18661
|
+
this.Gl = Gl | 0;
|
|
18662
|
+
this.Hh = Hh | 0;
|
|
18663
|
+
this.Hl = Hl | 0;
|
|
18664
|
+
}
|
|
18665
|
+
process(view2, offset) {
|
|
18666
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
18667
|
+
SHA512_W_H[i] = view2.getUint32(offset);
|
|
18668
|
+
SHA512_W_L[i] = view2.getUint32(offset += 4);
|
|
18669
|
+
}
|
|
18670
|
+
for (let i = 16; i < 80; i++) {
|
|
18671
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
18672
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
18673
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
18674
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
18675
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
18676
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
18677
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
18678
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
18679
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
18680
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
18681
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
18682
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
18683
|
+
}
|
|
18684
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
18685
|
+
for (let i = 0; i < 80; i++) {
|
|
18686
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
18687
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
18688
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
18689
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
18690
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
18691
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
18692
|
+
const T1l = T1ll | 0;
|
|
18693
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
18694
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
18695
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
18696
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
18697
|
+
Hh = Gh | 0;
|
|
18698
|
+
Hl = Gl | 0;
|
|
18699
|
+
Gh = Fh | 0;
|
|
18700
|
+
Gl = Fl | 0;
|
|
18701
|
+
Fh = Eh | 0;
|
|
18702
|
+
Fl = El | 0;
|
|
18703
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
18704
|
+
Dh = Ch | 0;
|
|
18705
|
+
Dl = Cl | 0;
|
|
18706
|
+
Ch = Bh | 0;
|
|
18707
|
+
Cl = Bl | 0;
|
|
18708
|
+
Bh = Ah | 0;
|
|
18709
|
+
Bl = Al | 0;
|
|
18710
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
18711
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
18712
|
+
Al = All | 0;
|
|
18713
|
+
}
|
|
18714
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
18715
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
18716
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
18717
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
18718
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
18719
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
18720
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
18721
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
18722
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
18723
|
+
}
|
|
18724
|
+
roundClean() {
|
|
18725
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
18726
|
+
}
|
|
18727
|
+
destroy() {
|
|
18728
|
+
clean(this.buffer);
|
|
18729
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
18730
|
+
}
|
|
18731
|
+
};
|
|
18732
|
+
var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
18733
|
+
var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
|
|
18734
|
+
|
|
18735
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha256.js
|
|
18736
|
+
var sha2562 = sha256;
|
|
18737
|
+
|
|
18738
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2.js
|
|
18739
|
+
var BCPV2_HEADER_LENGTH = 68;
|
|
18740
|
+
var BCPV2_SIGNATURE_LENGTH = 64;
|
|
18741
|
+
var BCPV2_MIN_RECORD_LENGTH = BCPV2_HEADER_LENGTH + 32 + BCPV2_SIGNATURE_LENGTH;
|
|
18742
|
+
var BCPV2_MAX_RECORD_LENGTH = 1316;
|
|
18743
|
+
var BCPV2_MAX_DIGESTS = 37;
|
|
18744
|
+
var BCPV2_HASH_ALG_SHA256 = 1;
|
|
18745
|
+
var text = new TextEncoder();
|
|
18746
|
+
var MAGIC = Uint8Array.from([77, 72, 77, 50]);
|
|
18747
|
+
var SIGNING_DOMAIN = text.encode("mcast-manifest-v2\0");
|
|
18748
|
+
function equalBytes(left, right) {
|
|
18749
|
+
if (left.byteLength !== right.byteLength)
|
|
18750
|
+
return false;
|
|
18751
|
+
let different = 0;
|
|
18752
|
+
for (let index = 0; index < left.byteLength; index++)
|
|
18753
|
+
different |= left[index] ^ right[index];
|
|
18754
|
+
return different === 0;
|
|
18755
|
+
}
|
|
18756
|
+
function u16(value) {
|
|
18757
|
+
const result = new Uint8Array(2);
|
|
18758
|
+
new DataView(result.buffer).setUint16(0, value);
|
|
18759
|
+
return result;
|
|
18760
|
+
}
|
|
18761
|
+
function u322(value) {
|
|
18762
|
+
const result = new Uint8Array(4);
|
|
18763
|
+
new DataView(result.buffer).setUint32(0, value);
|
|
18764
|
+
return result;
|
|
18765
|
+
}
|
|
18766
|
+
function validU32(value) {
|
|
18767
|
+
return Number.isInteger(value) && value >= 0 && value <= 4294967295;
|
|
18768
|
+
}
|
|
18769
|
+
function validPort(value) {
|
|
18770
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535;
|
|
18771
|
+
}
|
|
18772
|
+
function parseIpv4(address) {
|
|
18773
|
+
const fields = address.split(".");
|
|
18774
|
+
if (fields.length !== 4 || fields.some((field) => !/^\d+$/.test(field))) {
|
|
18775
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
18776
|
+
}
|
|
18777
|
+
const values = fields.map(Number);
|
|
18778
|
+
if (values.some((value, index) => value > 255 || String(value) !== fields[index])) {
|
|
18779
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
18780
|
+
}
|
|
18781
|
+
return Uint8Array.from(values);
|
|
18782
|
+
}
|
|
18783
|
+
function parseIpv6(address) {
|
|
18784
|
+
const compressed = address.includes("::");
|
|
18785
|
+
if ((address.match(/::/g) ?? []).length > 1)
|
|
18786
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
18787
|
+
const [leftText, rightText = ""] = address.split("::");
|
|
18788
|
+
const left = leftText ? leftText.split(":") : [];
|
|
18789
|
+
const right = rightText ? rightText.split(":") : [];
|
|
18790
|
+
if (left.some((group) => group.includes(".")) || right.some((group) => group.includes("."))) {
|
|
18791
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
18792
|
+
}
|
|
18793
|
+
const missing = 8 - left.length - right.length;
|
|
18794
|
+
if (missing < 0 || !compressed && missing !== 0 || compressed && missing < 1) {
|
|
18795
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
18796
|
+
}
|
|
18797
|
+
const groups = [...left, ...Array.from({ length: missing }, () => "0"), ...right];
|
|
18798
|
+
if (groups.length !== 8 || groups.some((group) => !/^[0-9a-f]{1,4}$/i.test(group))) {
|
|
18799
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
18800
|
+
}
|
|
18801
|
+
const result = new Uint8Array(16);
|
|
18802
|
+
const view2 = new DataView(result.buffer);
|
|
18803
|
+
groups.forEach((group, index) => view2.setUint16(index * 2, Number.parseInt(group, 16)));
|
|
18804
|
+
return result;
|
|
18805
|
+
}
|
|
18806
|
+
function parseBcProvenanceV2Address(address) {
|
|
18807
|
+
if (address.includes(":"))
|
|
18808
|
+
return parseIpv6(address);
|
|
18809
|
+
return parseIpv4(address);
|
|
18810
|
+
}
|
|
18811
|
+
function endpointBytes(endpoint) {
|
|
18812
|
+
if (!validPort(endpoint.port) || !validPort(endpoint.manifestPort) || !validU32(endpoint.streamId)) {
|
|
18813
|
+
throw new Error("invalid BCPV2 endpoint");
|
|
18814
|
+
}
|
|
18815
|
+
const source = parseBcProvenanceV2Address(endpoint.sourceAddress);
|
|
18816
|
+
const group = parseBcProvenanceV2Address(endpoint.groupAddress);
|
|
18817
|
+
if (source.byteLength !== group.byteLength)
|
|
18818
|
+
throw new Error("endpoint address families differ");
|
|
18819
|
+
return { source, group };
|
|
18820
|
+
}
|
|
18821
|
+
function readU32(view2, offset) {
|
|
18822
|
+
return view2.getUint32(offset);
|
|
18823
|
+
}
|
|
18824
|
+
function readU642(view2, offset) {
|
|
18825
|
+
return view2.getBigUint64(offset);
|
|
18826
|
+
}
|
|
18827
|
+
function decodeBcProvenanceV2Record(input) {
|
|
18828
|
+
const bytes = Uint8Array.from(input);
|
|
18829
|
+
if (bytes.byteLength < BCPV2_MIN_RECORD_LENGTH || bytes.byteLength > BCPV2_MAX_RECORD_LENGTH || (bytes.byteLength - 132) % 32 !== 0) {
|
|
18830
|
+
throw new Error("invalid BCPV2 record length");
|
|
18831
|
+
}
|
|
18832
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
18833
|
+
if (!equalBytes(bytes.subarray(0, 4), MAGIC))
|
|
18834
|
+
throw new Error("invalid BCPV2 record magic");
|
|
18835
|
+
const count = view2.getUint16(28);
|
|
18836
|
+
if (count < 1 || count > BCPV2_MAX_DIGESTS || bytes.byteLength !== 132 + count * 32) {
|
|
18837
|
+
throw new Error("invalid BCPV2 record count");
|
|
18838
|
+
}
|
|
18839
|
+
const body = bytes.slice(0, bytes.byteLength - BCPV2_SIGNATURE_LENGTH);
|
|
18840
|
+
const digests = [];
|
|
18841
|
+
for (let index = 0; index < count; index++) {
|
|
18842
|
+
digests.push(bytes.slice(BCPV2_HEADER_LENGTH + index * 32, BCPV2_HEADER_LENGTH + (index + 1) * 32));
|
|
18843
|
+
}
|
|
18844
|
+
return {
|
|
18845
|
+
kid: readU32(view2, 4),
|
|
18846
|
+
epoch: readU32(view2, 8),
|
|
18847
|
+
mseq: readU642(view2, 12),
|
|
18848
|
+
firstSeq: readU642(view2, 20),
|
|
18849
|
+
count,
|
|
18850
|
+
hashAlg: bytes[30],
|
|
18851
|
+
flags: bytes[31],
|
|
18852
|
+
sent: readU32(view2, 32),
|
|
18853
|
+
ctx: bytes.slice(36, 68),
|
|
18854
|
+
digests,
|
|
18855
|
+
signature: bytes.slice(bytes.byteLength - BCPV2_SIGNATURE_LENGTH),
|
|
18856
|
+
body,
|
|
18857
|
+
raw: bytes
|
|
18858
|
+
};
|
|
18859
|
+
}
|
|
18860
|
+
function bcProvenanceV2SignedMessage(record, endpoint) {
|
|
18861
|
+
const { source, group } = endpointBytes(endpoint);
|
|
18862
|
+
const prefix = new Uint8Array(SIGNING_DOMAIN.byteLength + 4 + source.byteLength + group.byteLength + 2);
|
|
18863
|
+
let offset = 0;
|
|
18864
|
+
prefix.set(SIGNING_DOMAIN, offset);
|
|
18865
|
+
offset += SIGNING_DOMAIN.byteLength;
|
|
18866
|
+
prefix.set(u322(endpoint.streamId), offset);
|
|
18867
|
+
offset += 4;
|
|
18868
|
+
prefix.set(source, offset);
|
|
18869
|
+
offset += source.byteLength;
|
|
18870
|
+
prefix.set(group, offset);
|
|
18871
|
+
offset += group.byteLength;
|
|
18872
|
+
prefix.set(u16(endpoint.manifestPort), offset);
|
|
18873
|
+
const message = new Uint8Array(prefix.byteLength + record.body.byteLength);
|
|
18874
|
+
message.set(prefix);
|
|
18875
|
+
message.set(record.body, prefix.byteLength);
|
|
18876
|
+
return message;
|
|
18877
|
+
}
|
|
18878
|
+
function bcProvenanceV2DatagramDigest(endpoint, payload) {
|
|
18879
|
+
if (payload.byteLength > 65535)
|
|
18880
|
+
throw new Error("datagram payload exceeds u16 length");
|
|
18881
|
+
const { source, group } = endpointBytes(endpoint);
|
|
18882
|
+
const pseudoheader = new Uint8Array(source.byteLength + group.byteLength + 2 + 2 + 2 + 2 + 4);
|
|
18883
|
+
let offset = 0;
|
|
18884
|
+
pseudoheader.set(source, offset);
|
|
18885
|
+
offset += source.byteLength;
|
|
18886
|
+
pseudoheader.set(group, offset);
|
|
18887
|
+
offset += group.byteLength;
|
|
18888
|
+
pseudoheader.set([0, 17], offset);
|
|
18889
|
+
offset += 2;
|
|
18890
|
+
pseudoheader.set(u16(payload.byteLength), offset);
|
|
18891
|
+
offset += 2;
|
|
18892
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
18893
|
+
offset += 2;
|
|
18894
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
18895
|
+
offset += 2;
|
|
18896
|
+
pseudoheader.set(u322(endpoint.streamId), offset);
|
|
18897
|
+
const input = new Uint8Array(pseudoheader.byteLength + payload.byteLength);
|
|
18898
|
+
input.set(pseudoheader);
|
|
18899
|
+
input.set(payload, pseudoheader.byteLength);
|
|
18900
|
+
return sha2562(input);
|
|
18901
|
+
}
|
|
18902
|
+
|
|
18903
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/utils.js
|
|
18904
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
18905
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
18906
|
+
function _abool2(value, title = "") {
|
|
18907
|
+
if (typeof value !== "boolean") {
|
|
18908
|
+
const prefix = title && `"${title}"`;
|
|
18909
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
18910
|
+
}
|
|
18911
|
+
return value;
|
|
18912
|
+
}
|
|
18913
|
+
function _abytes2(value, length, title = "") {
|
|
18914
|
+
const bytes = isBytes(value);
|
|
18915
|
+
const len = value?.length;
|
|
18916
|
+
const needsLen = length !== void 0;
|
|
18917
|
+
if (!bytes || needsLen && len !== length) {
|
|
18918
|
+
const prefix = title && `"${title}" `;
|
|
18919
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
18920
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
18921
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
18922
|
+
}
|
|
18923
|
+
return value;
|
|
18924
|
+
}
|
|
18925
|
+
function hexToNumber(hex2) {
|
|
18926
|
+
if (typeof hex2 !== "string")
|
|
18927
|
+
throw new Error("hex string expected, got " + typeof hex2);
|
|
18928
|
+
return hex2 === "" ? _0n : BigInt("0x" + hex2);
|
|
18929
|
+
}
|
|
18930
|
+
function bytesToNumberBE(bytes) {
|
|
18931
|
+
return hexToNumber(bytesToHex(bytes));
|
|
18932
|
+
}
|
|
18933
|
+
function bytesToNumberLE(bytes) {
|
|
18934
|
+
abytes(bytes);
|
|
18935
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
18936
|
+
}
|
|
18937
|
+
function numberToBytesBE(n, len) {
|
|
18938
|
+
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
18939
|
+
}
|
|
18940
|
+
function numberToBytesLE(n, len) {
|
|
18941
|
+
return numberToBytesBE(n, len).reverse();
|
|
18942
|
+
}
|
|
18943
|
+
function ensureBytes(title, hex2, expectedLength) {
|
|
18944
|
+
let res;
|
|
18945
|
+
if (typeof hex2 === "string") {
|
|
18946
|
+
try {
|
|
18947
|
+
res = hexToBytes(hex2);
|
|
18948
|
+
} catch (e) {
|
|
18949
|
+
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
18950
|
+
}
|
|
18951
|
+
} else if (isBytes(hex2)) {
|
|
18952
|
+
res = Uint8Array.from(hex2);
|
|
18953
|
+
} else {
|
|
18954
|
+
throw new Error(title + " must be hex string or Uint8Array");
|
|
18955
|
+
}
|
|
18956
|
+
const len = res.length;
|
|
18957
|
+
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
18958
|
+
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
18959
|
+
return res;
|
|
18960
|
+
}
|
|
18961
|
+
function equalBytes2(a, b) {
|
|
18962
|
+
if (a.length !== b.length)
|
|
18963
|
+
return false;
|
|
18964
|
+
let diff = 0;
|
|
18965
|
+
for (let i = 0; i < a.length; i++)
|
|
18966
|
+
diff |= a[i] ^ b[i];
|
|
18967
|
+
return diff === 0;
|
|
18968
|
+
}
|
|
18969
|
+
function copyBytes(bytes) {
|
|
18970
|
+
return Uint8Array.from(bytes);
|
|
18971
|
+
}
|
|
18972
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
18973
|
+
function inRange(n, min, max) {
|
|
18974
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
18975
|
+
}
|
|
18976
|
+
function aInRange(title, n, min, max) {
|
|
18977
|
+
if (!inRange(n, min, max))
|
|
18978
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
18979
|
+
}
|
|
18980
|
+
function bitLen(n) {
|
|
18981
|
+
let len;
|
|
18982
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
18983
|
+
;
|
|
18984
|
+
return len;
|
|
18985
|
+
}
|
|
18986
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
18987
|
+
function _validateObject(object, fields, optFields = {}) {
|
|
18988
|
+
if (!object || typeof object !== "object")
|
|
18989
|
+
throw new Error("expected valid options object");
|
|
18990
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
18991
|
+
const val = object[fieldName];
|
|
18992
|
+
if (isOpt && val === void 0)
|
|
18993
|
+
return;
|
|
18994
|
+
const current = typeof val;
|
|
18995
|
+
if (current !== expectedType || val === null)
|
|
18996
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
18997
|
+
}
|
|
18998
|
+
Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));
|
|
18999
|
+
Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));
|
|
19000
|
+
}
|
|
19001
|
+
var notImplemented = () => {
|
|
19002
|
+
throw new Error("not implemented");
|
|
19003
|
+
};
|
|
19004
|
+
function memoized(fn) {
|
|
19005
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
19006
|
+
return (arg, ...args) => {
|
|
19007
|
+
const val = map.get(arg);
|
|
19008
|
+
if (val !== void 0)
|
|
19009
|
+
return val;
|
|
19010
|
+
const computed = fn(arg, ...args);
|
|
19011
|
+
map.set(arg, computed);
|
|
19012
|
+
return computed;
|
|
19013
|
+
};
|
|
19014
|
+
}
|
|
19015
|
+
|
|
19016
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/modular.js
|
|
19017
|
+
var _0n2 = BigInt(0);
|
|
19018
|
+
var _1n2 = BigInt(1);
|
|
19019
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
19020
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
19021
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
19022
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
19023
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
19024
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
19025
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
19026
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
19027
|
+
function mod(a, b) {
|
|
19028
|
+
const result = a % b;
|
|
19029
|
+
return result >= _0n2 ? result : b + result;
|
|
19030
|
+
}
|
|
19031
|
+
function pow2(x, power, modulo) {
|
|
19032
|
+
let res = x;
|
|
19033
|
+
while (power-- > _0n2) {
|
|
19034
|
+
res *= res;
|
|
19035
|
+
res %= modulo;
|
|
19036
|
+
}
|
|
19037
|
+
return res;
|
|
19038
|
+
}
|
|
19039
|
+
function invert(number, modulo) {
|
|
19040
|
+
if (number === _0n2)
|
|
19041
|
+
throw new Error("invert: expected non-zero number");
|
|
19042
|
+
if (modulo <= _0n2)
|
|
19043
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
19044
|
+
let a = mod(number, modulo);
|
|
19045
|
+
let b = modulo;
|
|
19046
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
19047
|
+
while (a !== _0n2) {
|
|
19048
|
+
const q = b / a;
|
|
19049
|
+
const r = b % a;
|
|
19050
|
+
const m = x - u * q;
|
|
19051
|
+
const n = y - v * q;
|
|
19052
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
19053
|
+
}
|
|
19054
|
+
const gcd = b;
|
|
19055
|
+
if (gcd !== _1n2)
|
|
19056
|
+
throw new Error("invert: does not exist");
|
|
19057
|
+
return mod(x, modulo);
|
|
19058
|
+
}
|
|
19059
|
+
function assertIsSquare(Fp2, root, n) {
|
|
19060
|
+
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
19061
|
+
throw new Error("Cannot find square root");
|
|
19062
|
+
}
|
|
19063
|
+
function sqrt3mod4(Fp2, n) {
|
|
19064
|
+
const p1div4 = (Fp2.ORDER + _1n2) / _4n;
|
|
19065
|
+
const root = Fp2.pow(n, p1div4);
|
|
19066
|
+
assertIsSquare(Fp2, root, n);
|
|
19067
|
+
return root;
|
|
19068
|
+
}
|
|
19069
|
+
function sqrt5mod8(Fp2, n) {
|
|
19070
|
+
const p5div8 = (Fp2.ORDER - _5n) / _8n;
|
|
19071
|
+
const n2 = Fp2.mul(n, _2n);
|
|
19072
|
+
const v = Fp2.pow(n2, p5div8);
|
|
19073
|
+
const nv = Fp2.mul(n, v);
|
|
19074
|
+
const i = Fp2.mul(Fp2.mul(nv, _2n), v);
|
|
19075
|
+
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
19076
|
+
assertIsSquare(Fp2, root, n);
|
|
19077
|
+
return root;
|
|
19078
|
+
}
|
|
19079
|
+
function sqrt9mod16(P) {
|
|
19080
|
+
const Fp_ = Field(P);
|
|
19081
|
+
const tn = tonelliShanks(P);
|
|
19082
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
19083
|
+
const c2 = tn(Fp_, c1);
|
|
19084
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
19085
|
+
const c4 = (P + _7n) / _16n;
|
|
19086
|
+
return (Fp2, n) => {
|
|
19087
|
+
let tv1 = Fp2.pow(n, c4);
|
|
19088
|
+
let tv2 = Fp2.mul(tv1, c1);
|
|
19089
|
+
const tv3 = Fp2.mul(tv1, c2);
|
|
19090
|
+
const tv4 = Fp2.mul(tv1, c3);
|
|
19091
|
+
const e1 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
19092
|
+
const e2 = Fp2.eql(Fp2.sqr(tv3), n);
|
|
19093
|
+
tv1 = Fp2.cmov(tv1, tv2, e1);
|
|
19094
|
+
tv2 = Fp2.cmov(tv4, tv3, e2);
|
|
19095
|
+
const e3 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
19096
|
+
const root = Fp2.cmov(tv1, tv2, e3);
|
|
19097
|
+
assertIsSquare(Fp2, root, n);
|
|
19098
|
+
return root;
|
|
19099
|
+
};
|
|
19100
|
+
}
|
|
19101
|
+
function tonelliShanks(P) {
|
|
19102
|
+
if (P < _3n)
|
|
19103
|
+
throw new Error("sqrt is not defined for small field");
|
|
19104
|
+
let Q = P - _1n2;
|
|
19105
|
+
let S = 0;
|
|
19106
|
+
while (Q % _2n === _0n2) {
|
|
19107
|
+
Q /= _2n;
|
|
19108
|
+
S++;
|
|
19109
|
+
}
|
|
19110
|
+
let Z = _2n;
|
|
19111
|
+
const _Fp = Field(P);
|
|
19112
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
19113
|
+
if (Z++ > 1e3)
|
|
19114
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
19115
|
+
}
|
|
19116
|
+
if (S === 1)
|
|
19117
|
+
return sqrt3mod4;
|
|
19118
|
+
let cc = _Fp.pow(Z, Q);
|
|
19119
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
19120
|
+
return function tonelliSlow(Fp2, n) {
|
|
19121
|
+
if (Fp2.is0(n))
|
|
19122
|
+
return n;
|
|
19123
|
+
if (FpLegendre(Fp2, n) !== 1)
|
|
19124
|
+
throw new Error("Cannot find square root");
|
|
19125
|
+
let M = S;
|
|
19126
|
+
let c = Fp2.mul(Fp2.ONE, cc);
|
|
19127
|
+
let t = Fp2.pow(n, Q);
|
|
19128
|
+
let R = Fp2.pow(n, Q1div2);
|
|
19129
|
+
while (!Fp2.eql(t, Fp2.ONE)) {
|
|
19130
|
+
if (Fp2.is0(t))
|
|
19131
|
+
return Fp2.ZERO;
|
|
19132
|
+
let i = 1;
|
|
19133
|
+
let t_tmp = Fp2.sqr(t);
|
|
19134
|
+
while (!Fp2.eql(t_tmp, Fp2.ONE)) {
|
|
19135
|
+
i++;
|
|
19136
|
+
t_tmp = Fp2.sqr(t_tmp);
|
|
19137
|
+
if (i === M)
|
|
19138
|
+
throw new Error("Cannot find square root");
|
|
19139
|
+
}
|
|
19140
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
19141
|
+
const b = Fp2.pow(c, exponent);
|
|
19142
|
+
M = i;
|
|
19143
|
+
c = Fp2.sqr(b);
|
|
19144
|
+
t = Fp2.mul(t, c);
|
|
19145
|
+
R = Fp2.mul(R, b);
|
|
19146
|
+
}
|
|
19147
|
+
return R;
|
|
19148
|
+
};
|
|
19149
|
+
}
|
|
19150
|
+
function FpSqrt(P) {
|
|
19151
|
+
if (P % _4n === _3n)
|
|
19152
|
+
return sqrt3mod4;
|
|
19153
|
+
if (P % _8n === _5n)
|
|
19154
|
+
return sqrt5mod8;
|
|
19155
|
+
if (P % _16n === _9n)
|
|
19156
|
+
return sqrt9mod16(P);
|
|
19157
|
+
return tonelliShanks(P);
|
|
19158
|
+
}
|
|
19159
|
+
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
19160
|
+
var FIELD_FIELDS = [
|
|
19161
|
+
"create",
|
|
19162
|
+
"isValid",
|
|
19163
|
+
"is0",
|
|
19164
|
+
"neg",
|
|
19165
|
+
"inv",
|
|
19166
|
+
"sqrt",
|
|
19167
|
+
"sqr",
|
|
19168
|
+
"eql",
|
|
19169
|
+
"add",
|
|
19170
|
+
"sub",
|
|
19171
|
+
"mul",
|
|
19172
|
+
"pow",
|
|
19173
|
+
"div",
|
|
19174
|
+
"addN",
|
|
19175
|
+
"subN",
|
|
19176
|
+
"mulN",
|
|
19177
|
+
"sqrN"
|
|
19178
|
+
];
|
|
19179
|
+
function validateField(field) {
|
|
19180
|
+
const initial = {
|
|
19181
|
+
ORDER: "bigint",
|
|
19182
|
+
MASK: "bigint",
|
|
19183
|
+
BYTES: "number",
|
|
19184
|
+
BITS: "number"
|
|
19185
|
+
};
|
|
19186
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
19187
|
+
map[val] = "function";
|
|
19188
|
+
return map;
|
|
19189
|
+
}, initial);
|
|
19190
|
+
_validateObject(field, opts);
|
|
19191
|
+
return field;
|
|
19192
|
+
}
|
|
19193
|
+
function FpPow(Fp2, num, power) {
|
|
19194
|
+
if (power < _0n2)
|
|
19195
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
19196
|
+
if (power === _0n2)
|
|
19197
|
+
return Fp2.ONE;
|
|
19198
|
+
if (power === _1n2)
|
|
19199
|
+
return num;
|
|
19200
|
+
let p = Fp2.ONE;
|
|
19201
|
+
let d = num;
|
|
19202
|
+
while (power > _0n2) {
|
|
19203
|
+
if (power & _1n2)
|
|
19204
|
+
p = Fp2.mul(p, d);
|
|
19205
|
+
d = Fp2.sqr(d);
|
|
19206
|
+
power >>= _1n2;
|
|
19207
|
+
}
|
|
19208
|
+
return p;
|
|
19209
|
+
}
|
|
19210
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
19211
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
|
|
19212
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
19213
|
+
if (Fp2.is0(num))
|
|
19214
|
+
return acc;
|
|
19215
|
+
inverted[i] = acc;
|
|
19216
|
+
return Fp2.mul(acc, num);
|
|
19217
|
+
}, Fp2.ONE);
|
|
19218
|
+
const invertedAcc = Fp2.inv(multipliedAcc);
|
|
19219
|
+
nums.reduceRight((acc, num, i) => {
|
|
19220
|
+
if (Fp2.is0(num))
|
|
19221
|
+
return acc;
|
|
19222
|
+
inverted[i] = Fp2.mul(acc, inverted[i]);
|
|
19223
|
+
return Fp2.mul(acc, num);
|
|
19224
|
+
}, invertedAcc);
|
|
19225
|
+
return inverted;
|
|
19226
|
+
}
|
|
19227
|
+
function FpLegendre(Fp2, n) {
|
|
19228
|
+
const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
|
|
19229
|
+
const powered = Fp2.pow(n, p1mod2);
|
|
19230
|
+
const yes = Fp2.eql(powered, Fp2.ONE);
|
|
19231
|
+
const zero = Fp2.eql(powered, Fp2.ZERO);
|
|
19232
|
+
const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
|
|
19233
|
+
if (!yes && !zero && !no)
|
|
19234
|
+
throw new Error("invalid Legendre symbol result");
|
|
19235
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
19236
|
+
}
|
|
19237
|
+
function nLength(n, nBitLength) {
|
|
19238
|
+
if (nBitLength !== void 0)
|
|
19239
|
+
anumber(nBitLength);
|
|
19240
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
19241
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
19242
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
19243
|
+
}
|
|
19244
|
+
function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
|
|
19245
|
+
if (ORDER <= _0n2)
|
|
19246
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
19247
|
+
let _nbitLength = void 0;
|
|
19248
|
+
let _sqrt = void 0;
|
|
19249
|
+
let modFromBytes = false;
|
|
19250
|
+
let allowedLengths = void 0;
|
|
19251
|
+
if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
|
|
19252
|
+
if (opts.sqrt || isLE)
|
|
19253
|
+
throw new Error("cannot specify opts in two arguments");
|
|
19254
|
+
const _opts = bitLenOrOpts;
|
|
19255
|
+
if (_opts.BITS)
|
|
19256
|
+
_nbitLength = _opts.BITS;
|
|
19257
|
+
if (_opts.sqrt)
|
|
19258
|
+
_sqrt = _opts.sqrt;
|
|
19259
|
+
if (typeof _opts.isLE === "boolean")
|
|
19260
|
+
isLE = _opts.isLE;
|
|
19261
|
+
if (typeof _opts.modFromBytes === "boolean")
|
|
19262
|
+
modFromBytes = _opts.modFromBytes;
|
|
19263
|
+
allowedLengths = _opts.allowedLengths;
|
|
19264
|
+
} else {
|
|
19265
|
+
if (typeof bitLenOrOpts === "number")
|
|
19266
|
+
_nbitLength = bitLenOrOpts;
|
|
19267
|
+
if (opts.sqrt)
|
|
19268
|
+
_sqrt = opts.sqrt;
|
|
19269
|
+
}
|
|
19270
|
+
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);
|
|
19271
|
+
if (BYTES > 2048)
|
|
19272
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
19273
|
+
let sqrtP;
|
|
19274
|
+
const f = Object.freeze({
|
|
19275
|
+
ORDER,
|
|
19276
|
+
isLE,
|
|
19277
|
+
BITS,
|
|
19278
|
+
BYTES,
|
|
19279
|
+
MASK: bitMask(BITS),
|
|
19280
|
+
ZERO: _0n2,
|
|
19281
|
+
ONE: _1n2,
|
|
19282
|
+
allowedLengths,
|
|
19283
|
+
create: (num) => mod(num, ORDER),
|
|
19284
|
+
isValid: (num) => {
|
|
19285
|
+
if (typeof num !== "bigint")
|
|
19286
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
19287
|
+
return _0n2 <= num && num < ORDER;
|
|
19288
|
+
},
|
|
19289
|
+
is0: (num) => num === _0n2,
|
|
19290
|
+
// is valid and invertible
|
|
19291
|
+
isValidNot0: (num) => !f.is0(num) && f.isValid(num),
|
|
19292
|
+
isOdd: (num) => (num & _1n2) === _1n2,
|
|
19293
|
+
neg: (num) => mod(-num, ORDER),
|
|
19294
|
+
eql: (lhs, rhs) => lhs === rhs,
|
|
19295
|
+
sqr: (num) => mod(num * num, ORDER),
|
|
19296
|
+
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
19297
|
+
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
19298
|
+
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
19299
|
+
pow: (num, power) => FpPow(f, num, power),
|
|
19300
|
+
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
19301
|
+
// Same as above, but doesn't normalize
|
|
19302
|
+
sqrN: (num) => num * num,
|
|
19303
|
+
addN: (lhs, rhs) => lhs + rhs,
|
|
19304
|
+
subN: (lhs, rhs) => lhs - rhs,
|
|
19305
|
+
mulN: (lhs, rhs) => lhs * rhs,
|
|
19306
|
+
inv: (num) => invert(num, ORDER),
|
|
19307
|
+
sqrt: _sqrt || ((n) => {
|
|
19308
|
+
if (!sqrtP)
|
|
19309
|
+
sqrtP = FpSqrt(ORDER);
|
|
19310
|
+
return sqrtP(f, n);
|
|
19311
|
+
}),
|
|
19312
|
+
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
19313
|
+
fromBytes: (bytes, skipValidation = true) => {
|
|
19314
|
+
if (allowedLengths) {
|
|
19315
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
19316
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
19317
|
+
}
|
|
19318
|
+
const padded = new Uint8Array(BYTES);
|
|
19319
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
19320
|
+
bytes = padded;
|
|
19321
|
+
}
|
|
19322
|
+
if (bytes.length !== BYTES)
|
|
19323
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
19324
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
19325
|
+
if (modFromBytes)
|
|
19326
|
+
scalar = mod(scalar, ORDER);
|
|
19327
|
+
if (!skipValidation) {
|
|
19328
|
+
if (!f.isValid(scalar))
|
|
19329
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
19330
|
+
}
|
|
19331
|
+
return scalar;
|
|
19332
|
+
},
|
|
19333
|
+
// TODO: we don't need it here, move out to separate fn
|
|
19334
|
+
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
19335
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
19336
|
+
// and it's unclear what to return in there.
|
|
19337
|
+
cmov: (a, b, c) => c ? b : a
|
|
19338
|
+
});
|
|
19339
|
+
return Object.freeze(f);
|
|
19340
|
+
}
|
|
19341
|
+
|
|
19342
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/curve.js
|
|
19343
|
+
var _0n3 = BigInt(0);
|
|
19344
|
+
var _1n3 = BigInt(1);
|
|
19345
|
+
function negateCt(condition, item) {
|
|
19346
|
+
const neg = item.negate();
|
|
19347
|
+
return condition ? neg : item;
|
|
19348
|
+
}
|
|
19349
|
+
function normalizeZ(c, points) {
|
|
19350
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
19351
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
19352
|
+
}
|
|
19353
|
+
function validateW(W, bits) {
|
|
19354
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
19355
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
19356
|
+
}
|
|
19357
|
+
function calcWOpts(W, scalarBits) {
|
|
19358
|
+
validateW(W, scalarBits);
|
|
19359
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
19360
|
+
const windowSize = 2 ** (W - 1);
|
|
19361
|
+
const maxNumber = 2 ** W;
|
|
19362
|
+
const mask = bitMask(W);
|
|
19363
|
+
const shiftBy = BigInt(W);
|
|
19364
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
19365
|
+
}
|
|
19366
|
+
function calcOffsets(n, window2, wOpts) {
|
|
19367
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
19368
|
+
let wbits = Number(n & mask);
|
|
19369
|
+
let nextN = n >> shiftBy;
|
|
19370
|
+
if (wbits > windowSize) {
|
|
19371
|
+
wbits -= maxNumber;
|
|
19372
|
+
nextN += _1n3;
|
|
19373
|
+
}
|
|
19374
|
+
const offsetStart = window2 * windowSize;
|
|
19375
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
19376
|
+
const isZero = wbits === 0;
|
|
19377
|
+
const isNeg = wbits < 0;
|
|
19378
|
+
const isNegF = window2 % 2 !== 0;
|
|
19379
|
+
const offsetF = offsetStart;
|
|
19380
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
19381
|
+
}
|
|
19382
|
+
function validateMSMPoints(points, c) {
|
|
19383
|
+
if (!Array.isArray(points))
|
|
19384
|
+
throw new Error("array expected");
|
|
19385
|
+
points.forEach((p, i) => {
|
|
19386
|
+
if (!(p instanceof c))
|
|
19387
|
+
throw new Error("invalid point at index " + i);
|
|
19388
|
+
});
|
|
19389
|
+
}
|
|
19390
|
+
function validateMSMScalars(scalars, field) {
|
|
19391
|
+
if (!Array.isArray(scalars))
|
|
19392
|
+
throw new Error("array of scalars expected");
|
|
19393
|
+
scalars.forEach((s, i) => {
|
|
19394
|
+
if (!field.isValid(s))
|
|
19395
|
+
throw new Error("invalid scalar at index " + i);
|
|
19396
|
+
});
|
|
19397
|
+
}
|
|
19398
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
19399
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
19400
|
+
function getW(P) {
|
|
19401
|
+
return pointWindowSizes.get(P) || 1;
|
|
19402
|
+
}
|
|
19403
|
+
function assert0(n) {
|
|
19404
|
+
if (n !== _0n3)
|
|
19405
|
+
throw new Error("invalid wNAF");
|
|
19406
|
+
}
|
|
19407
|
+
var wNAF = class {
|
|
19408
|
+
// Parametrized with a given Point class (not individual point)
|
|
19409
|
+
constructor(Point, bits) {
|
|
19410
|
+
this.BASE = Point.BASE;
|
|
19411
|
+
this.ZERO = Point.ZERO;
|
|
19412
|
+
this.Fn = Point.Fn;
|
|
19413
|
+
this.bits = bits;
|
|
19414
|
+
}
|
|
19415
|
+
// non-const time multiplication ladder
|
|
19416
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
19417
|
+
let d = elm;
|
|
19418
|
+
while (n > _0n3) {
|
|
19419
|
+
if (n & _1n3)
|
|
19420
|
+
p = p.add(d);
|
|
19421
|
+
d = d.double();
|
|
19422
|
+
n >>= _1n3;
|
|
19423
|
+
}
|
|
19424
|
+
return p;
|
|
19425
|
+
}
|
|
19426
|
+
/**
|
|
19427
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
19428
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
19429
|
+
* Number of precomputed points depends on the curve size:
|
|
19430
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
19431
|
+
* - 𝑊 is the window size
|
|
19432
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
19433
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
19434
|
+
* @param point Point instance
|
|
19435
|
+
* @param W window size
|
|
19436
|
+
* @returns precomputed point tables flattened to a single array
|
|
19437
|
+
*/
|
|
19438
|
+
precomputeWindow(point, W) {
|
|
19439
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
19440
|
+
const points = [];
|
|
19441
|
+
let p = point;
|
|
19442
|
+
let base = p;
|
|
19443
|
+
for (let window2 = 0; window2 < windows; window2++) {
|
|
19444
|
+
base = p;
|
|
19445
|
+
points.push(base);
|
|
19446
|
+
for (let i = 1; i < windowSize; i++) {
|
|
19447
|
+
base = base.add(p);
|
|
19448
|
+
points.push(base);
|
|
19449
|
+
}
|
|
19450
|
+
p = base.double();
|
|
19451
|
+
}
|
|
19452
|
+
return points;
|
|
19453
|
+
}
|
|
19454
|
+
/**
|
|
19455
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
19456
|
+
* More compact implementation:
|
|
19457
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
19458
|
+
* @returns real and fake (for const-time) points
|
|
19459
|
+
*/
|
|
19460
|
+
wNAF(W, precomputes, n) {
|
|
19461
|
+
if (!this.Fn.isValid(n))
|
|
19462
|
+
throw new Error("invalid scalar");
|
|
19463
|
+
let p = this.ZERO;
|
|
19464
|
+
let f = this.BASE;
|
|
19465
|
+
const wo = calcWOpts(W, this.bits);
|
|
19466
|
+
for (let window2 = 0; window2 < wo.windows; window2++) {
|
|
19467
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window2, wo);
|
|
19468
|
+
n = nextN;
|
|
19469
|
+
if (isZero) {
|
|
19470
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
19471
|
+
} else {
|
|
19472
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
19473
|
+
}
|
|
19474
|
+
}
|
|
19475
|
+
assert0(n);
|
|
19476
|
+
return { p, f };
|
|
19477
|
+
}
|
|
19478
|
+
/**
|
|
19479
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
19480
|
+
* @param acc accumulator point to add result of multiplication
|
|
19481
|
+
* @returns point
|
|
19482
|
+
*/
|
|
19483
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
19484
|
+
const wo = calcWOpts(W, this.bits);
|
|
19485
|
+
for (let window2 = 0; window2 < wo.windows; window2++) {
|
|
19486
|
+
if (n === _0n3)
|
|
19487
|
+
break;
|
|
19488
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window2, wo);
|
|
19489
|
+
n = nextN;
|
|
19490
|
+
if (isZero) {
|
|
19491
|
+
continue;
|
|
19492
|
+
} else {
|
|
19493
|
+
const item = precomputes[offset];
|
|
19494
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
19495
|
+
}
|
|
19496
|
+
}
|
|
19497
|
+
assert0(n);
|
|
19498
|
+
return acc;
|
|
19499
|
+
}
|
|
19500
|
+
getPrecomputes(W, point, transform) {
|
|
19501
|
+
let comp = pointPrecomputes.get(point);
|
|
19502
|
+
if (!comp) {
|
|
19503
|
+
comp = this.precomputeWindow(point, W);
|
|
19504
|
+
if (W !== 1) {
|
|
19505
|
+
if (typeof transform === "function")
|
|
19506
|
+
comp = transform(comp);
|
|
19507
|
+
pointPrecomputes.set(point, comp);
|
|
19508
|
+
}
|
|
19509
|
+
}
|
|
19510
|
+
return comp;
|
|
19511
|
+
}
|
|
19512
|
+
cached(point, scalar, transform) {
|
|
19513
|
+
const W = getW(point);
|
|
19514
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
19515
|
+
}
|
|
19516
|
+
unsafe(point, scalar, transform, prev) {
|
|
19517
|
+
const W = getW(point);
|
|
19518
|
+
if (W === 1)
|
|
19519
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
19520
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
19521
|
+
}
|
|
19522
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
19523
|
+
// using windowed method. This specifies window size and
|
|
19524
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
19525
|
+
createCache(P, W) {
|
|
19526
|
+
validateW(W, this.bits);
|
|
19527
|
+
pointWindowSizes.set(P, W);
|
|
19528
|
+
pointPrecomputes.delete(P);
|
|
19529
|
+
}
|
|
19530
|
+
hasCache(elm) {
|
|
19531
|
+
return getW(elm) !== 1;
|
|
19532
|
+
}
|
|
19533
|
+
};
|
|
19534
|
+
function pippenger(c, fieldN, points, scalars) {
|
|
19535
|
+
validateMSMPoints(points, c);
|
|
19536
|
+
validateMSMScalars(scalars, fieldN);
|
|
19537
|
+
const plength = points.length;
|
|
19538
|
+
const slength = scalars.length;
|
|
19539
|
+
if (plength !== slength)
|
|
19540
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
19541
|
+
const zero = c.ZERO;
|
|
19542
|
+
const wbits = bitLen(BigInt(plength));
|
|
19543
|
+
let windowSize = 1;
|
|
19544
|
+
if (wbits > 12)
|
|
19545
|
+
windowSize = wbits - 3;
|
|
19546
|
+
else if (wbits > 4)
|
|
19547
|
+
windowSize = wbits - 2;
|
|
19548
|
+
else if (wbits > 0)
|
|
19549
|
+
windowSize = 2;
|
|
19550
|
+
const MASK = bitMask(windowSize);
|
|
19551
|
+
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
19552
|
+
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
19553
|
+
let sum = zero;
|
|
19554
|
+
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
19555
|
+
buckets.fill(zero);
|
|
19556
|
+
for (let j = 0; j < slength; j++) {
|
|
19557
|
+
const scalar = scalars[j];
|
|
19558
|
+
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
|
19559
|
+
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
19560
|
+
}
|
|
19561
|
+
let resI = zero;
|
|
19562
|
+
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
19563
|
+
sumI = sumI.add(buckets[j]);
|
|
19564
|
+
resI = resI.add(sumI);
|
|
19565
|
+
}
|
|
19566
|
+
sum = sum.add(resI);
|
|
19567
|
+
if (i !== 0)
|
|
19568
|
+
for (let j = 0; j < windowSize; j++)
|
|
19569
|
+
sum = sum.double();
|
|
19570
|
+
}
|
|
19571
|
+
return sum;
|
|
19572
|
+
}
|
|
19573
|
+
function createField(order, field, isLE) {
|
|
19574
|
+
if (field) {
|
|
19575
|
+
if (field.ORDER !== order)
|
|
19576
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
19577
|
+
validateField(field);
|
|
19578
|
+
return field;
|
|
19579
|
+
} else {
|
|
19580
|
+
return Field(order, { isLE });
|
|
19581
|
+
}
|
|
19582
|
+
}
|
|
19583
|
+
function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
19584
|
+
if (FpFnLE === void 0)
|
|
19585
|
+
FpFnLE = type === "edwards";
|
|
19586
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
19587
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
19588
|
+
for (const p of ["p", "n", "h"]) {
|
|
19589
|
+
const val = CURVE[p];
|
|
19590
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
19591
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
19592
|
+
}
|
|
19593
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
19594
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
19595
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
19596
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
19597
|
+
for (const p of params) {
|
|
19598
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
19599
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
19600
|
+
}
|
|
19601
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
19602
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
19603
|
+
}
|
|
19604
|
+
|
|
19605
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/edwards.js
|
|
19606
|
+
var _0n4 = BigInt(0);
|
|
19607
|
+
var _1n4 = BigInt(1);
|
|
19608
|
+
var _2n2 = BigInt(2);
|
|
19609
|
+
var _8n2 = BigInt(8);
|
|
19610
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
19611
|
+
const x2 = Fp2.sqr(x);
|
|
19612
|
+
const y2 = Fp2.sqr(y);
|
|
19613
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
19614
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
19615
|
+
return Fp2.eql(left, right);
|
|
19616
|
+
}
|
|
19617
|
+
function edwards(params, extraOpts = {}) {
|
|
19618
|
+
const validated = _createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
19619
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
19620
|
+
let CURVE = validated.CURVE;
|
|
19621
|
+
const { h: cofactor } = CURVE;
|
|
19622
|
+
_validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
19623
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
19624
|
+
const modP = (n) => Fp2.create(n);
|
|
19625
|
+
const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
|
|
19626
|
+
try {
|
|
19627
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
19628
|
+
} catch (e) {
|
|
19629
|
+
return { isValid: false, value: _0n4 };
|
|
19630
|
+
}
|
|
19631
|
+
});
|
|
19632
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
19633
|
+
throw new Error("bad curve params: generator point");
|
|
19634
|
+
function acoord(title, n, banZero = false) {
|
|
19635
|
+
const min = banZero ? _1n4 : _0n4;
|
|
19636
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
19637
|
+
return n;
|
|
19638
|
+
}
|
|
19639
|
+
function aextpoint(other) {
|
|
19640
|
+
if (!(other instanceof Point))
|
|
19641
|
+
throw new Error("ExtendedPoint expected");
|
|
19642
|
+
}
|
|
19643
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
19644
|
+
const { X, Y, Z } = p;
|
|
19645
|
+
const is0 = p.is0();
|
|
19646
|
+
if (iz == null)
|
|
19647
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
19648
|
+
const x = modP(X * iz);
|
|
19649
|
+
const y = modP(Y * iz);
|
|
19650
|
+
const zz = Fp2.mul(Z, iz);
|
|
19651
|
+
if (is0)
|
|
19652
|
+
return { x: _0n4, y: _1n4 };
|
|
19653
|
+
if (zz !== _1n4)
|
|
19654
|
+
throw new Error("invZ was invalid");
|
|
19655
|
+
return { x, y };
|
|
19656
|
+
});
|
|
19657
|
+
const assertValidMemo = memoized((p) => {
|
|
19658
|
+
const { a, d } = CURVE;
|
|
19659
|
+
if (p.is0())
|
|
19660
|
+
throw new Error("bad point: ZERO");
|
|
19661
|
+
const { X, Y, Z, T } = p;
|
|
19662
|
+
const X2 = modP(X * X);
|
|
19663
|
+
const Y2 = modP(Y * Y);
|
|
19664
|
+
const Z2 = modP(Z * Z);
|
|
19665
|
+
const Z4 = modP(Z2 * Z2);
|
|
19666
|
+
const aX2 = modP(X2 * a);
|
|
19667
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
19668
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
19669
|
+
if (left !== right)
|
|
19670
|
+
throw new Error("bad point: equation left != right (1)");
|
|
19671
|
+
const XY = modP(X * Y);
|
|
19672
|
+
const ZT = modP(Z * T);
|
|
19673
|
+
if (XY !== ZT)
|
|
19674
|
+
throw new Error("bad point: equation left != right (2)");
|
|
19675
|
+
return true;
|
|
19676
|
+
});
|
|
19677
|
+
class Point {
|
|
19678
|
+
constructor(X, Y, Z, T) {
|
|
19679
|
+
this.X = acoord("x", X);
|
|
19680
|
+
this.Y = acoord("y", Y);
|
|
19681
|
+
this.Z = acoord("z", Z, true);
|
|
19682
|
+
this.T = acoord("t", T);
|
|
19683
|
+
Object.freeze(this);
|
|
19684
|
+
}
|
|
19685
|
+
static CURVE() {
|
|
19686
|
+
return CURVE;
|
|
19687
|
+
}
|
|
19688
|
+
static fromAffine(p) {
|
|
19689
|
+
if (p instanceof Point)
|
|
19690
|
+
throw new Error("extended point not allowed");
|
|
19691
|
+
const { x, y } = p || {};
|
|
19692
|
+
acoord("x", x);
|
|
19693
|
+
acoord("y", y);
|
|
19694
|
+
return new Point(x, y, _1n4, modP(x * y));
|
|
19695
|
+
}
|
|
19696
|
+
// Uses algo from RFC8032 5.1.3.
|
|
19697
|
+
static fromBytes(bytes, zip215 = false) {
|
|
19698
|
+
const len = Fp2.BYTES;
|
|
19699
|
+
const { a, d } = CURVE;
|
|
19700
|
+
bytes = copyBytes(_abytes2(bytes, len, "point"));
|
|
19701
|
+
_abool2(zip215, "zip215");
|
|
19702
|
+
const normed = copyBytes(bytes);
|
|
19703
|
+
const lastByte = bytes[len - 1];
|
|
19704
|
+
normed[len - 1] = lastByte & ~128;
|
|
19705
|
+
const y = bytesToNumberLE(normed);
|
|
19706
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
19707
|
+
aInRange("point.y", y, _0n4, max);
|
|
19708
|
+
const y2 = modP(y * y);
|
|
19709
|
+
const u = modP(y2 - _1n4);
|
|
19710
|
+
const v = modP(d * y2 - a);
|
|
19711
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
19712
|
+
if (!isValid)
|
|
19713
|
+
throw new Error("bad point: invalid y coordinate");
|
|
19714
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
19715
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
19716
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
19717
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
19718
|
+
if (isLastByteOdd !== isXOdd)
|
|
19719
|
+
x = modP(-x);
|
|
19720
|
+
return Point.fromAffine({ x, y });
|
|
19721
|
+
}
|
|
19722
|
+
static fromHex(bytes, zip215 = false) {
|
|
19723
|
+
return Point.fromBytes(ensureBytes("point", bytes), zip215);
|
|
19724
|
+
}
|
|
19725
|
+
get x() {
|
|
19726
|
+
return this.toAffine().x;
|
|
19727
|
+
}
|
|
19728
|
+
get y() {
|
|
19729
|
+
return this.toAffine().y;
|
|
19730
|
+
}
|
|
19731
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
19732
|
+
wnaf.createCache(this, windowSize);
|
|
19733
|
+
if (!isLazy)
|
|
19734
|
+
this.multiply(_2n2);
|
|
19735
|
+
return this;
|
|
19736
|
+
}
|
|
19737
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
19738
|
+
assertValidity() {
|
|
19739
|
+
assertValidMemo(this);
|
|
19740
|
+
}
|
|
19741
|
+
// Compare one point to another.
|
|
19742
|
+
equals(other) {
|
|
19743
|
+
aextpoint(other);
|
|
19744
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
19745
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
19746
|
+
const X1Z2 = modP(X1 * Z2);
|
|
19747
|
+
const X2Z1 = modP(X2 * Z1);
|
|
19748
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
19749
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
19750
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
19751
|
+
}
|
|
19752
|
+
is0() {
|
|
19753
|
+
return this.equals(Point.ZERO);
|
|
19754
|
+
}
|
|
19755
|
+
negate() {
|
|
19756
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
19757
|
+
}
|
|
19758
|
+
// Fast algo for doubling Extended Point.
|
|
19759
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
19760
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
19761
|
+
double() {
|
|
19762
|
+
const { a } = CURVE;
|
|
19763
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
19764
|
+
const A = modP(X1 * X1);
|
|
19765
|
+
const B = modP(Y1 * Y1);
|
|
19766
|
+
const C = modP(_2n2 * modP(Z1 * Z1));
|
|
19767
|
+
const D = modP(a * A);
|
|
19768
|
+
const x1y1 = X1 + Y1;
|
|
19769
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
19770
|
+
const G = D + B;
|
|
19771
|
+
const F = G - C;
|
|
19772
|
+
const H = D - B;
|
|
19773
|
+
const X3 = modP(E * F);
|
|
19774
|
+
const Y3 = modP(G * H);
|
|
19775
|
+
const T3 = modP(E * H);
|
|
19776
|
+
const Z3 = modP(F * G);
|
|
19777
|
+
return new Point(X3, Y3, Z3, T3);
|
|
19778
|
+
}
|
|
19779
|
+
// Fast algo for adding 2 Extended Points.
|
|
19780
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
19781
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
19782
|
+
add(other) {
|
|
19783
|
+
aextpoint(other);
|
|
19784
|
+
const { a, d } = CURVE;
|
|
19785
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
19786
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
19787
|
+
const A = modP(X1 * X2);
|
|
19788
|
+
const B = modP(Y1 * Y2);
|
|
19789
|
+
const C = modP(T1 * d * T2);
|
|
19790
|
+
const D = modP(Z1 * Z2);
|
|
19791
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
19792
|
+
const F = D - C;
|
|
19793
|
+
const G = D + C;
|
|
19794
|
+
const H = modP(B - a * A);
|
|
19795
|
+
const X3 = modP(E * F);
|
|
19796
|
+
const Y3 = modP(G * H);
|
|
19797
|
+
const T3 = modP(E * H);
|
|
19798
|
+
const Z3 = modP(F * G);
|
|
19799
|
+
return new Point(X3, Y3, Z3, T3);
|
|
19800
|
+
}
|
|
19801
|
+
subtract(other) {
|
|
19802
|
+
return this.add(other.negate());
|
|
19803
|
+
}
|
|
19804
|
+
// Constant-time multiplication.
|
|
19805
|
+
multiply(scalar) {
|
|
19806
|
+
if (!Fn2.isValidNot0(scalar))
|
|
19807
|
+
throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
19808
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
|
|
19809
|
+
return normalizeZ(Point, [p, f])[0];
|
|
19810
|
+
}
|
|
19811
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
19812
|
+
// It's faster, but should only be used when you don't care about
|
|
19813
|
+
// an exposed private key e.g. sig verification.
|
|
19814
|
+
// Does NOT allow scalars higher than CURVE.n.
|
|
19815
|
+
// Accepts optional accumulator to merge with multiply (important for sparse scalars)
|
|
19816
|
+
multiplyUnsafe(scalar, acc = Point.ZERO) {
|
|
19817
|
+
if (!Fn2.isValid(scalar))
|
|
19818
|
+
throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
19819
|
+
if (scalar === _0n4)
|
|
19820
|
+
return Point.ZERO;
|
|
19821
|
+
if (this.is0() || scalar === _1n4)
|
|
19822
|
+
return this;
|
|
19823
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
|
|
19824
|
+
}
|
|
19825
|
+
// Checks if point is of small order.
|
|
19826
|
+
// If you add something to small order point, you will have "dirty"
|
|
19827
|
+
// point with torsion component.
|
|
19828
|
+
// Multiplies point by cofactor and checks if the result is 0.
|
|
19829
|
+
isSmallOrder() {
|
|
19830
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
19831
|
+
}
|
|
19832
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
19833
|
+
// Returns `false` is the point is dirty.
|
|
19834
|
+
isTorsionFree() {
|
|
19835
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
19836
|
+
}
|
|
19837
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
19838
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
19839
|
+
toAffine(invertedZ) {
|
|
19840
|
+
return toAffineMemo(this, invertedZ);
|
|
19841
|
+
}
|
|
19842
|
+
clearCofactor() {
|
|
19843
|
+
if (cofactor === _1n4)
|
|
19844
|
+
return this;
|
|
19845
|
+
return this.multiplyUnsafe(cofactor);
|
|
19846
|
+
}
|
|
19847
|
+
toBytes() {
|
|
19848
|
+
const { x, y } = this.toAffine();
|
|
19849
|
+
const bytes = Fp2.toBytes(y);
|
|
19850
|
+
bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
|
|
19851
|
+
return bytes;
|
|
19852
|
+
}
|
|
19853
|
+
toHex() {
|
|
19854
|
+
return bytesToHex(this.toBytes());
|
|
19855
|
+
}
|
|
19856
|
+
toString() {
|
|
19857
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
19858
|
+
}
|
|
19859
|
+
// TODO: remove
|
|
19860
|
+
get ex() {
|
|
19861
|
+
return this.X;
|
|
19862
|
+
}
|
|
19863
|
+
get ey() {
|
|
19864
|
+
return this.Y;
|
|
19865
|
+
}
|
|
19866
|
+
get ez() {
|
|
19867
|
+
return this.Z;
|
|
19868
|
+
}
|
|
19869
|
+
get et() {
|
|
19870
|
+
return this.T;
|
|
19871
|
+
}
|
|
19872
|
+
static normalizeZ(points) {
|
|
19873
|
+
return normalizeZ(Point, points);
|
|
19874
|
+
}
|
|
19875
|
+
static msm(points, scalars) {
|
|
19876
|
+
return pippenger(Point, Fn2, points, scalars);
|
|
19877
|
+
}
|
|
19878
|
+
_setWindowSize(windowSize) {
|
|
19879
|
+
this.precompute(windowSize);
|
|
19880
|
+
}
|
|
19881
|
+
toRawBytes() {
|
|
19882
|
+
return this.toBytes();
|
|
19883
|
+
}
|
|
19884
|
+
}
|
|
19885
|
+
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
19886
|
+
Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
19887
|
+
Point.Fp = Fp2;
|
|
19888
|
+
Point.Fn = Fn2;
|
|
19889
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
19890
|
+
Point.BASE.precompute(8);
|
|
19891
|
+
return Point;
|
|
19892
|
+
}
|
|
19893
|
+
var PrimeEdwardsPoint = class {
|
|
19894
|
+
constructor(ep) {
|
|
19895
|
+
this.ep = ep;
|
|
19896
|
+
}
|
|
19897
|
+
// Static methods that must be implemented by subclasses
|
|
19898
|
+
static fromBytes(_bytes) {
|
|
19899
|
+
notImplemented();
|
|
19900
|
+
}
|
|
19901
|
+
static fromHex(_hex) {
|
|
19902
|
+
notImplemented();
|
|
19903
|
+
}
|
|
19904
|
+
get x() {
|
|
19905
|
+
return this.toAffine().x;
|
|
19906
|
+
}
|
|
19907
|
+
get y() {
|
|
19908
|
+
return this.toAffine().y;
|
|
19909
|
+
}
|
|
19910
|
+
// Common implementations
|
|
19911
|
+
clearCofactor() {
|
|
19912
|
+
return this;
|
|
19913
|
+
}
|
|
19914
|
+
assertValidity() {
|
|
19915
|
+
this.ep.assertValidity();
|
|
19916
|
+
}
|
|
19917
|
+
toAffine(invertedZ) {
|
|
19918
|
+
return this.ep.toAffine(invertedZ);
|
|
19919
|
+
}
|
|
19920
|
+
toHex() {
|
|
19921
|
+
return bytesToHex(this.toBytes());
|
|
19922
|
+
}
|
|
19923
|
+
toString() {
|
|
19924
|
+
return this.toHex();
|
|
19925
|
+
}
|
|
19926
|
+
isTorsionFree() {
|
|
19927
|
+
return true;
|
|
19928
|
+
}
|
|
19929
|
+
isSmallOrder() {
|
|
19930
|
+
return false;
|
|
19931
|
+
}
|
|
19932
|
+
add(other) {
|
|
19933
|
+
this.assertSame(other);
|
|
19934
|
+
return this.init(this.ep.add(other.ep));
|
|
19935
|
+
}
|
|
19936
|
+
subtract(other) {
|
|
19937
|
+
this.assertSame(other);
|
|
19938
|
+
return this.init(this.ep.subtract(other.ep));
|
|
19939
|
+
}
|
|
19940
|
+
multiply(scalar) {
|
|
19941
|
+
return this.init(this.ep.multiply(scalar));
|
|
19942
|
+
}
|
|
19943
|
+
multiplyUnsafe(scalar) {
|
|
19944
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
19945
|
+
}
|
|
19946
|
+
double() {
|
|
19947
|
+
return this.init(this.ep.double());
|
|
19948
|
+
}
|
|
19949
|
+
negate() {
|
|
19950
|
+
return this.init(this.ep.negate());
|
|
19951
|
+
}
|
|
19952
|
+
precompute(windowSize, isLazy) {
|
|
19953
|
+
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
19954
|
+
}
|
|
19955
|
+
/** @deprecated use `toBytes` */
|
|
19956
|
+
toRawBytes() {
|
|
19957
|
+
return this.toBytes();
|
|
19958
|
+
}
|
|
19959
|
+
};
|
|
19960
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
19961
|
+
if (typeof cHash !== "function")
|
|
19962
|
+
throw new Error('"hash" function param is required');
|
|
19963
|
+
_validateObject(eddsaOpts, {}, {
|
|
19964
|
+
adjustScalarBytes: "function",
|
|
19965
|
+
randomBytes: "function",
|
|
19966
|
+
domain: "function",
|
|
19967
|
+
prehash: "function",
|
|
19968
|
+
mapToCurve: "function"
|
|
19969
|
+
});
|
|
19970
|
+
const { prehash } = eddsaOpts;
|
|
19971
|
+
const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
|
|
19972
|
+
const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
|
|
19973
|
+
const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
|
|
19974
|
+
const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
|
|
19975
|
+
_abool2(phflag, "phflag");
|
|
19976
|
+
if (ctx.length || phflag)
|
|
19977
|
+
throw new Error("Contexts/pre-hash are not supported");
|
|
19978
|
+
return data;
|
|
19979
|
+
});
|
|
19980
|
+
function modN_LE(hash) {
|
|
19981
|
+
return Fn2.create(bytesToNumberLE(hash));
|
|
19982
|
+
}
|
|
19983
|
+
function getPrivateScalar(key) {
|
|
19984
|
+
const len = lengths.secretKey;
|
|
19985
|
+
key = ensureBytes("private key", key, len);
|
|
19986
|
+
const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
|
|
19987
|
+
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
19988
|
+
const prefix = hashed.slice(len, 2 * len);
|
|
19989
|
+
const scalar = modN_LE(head);
|
|
19990
|
+
return { head, prefix, scalar };
|
|
19991
|
+
}
|
|
19992
|
+
function getExtendedPublicKey(secretKey) {
|
|
19993
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
19994
|
+
const point = BASE.multiply(scalar);
|
|
19995
|
+
const pointBytes = point.toBytes();
|
|
19996
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
19997
|
+
}
|
|
19998
|
+
function getPublicKey(secretKey) {
|
|
19999
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
20000
|
+
}
|
|
20001
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
20002
|
+
const msg = concatBytes(...msgs);
|
|
20003
|
+
return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
|
|
20004
|
+
}
|
|
20005
|
+
function sign(msg, secretKey, options = {}) {
|
|
20006
|
+
msg = ensureBytes("message", msg);
|
|
20007
|
+
if (prehash)
|
|
20008
|
+
msg = prehash(msg);
|
|
20009
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
20010
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
20011
|
+
const R = BASE.multiply(r).toBytes();
|
|
20012
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
20013
|
+
const s = Fn2.create(r + k * scalar);
|
|
20014
|
+
if (!Fn2.isValid(s))
|
|
20015
|
+
throw new Error("sign failed: invalid s");
|
|
20016
|
+
const rs = concatBytes(R, Fn2.toBytes(s));
|
|
20017
|
+
return _abytes2(rs, lengths.signature, "result");
|
|
20018
|
+
}
|
|
20019
|
+
const verifyOpts = { zip215: true };
|
|
20020
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
20021
|
+
const { context, zip215 } = options;
|
|
20022
|
+
const len = lengths.signature;
|
|
20023
|
+
sig = ensureBytes("signature", sig, len);
|
|
20024
|
+
msg = ensureBytes("message", msg);
|
|
20025
|
+
publicKey = ensureBytes("publicKey", publicKey, lengths.publicKey);
|
|
20026
|
+
if (zip215 !== void 0)
|
|
20027
|
+
_abool2(zip215, "zip215");
|
|
20028
|
+
if (prehash)
|
|
20029
|
+
msg = prehash(msg);
|
|
20030
|
+
const mid = len / 2;
|
|
20031
|
+
const r = sig.subarray(0, mid);
|
|
20032
|
+
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
20033
|
+
let A, R, SB;
|
|
20034
|
+
try {
|
|
20035
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
20036
|
+
R = Point.fromBytes(r, zip215);
|
|
20037
|
+
SB = BASE.multiplyUnsafe(s);
|
|
20038
|
+
} catch (error) {
|
|
20039
|
+
return false;
|
|
20040
|
+
}
|
|
20041
|
+
if (!zip215 && A.isSmallOrder())
|
|
20042
|
+
return false;
|
|
20043
|
+
const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
|
|
20044
|
+
const RkA = R.add(A.multiplyUnsafe(k));
|
|
20045
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
20046
|
+
}
|
|
20047
|
+
const _size = Fp2.BYTES;
|
|
20048
|
+
const lengths = {
|
|
20049
|
+
secretKey: _size,
|
|
20050
|
+
publicKey: _size,
|
|
20051
|
+
signature: 2 * _size,
|
|
20052
|
+
seed: _size
|
|
20053
|
+
};
|
|
20054
|
+
function randomSecretKey(seed = randomBytes2(lengths.seed)) {
|
|
20055
|
+
return _abytes2(seed, lengths.seed, "seed");
|
|
20056
|
+
}
|
|
20057
|
+
function keygen(seed) {
|
|
20058
|
+
const secretKey = utils.randomSecretKey(seed);
|
|
20059
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
20060
|
+
}
|
|
20061
|
+
function isValidSecretKey(key) {
|
|
20062
|
+
return isBytes(key) && key.length === Fn2.BYTES;
|
|
20063
|
+
}
|
|
20064
|
+
function isValidPublicKey(key, zip215) {
|
|
20065
|
+
try {
|
|
20066
|
+
return !!Point.fromBytes(key, zip215);
|
|
20067
|
+
} catch (error) {
|
|
20068
|
+
return false;
|
|
20069
|
+
}
|
|
20070
|
+
}
|
|
20071
|
+
const utils = {
|
|
20072
|
+
getExtendedPublicKey,
|
|
20073
|
+
randomSecretKey,
|
|
20074
|
+
isValidSecretKey,
|
|
20075
|
+
isValidPublicKey,
|
|
20076
|
+
/**
|
|
20077
|
+
* Converts ed public key to x public key. Uses formula:
|
|
20078
|
+
* - ed25519:
|
|
20079
|
+
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
20080
|
+
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
20081
|
+
* - ed448:
|
|
20082
|
+
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
20083
|
+
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
20084
|
+
*/
|
|
20085
|
+
toMontgomery(publicKey) {
|
|
20086
|
+
const { y } = Point.fromBytes(publicKey);
|
|
20087
|
+
const size = lengths.publicKey;
|
|
20088
|
+
const is25519 = size === 32;
|
|
20089
|
+
if (!is25519 && size !== 57)
|
|
20090
|
+
throw new Error("only defined for 25519 and 448");
|
|
20091
|
+
const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
|
|
20092
|
+
return Fp2.toBytes(u);
|
|
20093
|
+
},
|
|
20094
|
+
toMontgomerySecret(secretKey) {
|
|
20095
|
+
const size = lengths.secretKey;
|
|
20096
|
+
_abytes2(secretKey, size);
|
|
20097
|
+
const hashed = cHash(secretKey.subarray(0, size));
|
|
20098
|
+
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
20099
|
+
},
|
|
20100
|
+
/** @deprecated */
|
|
20101
|
+
randomPrivateKey: randomSecretKey,
|
|
20102
|
+
/** @deprecated */
|
|
20103
|
+
precompute(windowSize = 8, point = Point.BASE) {
|
|
20104
|
+
return point.precompute(windowSize, false);
|
|
20105
|
+
}
|
|
20106
|
+
};
|
|
20107
|
+
return Object.freeze({
|
|
20108
|
+
keygen,
|
|
20109
|
+
getPublicKey,
|
|
20110
|
+
sign,
|
|
20111
|
+
verify,
|
|
20112
|
+
utils,
|
|
20113
|
+
Point,
|
|
20114
|
+
lengths
|
|
20115
|
+
});
|
|
20116
|
+
}
|
|
20117
|
+
function _eddsa_legacy_opts_to_new(c) {
|
|
20118
|
+
const CURVE = {
|
|
20119
|
+
a: c.a,
|
|
20120
|
+
d: c.d,
|
|
20121
|
+
p: c.Fp.ORDER,
|
|
20122
|
+
n: c.n,
|
|
20123
|
+
h: c.h,
|
|
20124
|
+
Gx: c.Gx,
|
|
20125
|
+
Gy: c.Gy
|
|
20126
|
+
};
|
|
20127
|
+
const Fp2 = c.Fp;
|
|
20128
|
+
const Fn2 = Field(CURVE.n, c.nBitLength, true);
|
|
20129
|
+
const curveOpts = { Fp: Fp2, Fn: Fn2, uvRatio: c.uvRatio };
|
|
20130
|
+
const eddsaOpts = {
|
|
20131
|
+
randomBytes: c.randomBytes,
|
|
20132
|
+
adjustScalarBytes: c.adjustScalarBytes,
|
|
20133
|
+
domain: c.domain,
|
|
20134
|
+
prehash: c.prehash,
|
|
20135
|
+
mapToCurve: c.mapToCurve
|
|
20136
|
+
};
|
|
20137
|
+
return { CURVE, curveOpts, hash: c.hash, eddsaOpts };
|
|
20138
|
+
}
|
|
20139
|
+
function _eddsa_new_output_to_legacy(c, eddsa2) {
|
|
20140
|
+
const Point = eddsa2.Point;
|
|
20141
|
+
const legacy = Object.assign({}, eddsa2, {
|
|
20142
|
+
ExtendedPoint: Point,
|
|
20143
|
+
CURVE: c,
|
|
20144
|
+
nBitLength: Point.Fn.BITS,
|
|
20145
|
+
nByteLength: Point.Fn.BYTES
|
|
20146
|
+
});
|
|
20147
|
+
return legacy;
|
|
20148
|
+
}
|
|
20149
|
+
function twistedEdwards(c) {
|
|
20150
|
+
const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
|
|
20151
|
+
const Point = edwards(CURVE, curveOpts);
|
|
20152
|
+
const EDDSA = eddsa(Point, hash, eddsaOpts);
|
|
20153
|
+
return _eddsa_new_output_to_legacy(c, EDDSA);
|
|
20154
|
+
}
|
|
20155
|
+
|
|
20156
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/ed25519.js
|
|
20157
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
20158
|
+
var _1n5 = BigInt(1);
|
|
20159
|
+
var _2n3 = BigInt(2);
|
|
20160
|
+
var _3n2 = BigInt(3);
|
|
20161
|
+
var _5n2 = BigInt(5);
|
|
20162
|
+
var _8n3 = BigInt(8);
|
|
20163
|
+
var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
20164
|
+
var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
20165
|
+
p: ed25519_CURVE_p,
|
|
20166
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
20167
|
+
h: _8n3,
|
|
20168
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
20169
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
20170
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
20171
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
20172
|
+
}))();
|
|
20173
|
+
function ed25519_pow_2_252_3(x) {
|
|
20174
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
20175
|
+
const P = ed25519_CURVE_p;
|
|
20176
|
+
const x2 = x * x % P;
|
|
20177
|
+
const b2 = x2 * x % P;
|
|
20178
|
+
const b4 = pow2(b2, _2n3, P) * b2 % P;
|
|
20179
|
+
const b5 = pow2(b4, _1n5, P) * x % P;
|
|
20180
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
20181
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
20182
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
20183
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
20184
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
20185
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
20186
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
20187
|
+
const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
|
|
20188
|
+
return { pow_p_5_8, b2 };
|
|
20189
|
+
}
|
|
20190
|
+
function adjustScalarBytes(bytes) {
|
|
20191
|
+
bytes[0] &= 248;
|
|
20192
|
+
bytes[31] &= 127;
|
|
20193
|
+
bytes[31] |= 64;
|
|
20194
|
+
return bytes;
|
|
20195
|
+
}
|
|
20196
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
20197
|
+
function uvRatio(u, v) {
|
|
20198
|
+
const P = ed25519_CURVE_p;
|
|
20199
|
+
const v3 = mod(v * v * v, P);
|
|
20200
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
20201
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
20202
|
+
let x = mod(u * v3 * pow, P);
|
|
20203
|
+
const vx2 = mod(v * x * x, P);
|
|
20204
|
+
const root1 = x;
|
|
20205
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
20206
|
+
const useRoot1 = vx2 === u;
|
|
20207
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
20208
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
20209
|
+
if (useRoot1)
|
|
20210
|
+
x = root1;
|
|
20211
|
+
if (useRoot2 || noRoot)
|
|
20212
|
+
x = root2;
|
|
20213
|
+
if (isNegativeLE(x, P))
|
|
20214
|
+
x = mod(-x, P);
|
|
20215
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
20216
|
+
}
|
|
20217
|
+
var Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
|
|
20218
|
+
var Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
|
|
20219
|
+
var ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
20220
|
+
...ed25519_CURVE,
|
|
20221
|
+
Fp,
|
|
20222
|
+
hash: sha512,
|
|
20223
|
+
adjustScalarBytes,
|
|
20224
|
+
// dom2
|
|
20225
|
+
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
20226
|
+
// Constant-time, u/√v
|
|
20227
|
+
uvRatio
|
|
20228
|
+
}))();
|
|
20229
|
+
var ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
|
20230
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
20231
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
20232
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
20233
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
20234
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
20235
|
+
var invertSqrt = (number) => uvRatio(_1n5, number);
|
|
20236
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
20237
|
+
var bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
20238
|
+
function calcElligatorRistrettoMap(r0) {
|
|
20239
|
+
const { d } = ed25519_CURVE;
|
|
20240
|
+
const P = ed25519_CURVE_p;
|
|
20241
|
+
const mod2 = (n) => Fp.create(n);
|
|
20242
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
20243
|
+
const Ns = mod2((r + _1n5) * ONE_MINUS_D_SQ);
|
|
20244
|
+
let c = BigInt(-1);
|
|
20245
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
20246
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
20247
|
+
let s_ = mod2(s * r0);
|
|
20248
|
+
if (!isNegativeLE(s_, P))
|
|
20249
|
+
s_ = mod2(-s_);
|
|
20250
|
+
if (!Ns_D_is_sq)
|
|
20251
|
+
s = s_;
|
|
20252
|
+
if (!Ns_D_is_sq)
|
|
20253
|
+
c = r;
|
|
20254
|
+
const Nt = mod2(c * (r - _1n5) * D_MINUS_ONE_SQ - D);
|
|
20255
|
+
const s2 = s * s;
|
|
20256
|
+
const W0 = mod2((s + s) * D);
|
|
20257
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
20258
|
+
const W2 = mod2(_1n5 - s2);
|
|
20259
|
+
const W3 = mod2(_1n5 + s2);
|
|
20260
|
+
return new ed25519.Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
20261
|
+
}
|
|
20262
|
+
function ristretto255_map(bytes) {
|
|
20263
|
+
abytes(bytes, 64);
|
|
20264
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
20265
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
20266
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
20267
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
20268
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
20269
|
+
}
|
|
20270
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
20271
|
+
constructor(ep) {
|
|
20272
|
+
super(ep);
|
|
20273
|
+
}
|
|
20274
|
+
static fromAffine(ap) {
|
|
20275
|
+
return new __RistrettoPoint(ed25519.Point.fromAffine(ap));
|
|
20276
|
+
}
|
|
20277
|
+
assertSame(other) {
|
|
20278
|
+
if (!(other instanceof __RistrettoPoint))
|
|
20279
|
+
throw new Error("RistrettoPoint expected");
|
|
20280
|
+
}
|
|
20281
|
+
init(ep) {
|
|
20282
|
+
return new __RistrettoPoint(ep);
|
|
20283
|
+
}
|
|
20284
|
+
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
20285
|
+
static hashToCurve(hex2) {
|
|
20286
|
+
return ristretto255_map(ensureBytes("ristrettoHash", hex2, 64));
|
|
20287
|
+
}
|
|
20288
|
+
static fromBytes(bytes) {
|
|
20289
|
+
abytes(bytes, 32);
|
|
20290
|
+
const { a, d } = ed25519_CURVE;
|
|
20291
|
+
const P = ed25519_CURVE_p;
|
|
20292
|
+
const mod2 = (n) => Fp.create(n);
|
|
20293
|
+
const s = bytes255ToNumberLE(bytes);
|
|
20294
|
+
if (!equalBytes2(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
20295
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
20296
|
+
const s2 = mod2(s * s);
|
|
20297
|
+
const u1 = mod2(_1n5 + a * s2);
|
|
20298
|
+
const u2 = mod2(_1n5 - a * s2);
|
|
20299
|
+
const u1_2 = mod2(u1 * u1);
|
|
20300
|
+
const u2_2 = mod2(u2 * u2);
|
|
20301
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
20302
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
20303
|
+
const Dx = mod2(I * u2);
|
|
20304
|
+
const Dy = mod2(I * Dx * v);
|
|
20305
|
+
let x = mod2((s + s) * Dx);
|
|
20306
|
+
if (isNegativeLE(x, P))
|
|
20307
|
+
x = mod2(-x);
|
|
20308
|
+
const y = mod2(u1 * Dy);
|
|
20309
|
+
const t = mod2(x * y);
|
|
20310
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n5)
|
|
20311
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
20312
|
+
return new __RistrettoPoint(new ed25519.Point(x, y, _1n5, t));
|
|
20313
|
+
}
|
|
20314
|
+
/**
|
|
20315
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
20316
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
20317
|
+
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
20318
|
+
*/
|
|
20319
|
+
static fromHex(hex2) {
|
|
20320
|
+
return __RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex2, 32));
|
|
20321
|
+
}
|
|
20322
|
+
static msm(points, scalars) {
|
|
20323
|
+
return pippenger(__RistrettoPoint, ed25519.Point.Fn, points, scalars);
|
|
20324
|
+
}
|
|
20325
|
+
/**
|
|
20326
|
+
* Encodes ristretto point to Uint8Array.
|
|
20327
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
20328
|
+
*/
|
|
20329
|
+
toBytes() {
|
|
20330
|
+
let { X, Y, Z, T } = this.ep;
|
|
20331
|
+
const P = ed25519_CURVE_p;
|
|
20332
|
+
const mod2 = (n) => Fp.create(n);
|
|
20333
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
20334
|
+
const u2 = mod2(X * Y);
|
|
20335
|
+
const u2sq = mod2(u2 * u2);
|
|
20336
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
20337
|
+
const D1 = mod2(invsqrt * u1);
|
|
20338
|
+
const D2 = mod2(invsqrt * u2);
|
|
20339
|
+
const zInv = mod2(D1 * D2 * T);
|
|
20340
|
+
let D;
|
|
20341
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
20342
|
+
let _x = mod2(Y * SQRT_M1);
|
|
20343
|
+
let _y = mod2(X * SQRT_M1);
|
|
20344
|
+
X = _x;
|
|
20345
|
+
Y = _y;
|
|
20346
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
20347
|
+
} else {
|
|
20348
|
+
D = D2;
|
|
20349
|
+
}
|
|
20350
|
+
if (isNegativeLE(X * zInv, P))
|
|
20351
|
+
Y = mod2(-Y);
|
|
20352
|
+
let s = mod2((Z - Y) * D);
|
|
20353
|
+
if (isNegativeLE(s, P))
|
|
20354
|
+
s = mod2(-s);
|
|
20355
|
+
return Fp.toBytes(s);
|
|
20356
|
+
}
|
|
20357
|
+
/**
|
|
20358
|
+
* Compares two Ristretto points.
|
|
20359
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
20360
|
+
*/
|
|
20361
|
+
equals(other) {
|
|
20362
|
+
this.assertSame(other);
|
|
20363
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
20364
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
20365
|
+
const mod2 = (n) => Fp.create(n);
|
|
20366
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
20367
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
20368
|
+
return one || two;
|
|
20369
|
+
}
|
|
20370
|
+
is0() {
|
|
20371
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
20372
|
+
}
|
|
20373
|
+
};
|
|
20374
|
+
_RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
|
|
20375
|
+
_RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
|
|
20376
|
+
_RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
|
|
20377
|
+
_RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
|
|
20378
|
+
|
|
20379
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2-receiver.js
|
|
20380
|
+
var BCPV2_LEAD_BOUND_MS = 5e3;
|
|
20381
|
+
var REPLAY_WINDOW = 64n;
|
|
20382
|
+
var BUCKET_DEPTH = 64;
|
|
20383
|
+
var BUCKET_RATE_PER_MS = 1;
|
|
20384
|
+
var hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
20385
|
+
function sameBytes(a, b) {
|
|
20386
|
+
if (a.byteLength !== b.byteLength)
|
|
20387
|
+
return false;
|
|
20388
|
+
let diff = 0;
|
|
20389
|
+
for (let i = 0; i < a.byteLength; i++)
|
|
20390
|
+
diff |= a[i] ^ b[i];
|
|
20391
|
+
return diff === 0;
|
|
20392
|
+
}
|
|
20393
|
+
var BcProvenanceV2Receiver = class {
|
|
20394
|
+
maxAuthDelayMs;
|
|
20395
|
+
manifestTimerMs;
|
|
20396
|
+
widenMs;
|
|
20397
|
+
/** Datagrams that left a hold queue at expiry, per path: the loss signal for failover. */
|
|
20398
|
+
expired = { multicast: 0, unicast: 0 };
|
|
20399
|
+
/**
|
|
20400
|
+
* Unicast repair (Section 7.2.7 step 2): on a receiver that can request it, how
|
|
20401
|
+
* long ago (ms) the first packet admitted into the oldest open block with an FEC
|
|
20402
|
+
* deadline was admitted; undefined with none open. A pending entry still live
|
|
20403
|
+
* then is kept instead of expiring, and is released with the block, under the
|
|
20404
|
+
* open-block bound of [MOQ-FEC] Section 9.
|
|
20405
|
+
*/
|
|
20406
|
+
keepPendingAgeMs = null;
|
|
20407
|
+
cfg;
|
|
20408
|
+
now;
|
|
20409
|
+
verifySig;
|
|
20410
|
+
accepted;
|
|
20411
|
+
queueLimit;
|
|
20412
|
+
current = null;
|
|
20413
|
+
previous = null;
|
|
20414
|
+
graceStart = {};
|
|
20415
|
+
hashAlg = null;
|
|
20416
|
+
verified = /* @__PURE__ */ new Map();
|
|
20417
|
+
highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
20418
|
+
closedAt = /* @__PURE__ */ new Map();
|
|
20419
|
+
lead = null;
|
|
20420
|
+
pending = /* @__PURE__ */ new Map();
|
|
20421
|
+
admittedUntil = /* @__PURE__ */ new Map();
|
|
20422
|
+
hold = { multicast: [], unicast: [] };
|
|
20423
|
+
holdBytes = { multicast: 0, unicast: 0 };
|
|
20424
|
+
order = 0;
|
|
20425
|
+
buckets;
|
|
20426
|
+
constructor(cfg) {
|
|
20427
|
+
this.maxAuthDelayMs = cfg.maxAuthDelayMs ?? 200;
|
|
20428
|
+
this.manifestTimerMs = cfg.manifestTimerMs ?? 20;
|
|
20429
|
+
if (!(this.maxAuthDelayMs >= 50 && this.maxAuthDelayMs <= 1e3) || !(this.manifestTimerMs >= 1)) {
|
|
20430
|
+
throw new Error("BCPV2 endpoint ignored: maxAuthDelayMs or manifestTimerMs out of range");
|
|
20431
|
+
}
|
|
20432
|
+
if (!(cfg.bandwidth > 0))
|
|
20433
|
+
throw new Error("BCPV2 endpoint ignored: protected endpoint without bandwidth");
|
|
20434
|
+
const kids = new Set(cfg.keys.map((k) => k.kid));
|
|
20435
|
+
const firsts = new Set(cfg.keys.map((k) => k.firstEpoch));
|
|
20436
|
+
if (cfg.keys.length < 1 || kids.size !== cfg.keys.length || firsts.size !== cfg.keys.length) {
|
|
20437
|
+
throw new Error("BCPV2 key set invalid: no keys, or a repeated kid or firstEpoch");
|
|
20438
|
+
}
|
|
20439
|
+
this.cfg = cfg;
|
|
20440
|
+
this.widenMs = Math.min(Math.max(cfg.widenMs ?? 0, 0), BCPV2_LEAD_BOUND_MS);
|
|
20441
|
+
this.now = cfg.now ?? Date.now;
|
|
20442
|
+
this.verifySig = cfg.verifySignature ?? ((pk, msg, sig) => {
|
|
20443
|
+
try {
|
|
20444
|
+
return ed25519.verify(sig, msg, pk, { zip215: false });
|
|
20445
|
+
} catch {
|
|
20446
|
+
return false;
|
|
20447
|
+
}
|
|
20448
|
+
});
|
|
20449
|
+
this.accepted = cfg.acceptedHashAlgs ?? [BCPV2_HASH_ALG_SHA256];
|
|
20450
|
+
const bandwidth = Math.min(cfg.bandwidth, 1e10);
|
|
20451
|
+
this.queueLimit = {
|
|
20452
|
+
multicast: this.maxAuthDelayMs * bandwidth / 8e3,
|
|
20453
|
+
unicast: (this.maxAuthDelayMs + this.widenMs) * bandwidth / 8e3
|
|
20454
|
+
};
|
|
20455
|
+
const t = this.now();
|
|
20456
|
+
this.buckets = {
|
|
20457
|
+
multicast: { tokens: BUCKET_DEPTH, last: t, dropped: 0 },
|
|
20458
|
+
mirror: { tokens: BUCKET_DEPTH, last: t, dropped: 0 }
|
|
20459
|
+
};
|
|
20460
|
+
}
|
|
20461
|
+
get grace() {
|
|
20462
|
+
return this.maxAuthDelayMs + 2 * this.manifestTimerMs;
|
|
20463
|
+
}
|
|
20464
|
+
/** The mirror's lead in force: 0 without a manifest track, the bound until one is measured. */
|
|
20465
|
+
get leadMs() {
|
|
20466
|
+
if (!this.cfg.applyLead)
|
|
20467
|
+
return 0;
|
|
20468
|
+
return this.lead ?? BCPV2_LEAD_BOUND_MS;
|
|
20469
|
+
}
|
|
20470
|
+
/** Step 1: a record datagram on `manifestPort`, or a record object of the manifest track. */
|
|
20471
|
+
onRecord(bytes, path) {
|
|
20472
|
+
const now = this.now();
|
|
20473
|
+
this.expire(now);
|
|
20474
|
+
const drop = (verdict) => ({ verdict, admitted: [] });
|
|
20475
|
+
const len = bytes.byteLength;
|
|
20476
|
+
if (len < BCPV2_MIN_RECORD_LENGTH || len > BCPV2_MAX_RECORD_LENGTH || (len - 132) % 32 !== 0)
|
|
20477
|
+
return drop("length");
|
|
20478
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, len);
|
|
20479
|
+
if (view2.getUint32(0) !== 1296583986)
|
|
20480
|
+
return drop("magic");
|
|
20481
|
+
const kid = view2.getUint32(4);
|
|
20482
|
+
const epoch = view2.getUint32(8);
|
|
20483
|
+
const mseq = view2.getBigUint64(12);
|
|
20484
|
+
const count = view2.getUint16(28);
|
|
20485
|
+
const hashAlg = bytes[30];
|
|
20486
|
+
const sent = view2.getUint32(32);
|
|
20487
|
+
const key = this.cfg.keys.find((k) => k.kid === kid);
|
|
20488
|
+
if (!key)
|
|
20489
|
+
return drop("kid");
|
|
20490
|
+
const next = Math.min(...this.cfg.keys.map((k) => k.firstEpoch).filter((f) => f > key.firstEpoch), Infinity);
|
|
20491
|
+
if (epoch < key.firstEpoch || epoch >= next)
|
|
20492
|
+
return drop("epoch");
|
|
20493
|
+
const id = `${kid}:${epoch}:${mseq}`;
|
|
20494
|
+
const seen = this.verified.get(id);
|
|
20495
|
+
if (seen) {
|
|
20496
|
+
const admitted2 = hex(sha2562(bytes)) === seen.digest ? this.arrival(seen, path, now) : [];
|
|
20497
|
+
return { verdict: "duplicate", admitted: admitted2 };
|
|
20498
|
+
}
|
|
20499
|
+
const ke = `${kid}:${epoch}`;
|
|
20500
|
+
if (this.current === null) {
|
|
20501
|
+
if (epoch < this.cfg.minEpoch || epoch < (this.cfg.pinnedEpochFloor?.get(kid) ?? 0))
|
|
20502
|
+
return drop("epoch");
|
|
20503
|
+
} else if (epoch < this.current.epoch && !this.previousAdmitted(epoch, path, now)) {
|
|
20504
|
+
return drop("epoch");
|
|
20505
|
+
}
|
|
20506
|
+
const floor = this.floor(ke, path);
|
|
20507
|
+
if (floor !== void 0 && mseq <= floor - REPLAY_WINDOW)
|
|
20508
|
+
return drop("mseq");
|
|
20509
|
+
const closed = this.closedAt.get(ke);
|
|
20510
|
+
if (closed !== void 0 && mseq > closed)
|
|
20511
|
+
return drop("closed");
|
|
20512
|
+
if (this.cfg.checkFreshness !== false) {
|
|
20513
|
+
const behind = 7e3 + this.maxAuthDelayMs + (path === "mirror" ? this.widenMs : 0);
|
|
20514
|
+
if (sent * 1e3 < now - behind || sent * 1e3 > now + 6e3)
|
|
20515
|
+
return drop("sent");
|
|
20516
|
+
}
|
|
20517
|
+
if (count !== (len - 132) / 32)
|
|
20518
|
+
return drop("count");
|
|
20519
|
+
if (!this.accepted.includes(hashAlg) || this.hashAlg !== null && hashAlg !== this.hashAlg)
|
|
20520
|
+
return drop("hash_alg");
|
|
20521
|
+
if (this.cfg.catalogCtx && !sameBytes(bytes.subarray(36, 68), this.cfg.catalogCtx))
|
|
20522
|
+
return drop("ctx");
|
|
20523
|
+
if (!this.take(path, now))
|
|
20524
|
+
return drop("budget");
|
|
20525
|
+
const record = decodeBcProvenanceV2Record(bytes);
|
|
20526
|
+
if (!this.verifySig(key.publicKey, bcProvenanceV2SignedMessage(record, this.cfg.endpoint), record.signature)) {
|
|
20527
|
+
return drop("signature");
|
|
20528
|
+
}
|
|
20529
|
+
if (this.current === null || epoch > this.current.epoch) {
|
|
20530
|
+
if (this.current !== null) {
|
|
20531
|
+
this.previous = { epoch: this.current.epoch, deadline: now + BCPV2_LEAD_BOUND_MS + this.grace };
|
|
20532
|
+
}
|
|
20533
|
+
this.current = { epoch, since: now };
|
|
20534
|
+
this.graceStart = { [path]: now };
|
|
20535
|
+
if (this.hashAlg === null) {
|
|
20536
|
+
this.hashAlg = hashAlg;
|
|
20537
|
+
for (const queue of Object.values(this.hold)) {
|
|
20538
|
+
for (const held of queue)
|
|
20539
|
+
held.digest ??= this.digest(held.datagram);
|
|
20540
|
+
}
|
|
20541
|
+
}
|
|
20542
|
+
} else if (epoch === this.current.epoch) {
|
|
20543
|
+
this.graceStart[path] ??= now;
|
|
20544
|
+
}
|
|
20545
|
+
const entry = {
|
|
20546
|
+
kid,
|
|
20547
|
+
epoch,
|
|
20548
|
+
mseq,
|
|
20549
|
+
path,
|
|
20550
|
+
arrival: now,
|
|
20551
|
+
sent,
|
|
20552
|
+
digest: hex(sha2562(bytes)),
|
|
20553
|
+
pairs: record.digests.map((d, i) => ({ digest: hex(d), seq: record.firstSeq + BigInt(i), admitted: false })),
|
|
20554
|
+
awaitingMulticast: path === "mirror"
|
|
20555
|
+
};
|
|
20556
|
+
this.verified.set(id, entry);
|
|
20557
|
+
this.raise(ke, path, mseq);
|
|
20558
|
+
if (record.flags & 1)
|
|
20559
|
+
this.closedAt.set(ke, mseq);
|
|
20560
|
+
const widen = path === "mirror" ? this.cfg.applyLead ? this.leadMs : this.widenMs : 0;
|
|
20561
|
+
const admitted = [];
|
|
20562
|
+
for (const pair of entry.pairs)
|
|
20563
|
+
this.handlePair(pair, now, now + this.grace + widen, now, admitted);
|
|
20564
|
+
this.dropAdmittedHeld();
|
|
20565
|
+
this.prune(now);
|
|
20566
|
+
return { verdict: "verified", admitted };
|
|
20567
|
+
}
|
|
20568
|
+
/** Step 2: a data datagram on `port` (or a packet of a listed track over MoQ unicast). */
|
|
20569
|
+
onDatagram(datagram, path) {
|
|
20570
|
+
const now = this.now();
|
|
20571
|
+
this.expire(now);
|
|
20572
|
+
if (this.hashAlg !== null) {
|
|
20573
|
+
const d = this.digest(datagram);
|
|
20574
|
+
if (this.consumePending(d, now))
|
|
20575
|
+
return { verdict: "admitted", admitted: [datagram] };
|
|
20576
|
+
if (this.admittedUntil.has(d))
|
|
20577
|
+
return { verdict: "duplicate", admitted: [] };
|
|
20578
|
+
return this.enqueue({ digest: d, datagram, expiry: 0, order: 0 }, path, now);
|
|
20579
|
+
}
|
|
20580
|
+
return this.enqueue({ datagram, expiry: 0, order: 0 }, path, now);
|
|
20581
|
+
}
|
|
20582
|
+
/**
|
|
20583
|
+
* A source packet recovered by FEC decode, its base SS_ID (SS_Start + ESI)
|
|
20584
|
+
* appended: admitted only on a pending entry it consumes, never held. One no
|
|
20585
|
+
* verified record lists, or a copy of an admitted packet, is dropped.
|
|
20586
|
+
*/
|
|
20587
|
+
admitRecovered(datagram) {
|
|
20588
|
+
const now = this.now();
|
|
20589
|
+
this.expire(now);
|
|
20590
|
+
return this.hashAlg !== null && this.consumePending(this.digest(datagram), now);
|
|
20591
|
+
}
|
|
20592
|
+
/**
|
|
20593
|
+
* Discard the per-flow state on a `streamId` change or a key-set change that fails the
|
|
20594
|
+
* flow's current (kid, epoch): budgets and hold queues survive, held digests are cleared.
|
|
20595
|
+
*/
|
|
20596
|
+
resetGeneration() {
|
|
20597
|
+
this.current = null;
|
|
20598
|
+
this.previous = null;
|
|
20599
|
+
this.graceStart = {};
|
|
20600
|
+
this.hashAlg = null;
|
|
20601
|
+
this.verified.clear();
|
|
20602
|
+
this.highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
20603
|
+
this.closedAt.clear();
|
|
20604
|
+
this.lead = null;
|
|
20605
|
+
this.pending.clear();
|
|
20606
|
+
this.admittedUntil.clear();
|
|
20607
|
+
for (const queue of Object.values(this.hold))
|
|
20608
|
+
for (const held of queue)
|
|
20609
|
+
held.digest = void 0;
|
|
20610
|
+
}
|
|
20611
|
+
/** The receiver ceased to hold the endpoint: drop everything; no drop here is an expiry. */
|
|
20612
|
+
release() {
|
|
20613
|
+
this.resetGeneration();
|
|
20614
|
+
this.hold = { multicast: [], unicast: [] };
|
|
20615
|
+
this.holdBytes = { multicast: 0, unicast: 0 };
|
|
20616
|
+
}
|
|
20617
|
+
digest(datagram) {
|
|
20618
|
+
return hex(bcProvenanceV2DatagramDigest(this.cfg.endpoint, datagram));
|
|
20619
|
+
}
|
|
20620
|
+
/** Consume the lowest-seq pending entry for `digest`, admitting it. */
|
|
20621
|
+
consumePending(digest, now) {
|
|
20622
|
+
const list = this.pending.get(digest);
|
|
20623
|
+
if (!list?.length)
|
|
20624
|
+
return false;
|
|
20625
|
+
list.sort((a, b) => a.pair.seq < b.pair.seq ? -1 : a.pair.seq > b.pair.seq ? 1 : 0);
|
|
20626
|
+
const hit = list.shift();
|
|
20627
|
+
if (list.length === 0)
|
|
20628
|
+
this.pending.delete(digest);
|
|
20629
|
+
hit.pair.pending = void 0;
|
|
20630
|
+
hit.pair.admitted = true;
|
|
20631
|
+
this.admittedUntil.set(digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
20632
|
+
return true;
|
|
20633
|
+
}
|
|
20634
|
+
enqueue(held, path, now) {
|
|
20635
|
+
if (this.holdBytes[path] + held.datagram.byteLength > this.queueLimit[path]) {
|
|
20636
|
+
return { verdict: "overflow", admitted: [] };
|
|
20637
|
+
}
|
|
20638
|
+
held.expiry = now + this.maxAuthDelayMs + (path === "unicast" ? this.widenMs : 0);
|
|
20639
|
+
held.order = this.order++;
|
|
20640
|
+
this.hold[path].push(held);
|
|
20641
|
+
this.holdBytes[path] += held.datagram.byteLength;
|
|
20642
|
+
return { verdict: "held", admitted: [] };
|
|
20643
|
+
}
|
|
20644
|
+
/** Remove and return the oldest held datagram with `digest`, across both queues. */
|
|
20645
|
+
takeHeld(digest) {
|
|
20646
|
+
let best;
|
|
20647
|
+
for (const path of ["multicast", "unicast"]) {
|
|
20648
|
+
const index = this.hold[path].findIndex((h) => h.digest === digest);
|
|
20649
|
+
if (index >= 0 && (!best || this.hold[path][index].order < best.order)) {
|
|
20650
|
+
best = { path, index, order: this.hold[path][index].order };
|
|
20651
|
+
}
|
|
20652
|
+
}
|
|
20653
|
+
if (!best)
|
|
20654
|
+
return void 0;
|
|
20655
|
+
const [held] = this.hold[best.path].splice(best.index, 1);
|
|
20656
|
+
this.holdBytes[best.path] -= held.datagram.byteLength;
|
|
20657
|
+
return held.datagram;
|
|
20658
|
+
}
|
|
20659
|
+
dropAdmittedHeld() {
|
|
20660
|
+
for (const path of ["multicast", "unicast"]) {
|
|
20661
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
20662
|
+
const keep = h.digest === void 0 || !this.admittedUntil.has(h.digest);
|
|
20663
|
+
if (!keep)
|
|
20664
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
20665
|
+
return keep;
|
|
20666
|
+
});
|
|
20667
|
+
}
|
|
20668
|
+
}
|
|
20669
|
+
/** Step 1 pair handling: admit a held copy, or insert a pending entry. */
|
|
20670
|
+
handlePair(pair, now, expiry, inserted, out) {
|
|
20671
|
+
const datagram = this.takeHeld(pair.digest);
|
|
20672
|
+
if (datagram) {
|
|
20673
|
+
pair.admitted = true;
|
|
20674
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
20675
|
+
out.push(datagram);
|
|
20676
|
+
return;
|
|
20677
|
+
}
|
|
20678
|
+
const entry = { pair, inserted, expiry };
|
|
20679
|
+
pair.pending = entry;
|
|
20680
|
+
const list = this.pending.get(pair.digest);
|
|
20681
|
+
if (list)
|
|
20682
|
+
list.push(entry);
|
|
20683
|
+
else
|
|
20684
|
+
this.pending.set(pair.digest, [entry]);
|
|
20685
|
+
}
|
|
20686
|
+
/** A byte-identical copy dropped by the already-verified lookup. */
|
|
20687
|
+
arrival(seen, path, now) {
|
|
20688
|
+
const ke = `${seen.kid}:${seen.epoch}`;
|
|
20689
|
+
this.raise(ke, path, seen.mseq);
|
|
20690
|
+
if (this.current && seen.epoch === this.current.epoch)
|
|
20691
|
+
this.graceStart[path] ??= now;
|
|
20692
|
+
if (path !== "multicast" || !seen.awaitingMulticast)
|
|
20693
|
+
return [];
|
|
20694
|
+
seen.awaitingMulticast = false;
|
|
20695
|
+
if (!this.cfg.applyLead)
|
|
20696
|
+
return [];
|
|
20697
|
+
const t = this.manifestTimerMs;
|
|
20698
|
+
const interval = Math.min(Math.ceil((now - seen.arrival) / t) * t, BCPV2_LEAD_BOUND_MS);
|
|
20699
|
+
if (this.lead === null) {
|
|
20700
|
+
this.lead = interval;
|
|
20701
|
+
return [];
|
|
20702
|
+
}
|
|
20703
|
+
if (interval <= this.lead)
|
|
20704
|
+
return [];
|
|
20705
|
+
this.lead = interval;
|
|
20706
|
+
const out = [];
|
|
20707
|
+
const records = [seen, ...[...this.verified.values()].filter((v) => v.awaitingMulticast && v.arrival >= now - interval)];
|
|
20708
|
+
for (const v of records) {
|
|
20709
|
+
for (const pair of v.pairs) {
|
|
20710
|
+
if (pair.admitted) {
|
|
20711
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + interval);
|
|
20712
|
+
} else if (pair.pending) {
|
|
20713
|
+
pair.pending.expiry = pair.pending.inserted + this.grace + interval;
|
|
20714
|
+
} else {
|
|
20715
|
+
this.handlePair(pair, now, now + this.grace + interval, now, out);
|
|
20716
|
+
}
|
|
20717
|
+
}
|
|
20718
|
+
}
|
|
20719
|
+
this.dropAdmittedHeld();
|
|
20720
|
+
return out;
|
|
20721
|
+
}
|
|
20722
|
+
previousAdmitted(epoch, path, now) {
|
|
20723
|
+
if (!this.previous || epoch !== this.previous.epoch)
|
|
20724
|
+
return false;
|
|
20725
|
+
const m = this.graceStart.multicast;
|
|
20726
|
+
if (path === "multicast")
|
|
20727
|
+
return m === void 0 || now <= m + this.grace;
|
|
20728
|
+
const starts = [m, this.graceStart.mirror].filter((s) => s !== void 0);
|
|
20729
|
+
return starts.length > 0 && now <= Math.min(...starts) + this.grace;
|
|
20730
|
+
}
|
|
20731
|
+
/** The replay floor this record meets: its path's, or the higher of both from the mirror. */
|
|
20732
|
+
floor(ke, path) {
|
|
20733
|
+
const m = this.highest.multicast.get(ke);
|
|
20734
|
+
if (path === "multicast")
|
|
20735
|
+
return m;
|
|
20736
|
+
const r = this.highest.mirror.get(ke);
|
|
20737
|
+
if (m === void 0)
|
|
20738
|
+
return r;
|
|
20739
|
+
return r === void 0 || m > r ? m : r;
|
|
20740
|
+
}
|
|
20741
|
+
raise(ke, path, mseq) {
|
|
20742
|
+
const h = this.highest[path].get(ke);
|
|
20743
|
+
if (h === void 0 || mseq > h)
|
|
20744
|
+
this.highest[path].set(ke, mseq);
|
|
20745
|
+
}
|
|
20746
|
+
take(path, now) {
|
|
20747
|
+
const b = this.buckets[path];
|
|
20748
|
+
b.tokens = Math.min(BUCKET_DEPTH, b.tokens + (now - b.last) * BUCKET_RATE_PER_MS);
|
|
20749
|
+
b.last = now;
|
|
20750
|
+
if (b.tokens < 1) {
|
|
20751
|
+
b.dropped++;
|
|
20752
|
+
return false;
|
|
20753
|
+
}
|
|
20754
|
+
b.tokens -= 1;
|
|
20755
|
+
return true;
|
|
20756
|
+
}
|
|
20757
|
+
expire(now) {
|
|
20758
|
+
for (const path of ["multicast", "unicast"]) {
|
|
20759
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
20760
|
+
if (h.expiry > now)
|
|
20761
|
+
return true;
|
|
20762
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
20763
|
+
this.expired[path]++;
|
|
20764
|
+
return false;
|
|
20765
|
+
});
|
|
20766
|
+
}
|
|
20767
|
+
let keepFrom = null;
|
|
20768
|
+
for (const [d, list] of this.pending) {
|
|
20769
|
+
const live = list.filter((p) => {
|
|
20770
|
+
if (p.expiry > now)
|
|
20771
|
+
return true;
|
|
20772
|
+
if (keepFrom === null) {
|
|
20773
|
+
const age = this.keepPendingAgeMs?.();
|
|
20774
|
+
keepFrom = age === void 0 ? void 0 : now - age;
|
|
20775
|
+
}
|
|
20776
|
+
if (keepFrom !== void 0 && keepFrom !== null && p.expiry > keepFrom)
|
|
20777
|
+
return true;
|
|
20778
|
+
p.pair.pending = void 0;
|
|
20779
|
+
return false;
|
|
20780
|
+
});
|
|
20781
|
+
if (live.length)
|
|
20782
|
+
this.pending.set(d, live);
|
|
20783
|
+
else
|
|
20784
|
+
this.pending.delete(d);
|
|
20785
|
+
}
|
|
20786
|
+
for (const [d, until] of this.admittedUntil)
|
|
20787
|
+
if (until <= now)
|
|
20788
|
+
this.admittedUntil.delete(d);
|
|
20789
|
+
if (this.previous && now > this.previous.deadline)
|
|
20790
|
+
this.previous = null;
|
|
20791
|
+
}
|
|
20792
|
+
/**
|
|
20793
|
+
* Retire verified entries below their arrival path's replay floor, keeping a mirrored entry
|
|
20794
|
+
* while the freshness check would still pass its copy on `manifestPort`.
|
|
20795
|
+
* ponytail: linear scan per record; index by (kid, epoch) if retained sets grow past a few thousand.
|
|
20796
|
+
*/
|
|
20797
|
+
prune(now) {
|
|
20798
|
+
const retention = 7e3 + this.maxAuthDelayMs;
|
|
20799
|
+
for (const [id, v] of this.verified) {
|
|
20800
|
+
const h = this.highest[v.path].get(`${v.kid}:${v.epoch}`);
|
|
20801
|
+
const aboveFloor = h === void 0 || v.mseq > h - REPLAY_WINDOW;
|
|
20802
|
+
const fresh = this.cfg.checkFreshness === false ? v.arrival + retention >= now : v.sent * 1e3 + retention >= now;
|
|
20803
|
+
const current = this.current && (v.epoch === this.current.epoch || v.epoch === this.previous?.epoch);
|
|
20804
|
+
if (current && (aboveFloor || v.path === "mirror" && fresh))
|
|
20805
|
+
continue;
|
|
20806
|
+
this.verified.delete(id);
|
|
20807
|
+
}
|
|
20808
|
+
}
|
|
20809
|
+
};
|
|
20810
|
+
|
|
20811
|
+
// src/multicast-manager.ts
|
|
20812
|
+
import { deriveFecInterleaveDepth as deriveFecInterleaveDepth3, FecBlockMapper as FecBlockMapper2 } from "@blockcast/mmt-fec";
|
|
20813
|
+
|
|
20814
|
+
// src/repair-layers.ts
|
|
20815
|
+
var RepairContractError = class extends Error {
|
|
20816
|
+
constructor(code, path, message) {
|
|
20817
|
+
super(`[repair-layers:${code}] ${path}: ${message}`);
|
|
20818
|
+
this.code = code;
|
|
20819
|
+
this.path = path;
|
|
20820
|
+
this.name = "RepairContractError";
|
|
17869
20821
|
}
|
|
17870
20822
|
};
|
|
17871
20823
|
var throwRepairContractError = (code, path, message) => {
|
|
@@ -17984,11 +20936,12 @@ function catalogFecUnavailable(fec, fecPath) {
|
|
|
17984
20936
|
}
|
|
17985
20937
|
|
|
17986
20938
|
// src/multicast-manager.ts
|
|
17987
|
-
function freezeIngress(tracks) {
|
|
20939
|
+
function freezeIngress(tracks, flow) {
|
|
17988
20940
|
return Object.freeze({
|
|
17989
20941
|
tracks: Object.freeze(
|
|
17990
20942
|
[...tracks].map(({ name, packetId }) => Object.freeze({ name, packetId }))
|
|
17991
|
-
)
|
|
20943
|
+
),
|
|
20944
|
+
...flow === void 0 ? {} : { flow }
|
|
17992
20945
|
});
|
|
17993
20946
|
}
|
|
17994
20947
|
function sameIdSet(a, b) {
|
|
@@ -18018,6 +20971,7 @@ function newBlockPolicyStats(packetId, catalogTrackName, epoch) {
|
|
|
18018
20971
|
repairsSkippedRttExceedsBudget: 0,
|
|
18019
20972
|
repairsSkippedPresentationDelayUnavailable: 0,
|
|
18020
20973
|
repairsSkippedRangeNoAnchor: 0,
|
|
20974
|
+
repairsSkippedGroupCannotComplete: 0,
|
|
18021
20975
|
lastRepairSkip: null,
|
|
18022
20976
|
keyframeRepairFetchesSent: 0,
|
|
18023
20977
|
rangeRepairFetchesSent: 0,
|
|
@@ -18102,7 +21056,9 @@ var REPAIR_PRIORITY_REASONS = /* @__PURE__ */ new Set([
|
|
|
18102
21056
|
var REPAIR_SKIP_REASONS = /* @__PURE__ */ new Set([
|
|
18103
21057
|
"rtt-unmeasured",
|
|
18104
21058
|
"rtt-exceeds-budget",
|
|
18105
|
-
"presentation-delay-unavailable"
|
|
21059
|
+
"presentation-delay-unavailable",
|
|
21060
|
+
"range-no-anchor",
|
|
21061
|
+
"group-cannot-complete"
|
|
18106
21062
|
]);
|
|
18107
21063
|
var REPAIR_DEADLINE_BOUNDS = /* @__PURE__ */ new Set(["wait", "presentation", "keyframe", "retention"]);
|
|
18108
21064
|
var GAP_REASONS = {
|
|
@@ -18320,7 +21276,7 @@ function altGroupKey(track) {
|
|
|
18320
21276
|
}
|
|
18321
21277
|
function repairTrackNamesBySource(tracks) {
|
|
18322
21278
|
const bySource = /* @__PURE__ */ new Map();
|
|
18323
|
-
const
|
|
21279
|
+
const add2 = (source, repair) => {
|
|
18324
21280
|
if (!source || !repair) return;
|
|
18325
21281
|
const names = bySource.get(source) ?? /* @__PURE__ */ new Set();
|
|
18326
21282
|
names.add(repair);
|
|
@@ -18328,9 +21284,9 @@ function repairTrackNamesBySource(tracks) {
|
|
|
18328
21284
|
};
|
|
18329
21285
|
for (const track of tracks) {
|
|
18330
21286
|
const repairTrack = typeof track.fec === "object" && track.fec !== null ? track.fec.repairTrack : void 0;
|
|
18331
|
-
if (typeof repairTrack === "string")
|
|
21287
|
+
if (typeof repairTrack === "string") add2(track.name, repairTrack);
|
|
18332
21288
|
if (track.packaging === "fec-repair" && Array.isArray(track.depends) && track.depends.length === 1) {
|
|
18333
|
-
|
|
21289
|
+
add2(track.depends[0], track.name);
|
|
18334
21290
|
}
|
|
18335
21291
|
}
|
|
18336
21292
|
return bySource;
|
|
@@ -18340,6 +21296,10 @@ var MulticastManager = class {
|
|
|
18340
21296
|
#router;
|
|
18341
21297
|
#hostMfuTimeoutMs;
|
|
18342
21298
|
#transport;
|
|
21299
|
+
/** The protected flow of the current join and its manifestPort subscription. */
|
|
21300
|
+
#provenanceJoin = null;
|
|
21301
|
+
#onFailback = null;
|
|
21302
|
+
#failbackHooked = false;
|
|
18343
21303
|
/** Live joinRepairEndpoints() releases → the repair owners they registered,
|
|
18344
21304
|
* re-applied after configureMultiTrack() clears catalog-derived owners. */
|
|
18345
21305
|
#repairReleases = /* @__PURE__ */ new Map();
|
|
@@ -18392,10 +21352,6 @@ var MulticastManager = class {
|
|
|
18392
21352
|
reassemblyPolicy: options?.reassemblyPolicy,
|
|
18393
21353
|
reassembler: options?.reassembler,
|
|
18394
21354
|
maxBufferedMfus: options?.maxBufferedMfus,
|
|
18395
|
-
manifestVerifier: options?.manifestVerifier,
|
|
18396
|
-
manifestVerifierOwnership: options?.manifestVerifierOwnership,
|
|
18397
|
-
resolveManifestIdentity: options?.resolveManifestIdentity,
|
|
18398
|
-
resolveManifestIdentityForRoute: options?.resolveManifestIdentityForRoute,
|
|
18399
21355
|
// The router has reset its own state for the stream; reset the rest.
|
|
18400
21356
|
onPublisherRestart: (packetId, catalogTrackName) => {
|
|
18401
21357
|
this.#resetStreamOutsideRouter(packetId, catalogTrackName);
|
|
@@ -18442,7 +21398,7 @@ var MulticastManager = class {
|
|
|
18442
21398
|
});
|
|
18443
21399
|
this.#hostMfuTimeoutMs = this.#router.reassembler.timeoutMs;
|
|
18444
21400
|
this.#transport = new MulticastTransport((packet, ingress) => {
|
|
18445
|
-
this.#router.feedPacket(packet, "multicast",
|
|
21401
|
+
this.#router.feedPacket(packet, "multicast", ingress);
|
|
18446
21402
|
});
|
|
18447
21403
|
this.#transport.onDelivery((delivery) => {
|
|
18448
21404
|
this.#fec.setMulticastSymbolDelivery(delivery);
|
|
@@ -18450,17 +21406,7 @@ var MulticastManager = class {
|
|
|
18450
21406
|
this.#fec.onBlockRecovered = (sbn, packetId, data, meta, recoveredTrackName) => {
|
|
18451
21407
|
const recovered = new Uint8Array(data);
|
|
18452
21408
|
const catalogTrackName = recoveredTrackName ?? this.#fec.catalogTrackNameForBlock(sbn, packetId);
|
|
18453
|
-
|
|
18454
|
-
this.#router.routeRecoveredSymbol(sbn, recovered, packetId, meta.esi);
|
|
18455
|
-
} else {
|
|
18456
|
-
this.#router.routeRecoveredSymbol(
|
|
18457
|
-
sbn,
|
|
18458
|
-
recovered,
|
|
18459
|
-
packetId,
|
|
18460
|
-
meta.esi,
|
|
18461
|
-
catalogTrackName
|
|
18462
|
-
);
|
|
18463
|
-
}
|
|
21409
|
+
return catalogTrackName === void 0 ? this.#router.routeRecoveredSymbol(sbn, recovered, packetId, meta.esi) : this.#router.routeRecoveredSymbol(sbn, recovered, packetId, meta.esi, catalogTrackName);
|
|
18464
21410
|
};
|
|
18465
21411
|
this.#fec.onRepairResolved = (sbn, packetId, _outcome, catalogTrackName) => {
|
|
18466
21412
|
this.clearRepairForBlock(sbn, packetId, catalogTrackName);
|
|
@@ -18535,6 +21481,10 @@ var MulticastManager = class {
|
|
|
18535
21481
|
get router() {
|
|
18536
21482
|
return this.#router;
|
|
18537
21483
|
}
|
|
21484
|
+
/** Repair FETCH wiring (wireRepairFetch): this manager can request unicast repair. */
|
|
21485
|
+
setUnicastRepair(enabled) {
|
|
21486
|
+
this.#router.setUnicastRepair(enabled);
|
|
21487
|
+
}
|
|
18538
21488
|
get transport() {
|
|
18539
21489
|
return this.#transport;
|
|
18540
21490
|
}
|
|
@@ -18945,8 +21895,35 @@ var MulticastManager = class {
|
|
|
18945
21895
|
onTakeover(cb) {
|
|
18946
21896
|
this.#transport.onTakeover(cb);
|
|
18947
21897
|
}
|
|
21898
|
+
/** Failback un-protects the join's flow (7.2.7 failover), so the fallback's MoQ packets are read. */
|
|
18948
21899
|
onFailback(cb) {
|
|
18949
|
-
this.#
|
|
21900
|
+
this.#onFailback = cb;
|
|
21901
|
+
this.#hookFailback();
|
|
21902
|
+
}
|
|
21903
|
+
/**
|
|
21904
|
+
* Install the manager's own failback listener on the media transport, which
|
|
21905
|
+
* arms its silence watchdog: a protected join un-protects on failover even
|
|
21906
|
+
* when no player listens, else its tracks' MoQ copies stay dropped.
|
|
21907
|
+
*/
|
|
21908
|
+
#hookFailback() {
|
|
21909
|
+
if (this.#failbackHooked) return;
|
|
21910
|
+
this.#failbackHooked = true;
|
|
21911
|
+
this.#transport.onFailback((reason) => {
|
|
21912
|
+
const joined = this.#provenanceJoin;
|
|
21913
|
+
if (joined !== null) this.#router.unprotectProvenanceFlow(joined.flow);
|
|
21914
|
+
this.#onFailback?.(reason);
|
|
21915
|
+
});
|
|
21916
|
+
}
|
|
21917
|
+
/**
|
|
21918
|
+
* A protected join's handoff (draft -01 7.2.7), before `onTakeover`: its
|
|
21919
|
+
* first admitted datagram. MoQ copies of the flow's tracks the router still
|
|
21920
|
+
* held unadmitted are discarded, and later ones are dropped. Fires again
|
|
21921
|
+
* after a failback once multicast resumes.
|
|
21922
|
+
*/
|
|
21923
|
+
onProtected(cb) {
|
|
21924
|
+
this.#router.onProvenanceFlowProtected(cb && ((flow) => {
|
|
21925
|
+
if (this.#provenanceJoin?.flow === flow) cb();
|
|
21926
|
+
}));
|
|
18950
21927
|
}
|
|
18951
21928
|
/** See MulticastTransport.onDataPlaneLive: the join's first accepted packet. */
|
|
18952
21929
|
onDataPlaneLive(cb) {
|
|
@@ -18962,6 +21939,12 @@ var MulticastManager = class {
|
|
|
18962
21939
|
return MulticastTransport.isAvailable();
|
|
18963
21940
|
}
|
|
18964
21941
|
async join(config) {
|
|
21942
|
+
const provenanceFlow = config.provenance?.flow;
|
|
21943
|
+
if (provenanceFlow !== void 0 && ((config.sourceAddress ?? config.senderAddress) !== provenanceFlow.source || config.groupAddress !== provenanceFlow.groupAddress || config.port !== provenanceFlow.port)) {
|
|
21944
|
+
throw new RangeError(
|
|
21945
|
+
"[MulticastManager] join: provenance flow (S, G, port) must match sourceAddress ?? senderAddress, groupAddress and port"
|
|
21946
|
+
);
|
|
21947
|
+
}
|
|
18965
21948
|
const staged = this.#renditionSwitch;
|
|
18966
21949
|
if (staged !== null) this.#endRenditionSwitch(staged, { reason: "superseded" }, false);
|
|
18967
21950
|
this.configureFec(config);
|
|
@@ -18969,16 +21952,21 @@ var MulticastManager = class {
|
|
|
18969
21952
|
const selectedPacketIds = config.packetIds?.length ? new Set(config.packetIds) : null;
|
|
18970
21953
|
const catalogTracks = config.tracks?.filter(({ packetId }) => selectedPacketIds === null || selectedPacketIds.has(packetId));
|
|
18971
21954
|
const ingressTracks = catalogTracks ? catalogTracks : selectedPacketIds === null ? void 0 : [...selectedPacketIds].map((packetId) => ({ name: "", packetId }));
|
|
18972
|
-
|
|
21955
|
+
this.#releaseProvenanceJoin();
|
|
21956
|
+
const provenance = config.provenance;
|
|
21957
|
+
const protectedTracks = ingressTracks ?? provenance?.flow.tracks;
|
|
21958
|
+
const ingress = protectedTracks === void 0 ? void 0 : {
|
|
18973
21959
|
tracks: Object.freeze(
|
|
18974
|
-
|
|
21960
|
+
protectedTracks.map(
|
|
18975
21961
|
({ name, packetId }) => Object.freeze({ name, packetId })
|
|
18976
21962
|
)
|
|
18977
|
-
)
|
|
21963
|
+
),
|
|
21964
|
+
...provenance === void 0 ? {} : { flow: multicastProvenanceFlowKey(provenance.flow) }
|
|
18978
21965
|
};
|
|
18979
21966
|
const amtRelay = config.amtRelay;
|
|
18980
21967
|
const transportConfig = {
|
|
18981
21968
|
sourceAddress: config.sourceAddress,
|
|
21969
|
+
senderAddress: config.senderAddress,
|
|
18982
21970
|
groupAddress: config.groupAddress,
|
|
18983
21971
|
port: config.port,
|
|
18984
21972
|
mode: config.mode,
|
|
@@ -19005,7 +21993,61 @@ var MulticastManager = class {
|
|
|
19005
21993
|
packetIds: config.packetIds,
|
|
19006
21994
|
tracks: catalogTracks?.map(({ name, packetId }) => ({ name, packetId }))
|
|
19007
21995
|
};
|
|
19008
|
-
|
|
21996
|
+
if (provenance !== void 0) this.#joinProvenance(provenance, ingress, transportConfig);
|
|
21997
|
+
const protectedJoin = this.#provenanceJoin;
|
|
21998
|
+
const joined = await (ingress === void 0 ? this.#transport.join(transportConfig) : this.#transport.join(transportConfig, ingress));
|
|
21999
|
+
if (!joined && protectedJoin !== null && this.#provenanceJoin === protectedJoin) this.#releaseProvenanceJoin();
|
|
22000
|
+
return joined;
|
|
22001
|
+
}
|
|
22002
|
+
/**
|
|
22003
|
+
* Register the protected flow and subscribe its `manifestPort` on the same
|
|
22004
|
+
* `(S, G)` with its own listener (draft -01 7.2.7), so records reach the
|
|
22005
|
+
* flow's receiver and media datagrams are held until one admits them. A
|
|
22006
|
+
* manifest subscription that fails to join admits nothing: the media is
|
|
22007
|
+
* held and expires, never read unauthenticated.
|
|
22008
|
+
*/
|
|
22009
|
+
#joinProvenance(provenance, ingress, media) {
|
|
22010
|
+
const { flow, auth } = provenance;
|
|
22011
|
+
const receiver = new BcProvenanceV2Receiver({
|
|
22012
|
+
...provenance.receiver,
|
|
22013
|
+
// The caller's ctx when it passes one, else the catalog's: an explicit
|
|
22014
|
+
// `catalogCtx: undefined` must not switch off the 7.2.4 ctx check.
|
|
22015
|
+
catalogCtx: provenance.receiver?.catalogCtx ?? flow.ctx,
|
|
22016
|
+
endpoint: {
|
|
22017
|
+
sourceAddress: flow.source,
|
|
22018
|
+
groupAddress: flow.groupAddress,
|
|
22019
|
+
port: flow.port,
|
|
22020
|
+
manifestPort: flow.manifestPort,
|
|
22021
|
+
streamId: flow.streamId
|
|
22022
|
+
},
|
|
22023
|
+
keys: auth.keys,
|
|
22024
|
+
minEpoch: auth.minEpoch,
|
|
22025
|
+
bandwidth: flow.bandwidth,
|
|
22026
|
+
maxAuthDelayMs: flow.maxAuthDelayMs,
|
|
22027
|
+
manifestTimerMs: flow.manifestTimerMs
|
|
22028
|
+
});
|
|
22029
|
+
this.#router.registerProvenanceFlow(ingress, receiver);
|
|
22030
|
+
this.#hookFailback();
|
|
22031
|
+
const manifest = new MulticastTransport((record) => {
|
|
22032
|
+
if (this.#provenanceJoin?.manifest === manifest) {
|
|
22033
|
+
this.#router.feedProvenanceRecord(ingress.flow, record, "multicast");
|
|
22034
|
+
}
|
|
22035
|
+
});
|
|
22036
|
+
this.#provenanceJoin = { flow: ingress.flow, manifest };
|
|
22037
|
+
const { packetIds: _packetIds, tracks: _tracks, ...path } = media;
|
|
22038
|
+
void manifest.join({ ...path, port: flow.manifestPort }).then((joined) => {
|
|
22039
|
+
if (!joined && this.#provenanceJoin?.manifest === manifest) {
|
|
22040
|
+
console.warn(`[MulticastManager] manifestPort ${flow.groupAddress}:${flow.manifestPort} not joined`, manifest.lastJoinOutcome);
|
|
22041
|
+
}
|
|
22042
|
+
});
|
|
22043
|
+
}
|
|
22044
|
+
/** Un-protect: drop the flow's hold queues (no expiry counted) and leave its manifestPort. */
|
|
22045
|
+
#releaseProvenanceJoin() {
|
|
22046
|
+
const joined = this.#provenanceJoin;
|
|
22047
|
+
if (joined === null) return;
|
|
22048
|
+
this.#provenanceJoin = null;
|
|
22049
|
+
joined.manifest.dispose();
|
|
22050
|
+
this.#router.releaseProvenanceFlow(joined.flow);
|
|
19009
22051
|
}
|
|
19010
22052
|
/**
|
|
19011
22053
|
* Join the selected rung's dedicated repair groups beside the media join
|
|
@@ -19035,10 +22077,11 @@ var MulticastManager = class {
|
|
|
19035
22077
|
this.#applyRepairOwners(repairOwners);
|
|
19036
22078
|
for (const endpoint of endpoints) {
|
|
19037
22079
|
const ingress = {
|
|
19038
|
-
tracks: Object.freeze(endpoint.tracks.map(({ name, packetId }) => Object.freeze({ name, packetId })))
|
|
22080
|
+
tracks: Object.freeze(endpoint.tracks.map(({ name, packetId }) => Object.freeze({ name, packetId }))),
|
|
22081
|
+
...endpoint.flow === void 0 ? {} : { flow: endpoint.flow }
|
|
19039
22082
|
};
|
|
19040
22083
|
const onPacket = (packet) => {
|
|
19041
|
-
if (!released) this.#router.feedPacket(packet, "multicast",
|
|
22084
|
+
if (!released) this.#router.feedPacket(packet, "multicast", ingress);
|
|
19042
22085
|
};
|
|
19043
22086
|
const fail = (error) => console.warn(`[MulticastManager] repair group ${endpoint.group}:${endpoint.port} not joined`, error);
|
|
19044
22087
|
if (typeof provider === "function") {
|
|
@@ -19176,7 +22219,7 @@ var MulticastManager = class {
|
|
|
19176
22219
|
for (const track of [...live.tracks, ...request.tracks]) {
|
|
19177
22220
|
stagedTracks.set(`${track.packetId}\0${track.name}`, track);
|
|
19178
22221
|
}
|
|
19179
|
-
const stagedIngress = freezeIngress(stagedTracks.values());
|
|
22222
|
+
const stagedIngress = freezeIngress(stagedTracks.values(), live.flow);
|
|
19180
22223
|
const stagedPacketIds = committedPacketIds === void 0 || livePacketIds === null ? null : /* @__PURE__ */ new Set([...livePacketIds, ...committedPacketIds]);
|
|
19181
22224
|
const widened = stagedPacketIds !== null && !sameIdSet(stagedPacketIds, livePacketIds);
|
|
19182
22225
|
const active = {
|
|
@@ -19187,7 +22230,7 @@ var MulticastManager = class {
|
|
|
19187
22230
|
liveIngress: live,
|
|
19188
22231
|
livePacketIds,
|
|
19189
22232
|
widened,
|
|
19190
|
-
committedIngress: freezeIngress(request.tracks),
|
|
22233
|
+
committedIngress: freezeIngress(request.tracks, live.flow),
|
|
19191
22234
|
committedPacketIds,
|
|
19192
22235
|
router: null,
|
|
19193
22236
|
state: "staged"
|
|
@@ -19200,7 +22243,7 @@ var MulticastManager = class {
|
|
|
19200
22243
|
let widening;
|
|
19201
22244
|
try {
|
|
19202
22245
|
if (widened) widening = this.#transport.setPacketIds([...stagedPacketIds]);
|
|
19203
|
-
this.#
|
|
22246
|
+
this.#setSwitchIngress(stagedIngress);
|
|
19204
22247
|
} catch (error) {
|
|
19205
22248
|
active.router.cancel();
|
|
19206
22249
|
throw error;
|
|
@@ -19221,6 +22264,11 @@ var MulticastManager = class {
|
|
|
19221
22264
|
}
|
|
19222
22265
|
};
|
|
19223
22266
|
}
|
|
22267
|
+
/** Route a switch's ingress and keep a protected flow's registration in step (7.2.7). */
|
|
22268
|
+
#setSwitchIngress(ingress) {
|
|
22269
|
+
this.#transport.setContext(ingress);
|
|
22270
|
+
if (ingress.flow !== void 0) this.#router.updateProvenanceFlowIngress({ ...ingress, flow: ingress.flow });
|
|
22271
|
+
}
|
|
19224
22272
|
/** The router committed the staged switch (it already retired `from`). */
|
|
19225
22273
|
#commitRenditionSwitch(active, commit) {
|
|
19226
22274
|
if (this.#renditionSwitch !== active) return;
|
|
@@ -19228,7 +22276,7 @@ var MulticastManager = class {
|
|
|
19228
22276
|
active.state = "committed";
|
|
19229
22277
|
active.router = null;
|
|
19230
22278
|
if (this.#transport.joined) {
|
|
19231
|
-
this.#
|
|
22279
|
+
this.#setSwitchIngress(active.committedIngress);
|
|
19232
22280
|
if (active.committedPacketIds !== void 0) {
|
|
19233
22281
|
this.#transport.setPacketIds(active.committedPacketIds).catch((error) => {
|
|
19234
22282
|
console.warn(
|
|
@@ -19264,7 +22312,7 @@ var MulticastManager = class {
|
|
|
19264
22312
|
active.router = null;
|
|
19265
22313
|
if (restore) {
|
|
19266
22314
|
if (this.#transport.joined) {
|
|
19267
|
-
this.#
|
|
22315
|
+
this.#setSwitchIngress(active.liveIngress);
|
|
19268
22316
|
if (active.widened && active.livePacketIds !== null) {
|
|
19269
22317
|
this.#transport.setPacketIds([...active.livePacketIds]).catch((error) => {
|
|
19270
22318
|
console.warn(
|
|
@@ -19336,11 +22384,11 @@ var MulticastManager = class {
|
|
|
19336
22384
|
}
|
|
19337
22385
|
/** Route a Worker-recovered FEC block through the MMTP parser + dedup pipeline. */
|
|
19338
22386
|
routeRecoveredBlock(blockId, data, packetId, catalogTrackName) {
|
|
19339
|
-
this.#router.routeRecoveredBlock(blockId, data, packetId, catalogTrackName);
|
|
22387
|
+
return this.#router.routeRecoveredBlock(blockId, data, packetId, catalogTrackName);
|
|
19340
22388
|
}
|
|
19341
22389
|
/** Route one Worker-recovered source symbol through the MMTP parser + dedup pipeline. */
|
|
19342
22390
|
routeRecoveredSymbol(blockId, data, packetId, esi = 0, catalogTrackName) {
|
|
19343
|
-
this.#router.routeRecoveredSymbol(blockId, data, packetId, esi, catalogTrackName);
|
|
22391
|
+
return this.#router.routeRecoveredSymbol(blockId, data, packetId, esi, catalogTrackName);
|
|
19344
22392
|
}
|
|
19345
22393
|
// ---------------------------------------------------------------------------
|
|
19346
22394
|
// Phase 14: multi-track catalog ingest (D-04, D-06, D-07, D-17, D-26, D-28)
|
|
@@ -19650,6 +22698,7 @@ var MulticastManager = class {
|
|
|
19650
22698
|
if (reason === "rtt-unmeasured") stats.repairsSkippedRttUnmeasured++;
|
|
19651
22699
|
else if (reason === "rtt-exceeds-budget") stats.repairsSkippedRttExceedsBudget++;
|
|
19652
22700
|
else if (reason === "range-no-anchor") stats.repairsSkippedRangeNoAnchor++;
|
|
22701
|
+
else if (reason === "group-cannot-complete") stats.repairsSkippedGroupCannotComplete++;
|
|
19653
22702
|
else stats.repairsSkippedPresentationDelayUnavailable++;
|
|
19654
22703
|
stats.lastRepairSkip = {
|
|
19655
22704
|
reason,
|
|
@@ -19845,16 +22894,33 @@ var MulticastManager = class {
|
|
|
19845
22894
|
this.#trackResetObservers.clear();
|
|
19846
22895
|
this.#repairLifecycleObservers.clear();
|
|
19847
22896
|
this.#transport.dispose();
|
|
22897
|
+
this.#releaseProvenanceJoin();
|
|
19848
22898
|
for (const release of [...this.#repairReleases.keys()]) release();
|
|
19849
22899
|
this.#router.dispose();
|
|
19850
22900
|
this.#fec.dispose();
|
|
19851
22901
|
}
|
|
19852
22902
|
};
|
|
22903
|
+
function multicastProvenanceFlowKey(flow) {
|
|
22904
|
+
return JSON.stringify([flow.source, flow.groupAddress, flow.port]);
|
|
22905
|
+
}
|
|
19853
22906
|
export {
|
|
19854
22907
|
MulticastManager,
|
|
19855
22908
|
REPAIR_LIFECYCLE_EVENT_TYPE,
|
|
19856
22909
|
REPAIR_LIFECYCLE_LOG_PREFIX,
|
|
19857
22910
|
forwardRepairLifecycleEvents,
|
|
19858
|
-
isRepairLifecycleEvent
|
|
22911
|
+
isRepairLifecycleEvent,
|
|
22912
|
+
multicastProvenanceFlowKey
|
|
19859
22913
|
};
|
|
22914
|
+
/*! Bundled license information:
|
|
22915
|
+
|
|
22916
|
+
@noble/hashes/esm/utils.js:
|
|
22917
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
22918
|
+
|
|
22919
|
+
@noble/curves/esm/utils.js:
|
|
22920
|
+
@noble/curves/esm/abstract/modular.js:
|
|
22921
|
+
@noble/curves/esm/abstract/curve.js:
|
|
22922
|
+
@noble/curves/esm/abstract/edwards.js:
|
|
22923
|
+
@noble/curves/esm/ed25519.js:
|
|
22924
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
22925
|
+
*/
|
|
19860
22926
|
//# sourceMappingURL=multicast-manager.js.map
|