@blockcast/mmt-container 0.4.1 → 0.5.0-main.6f3380bce0c1
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 +3407 -376
- 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 +13 -16
- package/dist/fec-manager.d.ts.map +1 -1
- package/dist/fec-manager.js +137 -76
- 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 +64 -28
- package/dist/mmtp-router.d.ts.map +1 -1
- package/dist/mmtp-router.js +286 -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 +3349 -354
- 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 +74 -127
- 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 +341 -272
- package/src/multicast-auth-catalog.ts +81 -8
- package/src/multicast-manager.ts +189 -48
- package/src/multicast-transport.ts +6 -2
- package/src/no-modules.ts +1 -6
- package/src/repair-fetch.ts +24 -4
- package/dist/manifest-auth-plan.d.ts +0 -31
- package/dist/manifest-auth-plan.d.ts.map +0 -1
- package/dist/manifest-auth-plan.js +0 -426
- package/dist/manifest-auth-plan.js.map +0 -7
- package/dist/source-auth.d.ts +0 -2
- package/dist/source-auth.d.ts.map +0 -1
- package/dist/source-auth.js +0 -3912
- package/dist/source-auth.js.map +0 -7
- package/src/manifest-auth-plan.ts +0 -55
- package/src/source-auth.ts +0 -1
- package/test-vectors/generate-source-auth-fixture.mjs +0 -114
- package/test-vectors/source-auth-v1.json +0 -61
package/dist/browser-runtime.js
CHANGED
|
@@ -5066,18 +5066,18 @@ function hevcNalUnitsContainIrap(nals) {
|
|
|
5066
5066
|
return sawNal ? false : void 0;
|
|
5067
5067
|
}
|
|
5068
5068
|
function classifyHevcKeyframe(data) {
|
|
5069
|
-
const
|
|
5070
|
-
return
|
|
5069
|
+
const split2 = splitHevcSample(data);
|
|
5070
|
+
return split2 === null ? void 0 : hevcNalUnitsContainIrap(split2.nals);
|
|
5071
5071
|
}
|
|
5072
5072
|
function detectHevcKeyframe(data) {
|
|
5073
5073
|
return classifyHevcKeyframe(data) === true;
|
|
5074
5074
|
}
|
|
5075
5075
|
function prepareHevcCmafSample(data, sampleEntry) {
|
|
5076
|
-
const
|
|
5077
|
-
if (
|
|
5078
|
-
const keyframe = hevcNalUnitsContainIrap(
|
|
5076
|
+
const split2 = splitHevcSample(data);
|
|
5077
|
+
if (split2 === null) return null;
|
|
5078
|
+
const keyframe = hevcNalUnitsContainIrap(split2.nals);
|
|
5079
5079
|
if (keyframe === void 0) return null;
|
|
5080
|
-
const normalized = hevcNalUnitsToLengthPrefixed(data,
|
|
5080
|
+
const normalized = hevcNalUnitsToLengthPrefixed(data, split2.nals, split2.lengthPrefixed, sampleEntry);
|
|
5081
5081
|
return normalized === null ? null : { keyframe, data: normalized };
|
|
5082
5082
|
}
|
|
5083
5083
|
function normalizeHevcSampleToLengthPrefixed(data, sampleEntry) {
|
|
@@ -7566,14 +7566,22 @@ function toWindowMulticastSubscribeConfig(input) {
|
|
|
7566
7566
|
if (input.packetIds?.some((id) => !isMulticastTrackPacketId(id)) || input.tracks?.some((track) => !isMulticastTrackPacketId(track.packetId))) {
|
|
7567
7567
|
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`);
|
|
7568
7568
|
}
|
|
7569
|
+
const source = input.source?.trim() || void 0;
|
|
7570
|
+
const senderAddress = input.senderAddress?.trim() || void 0;
|
|
7571
|
+
if (source && senderAddress) {
|
|
7572
|
+
throw new TypeError("multicast senderAddress is an ASM filter and never appears beside source");
|
|
7573
|
+
}
|
|
7569
7574
|
const base = {
|
|
7570
7575
|
group: input.group,
|
|
7571
7576
|
port: input.port,
|
|
7572
|
-
...
|
|
7577
|
+
...source ? { source } : {},
|
|
7573
7578
|
consume: "bytes",
|
|
7574
7579
|
...input.packetIds?.length ? { packetIds: [...input.packetIds] } : {}
|
|
7575
7580
|
};
|
|
7581
|
+
const filter = senderAddress ? { senderAddress } : {};
|
|
7576
7582
|
if (input.atsc3) {
|
|
7583
|
+
if (senderAddress)
|
|
7584
|
+
throw new TypeError("an ATSC3 tuner request carries no senderAddress");
|
|
7577
7585
|
const { frequency, plpId, serviceId } = input.atsc3;
|
|
7578
7586
|
if (!Number.isInteger(frequency) || frequency < 1) {
|
|
7579
7587
|
throw new TypeError(`ATSC3 tuning requires a positive integer frequency in kHz, got ${String(frequency)}`);
|
|
@@ -7593,6 +7601,7 @@ function toWindowMulticastSubscribeConfig(input) {
|
|
|
7593
7601
|
if (!wantsAmt) {
|
|
7594
7602
|
const native = {
|
|
7595
7603
|
...base,
|
|
7604
|
+
...filter,
|
|
7596
7605
|
origin: "ip-multicast",
|
|
7597
7606
|
...input.tracks?.length ? { tracks: input.tracks.map(({ name, packetId }) => ({ name, packetId })) } : {}
|
|
7598
7607
|
};
|
|
@@ -7618,6 +7627,7 @@ function toWindowMulticastSubscribeConfig(input) {
|
|
|
7618
7627
|
}
|
|
7619
7628
|
return {
|
|
7620
7629
|
...base,
|
|
7630
|
+
...filter,
|
|
7621
7631
|
origin: "amt",
|
|
7622
7632
|
relay,
|
|
7623
7633
|
relayPort
|
|
@@ -7643,6 +7653,9 @@ function isValidMulticastGroupAddress(address) {
|
|
|
7643
7653
|
function normalizeMulticastEndpoint(endpoint) {
|
|
7644
7654
|
const groupAddress = endpoint.groupAddress.trim().toLowerCase();
|
|
7645
7655
|
const sourceAddress = endpoint.sourceAddress?.trim().toLowerCase() || void 0;
|
|
7656
|
+
const senderAddress = endpoint.senderAddress?.trim().toLowerCase() || void 0;
|
|
7657
|
+
if (sourceAddress && senderAddress)
|
|
7658
|
+
return void 0;
|
|
7646
7659
|
const amtRelay = endpoint.amtRelay?.trim().toLowerCase() || void 0;
|
|
7647
7660
|
if (!isValidMulticastGroupAddress(groupAddress) || !isPositivePort(endpoint.port))
|
|
7648
7661
|
return void 0;
|
|
@@ -7653,6 +7666,7 @@ function normalizeMulticastEndpoint(endpoint) {
|
|
|
7653
7666
|
return {
|
|
7654
7667
|
groupAddress,
|
|
7655
7668
|
...sourceAddress ? { sourceAddress } : {},
|
|
7669
|
+
...senderAddress ? { senderAddress } : {},
|
|
7656
7670
|
port: endpoint.port,
|
|
7657
7671
|
...endpoint.mode ? { mode: endpoint.mode } : {},
|
|
7658
7672
|
...amtRelay ? { amtRelay } : {},
|
|
@@ -7672,7 +7686,9 @@ function multicastEndpointKey(endpoint) {
|
|
|
7672
7686
|
normalized.amtRelay ?? "",
|
|
7673
7687
|
normalized.amtRelayPort ?? "",
|
|
7674
7688
|
// A DRIAD join is a different gateway subscription from a native one.
|
|
7675
|
-
...normalized.amtDiscovery ? [normalized.amtDiscovery] : []
|
|
7689
|
+
...normalized.amtDiscovery ? [normalized.amtDiscovery] : [],
|
|
7690
|
+
// An ASM sender filter is a different subscription from the unfiltered join.
|
|
7691
|
+
...normalized.senderAddress ? [{ senderAddress: normalized.senderAddress }] : []
|
|
7676
7692
|
]);
|
|
7677
7693
|
}
|
|
7678
7694
|
function isMulticastTrackPacketId(value) {
|
|
@@ -8930,14 +8946,14 @@ function validateNetworkSourceEntry(s, i) {
|
|
|
8930
8946
|
}
|
|
8931
8947
|
return;
|
|
8932
8948
|
}
|
|
8933
|
-
const
|
|
8934
|
-
if (!
|
|
8949
|
+
const inRange2 = (v, min, max) => Number.isInteger(v) && v >= min && v <= max;
|
|
8950
|
+
if (!inRange2(s.frequency, 1, Number.MAX_SAFE_INTEGER)) {
|
|
8935
8951
|
throw err("invalid_network_source", `frequency must be a positive integer in kHz; got ${s.frequency}`);
|
|
8936
8952
|
}
|
|
8937
|
-
if (s.plpId !== void 0 && !
|
|
8953
|
+
if (s.plpId !== void 0 && !inRange2(s.plpId, 0, 63)) {
|
|
8938
8954
|
throw err("invalid_network_source", `plpId must be 0..63; got ${s.plpId}`);
|
|
8939
8955
|
}
|
|
8940
|
-
if (s.serviceId !== void 0 && !
|
|
8956
|
+
if (s.serviceId !== void 0 && !inRange2(s.serviceId, 0, 65535)) {
|
|
8941
8957
|
throw err("invalid_network_source", `serviceId must be 0..65535; got ${s.serviceId}`);
|
|
8942
8958
|
}
|
|
8943
8959
|
if (s.slsUri !== void 0 && (typeof s.slsUri !== "string" || !isAbsoluteUri(s.slsUri))) {
|
|
@@ -10647,9 +10663,9 @@ var CmafBlockAssembler = class {
|
|
|
10647
10663
|
const sample = splitH264SampleNalUnits(data);
|
|
10648
10664
|
const nalTypes = describeH264NalTypes(sample.nals);
|
|
10649
10665
|
const classify = classifyH264Keyframe(data);
|
|
10650
|
-
const
|
|
10666
|
+
const hex3 = Array.from(data.subarray(0, Math.min(48, data.length))).map((b) => b.toString(16).padStart(2, "0")).join(" ");
|
|
10651
10667
|
console.log(
|
|
10652
|
-
`[L4-KF-GATE] reject#${this.#keyframeRejectDumpCount} len=${data.length} metadataKf=${metadataKeyframe} classify=${classify} format=${sample.format} nalTypes=[${nalTypes.join(",")}] first48=[${
|
|
10668
|
+
`[L4-KF-GATE] reject#${this.#keyframeRejectDumpCount} len=${data.length} metadataKf=${metadataKeyframe} classify=${classify} format=${sample.format} nalTypes=[${nalTypes.join(",")}] first48=[${hex3}]`
|
|
10653
10669
|
);
|
|
10654
10670
|
}
|
|
10655
10671
|
/** Mark video as already synchronized by an external decoder path.
|
|
@@ -13257,6 +13273,79 @@ function decideGap(input) {
|
|
|
13257
13273
|
});
|
|
13258
13274
|
}
|
|
13259
13275
|
|
|
13276
|
+
// src/held-layer-admission.ts
|
|
13277
|
+
var HORIZON_CAP = 1073741824;
|
|
13278
|
+
function heldLayerAnchorHorizon(retentionBlocks, k) {
|
|
13279
|
+
return retentionBlocks === void 0 || k <= 0 ? HORIZON_CAP : Math.min(HORIZON_CAP, retentionBlocks * k);
|
|
13280
|
+
}
|
|
13281
|
+
var HeldLayerAdmission = class {
|
|
13282
|
+
#layerNames;
|
|
13283
|
+
#states;
|
|
13284
|
+
constructor(layers, heldLayers = [0]) {
|
|
13285
|
+
const held = new Set(heldLayers);
|
|
13286
|
+
this.#layerNames = new Set(layers.map((l) => l.repairTrackName));
|
|
13287
|
+
this.#states = layers.filter((l) => held.has(l.layer)).map((l) => ({ layer: l.layer, repairTrackName: l.repairTrackName, total: l.rsIdOffset + l.repairSymbols }));
|
|
13288
|
+
}
|
|
13289
|
+
/**
|
|
13290
|
+
* Judges one repair packet, after the 4.5 discards and the size check and
|
|
13291
|
+
* before it places a block. `repairTrackName` is the subscribed repair
|
|
13292
|
+
* track, never inferred from RS_ID; without it only (b) runs. A packet on a
|
|
13293
|
+
* repair track the catalog does not list is `'discard'` and anchors nothing.
|
|
13294
|
+
* `sourceFrontier` is the newest source SS_ID, if any: a repair more than
|
|
13295
|
+
* `horizon` past it is judged without moving the anchored state.
|
|
13296
|
+
*
|
|
13297
|
+
* @returns undefined to admit, `'discard'`, or the fail-closed reason.
|
|
13298
|
+
*/
|
|
13299
|
+
admit(repairTrackName, ssStart, rsbLength, horizon, sourceFrontier) {
|
|
13300
|
+
if (repairTrackName !== void 0 && !this.#layerNames.has(repairTrackName)) return "discard";
|
|
13301
|
+
const farAhead = sourceFrontier !== void 0 && serial32Precedes(sourceFrontier, ssStart) && serial32Distance(sourceFrontier, ssStart) > horizon;
|
|
13302
|
+
for (const state of this.#states) {
|
|
13303
|
+
let code;
|
|
13304
|
+
if (farAhead) {
|
|
13305
|
+
if (rsbLength < state.total) {
|
|
13306
|
+
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
13307
|
+
else if (state.anchor !== void 0 || state.settled) code = "below_anchored_total";
|
|
13308
|
+
}
|
|
13309
|
+
} else {
|
|
13310
|
+
if (state.newest === void 0 || serial32Precedes(state.newest, ssStart)) state.newest = ssStart;
|
|
13311
|
+
const stale = (s) => serial32Distance(s, state.newest) > horizon;
|
|
13312
|
+
if (state.latestBelow !== void 0 && stale(state.latestBelow)) {
|
|
13313
|
+
delete state.latestBelow;
|
|
13314
|
+
delete state.latestBelowRsb;
|
|
13315
|
+
}
|
|
13316
|
+
if (state.anchor !== void 0 && stale(state.anchor)) state.settled = true;
|
|
13317
|
+
if (rsbLength < state.total) {
|
|
13318
|
+
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
13319
|
+
else if (stale(ssStart)) continue;
|
|
13320
|
+
else if (state.settled || state.anchor !== void 0 && !serial32Precedes(ssStart, state.anchor)) code = "below_anchored_total";
|
|
13321
|
+
else if (state.latestBelow === void 0 || serial32Precedes(state.latestBelow, ssStart)) {
|
|
13322
|
+
state.latestBelow = ssStart;
|
|
13323
|
+
state.latestBelowRsb = rsbLength;
|
|
13324
|
+
}
|
|
13325
|
+
} else if (!state.settled && !stale(ssStart)) {
|
|
13326
|
+
if (state.anchor === void 0 || serial32Precedes(ssStart, state.anchor)) state.anchor = ssStart;
|
|
13327
|
+
if (state.latestBelow !== void 0 && !serial32Precedes(state.latestBelow, state.anchor)) {
|
|
13328
|
+
code = "below_anchored_total";
|
|
13329
|
+
ssStart = state.latestBelow;
|
|
13330
|
+
rsbLength = state.latestBelowRsb;
|
|
13331
|
+
}
|
|
13332
|
+
}
|
|
13333
|
+
}
|
|
13334
|
+
if (code !== void 0) {
|
|
13335
|
+
return {
|
|
13336
|
+
code,
|
|
13337
|
+
layer: state.layer,
|
|
13338
|
+
total: state.total,
|
|
13339
|
+
rsbLength,
|
|
13340
|
+
ssStart,
|
|
13341
|
+
...state.anchor === void 0 ? {} : { anchor: state.anchor }
|
|
13342
|
+
};
|
|
13343
|
+
}
|
|
13344
|
+
}
|
|
13345
|
+
return void 0;
|
|
13346
|
+
}
|
|
13347
|
+
};
|
|
13348
|
+
|
|
13260
13349
|
// src/fec-manager.ts
|
|
13261
13350
|
var RS_MAX_BLOCK_SYMBOLS = 255;
|
|
13262
13351
|
var WORKER_ERROR_LOG_CAP = 5;
|
|
@@ -13460,6 +13549,9 @@ var FECManager = class _FECManager {
|
|
|
13460
13549
|
// Worker recovery emits all K source symbols. Track recovery once per block
|
|
13461
13550
|
// while routing only ESI slots that were absent from the direct source path.
|
|
13462
13551
|
this.#workerRecoveredBlocks = /* @__PURE__ */ new Set();
|
|
13552
|
+
// Worker-recovered blocks a recovered packet of which admission refused,
|
|
13553
|
+
// until the decode's final symbol (7.2.7: the block is unrecovered).
|
|
13554
|
+
this.#refusedRecoveries = /* @__PURE__ */ new Set();
|
|
13463
13555
|
// How the multicast leg delivers symbols (see MulticastSymbolDelivery). The
|
|
13464
13556
|
// provider declares it per transport join; absence keeps datagram semantics.
|
|
13465
13557
|
this.#multicastSymbolDelivery = "datagram";
|
|
@@ -13598,6 +13690,11 @@ var FECManager = class _FECManager {
|
|
|
13598
13690
|
/** A stream's SBN went backwards (or jumped implausibly): the source restarted. */
|
|
13599
13691
|
this.onSourceDiscontinuity = null;
|
|
13600
13692
|
// Worker-path callbacks — async recovery and stats from FecWorkerClient.
|
|
13693
|
+
/**
|
|
13694
|
+
* A recovered source symbol. Return false when admission refused the
|
|
13695
|
+
* packet it carries (bc-provenance v2, draft-ramadan-moq-multicast-01
|
|
13696
|
+
* 7.2.7): the block is then unrecovered and stays open to its FEC deadline.
|
|
13697
|
+
*/
|
|
13601
13698
|
this.onBlockRecovered = null;
|
|
13602
13699
|
this.onFecStatsUpdate = null;
|
|
13603
13700
|
}
|
|
@@ -13662,6 +13759,7 @@ var FECManager = class _FECManager {
|
|
|
13662
13759
|
#repairDeadlines;
|
|
13663
13760
|
#repairDeadlineAtMs;
|
|
13664
13761
|
#workerRecoveredBlocks;
|
|
13762
|
+
#refusedRecoveries;
|
|
13665
13763
|
#multicastSymbolDelivery;
|
|
13666
13764
|
#orderedFrontiers;
|
|
13667
13765
|
#orderedPendingBlocks;
|
|
@@ -13771,12 +13869,12 @@ var FECManager = class _FECManager {
|
|
|
13771
13869
|
* still be held behind it. Retirement is swept lazily here, on the stream's
|
|
13772
13870
|
* own terminal events, so it needs no timer.
|
|
13773
13871
|
*/
|
|
13774
|
-
#markTerminal(key, sbn, packetId,
|
|
13872
|
+
#markTerminal(key, sbn, packetId, clean2) {
|
|
13775
13873
|
const nowMs = performance.now();
|
|
13776
13874
|
this.#retireTerminalBlocks(packetId, nowMs);
|
|
13777
13875
|
this.#forgetOrderedPending(key);
|
|
13778
13876
|
this.#completedBlocks.add(key);
|
|
13779
|
-
if (
|
|
13877
|
+
if (clean2) this.#cleanlyCompletedBlocks.add(key);
|
|
13780
13878
|
this.#indexBlockKey(key, sbn, packetId);
|
|
13781
13879
|
const config = this.#trackConfig(packetId);
|
|
13782
13880
|
const expiresAtMs = nowMs + config.interleaveDepthMs + 2 * this.#blockLifetimeMs(config);
|
|
@@ -13801,6 +13899,7 @@ var FECManager = class _FECManager {
|
|
|
13801
13899
|
this.#completedBlocks.delete(record.key);
|
|
13802
13900
|
this.#cleanlyCompletedBlocks.delete(record.key);
|
|
13803
13901
|
this.#workerRecoveredBlocks.delete(record.key);
|
|
13902
|
+
this.#refusedRecoveries.delete(record.key);
|
|
13804
13903
|
this.#failedBlocks.delete(record.key);
|
|
13805
13904
|
this.#resolvedBlockClasses.delete(record.key);
|
|
13806
13905
|
this.#repairedBlocks.delete(record.key);
|
|
@@ -14952,6 +15051,7 @@ var FECManager = class _FECManager {
|
|
|
14952
15051
|
resetSet(this.#cleanlyCompletedBlocks);
|
|
14953
15052
|
resetSet(this.#repairRequestedBlocks);
|
|
14954
15053
|
resetSet(this.#workerRecoveredBlocks);
|
|
15054
|
+
resetSet(this.#refusedRecoveries);
|
|
14955
15055
|
resetSet(this.#startupAlignedPackets);
|
|
14956
15056
|
resetSet(this.#startupPartialBlocks);
|
|
14957
15057
|
resetSet(this.#startupRepairReadyPackets);
|
|
@@ -15078,6 +15178,7 @@ var FECManager = class _FECManager {
|
|
|
15078
15178
|
dropSet(this.#cleanlyCompletedBlocks);
|
|
15079
15179
|
dropSet(this.#repairRequestedBlocks);
|
|
15080
15180
|
dropSet(this.#workerRecoveredBlocks);
|
|
15181
|
+
dropSet(this.#refusedRecoveries);
|
|
15081
15182
|
dropSet(this.#startupAlignedPackets);
|
|
15082
15183
|
dropSet(this.#startupPartialBlocks);
|
|
15083
15184
|
dropSet(this.#startupRepairReadyPackets);
|
|
@@ -15352,6 +15453,18 @@ var FECManager = class _FECManager {
|
|
|
15352
15453
|
blockAnchorAtMs(sbn, packetId, catalogTrackName) {
|
|
15353
15454
|
return this.#blockAnchorsMs.get(this.#blockKey(sbn, packetId, catalogTrackName));
|
|
15354
15455
|
}
|
|
15456
|
+
/**
|
|
15457
|
+
* How long ago (ms) the oldest open block of the stream, one with an FEC
|
|
15458
|
+
* deadline, took its first accepted symbol; undefined with none open.
|
|
15459
|
+
*/
|
|
15460
|
+
oldestOpenBlockAgeMs(packetId, catalogTrackName) {
|
|
15461
|
+
let oldest = Infinity;
|
|
15462
|
+
for (const key of this.#activeBlocks.keys()) {
|
|
15463
|
+
if (!this.#belongsToStream(key, packetId, catalogTrackName)) continue;
|
|
15464
|
+
oldest = Math.min(oldest, this.#blockAnchorsMs.get(key) ?? Infinity);
|
|
15465
|
+
}
|
|
15466
|
+
return oldest === Infinity ? void 0 : performance.now() - oldest;
|
|
15467
|
+
}
|
|
15355
15468
|
/** Absolute fail deadline of a still-open block, or undefined. */
|
|
15356
15469
|
blockDeadlineAtMs(sbn, packetId, catalogTrackName) {
|
|
15357
15470
|
return this.#activeBlockDeadlinesMs.get(this.#blockKey(sbn, packetId, catalogTrackName));
|
|
@@ -15852,7 +15965,25 @@ var FECManager = class _FECManager {
|
|
|
15852
15965
|
const blockKey = this.#blockKey(meta.sbn, packetId, recoveredCatalogTrackName);
|
|
15853
15966
|
const cleanlyCompleted = this.#cleanlyCompletedBlocks.has(blockKey);
|
|
15854
15967
|
if (!cleanlyCompleted) {
|
|
15855
|
-
|
|
15968
|
+
const symbolKey = `${blockKey}:${esi}`;
|
|
15969
|
+
if (!this.#seenSymbols.has(symbolKey)) {
|
|
15970
|
+
this.#seenSymbols.add(symbolKey);
|
|
15971
|
+
const admitted = this.onBlockRecovered?.(
|
|
15972
|
+
meta.sbn,
|
|
15973
|
+
packetId,
|
|
15974
|
+
data,
|
|
15975
|
+
meta,
|
|
15976
|
+
recoveredCatalogTrackName
|
|
15977
|
+
);
|
|
15978
|
+
if (admitted === false) {
|
|
15979
|
+
this.#refusedRecoveries.add(blockKey);
|
|
15980
|
+
if (this.#refusedRecoveries.size > 1e3) dropOldest(this.#refusedRecoveries, 500);
|
|
15981
|
+
}
|
|
15982
|
+
}
|
|
15983
|
+
}
|
|
15984
|
+
const refused = esi >= meta.ssbLength - 1 && this.#refusedRecoveries.delete(blockKey);
|
|
15985
|
+
if (esi >= meta.ssbLength - 1 && !refused && !this.#completedBlocks.has(blockKey)) {
|
|
15986
|
+
if (!cleanlyCompleted && !this.#workerRecoveredBlocks.has(blockKey)) {
|
|
15856
15987
|
this.#traceLifecycle(
|
|
15857
15988
|
"recovered",
|
|
15858
15989
|
meta.sbn,
|
|
@@ -15867,19 +15998,6 @@ var FECManager = class _FECManager {
|
|
|
15867
15998
|
}
|
|
15868
15999
|
this.#noteBlockRepaired(blockKey, meta.sbn, packetId, recoveredCatalogTrackName);
|
|
15869
16000
|
}
|
|
15870
|
-
const symbolKey = `${blockKey}:${esi}`;
|
|
15871
|
-
if (!this.#seenSymbols.has(symbolKey)) {
|
|
15872
|
-
this.#seenSymbols.add(symbolKey);
|
|
15873
|
-
this.onBlockRecovered?.(
|
|
15874
|
-
meta.sbn,
|
|
15875
|
-
packetId,
|
|
15876
|
-
data,
|
|
15877
|
-
meta,
|
|
15878
|
-
recoveredCatalogTrackName
|
|
15879
|
-
);
|
|
15880
|
-
}
|
|
15881
|
-
}
|
|
15882
|
-
if (esi >= meta.ssbLength - 1 && !this.#completedBlocks.has(blockKey)) {
|
|
15883
16001
|
this.cancelBlockTimeout(meta.sbn, packetId, recoveredCatalogTrackName);
|
|
15884
16002
|
if (recoveredCatalogTrackName === void 0) {
|
|
15885
16003
|
this.onBlockComplete?.(meta.sbn, packetId);
|
|
@@ -16174,66 +16292,26 @@ var FECManager = class _FECManager {
|
|
|
16174
16292
|
const config = this.#trackConfigs.get(packetId);
|
|
16175
16293
|
const layers = config?.repairLayers;
|
|
16176
16294
|
if (layers === void 0 || layers.length === 0) return true;
|
|
16177
|
-
|
|
16178
|
-
|
|
16179
|
-
|
|
16180
|
-
|
|
16181
|
-
if (states === void 0) {
|
|
16182
|
-
const held = new Set(config.heldLayers ?? [0]);
|
|
16183
|
-
states = layers.filter((l) => held.has(l.layer)).map((l) => ({ layer: l.layer, repairTrackName: l.repairTrackName, total: l.rsIdOffset + l.repairSymbols }));
|
|
16184
|
-
this.#heldLayerStates.set(streamKey, states);
|
|
16295
|
+
let admission = this.#heldLayerStates.get(streamKey);
|
|
16296
|
+
if (admission === void 0) {
|
|
16297
|
+
admission = new HeldLayerAdmission(layers, config.heldLayers);
|
|
16298
|
+
this.#heldLayerStates.set(streamKey, admission);
|
|
16185
16299
|
}
|
|
16186
|
-
const
|
|
16187
|
-
|
|
16188
|
-
|
|
16189
|
-
|
|
16190
|
-
|
|
16191
|
-
|
|
16192
|
-
|
|
16193
|
-
|
|
16194
|
-
|
|
16195
|
-
|
|
16196
|
-
|
|
16197
|
-
|
|
16198
|
-
|
|
16199
|
-
|
|
16200
|
-
|
|
16201
|
-
delete state.latestBelowRsb;
|
|
16202
|
-
}
|
|
16203
|
-
if (state.anchor !== void 0 && stale(state.anchor)) state.settled = true;
|
|
16204
|
-
if (rsbLength < state.total) {
|
|
16205
|
-
if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
|
|
16206
|
-
else if (stale(ssStart)) continue;
|
|
16207
|
-
else if (state.settled || state.anchor !== void 0 && !serial32Precedes(ssStart, state.anchor)) code = "below_anchored_total";
|
|
16208
|
-
else if (state.latestBelow === void 0 || serial32Precedes(state.latestBelow, ssStart)) {
|
|
16209
|
-
state.latestBelow = ssStart;
|
|
16210
|
-
state.latestBelowRsb = rsbLength;
|
|
16211
|
-
}
|
|
16212
|
-
} else if (!state.settled && !stale(ssStart)) {
|
|
16213
|
-
if (state.anchor === void 0 || serial32Precedes(ssStart, state.anchor)) state.anchor = ssStart;
|
|
16214
|
-
if (state.latestBelow !== void 0 && !serial32Precedes(state.latestBelow, state.anchor)) {
|
|
16215
|
-
code = "below_anchored_total";
|
|
16216
|
-
ssStart = state.latestBelow;
|
|
16217
|
-
rsbLength = state.latestBelowRsb;
|
|
16218
|
-
}
|
|
16219
|
-
}
|
|
16220
|
-
}
|
|
16221
|
-
if (code !== void 0) {
|
|
16222
|
-
const reason = {
|
|
16223
|
-
code,
|
|
16224
|
-
layer: state.layer,
|
|
16225
|
-
total: state.total,
|
|
16226
|
-
rsbLength,
|
|
16227
|
-
ssStart,
|
|
16228
|
-
...state.anchor === void 0 ? {} : { anchor: state.anchor }
|
|
16229
|
-
};
|
|
16230
|
-
this.#fecFailClosed.set(streamKey, reason);
|
|
16231
|
-
this.#onFecFailClosed?.({ ...reason, packetId, catalogTrackName });
|
|
16232
|
-
console.warn(
|
|
16233
|
-
`[FECManager] FEC failed closed (fec-01 6.3.9) packetId=${packetId} track=${catalogTrackName ?? "-"}: ${JSON.stringify(reason)}`
|
|
16234
|
-
);
|
|
16235
|
-
return false;
|
|
16236
|
-
}
|
|
16300
|
+
const verdict = admission.admit(
|
|
16301
|
+
repairTrackName,
|
|
16302
|
+
ssStart,
|
|
16303
|
+
rsbLength,
|
|
16304
|
+
this.#layerAnchorHorizon(packetId),
|
|
16305
|
+
this.#sourceFrontiers.get(streamKey)?.sbn
|
|
16306
|
+
);
|
|
16307
|
+
if (verdict === "discard") return false;
|
|
16308
|
+
if (verdict !== void 0) {
|
|
16309
|
+
this.#fecFailClosed.set(streamKey, verdict);
|
|
16310
|
+
this.#onFecFailClosed?.({ ...verdict, packetId, catalogTrackName });
|
|
16311
|
+
console.warn(
|
|
16312
|
+
`[FECManager] FEC failed closed (fec-01 6.3.9) packetId=${packetId} track=${catalogTrackName ?? "-"}: ${JSON.stringify(verdict)}`
|
|
16313
|
+
);
|
|
16314
|
+
return false;
|
|
16237
16315
|
}
|
|
16238
16316
|
return true;
|
|
16239
16317
|
}
|
|
@@ -16243,15 +16321,13 @@ var FECManager = class _FECManager {
|
|
|
16243
16321
|
* times K, capped at 2^30 so compared serials stay inside one half-window.
|
|
16244
16322
|
*/
|
|
16245
16323
|
#layerAnchorHorizon(packetId) {
|
|
16246
|
-
const cap = 1073741824;
|
|
16247
16324
|
let blocks;
|
|
16248
16325
|
try {
|
|
16249
16326
|
blocks = this.isoRetentionBlocksForPacket(packetId);
|
|
16250
16327
|
} catch {
|
|
16251
16328
|
blocks = void 0;
|
|
16252
16329
|
}
|
|
16253
|
-
|
|
16254
|
-
return blocks === void 0 || k <= 0 ? cap : Math.min(cap, blocks * k);
|
|
16330
|
+
return heldLayerAnchorHorizon(blocks, this.#trackConfigs.get(packetId)?.k ?? 0);
|
|
16255
16331
|
}
|
|
16256
16332
|
/**
|
|
16257
16333
|
* Report each stream whose FEC fails closed at runtime (fec-01 6.3.9), once,
|
|
@@ -16669,6 +16745,7 @@ var FECManager = class _FECManager {
|
|
|
16669
16745
|
this.#cleanlyCompletedBlocks.clear();
|
|
16670
16746
|
this.#repairRequestedBlocks.clear();
|
|
16671
16747
|
this.#workerRecoveredBlocks.clear();
|
|
16748
|
+
this.#refusedRecoveries.clear();
|
|
16672
16749
|
this.#syncDecoderBlockOwners.clear();
|
|
16673
16750
|
this.#startupAlignedPackets.clear();
|
|
16674
16751
|
this.#startupPartialBlocks.clear();
|
|
@@ -19366,11 +19443,19 @@ var MmtpRouter = class {
|
|
|
19366
19443
|
#fecManager;
|
|
19367
19444
|
#defaultFecMapper;
|
|
19368
19445
|
#fecMappersByPacketId = /* @__PURE__ */ new Map();
|
|
19369
|
-
|
|
19370
|
-
#
|
|
19371
|
-
#
|
|
19372
|
-
#
|
|
19373
|
-
|
|
19446
|
+
/** Protected flows: one 7.2.7 receiver each, and the ingress its datagrams arrive with. */
|
|
19447
|
+
#provenance = /* @__PURE__ */ new Map();
|
|
19448
|
+
#onFlowProtected = null;
|
|
19449
|
+
#unadmittedListedDrops = 0;
|
|
19450
|
+
/** The ingress a held datagram arrived with; the receiver returns the same bytes on release. */
|
|
19451
|
+
#heldIngress = /* @__PURE__ */ new WeakMap();
|
|
19452
|
+
#unicastRepair = false;
|
|
19453
|
+
/** How to route a held unicast (MoQ) datagram once a record admits it. */
|
|
19454
|
+
#heldReplay = /* @__PURE__ */ new WeakMap();
|
|
19455
|
+
/** The flow whose receiver admitted a stream's source datagrams, by packetId then catalog track name: recovered packets answer to it. */
|
|
19456
|
+
#provenanceStreamFlow = /* @__PURE__ */ new Map();
|
|
19457
|
+
#unregisteredFlowDrops = 0;
|
|
19458
|
+
#ambiguousRecoveredDrops = 0;
|
|
19374
19459
|
// Temp state set by #processMMTP before calling parser.feed(),
|
|
19375
19460
|
// read by the parser's onFrame callback for CMAF assembly + dedup.
|
|
19376
19461
|
#currentFecInfo;
|
|
@@ -19507,10 +19592,6 @@ var MmtpRouter = class {
|
|
|
19507
19592
|
this.#fecManager = config?.fecManager ?? null;
|
|
19508
19593
|
this.#onFrameGap = config?.onFrameGap ?? null;
|
|
19509
19594
|
this.#defaultFecMapper = config?.fecMapper ?? null;
|
|
19510
|
-
this.#manifestVerifier = config?.manifestVerifier ?? null;
|
|
19511
|
-
this.#ownsManifestVerifier = config?.manifestVerifierOwnership !== "caller";
|
|
19512
|
-
this.#resolveManifestIdentity = config?.resolveManifestIdentity ?? null;
|
|
19513
|
-
this.#resolveManifestIdentityForRoute = config?.resolveManifestIdentityForRoute ?? null;
|
|
19514
19595
|
this.#parser = new MmtpContainerParser({
|
|
19515
19596
|
mfuTimeoutMs: config?.mfuTimeoutMs,
|
|
19516
19597
|
reassemblyPolicy: config?.reassemblyPolicy,
|
|
@@ -20042,29 +20123,162 @@ var MmtpRouter = class {
|
|
|
20042
20123
|
}
|
|
20043
20124
|
});
|
|
20044
20125
|
}
|
|
20045
|
-
|
|
20046
|
-
|
|
20047
|
-
|
|
20048
|
-
|
|
20049
|
-
|
|
20050
|
-
|
|
20051
|
-
|
|
20052
|
-
|
|
20053
|
-
this.#
|
|
20054
|
-
|
|
20055
|
-
|
|
20056
|
-
|
|
20057
|
-
|
|
20058
|
-
|
|
20059
|
-
|
|
20060
|
-
|
|
20061
|
-
|
|
20062
|
-
|
|
20126
|
+
/**
|
|
20127
|
+
* Protect a flow (draft-ramadan-moq-multicast-01 7.2.7): every multicast
|
|
20128
|
+
* datagram whose ingress names `ingress.flow`, source and repair alike, is
|
|
20129
|
+
* admitted by `receiver` before anything reads it, and an unadmitted one is
|
|
20130
|
+
* held or dropped. The caller owns the receiver's configuration; the router
|
|
20131
|
+
* releases it on `releaseProvenanceFlow` and on dispose.
|
|
20132
|
+
*/
|
|
20133
|
+
registerProvenanceFlow(ingress, receiver) {
|
|
20134
|
+
this.#provenance.get(ingress.flow)?.receiver.release();
|
|
20135
|
+
this.#provenance.set(ingress.flow, { receiver, ingress });
|
|
20136
|
+
receiver.keepPendingAgeMs = () => this.#oldestOpenBlockAgeMs(ingress.flow);
|
|
20137
|
+
}
|
|
20138
|
+
/**
|
|
20139
|
+
* Declare that this receiver can request unicast repair (a repair FETCH
|
|
20140
|
+
* scheduler is wired): a protected flow then keeps the pending entries of
|
|
20141
|
+
* its open blocks for the repair to consume (7.2.7 step 2).
|
|
20142
|
+
*/
|
|
20143
|
+
setUnicastRepair(enabled) {
|
|
20144
|
+
this.#unicastRepair = enabled;
|
|
20145
|
+
}
|
|
20146
|
+
#oldestOpenBlockAgeMs(flow) {
|
|
20147
|
+
const entry = this.#provenance.get(flow);
|
|
20148
|
+
if (!this.#unicastRepair || !entry) return void 0;
|
|
20149
|
+
let age;
|
|
20150
|
+
for (const { name, packetId } of entry.ingress.tracks) {
|
|
20151
|
+
const a = name ? this.#fecManager?.oldestOpenBlockAgeMs?.(packetId, name) : void 0;
|
|
20152
|
+
if (a !== void 0 && (age === void 0 || a > age)) age = a;
|
|
20153
|
+
}
|
|
20154
|
+
return age;
|
|
20155
|
+
}
|
|
20156
|
+
/**
|
|
20157
|
+
* Replace a registered flow's ingress (a rendition switch staged, committed
|
|
20158
|
+
* or cancelled) while keeping its receiver; a no-op for an unregistered flow.
|
|
20159
|
+
*/
|
|
20160
|
+
updateProvenanceFlowIngress(ingress) {
|
|
20161
|
+
const entry = this.#provenance.get(ingress.flow);
|
|
20162
|
+
if (!entry) return;
|
|
20163
|
+
this.#provenance.set(ingress.flow, { ...entry, ingress });
|
|
20164
|
+
if (entry.protected) {
|
|
20165
|
+
for (const { name, packetId } of ingress.tracks) {
|
|
20166
|
+
if (name && !entry.ingress.tracks.some((t) => t.name === name && t.packetId === packetId)) {
|
|
20167
|
+
this.#fecManager?.resetTrack?.(packetId, name);
|
|
20063
20168
|
}
|
|
20064
20169
|
}
|
|
20065
|
-
this.#manifestRecoveryQueues.clear();
|
|
20066
20170
|
}
|
|
20067
20171
|
}
|
|
20172
|
+
/**
|
|
20173
|
+
* Called once per protection of a flow: at the admission that makes this
|
|
20174
|
+
* router's consumers protected under it (7.2.7), after their unadmitted
|
|
20175
|
+
* holdings of its listed tracks are discarded. Again after an unprotect.
|
|
20176
|
+
*/
|
|
20177
|
+
onProvenanceFlowProtected(cb) {
|
|
20178
|
+
this.#onFlowProtected = cb;
|
|
20179
|
+
}
|
|
20180
|
+
/**
|
|
20181
|
+
* The receiver ceased to hold the flow's endpoint on failover (7.2.7): drop
|
|
20182
|
+
* its state and hold queues (no expiry counted) and un-protect its tracks,
|
|
20183
|
+
* which then take MoQ again. The registration stays, so multicast resuming
|
|
20184
|
+
* protects the flow anew at its next admission.
|
|
20185
|
+
*/
|
|
20186
|
+
unprotectProvenanceFlow(flow) {
|
|
20187
|
+
const entry = this.#provenance.get(flow);
|
|
20188
|
+
if (!entry) return;
|
|
20189
|
+
entry.receiver.release();
|
|
20190
|
+
entry.protected = false;
|
|
20191
|
+
}
|
|
20192
|
+
/** MoQ packets of a track a protected flow lists, dropped on its unicast path: duplicate, overflow, or listed by more than one flow. */
|
|
20193
|
+
get unadmittedListedDropCount() {
|
|
20194
|
+
return this.#unadmittedListedDrops;
|
|
20195
|
+
}
|
|
20196
|
+
/**
|
|
20197
|
+
* The admission that makes this router protected under `flow` (7.2.7):
|
|
20198
|
+
* its FEC instances discard what they hold of the flow's listed tracks so
|
|
20199
|
+
* no unadmitted symbol stands for an admitted one, then notify. Before it
|
|
20200
|
+
* nothing of the flow was admitted, so every such symbol is unadmitted.
|
|
20201
|
+
* The playback parse ledger is kept: it records MoQ copies already played,
|
|
20202
|
+
* not holdings, and clearing it re-emits that media (audio twice).
|
|
20203
|
+
* The FEC reset is the whole 7.2.7 discard here: no keyframe-overlay
|
|
20204
|
+
* instance exists to hold anything (repair-layers.ts is the catalog
|
|
20205
|
+
* contract only, no decoder), and packet_id 0 is never a listed track
|
|
20206
|
+
* (`ingressHasPacketId`), so nothing of it is held for the flow.
|
|
20207
|
+
*/
|
|
20208
|
+
#noteFlowAdmitted(flow) {
|
|
20209
|
+
const entry = this.#provenance.get(flow);
|
|
20210
|
+
if (!entry || entry.protected) return;
|
|
20211
|
+
entry.protected = true;
|
|
20212
|
+
for (const { name, packetId } of entry.ingress.tracks) {
|
|
20213
|
+
if (name) this.#fecManager?.resetTrack?.(packetId, name);
|
|
20214
|
+
}
|
|
20215
|
+
this.#onFlowProtected?.(flow);
|
|
20216
|
+
}
|
|
20217
|
+
/**
|
|
20218
|
+
* Admit a MoQ datagram of `packetId` on the unicast path of the protected
|
|
20219
|
+
* flow that lists it (7.2.7). `unlisted`: no protected flow lists it, so it
|
|
20220
|
+
* is read as before. A held datagram is routed by `replay` when a record
|
|
20221
|
+
* admits it; one more than one protected flow lists is dropped. So is an
|
|
20222
|
+
* overlap copy: it never enters FEC accounting, so admitting it would leave
|
|
20223
|
+
* its multicast twin a duplicate and the block one symbol short.
|
|
20224
|
+
*/
|
|
20225
|
+
#admitUnicast(packetId, packet, replay, overlapCopy = false) {
|
|
20226
|
+
let receiver;
|
|
20227
|
+
for (const entry of this.#provenance.values()) {
|
|
20228
|
+
if (!entry.protected || !ingressHasPacketId(entry.ingress, packetId)) continue;
|
|
20229
|
+
if (receiver || overlapCopy) {
|
|
20230
|
+
this.#unadmittedListedDrops++;
|
|
20231
|
+
return "dropped";
|
|
20232
|
+
}
|
|
20233
|
+
receiver = entry.receiver;
|
|
20234
|
+
}
|
|
20235
|
+
if (!receiver) return "unlisted";
|
|
20236
|
+
const { verdict } = receiver.onDatagram(packet, "unicast");
|
|
20237
|
+
if (verdict === "admitted") return "admitted";
|
|
20238
|
+
if (verdict === "held") {
|
|
20239
|
+
this.#heldReplay.set(packet, replay);
|
|
20240
|
+
return "held";
|
|
20241
|
+
}
|
|
20242
|
+
this.#unadmittedListedDrops++;
|
|
20243
|
+
return "dropped";
|
|
20244
|
+
}
|
|
20245
|
+
/**
|
|
20246
|
+
* Feed one bc-provenance record of `flow`: a datagram from `manifestPort`
|
|
20247
|
+
* (`multicast`) or an object of the manifest track (`mirror`). Held
|
|
20248
|
+
* datagrams the record admits are routed before this returns.
|
|
20249
|
+
*/
|
|
20250
|
+
feedProvenanceRecord(flow, record, path) {
|
|
20251
|
+
const entry = this.#provenance.get(flow);
|
|
20252
|
+
if (!entry || this.#disposed) return "unregistered";
|
|
20253
|
+
const result = entry.receiver.onRecord(record, path);
|
|
20254
|
+
if (result.admitted.length > 0) {
|
|
20255
|
+
this.#noteFlowAdmitted(flow);
|
|
20256
|
+
this.#withDeferredFecPokes(() => {
|
|
20257
|
+
for (const datagram of result.admitted) {
|
|
20258
|
+
const replay = this.#heldReplay.get(datagram);
|
|
20259
|
+
if (replay) replay();
|
|
20260
|
+
else this.#processMMTP(datagram, "multicast", true, this.#heldIngress.get(datagram) ?? entry.ingress, false, true);
|
|
20261
|
+
}
|
|
20262
|
+
});
|
|
20263
|
+
}
|
|
20264
|
+
return result.verdict;
|
|
20265
|
+
}
|
|
20266
|
+
/** The receiver ceased to hold the flow: its hold queues are dropped, not expired. */
|
|
20267
|
+
releaseProvenanceFlow(flow) {
|
|
20268
|
+
this.#provenance.get(flow)?.receiver.release();
|
|
20269
|
+
this.#provenance.delete(flow);
|
|
20270
|
+
for (const byName of this.#provenanceStreamFlow.values()) {
|
|
20271
|
+
for (const [name, bound] of byName) if (bound === flow) byName.delete(name);
|
|
20272
|
+
}
|
|
20273
|
+
}
|
|
20274
|
+
/** Multicast datagrams dropped because their ingress names a flow with no registered receiver. */
|
|
20275
|
+
get unregisteredFlowDropCount() {
|
|
20276
|
+
return this.#unregisteredFlowDrops;
|
|
20277
|
+
}
|
|
20278
|
+
/** Recovered packets dropped because more than one protected flow could own their stream. */
|
|
20279
|
+
get ambiguousRecoveredDropCount() {
|
|
20280
|
+
return this.#ambiguousRecoveredDrops;
|
|
20281
|
+
}
|
|
20068
20282
|
/**
|
|
20069
20283
|
* Main entry point: feed a raw MMTP packet for processing.
|
|
20070
20284
|
*
|
|
@@ -20079,10 +20293,10 @@ var MmtpRouter = class {
|
|
|
20079
20293
|
* they may never have crossed — and that attribution is what the physical-
|
|
20080
20294
|
* delivery evidence path is built on. Absence is not multicast; say which.
|
|
20081
20295
|
*/
|
|
20082
|
-
feedPacket(packet, source,
|
|
20296
|
+
feedPacket(packet, source, ingress) {
|
|
20083
20297
|
if (this.#disposed) return;
|
|
20084
20298
|
this.#withDeferredFecPokes(() => {
|
|
20085
|
-
this.#processMMTP(packet, source,
|
|
20299
|
+
this.#processMMTP(packet, source, false, ingress);
|
|
20086
20300
|
});
|
|
20087
20301
|
}
|
|
20088
20302
|
/**
|
|
@@ -20117,7 +20331,7 @@ var MmtpRouter = class {
|
|
|
20117
20331
|
feedOverlapCopy(packet, ingress) {
|
|
20118
20332
|
if (this.#disposed) return;
|
|
20119
20333
|
this.#withDeferredFecPokes(() => {
|
|
20120
|
-
this.#processMMTP(packet, "moq",
|
|
20334
|
+
this.#processMMTP(packet, "moq", false, ingress, true);
|
|
20121
20335
|
});
|
|
20122
20336
|
}
|
|
20123
20337
|
/**
|
|
@@ -20130,7 +20344,7 @@ var MmtpRouter = class {
|
|
|
20130
20344
|
this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName);
|
|
20131
20345
|
});
|
|
20132
20346
|
}
|
|
20133
|
-
#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName) {
|
|
20347
|
+
#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName, admitted = false) {
|
|
20134
20348
|
let header;
|
|
20135
20349
|
try {
|
|
20136
20350
|
header = parseMMTPHeader(packet);
|
|
@@ -20147,6 +20361,10 @@ var MmtpRouter = class {
|
|
|
20147
20361
|
`[MmtpRouter] Dropping MoQ repair packet pid=${repairPacketId}: owner hint is required`
|
|
20148
20362
|
);
|
|
20149
20363
|
}
|
|
20364
|
+
if (ownerPacketId != null && !admitted) {
|
|
20365
|
+
const admit = this.#admitUnicast(ownerPacketId, packet, () => this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName, true));
|
|
20366
|
+
if (admit === "held" || admit === "dropped") return;
|
|
20367
|
+
}
|
|
20150
20368
|
if (ownerPacketId != null && this.#fecManager) {
|
|
20151
20369
|
this.#moqRepairPidCounts[repairPacketId] = (this.#moqRepairPidCounts[repairPacketId] || 0) + 1;
|
|
20152
20370
|
this.#moqRepairOwnerCounts[ownerPacketId] = (this.#moqRepairOwnerCounts[ownerPacketId] || 0) + 1;
|
|
@@ -20290,10 +20508,11 @@ var MmtpRouter = class {
|
|
|
20290
20508
|
catalogTrackName
|
|
20291
20509
|
);
|
|
20292
20510
|
if (recovered && !alreadyCompleted) {
|
|
20293
|
-
|
|
20294
|
-
|
|
20295
|
-
|
|
20296
|
-
|
|
20511
|
+
this.#routeRecoveredBlock(blockStart, recovered, ownerPacketId, catalogTrackName, () => {
|
|
20512
|
+
fec.cancelBlockTimeout(blockStart, ownerPacketId, catalogTrackName);
|
|
20513
|
+
fec.countRecovery(ownerPacketId, blockStart, catalogTrackName);
|
|
20514
|
+
fec.markBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
|
|
20515
|
+
});
|
|
20297
20516
|
}
|
|
20298
20517
|
}
|
|
20299
20518
|
/**
|
|
@@ -20334,10 +20553,11 @@ var MmtpRouter = class {
|
|
|
20334
20553
|
const alreadyCompleted = fec.isBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
|
|
20335
20554
|
const recovered = fec.feedSourceSymbol(ownerPacketId, ssId, payload.symbolData, catalogTrackName);
|
|
20336
20555
|
if (!recovered || alreadyCompleted) continue;
|
|
20337
|
-
|
|
20338
|
-
|
|
20339
|
-
|
|
20340
|
-
|
|
20556
|
+
this.#routeRecoveredBlock(blockStart, recovered, ownerPacketId, catalogTrackName, () => {
|
|
20557
|
+
fec.cancelBlockTimeout(blockStart, ownerPacketId, catalogTrackName);
|
|
20558
|
+
fec.countRecovery(ownerPacketId, blockStart, catalogTrackName);
|
|
20559
|
+
fec.markBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
|
|
20560
|
+
});
|
|
20341
20561
|
}
|
|
20342
20562
|
}
|
|
20343
20563
|
#resolveRepairOwnerPacketId(packetId, ingress) {
|
|
@@ -20360,14 +20580,18 @@ var MmtpRouter = class {
|
|
|
20360
20580
|
* Sync WASM decoders return the block's K' = SSB_length fixed-width
|
|
20361
20581
|
* ssbg_mode1 source symbols concatenated. */
|
|
20362
20582
|
routeRecoveredBlock(blockId, data, packetId, catalogTrackName) {
|
|
20363
|
-
|
|
20583
|
+
let recovered = false;
|
|
20584
|
+
this.#withDeferredFecPokes(() => {
|
|
20585
|
+
recovered = this.#routeRecoveredBlock(blockId, data, packetId, catalogTrackName);
|
|
20586
|
+
});
|
|
20587
|
+
return recovered;
|
|
20364
20588
|
}
|
|
20365
20589
|
/** Route a single recovered source symbol through the parser + dedup pipeline.
|
|
20366
20590
|
* Worker-backed decoders emit one padded source symbol per frame event. */
|
|
20367
20591
|
routeRecoveredSymbol(blockId, data, packetId, esi = 0, catalogTrackName) {
|
|
20368
20592
|
const blockProfile = this.#blockProfileFor(packetId);
|
|
20369
20593
|
const result = processFecSymbol(data, { ownerPacketId: packetId, index: esi, blockProfile });
|
|
20370
|
-
this.#routeVerifiedFecSymbols(blockId, result, packetId, catalogTrackName);
|
|
20594
|
+
return this.#routeVerifiedFecSymbols(blockId, result, packetId, catalogTrackName);
|
|
20371
20595
|
}
|
|
20372
20596
|
// ── Keyframe timer cancellation ─────────────────────────────────
|
|
20373
20597
|
/** Cancel the active keyframe loss timer.
|
|
@@ -20410,7 +20634,6 @@ var MmtpRouter = class {
|
|
|
20410
20634
|
this.#lastDirectSourceAudioCursorByStream.delete(streamKey);
|
|
20411
20635
|
this.#lastDirectEmittedAudioCursorByStream.delete(streamKey);
|
|
20412
20636
|
this.#pendingDirectRecoveredAudioByStream.delete(streamKey);
|
|
20413
|
-
this.#manifestRecoveryQueues.delete(streamKey);
|
|
20414
20637
|
this.#deliveredVideoHighWaterByStream.delete(streamKey);
|
|
20415
20638
|
this.#stream(packetId, catalogTrackName).duplicates.clear();
|
|
20416
20639
|
const assemblersToReset = /* @__PURE__ */ new Set();
|
|
@@ -20460,7 +20683,6 @@ var MmtpRouter = class {
|
|
|
20460
20683
|
this.#lastDirectSourceAudioCursorByStream.delete(key);
|
|
20461
20684
|
this.#lastDirectEmittedAudioCursorByStream.delete(key);
|
|
20462
20685
|
this.#pendingDirectRecoveredAudioByStream.delete(key);
|
|
20463
|
-
this.#manifestRecoveryQueues.delete(key);
|
|
20464
20686
|
this.#trackInitDelivered.delete(key);
|
|
20465
20687
|
this.#parserDiagnostics.delete(key);
|
|
20466
20688
|
this.#pendingFecPokes.delete(key);
|
|
@@ -20721,8 +20943,9 @@ var MmtpRouter = class {
|
|
|
20721
20943
|
this.#parserDiagnostics.clear();
|
|
20722
20944
|
this.#catalogTrackNamesByPacketId.clear();
|
|
20723
20945
|
this.#repairOwnersByPacketId.clear();
|
|
20724
|
-
|
|
20725
|
-
this.#
|
|
20946
|
+
for (const { receiver } of this.#provenance.values()) receiver.release();
|
|
20947
|
+
this.#provenance.clear();
|
|
20948
|
+
this.#provenanceStreamFlow.clear();
|
|
20726
20949
|
for (const assembler of this.#mseAssemblers.values()) assembler.dispose();
|
|
20727
20950
|
this.#mseAssemblers.clear();
|
|
20728
20951
|
}
|
|
@@ -20999,7 +21222,7 @@ var MmtpRouter = class {
|
|
|
20999
21222
|
* 1. parseHeader() → extract packetId + fecPayloadId for FEC feeding
|
|
21000
21223
|
* 2. feed() → MFU extraction, reassembly, keyframe detection, emit via callbacks
|
|
21001
21224
|
*/
|
|
21002
|
-
#processMMTP(packet, source,
|
|
21225
|
+
#processMMTP(packet, source, manifestAdmitted = false, ingress, overlapCopy = false, releasedFromQuarantine = false) {
|
|
21003
21226
|
const parsed = this.#parser.parsePacket(packet);
|
|
21004
21227
|
if (!parsed) return;
|
|
21005
21228
|
const info = parsed.info;
|
|
@@ -21024,43 +21247,29 @@ var MmtpRouter = class {
|
|
|
21024
21247
|
}
|
|
21025
21248
|
const fecUnreadable = !info.isRepair && info.fecPayloadId != null && this.#fecManager !== null && this.#classifyPacketId(info.packetId) !== null && this.#configuredBlockProfile(info.packetId) === void 0;
|
|
21026
21249
|
if (fecUnreadable && !manifestAdmitted) this.#noteUnreadableFecPayloadId(info.packetId, "source");
|
|
21027
|
-
if (!info.isRepair &&
|
|
21028
|
-
let
|
|
21029
|
-
if (!
|
|
21030
|
-
|
|
21031
|
-
|
|
21032
|
-
|
|
21033
|
-
|
|
21034
|
-
|
|
21035
|
-
|
|
21036
|
-
|
|
21037
|
-
recovered: false,
|
|
21038
|
-
sourceSymbolFormat: "full-mmtp-packet"
|
|
21039
|
-
}) : this.#resolveManifestIdentity(
|
|
21040
|
-
info.packetId,
|
|
21041
|
-
coordinates.sbn,
|
|
21042
|
-
coordinates.esi,
|
|
21043
|
-
catalogTrackName
|
|
21044
|
-
)) ?? void 0;
|
|
21045
|
-
}
|
|
21046
|
-
if (identity) {
|
|
21047
|
-
identity = {
|
|
21048
|
-
...identity,
|
|
21049
|
-
packetId: info.packetId,
|
|
21050
|
-
...info.fecPayloadId ? { ssId: info.fecPayloadId.ssId } : {}
|
|
21051
|
-
};
|
|
21052
|
-
const authenticated = info.fecPayloadId ? packet.subarray(0, packet.length - 4) : packet;
|
|
21053
|
-
let quarantined = false;
|
|
21054
|
-
const admission = this.#manifestVerifier.admitObject(identity, authenticated, () => {
|
|
21055
|
-
if (!this.#disposed) {
|
|
21056
|
-
this.#processMMTP(packet, source, identity, true, ingress, overlapCopy, quarantined);
|
|
21057
|
-
}
|
|
21058
|
-
});
|
|
21059
|
-
quarantined = admission.action === "stall";
|
|
21250
|
+
if (source === "multicast" && manifestAdmitted && !info.isRepair && ingress?.flow !== void 0) {
|
|
21251
|
+
let byName = this.#provenanceStreamFlow.get(info.packetId);
|
|
21252
|
+
if (!byName) this.#provenanceStreamFlow.set(info.packetId, byName = /* @__PURE__ */ new Map());
|
|
21253
|
+
byName.set(catalogTrackNameForPacket(ingress, info.packetId), ingress.flow);
|
|
21254
|
+
}
|
|
21255
|
+
if (source === "multicast" && !manifestAdmitted && ingress?.flow !== void 0) {
|
|
21256
|
+
const protectedFlow = this.#provenance.get(ingress.flow);
|
|
21257
|
+
if (!protectedFlow) {
|
|
21258
|
+
this.#unregisteredFlowDrops++;
|
|
21259
|
+
debugRouter(`[MmtpRouter] Dropping multicast packet pid=${info.packetId}: flow ${ingress.flow} has no receiver`);
|
|
21060
21260
|
return;
|
|
21061
21261
|
}
|
|
21062
|
-
|
|
21063
|
-
|
|
21262
|
+
const { verdict } = protectedFlow.receiver.onDatagram(packet, "multicast");
|
|
21263
|
+
if (verdict === "admitted") {
|
|
21264
|
+
this.#noteFlowAdmitted(ingress.flow);
|
|
21265
|
+
this.#processMMTP(packet, source, true, ingress, overlapCopy);
|
|
21266
|
+
} else if (verdict === "held") this.#heldIngress.set(packet, ingress);
|
|
21267
|
+
return;
|
|
21268
|
+
}
|
|
21269
|
+
if (source === "moq" && !manifestAdmitted) {
|
|
21270
|
+
const admit = this.#admitUnicast(info.packetId, packet, () => this.#processMMTP(packet, source, true, ingress, overlapCopy, true), overlapCopy);
|
|
21271
|
+
if (admit !== "unlisted") {
|
|
21272
|
+
if (admit === "admitted") this.#processMMTP(packet, source, true, ingress, overlapCopy);
|
|
21064
21273
|
return;
|
|
21065
21274
|
}
|
|
21066
21275
|
}
|
|
@@ -21237,36 +21446,22 @@ var MmtpRouter = class {
|
|
|
21237
21446
|
sourceFec.symbolData,
|
|
21238
21447
|
catalogTrackName
|
|
21239
21448
|
);
|
|
21240
|
-
if (recovered) {
|
|
21241
|
-
|
|
21242
|
-
|
|
21243
|
-
|
|
21244
|
-
|
|
21245
|
-
|
|
21246
|
-
sourceFec.sbn,
|
|
21247
|
-
info.packetId,
|
|
21248
|
-
catalogTrackName
|
|
21249
|
-
);
|
|
21250
|
-
}
|
|
21251
|
-
}
|
|
21252
|
-
if (!alreadyCompleted) {
|
|
21253
|
-
this.#fecManager.countRecovery(info.packetId, sourceFec.sbn, catalogTrackName);
|
|
21449
|
+
if (recovered && !alreadyCompleted) {
|
|
21450
|
+
const fec = this.#fecManager;
|
|
21451
|
+
const sbn = sourceFec.sbn;
|
|
21452
|
+
const packetId = info.packetId;
|
|
21453
|
+
const tracked = this.#classifyPacketId(packetId) !== null;
|
|
21454
|
+
this.#routeRecoveredBlock(sbn, recovered, packetId, catalogTrackName, () => {
|
|
21254
21455
|
if (catalogTrackName === void 0) {
|
|
21255
|
-
|
|
21456
|
+
if (tracked) fec.cancelBlockTimeout(sbn, packetId);
|
|
21457
|
+
fec.countRecovery(packetId, sbn, catalogTrackName);
|
|
21458
|
+
fec.markBlockCompleted(sbn, packetId);
|
|
21256
21459
|
} else {
|
|
21257
|
-
|
|
21258
|
-
|
|
21259
|
-
|
|
21260
|
-
catalogTrackName
|
|
21261
|
-
);
|
|
21460
|
+
if (tracked) fec.cancelBlockTimeout(sbn, packetId, catalogTrackName);
|
|
21461
|
+
fec.countRecovery(packetId, sbn, catalogTrackName);
|
|
21462
|
+
fec.markBlockCompleted(sbn, packetId, catalogTrackName);
|
|
21262
21463
|
}
|
|
21263
|
-
|
|
21264
|
-
sourceFec.sbn,
|
|
21265
|
-
recovered,
|
|
21266
|
-
info.packetId,
|
|
21267
|
-
catalogTrackName
|
|
21268
|
-
);
|
|
21269
|
-
}
|
|
21464
|
+
});
|
|
21270
21465
|
}
|
|
21271
21466
|
}
|
|
21272
21467
|
}
|
|
@@ -21642,7 +21837,7 @@ var MmtpRouter = class {
|
|
|
21642
21837
|
}
|
|
21643
21838
|
// ── Private: FEC recovery routing ───────────────────────────────
|
|
21644
21839
|
/** Route WASM-recovered block through appropriate path based on container kind. */
|
|
21645
|
-
#routeRecoveredBlock(blockId, recovered, ownerPacketId, catalogTrackName) {
|
|
21840
|
+
#routeRecoveredBlock(blockId, recovered, ownerPacketId, catalogTrackName, complete) {
|
|
21646
21841
|
const packetSymbolSize = this.#symbolSizeForPacket(ownerPacketId);
|
|
21647
21842
|
if (ownerPacketId === void 0) {
|
|
21648
21843
|
throw new Error("[MmtpRouter] a recovered FEC block needs its owner packetId to read its fec.blockProfile");
|
|
@@ -21659,103 +21854,68 @@ var MmtpRouter = class {
|
|
|
21659
21854
|
ownerPacketId,
|
|
21660
21855
|
blockProfile
|
|
21661
21856
|
});
|
|
21662
|
-
this.#routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
|
|
21663
|
-
}
|
|
21664
|
-
|
|
21665
|
-
|
|
21666
|
-
|
|
21667
|
-
|
|
21668
|
-
|
|
21669
|
-
|
|
21670
|
-
|
|
21671
|
-
|
|
21672
|
-
|
|
21857
|
+
return this.#routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName, complete);
|
|
21858
|
+
}
|
|
21859
|
+
/**
|
|
21860
|
+
* A recovered source packet of a protected flow is admitted on its own
|
|
21861
|
+
* digest: its bytes with the SS_ID it was sent with (SS_Start + ESI)
|
|
21862
|
+
* appended are the datagram the record listed (7.2.7). One that no record
|
|
21863
|
+
* lists is dropped; holding it would let it out later as a multicast arrival.
|
|
21864
|
+
* Returns whether the decode recovered the block: every packet it yields
|
|
21865
|
+
* that this router has not parsed already is admitted. A block one of whose
|
|
21866
|
+
* recovered packets is refused is unrecovered (7.2.7): its caller leaves it
|
|
21867
|
+
* open, so it reaches its FEC deadline and fails by class. Always true
|
|
21868
|
+
* outside a protected flow.
|
|
21869
|
+
*/
|
|
21870
|
+
#routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName, complete) {
|
|
21871
|
+
const receiver = ownerPacketId == null ? void 0 : this.#provenanceFor(ownerPacketId, catalogTrackName);
|
|
21872
|
+
let recovered = true;
|
|
21873
|
+
if (receiver) {
|
|
21874
|
+
const parsed = this.#stream(ownerPacketId, catalogTrackName).duplicates.parsedSymbols;
|
|
21875
|
+
result = {
|
|
21876
|
+
...result,
|
|
21877
|
+
symbols: result.symbols.filter((sym) => {
|
|
21878
|
+
const ssId = this.#fecAnchorForRecovered(blockId, sym.index);
|
|
21879
|
+
if (parsed.has(ssId)) return false;
|
|
21880
|
+
let admitted = false;
|
|
21881
|
+
if (receiver === "ambiguous") {
|
|
21882
|
+
this.#ambiguousRecoveredDrops++;
|
|
21883
|
+
} else {
|
|
21884
|
+
const wire = new Uint8Array(sym.rawSymbol.byteLength + 4);
|
|
21885
|
+
wire.set(sym.rawSymbol);
|
|
21886
|
+
new DataView(wire.buffer).setUint32(sym.rawSymbol.byteLength, ssId);
|
|
21887
|
+
admitted = receiver.admitRecovered(wire);
|
|
21888
|
+
}
|
|
21889
|
+
if (!admitted) recovered = false;
|
|
21890
|
+
return admitted;
|
|
21891
|
+
})
|
|
21892
|
+
};
|
|
21673
21893
|
}
|
|
21674
|
-
|
|
21675
|
-
|
|
21676
|
-
|
|
21677
|
-
|
|
21678
|
-
|
|
21679
|
-
|
|
21680
|
-
|
|
21681
|
-
|
|
21682
|
-
|
|
21683
|
-
|
|
21894
|
+
if (recovered) complete?.();
|
|
21895
|
+
this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
|
|
21896
|
+
return recovered;
|
|
21897
|
+
}
|
|
21898
|
+
/**
|
|
21899
|
+
* The receiver a recovered packet of `packetId` answers to: the flow that
|
|
21900
|
+
* admitted the stream's source datagrams. Before any was admitted, the one
|
|
21901
|
+
* flow whose registration lists the stream; more than one (a repair flow
|
|
21902
|
+
* also lists its owners' source tracks) cannot be told apart, so none.
|
|
21903
|
+
*/
|
|
21904
|
+
#provenanceFor(packetId, catalogTrackName) {
|
|
21905
|
+
const byName = this.#provenanceStreamFlow.get(packetId);
|
|
21906
|
+
const flows = new Set(
|
|
21907
|
+
catalogTrackName === void 0 || !byName ? byName?.values() : byName.has(catalogTrackName) ? [byName.get(catalogTrackName)] : []
|
|
21908
|
+
);
|
|
21909
|
+
if (flows.size > 1) return "ambiguous";
|
|
21910
|
+
for (const bound of flows) return this.#provenance.get(bound)?.receiver ?? "ambiguous";
|
|
21911
|
+
let match;
|
|
21912
|
+
for (const { receiver, ingress } of this.#provenance.values()) {
|
|
21913
|
+
if (ingress.tracks.some((t) => t.packetId === packetId && (catalogTrackName === void 0 || t.name === catalogTrackName))) {
|
|
21914
|
+
if (match) return "ambiguous";
|
|
21915
|
+
match = receiver;
|
|
21684
21916
|
}
|
|
21685
|
-
this.#admitRecoveredResult(
|
|
21686
|
-
next.blockId,
|
|
21687
|
-
next.result,
|
|
21688
|
-
next.ownerPacketId,
|
|
21689
|
-
next.catalogTrackName,
|
|
21690
|
-
() => {
|
|
21691
|
-
queue.shift();
|
|
21692
|
-
drain();
|
|
21693
|
-
}
|
|
21694
|
-
);
|
|
21695
|
-
};
|
|
21696
|
-
drain();
|
|
21697
|
-
}
|
|
21698
|
-
#admitRecoveredResult(blockId, result, ownerPacketId, catalogTrackName, done) {
|
|
21699
|
-
const verifier = this.#manifestVerifier;
|
|
21700
|
-
const resolveIdentity = this.#resolveManifestIdentity;
|
|
21701
|
-
const resolveIdentityForRoute = this.#resolveManifestIdentityForRoute;
|
|
21702
|
-
if (!verifier) {
|
|
21703
|
-
this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
|
|
21704
|
-
done();
|
|
21705
|
-
return;
|
|
21706
21917
|
}
|
|
21707
|
-
|
|
21708
|
-
const symbols = result.symbols;
|
|
21709
|
-
const admitNext = (index) => {
|
|
21710
|
-
const symbol = symbols[index];
|
|
21711
|
-
if (!symbol) {
|
|
21712
|
-
done();
|
|
21713
|
-
return;
|
|
21714
|
-
}
|
|
21715
|
-
const identity = resolveIdentityForRoute ? resolveIdentityForRoute({
|
|
21716
|
-
packetId: ownerPacketId,
|
|
21717
|
-
sbn: blockId,
|
|
21718
|
-
esi: symbol.index,
|
|
21719
|
-
...catalogTrackName === void 0 ? {} : { catalogTrackName },
|
|
21720
|
-
recovered: true,
|
|
21721
|
-
sourceSymbolFormat: "fec-padded-recovery"
|
|
21722
|
-
}) : resolveIdentity(ownerPacketId, blockId, symbol.index, catalogTrackName);
|
|
21723
|
-
if (!identity) {
|
|
21724
|
-
console.warn(`[MmtpRouter] manifest identity unresolved; recovered symbol is fail-open for packetId=${ownerPacketId} block=${blockId} esi=${symbol.index}`);
|
|
21725
|
-
this.#routeProcessedFecSymbols(
|
|
21726
|
-
blockId,
|
|
21727
|
-
{ ...result, symbols: [symbol] },
|
|
21728
|
-
ownerPacketId,
|
|
21729
|
-
catalogTrackName
|
|
21730
|
-
);
|
|
21731
|
-
admitNext(index + 1);
|
|
21732
|
-
return;
|
|
21733
|
-
}
|
|
21734
|
-
const admission = verifier.admitObject(
|
|
21735
|
-
{
|
|
21736
|
-
...identity,
|
|
21737
|
-
packetId: ownerPacketId
|
|
21738
|
-
},
|
|
21739
|
-
symbol.rawSymbol,
|
|
21740
|
-
() => {
|
|
21741
|
-
if (!this.#disposed) {
|
|
21742
|
-
this.#routeProcessedFecSymbols(
|
|
21743
|
-
blockId,
|
|
21744
|
-
{ ...result, symbols: [symbol] },
|
|
21745
|
-
ownerPacketId,
|
|
21746
|
-
catalogTrackName
|
|
21747
|
-
);
|
|
21748
|
-
}
|
|
21749
|
-
admitNext(index + 1);
|
|
21750
|
-
},
|
|
21751
|
-
// A stalled symbol that is later discarded instead of played must still
|
|
21752
|
-
// advance this loop; enforce mode never calls the release callback for
|
|
21753
|
-
// it (BLO-30095).
|
|
21754
|
-
() => admitNext(index + 1)
|
|
21755
|
-
);
|
|
21756
|
-
if (admission.action === "drop") admitNext(index + 1);
|
|
21757
|
-
};
|
|
21758
|
-
admitNext(0);
|
|
21918
|
+
return match;
|
|
21759
21919
|
}
|
|
21760
21920
|
#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName) {
|
|
21761
21921
|
if (ownerPacketId == null) return;
|
|
@@ -22652,10 +22812,11 @@ var MulticastTransport = class {
|
|
|
22652
22812
|
try {
|
|
22653
22813
|
const packetIds = config.packetIds?.length ? new Set(config.packetIds) : null;
|
|
22654
22814
|
console.log(
|
|
22655
|
-
`[MulticastTransport] window.multicast.subscribe(${config.sourceAddress}, ${config.groupAddress}, ${config.port})`
|
|
22815
|
+
`[MulticastTransport] window.multicast.subscribe(${config.sourceAddress ?? config.senderAddress ?? "*"}, ${config.groupAddress}, ${config.port})`
|
|
22656
22816
|
);
|
|
22657
22817
|
const subscribeConfig = toWindowMulticastSubscribeConfig({
|
|
22658
22818
|
source: config.sourceAddress,
|
|
22819
|
+
senderAddress: config.senderAddress,
|
|
22659
22820
|
group: config.groupAddress,
|
|
22660
22821
|
port: config.port,
|
|
22661
22822
|
mode: config.mode,
|
|
@@ -23251,47 +23412,2767 @@ var MulticastTransport = class {
|
|
|
23251
23412
|
}
|
|
23252
23413
|
};
|
|
23253
23414
|
|
|
23254
|
-
//
|
|
23255
|
-
|
|
23256
|
-
|
|
23257
|
-
|
|
23415
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/crypto.js
|
|
23416
|
+
var crypto2 = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
23417
|
+
|
|
23418
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/utils.js
|
|
23419
|
+
function isBytes(a) {
|
|
23420
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
23421
|
+
}
|
|
23422
|
+
function anumber(n) {
|
|
23423
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
23424
|
+
throw new Error("positive integer expected, got " + n);
|
|
23425
|
+
}
|
|
23426
|
+
function abytes(b, ...lengths) {
|
|
23427
|
+
if (!isBytes(b))
|
|
23428
|
+
throw new Error("Uint8Array expected");
|
|
23429
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
23430
|
+
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
23431
|
+
}
|
|
23432
|
+
function aexists(instance, checkFinished = true) {
|
|
23433
|
+
if (instance.destroyed)
|
|
23434
|
+
throw new Error("Hash instance has been destroyed");
|
|
23435
|
+
if (checkFinished && instance.finished)
|
|
23436
|
+
throw new Error("Hash#digest() has already been called");
|
|
23437
|
+
}
|
|
23438
|
+
function aoutput(out, instance) {
|
|
23439
|
+
abytes(out);
|
|
23440
|
+
const min = instance.outputLen;
|
|
23441
|
+
if (out.length < min) {
|
|
23442
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
23443
|
+
}
|
|
23444
|
+
}
|
|
23445
|
+
function clean(...arrays) {
|
|
23446
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
23447
|
+
arrays[i].fill(0);
|
|
23448
|
+
}
|
|
23449
|
+
}
|
|
23450
|
+
function createView(arr) {
|
|
23451
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
23452
|
+
}
|
|
23453
|
+
function rotr(word, shift) {
|
|
23454
|
+
return word << 32 - shift | word >>> shift;
|
|
23455
|
+
}
|
|
23456
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
23457
|
+
// @ts-ignore
|
|
23458
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
23459
|
+
))();
|
|
23460
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
23461
|
+
function bytesToHex(bytes) {
|
|
23462
|
+
abytes(bytes);
|
|
23463
|
+
if (hasHexBuiltin)
|
|
23464
|
+
return bytes.toHex();
|
|
23465
|
+
let hex3 = "";
|
|
23466
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
23467
|
+
hex3 += hexes[bytes[i]];
|
|
23468
|
+
}
|
|
23469
|
+
return hex3;
|
|
23470
|
+
}
|
|
23471
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
23472
|
+
function asciiToBase16(ch) {
|
|
23473
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
23474
|
+
return ch - asciis._0;
|
|
23475
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
23476
|
+
return ch - (asciis.A - 10);
|
|
23477
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
23478
|
+
return ch - (asciis.a - 10);
|
|
23479
|
+
return;
|
|
23480
|
+
}
|
|
23481
|
+
function hexToBytes(hex3) {
|
|
23482
|
+
if (typeof hex3 !== "string")
|
|
23483
|
+
throw new Error("hex string expected, got " + typeof hex3);
|
|
23484
|
+
if (hasHexBuiltin)
|
|
23485
|
+
return Uint8Array.fromHex(hex3);
|
|
23486
|
+
const hl = hex3.length;
|
|
23487
|
+
const al = hl / 2;
|
|
23488
|
+
if (hl % 2)
|
|
23489
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
23490
|
+
const array = new Uint8Array(al);
|
|
23491
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
23492
|
+
const n1 = asciiToBase16(hex3.charCodeAt(hi));
|
|
23493
|
+
const n2 = asciiToBase16(hex3.charCodeAt(hi + 1));
|
|
23494
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
23495
|
+
const char = hex3[hi] + hex3[hi + 1];
|
|
23496
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
23497
|
+
}
|
|
23498
|
+
array[ai] = n1 * 16 + n2;
|
|
23499
|
+
}
|
|
23500
|
+
return array;
|
|
23501
|
+
}
|
|
23502
|
+
function utf8ToBytes(str) {
|
|
23503
|
+
if (typeof str !== "string")
|
|
23504
|
+
throw new Error("string expected");
|
|
23505
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
23506
|
+
}
|
|
23507
|
+
function toBytes(data) {
|
|
23508
|
+
if (typeof data === "string")
|
|
23509
|
+
data = utf8ToBytes(data);
|
|
23510
|
+
abytes(data);
|
|
23511
|
+
return data;
|
|
23512
|
+
}
|
|
23513
|
+
function concatBytes(...arrays) {
|
|
23514
|
+
let sum = 0;
|
|
23515
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
23516
|
+
const a = arrays[i];
|
|
23517
|
+
abytes(a);
|
|
23518
|
+
sum += a.length;
|
|
23519
|
+
}
|
|
23520
|
+
const res = new Uint8Array(sum);
|
|
23521
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
23522
|
+
const a = arrays[i];
|
|
23523
|
+
res.set(a, pad);
|
|
23524
|
+
pad += a.length;
|
|
23525
|
+
}
|
|
23526
|
+
return res;
|
|
23527
|
+
}
|
|
23528
|
+
var Hash = class {
|
|
23529
|
+
};
|
|
23530
|
+
function createHasher(hashCons) {
|
|
23531
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
23532
|
+
const tmp = hashCons();
|
|
23533
|
+
hashC.outputLen = tmp.outputLen;
|
|
23534
|
+
hashC.blockLen = tmp.blockLen;
|
|
23535
|
+
hashC.create = () => hashCons();
|
|
23536
|
+
return hashC;
|
|
23258
23537
|
}
|
|
23259
|
-
function
|
|
23260
|
-
|
|
23261
|
-
|
|
23262
|
-
if (!raw || typeof raw !== "object") continue;
|
|
23263
|
-
const ns = raw;
|
|
23264
|
-
if (ns.type !== "amt") continue;
|
|
23265
|
-
if (ns.discovery === "driad") continue;
|
|
23266
|
-
const relay = typeof ns.relay === "string" ? ns.relay.trim() : "";
|
|
23267
|
-
if (!relay) continue;
|
|
23268
|
-
return { relay, port: normalizePort(ns.port) ?? DEFAULT_AMT_RELAY_PORT };
|
|
23538
|
+
function randomBytes(bytesLength = 32) {
|
|
23539
|
+
if (crypto2 && typeof crypto2.getRandomValues === "function") {
|
|
23540
|
+
return crypto2.getRandomValues(new Uint8Array(bytesLength));
|
|
23269
23541
|
}
|
|
23270
|
-
|
|
23271
|
-
|
|
23272
|
-
function intIn(value, min, max) {
|
|
23273
|
-
return Number.isInteger(value) && value >= min && value <= max;
|
|
23274
|
-
}
|
|
23275
|
-
function catalogAtsc3NetworkSource(networkSource) {
|
|
23276
|
-
const entries = Array.isArray(networkSource) ? networkSource : networkSource ? [networkSource] : [];
|
|
23277
|
-
for (const raw of entries) {
|
|
23278
|
-
if (!raw || typeof raw !== "object") continue;
|
|
23279
|
-
const ns = raw;
|
|
23280
|
-
if (ns.type !== "atsc3" || !intIn(ns.frequency, 1, Number.MAX_SAFE_INTEGER)) continue;
|
|
23281
|
-
const slsUri = typeof ns.slsUri === "string" ? ns.slsUri.trim() : "";
|
|
23282
|
-
return {
|
|
23283
|
-
frequency: ns.frequency,
|
|
23284
|
-
...intIn(ns.plpId, 0, 63) ? { plpId: ns.plpId } : {},
|
|
23285
|
-
...intIn(ns.serviceId, 0, 65535) ? { serviceId: ns.serviceId } : {},
|
|
23286
|
-
...slsUri ? { slsUri } : {}
|
|
23287
|
-
};
|
|
23542
|
+
if (crypto2 && typeof crypto2.randomBytes === "function") {
|
|
23543
|
+
return Uint8Array.from(crypto2.randomBytes(bytesLength));
|
|
23288
23544
|
}
|
|
23289
|
-
|
|
23545
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
23290
23546
|
}
|
|
23291
23547
|
|
|
23292
|
-
//
|
|
23293
|
-
|
|
23294
|
-
|
|
23548
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_md.js
|
|
23549
|
+
function setBigUint64(view2, byteOffset, value, isLE) {
|
|
23550
|
+
if (typeof view2.setBigUint64 === "function")
|
|
23551
|
+
return view2.setBigUint64(byteOffset, value, isLE);
|
|
23552
|
+
const _32n2 = BigInt(32);
|
|
23553
|
+
const _u32_max = BigInt(4294967295);
|
|
23554
|
+
const wh = Number(value >> _32n2 & _u32_max);
|
|
23555
|
+
const wl = Number(value & _u32_max);
|
|
23556
|
+
const h = isLE ? 4 : 0;
|
|
23557
|
+
const l = isLE ? 0 : 4;
|
|
23558
|
+
view2.setUint32(byteOffset + h, wh, isLE);
|
|
23559
|
+
view2.setUint32(byteOffset + l, wl, isLE);
|
|
23560
|
+
}
|
|
23561
|
+
function Chi(a, b, c) {
|
|
23562
|
+
return a & b ^ ~a & c;
|
|
23563
|
+
}
|
|
23564
|
+
function Maj(a, b, c) {
|
|
23565
|
+
return a & b ^ a & c ^ b & c;
|
|
23566
|
+
}
|
|
23567
|
+
var HashMD = class extends Hash {
|
|
23568
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
23569
|
+
super();
|
|
23570
|
+
this.finished = false;
|
|
23571
|
+
this.length = 0;
|
|
23572
|
+
this.pos = 0;
|
|
23573
|
+
this.destroyed = false;
|
|
23574
|
+
this.blockLen = blockLen;
|
|
23575
|
+
this.outputLen = outputLen;
|
|
23576
|
+
this.padOffset = padOffset;
|
|
23577
|
+
this.isLE = isLE;
|
|
23578
|
+
this.buffer = new Uint8Array(blockLen);
|
|
23579
|
+
this.view = createView(this.buffer);
|
|
23580
|
+
}
|
|
23581
|
+
update(data) {
|
|
23582
|
+
aexists(this);
|
|
23583
|
+
data = toBytes(data);
|
|
23584
|
+
abytes(data);
|
|
23585
|
+
const { view: view2, buffer, blockLen } = this;
|
|
23586
|
+
const len = data.length;
|
|
23587
|
+
for (let pos = 0; pos < len; ) {
|
|
23588
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
23589
|
+
if (take === blockLen) {
|
|
23590
|
+
const dataView = createView(data);
|
|
23591
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
23592
|
+
this.process(dataView, pos);
|
|
23593
|
+
continue;
|
|
23594
|
+
}
|
|
23595
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
23596
|
+
this.pos += take;
|
|
23597
|
+
pos += take;
|
|
23598
|
+
if (this.pos === blockLen) {
|
|
23599
|
+
this.process(view2, 0);
|
|
23600
|
+
this.pos = 0;
|
|
23601
|
+
}
|
|
23602
|
+
}
|
|
23603
|
+
this.length += data.length;
|
|
23604
|
+
this.roundClean();
|
|
23605
|
+
return this;
|
|
23606
|
+
}
|
|
23607
|
+
digestInto(out) {
|
|
23608
|
+
aexists(this);
|
|
23609
|
+
aoutput(out, this);
|
|
23610
|
+
this.finished = true;
|
|
23611
|
+
const { buffer, view: view2, blockLen, isLE } = this;
|
|
23612
|
+
let { pos } = this;
|
|
23613
|
+
buffer[pos++] = 128;
|
|
23614
|
+
clean(this.buffer.subarray(pos));
|
|
23615
|
+
if (this.padOffset > blockLen - pos) {
|
|
23616
|
+
this.process(view2, 0);
|
|
23617
|
+
pos = 0;
|
|
23618
|
+
}
|
|
23619
|
+
for (let i = pos; i < blockLen; i++)
|
|
23620
|
+
buffer[i] = 0;
|
|
23621
|
+
setBigUint64(view2, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
23622
|
+
this.process(view2, 0);
|
|
23623
|
+
const oview = createView(out);
|
|
23624
|
+
const len = this.outputLen;
|
|
23625
|
+
if (len % 4)
|
|
23626
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
23627
|
+
const outLen = len / 4;
|
|
23628
|
+
const state = this.get();
|
|
23629
|
+
if (outLen > state.length)
|
|
23630
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
23631
|
+
for (let i = 0; i < outLen; i++)
|
|
23632
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
23633
|
+
}
|
|
23634
|
+
digest() {
|
|
23635
|
+
const { buffer, outputLen } = this;
|
|
23636
|
+
this.digestInto(buffer);
|
|
23637
|
+
const res = buffer.slice(0, outputLen);
|
|
23638
|
+
this.destroy();
|
|
23639
|
+
return res;
|
|
23640
|
+
}
|
|
23641
|
+
_cloneInto(to) {
|
|
23642
|
+
to || (to = new this.constructor());
|
|
23643
|
+
to.set(...this.get());
|
|
23644
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
23645
|
+
to.destroyed = destroyed;
|
|
23646
|
+
to.finished = finished;
|
|
23647
|
+
to.length = length;
|
|
23648
|
+
to.pos = pos;
|
|
23649
|
+
if (length % blockLen)
|
|
23650
|
+
to.buffer.set(buffer);
|
|
23651
|
+
return to;
|
|
23652
|
+
}
|
|
23653
|
+
clone() {
|
|
23654
|
+
return this._cloneInto();
|
|
23655
|
+
}
|
|
23656
|
+
};
|
|
23657
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
23658
|
+
1779033703,
|
|
23659
|
+
3144134277,
|
|
23660
|
+
1013904242,
|
|
23661
|
+
2773480762,
|
|
23662
|
+
1359893119,
|
|
23663
|
+
2600822924,
|
|
23664
|
+
528734635,
|
|
23665
|
+
1541459225
|
|
23666
|
+
]);
|
|
23667
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
23668
|
+
1779033703,
|
|
23669
|
+
4089235720,
|
|
23670
|
+
3144134277,
|
|
23671
|
+
2227873595,
|
|
23672
|
+
1013904242,
|
|
23673
|
+
4271175723,
|
|
23674
|
+
2773480762,
|
|
23675
|
+
1595750129,
|
|
23676
|
+
1359893119,
|
|
23677
|
+
2917565137,
|
|
23678
|
+
2600822924,
|
|
23679
|
+
725511199,
|
|
23680
|
+
528734635,
|
|
23681
|
+
4215389547,
|
|
23682
|
+
1541459225,
|
|
23683
|
+
327033209
|
|
23684
|
+
]);
|
|
23685
|
+
|
|
23686
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_u64.js
|
|
23687
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
23688
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
23689
|
+
function fromBig(n, le = false) {
|
|
23690
|
+
if (le)
|
|
23691
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
23692
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
23693
|
+
}
|
|
23694
|
+
function split(lst, le = false) {
|
|
23695
|
+
const len = lst.length;
|
|
23696
|
+
let Ah = new Uint32Array(len);
|
|
23697
|
+
let Al = new Uint32Array(len);
|
|
23698
|
+
for (let i = 0; i < len; i++) {
|
|
23699
|
+
const { h, l } = fromBig(lst[i], le);
|
|
23700
|
+
[Ah[i], Al[i]] = [h, l];
|
|
23701
|
+
}
|
|
23702
|
+
return [Ah, Al];
|
|
23703
|
+
}
|
|
23704
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
23705
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
23706
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
23707
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
23708
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
23709
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
23710
|
+
function add(Ah, Al, Bh, Bl) {
|
|
23711
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
23712
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
23713
|
+
}
|
|
23714
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
23715
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
23716
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
23717
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
23718
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
23719
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
23720
|
+
|
|
23721
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha2.js
|
|
23722
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
23723
|
+
1116352408,
|
|
23724
|
+
1899447441,
|
|
23725
|
+
3049323471,
|
|
23726
|
+
3921009573,
|
|
23727
|
+
961987163,
|
|
23728
|
+
1508970993,
|
|
23729
|
+
2453635748,
|
|
23730
|
+
2870763221,
|
|
23731
|
+
3624381080,
|
|
23732
|
+
310598401,
|
|
23733
|
+
607225278,
|
|
23734
|
+
1426881987,
|
|
23735
|
+
1925078388,
|
|
23736
|
+
2162078206,
|
|
23737
|
+
2614888103,
|
|
23738
|
+
3248222580,
|
|
23739
|
+
3835390401,
|
|
23740
|
+
4022224774,
|
|
23741
|
+
264347078,
|
|
23742
|
+
604807628,
|
|
23743
|
+
770255983,
|
|
23744
|
+
1249150122,
|
|
23745
|
+
1555081692,
|
|
23746
|
+
1996064986,
|
|
23747
|
+
2554220882,
|
|
23748
|
+
2821834349,
|
|
23749
|
+
2952996808,
|
|
23750
|
+
3210313671,
|
|
23751
|
+
3336571891,
|
|
23752
|
+
3584528711,
|
|
23753
|
+
113926993,
|
|
23754
|
+
338241895,
|
|
23755
|
+
666307205,
|
|
23756
|
+
773529912,
|
|
23757
|
+
1294757372,
|
|
23758
|
+
1396182291,
|
|
23759
|
+
1695183700,
|
|
23760
|
+
1986661051,
|
|
23761
|
+
2177026350,
|
|
23762
|
+
2456956037,
|
|
23763
|
+
2730485921,
|
|
23764
|
+
2820302411,
|
|
23765
|
+
3259730800,
|
|
23766
|
+
3345764771,
|
|
23767
|
+
3516065817,
|
|
23768
|
+
3600352804,
|
|
23769
|
+
4094571909,
|
|
23770
|
+
275423344,
|
|
23771
|
+
430227734,
|
|
23772
|
+
506948616,
|
|
23773
|
+
659060556,
|
|
23774
|
+
883997877,
|
|
23775
|
+
958139571,
|
|
23776
|
+
1322822218,
|
|
23777
|
+
1537002063,
|
|
23778
|
+
1747873779,
|
|
23779
|
+
1955562222,
|
|
23780
|
+
2024104815,
|
|
23781
|
+
2227730452,
|
|
23782
|
+
2361852424,
|
|
23783
|
+
2428436474,
|
|
23784
|
+
2756734187,
|
|
23785
|
+
3204031479,
|
|
23786
|
+
3329325298
|
|
23787
|
+
]);
|
|
23788
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
23789
|
+
var SHA256 = class extends HashMD {
|
|
23790
|
+
constructor(outputLen = 32) {
|
|
23791
|
+
super(64, outputLen, 8, false);
|
|
23792
|
+
this.A = SHA256_IV[0] | 0;
|
|
23793
|
+
this.B = SHA256_IV[1] | 0;
|
|
23794
|
+
this.C = SHA256_IV[2] | 0;
|
|
23795
|
+
this.D = SHA256_IV[3] | 0;
|
|
23796
|
+
this.E = SHA256_IV[4] | 0;
|
|
23797
|
+
this.F = SHA256_IV[5] | 0;
|
|
23798
|
+
this.G = SHA256_IV[6] | 0;
|
|
23799
|
+
this.H = SHA256_IV[7] | 0;
|
|
23800
|
+
}
|
|
23801
|
+
get() {
|
|
23802
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
23803
|
+
return [A, B, C, D, E, F, G, H];
|
|
23804
|
+
}
|
|
23805
|
+
// prettier-ignore
|
|
23806
|
+
set(A, B, C, D, E, F, G, H) {
|
|
23807
|
+
this.A = A | 0;
|
|
23808
|
+
this.B = B | 0;
|
|
23809
|
+
this.C = C | 0;
|
|
23810
|
+
this.D = D | 0;
|
|
23811
|
+
this.E = E | 0;
|
|
23812
|
+
this.F = F | 0;
|
|
23813
|
+
this.G = G | 0;
|
|
23814
|
+
this.H = H | 0;
|
|
23815
|
+
}
|
|
23816
|
+
process(view2, offset) {
|
|
23817
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
23818
|
+
SHA256_W[i] = view2.getUint32(offset, false);
|
|
23819
|
+
for (let i = 16; i < 64; i++) {
|
|
23820
|
+
const W15 = SHA256_W[i - 15];
|
|
23821
|
+
const W2 = SHA256_W[i - 2];
|
|
23822
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
23823
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
23824
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
23825
|
+
}
|
|
23826
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
23827
|
+
for (let i = 0; i < 64; i++) {
|
|
23828
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
23829
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
23830
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
23831
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
23832
|
+
H = G;
|
|
23833
|
+
G = F;
|
|
23834
|
+
F = E;
|
|
23835
|
+
E = D + T1 | 0;
|
|
23836
|
+
D = C;
|
|
23837
|
+
C = B;
|
|
23838
|
+
B = A;
|
|
23839
|
+
A = T1 + T2 | 0;
|
|
23840
|
+
}
|
|
23841
|
+
A = A + this.A | 0;
|
|
23842
|
+
B = B + this.B | 0;
|
|
23843
|
+
C = C + this.C | 0;
|
|
23844
|
+
D = D + this.D | 0;
|
|
23845
|
+
E = E + this.E | 0;
|
|
23846
|
+
F = F + this.F | 0;
|
|
23847
|
+
G = G + this.G | 0;
|
|
23848
|
+
H = H + this.H | 0;
|
|
23849
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
23850
|
+
}
|
|
23851
|
+
roundClean() {
|
|
23852
|
+
clean(SHA256_W);
|
|
23853
|
+
}
|
|
23854
|
+
destroy() {
|
|
23855
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
23856
|
+
clean(this.buffer);
|
|
23857
|
+
}
|
|
23858
|
+
};
|
|
23859
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
23860
|
+
"0x428a2f98d728ae22",
|
|
23861
|
+
"0x7137449123ef65cd",
|
|
23862
|
+
"0xb5c0fbcfec4d3b2f",
|
|
23863
|
+
"0xe9b5dba58189dbbc",
|
|
23864
|
+
"0x3956c25bf348b538",
|
|
23865
|
+
"0x59f111f1b605d019",
|
|
23866
|
+
"0x923f82a4af194f9b",
|
|
23867
|
+
"0xab1c5ed5da6d8118",
|
|
23868
|
+
"0xd807aa98a3030242",
|
|
23869
|
+
"0x12835b0145706fbe",
|
|
23870
|
+
"0x243185be4ee4b28c",
|
|
23871
|
+
"0x550c7dc3d5ffb4e2",
|
|
23872
|
+
"0x72be5d74f27b896f",
|
|
23873
|
+
"0x80deb1fe3b1696b1",
|
|
23874
|
+
"0x9bdc06a725c71235",
|
|
23875
|
+
"0xc19bf174cf692694",
|
|
23876
|
+
"0xe49b69c19ef14ad2",
|
|
23877
|
+
"0xefbe4786384f25e3",
|
|
23878
|
+
"0x0fc19dc68b8cd5b5",
|
|
23879
|
+
"0x240ca1cc77ac9c65",
|
|
23880
|
+
"0x2de92c6f592b0275",
|
|
23881
|
+
"0x4a7484aa6ea6e483",
|
|
23882
|
+
"0x5cb0a9dcbd41fbd4",
|
|
23883
|
+
"0x76f988da831153b5",
|
|
23884
|
+
"0x983e5152ee66dfab",
|
|
23885
|
+
"0xa831c66d2db43210",
|
|
23886
|
+
"0xb00327c898fb213f",
|
|
23887
|
+
"0xbf597fc7beef0ee4",
|
|
23888
|
+
"0xc6e00bf33da88fc2",
|
|
23889
|
+
"0xd5a79147930aa725",
|
|
23890
|
+
"0x06ca6351e003826f",
|
|
23891
|
+
"0x142929670a0e6e70",
|
|
23892
|
+
"0x27b70a8546d22ffc",
|
|
23893
|
+
"0x2e1b21385c26c926",
|
|
23894
|
+
"0x4d2c6dfc5ac42aed",
|
|
23895
|
+
"0x53380d139d95b3df",
|
|
23896
|
+
"0x650a73548baf63de",
|
|
23897
|
+
"0x766a0abb3c77b2a8",
|
|
23898
|
+
"0x81c2c92e47edaee6",
|
|
23899
|
+
"0x92722c851482353b",
|
|
23900
|
+
"0xa2bfe8a14cf10364",
|
|
23901
|
+
"0xa81a664bbc423001",
|
|
23902
|
+
"0xc24b8b70d0f89791",
|
|
23903
|
+
"0xc76c51a30654be30",
|
|
23904
|
+
"0xd192e819d6ef5218",
|
|
23905
|
+
"0xd69906245565a910",
|
|
23906
|
+
"0xf40e35855771202a",
|
|
23907
|
+
"0x106aa07032bbd1b8",
|
|
23908
|
+
"0x19a4c116b8d2d0c8",
|
|
23909
|
+
"0x1e376c085141ab53",
|
|
23910
|
+
"0x2748774cdf8eeb99",
|
|
23911
|
+
"0x34b0bcb5e19b48a8",
|
|
23912
|
+
"0x391c0cb3c5c95a63",
|
|
23913
|
+
"0x4ed8aa4ae3418acb",
|
|
23914
|
+
"0x5b9cca4f7763e373",
|
|
23915
|
+
"0x682e6ff3d6b2b8a3",
|
|
23916
|
+
"0x748f82ee5defb2fc",
|
|
23917
|
+
"0x78a5636f43172f60",
|
|
23918
|
+
"0x84c87814a1f0ab72",
|
|
23919
|
+
"0x8cc702081a6439ec",
|
|
23920
|
+
"0x90befffa23631e28",
|
|
23921
|
+
"0xa4506cebde82bde9",
|
|
23922
|
+
"0xbef9a3f7b2c67915",
|
|
23923
|
+
"0xc67178f2e372532b",
|
|
23924
|
+
"0xca273eceea26619c",
|
|
23925
|
+
"0xd186b8c721c0c207",
|
|
23926
|
+
"0xeada7dd6cde0eb1e",
|
|
23927
|
+
"0xf57d4f7fee6ed178",
|
|
23928
|
+
"0x06f067aa72176fba",
|
|
23929
|
+
"0x0a637dc5a2c898a6",
|
|
23930
|
+
"0x113f9804bef90dae",
|
|
23931
|
+
"0x1b710b35131c471b",
|
|
23932
|
+
"0x28db77f523047d84",
|
|
23933
|
+
"0x32caab7b40c72493",
|
|
23934
|
+
"0x3c9ebe0a15c9bebc",
|
|
23935
|
+
"0x431d67c49c100d4c",
|
|
23936
|
+
"0x4cc5d4becb3e42b6",
|
|
23937
|
+
"0x597f299cfc657e2a",
|
|
23938
|
+
"0x5fcb6fab3ad6faec",
|
|
23939
|
+
"0x6c44198c4a475817"
|
|
23940
|
+
].map((n) => BigInt(n))))();
|
|
23941
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
23942
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
23943
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
23944
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
23945
|
+
var SHA512 = class extends HashMD {
|
|
23946
|
+
constructor(outputLen = 64) {
|
|
23947
|
+
super(128, outputLen, 16, false);
|
|
23948
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
23949
|
+
this.Al = SHA512_IV[1] | 0;
|
|
23950
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
23951
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
23952
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
23953
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
23954
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
23955
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
23956
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
23957
|
+
this.El = SHA512_IV[9] | 0;
|
|
23958
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
23959
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
23960
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
23961
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
23962
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
23963
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
23964
|
+
}
|
|
23965
|
+
// prettier-ignore
|
|
23966
|
+
get() {
|
|
23967
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
23968
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
23969
|
+
}
|
|
23970
|
+
// prettier-ignore
|
|
23971
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
23972
|
+
this.Ah = Ah | 0;
|
|
23973
|
+
this.Al = Al | 0;
|
|
23974
|
+
this.Bh = Bh | 0;
|
|
23975
|
+
this.Bl = Bl | 0;
|
|
23976
|
+
this.Ch = Ch | 0;
|
|
23977
|
+
this.Cl = Cl | 0;
|
|
23978
|
+
this.Dh = Dh | 0;
|
|
23979
|
+
this.Dl = Dl | 0;
|
|
23980
|
+
this.Eh = Eh | 0;
|
|
23981
|
+
this.El = El | 0;
|
|
23982
|
+
this.Fh = Fh | 0;
|
|
23983
|
+
this.Fl = Fl | 0;
|
|
23984
|
+
this.Gh = Gh | 0;
|
|
23985
|
+
this.Gl = Gl | 0;
|
|
23986
|
+
this.Hh = Hh | 0;
|
|
23987
|
+
this.Hl = Hl | 0;
|
|
23988
|
+
}
|
|
23989
|
+
process(view2, offset) {
|
|
23990
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
23991
|
+
SHA512_W_H[i] = view2.getUint32(offset);
|
|
23992
|
+
SHA512_W_L[i] = view2.getUint32(offset += 4);
|
|
23993
|
+
}
|
|
23994
|
+
for (let i = 16; i < 80; i++) {
|
|
23995
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
23996
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
23997
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
23998
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
23999
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
24000
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
24001
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
24002
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
24003
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
24004
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
24005
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
24006
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
24007
|
+
}
|
|
24008
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
24009
|
+
for (let i = 0; i < 80; i++) {
|
|
24010
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
24011
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
24012
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
24013
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
24014
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
24015
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
24016
|
+
const T1l = T1ll | 0;
|
|
24017
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
24018
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
24019
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
24020
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
24021
|
+
Hh = Gh | 0;
|
|
24022
|
+
Hl = Gl | 0;
|
|
24023
|
+
Gh = Fh | 0;
|
|
24024
|
+
Gl = Fl | 0;
|
|
24025
|
+
Fh = Eh | 0;
|
|
24026
|
+
Fl = El | 0;
|
|
24027
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
24028
|
+
Dh = Ch | 0;
|
|
24029
|
+
Dl = Cl | 0;
|
|
24030
|
+
Ch = Bh | 0;
|
|
24031
|
+
Cl = Bl | 0;
|
|
24032
|
+
Bh = Ah | 0;
|
|
24033
|
+
Bl = Al | 0;
|
|
24034
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
24035
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
24036
|
+
Al = All | 0;
|
|
24037
|
+
}
|
|
24038
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
24039
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
24040
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
24041
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
24042
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
24043
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
24044
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
24045
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
24046
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
24047
|
+
}
|
|
24048
|
+
roundClean() {
|
|
24049
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
24050
|
+
}
|
|
24051
|
+
destroy() {
|
|
24052
|
+
clean(this.buffer);
|
|
24053
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
24054
|
+
}
|
|
24055
|
+
};
|
|
24056
|
+
var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
24057
|
+
var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
|
|
24058
|
+
|
|
24059
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha256.js
|
|
24060
|
+
var sha2562 = sha256;
|
|
24061
|
+
|
|
24062
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2.js
|
|
24063
|
+
var BCPV2_HEADER_LENGTH = 68;
|
|
24064
|
+
var BCPV2_SIGNATURE_LENGTH = 64;
|
|
24065
|
+
var BCPV2_MIN_RECORD_LENGTH = BCPV2_HEADER_LENGTH + 32 + BCPV2_SIGNATURE_LENGTH;
|
|
24066
|
+
var BCPV2_MAX_RECORD_LENGTH = 1316;
|
|
24067
|
+
var BCPV2_MAX_DIGESTS = 37;
|
|
24068
|
+
var BCPV2_HASH_ALG_SHA256 = 1;
|
|
24069
|
+
var text = new TextEncoder();
|
|
24070
|
+
var MAGIC = Uint8Array.from([77, 72, 77, 50]);
|
|
24071
|
+
var SIGNING_DOMAIN = text.encode("mcast-manifest-v2\0");
|
|
24072
|
+
function equalBytes(left, right) {
|
|
24073
|
+
if (left.byteLength !== right.byteLength)
|
|
24074
|
+
return false;
|
|
24075
|
+
let different = 0;
|
|
24076
|
+
for (let index = 0; index < left.byteLength; index++)
|
|
24077
|
+
different |= left[index] ^ right[index];
|
|
24078
|
+
return different === 0;
|
|
24079
|
+
}
|
|
24080
|
+
function u16(value) {
|
|
24081
|
+
const result = new Uint8Array(2);
|
|
24082
|
+
new DataView(result.buffer).setUint16(0, value);
|
|
24083
|
+
return result;
|
|
24084
|
+
}
|
|
24085
|
+
function u322(value) {
|
|
24086
|
+
const result = new Uint8Array(4);
|
|
24087
|
+
new DataView(result.buffer).setUint32(0, value);
|
|
24088
|
+
return result;
|
|
24089
|
+
}
|
|
24090
|
+
function validU32(value) {
|
|
24091
|
+
return Number.isInteger(value) && value >= 0 && value <= 4294967295;
|
|
24092
|
+
}
|
|
24093
|
+
function validPort(value) {
|
|
24094
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535;
|
|
24095
|
+
}
|
|
24096
|
+
function parseIpv4(address) {
|
|
24097
|
+
const fields = address.split(".");
|
|
24098
|
+
if (fields.length !== 4 || fields.some((field) => !/^\d+$/.test(field))) {
|
|
24099
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
24100
|
+
}
|
|
24101
|
+
const values = fields.map(Number);
|
|
24102
|
+
if (values.some((value, index) => value > 255 || String(value) !== fields[index])) {
|
|
24103
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
24104
|
+
}
|
|
24105
|
+
return Uint8Array.from(values);
|
|
24106
|
+
}
|
|
24107
|
+
function parseIpv6(address) {
|
|
24108
|
+
const compressed = address.includes("::");
|
|
24109
|
+
if ((address.match(/::/g) ?? []).length > 1)
|
|
24110
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24111
|
+
const [leftText, rightText = ""] = address.split("::");
|
|
24112
|
+
const left = leftText ? leftText.split(":") : [];
|
|
24113
|
+
const right = rightText ? rightText.split(":") : [];
|
|
24114
|
+
if (left.some((group) => group.includes(".")) || right.some((group) => group.includes("."))) {
|
|
24115
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24116
|
+
}
|
|
24117
|
+
const missing = 8 - left.length - right.length;
|
|
24118
|
+
if (missing < 0 || !compressed && missing !== 0 || compressed && missing < 1) {
|
|
24119
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24120
|
+
}
|
|
24121
|
+
const groups = [...left, ...Array.from({ length: missing }, () => "0"), ...right];
|
|
24122
|
+
if (groups.length !== 8 || groups.some((group) => !/^[0-9a-f]{1,4}$/i.test(group))) {
|
|
24123
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24124
|
+
}
|
|
24125
|
+
const result = new Uint8Array(16);
|
|
24126
|
+
const view2 = new DataView(result.buffer);
|
|
24127
|
+
groups.forEach((group, index) => view2.setUint16(index * 2, Number.parseInt(group, 16)));
|
|
24128
|
+
return result;
|
|
24129
|
+
}
|
|
24130
|
+
function parseBcProvenanceV2Address(address) {
|
|
24131
|
+
if (address.includes(":"))
|
|
24132
|
+
return parseIpv6(address);
|
|
24133
|
+
return parseIpv4(address);
|
|
24134
|
+
}
|
|
24135
|
+
function endpointBytes(endpoint) {
|
|
24136
|
+
if (!validPort(endpoint.port) || !validPort(endpoint.manifestPort) || !validU32(endpoint.streamId)) {
|
|
24137
|
+
throw new Error("invalid BCPV2 endpoint");
|
|
24138
|
+
}
|
|
24139
|
+
const source = parseBcProvenanceV2Address(endpoint.sourceAddress);
|
|
24140
|
+
const group = parseBcProvenanceV2Address(endpoint.groupAddress);
|
|
24141
|
+
if (source.byteLength !== group.byteLength)
|
|
24142
|
+
throw new Error("endpoint address families differ");
|
|
24143
|
+
return { source, group };
|
|
24144
|
+
}
|
|
24145
|
+
function readU32(view2, offset) {
|
|
24146
|
+
return view2.getUint32(offset);
|
|
24147
|
+
}
|
|
24148
|
+
function readU642(view2, offset) {
|
|
24149
|
+
return view2.getBigUint64(offset);
|
|
24150
|
+
}
|
|
24151
|
+
function decodeBcProvenanceV2Record(input) {
|
|
24152
|
+
const bytes = Uint8Array.from(input);
|
|
24153
|
+
if (bytes.byteLength < BCPV2_MIN_RECORD_LENGTH || bytes.byteLength > BCPV2_MAX_RECORD_LENGTH || (bytes.byteLength - 132) % 32 !== 0) {
|
|
24154
|
+
throw new Error("invalid BCPV2 record length");
|
|
24155
|
+
}
|
|
24156
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
24157
|
+
if (!equalBytes(bytes.subarray(0, 4), MAGIC))
|
|
24158
|
+
throw new Error("invalid BCPV2 record magic");
|
|
24159
|
+
const count = view2.getUint16(28);
|
|
24160
|
+
if (count < 1 || count > BCPV2_MAX_DIGESTS || bytes.byteLength !== 132 + count * 32) {
|
|
24161
|
+
throw new Error("invalid BCPV2 record count");
|
|
24162
|
+
}
|
|
24163
|
+
const body = bytes.slice(0, bytes.byteLength - BCPV2_SIGNATURE_LENGTH);
|
|
24164
|
+
const digests = [];
|
|
24165
|
+
for (let index = 0; index < count; index++) {
|
|
24166
|
+
digests.push(bytes.slice(BCPV2_HEADER_LENGTH + index * 32, BCPV2_HEADER_LENGTH + (index + 1) * 32));
|
|
24167
|
+
}
|
|
24168
|
+
return {
|
|
24169
|
+
kid: readU32(view2, 4),
|
|
24170
|
+
epoch: readU32(view2, 8),
|
|
24171
|
+
mseq: readU642(view2, 12),
|
|
24172
|
+
firstSeq: readU642(view2, 20),
|
|
24173
|
+
count,
|
|
24174
|
+
hashAlg: bytes[30],
|
|
24175
|
+
flags: bytes[31],
|
|
24176
|
+
sent: readU32(view2, 32),
|
|
24177
|
+
ctx: bytes.slice(36, 68),
|
|
24178
|
+
digests,
|
|
24179
|
+
signature: bytes.slice(bytes.byteLength - BCPV2_SIGNATURE_LENGTH),
|
|
24180
|
+
body,
|
|
24181
|
+
raw: bytes
|
|
24182
|
+
};
|
|
24183
|
+
}
|
|
24184
|
+
function bcProvenanceV2SignedMessage(record, endpoint) {
|
|
24185
|
+
const { source, group } = endpointBytes(endpoint);
|
|
24186
|
+
const prefix = new Uint8Array(SIGNING_DOMAIN.byteLength + 4 + source.byteLength + group.byteLength + 2);
|
|
24187
|
+
let offset = 0;
|
|
24188
|
+
prefix.set(SIGNING_DOMAIN, offset);
|
|
24189
|
+
offset += SIGNING_DOMAIN.byteLength;
|
|
24190
|
+
prefix.set(u322(endpoint.streamId), offset);
|
|
24191
|
+
offset += 4;
|
|
24192
|
+
prefix.set(source, offset);
|
|
24193
|
+
offset += source.byteLength;
|
|
24194
|
+
prefix.set(group, offset);
|
|
24195
|
+
offset += group.byteLength;
|
|
24196
|
+
prefix.set(u16(endpoint.manifestPort), offset);
|
|
24197
|
+
const message = new Uint8Array(prefix.byteLength + record.body.byteLength);
|
|
24198
|
+
message.set(prefix);
|
|
24199
|
+
message.set(record.body, prefix.byteLength);
|
|
24200
|
+
return message;
|
|
24201
|
+
}
|
|
24202
|
+
function bcProvenanceV2DatagramDigest(endpoint, payload) {
|
|
24203
|
+
if (payload.byteLength > 65535)
|
|
24204
|
+
throw new Error("datagram payload exceeds u16 length");
|
|
24205
|
+
const { source, group } = endpointBytes(endpoint);
|
|
24206
|
+
const pseudoheader = new Uint8Array(source.byteLength + group.byteLength + 2 + 2 + 2 + 2 + 4);
|
|
24207
|
+
let offset = 0;
|
|
24208
|
+
pseudoheader.set(source, offset);
|
|
24209
|
+
offset += source.byteLength;
|
|
24210
|
+
pseudoheader.set(group, offset);
|
|
24211
|
+
offset += group.byteLength;
|
|
24212
|
+
pseudoheader.set([0, 17], offset);
|
|
24213
|
+
offset += 2;
|
|
24214
|
+
pseudoheader.set(u16(payload.byteLength), offset);
|
|
24215
|
+
offset += 2;
|
|
24216
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
24217
|
+
offset += 2;
|
|
24218
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
24219
|
+
offset += 2;
|
|
24220
|
+
pseudoheader.set(u322(endpoint.streamId), offset);
|
|
24221
|
+
const input = new Uint8Array(pseudoheader.byteLength + payload.byteLength);
|
|
24222
|
+
input.set(pseudoheader);
|
|
24223
|
+
input.set(payload, pseudoheader.byteLength);
|
|
24224
|
+
return sha2562(input);
|
|
24225
|
+
}
|
|
24226
|
+
|
|
24227
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/utils.js
|
|
24228
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
24229
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
24230
|
+
function _abool2(value, title = "") {
|
|
24231
|
+
if (typeof value !== "boolean") {
|
|
24232
|
+
const prefix = title && `"${title}"`;
|
|
24233
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
24234
|
+
}
|
|
24235
|
+
return value;
|
|
24236
|
+
}
|
|
24237
|
+
function _abytes2(value, length, title = "") {
|
|
24238
|
+
const bytes = isBytes(value);
|
|
24239
|
+
const len = value?.length;
|
|
24240
|
+
const needsLen = length !== void 0;
|
|
24241
|
+
if (!bytes || needsLen && len !== length) {
|
|
24242
|
+
const prefix = title && `"${title}" `;
|
|
24243
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
24244
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
24245
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
24246
|
+
}
|
|
24247
|
+
return value;
|
|
24248
|
+
}
|
|
24249
|
+
function hexToNumber(hex3) {
|
|
24250
|
+
if (typeof hex3 !== "string")
|
|
24251
|
+
throw new Error("hex string expected, got " + typeof hex3);
|
|
24252
|
+
return hex3 === "" ? _0n : BigInt("0x" + hex3);
|
|
24253
|
+
}
|
|
24254
|
+
function bytesToNumberBE(bytes) {
|
|
24255
|
+
return hexToNumber(bytesToHex(bytes));
|
|
24256
|
+
}
|
|
24257
|
+
function bytesToNumberLE(bytes) {
|
|
24258
|
+
abytes(bytes);
|
|
24259
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
24260
|
+
}
|
|
24261
|
+
function numberToBytesBE(n, len) {
|
|
24262
|
+
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
24263
|
+
}
|
|
24264
|
+
function numberToBytesLE(n, len) {
|
|
24265
|
+
return numberToBytesBE(n, len).reverse();
|
|
24266
|
+
}
|
|
24267
|
+
function ensureBytes(title, hex3, expectedLength) {
|
|
24268
|
+
let res;
|
|
24269
|
+
if (typeof hex3 === "string") {
|
|
24270
|
+
try {
|
|
24271
|
+
res = hexToBytes(hex3);
|
|
24272
|
+
} catch (e) {
|
|
24273
|
+
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
24274
|
+
}
|
|
24275
|
+
} else if (isBytes(hex3)) {
|
|
24276
|
+
res = Uint8Array.from(hex3);
|
|
24277
|
+
} else {
|
|
24278
|
+
throw new Error(title + " must be hex string or Uint8Array");
|
|
24279
|
+
}
|
|
24280
|
+
const len = res.length;
|
|
24281
|
+
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
24282
|
+
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
24283
|
+
return res;
|
|
24284
|
+
}
|
|
24285
|
+
function equalBytes2(a, b) {
|
|
24286
|
+
if (a.length !== b.length)
|
|
24287
|
+
return false;
|
|
24288
|
+
let diff = 0;
|
|
24289
|
+
for (let i = 0; i < a.length; i++)
|
|
24290
|
+
diff |= a[i] ^ b[i];
|
|
24291
|
+
return diff === 0;
|
|
24292
|
+
}
|
|
24293
|
+
function copyBytes(bytes) {
|
|
24294
|
+
return Uint8Array.from(bytes);
|
|
24295
|
+
}
|
|
24296
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
24297
|
+
function inRange(n, min, max) {
|
|
24298
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
24299
|
+
}
|
|
24300
|
+
function aInRange(title, n, min, max) {
|
|
24301
|
+
if (!inRange(n, min, max))
|
|
24302
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
24303
|
+
}
|
|
24304
|
+
function bitLen(n) {
|
|
24305
|
+
let len;
|
|
24306
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
24307
|
+
;
|
|
24308
|
+
return len;
|
|
24309
|
+
}
|
|
24310
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
24311
|
+
function _validateObject(object2, fields, optFields = {}) {
|
|
24312
|
+
if (!object2 || typeof object2 !== "object")
|
|
24313
|
+
throw new Error("expected valid options object");
|
|
24314
|
+
function checkField2(fieldName, expectedType, isOpt) {
|
|
24315
|
+
const val = object2[fieldName];
|
|
24316
|
+
if (isOpt && val === void 0)
|
|
24317
|
+
return;
|
|
24318
|
+
const current = typeof val;
|
|
24319
|
+
if (current !== expectedType || val === null)
|
|
24320
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
24321
|
+
}
|
|
24322
|
+
Object.entries(fields).forEach(([k, v]) => checkField2(k, v, false));
|
|
24323
|
+
Object.entries(optFields).forEach(([k, v]) => checkField2(k, v, true));
|
|
24324
|
+
}
|
|
24325
|
+
var notImplemented = () => {
|
|
24326
|
+
throw new Error("not implemented");
|
|
24327
|
+
};
|
|
24328
|
+
function memoized(fn) {
|
|
24329
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
24330
|
+
return (arg, ...args) => {
|
|
24331
|
+
const val = map.get(arg);
|
|
24332
|
+
if (val !== void 0)
|
|
24333
|
+
return val;
|
|
24334
|
+
const computed = fn(arg, ...args);
|
|
24335
|
+
map.set(arg, computed);
|
|
24336
|
+
return computed;
|
|
24337
|
+
};
|
|
24338
|
+
}
|
|
24339
|
+
|
|
24340
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/modular.js
|
|
24341
|
+
var _0n2 = BigInt(0);
|
|
24342
|
+
var _1n2 = BigInt(1);
|
|
24343
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
24344
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
24345
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
24346
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
24347
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
24348
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
24349
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
24350
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
24351
|
+
function mod(a, b) {
|
|
24352
|
+
const result = a % b;
|
|
24353
|
+
return result >= _0n2 ? result : b + result;
|
|
24354
|
+
}
|
|
24355
|
+
function pow2(x, power, modulo) {
|
|
24356
|
+
let res = x;
|
|
24357
|
+
while (power-- > _0n2) {
|
|
24358
|
+
res *= res;
|
|
24359
|
+
res %= modulo;
|
|
24360
|
+
}
|
|
24361
|
+
return res;
|
|
24362
|
+
}
|
|
24363
|
+
function invert(number, modulo) {
|
|
24364
|
+
if (number === _0n2)
|
|
24365
|
+
throw new Error("invert: expected non-zero number");
|
|
24366
|
+
if (modulo <= _0n2)
|
|
24367
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
24368
|
+
let a = mod(number, modulo);
|
|
24369
|
+
let b = modulo;
|
|
24370
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
24371
|
+
while (a !== _0n2) {
|
|
24372
|
+
const q = b / a;
|
|
24373
|
+
const r = b % a;
|
|
24374
|
+
const m = x - u * q;
|
|
24375
|
+
const n = y - v * q;
|
|
24376
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
24377
|
+
}
|
|
24378
|
+
const gcd = b;
|
|
24379
|
+
if (gcd !== _1n2)
|
|
24380
|
+
throw new Error("invert: does not exist");
|
|
24381
|
+
return mod(x, modulo);
|
|
24382
|
+
}
|
|
24383
|
+
function assertIsSquare(Fp2, root, n) {
|
|
24384
|
+
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
24385
|
+
throw new Error("Cannot find square root");
|
|
24386
|
+
}
|
|
24387
|
+
function sqrt3mod4(Fp2, n) {
|
|
24388
|
+
const p1div4 = (Fp2.ORDER + _1n2) / _4n;
|
|
24389
|
+
const root = Fp2.pow(n, p1div4);
|
|
24390
|
+
assertIsSquare(Fp2, root, n);
|
|
24391
|
+
return root;
|
|
24392
|
+
}
|
|
24393
|
+
function sqrt5mod8(Fp2, n) {
|
|
24394
|
+
const p5div8 = (Fp2.ORDER - _5n) / _8n;
|
|
24395
|
+
const n2 = Fp2.mul(n, _2n);
|
|
24396
|
+
const v = Fp2.pow(n2, p5div8);
|
|
24397
|
+
const nv = Fp2.mul(n, v);
|
|
24398
|
+
const i = Fp2.mul(Fp2.mul(nv, _2n), v);
|
|
24399
|
+
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
24400
|
+
assertIsSquare(Fp2, root, n);
|
|
24401
|
+
return root;
|
|
24402
|
+
}
|
|
24403
|
+
function sqrt9mod16(P) {
|
|
24404
|
+
const Fp_ = Field(P);
|
|
24405
|
+
const tn = tonelliShanks(P);
|
|
24406
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
24407
|
+
const c2 = tn(Fp_, c1);
|
|
24408
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
24409
|
+
const c4 = (P + _7n) / _16n;
|
|
24410
|
+
return (Fp2, n) => {
|
|
24411
|
+
let tv1 = Fp2.pow(n, c4);
|
|
24412
|
+
let tv2 = Fp2.mul(tv1, c1);
|
|
24413
|
+
const tv3 = Fp2.mul(tv1, c2);
|
|
24414
|
+
const tv4 = Fp2.mul(tv1, c3);
|
|
24415
|
+
const e1 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
24416
|
+
const e2 = Fp2.eql(Fp2.sqr(tv3), n);
|
|
24417
|
+
tv1 = Fp2.cmov(tv1, tv2, e1);
|
|
24418
|
+
tv2 = Fp2.cmov(tv4, tv3, e2);
|
|
24419
|
+
const e3 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
24420
|
+
const root = Fp2.cmov(tv1, tv2, e3);
|
|
24421
|
+
assertIsSquare(Fp2, root, n);
|
|
24422
|
+
return root;
|
|
24423
|
+
};
|
|
24424
|
+
}
|
|
24425
|
+
function tonelliShanks(P) {
|
|
24426
|
+
if (P < _3n)
|
|
24427
|
+
throw new Error("sqrt is not defined for small field");
|
|
24428
|
+
let Q = P - _1n2;
|
|
24429
|
+
let S = 0;
|
|
24430
|
+
while (Q % _2n === _0n2) {
|
|
24431
|
+
Q /= _2n;
|
|
24432
|
+
S++;
|
|
24433
|
+
}
|
|
24434
|
+
let Z = _2n;
|
|
24435
|
+
const _Fp = Field(P);
|
|
24436
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
24437
|
+
if (Z++ > 1e3)
|
|
24438
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
24439
|
+
}
|
|
24440
|
+
if (S === 1)
|
|
24441
|
+
return sqrt3mod4;
|
|
24442
|
+
let cc = _Fp.pow(Z, Q);
|
|
24443
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
24444
|
+
return function tonelliSlow(Fp2, n) {
|
|
24445
|
+
if (Fp2.is0(n))
|
|
24446
|
+
return n;
|
|
24447
|
+
if (FpLegendre(Fp2, n) !== 1)
|
|
24448
|
+
throw new Error("Cannot find square root");
|
|
24449
|
+
let M = S;
|
|
24450
|
+
let c = Fp2.mul(Fp2.ONE, cc);
|
|
24451
|
+
let t = Fp2.pow(n, Q);
|
|
24452
|
+
let R = Fp2.pow(n, Q1div2);
|
|
24453
|
+
while (!Fp2.eql(t, Fp2.ONE)) {
|
|
24454
|
+
if (Fp2.is0(t))
|
|
24455
|
+
return Fp2.ZERO;
|
|
24456
|
+
let i = 1;
|
|
24457
|
+
let t_tmp = Fp2.sqr(t);
|
|
24458
|
+
while (!Fp2.eql(t_tmp, Fp2.ONE)) {
|
|
24459
|
+
i++;
|
|
24460
|
+
t_tmp = Fp2.sqr(t_tmp);
|
|
24461
|
+
if (i === M)
|
|
24462
|
+
throw new Error("Cannot find square root");
|
|
24463
|
+
}
|
|
24464
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
24465
|
+
const b = Fp2.pow(c, exponent);
|
|
24466
|
+
M = i;
|
|
24467
|
+
c = Fp2.sqr(b);
|
|
24468
|
+
t = Fp2.mul(t, c);
|
|
24469
|
+
R = Fp2.mul(R, b);
|
|
24470
|
+
}
|
|
24471
|
+
return R;
|
|
24472
|
+
};
|
|
24473
|
+
}
|
|
24474
|
+
function FpSqrt(P) {
|
|
24475
|
+
if (P % _4n === _3n)
|
|
24476
|
+
return sqrt3mod4;
|
|
24477
|
+
if (P % _8n === _5n)
|
|
24478
|
+
return sqrt5mod8;
|
|
24479
|
+
if (P % _16n === _9n)
|
|
24480
|
+
return sqrt9mod16(P);
|
|
24481
|
+
return tonelliShanks(P);
|
|
24482
|
+
}
|
|
24483
|
+
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
24484
|
+
var FIELD_FIELDS = [
|
|
24485
|
+
"create",
|
|
24486
|
+
"isValid",
|
|
24487
|
+
"is0",
|
|
24488
|
+
"neg",
|
|
24489
|
+
"inv",
|
|
24490
|
+
"sqrt",
|
|
24491
|
+
"sqr",
|
|
24492
|
+
"eql",
|
|
24493
|
+
"add",
|
|
24494
|
+
"sub",
|
|
24495
|
+
"mul",
|
|
24496
|
+
"pow",
|
|
24497
|
+
"div",
|
|
24498
|
+
"addN",
|
|
24499
|
+
"subN",
|
|
24500
|
+
"mulN",
|
|
24501
|
+
"sqrN"
|
|
24502
|
+
];
|
|
24503
|
+
function validateField(field) {
|
|
24504
|
+
const initial = {
|
|
24505
|
+
ORDER: "bigint",
|
|
24506
|
+
MASK: "bigint",
|
|
24507
|
+
BYTES: "number",
|
|
24508
|
+
BITS: "number"
|
|
24509
|
+
};
|
|
24510
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
24511
|
+
map[val] = "function";
|
|
24512
|
+
return map;
|
|
24513
|
+
}, initial);
|
|
24514
|
+
_validateObject(field, opts);
|
|
24515
|
+
return field;
|
|
24516
|
+
}
|
|
24517
|
+
function FpPow(Fp2, num, power) {
|
|
24518
|
+
if (power < _0n2)
|
|
24519
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
24520
|
+
if (power === _0n2)
|
|
24521
|
+
return Fp2.ONE;
|
|
24522
|
+
if (power === _1n2)
|
|
24523
|
+
return num;
|
|
24524
|
+
let p = Fp2.ONE;
|
|
24525
|
+
let d = num;
|
|
24526
|
+
while (power > _0n2) {
|
|
24527
|
+
if (power & _1n2)
|
|
24528
|
+
p = Fp2.mul(p, d);
|
|
24529
|
+
d = Fp2.sqr(d);
|
|
24530
|
+
power >>= _1n2;
|
|
24531
|
+
}
|
|
24532
|
+
return p;
|
|
24533
|
+
}
|
|
24534
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
24535
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
|
|
24536
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
24537
|
+
if (Fp2.is0(num))
|
|
24538
|
+
return acc;
|
|
24539
|
+
inverted[i] = acc;
|
|
24540
|
+
return Fp2.mul(acc, num);
|
|
24541
|
+
}, Fp2.ONE);
|
|
24542
|
+
const invertedAcc = Fp2.inv(multipliedAcc);
|
|
24543
|
+
nums.reduceRight((acc, num, i) => {
|
|
24544
|
+
if (Fp2.is0(num))
|
|
24545
|
+
return acc;
|
|
24546
|
+
inverted[i] = Fp2.mul(acc, inverted[i]);
|
|
24547
|
+
return Fp2.mul(acc, num);
|
|
24548
|
+
}, invertedAcc);
|
|
24549
|
+
return inverted;
|
|
24550
|
+
}
|
|
24551
|
+
function FpLegendre(Fp2, n) {
|
|
24552
|
+
const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
|
|
24553
|
+
const powered = Fp2.pow(n, p1mod2);
|
|
24554
|
+
const yes = Fp2.eql(powered, Fp2.ONE);
|
|
24555
|
+
const zero = Fp2.eql(powered, Fp2.ZERO);
|
|
24556
|
+
const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
|
|
24557
|
+
if (!yes && !zero && !no)
|
|
24558
|
+
throw new Error("invalid Legendre symbol result");
|
|
24559
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
24560
|
+
}
|
|
24561
|
+
function nLength(n, nBitLength) {
|
|
24562
|
+
if (nBitLength !== void 0)
|
|
24563
|
+
anumber(nBitLength);
|
|
24564
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
24565
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
24566
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
24567
|
+
}
|
|
24568
|
+
function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
|
|
24569
|
+
if (ORDER <= _0n2)
|
|
24570
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
24571
|
+
let _nbitLength = void 0;
|
|
24572
|
+
let _sqrt = void 0;
|
|
24573
|
+
let modFromBytes = false;
|
|
24574
|
+
let allowedLengths = void 0;
|
|
24575
|
+
if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
|
|
24576
|
+
if (opts.sqrt || isLE)
|
|
24577
|
+
throw new Error("cannot specify opts in two arguments");
|
|
24578
|
+
const _opts = bitLenOrOpts;
|
|
24579
|
+
if (_opts.BITS)
|
|
24580
|
+
_nbitLength = _opts.BITS;
|
|
24581
|
+
if (_opts.sqrt)
|
|
24582
|
+
_sqrt = _opts.sqrt;
|
|
24583
|
+
if (typeof _opts.isLE === "boolean")
|
|
24584
|
+
isLE = _opts.isLE;
|
|
24585
|
+
if (typeof _opts.modFromBytes === "boolean")
|
|
24586
|
+
modFromBytes = _opts.modFromBytes;
|
|
24587
|
+
allowedLengths = _opts.allowedLengths;
|
|
24588
|
+
} else {
|
|
24589
|
+
if (typeof bitLenOrOpts === "number")
|
|
24590
|
+
_nbitLength = bitLenOrOpts;
|
|
24591
|
+
if (opts.sqrt)
|
|
24592
|
+
_sqrt = opts.sqrt;
|
|
24593
|
+
}
|
|
24594
|
+
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);
|
|
24595
|
+
if (BYTES > 2048)
|
|
24596
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
24597
|
+
let sqrtP;
|
|
24598
|
+
const f = Object.freeze({
|
|
24599
|
+
ORDER,
|
|
24600
|
+
isLE,
|
|
24601
|
+
BITS,
|
|
24602
|
+
BYTES,
|
|
24603
|
+
MASK: bitMask(BITS),
|
|
24604
|
+
ZERO: _0n2,
|
|
24605
|
+
ONE: _1n2,
|
|
24606
|
+
allowedLengths,
|
|
24607
|
+
create: (num) => mod(num, ORDER),
|
|
24608
|
+
isValid: (num) => {
|
|
24609
|
+
if (typeof num !== "bigint")
|
|
24610
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
24611
|
+
return _0n2 <= num && num < ORDER;
|
|
24612
|
+
},
|
|
24613
|
+
is0: (num) => num === _0n2,
|
|
24614
|
+
// is valid and invertible
|
|
24615
|
+
isValidNot0: (num) => !f.is0(num) && f.isValid(num),
|
|
24616
|
+
isOdd: (num) => (num & _1n2) === _1n2,
|
|
24617
|
+
neg: (num) => mod(-num, ORDER),
|
|
24618
|
+
eql: (lhs, rhs) => lhs === rhs,
|
|
24619
|
+
sqr: (num) => mod(num * num, ORDER),
|
|
24620
|
+
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
24621
|
+
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
24622
|
+
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
24623
|
+
pow: (num, power) => FpPow(f, num, power),
|
|
24624
|
+
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
24625
|
+
// Same as above, but doesn't normalize
|
|
24626
|
+
sqrN: (num) => num * num,
|
|
24627
|
+
addN: (lhs, rhs) => lhs + rhs,
|
|
24628
|
+
subN: (lhs, rhs) => lhs - rhs,
|
|
24629
|
+
mulN: (lhs, rhs) => lhs * rhs,
|
|
24630
|
+
inv: (num) => invert(num, ORDER),
|
|
24631
|
+
sqrt: _sqrt || ((n) => {
|
|
24632
|
+
if (!sqrtP)
|
|
24633
|
+
sqrtP = FpSqrt(ORDER);
|
|
24634
|
+
return sqrtP(f, n);
|
|
24635
|
+
}),
|
|
24636
|
+
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
24637
|
+
fromBytes: (bytes, skipValidation = true) => {
|
|
24638
|
+
if (allowedLengths) {
|
|
24639
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
24640
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
24641
|
+
}
|
|
24642
|
+
const padded = new Uint8Array(BYTES);
|
|
24643
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
24644
|
+
bytes = padded;
|
|
24645
|
+
}
|
|
24646
|
+
if (bytes.length !== BYTES)
|
|
24647
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
24648
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
24649
|
+
if (modFromBytes)
|
|
24650
|
+
scalar = mod(scalar, ORDER);
|
|
24651
|
+
if (!skipValidation) {
|
|
24652
|
+
if (!f.isValid(scalar))
|
|
24653
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
24654
|
+
}
|
|
24655
|
+
return scalar;
|
|
24656
|
+
},
|
|
24657
|
+
// TODO: we don't need it here, move out to separate fn
|
|
24658
|
+
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
24659
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
24660
|
+
// and it's unclear what to return in there.
|
|
24661
|
+
cmov: (a, b, c) => c ? b : a
|
|
24662
|
+
});
|
|
24663
|
+
return Object.freeze(f);
|
|
24664
|
+
}
|
|
24665
|
+
|
|
24666
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/curve.js
|
|
24667
|
+
var _0n3 = BigInt(0);
|
|
24668
|
+
var _1n3 = BigInt(1);
|
|
24669
|
+
function negateCt(condition, item) {
|
|
24670
|
+
const neg = item.negate();
|
|
24671
|
+
return condition ? neg : item;
|
|
24672
|
+
}
|
|
24673
|
+
function normalizeZ(c, points) {
|
|
24674
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
24675
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
24676
|
+
}
|
|
24677
|
+
function validateW(W, bits) {
|
|
24678
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
24679
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
24680
|
+
}
|
|
24681
|
+
function calcWOpts(W, scalarBits) {
|
|
24682
|
+
validateW(W, scalarBits);
|
|
24683
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
24684
|
+
const windowSize = 2 ** (W - 1);
|
|
24685
|
+
const maxNumber = 2 ** W;
|
|
24686
|
+
const mask = bitMask(W);
|
|
24687
|
+
const shiftBy = BigInt(W);
|
|
24688
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
24689
|
+
}
|
|
24690
|
+
function calcOffsets(n, window2, wOpts) {
|
|
24691
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
24692
|
+
let wbits = Number(n & mask);
|
|
24693
|
+
let nextN = n >> shiftBy;
|
|
24694
|
+
if (wbits > windowSize) {
|
|
24695
|
+
wbits -= maxNumber;
|
|
24696
|
+
nextN += _1n3;
|
|
24697
|
+
}
|
|
24698
|
+
const offsetStart = window2 * windowSize;
|
|
24699
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
24700
|
+
const isZero = wbits === 0;
|
|
24701
|
+
const isNeg = wbits < 0;
|
|
24702
|
+
const isNegF = window2 % 2 !== 0;
|
|
24703
|
+
const offsetF = offsetStart;
|
|
24704
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
24705
|
+
}
|
|
24706
|
+
function validateMSMPoints(points, c) {
|
|
24707
|
+
if (!Array.isArray(points))
|
|
24708
|
+
throw new Error("array expected");
|
|
24709
|
+
points.forEach((p, i) => {
|
|
24710
|
+
if (!(p instanceof c))
|
|
24711
|
+
throw new Error("invalid point at index " + i);
|
|
24712
|
+
});
|
|
24713
|
+
}
|
|
24714
|
+
function validateMSMScalars(scalars, field) {
|
|
24715
|
+
if (!Array.isArray(scalars))
|
|
24716
|
+
throw new Error("array of scalars expected");
|
|
24717
|
+
scalars.forEach((s, i) => {
|
|
24718
|
+
if (!field.isValid(s))
|
|
24719
|
+
throw new Error("invalid scalar at index " + i);
|
|
24720
|
+
});
|
|
24721
|
+
}
|
|
24722
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
24723
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
24724
|
+
function getW(P) {
|
|
24725
|
+
return pointWindowSizes.get(P) || 1;
|
|
24726
|
+
}
|
|
24727
|
+
function assert0(n) {
|
|
24728
|
+
if (n !== _0n3)
|
|
24729
|
+
throw new Error("invalid wNAF");
|
|
24730
|
+
}
|
|
24731
|
+
var wNAF = class {
|
|
24732
|
+
// Parametrized with a given Point class (not individual point)
|
|
24733
|
+
constructor(Point, bits) {
|
|
24734
|
+
this.BASE = Point.BASE;
|
|
24735
|
+
this.ZERO = Point.ZERO;
|
|
24736
|
+
this.Fn = Point.Fn;
|
|
24737
|
+
this.bits = bits;
|
|
24738
|
+
}
|
|
24739
|
+
// non-const time multiplication ladder
|
|
24740
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
24741
|
+
let d = elm;
|
|
24742
|
+
while (n > _0n3) {
|
|
24743
|
+
if (n & _1n3)
|
|
24744
|
+
p = p.add(d);
|
|
24745
|
+
d = d.double();
|
|
24746
|
+
n >>= _1n3;
|
|
24747
|
+
}
|
|
24748
|
+
return p;
|
|
24749
|
+
}
|
|
24750
|
+
/**
|
|
24751
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
24752
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
24753
|
+
* Number of precomputed points depends on the curve size:
|
|
24754
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
24755
|
+
* - 𝑊 is the window size
|
|
24756
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
24757
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
24758
|
+
* @param point Point instance
|
|
24759
|
+
* @param W window size
|
|
24760
|
+
* @returns precomputed point tables flattened to a single array
|
|
24761
|
+
*/
|
|
24762
|
+
precomputeWindow(point, W) {
|
|
24763
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
24764
|
+
const points = [];
|
|
24765
|
+
let p = point;
|
|
24766
|
+
let base = p;
|
|
24767
|
+
for (let window2 = 0; window2 < windows; window2++) {
|
|
24768
|
+
base = p;
|
|
24769
|
+
points.push(base);
|
|
24770
|
+
for (let i = 1; i < windowSize; i++) {
|
|
24771
|
+
base = base.add(p);
|
|
24772
|
+
points.push(base);
|
|
24773
|
+
}
|
|
24774
|
+
p = base.double();
|
|
24775
|
+
}
|
|
24776
|
+
return points;
|
|
24777
|
+
}
|
|
24778
|
+
/**
|
|
24779
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
24780
|
+
* More compact implementation:
|
|
24781
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
24782
|
+
* @returns real and fake (for const-time) points
|
|
24783
|
+
*/
|
|
24784
|
+
wNAF(W, precomputes, n) {
|
|
24785
|
+
if (!this.Fn.isValid(n))
|
|
24786
|
+
throw new Error("invalid scalar");
|
|
24787
|
+
let p = this.ZERO;
|
|
24788
|
+
let f = this.BASE;
|
|
24789
|
+
const wo = calcWOpts(W, this.bits);
|
|
24790
|
+
for (let window2 = 0; window2 < wo.windows; window2++) {
|
|
24791
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window2, wo);
|
|
24792
|
+
n = nextN;
|
|
24793
|
+
if (isZero) {
|
|
24794
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
24795
|
+
} else {
|
|
24796
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
24797
|
+
}
|
|
24798
|
+
}
|
|
24799
|
+
assert0(n);
|
|
24800
|
+
return { p, f };
|
|
24801
|
+
}
|
|
24802
|
+
/**
|
|
24803
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
24804
|
+
* @param acc accumulator point to add result of multiplication
|
|
24805
|
+
* @returns point
|
|
24806
|
+
*/
|
|
24807
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
24808
|
+
const wo = calcWOpts(W, this.bits);
|
|
24809
|
+
for (let window2 = 0; window2 < wo.windows; window2++) {
|
|
24810
|
+
if (n === _0n3)
|
|
24811
|
+
break;
|
|
24812
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window2, wo);
|
|
24813
|
+
n = nextN;
|
|
24814
|
+
if (isZero) {
|
|
24815
|
+
continue;
|
|
24816
|
+
} else {
|
|
24817
|
+
const item = precomputes[offset];
|
|
24818
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
24819
|
+
}
|
|
24820
|
+
}
|
|
24821
|
+
assert0(n);
|
|
24822
|
+
return acc;
|
|
24823
|
+
}
|
|
24824
|
+
getPrecomputes(W, point, transform) {
|
|
24825
|
+
let comp = pointPrecomputes.get(point);
|
|
24826
|
+
if (!comp) {
|
|
24827
|
+
comp = this.precomputeWindow(point, W);
|
|
24828
|
+
if (W !== 1) {
|
|
24829
|
+
if (typeof transform === "function")
|
|
24830
|
+
comp = transform(comp);
|
|
24831
|
+
pointPrecomputes.set(point, comp);
|
|
24832
|
+
}
|
|
24833
|
+
}
|
|
24834
|
+
return comp;
|
|
24835
|
+
}
|
|
24836
|
+
cached(point, scalar, transform) {
|
|
24837
|
+
const W = getW(point);
|
|
24838
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
24839
|
+
}
|
|
24840
|
+
unsafe(point, scalar, transform, prev) {
|
|
24841
|
+
const W = getW(point);
|
|
24842
|
+
if (W === 1)
|
|
24843
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
24844
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
24845
|
+
}
|
|
24846
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
24847
|
+
// using windowed method. This specifies window size and
|
|
24848
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
24849
|
+
createCache(P, W) {
|
|
24850
|
+
validateW(W, this.bits);
|
|
24851
|
+
pointWindowSizes.set(P, W);
|
|
24852
|
+
pointPrecomputes.delete(P);
|
|
24853
|
+
}
|
|
24854
|
+
hasCache(elm) {
|
|
24855
|
+
return getW(elm) !== 1;
|
|
24856
|
+
}
|
|
24857
|
+
};
|
|
24858
|
+
function pippenger(c, fieldN, points, scalars) {
|
|
24859
|
+
validateMSMPoints(points, c);
|
|
24860
|
+
validateMSMScalars(scalars, fieldN);
|
|
24861
|
+
const plength = points.length;
|
|
24862
|
+
const slength = scalars.length;
|
|
24863
|
+
if (plength !== slength)
|
|
24864
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
24865
|
+
const zero = c.ZERO;
|
|
24866
|
+
const wbits = bitLen(BigInt(plength));
|
|
24867
|
+
let windowSize = 1;
|
|
24868
|
+
if (wbits > 12)
|
|
24869
|
+
windowSize = wbits - 3;
|
|
24870
|
+
else if (wbits > 4)
|
|
24871
|
+
windowSize = wbits - 2;
|
|
24872
|
+
else if (wbits > 0)
|
|
24873
|
+
windowSize = 2;
|
|
24874
|
+
const MASK = bitMask(windowSize);
|
|
24875
|
+
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
24876
|
+
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
24877
|
+
let sum = zero;
|
|
24878
|
+
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
24879
|
+
buckets.fill(zero);
|
|
24880
|
+
for (let j = 0; j < slength; j++) {
|
|
24881
|
+
const scalar = scalars[j];
|
|
24882
|
+
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
|
24883
|
+
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
24884
|
+
}
|
|
24885
|
+
let resI = zero;
|
|
24886
|
+
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
24887
|
+
sumI = sumI.add(buckets[j]);
|
|
24888
|
+
resI = resI.add(sumI);
|
|
24889
|
+
}
|
|
24890
|
+
sum = sum.add(resI);
|
|
24891
|
+
if (i !== 0)
|
|
24892
|
+
for (let j = 0; j < windowSize; j++)
|
|
24893
|
+
sum = sum.double();
|
|
24894
|
+
}
|
|
24895
|
+
return sum;
|
|
24896
|
+
}
|
|
24897
|
+
function createField(order, field, isLE) {
|
|
24898
|
+
if (field) {
|
|
24899
|
+
if (field.ORDER !== order)
|
|
24900
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
24901
|
+
validateField(field);
|
|
24902
|
+
return field;
|
|
24903
|
+
} else {
|
|
24904
|
+
return Field(order, { isLE });
|
|
24905
|
+
}
|
|
24906
|
+
}
|
|
24907
|
+
function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
24908
|
+
if (FpFnLE === void 0)
|
|
24909
|
+
FpFnLE = type === "edwards";
|
|
24910
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
24911
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
24912
|
+
for (const p of ["p", "n", "h"]) {
|
|
24913
|
+
const val = CURVE[p];
|
|
24914
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
24915
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
24916
|
+
}
|
|
24917
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
24918
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
24919
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
24920
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
24921
|
+
for (const p of params) {
|
|
24922
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
24923
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
24924
|
+
}
|
|
24925
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
24926
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
24927
|
+
}
|
|
24928
|
+
|
|
24929
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/edwards.js
|
|
24930
|
+
var _0n4 = BigInt(0);
|
|
24931
|
+
var _1n4 = BigInt(1);
|
|
24932
|
+
var _2n2 = BigInt(2);
|
|
24933
|
+
var _8n2 = BigInt(8);
|
|
24934
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
24935
|
+
const x2 = Fp2.sqr(x);
|
|
24936
|
+
const y2 = Fp2.sqr(y);
|
|
24937
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
24938
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
24939
|
+
return Fp2.eql(left, right);
|
|
24940
|
+
}
|
|
24941
|
+
function edwards(params, extraOpts = {}) {
|
|
24942
|
+
const validated = _createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
24943
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
24944
|
+
let CURVE = validated.CURVE;
|
|
24945
|
+
const { h: cofactor } = CURVE;
|
|
24946
|
+
_validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
24947
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
24948
|
+
const modP = (n) => Fp2.create(n);
|
|
24949
|
+
const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
|
|
24950
|
+
try {
|
|
24951
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
24952
|
+
} catch (e) {
|
|
24953
|
+
return { isValid: false, value: _0n4 };
|
|
24954
|
+
}
|
|
24955
|
+
});
|
|
24956
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
24957
|
+
throw new Error("bad curve params: generator point");
|
|
24958
|
+
function acoord(title, n, banZero = false) {
|
|
24959
|
+
const min = banZero ? _1n4 : _0n4;
|
|
24960
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
24961
|
+
return n;
|
|
24962
|
+
}
|
|
24963
|
+
function aextpoint(other) {
|
|
24964
|
+
if (!(other instanceof Point))
|
|
24965
|
+
throw new Error("ExtendedPoint expected");
|
|
24966
|
+
}
|
|
24967
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
24968
|
+
const { X, Y, Z } = p;
|
|
24969
|
+
const is0 = p.is0();
|
|
24970
|
+
if (iz == null)
|
|
24971
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
24972
|
+
const x = modP(X * iz);
|
|
24973
|
+
const y = modP(Y * iz);
|
|
24974
|
+
const zz = Fp2.mul(Z, iz);
|
|
24975
|
+
if (is0)
|
|
24976
|
+
return { x: _0n4, y: _1n4 };
|
|
24977
|
+
if (zz !== _1n4)
|
|
24978
|
+
throw new Error("invZ was invalid");
|
|
24979
|
+
return { x, y };
|
|
24980
|
+
});
|
|
24981
|
+
const assertValidMemo = memoized((p) => {
|
|
24982
|
+
const { a, d } = CURVE;
|
|
24983
|
+
if (p.is0())
|
|
24984
|
+
throw new Error("bad point: ZERO");
|
|
24985
|
+
const { X, Y, Z, T } = p;
|
|
24986
|
+
const X2 = modP(X * X);
|
|
24987
|
+
const Y2 = modP(Y * Y);
|
|
24988
|
+
const Z2 = modP(Z * Z);
|
|
24989
|
+
const Z4 = modP(Z2 * Z2);
|
|
24990
|
+
const aX2 = modP(X2 * a);
|
|
24991
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
24992
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
24993
|
+
if (left !== right)
|
|
24994
|
+
throw new Error("bad point: equation left != right (1)");
|
|
24995
|
+
const XY = modP(X * Y);
|
|
24996
|
+
const ZT = modP(Z * T);
|
|
24997
|
+
if (XY !== ZT)
|
|
24998
|
+
throw new Error("bad point: equation left != right (2)");
|
|
24999
|
+
return true;
|
|
25000
|
+
});
|
|
25001
|
+
class Point {
|
|
25002
|
+
constructor(X, Y, Z, T) {
|
|
25003
|
+
this.X = acoord("x", X);
|
|
25004
|
+
this.Y = acoord("y", Y);
|
|
25005
|
+
this.Z = acoord("z", Z, true);
|
|
25006
|
+
this.T = acoord("t", T);
|
|
25007
|
+
Object.freeze(this);
|
|
25008
|
+
}
|
|
25009
|
+
static CURVE() {
|
|
25010
|
+
return CURVE;
|
|
25011
|
+
}
|
|
25012
|
+
static fromAffine(p) {
|
|
25013
|
+
if (p instanceof Point)
|
|
25014
|
+
throw new Error("extended point not allowed");
|
|
25015
|
+
const { x, y } = p || {};
|
|
25016
|
+
acoord("x", x);
|
|
25017
|
+
acoord("y", y);
|
|
25018
|
+
return new Point(x, y, _1n4, modP(x * y));
|
|
25019
|
+
}
|
|
25020
|
+
// Uses algo from RFC8032 5.1.3.
|
|
25021
|
+
static fromBytes(bytes, zip215 = false) {
|
|
25022
|
+
const len = Fp2.BYTES;
|
|
25023
|
+
const { a, d } = CURVE;
|
|
25024
|
+
bytes = copyBytes(_abytes2(bytes, len, "point"));
|
|
25025
|
+
_abool2(zip215, "zip215");
|
|
25026
|
+
const normed = copyBytes(bytes);
|
|
25027
|
+
const lastByte = bytes[len - 1];
|
|
25028
|
+
normed[len - 1] = lastByte & ~128;
|
|
25029
|
+
const y = bytesToNumberLE(normed);
|
|
25030
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
25031
|
+
aInRange("point.y", y, _0n4, max);
|
|
25032
|
+
const y2 = modP(y * y);
|
|
25033
|
+
const u = modP(y2 - _1n4);
|
|
25034
|
+
const v = modP(d * y2 - a);
|
|
25035
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
25036
|
+
if (!isValid)
|
|
25037
|
+
throw new Error("bad point: invalid y coordinate");
|
|
25038
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
25039
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
25040
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
25041
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
25042
|
+
if (isLastByteOdd !== isXOdd)
|
|
25043
|
+
x = modP(-x);
|
|
25044
|
+
return Point.fromAffine({ x, y });
|
|
25045
|
+
}
|
|
25046
|
+
static fromHex(bytes, zip215 = false) {
|
|
25047
|
+
return Point.fromBytes(ensureBytes("point", bytes), zip215);
|
|
25048
|
+
}
|
|
25049
|
+
get x() {
|
|
25050
|
+
return this.toAffine().x;
|
|
25051
|
+
}
|
|
25052
|
+
get y() {
|
|
25053
|
+
return this.toAffine().y;
|
|
25054
|
+
}
|
|
25055
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
25056
|
+
wnaf.createCache(this, windowSize);
|
|
25057
|
+
if (!isLazy)
|
|
25058
|
+
this.multiply(_2n2);
|
|
25059
|
+
return this;
|
|
25060
|
+
}
|
|
25061
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
25062
|
+
assertValidity() {
|
|
25063
|
+
assertValidMemo(this);
|
|
25064
|
+
}
|
|
25065
|
+
// Compare one point to another.
|
|
25066
|
+
equals(other) {
|
|
25067
|
+
aextpoint(other);
|
|
25068
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
25069
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
25070
|
+
const X1Z2 = modP(X1 * Z2);
|
|
25071
|
+
const X2Z1 = modP(X2 * Z1);
|
|
25072
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
25073
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
25074
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
25075
|
+
}
|
|
25076
|
+
is0() {
|
|
25077
|
+
return this.equals(Point.ZERO);
|
|
25078
|
+
}
|
|
25079
|
+
negate() {
|
|
25080
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
25081
|
+
}
|
|
25082
|
+
// Fast algo for doubling Extended Point.
|
|
25083
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
25084
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
25085
|
+
double() {
|
|
25086
|
+
const { a } = CURVE;
|
|
25087
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
25088
|
+
const A = modP(X1 * X1);
|
|
25089
|
+
const B = modP(Y1 * Y1);
|
|
25090
|
+
const C = modP(_2n2 * modP(Z1 * Z1));
|
|
25091
|
+
const D = modP(a * A);
|
|
25092
|
+
const x1y1 = X1 + Y1;
|
|
25093
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
25094
|
+
const G = D + B;
|
|
25095
|
+
const F = G - C;
|
|
25096
|
+
const H = D - B;
|
|
25097
|
+
const X3 = modP(E * F);
|
|
25098
|
+
const Y3 = modP(G * H);
|
|
25099
|
+
const T3 = modP(E * H);
|
|
25100
|
+
const Z3 = modP(F * G);
|
|
25101
|
+
return new Point(X3, Y3, Z3, T3);
|
|
25102
|
+
}
|
|
25103
|
+
// Fast algo for adding 2 Extended Points.
|
|
25104
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
25105
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
25106
|
+
add(other) {
|
|
25107
|
+
aextpoint(other);
|
|
25108
|
+
const { a, d } = CURVE;
|
|
25109
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
25110
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
25111
|
+
const A = modP(X1 * X2);
|
|
25112
|
+
const B = modP(Y1 * Y2);
|
|
25113
|
+
const C = modP(T1 * d * T2);
|
|
25114
|
+
const D = modP(Z1 * Z2);
|
|
25115
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
25116
|
+
const F = D - C;
|
|
25117
|
+
const G = D + C;
|
|
25118
|
+
const H = modP(B - a * A);
|
|
25119
|
+
const X3 = modP(E * F);
|
|
25120
|
+
const Y3 = modP(G * H);
|
|
25121
|
+
const T3 = modP(E * H);
|
|
25122
|
+
const Z3 = modP(F * G);
|
|
25123
|
+
return new Point(X3, Y3, Z3, T3);
|
|
25124
|
+
}
|
|
25125
|
+
subtract(other) {
|
|
25126
|
+
return this.add(other.negate());
|
|
25127
|
+
}
|
|
25128
|
+
// Constant-time multiplication.
|
|
25129
|
+
multiply(scalar) {
|
|
25130
|
+
if (!Fn2.isValidNot0(scalar))
|
|
25131
|
+
throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
25132
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
|
|
25133
|
+
return normalizeZ(Point, [p, f])[0];
|
|
25134
|
+
}
|
|
25135
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
25136
|
+
// It's faster, but should only be used when you don't care about
|
|
25137
|
+
// an exposed private key e.g. sig verification.
|
|
25138
|
+
// Does NOT allow scalars higher than CURVE.n.
|
|
25139
|
+
// Accepts optional accumulator to merge with multiply (important for sparse scalars)
|
|
25140
|
+
multiplyUnsafe(scalar, acc = Point.ZERO) {
|
|
25141
|
+
if (!Fn2.isValid(scalar))
|
|
25142
|
+
throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
25143
|
+
if (scalar === _0n4)
|
|
25144
|
+
return Point.ZERO;
|
|
25145
|
+
if (this.is0() || scalar === _1n4)
|
|
25146
|
+
return this;
|
|
25147
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
|
|
25148
|
+
}
|
|
25149
|
+
// Checks if point is of small order.
|
|
25150
|
+
// If you add something to small order point, you will have "dirty"
|
|
25151
|
+
// point with torsion component.
|
|
25152
|
+
// Multiplies point by cofactor and checks if the result is 0.
|
|
25153
|
+
isSmallOrder() {
|
|
25154
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
25155
|
+
}
|
|
25156
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
25157
|
+
// Returns `false` is the point is dirty.
|
|
25158
|
+
isTorsionFree() {
|
|
25159
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
25160
|
+
}
|
|
25161
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
25162
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
25163
|
+
toAffine(invertedZ) {
|
|
25164
|
+
return toAffineMemo(this, invertedZ);
|
|
25165
|
+
}
|
|
25166
|
+
clearCofactor() {
|
|
25167
|
+
if (cofactor === _1n4)
|
|
25168
|
+
return this;
|
|
25169
|
+
return this.multiplyUnsafe(cofactor);
|
|
25170
|
+
}
|
|
25171
|
+
toBytes() {
|
|
25172
|
+
const { x, y } = this.toAffine();
|
|
25173
|
+
const bytes = Fp2.toBytes(y);
|
|
25174
|
+
bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
|
|
25175
|
+
return bytes;
|
|
25176
|
+
}
|
|
25177
|
+
toHex() {
|
|
25178
|
+
return bytesToHex(this.toBytes());
|
|
25179
|
+
}
|
|
25180
|
+
toString() {
|
|
25181
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
25182
|
+
}
|
|
25183
|
+
// TODO: remove
|
|
25184
|
+
get ex() {
|
|
25185
|
+
return this.X;
|
|
25186
|
+
}
|
|
25187
|
+
get ey() {
|
|
25188
|
+
return this.Y;
|
|
25189
|
+
}
|
|
25190
|
+
get ez() {
|
|
25191
|
+
return this.Z;
|
|
25192
|
+
}
|
|
25193
|
+
get et() {
|
|
25194
|
+
return this.T;
|
|
25195
|
+
}
|
|
25196
|
+
static normalizeZ(points) {
|
|
25197
|
+
return normalizeZ(Point, points);
|
|
25198
|
+
}
|
|
25199
|
+
static msm(points, scalars) {
|
|
25200
|
+
return pippenger(Point, Fn2, points, scalars);
|
|
25201
|
+
}
|
|
25202
|
+
_setWindowSize(windowSize) {
|
|
25203
|
+
this.precompute(windowSize);
|
|
25204
|
+
}
|
|
25205
|
+
toRawBytes() {
|
|
25206
|
+
return this.toBytes();
|
|
25207
|
+
}
|
|
25208
|
+
}
|
|
25209
|
+
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
25210
|
+
Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
25211
|
+
Point.Fp = Fp2;
|
|
25212
|
+
Point.Fn = Fn2;
|
|
25213
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
25214
|
+
Point.BASE.precompute(8);
|
|
25215
|
+
return Point;
|
|
25216
|
+
}
|
|
25217
|
+
var PrimeEdwardsPoint = class {
|
|
25218
|
+
constructor(ep) {
|
|
25219
|
+
this.ep = ep;
|
|
25220
|
+
}
|
|
25221
|
+
// Static methods that must be implemented by subclasses
|
|
25222
|
+
static fromBytes(_bytes) {
|
|
25223
|
+
notImplemented();
|
|
25224
|
+
}
|
|
25225
|
+
static fromHex(_hex) {
|
|
25226
|
+
notImplemented();
|
|
25227
|
+
}
|
|
25228
|
+
get x() {
|
|
25229
|
+
return this.toAffine().x;
|
|
25230
|
+
}
|
|
25231
|
+
get y() {
|
|
25232
|
+
return this.toAffine().y;
|
|
25233
|
+
}
|
|
25234
|
+
// Common implementations
|
|
25235
|
+
clearCofactor() {
|
|
25236
|
+
return this;
|
|
25237
|
+
}
|
|
25238
|
+
assertValidity() {
|
|
25239
|
+
this.ep.assertValidity();
|
|
25240
|
+
}
|
|
25241
|
+
toAffine(invertedZ) {
|
|
25242
|
+
return this.ep.toAffine(invertedZ);
|
|
25243
|
+
}
|
|
25244
|
+
toHex() {
|
|
25245
|
+
return bytesToHex(this.toBytes());
|
|
25246
|
+
}
|
|
25247
|
+
toString() {
|
|
25248
|
+
return this.toHex();
|
|
25249
|
+
}
|
|
25250
|
+
isTorsionFree() {
|
|
25251
|
+
return true;
|
|
25252
|
+
}
|
|
25253
|
+
isSmallOrder() {
|
|
25254
|
+
return false;
|
|
25255
|
+
}
|
|
25256
|
+
add(other) {
|
|
25257
|
+
this.assertSame(other);
|
|
25258
|
+
return this.init(this.ep.add(other.ep));
|
|
25259
|
+
}
|
|
25260
|
+
subtract(other) {
|
|
25261
|
+
this.assertSame(other);
|
|
25262
|
+
return this.init(this.ep.subtract(other.ep));
|
|
25263
|
+
}
|
|
25264
|
+
multiply(scalar) {
|
|
25265
|
+
return this.init(this.ep.multiply(scalar));
|
|
25266
|
+
}
|
|
25267
|
+
multiplyUnsafe(scalar) {
|
|
25268
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
25269
|
+
}
|
|
25270
|
+
double() {
|
|
25271
|
+
return this.init(this.ep.double());
|
|
25272
|
+
}
|
|
25273
|
+
negate() {
|
|
25274
|
+
return this.init(this.ep.negate());
|
|
25275
|
+
}
|
|
25276
|
+
precompute(windowSize, isLazy) {
|
|
25277
|
+
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
25278
|
+
}
|
|
25279
|
+
/** @deprecated use `toBytes` */
|
|
25280
|
+
toRawBytes() {
|
|
25281
|
+
return this.toBytes();
|
|
25282
|
+
}
|
|
25283
|
+
};
|
|
25284
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
25285
|
+
if (typeof cHash !== "function")
|
|
25286
|
+
throw new Error('"hash" function param is required');
|
|
25287
|
+
_validateObject(eddsaOpts, {}, {
|
|
25288
|
+
adjustScalarBytes: "function",
|
|
25289
|
+
randomBytes: "function",
|
|
25290
|
+
domain: "function",
|
|
25291
|
+
prehash: "function",
|
|
25292
|
+
mapToCurve: "function"
|
|
25293
|
+
});
|
|
25294
|
+
const { prehash } = eddsaOpts;
|
|
25295
|
+
const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
|
|
25296
|
+
const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
|
|
25297
|
+
const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
|
|
25298
|
+
const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
|
|
25299
|
+
_abool2(phflag, "phflag");
|
|
25300
|
+
if (ctx.length || phflag)
|
|
25301
|
+
throw new Error("Contexts/pre-hash are not supported");
|
|
25302
|
+
return data;
|
|
25303
|
+
});
|
|
25304
|
+
function modN_LE(hash) {
|
|
25305
|
+
return Fn2.create(bytesToNumberLE(hash));
|
|
25306
|
+
}
|
|
25307
|
+
function getPrivateScalar(key) {
|
|
25308
|
+
const len = lengths.secretKey;
|
|
25309
|
+
key = ensureBytes("private key", key, len);
|
|
25310
|
+
const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
|
|
25311
|
+
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
25312
|
+
const prefix = hashed.slice(len, 2 * len);
|
|
25313
|
+
const scalar = modN_LE(head);
|
|
25314
|
+
return { head, prefix, scalar };
|
|
25315
|
+
}
|
|
25316
|
+
function getExtendedPublicKey(secretKey) {
|
|
25317
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
25318
|
+
const point = BASE.multiply(scalar);
|
|
25319
|
+
const pointBytes = point.toBytes();
|
|
25320
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
25321
|
+
}
|
|
25322
|
+
function getPublicKey(secretKey) {
|
|
25323
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
25324
|
+
}
|
|
25325
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
25326
|
+
const msg = concatBytes(...msgs);
|
|
25327
|
+
return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
|
|
25328
|
+
}
|
|
25329
|
+
function sign(msg, secretKey, options = {}) {
|
|
25330
|
+
msg = ensureBytes("message", msg);
|
|
25331
|
+
if (prehash)
|
|
25332
|
+
msg = prehash(msg);
|
|
25333
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
25334
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
25335
|
+
const R = BASE.multiply(r).toBytes();
|
|
25336
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
25337
|
+
const s = Fn2.create(r + k * scalar);
|
|
25338
|
+
if (!Fn2.isValid(s))
|
|
25339
|
+
throw new Error("sign failed: invalid s");
|
|
25340
|
+
const rs = concatBytes(R, Fn2.toBytes(s));
|
|
25341
|
+
return _abytes2(rs, lengths.signature, "result");
|
|
25342
|
+
}
|
|
25343
|
+
const verifyOpts = { zip215: true };
|
|
25344
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
25345
|
+
const { context, zip215 } = options;
|
|
25346
|
+
const len = lengths.signature;
|
|
25347
|
+
sig = ensureBytes("signature", sig, len);
|
|
25348
|
+
msg = ensureBytes("message", msg);
|
|
25349
|
+
publicKey = ensureBytes("publicKey", publicKey, lengths.publicKey);
|
|
25350
|
+
if (zip215 !== void 0)
|
|
25351
|
+
_abool2(zip215, "zip215");
|
|
25352
|
+
if (prehash)
|
|
25353
|
+
msg = prehash(msg);
|
|
25354
|
+
const mid = len / 2;
|
|
25355
|
+
const r = sig.subarray(0, mid);
|
|
25356
|
+
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
25357
|
+
let A, R, SB;
|
|
25358
|
+
try {
|
|
25359
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
25360
|
+
R = Point.fromBytes(r, zip215);
|
|
25361
|
+
SB = BASE.multiplyUnsafe(s);
|
|
25362
|
+
} catch (error) {
|
|
25363
|
+
return false;
|
|
25364
|
+
}
|
|
25365
|
+
if (!zip215 && A.isSmallOrder())
|
|
25366
|
+
return false;
|
|
25367
|
+
const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
|
|
25368
|
+
const RkA = R.add(A.multiplyUnsafe(k));
|
|
25369
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
25370
|
+
}
|
|
25371
|
+
const _size = Fp2.BYTES;
|
|
25372
|
+
const lengths = {
|
|
25373
|
+
secretKey: _size,
|
|
25374
|
+
publicKey: _size,
|
|
25375
|
+
signature: 2 * _size,
|
|
25376
|
+
seed: _size
|
|
25377
|
+
};
|
|
25378
|
+
function randomSecretKey(seed = randomBytes2(lengths.seed)) {
|
|
25379
|
+
return _abytes2(seed, lengths.seed, "seed");
|
|
25380
|
+
}
|
|
25381
|
+
function keygen(seed) {
|
|
25382
|
+
const secretKey = utils.randomSecretKey(seed);
|
|
25383
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
25384
|
+
}
|
|
25385
|
+
function isValidSecretKey(key) {
|
|
25386
|
+
return isBytes(key) && key.length === Fn2.BYTES;
|
|
25387
|
+
}
|
|
25388
|
+
function isValidPublicKey(key, zip215) {
|
|
25389
|
+
try {
|
|
25390
|
+
return !!Point.fromBytes(key, zip215);
|
|
25391
|
+
} catch (error) {
|
|
25392
|
+
return false;
|
|
25393
|
+
}
|
|
25394
|
+
}
|
|
25395
|
+
const utils = {
|
|
25396
|
+
getExtendedPublicKey,
|
|
25397
|
+
randomSecretKey,
|
|
25398
|
+
isValidSecretKey,
|
|
25399
|
+
isValidPublicKey,
|
|
25400
|
+
/**
|
|
25401
|
+
* Converts ed public key to x public key. Uses formula:
|
|
25402
|
+
* - ed25519:
|
|
25403
|
+
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
25404
|
+
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
25405
|
+
* - ed448:
|
|
25406
|
+
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
25407
|
+
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
25408
|
+
*/
|
|
25409
|
+
toMontgomery(publicKey) {
|
|
25410
|
+
const { y } = Point.fromBytes(publicKey);
|
|
25411
|
+
const size = lengths.publicKey;
|
|
25412
|
+
const is25519 = size === 32;
|
|
25413
|
+
if (!is25519 && size !== 57)
|
|
25414
|
+
throw new Error("only defined for 25519 and 448");
|
|
25415
|
+
const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
|
|
25416
|
+
return Fp2.toBytes(u);
|
|
25417
|
+
},
|
|
25418
|
+
toMontgomerySecret(secretKey) {
|
|
25419
|
+
const size = lengths.secretKey;
|
|
25420
|
+
_abytes2(secretKey, size);
|
|
25421
|
+
const hashed = cHash(secretKey.subarray(0, size));
|
|
25422
|
+
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
25423
|
+
},
|
|
25424
|
+
/** @deprecated */
|
|
25425
|
+
randomPrivateKey: randomSecretKey,
|
|
25426
|
+
/** @deprecated */
|
|
25427
|
+
precompute(windowSize = 8, point = Point.BASE) {
|
|
25428
|
+
return point.precompute(windowSize, false);
|
|
25429
|
+
}
|
|
25430
|
+
};
|
|
25431
|
+
return Object.freeze({
|
|
25432
|
+
keygen,
|
|
25433
|
+
getPublicKey,
|
|
25434
|
+
sign,
|
|
25435
|
+
verify,
|
|
25436
|
+
utils,
|
|
25437
|
+
Point,
|
|
25438
|
+
lengths
|
|
25439
|
+
});
|
|
25440
|
+
}
|
|
25441
|
+
function _eddsa_legacy_opts_to_new(c) {
|
|
25442
|
+
const CURVE = {
|
|
25443
|
+
a: c.a,
|
|
25444
|
+
d: c.d,
|
|
25445
|
+
p: c.Fp.ORDER,
|
|
25446
|
+
n: c.n,
|
|
25447
|
+
h: c.h,
|
|
25448
|
+
Gx: c.Gx,
|
|
25449
|
+
Gy: c.Gy
|
|
25450
|
+
};
|
|
25451
|
+
const Fp2 = c.Fp;
|
|
25452
|
+
const Fn2 = Field(CURVE.n, c.nBitLength, true);
|
|
25453
|
+
const curveOpts = { Fp: Fp2, Fn: Fn2, uvRatio: c.uvRatio };
|
|
25454
|
+
const eddsaOpts = {
|
|
25455
|
+
randomBytes: c.randomBytes,
|
|
25456
|
+
adjustScalarBytes: c.adjustScalarBytes,
|
|
25457
|
+
domain: c.domain,
|
|
25458
|
+
prehash: c.prehash,
|
|
25459
|
+
mapToCurve: c.mapToCurve
|
|
25460
|
+
};
|
|
25461
|
+
return { CURVE, curveOpts, hash: c.hash, eddsaOpts };
|
|
25462
|
+
}
|
|
25463
|
+
function _eddsa_new_output_to_legacy(c, eddsa2) {
|
|
25464
|
+
const Point = eddsa2.Point;
|
|
25465
|
+
const legacy = Object.assign({}, eddsa2, {
|
|
25466
|
+
ExtendedPoint: Point,
|
|
25467
|
+
CURVE: c,
|
|
25468
|
+
nBitLength: Point.Fn.BITS,
|
|
25469
|
+
nByteLength: Point.Fn.BYTES
|
|
25470
|
+
});
|
|
25471
|
+
return legacy;
|
|
25472
|
+
}
|
|
25473
|
+
function twistedEdwards(c) {
|
|
25474
|
+
const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
|
|
25475
|
+
const Point = edwards(CURVE, curveOpts);
|
|
25476
|
+
const EDDSA = eddsa(Point, hash, eddsaOpts);
|
|
25477
|
+
return _eddsa_new_output_to_legacy(c, EDDSA);
|
|
25478
|
+
}
|
|
25479
|
+
|
|
25480
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/ed25519.js
|
|
25481
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
25482
|
+
var _1n5 = BigInt(1);
|
|
25483
|
+
var _2n3 = BigInt(2);
|
|
25484
|
+
var _3n2 = BigInt(3);
|
|
25485
|
+
var _5n2 = BigInt(5);
|
|
25486
|
+
var _8n3 = BigInt(8);
|
|
25487
|
+
var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
25488
|
+
var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
25489
|
+
p: ed25519_CURVE_p,
|
|
25490
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
25491
|
+
h: _8n3,
|
|
25492
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
25493
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
25494
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
25495
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
25496
|
+
}))();
|
|
25497
|
+
function ed25519_pow_2_252_3(x) {
|
|
25498
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
25499
|
+
const P = ed25519_CURVE_p;
|
|
25500
|
+
const x2 = x * x % P;
|
|
25501
|
+
const b2 = x2 * x % P;
|
|
25502
|
+
const b4 = pow2(b2, _2n3, P) * b2 % P;
|
|
25503
|
+
const b5 = pow2(b4, _1n5, P) * x % P;
|
|
25504
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
25505
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
25506
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
25507
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
25508
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
25509
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
25510
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
25511
|
+
const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
|
|
25512
|
+
return { pow_p_5_8, b2 };
|
|
25513
|
+
}
|
|
25514
|
+
function adjustScalarBytes(bytes) {
|
|
25515
|
+
bytes[0] &= 248;
|
|
25516
|
+
bytes[31] &= 127;
|
|
25517
|
+
bytes[31] |= 64;
|
|
25518
|
+
return bytes;
|
|
25519
|
+
}
|
|
25520
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
25521
|
+
function uvRatio(u, v) {
|
|
25522
|
+
const P = ed25519_CURVE_p;
|
|
25523
|
+
const v3 = mod(v * v * v, P);
|
|
25524
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
25525
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
25526
|
+
let x = mod(u * v3 * pow, P);
|
|
25527
|
+
const vx2 = mod(v * x * x, P);
|
|
25528
|
+
const root1 = x;
|
|
25529
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
25530
|
+
const useRoot1 = vx2 === u;
|
|
25531
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
25532
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
25533
|
+
if (useRoot1)
|
|
25534
|
+
x = root1;
|
|
25535
|
+
if (useRoot2 || noRoot)
|
|
25536
|
+
x = root2;
|
|
25537
|
+
if (isNegativeLE(x, P))
|
|
25538
|
+
x = mod(-x, P);
|
|
25539
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
25540
|
+
}
|
|
25541
|
+
var Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
|
|
25542
|
+
var Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
|
|
25543
|
+
var ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
25544
|
+
...ed25519_CURVE,
|
|
25545
|
+
Fp,
|
|
25546
|
+
hash: sha512,
|
|
25547
|
+
adjustScalarBytes,
|
|
25548
|
+
// dom2
|
|
25549
|
+
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
25550
|
+
// Constant-time, u/√v
|
|
25551
|
+
uvRatio
|
|
25552
|
+
}))();
|
|
25553
|
+
var ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
|
25554
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
25555
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
25556
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
25557
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
25558
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
25559
|
+
var invertSqrt = (number) => uvRatio(_1n5, number);
|
|
25560
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
25561
|
+
var bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
25562
|
+
function calcElligatorRistrettoMap(r0) {
|
|
25563
|
+
const { d } = ed25519_CURVE;
|
|
25564
|
+
const P = ed25519_CURVE_p;
|
|
25565
|
+
const mod2 = (n) => Fp.create(n);
|
|
25566
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
25567
|
+
const Ns = mod2((r + _1n5) * ONE_MINUS_D_SQ);
|
|
25568
|
+
let c = BigInt(-1);
|
|
25569
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
25570
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
25571
|
+
let s_ = mod2(s * r0);
|
|
25572
|
+
if (!isNegativeLE(s_, P))
|
|
25573
|
+
s_ = mod2(-s_);
|
|
25574
|
+
if (!Ns_D_is_sq)
|
|
25575
|
+
s = s_;
|
|
25576
|
+
if (!Ns_D_is_sq)
|
|
25577
|
+
c = r;
|
|
25578
|
+
const Nt = mod2(c * (r - _1n5) * D_MINUS_ONE_SQ - D);
|
|
25579
|
+
const s2 = s * s;
|
|
25580
|
+
const W0 = mod2((s + s) * D);
|
|
25581
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
25582
|
+
const W2 = mod2(_1n5 - s2);
|
|
25583
|
+
const W3 = mod2(_1n5 + s2);
|
|
25584
|
+
return new ed25519.Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
25585
|
+
}
|
|
25586
|
+
function ristretto255_map(bytes) {
|
|
25587
|
+
abytes(bytes, 64);
|
|
25588
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
25589
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
25590
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
25591
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
25592
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
25593
|
+
}
|
|
25594
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
25595
|
+
constructor(ep) {
|
|
25596
|
+
super(ep);
|
|
25597
|
+
}
|
|
25598
|
+
static fromAffine(ap) {
|
|
25599
|
+
return new __RistrettoPoint(ed25519.Point.fromAffine(ap));
|
|
25600
|
+
}
|
|
25601
|
+
assertSame(other) {
|
|
25602
|
+
if (!(other instanceof __RistrettoPoint))
|
|
25603
|
+
throw new Error("RistrettoPoint expected");
|
|
25604
|
+
}
|
|
25605
|
+
init(ep) {
|
|
25606
|
+
return new __RistrettoPoint(ep);
|
|
25607
|
+
}
|
|
25608
|
+
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
25609
|
+
static hashToCurve(hex3) {
|
|
25610
|
+
return ristretto255_map(ensureBytes("ristrettoHash", hex3, 64));
|
|
25611
|
+
}
|
|
25612
|
+
static fromBytes(bytes) {
|
|
25613
|
+
abytes(bytes, 32);
|
|
25614
|
+
const { a, d } = ed25519_CURVE;
|
|
25615
|
+
const P = ed25519_CURVE_p;
|
|
25616
|
+
const mod2 = (n) => Fp.create(n);
|
|
25617
|
+
const s = bytes255ToNumberLE(bytes);
|
|
25618
|
+
if (!equalBytes2(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
25619
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
25620
|
+
const s2 = mod2(s * s);
|
|
25621
|
+
const u1 = mod2(_1n5 + a * s2);
|
|
25622
|
+
const u2 = mod2(_1n5 - a * s2);
|
|
25623
|
+
const u1_2 = mod2(u1 * u1);
|
|
25624
|
+
const u2_2 = mod2(u2 * u2);
|
|
25625
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
25626
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
25627
|
+
const Dx = mod2(I * u2);
|
|
25628
|
+
const Dy = mod2(I * Dx * v);
|
|
25629
|
+
let x = mod2((s + s) * Dx);
|
|
25630
|
+
if (isNegativeLE(x, P))
|
|
25631
|
+
x = mod2(-x);
|
|
25632
|
+
const y = mod2(u1 * Dy);
|
|
25633
|
+
const t = mod2(x * y);
|
|
25634
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n5)
|
|
25635
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
25636
|
+
return new __RistrettoPoint(new ed25519.Point(x, y, _1n5, t));
|
|
25637
|
+
}
|
|
25638
|
+
/**
|
|
25639
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
25640
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
25641
|
+
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
25642
|
+
*/
|
|
25643
|
+
static fromHex(hex3) {
|
|
25644
|
+
return __RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex3, 32));
|
|
25645
|
+
}
|
|
25646
|
+
static msm(points, scalars) {
|
|
25647
|
+
return pippenger(__RistrettoPoint, ed25519.Point.Fn, points, scalars);
|
|
25648
|
+
}
|
|
25649
|
+
/**
|
|
25650
|
+
* Encodes ristretto point to Uint8Array.
|
|
25651
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
25652
|
+
*/
|
|
25653
|
+
toBytes() {
|
|
25654
|
+
let { X, Y, Z, T } = this.ep;
|
|
25655
|
+
const P = ed25519_CURVE_p;
|
|
25656
|
+
const mod2 = (n) => Fp.create(n);
|
|
25657
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
25658
|
+
const u2 = mod2(X * Y);
|
|
25659
|
+
const u2sq = mod2(u2 * u2);
|
|
25660
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
25661
|
+
const D1 = mod2(invsqrt * u1);
|
|
25662
|
+
const D2 = mod2(invsqrt * u2);
|
|
25663
|
+
const zInv = mod2(D1 * D2 * T);
|
|
25664
|
+
let D;
|
|
25665
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
25666
|
+
let _x = mod2(Y * SQRT_M1);
|
|
25667
|
+
let _y = mod2(X * SQRT_M1);
|
|
25668
|
+
X = _x;
|
|
25669
|
+
Y = _y;
|
|
25670
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
25671
|
+
} else {
|
|
25672
|
+
D = D2;
|
|
25673
|
+
}
|
|
25674
|
+
if (isNegativeLE(X * zInv, P))
|
|
25675
|
+
Y = mod2(-Y);
|
|
25676
|
+
let s = mod2((Z - Y) * D);
|
|
25677
|
+
if (isNegativeLE(s, P))
|
|
25678
|
+
s = mod2(-s);
|
|
25679
|
+
return Fp.toBytes(s);
|
|
25680
|
+
}
|
|
25681
|
+
/**
|
|
25682
|
+
* Compares two Ristretto points.
|
|
25683
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
25684
|
+
*/
|
|
25685
|
+
equals(other) {
|
|
25686
|
+
this.assertSame(other);
|
|
25687
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
25688
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
25689
|
+
const mod2 = (n) => Fp.create(n);
|
|
25690
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
25691
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
25692
|
+
return one || two;
|
|
25693
|
+
}
|
|
25694
|
+
is0() {
|
|
25695
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
25696
|
+
}
|
|
25697
|
+
};
|
|
25698
|
+
_RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
|
|
25699
|
+
_RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
|
|
25700
|
+
_RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
|
|
25701
|
+
_RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
|
|
25702
|
+
|
|
25703
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2-receiver.js
|
|
25704
|
+
var BCPV2_LEAD_BOUND_MS = 5e3;
|
|
25705
|
+
var REPLAY_WINDOW = 64n;
|
|
25706
|
+
var BUCKET_DEPTH = 64;
|
|
25707
|
+
var BUCKET_RATE_PER_MS = 1;
|
|
25708
|
+
var hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
25709
|
+
function sameBytes(a, b) {
|
|
25710
|
+
if (a.byteLength !== b.byteLength)
|
|
25711
|
+
return false;
|
|
25712
|
+
let diff = 0;
|
|
25713
|
+
for (let i = 0; i < a.byteLength; i++)
|
|
25714
|
+
diff |= a[i] ^ b[i];
|
|
25715
|
+
return diff === 0;
|
|
25716
|
+
}
|
|
25717
|
+
var BcProvenanceV2Receiver = class {
|
|
25718
|
+
maxAuthDelayMs;
|
|
25719
|
+
manifestTimerMs;
|
|
25720
|
+
widenMs;
|
|
25721
|
+
/** Datagrams that left a hold queue at expiry, per path: the loss signal for failover. */
|
|
25722
|
+
expired = { multicast: 0, unicast: 0 };
|
|
25723
|
+
/**
|
|
25724
|
+
* Unicast repair (Section 7.2.7 step 2): on a receiver that can request it, how
|
|
25725
|
+
* long ago (ms) the first packet admitted into the oldest open block with an FEC
|
|
25726
|
+
* deadline was admitted; undefined with none open. A pending entry still live
|
|
25727
|
+
* then is kept instead of expiring, and is released with the block, under the
|
|
25728
|
+
* open-block bound of [MOQ-FEC] Section 9.
|
|
25729
|
+
*/
|
|
25730
|
+
keepPendingAgeMs = null;
|
|
25731
|
+
cfg;
|
|
25732
|
+
now;
|
|
25733
|
+
verifySig;
|
|
25734
|
+
accepted;
|
|
25735
|
+
queueLimit;
|
|
25736
|
+
current = null;
|
|
25737
|
+
previous = null;
|
|
25738
|
+
graceStart = {};
|
|
25739
|
+
hashAlg = null;
|
|
25740
|
+
verified = /* @__PURE__ */ new Map();
|
|
25741
|
+
highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
25742
|
+
closedAt = /* @__PURE__ */ new Map();
|
|
25743
|
+
lead = null;
|
|
25744
|
+
pending = /* @__PURE__ */ new Map();
|
|
25745
|
+
admittedUntil = /* @__PURE__ */ new Map();
|
|
25746
|
+
hold = { multicast: [], unicast: [] };
|
|
25747
|
+
holdBytes = { multicast: 0, unicast: 0 };
|
|
25748
|
+
order = 0;
|
|
25749
|
+
buckets;
|
|
25750
|
+
constructor(cfg) {
|
|
25751
|
+
this.maxAuthDelayMs = cfg.maxAuthDelayMs ?? 200;
|
|
25752
|
+
this.manifestTimerMs = cfg.manifestTimerMs ?? 20;
|
|
25753
|
+
if (!(this.maxAuthDelayMs >= 50 && this.maxAuthDelayMs <= 1e3) || !(this.manifestTimerMs >= 1)) {
|
|
25754
|
+
throw new Error("BCPV2 endpoint ignored: maxAuthDelayMs or manifestTimerMs out of range");
|
|
25755
|
+
}
|
|
25756
|
+
if (!(cfg.bandwidth > 0))
|
|
25757
|
+
throw new Error("BCPV2 endpoint ignored: protected endpoint without bandwidth");
|
|
25758
|
+
const kids = new Set(cfg.keys.map((k) => k.kid));
|
|
25759
|
+
const firsts = new Set(cfg.keys.map((k) => k.firstEpoch));
|
|
25760
|
+
if (cfg.keys.length < 1 || kids.size !== cfg.keys.length || firsts.size !== cfg.keys.length) {
|
|
25761
|
+
throw new Error("BCPV2 key set invalid: no keys, or a repeated kid or firstEpoch");
|
|
25762
|
+
}
|
|
25763
|
+
this.cfg = cfg;
|
|
25764
|
+
this.widenMs = Math.min(Math.max(cfg.widenMs ?? 0, 0), BCPV2_LEAD_BOUND_MS);
|
|
25765
|
+
this.now = cfg.now ?? Date.now;
|
|
25766
|
+
this.verifySig = cfg.verifySignature ?? ((pk, msg, sig) => {
|
|
25767
|
+
try {
|
|
25768
|
+
return ed25519.verify(sig, msg, pk, { zip215: false });
|
|
25769
|
+
} catch {
|
|
25770
|
+
return false;
|
|
25771
|
+
}
|
|
25772
|
+
});
|
|
25773
|
+
this.accepted = cfg.acceptedHashAlgs ?? [BCPV2_HASH_ALG_SHA256];
|
|
25774
|
+
const bandwidth = Math.min(cfg.bandwidth, 1e10);
|
|
25775
|
+
this.queueLimit = {
|
|
25776
|
+
multicast: this.maxAuthDelayMs * bandwidth / 8e3,
|
|
25777
|
+
unicast: (this.maxAuthDelayMs + this.widenMs) * bandwidth / 8e3
|
|
25778
|
+
};
|
|
25779
|
+
const t = this.now();
|
|
25780
|
+
this.buckets = {
|
|
25781
|
+
multicast: { tokens: BUCKET_DEPTH, last: t, dropped: 0 },
|
|
25782
|
+
mirror: { tokens: BUCKET_DEPTH, last: t, dropped: 0 }
|
|
25783
|
+
};
|
|
25784
|
+
}
|
|
25785
|
+
get grace() {
|
|
25786
|
+
return this.maxAuthDelayMs + 2 * this.manifestTimerMs;
|
|
25787
|
+
}
|
|
25788
|
+
/** The mirror's lead in force: 0 without a manifest track, the bound until one is measured. */
|
|
25789
|
+
get leadMs() {
|
|
25790
|
+
if (!this.cfg.applyLead)
|
|
25791
|
+
return 0;
|
|
25792
|
+
return this.lead ?? BCPV2_LEAD_BOUND_MS;
|
|
25793
|
+
}
|
|
25794
|
+
/** Step 1: a record datagram on `manifestPort`, or a record object of the manifest track. */
|
|
25795
|
+
onRecord(bytes, path) {
|
|
25796
|
+
const now = this.now();
|
|
25797
|
+
this.expire(now);
|
|
25798
|
+
const drop = (verdict) => ({ verdict, admitted: [] });
|
|
25799
|
+
const len = bytes.byteLength;
|
|
25800
|
+
if (len < BCPV2_MIN_RECORD_LENGTH || len > BCPV2_MAX_RECORD_LENGTH || (len - 132) % 32 !== 0)
|
|
25801
|
+
return drop("length");
|
|
25802
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, len);
|
|
25803
|
+
if (view2.getUint32(0) !== 1296583986)
|
|
25804
|
+
return drop("magic");
|
|
25805
|
+
const kid = view2.getUint32(4);
|
|
25806
|
+
const epoch = view2.getUint32(8);
|
|
25807
|
+
const mseq = view2.getBigUint64(12);
|
|
25808
|
+
const count = view2.getUint16(28);
|
|
25809
|
+
const hashAlg = bytes[30];
|
|
25810
|
+
const sent = view2.getUint32(32);
|
|
25811
|
+
const key = this.cfg.keys.find((k) => k.kid === kid);
|
|
25812
|
+
if (!key)
|
|
25813
|
+
return drop("kid");
|
|
25814
|
+
const next = Math.min(...this.cfg.keys.map((k) => k.firstEpoch).filter((f) => f > key.firstEpoch), Infinity);
|
|
25815
|
+
if (epoch < key.firstEpoch || epoch >= next)
|
|
25816
|
+
return drop("epoch");
|
|
25817
|
+
const id = `${kid}:${epoch}:${mseq}`;
|
|
25818
|
+
const seen = this.verified.get(id);
|
|
25819
|
+
if (seen) {
|
|
25820
|
+
const admitted2 = hex(sha2562(bytes)) === seen.digest ? this.arrival(seen, path, now) : [];
|
|
25821
|
+
return { verdict: "duplicate", admitted: admitted2 };
|
|
25822
|
+
}
|
|
25823
|
+
const ke = `${kid}:${epoch}`;
|
|
25824
|
+
if (this.current === null) {
|
|
25825
|
+
if (epoch < this.cfg.minEpoch || epoch < (this.cfg.pinnedEpochFloor?.get(kid) ?? 0))
|
|
25826
|
+
return drop("epoch");
|
|
25827
|
+
} else if (epoch < this.current.epoch && !this.previousAdmitted(epoch, path, now)) {
|
|
25828
|
+
return drop("epoch");
|
|
25829
|
+
}
|
|
25830
|
+
const floor = this.floor(ke, path);
|
|
25831
|
+
if (floor !== void 0 && mseq <= floor - REPLAY_WINDOW)
|
|
25832
|
+
return drop("mseq");
|
|
25833
|
+
const closed = this.closedAt.get(ke);
|
|
25834
|
+
if (closed !== void 0 && mseq > closed)
|
|
25835
|
+
return drop("closed");
|
|
25836
|
+
if (this.cfg.checkFreshness !== false) {
|
|
25837
|
+
const behind = 7e3 + this.maxAuthDelayMs + (path === "mirror" ? this.widenMs : 0);
|
|
25838
|
+
if (sent * 1e3 < now - behind || sent * 1e3 > now + 6e3)
|
|
25839
|
+
return drop("sent");
|
|
25840
|
+
}
|
|
25841
|
+
if (count !== (len - 132) / 32)
|
|
25842
|
+
return drop("count");
|
|
25843
|
+
if (!this.accepted.includes(hashAlg) || this.hashAlg !== null && hashAlg !== this.hashAlg)
|
|
25844
|
+
return drop("hash_alg");
|
|
25845
|
+
if (this.cfg.catalogCtx && !sameBytes(bytes.subarray(36, 68), this.cfg.catalogCtx))
|
|
25846
|
+
return drop("ctx");
|
|
25847
|
+
if (!this.take(path, now))
|
|
25848
|
+
return drop("budget");
|
|
25849
|
+
const record = decodeBcProvenanceV2Record(bytes);
|
|
25850
|
+
if (!this.verifySig(key.publicKey, bcProvenanceV2SignedMessage(record, this.cfg.endpoint), record.signature)) {
|
|
25851
|
+
return drop("signature");
|
|
25852
|
+
}
|
|
25853
|
+
if (this.current === null || epoch > this.current.epoch) {
|
|
25854
|
+
if (this.current !== null) {
|
|
25855
|
+
this.previous = { epoch: this.current.epoch, deadline: now + BCPV2_LEAD_BOUND_MS + this.grace };
|
|
25856
|
+
}
|
|
25857
|
+
this.current = { epoch, since: now };
|
|
25858
|
+
this.graceStart = { [path]: now };
|
|
25859
|
+
if (this.hashAlg === null) {
|
|
25860
|
+
this.hashAlg = hashAlg;
|
|
25861
|
+
for (const queue of Object.values(this.hold)) {
|
|
25862
|
+
for (const held of queue)
|
|
25863
|
+
held.digest ??= this.digest(held.datagram);
|
|
25864
|
+
}
|
|
25865
|
+
}
|
|
25866
|
+
} else if (epoch === this.current.epoch) {
|
|
25867
|
+
this.graceStart[path] ??= now;
|
|
25868
|
+
}
|
|
25869
|
+
const entry = {
|
|
25870
|
+
kid,
|
|
25871
|
+
epoch,
|
|
25872
|
+
mseq,
|
|
25873
|
+
path,
|
|
25874
|
+
arrival: now,
|
|
25875
|
+
sent,
|
|
25876
|
+
digest: hex(sha2562(bytes)),
|
|
25877
|
+
pairs: record.digests.map((d, i) => ({ digest: hex(d), seq: record.firstSeq + BigInt(i), admitted: false })),
|
|
25878
|
+
awaitingMulticast: path === "mirror"
|
|
25879
|
+
};
|
|
25880
|
+
this.verified.set(id, entry);
|
|
25881
|
+
this.raise(ke, path, mseq);
|
|
25882
|
+
if (record.flags & 1)
|
|
25883
|
+
this.closedAt.set(ke, mseq);
|
|
25884
|
+
const widen = path === "mirror" ? this.cfg.applyLead ? this.leadMs : this.widenMs : 0;
|
|
25885
|
+
const admitted = [];
|
|
25886
|
+
for (const pair of entry.pairs)
|
|
25887
|
+
this.handlePair(pair, now, now + this.grace + widen, now, admitted);
|
|
25888
|
+
this.dropAdmittedHeld();
|
|
25889
|
+
this.prune(now);
|
|
25890
|
+
return { verdict: "verified", admitted };
|
|
25891
|
+
}
|
|
25892
|
+
/** Step 2: a data datagram on `port` (or a packet of a listed track over MoQ unicast). */
|
|
25893
|
+
onDatagram(datagram, path) {
|
|
25894
|
+
const now = this.now();
|
|
25895
|
+
this.expire(now);
|
|
25896
|
+
if (this.hashAlg !== null) {
|
|
25897
|
+
const d = this.digest(datagram);
|
|
25898
|
+
if (this.consumePending(d, now))
|
|
25899
|
+
return { verdict: "admitted", admitted: [datagram] };
|
|
25900
|
+
if (this.admittedUntil.has(d))
|
|
25901
|
+
return { verdict: "duplicate", admitted: [] };
|
|
25902
|
+
return this.enqueue({ digest: d, datagram, expiry: 0, order: 0 }, path, now);
|
|
25903
|
+
}
|
|
25904
|
+
return this.enqueue({ datagram, expiry: 0, order: 0 }, path, now);
|
|
25905
|
+
}
|
|
25906
|
+
/**
|
|
25907
|
+
* A source packet recovered by FEC decode, its base SS_ID (SS_Start + ESI)
|
|
25908
|
+
* appended: admitted only on a pending entry it consumes, never held. One no
|
|
25909
|
+
* verified record lists, or a copy of an admitted packet, is dropped.
|
|
25910
|
+
*/
|
|
25911
|
+
admitRecovered(datagram) {
|
|
25912
|
+
const now = this.now();
|
|
25913
|
+
this.expire(now);
|
|
25914
|
+
return this.hashAlg !== null && this.consumePending(this.digest(datagram), now);
|
|
25915
|
+
}
|
|
25916
|
+
/**
|
|
25917
|
+
* Discard the per-flow state on a `streamId` change or a key-set change that fails the
|
|
25918
|
+
* flow's current (kid, epoch): budgets and hold queues survive, held digests are cleared.
|
|
25919
|
+
*/
|
|
25920
|
+
resetGeneration() {
|
|
25921
|
+
this.current = null;
|
|
25922
|
+
this.previous = null;
|
|
25923
|
+
this.graceStart = {};
|
|
25924
|
+
this.hashAlg = null;
|
|
25925
|
+
this.verified.clear();
|
|
25926
|
+
this.highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
25927
|
+
this.closedAt.clear();
|
|
25928
|
+
this.lead = null;
|
|
25929
|
+
this.pending.clear();
|
|
25930
|
+
this.admittedUntil.clear();
|
|
25931
|
+
for (const queue of Object.values(this.hold))
|
|
25932
|
+
for (const held of queue)
|
|
25933
|
+
held.digest = void 0;
|
|
25934
|
+
}
|
|
25935
|
+
/** The receiver ceased to hold the endpoint: drop everything; no drop here is an expiry. */
|
|
25936
|
+
release() {
|
|
25937
|
+
this.resetGeneration();
|
|
25938
|
+
this.hold = { multicast: [], unicast: [] };
|
|
25939
|
+
this.holdBytes = { multicast: 0, unicast: 0 };
|
|
25940
|
+
}
|
|
25941
|
+
digest(datagram) {
|
|
25942
|
+
return hex(bcProvenanceV2DatagramDigest(this.cfg.endpoint, datagram));
|
|
25943
|
+
}
|
|
25944
|
+
/** Consume the lowest-seq pending entry for `digest`, admitting it. */
|
|
25945
|
+
consumePending(digest, now) {
|
|
25946
|
+
const list = this.pending.get(digest);
|
|
25947
|
+
if (!list?.length)
|
|
25948
|
+
return false;
|
|
25949
|
+
list.sort((a, b) => a.pair.seq < b.pair.seq ? -1 : a.pair.seq > b.pair.seq ? 1 : 0);
|
|
25950
|
+
const hit = list.shift();
|
|
25951
|
+
if (list.length === 0)
|
|
25952
|
+
this.pending.delete(digest);
|
|
25953
|
+
hit.pair.pending = void 0;
|
|
25954
|
+
hit.pair.admitted = true;
|
|
25955
|
+
this.admittedUntil.set(digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
25956
|
+
return true;
|
|
25957
|
+
}
|
|
25958
|
+
enqueue(held, path, now) {
|
|
25959
|
+
if (this.holdBytes[path] + held.datagram.byteLength > this.queueLimit[path]) {
|
|
25960
|
+
return { verdict: "overflow", admitted: [] };
|
|
25961
|
+
}
|
|
25962
|
+
held.expiry = now + this.maxAuthDelayMs + (path === "unicast" ? this.widenMs : 0);
|
|
25963
|
+
held.order = this.order++;
|
|
25964
|
+
this.hold[path].push(held);
|
|
25965
|
+
this.holdBytes[path] += held.datagram.byteLength;
|
|
25966
|
+
return { verdict: "held", admitted: [] };
|
|
25967
|
+
}
|
|
25968
|
+
/** Remove and return the oldest held datagram with `digest`, across both queues. */
|
|
25969
|
+
takeHeld(digest) {
|
|
25970
|
+
let best;
|
|
25971
|
+
for (const path of ["multicast", "unicast"]) {
|
|
25972
|
+
const index = this.hold[path].findIndex((h) => h.digest === digest);
|
|
25973
|
+
if (index >= 0 && (!best || this.hold[path][index].order < best.order)) {
|
|
25974
|
+
best = { path, index, order: this.hold[path][index].order };
|
|
25975
|
+
}
|
|
25976
|
+
}
|
|
25977
|
+
if (!best)
|
|
25978
|
+
return void 0;
|
|
25979
|
+
const [held] = this.hold[best.path].splice(best.index, 1);
|
|
25980
|
+
this.holdBytes[best.path] -= held.datagram.byteLength;
|
|
25981
|
+
return held.datagram;
|
|
25982
|
+
}
|
|
25983
|
+
dropAdmittedHeld() {
|
|
25984
|
+
for (const path of ["multicast", "unicast"]) {
|
|
25985
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
25986
|
+
const keep = h.digest === void 0 || !this.admittedUntil.has(h.digest);
|
|
25987
|
+
if (!keep)
|
|
25988
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
25989
|
+
return keep;
|
|
25990
|
+
});
|
|
25991
|
+
}
|
|
25992
|
+
}
|
|
25993
|
+
/** Step 1 pair handling: admit a held copy, or insert a pending entry. */
|
|
25994
|
+
handlePair(pair, now, expiry, inserted, out) {
|
|
25995
|
+
const datagram = this.takeHeld(pair.digest);
|
|
25996
|
+
if (datagram) {
|
|
25997
|
+
pair.admitted = true;
|
|
25998
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
25999
|
+
out.push(datagram);
|
|
26000
|
+
return;
|
|
26001
|
+
}
|
|
26002
|
+
const entry = { pair, inserted, expiry };
|
|
26003
|
+
pair.pending = entry;
|
|
26004
|
+
const list = this.pending.get(pair.digest);
|
|
26005
|
+
if (list)
|
|
26006
|
+
list.push(entry);
|
|
26007
|
+
else
|
|
26008
|
+
this.pending.set(pair.digest, [entry]);
|
|
26009
|
+
}
|
|
26010
|
+
/** A byte-identical copy dropped by the already-verified lookup. */
|
|
26011
|
+
arrival(seen, path, now) {
|
|
26012
|
+
const ke = `${seen.kid}:${seen.epoch}`;
|
|
26013
|
+
this.raise(ke, path, seen.mseq);
|
|
26014
|
+
if (this.current && seen.epoch === this.current.epoch)
|
|
26015
|
+
this.graceStart[path] ??= now;
|
|
26016
|
+
if (path !== "multicast" || !seen.awaitingMulticast)
|
|
26017
|
+
return [];
|
|
26018
|
+
seen.awaitingMulticast = false;
|
|
26019
|
+
if (!this.cfg.applyLead)
|
|
26020
|
+
return [];
|
|
26021
|
+
const t = this.manifestTimerMs;
|
|
26022
|
+
const interval = Math.min(Math.ceil((now - seen.arrival) / t) * t, BCPV2_LEAD_BOUND_MS);
|
|
26023
|
+
if (this.lead === null) {
|
|
26024
|
+
this.lead = interval;
|
|
26025
|
+
return [];
|
|
26026
|
+
}
|
|
26027
|
+
if (interval <= this.lead)
|
|
26028
|
+
return [];
|
|
26029
|
+
this.lead = interval;
|
|
26030
|
+
const out = [];
|
|
26031
|
+
const records = [seen, ...[...this.verified.values()].filter((v) => v.awaitingMulticast && v.arrival >= now - interval)];
|
|
26032
|
+
for (const v of records) {
|
|
26033
|
+
for (const pair of v.pairs) {
|
|
26034
|
+
if (pair.admitted) {
|
|
26035
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + interval);
|
|
26036
|
+
} else if (pair.pending) {
|
|
26037
|
+
pair.pending.expiry = pair.pending.inserted + this.grace + interval;
|
|
26038
|
+
} else {
|
|
26039
|
+
this.handlePair(pair, now, now + this.grace + interval, now, out);
|
|
26040
|
+
}
|
|
26041
|
+
}
|
|
26042
|
+
}
|
|
26043
|
+
this.dropAdmittedHeld();
|
|
26044
|
+
return out;
|
|
26045
|
+
}
|
|
26046
|
+
previousAdmitted(epoch, path, now) {
|
|
26047
|
+
if (!this.previous || epoch !== this.previous.epoch)
|
|
26048
|
+
return false;
|
|
26049
|
+
const m = this.graceStart.multicast;
|
|
26050
|
+
if (path === "multicast")
|
|
26051
|
+
return m === void 0 || now <= m + this.grace;
|
|
26052
|
+
const starts = [m, this.graceStart.mirror].filter((s) => s !== void 0);
|
|
26053
|
+
return starts.length > 0 && now <= Math.min(...starts) + this.grace;
|
|
26054
|
+
}
|
|
26055
|
+
/** The replay floor this record meets: its path's, or the higher of both from the mirror. */
|
|
26056
|
+
floor(ke, path) {
|
|
26057
|
+
const m = this.highest.multicast.get(ke);
|
|
26058
|
+
if (path === "multicast")
|
|
26059
|
+
return m;
|
|
26060
|
+
const r = this.highest.mirror.get(ke);
|
|
26061
|
+
if (m === void 0)
|
|
26062
|
+
return r;
|
|
26063
|
+
return r === void 0 || m > r ? m : r;
|
|
26064
|
+
}
|
|
26065
|
+
raise(ke, path, mseq) {
|
|
26066
|
+
const h = this.highest[path].get(ke);
|
|
26067
|
+
if (h === void 0 || mseq > h)
|
|
26068
|
+
this.highest[path].set(ke, mseq);
|
|
26069
|
+
}
|
|
26070
|
+
take(path, now) {
|
|
26071
|
+
const b = this.buckets[path];
|
|
26072
|
+
b.tokens = Math.min(BUCKET_DEPTH, b.tokens + (now - b.last) * BUCKET_RATE_PER_MS);
|
|
26073
|
+
b.last = now;
|
|
26074
|
+
if (b.tokens < 1) {
|
|
26075
|
+
b.dropped++;
|
|
26076
|
+
return false;
|
|
26077
|
+
}
|
|
26078
|
+
b.tokens -= 1;
|
|
26079
|
+
return true;
|
|
26080
|
+
}
|
|
26081
|
+
expire(now) {
|
|
26082
|
+
for (const path of ["multicast", "unicast"]) {
|
|
26083
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
26084
|
+
if (h.expiry > now)
|
|
26085
|
+
return true;
|
|
26086
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
26087
|
+
this.expired[path]++;
|
|
26088
|
+
return false;
|
|
26089
|
+
});
|
|
26090
|
+
}
|
|
26091
|
+
let keepFrom = null;
|
|
26092
|
+
for (const [d, list] of this.pending) {
|
|
26093
|
+
const live = list.filter((p) => {
|
|
26094
|
+
if (p.expiry > now)
|
|
26095
|
+
return true;
|
|
26096
|
+
if (keepFrom === null) {
|
|
26097
|
+
const age = this.keepPendingAgeMs?.();
|
|
26098
|
+
keepFrom = age === void 0 ? void 0 : now - age;
|
|
26099
|
+
}
|
|
26100
|
+
if (keepFrom !== void 0 && keepFrom !== null && p.expiry > keepFrom)
|
|
26101
|
+
return true;
|
|
26102
|
+
p.pair.pending = void 0;
|
|
26103
|
+
return false;
|
|
26104
|
+
});
|
|
26105
|
+
if (live.length)
|
|
26106
|
+
this.pending.set(d, live);
|
|
26107
|
+
else
|
|
26108
|
+
this.pending.delete(d);
|
|
26109
|
+
}
|
|
26110
|
+
for (const [d, until] of this.admittedUntil)
|
|
26111
|
+
if (until <= now)
|
|
26112
|
+
this.admittedUntil.delete(d);
|
|
26113
|
+
if (this.previous && now > this.previous.deadline)
|
|
26114
|
+
this.previous = null;
|
|
26115
|
+
}
|
|
26116
|
+
/**
|
|
26117
|
+
* Retire verified entries below their arrival path's replay floor, keeping a mirrored entry
|
|
26118
|
+
* while the freshness check would still pass its copy on `manifestPort`.
|
|
26119
|
+
* ponytail: linear scan per record; index by (kid, epoch) if retained sets grow past a few thousand.
|
|
26120
|
+
*/
|
|
26121
|
+
prune(now) {
|
|
26122
|
+
const retention = 7e3 + this.maxAuthDelayMs;
|
|
26123
|
+
for (const [id, v] of this.verified) {
|
|
26124
|
+
const h = this.highest[v.path].get(`${v.kid}:${v.epoch}`);
|
|
26125
|
+
const aboveFloor = h === void 0 || v.mseq > h - REPLAY_WINDOW;
|
|
26126
|
+
const fresh = this.cfg.checkFreshness === false ? v.arrival + retention >= now : v.sent * 1e3 + retention >= now;
|
|
26127
|
+
const current = this.current && (v.epoch === this.current.epoch || v.epoch === this.previous?.epoch);
|
|
26128
|
+
if (current && (aboveFloor || v.path === "mirror" && fresh))
|
|
26129
|
+
continue;
|
|
26130
|
+
this.verified.delete(id);
|
|
26131
|
+
}
|
|
26132
|
+
}
|
|
26133
|
+
};
|
|
26134
|
+
|
|
26135
|
+
// src/catalog-network-source.ts
|
|
26136
|
+
function normalizePort(value) {
|
|
26137
|
+
const n = typeof value === "number" ? value : typeof value === "string" ? Number(value) : Number.NaN;
|
|
26138
|
+
return Number.isInteger(n) && n > 0 && n <= 65535 ? n : void 0;
|
|
26139
|
+
}
|
|
26140
|
+
function catalogAmtNetworkSource(networkSource) {
|
|
26141
|
+
const entries = Array.isArray(networkSource) ? networkSource : networkSource ? [networkSource] : [];
|
|
26142
|
+
for (const raw of entries) {
|
|
26143
|
+
if (!raw || typeof raw !== "object") continue;
|
|
26144
|
+
const ns = raw;
|
|
26145
|
+
if (ns.type !== "amt") continue;
|
|
26146
|
+
if (ns.discovery === "driad") continue;
|
|
26147
|
+
const relay = typeof ns.relay === "string" ? ns.relay.trim() : "";
|
|
26148
|
+
if (!relay) continue;
|
|
26149
|
+
return { relay, port: normalizePort(ns.port) ?? DEFAULT_AMT_RELAY_PORT };
|
|
26150
|
+
}
|
|
26151
|
+
return void 0;
|
|
26152
|
+
}
|
|
26153
|
+
function intIn(value, min, max) {
|
|
26154
|
+
return Number.isInteger(value) && value >= min && value <= max;
|
|
26155
|
+
}
|
|
26156
|
+
function catalogAtsc3NetworkSource(networkSource) {
|
|
26157
|
+
const entries = Array.isArray(networkSource) ? networkSource : networkSource ? [networkSource] : [];
|
|
26158
|
+
for (const raw of entries) {
|
|
26159
|
+
if (!raw || typeof raw !== "object") continue;
|
|
26160
|
+
const ns = raw;
|
|
26161
|
+
if (ns.type !== "atsc3" || !intIn(ns.frequency, 1, Number.MAX_SAFE_INTEGER)) continue;
|
|
26162
|
+
const slsUri = typeof ns.slsUri === "string" ? ns.slsUri.trim() : "";
|
|
26163
|
+
return {
|
|
26164
|
+
frequency: ns.frequency,
|
|
26165
|
+
...intIn(ns.plpId, 0, 63) ? { plpId: ns.plpId } : {},
|
|
26166
|
+
...intIn(ns.serviceId, 0, 65535) ? { serviceId: ns.serviceId } : {},
|
|
26167
|
+
...slsUri ? { slsUri } : {}
|
|
26168
|
+
};
|
|
26169
|
+
}
|
|
26170
|
+
return void 0;
|
|
26171
|
+
}
|
|
26172
|
+
|
|
26173
|
+
// src/catalog-fec-params.ts
|
|
26174
|
+
var MAX_SAFE_TICKS_PER_HZ = Number.MAX_SAFE_INTEGER / 1e3;
|
|
26175
|
+
function timescaleHz(value) {
|
|
23295
26176
|
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : void 0;
|
|
23296
26177
|
}
|
|
23297
26178
|
function catalogGroupDurationMs(rendition) {
|
|
@@ -23602,11 +26483,12 @@ function resolveCatalogTrackFec(catalog, tracks) {
|
|
|
23602
26483
|
}
|
|
23603
26484
|
|
|
23604
26485
|
// src/multicast-manager.ts
|
|
23605
|
-
function freezeIngress(tracks) {
|
|
26486
|
+
function freezeIngress(tracks, flow) {
|
|
23606
26487
|
return Object.freeze({
|
|
23607
26488
|
tracks: Object.freeze(
|
|
23608
26489
|
[...tracks].map(({ name, packetId }) => Object.freeze({ name, packetId }))
|
|
23609
|
-
)
|
|
26490
|
+
),
|
|
26491
|
+
...flow === void 0 ? {} : { flow }
|
|
23610
26492
|
});
|
|
23611
26493
|
}
|
|
23612
26494
|
function sameIdSet(a, b) {
|
|
@@ -23636,6 +26518,7 @@ function newBlockPolicyStats(packetId, catalogTrackName, epoch) {
|
|
|
23636
26518
|
repairsSkippedRttExceedsBudget: 0,
|
|
23637
26519
|
repairsSkippedPresentationDelayUnavailable: 0,
|
|
23638
26520
|
repairsSkippedRangeNoAnchor: 0,
|
|
26521
|
+
repairsSkippedGroupCannotComplete: 0,
|
|
23639
26522
|
lastRepairSkip: null,
|
|
23640
26523
|
keyframeRepairFetchesSent: 0,
|
|
23641
26524
|
rangeRepairFetchesSent: 0,
|
|
@@ -23720,7 +26603,9 @@ var REPAIR_PRIORITY_REASONS = /* @__PURE__ */ new Set([
|
|
|
23720
26603
|
var REPAIR_SKIP_REASONS = /* @__PURE__ */ new Set([
|
|
23721
26604
|
"rtt-unmeasured",
|
|
23722
26605
|
"rtt-exceeds-budget",
|
|
23723
|
-
"presentation-delay-unavailable"
|
|
26606
|
+
"presentation-delay-unavailable",
|
|
26607
|
+
"range-no-anchor",
|
|
26608
|
+
"group-cannot-complete"
|
|
23724
26609
|
]);
|
|
23725
26610
|
var REPAIR_DEADLINE_BOUNDS = /* @__PURE__ */ new Set(["wait", "presentation", "keyframe", "retention"]);
|
|
23726
26611
|
var GAP_REASONS = {
|
|
@@ -23938,7 +26823,7 @@ function altGroupKey(track) {
|
|
|
23938
26823
|
}
|
|
23939
26824
|
function repairTrackNamesBySource(tracks) {
|
|
23940
26825
|
const bySource = /* @__PURE__ */ new Map();
|
|
23941
|
-
const
|
|
26826
|
+
const add2 = (source, repair) => {
|
|
23942
26827
|
if (!source || !repair) return;
|
|
23943
26828
|
const names = bySource.get(source) ?? /* @__PURE__ */ new Set();
|
|
23944
26829
|
names.add(repair);
|
|
@@ -23946,9 +26831,9 @@ function repairTrackNamesBySource(tracks) {
|
|
|
23946
26831
|
};
|
|
23947
26832
|
for (const track of tracks) {
|
|
23948
26833
|
const repairTrack = typeof track.fec === "object" && track.fec !== null ? track.fec.repairTrack : void 0;
|
|
23949
|
-
if (typeof repairTrack === "string")
|
|
26834
|
+
if (typeof repairTrack === "string") add2(track.name, repairTrack);
|
|
23950
26835
|
if (track.packaging === "fec-repair" && Array.isArray(track.depends) && track.depends.length === 1) {
|
|
23951
|
-
|
|
26836
|
+
add2(track.depends[0], track.name);
|
|
23952
26837
|
}
|
|
23953
26838
|
}
|
|
23954
26839
|
return bySource;
|
|
@@ -23958,6 +26843,10 @@ var MulticastManager = class {
|
|
|
23958
26843
|
#router;
|
|
23959
26844
|
#hostMfuTimeoutMs;
|
|
23960
26845
|
#transport;
|
|
26846
|
+
/** The protected flow of the current join and its manifestPort subscription. */
|
|
26847
|
+
#provenanceJoin = null;
|
|
26848
|
+
#onFailback = null;
|
|
26849
|
+
#failbackHooked = false;
|
|
23961
26850
|
/** Live joinRepairEndpoints() releases → the repair owners they registered,
|
|
23962
26851
|
* re-applied after configureMultiTrack() clears catalog-derived owners. */
|
|
23963
26852
|
#repairReleases = /* @__PURE__ */ new Map();
|
|
@@ -24010,10 +26899,6 @@ var MulticastManager = class {
|
|
|
24010
26899
|
reassemblyPolicy: options?.reassemblyPolicy,
|
|
24011
26900
|
reassembler: options?.reassembler,
|
|
24012
26901
|
maxBufferedMfus: options?.maxBufferedMfus,
|
|
24013
|
-
manifestVerifier: options?.manifestVerifier,
|
|
24014
|
-
manifestVerifierOwnership: options?.manifestVerifierOwnership,
|
|
24015
|
-
resolveManifestIdentity: options?.resolveManifestIdentity,
|
|
24016
|
-
resolveManifestIdentityForRoute: options?.resolveManifestIdentityForRoute,
|
|
24017
26902
|
// The router has reset its own state for the stream; reset the rest.
|
|
24018
26903
|
onPublisherRestart: (packetId, catalogTrackName) => {
|
|
24019
26904
|
this.#resetStreamOutsideRouter(packetId, catalogTrackName);
|
|
@@ -24060,7 +26945,7 @@ var MulticastManager = class {
|
|
|
24060
26945
|
});
|
|
24061
26946
|
this.#hostMfuTimeoutMs = this.#router.reassembler.timeoutMs;
|
|
24062
26947
|
this.#transport = new MulticastTransport((packet, ingress) => {
|
|
24063
|
-
this.#router.feedPacket(packet, "multicast",
|
|
26948
|
+
this.#router.feedPacket(packet, "multicast", ingress);
|
|
24064
26949
|
});
|
|
24065
26950
|
this.#transport.onDelivery((delivery) => {
|
|
24066
26951
|
this.#fec.setMulticastSymbolDelivery(delivery);
|
|
@@ -24068,17 +26953,7 @@ var MulticastManager = class {
|
|
|
24068
26953
|
this.#fec.onBlockRecovered = (sbn, packetId, data, meta, recoveredTrackName) => {
|
|
24069
26954
|
const recovered = new Uint8Array(data);
|
|
24070
26955
|
const catalogTrackName = recoveredTrackName ?? this.#fec.catalogTrackNameForBlock(sbn, packetId);
|
|
24071
|
-
|
|
24072
|
-
this.#router.routeRecoveredSymbol(sbn, recovered, packetId, meta.esi);
|
|
24073
|
-
} else {
|
|
24074
|
-
this.#router.routeRecoveredSymbol(
|
|
24075
|
-
sbn,
|
|
24076
|
-
recovered,
|
|
24077
|
-
packetId,
|
|
24078
|
-
meta.esi,
|
|
24079
|
-
catalogTrackName
|
|
24080
|
-
);
|
|
24081
|
-
}
|
|
26956
|
+
return catalogTrackName === void 0 ? this.#router.routeRecoveredSymbol(sbn, recovered, packetId, meta.esi) : this.#router.routeRecoveredSymbol(sbn, recovered, packetId, meta.esi, catalogTrackName);
|
|
24082
26957
|
};
|
|
24083
26958
|
this.#fec.onRepairResolved = (sbn, packetId, _outcome, catalogTrackName) => {
|
|
24084
26959
|
this.clearRepairForBlock(sbn, packetId, catalogTrackName);
|
|
@@ -24153,6 +27028,10 @@ var MulticastManager = class {
|
|
|
24153
27028
|
get router() {
|
|
24154
27029
|
return this.#router;
|
|
24155
27030
|
}
|
|
27031
|
+
/** Repair FETCH wiring (wireRepairFetch): this manager can request unicast repair. */
|
|
27032
|
+
setUnicastRepair(enabled) {
|
|
27033
|
+
this.#router.setUnicastRepair(enabled);
|
|
27034
|
+
}
|
|
24156
27035
|
get transport() {
|
|
24157
27036
|
return this.#transport;
|
|
24158
27037
|
}
|
|
@@ -24563,8 +27442,35 @@ var MulticastManager = class {
|
|
|
24563
27442
|
onTakeover(cb) {
|
|
24564
27443
|
this.#transport.onTakeover(cb);
|
|
24565
27444
|
}
|
|
27445
|
+
/** Failback un-protects the join's flow (7.2.7 failover), so the fallback's MoQ packets are read. */
|
|
24566
27446
|
onFailback(cb) {
|
|
24567
|
-
this.#
|
|
27447
|
+
this.#onFailback = cb;
|
|
27448
|
+
this.#hookFailback();
|
|
27449
|
+
}
|
|
27450
|
+
/**
|
|
27451
|
+
* Install the manager's own failback listener on the media transport, which
|
|
27452
|
+
* arms its silence watchdog: a protected join un-protects on failover even
|
|
27453
|
+
* when no player listens, else its tracks' MoQ copies stay dropped.
|
|
27454
|
+
*/
|
|
27455
|
+
#hookFailback() {
|
|
27456
|
+
if (this.#failbackHooked) return;
|
|
27457
|
+
this.#failbackHooked = true;
|
|
27458
|
+
this.#transport.onFailback((reason) => {
|
|
27459
|
+
const joined = this.#provenanceJoin;
|
|
27460
|
+
if (joined !== null) this.#router.unprotectProvenanceFlow(joined.flow);
|
|
27461
|
+
this.#onFailback?.(reason);
|
|
27462
|
+
});
|
|
27463
|
+
}
|
|
27464
|
+
/**
|
|
27465
|
+
* A protected join's handoff (draft -01 7.2.7), before `onTakeover`: its
|
|
27466
|
+
* first admitted datagram. MoQ copies of the flow's tracks the router still
|
|
27467
|
+
* held unadmitted are discarded, and later ones are dropped. Fires again
|
|
27468
|
+
* after a failback once multicast resumes.
|
|
27469
|
+
*/
|
|
27470
|
+
onProtected(cb) {
|
|
27471
|
+
this.#router.onProvenanceFlowProtected(cb && ((flow) => {
|
|
27472
|
+
if (this.#provenanceJoin?.flow === flow) cb();
|
|
27473
|
+
}));
|
|
24568
27474
|
}
|
|
24569
27475
|
/** See MulticastTransport.onDataPlaneLive: the join's first accepted packet. */
|
|
24570
27476
|
onDataPlaneLive(cb) {
|
|
@@ -24580,6 +27486,12 @@ var MulticastManager = class {
|
|
|
24580
27486
|
return MulticastTransport.isAvailable();
|
|
24581
27487
|
}
|
|
24582
27488
|
async join(config) {
|
|
27489
|
+
const provenanceFlow = config.provenance?.flow;
|
|
27490
|
+
if (provenanceFlow !== void 0 && ((config.sourceAddress ?? config.senderAddress) !== provenanceFlow.source || config.groupAddress !== provenanceFlow.groupAddress || config.port !== provenanceFlow.port)) {
|
|
27491
|
+
throw new RangeError(
|
|
27492
|
+
"[MulticastManager] join: provenance flow (S, G, port) must match sourceAddress ?? senderAddress, groupAddress and port"
|
|
27493
|
+
);
|
|
27494
|
+
}
|
|
24583
27495
|
const staged = this.#renditionSwitch;
|
|
24584
27496
|
if (staged !== null) this.#endRenditionSwitch(staged, { reason: "superseded" }, false);
|
|
24585
27497
|
this.configureFec(config);
|
|
@@ -24587,16 +27499,21 @@ var MulticastManager = class {
|
|
|
24587
27499
|
const selectedPacketIds = config.packetIds?.length ? new Set(config.packetIds) : null;
|
|
24588
27500
|
const catalogTracks = config.tracks?.filter(({ packetId }) => selectedPacketIds === null || selectedPacketIds.has(packetId));
|
|
24589
27501
|
const ingressTracks = catalogTracks ? catalogTracks : selectedPacketIds === null ? void 0 : [...selectedPacketIds].map((packetId) => ({ name: "", packetId }));
|
|
24590
|
-
|
|
27502
|
+
this.#releaseProvenanceJoin();
|
|
27503
|
+
const provenance = config.provenance;
|
|
27504
|
+
const protectedTracks = ingressTracks ?? provenance?.flow.tracks;
|
|
27505
|
+
const ingress = protectedTracks === void 0 ? void 0 : {
|
|
24591
27506
|
tracks: Object.freeze(
|
|
24592
|
-
|
|
27507
|
+
protectedTracks.map(
|
|
24593
27508
|
({ name, packetId }) => Object.freeze({ name, packetId })
|
|
24594
27509
|
)
|
|
24595
|
-
)
|
|
27510
|
+
),
|
|
27511
|
+
...provenance === void 0 ? {} : { flow: multicastProvenanceFlowKey(provenance.flow) }
|
|
24596
27512
|
};
|
|
24597
27513
|
const amtRelay = config.amtRelay;
|
|
24598
27514
|
const transportConfig = {
|
|
24599
27515
|
sourceAddress: config.sourceAddress,
|
|
27516
|
+
senderAddress: config.senderAddress,
|
|
24600
27517
|
groupAddress: config.groupAddress,
|
|
24601
27518
|
port: config.port,
|
|
24602
27519
|
mode: config.mode,
|
|
@@ -24623,7 +27540,61 @@ var MulticastManager = class {
|
|
|
24623
27540
|
packetIds: config.packetIds,
|
|
24624
27541
|
tracks: catalogTracks?.map(({ name, packetId }) => ({ name, packetId }))
|
|
24625
27542
|
};
|
|
24626
|
-
|
|
27543
|
+
if (provenance !== void 0) this.#joinProvenance(provenance, ingress, transportConfig);
|
|
27544
|
+
const protectedJoin = this.#provenanceJoin;
|
|
27545
|
+
const joined = await (ingress === void 0 ? this.#transport.join(transportConfig) : this.#transport.join(transportConfig, ingress));
|
|
27546
|
+
if (!joined && protectedJoin !== null && this.#provenanceJoin === protectedJoin) this.#releaseProvenanceJoin();
|
|
27547
|
+
return joined;
|
|
27548
|
+
}
|
|
27549
|
+
/**
|
|
27550
|
+
* Register the protected flow and subscribe its `manifestPort` on the same
|
|
27551
|
+
* `(S, G)` with its own listener (draft -01 7.2.7), so records reach the
|
|
27552
|
+
* flow's receiver and media datagrams are held until one admits them. A
|
|
27553
|
+
* manifest subscription that fails to join admits nothing: the media is
|
|
27554
|
+
* held and expires, never read unauthenticated.
|
|
27555
|
+
*/
|
|
27556
|
+
#joinProvenance(provenance, ingress, media) {
|
|
27557
|
+
const { flow, auth } = provenance;
|
|
27558
|
+
const receiver = new BcProvenanceV2Receiver({
|
|
27559
|
+
...provenance.receiver,
|
|
27560
|
+
// The caller's ctx when it passes one, else the catalog's: an explicit
|
|
27561
|
+
// `catalogCtx: undefined` must not switch off the 7.2.4 ctx check.
|
|
27562
|
+
catalogCtx: provenance.receiver?.catalogCtx ?? flow.ctx,
|
|
27563
|
+
endpoint: {
|
|
27564
|
+
sourceAddress: flow.source,
|
|
27565
|
+
groupAddress: flow.groupAddress,
|
|
27566
|
+
port: flow.port,
|
|
27567
|
+
manifestPort: flow.manifestPort,
|
|
27568
|
+
streamId: flow.streamId
|
|
27569
|
+
},
|
|
27570
|
+
keys: auth.keys,
|
|
27571
|
+
minEpoch: auth.minEpoch,
|
|
27572
|
+
bandwidth: flow.bandwidth,
|
|
27573
|
+
maxAuthDelayMs: flow.maxAuthDelayMs,
|
|
27574
|
+
manifestTimerMs: flow.manifestTimerMs
|
|
27575
|
+
});
|
|
27576
|
+
this.#router.registerProvenanceFlow(ingress, receiver);
|
|
27577
|
+
this.#hookFailback();
|
|
27578
|
+
const manifest = new MulticastTransport((record) => {
|
|
27579
|
+
if (this.#provenanceJoin?.manifest === manifest) {
|
|
27580
|
+
this.#router.feedProvenanceRecord(ingress.flow, record, "multicast");
|
|
27581
|
+
}
|
|
27582
|
+
});
|
|
27583
|
+
this.#provenanceJoin = { flow: ingress.flow, manifest };
|
|
27584
|
+
const { packetIds: _packetIds, tracks: _tracks, ...path } = media;
|
|
27585
|
+
void manifest.join({ ...path, port: flow.manifestPort }).then((joined) => {
|
|
27586
|
+
if (!joined && this.#provenanceJoin?.manifest === manifest) {
|
|
27587
|
+
console.warn(`[MulticastManager] manifestPort ${flow.groupAddress}:${flow.manifestPort} not joined`, manifest.lastJoinOutcome);
|
|
27588
|
+
}
|
|
27589
|
+
});
|
|
27590
|
+
}
|
|
27591
|
+
/** Un-protect: drop the flow's hold queues (no expiry counted) and leave its manifestPort. */
|
|
27592
|
+
#releaseProvenanceJoin() {
|
|
27593
|
+
const joined = this.#provenanceJoin;
|
|
27594
|
+
if (joined === null) return;
|
|
27595
|
+
this.#provenanceJoin = null;
|
|
27596
|
+
joined.manifest.dispose();
|
|
27597
|
+
this.#router.releaseProvenanceFlow(joined.flow);
|
|
24627
27598
|
}
|
|
24628
27599
|
/**
|
|
24629
27600
|
* Join the selected rung's dedicated repair groups beside the media join
|
|
@@ -24653,10 +27624,11 @@ var MulticastManager = class {
|
|
|
24653
27624
|
this.#applyRepairOwners(repairOwners);
|
|
24654
27625
|
for (const endpoint of endpoints) {
|
|
24655
27626
|
const ingress = {
|
|
24656
|
-
tracks: Object.freeze(endpoint.tracks.map(({ name, packetId }) => Object.freeze({ name, packetId })))
|
|
27627
|
+
tracks: Object.freeze(endpoint.tracks.map(({ name, packetId }) => Object.freeze({ name, packetId }))),
|
|
27628
|
+
...endpoint.flow === void 0 ? {} : { flow: endpoint.flow }
|
|
24657
27629
|
};
|
|
24658
27630
|
const onPacket = (packet) => {
|
|
24659
|
-
if (!released) this.#router.feedPacket(packet, "multicast",
|
|
27631
|
+
if (!released) this.#router.feedPacket(packet, "multicast", ingress);
|
|
24660
27632
|
};
|
|
24661
27633
|
const fail = (error) => console.warn(`[MulticastManager] repair group ${endpoint.group}:${endpoint.port} not joined`, error);
|
|
24662
27634
|
if (typeof provider === "function") {
|
|
@@ -24794,7 +27766,7 @@ var MulticastManager = class {
|
|
|
24794
27766
|
for (const track of [...live.tracks, ...request.tracks]) {
|
|
24795
27767
|
stagedTracks.set(`${track.packetId}\0${track.name}`, track);
|
|
24796
27768
|
}
|
|
24797
|
-
const stagedIngress = freezeIngress(stagedTracks.values());
|
|
27769
|
+
const stagedIngress = freezeIngress(stagedTracks.values(), live.flow);
|
|
24798
27770
|
const stagedPacketIds = committedPacketIds === void 0 || livePacketIds === null ? null : /* @__PURE__ */ new Set([...livePacketIds, ...committedPacketIds]);
|
|
24799
27771
|
const widened = stagedPacketIds !== null && !sameIdSet(stagedPacketIds, livePacketIds);
|
|
24800
27772
|
const active = {
|
|
@@ -24805,7 +27777,7 @@ var MulticastManager = class {
|
|
|
24805
27777
|
liveIngress: live,
|
|
24806
27778
|
livePacketIds,
|
|
24807
27779
|
widened,
|
|
24808
|
-
committedIngress: freezeIngress(request.tracks),
|
|
27780
|
+
committedIngress: freezeIngress(request.tracks, live.flow),
|
|
24809
27781
|
committedPacketIds,
|
|
24810
27782
|
router: null,
|
|
24811
27783
|
state: "staged"
|
|
@@ -24818,7 +27790,7 @@ var MulticastManager = class {
|
|
|
24818
27790
|
let widening;
|
|
24819
27791
|
try {
|
|
24820
27792
|
if (widened) widening = this.#transport.setPacketIds([...stagedPacketIds]);
|
|
24821
|
-
this.#
|
|
27793
|
+
this.#setSwitchIngress(stagedIngress);
|
|
24822
27794
|
} catch (error) {
|
|
24823
27795
|
active.router.cancel();
|
|
24824
27796
|
throw error;
|
|
@@ -24839,6 +27811,11 @@ var MulticastManager = class {
|
|
|
24839
27811
|
}
|
|
24840
27812
|
};
|
|
24841
27813
|
}
|
|
27814
|
+
/** Route a switch's ingress and keep a protected flow's registration in step (7.2.7). */
|
|
27815
|
+
#setSwitchIngress(ingress) {
|
|
27816
|
+
this.#transport.setContext(ingress);
|
|
27817
|
+
if (ingress.flow !== void 0) this.#router.updateProvenanceFlowIngress({ ...ingress, flow: ingress.flow });
|
|
27818
|
+
}
|
|
24842
27819
|
/** The router committed the staged switch (it already retired `from`). */
|
|
24843
27820
|
#commitRenditionSwitch(active, commit) {
|
|
24844
27821
|
if (this.#renditionSwitch !== active) return;
|
|
@@ -24846,7 +27823,7 @@ var MulticastManager = class {
|
|
|
24846
27823
|
active.state = "committed";
|
|
24847
27824
|
active.router = null;
|
|
24848
27825
|
if (this.#transport.joined) {
|
|
24849
|
-
this.#
|
|
27826
|
+
this.#setSwitchIngress(active.committedIngress);
|
|
24850
27827
|
if (active.committedPacketIds !== void 0) {
|
|
24851
27828
|
this.#transport.setPacketIds(active.committedPacketIds).catch((error) => {
|
|
24852
27829
|
console.warn(
|
|
@@ -24882,7 +27859,7 @@ var MulticastManager = class {
|
|
|
24882
27859
|
active.router = null;
|
|
24883
27860
|
if (restore) {
|
|
24884
27861
|
if (this.#transport.joined) {
|
|
24885
|
-
this.#
|
|
27862
|
+
this.#setSwitchIngress(active.liveIngress);
|
|
24886
27863
|
if (active.widened && active.livePacketIds !== null) {
|
|
24887
27864
|
this.#transport.setPacketIds([...active.livePacketIds]).catch((error) => {
|
|
24888
27865
|
console.warn(
|
|
@@ -24954,11 +27931,11 @@ var MulticastManager = class {
|
|
|
24954
27931
|
}
|
|
24955
27932
|
/** Route a Worker-recovered FEC block through the MMTP parser + dedup pipeline. */
|
|
24956
27933
|
routeRecoveredBlock(blockId, data, packetId, catalogTrackName) {
|
|
24957
|
-
this.#router.routeRecoveredBlock(blockId, data, packetId, catalogTrackName);
|
|
27934
|
+
return this.#router.routeRecoveredBlock(blockId, data, packetId, catalogTrackName);
|
|
24958
27935
|
}
|
|
24959
27936
|
/** Route one Worker-recovered source symbol through the MMTP parser + dedup pipeline. */
|
|
24960
27937
|
routeRecoveredSymbol(blockId, data, packetId, esi = 0, catalogTrackName) {
|
|
24961
|
-
this.#router.routeRecoveredSymbol(blockId, data, packetId, esi, catalogTrackName);
|
|
27938
|
+
return this.#router.routeRecoveredSymbol(blockId, data, packetId, esi, catalogTrackName);
|
|
24962
27939
|
}
|
|
24963
27940
|
// ---------------------------------------------------------------------------
|
|
24964
27941
|
// Phase 14: multi-track catalog ingest (D-04, D-06, D-07, D-17, D-26, D-28)
|
|
@@ -25268,6 +28245,7 @@ var MulticastManager = class {
|
|
|
25268
28245
|
if (reason === "rtt-unmeasured") stats.repairsSkippedRttUnmeasured++;
|
|
25269
28246
|
else if (reason === "rtt-exceeds-budget") stats.repairsSkippedRttExceedsBudget++;
|
|
25270
28247
|
else if (reason === "range-no-anchor") stats.repairsSkippedRangeNoAnchor++;
|
|
28248
|
+
else if (reason === "group-cannot-complete") stats.repairsSkippedGroupCannotComplete++;
|
|
25271
28249
|
else stats.repairsSkippedPresentationDelayUnavailable++;
|
|
25272
28250
|
stats.lastRepairSkip = {
|
|
25273
28251
|
reason,
|
|
@@ -25463,11 +28441,15 @@ var MulticastManager = class {
|
|
|
25463
28441
|
this.#trackResetObservers.clear();
|
|
25464
28442
|
this.#repairLifecycleObservers.clear();
|
|
25465
28443
|
this.#transport.dispose();
|
|
28444
|
+
this.#releaseProvenanceJoin();
|
|
25466
28445
|
for (const release of [...this.#repairReleases.keys()]) release();
|
|
25467
28446
|
this.#router.dispose();
|
|
25468
28447
|
this.#fec.dispose();
|
|
25469
28448
|
}
|
|
25470
28449
|
};
|
|
28450
|
+
function multicastProvenanceFlowKey(flow) {
|
|
28451
|
+
return JSON.stringify([flow.source, flow.groupAddress, flow.port]);
|
|
28452
|
+
}
|
|
25471
28453
|
|
|
25472
28454
|
// src/fec-worker-setup.ts
|
|
25473
28455
|
async function setupFecWorkerForTrack(opts) {
|
|
@@ -27279,6 +30261,9 @@ var MmtpFecSession = class {
|
|
|
27279
30261
|
/** The newest SS_ID when the remembered SS_IDs were last pruned. */
|
|
27280
30262
|
#prunedAt;
|
|
27281
30263
|
#repairPacketId;
|
|
30264
|
+
#newestSource;
|
|
30265
|
+
#heldLayers;
|
|
30266
|
+
#anchorHorizon;
|
|
27282
30267
|
#stats = {
|
|
27283
30268
|
sourceSymbols: 0,
|
|
27284
30269
|
repairSymbols: 0,
|
|
@@ -27291,11 +30276,12 @@ var MmtpFecSession = class {
|
|
|
27291
30276
|
dropped: {},
|
|
27292
30277
|
sourceSymbolsByPath: { moq: 0, multicast: 0 },
|
|
27293
30278
|
repairSymbolsByPath: { moq: 0, multicast: 0 },
|
|
27294
|
-
mixedPath: { clean: 0, recovered: 0, failed: 0, released: 0 }
|
|
30279
|
+
mixedPath: { clean: 0, recovered: 0, failed: 0, released: 0 },
|
|
30280
|
+
failClosed: null
|
|
27295
30281
|
};
|
|
27296
30282
|
/**
|
|
27297
30283
|
* @throws when the catalog block lacks or misstates a field the session
|
|
27298
|
-
* needs: the block profile, K, T, `interleaveDepthMs` or
|
|
30284
|
+
* needs: the block profile, K, P_0 (`repairSymbols`), T, `interleaveDepthMs` or
|
|
27299
30285
|
* `reorderToleranceMs`. None is defaulted.
|
|
27300
30286
|
*/
|
|
27301
30287
|
constructor(config) {
|
|
@@ -27309,6 +30295,7 @@ var MmtpFecSession = class {
|
|
|
27309
30295
|
}
|
|
27310
30296
|
this.algorithm = fec.algorithm === "reed-solomon" ? "reed-solomon" : "raptorq";
|
|
27311
30297
|
this.sourceSymbols = requirePositiveInteger(fec.sourceSymbols, "fec.sourceSymbols");
|
|
30298
|
+
this.repairSymbols = requirePositiveInteger(fec.repairSymbols, "fec.repairSymbols");
|
|
27312
30299
|
this.symbolSize = requirePositiveInteger(fec.symbolSize, "fec.symbolSize");
|
|
27313
30300
|
const interleaveDepthMs = requireNumber(fec.interleaveDepthMs, "fec.interleaveDepthMs");
|
|
27314
30301
|
const reorderToleranceMs = requireNumber(fec.reorderToleranceMs, "fec.reorderToleranceMs");
|
|
@@ -27316,6 +30303,18 @@ var MmtpFecSession = class {
|
|
|
27316
30303
|
fecBlockBudgetMs(interleaveDepthMs, reorderToleranceMs),
|
|
27317
30304
|
interleaveDepthMs
|
|
27318
30305
|
);
|
|
30306
|
+
this.#heldLayers = new HeldLayerAdmission(
|
|
30307
|
+
config.repairLayers ?? [
|
|
30308
|
+
{
|
|
30309
|
+
layer: 0,
|
|
30310
|
+
repairTrackName: typeof fec.repairTrack === "string" ? fec.repairTrack : "",
|
|
30311
|
+
rsIdOffset: 0,
|
|
30312
|
+
repairSymbols: this.repairSymbols
|
|
30313
|
+
}
|
|
30314
|
+
],
|
|
30315
|
+
config.heldLayers
|
|
30316
|
+
);
|
|
30317
|
+
this.#anchorHorizon = heldLayerAnchorHorizon(this.retentionBlocks, this.sourceSymbols);
|
|
27319
30318
|
if (typeof config.Decoder !== "function") {
|
|
27320
30319
|
throw new Error("MmtpFecSession: a decoder constructor (wasm_bindgen.MmtFecDecoder) is required");
|
|
27321
30320
|
}
|
|
@@ -27338,7 +30337,8 @@ var MmtpFecSession = class {
|
|
|
27338
30337
|
dropped: { ...this.#stats.dropped },
|
|
27339
30338
|
sourceSymbolsByPath: { ...this.#stats.sourceSymbolsByPath },
|
|
27340
30339
|
repairSymbolsByPath: { ...this.#stats.repairSymbolsByPath },
|
|
27341
|
-
mixedPath: { ...this.#stats.mixedPath }
|
|
30340
|
+
mixedPath: { ...this.#stats.mixedPath },
|
|
30341
|
+
failClosed: this.#stats.failClosed && { ...this.#stats.failClosed }
|
|
27342
30342
|
};
|
|
27343
30343
|
}
|
|
27344
30344
|
/**
|
|
@@ -27363,6 +30363,7 @@ var MmtpFecSession = class {
|
|
|
27363
30363
|
this.#stats.sourceSymbols++;
|
|
27364
30364
|
this.#stats.sourceSymbolsByPath[path]++;
|
|
27365
30365
|
this.#noteNewest(ssId);
|
|
30366
|
+
if (this.#newestSource === void 0 || serial32Precedes(this.#newestSource, ssId)) this.#newestSource = ssId;
|
|
27366
30367
|
const start = this.#blockStartOf(ssId);
|
|
27367
30368
|
const block = start === void 0 ? void 0 : this.#blocks.get(start);
|
|
27368
30369
|
if (block) {
|
|
@@ -27391,14 +30392,17 @@ var MmtpFecSession = class {
|
|
|
27391
30392
|
* Feeds one object of the repair track: a whole MMTP repair packet (ISO/IEC
|
|
27392
30393
|
* 23008-1 C.5.3). A repair track carries one repair flow, so the first
|
|
27393
30394
|
* repair packet fixes its packet ID and a packet of any other is refused.
|
|
27394
|
-
* `path` is where it was received, as for addSource().
|
|
30395
|
+
* `path` is where it was received, as for addSource(). `repairTrackName`
|
|
30396
|
+
* is the repair track it was received on, for the 6.3.9 own-track rule; a
|
|
30397
|
+
* track the layers do not list is refused as `repair:unlisted-track`.
|
|
27395
30398
|
*
|
|
27396
30399
|
* @returns the protected packets this repair symbol recovered, as for
|
|
27397
30400
|
* addSource().
|
|
27398
30401
|
*/
|
|
27399
|
-
addRepair(packet, path = "moq") {
|
|
30402
|
+
addRepair(packet, path = "moq", repairTrackName) {
|
|
27400
30403
|
const decoder = this.#decoder;
|
|
27401
30404
|
if (!decoder) return this.#drop("repair:released");
|
|
30405
|
+
if (this.#stats.failClosed) return this.#drop("repair:fail-closed");
|
|
27402
30406
|
const pathBit = PATH_BITS[path];
|
|
27403
30407
|
if (!pathBit) return this.#drop("repair:unknown-path");
|
|
27404
30408
|
let header;
|
|
@@ -27419,6 +30423,18 @@ var MmtpFecSession = class {
|
|
|
27419
30423
|
if (this.algorithm === "reed-solomon" && frame.ssbLength + frame.rsbLength > 255) {
|
|
27420
30424
|
return this.#drop("repair:rs-block-length");
|
|
27421
30425
|
}
|
|
30426
|
+
const verdict = this.#heldLayers.admit(
|
|
30427
|
+
repairTrackName,
|
|
30428
|
+
frame.ssStart,
|
|
30429
|
+
frame.rsbLength,
|
|
30430
|
+
this.#anchorHorizon,
|
|
30431
|
+
this.#newestSource
|
|
30432
|
+
);
|
|
30433
|
+
if (verdict === "discard") return this.#drop("repair:unlisted-track");
|
|
30434
|
+
if (verdict !== void 0) {
|
|
30435
|
+
this.#stats.failClosed = verdict;
|
|
30436
|
+
return this.#drop("repair:fail-closed");
|
|
30437
|
+
}
|
|
27422
30438
|
const start = frame.ssStart;
|
|
27423
30439
|
let block = this.#blocks.get(start);
|
|
27424
30440
|
if (block && block.ssbLength !== frame.ssbLength) return this.#drop("repair:conflicting-ssb-length");
|
|
@@ -27638,6 +30654,7 @@ function wireRepairFetch(mc, opts) {
|
|
|
27638
30654
|
if (opts.rangeTransport !== void 0 && (typeof opts.repairEpochReference !== "function" || !mc.onUndelimitedLoss || !mc.closeIsoHoles)) {
|
|
27639
30655
|
throw new Error("[RepairFetch] rangeTransport needs repairEpochReference and a host that reports undelimited losses");
|
|
27640
30656
|
}
|
|
30657
|
+
mc.setUnicastRepair?.(true);
|
|
27641
30658
|
const entries = /* @__PURE__ */ new Map();
|
|
27642
30659
|
const warnedNoTarget = /* @__PURE__ */ new Set();
|
|
27643
30660
|
let disposed = false;
|
|
@@ -27689,12 +30706,13 @@ function wireRepairFetch(mc, opts) {
|
|
|
27689
30706
|
const admit = (entry, now) => {
|
|
27690
30707
|
const budgetMs = entry.deadlineAt - now;
|
|
27691
30708
|
const rttMs = readRtt();
|
|
27692
|
-
|
|
30709
|
+
const reason = !entry.range && !groupCanComplete(entry.sbn, entry.packetId, entry.catalogTrackName) ? "group-cannot-complete" : rttMs === null ? "rtt-unmeasured" : rttMs < budgetMs ? null : "rtt-exceeds-budget";
|
|
30710
|
+
if (reason === null) return rttMs;
|
|
27693
30711
|
retire(entry);
|
|
27694
30712
|
releaseHold(entry);
|
|
27695
30713
|
clearEntry(entry);
|
|
27696
30714
|
mc.noteRepairSkipped(
|
|
27697
|
-
|
|
30715
|
+
reason,
|
|
27698
30716
|
{ rttMs, budgetMs, priority: entry.priority },
|
|
27699
30717
|
entry.sbn,
|
|
27700
30718
|
entry.packetId,
|
|
@@ -28123,6 +31141,7 @@ function wireRepairFetch(mc, opts) {
|
|
|
28123
31141
|
}
|
|
28124
31142
|
return () => {
|
|
28125
31143
|
disposed = true;
|
|
31144
|
+
mc.setUnicastRepair?.(false);
|
|
28126
31145
|
removeResetObserver();
|
|
28127
31146
|
removeLifecycleObserver();
|
|
28128
31147
|
for (const entry of [...entries.values()]) {
|
|
@@ -28332,4 +31351,16 @@ export {
|
|
|
28332
31351
|
wireRepairFetch,
|
|
28333
31352
|
wrapAdtsFrame
|
|
28334
31353
|
};
|
|
31354
|
+
/*! Bundled license information:
|
|
31355
|
+
|
|
31356
|
+
@noble/hashes/esm/utils.js:
|
|
31357
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
31358
|
+
|
|
31359
|
+
@noble/curves/esm/utils.js:
|
|
31360
|
+
@noble/curves/esm/abstract/modular.js:
|
|
31361
|
+
@noble/curves/esm/abstract/curve.js:
|
|
31362
|
+
@noble/curves/esm/abstract/edwards.js:
|
|
31363
|
+
@noble/curves/esm/ed25519.js:
|
|
31364
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
31365
|
+
*/
|
|
28335
31366
|
//# sourceMappingURL=browser-runtime.js.map
|