@blockcast/mmt-container 0.4.1-main.22841c1f0a2d → 0.4.1-main.4247ef4920bb
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser-runtime.js +2960 -37
- package/dist/browser-runtime.js.map +4 -4
- package/dist/delivery-catalog.js +18 -2
- package/dist/delivery-catalog.js.map +2 -2
- package/dist/mmtp-router.d.ts +24 -0
- package/dist/mmtp-router.d.ts.map +1 -1
- package/dist/mmtp-router.js +83 -2
- package/dist/mmtp-router.js.map +2 -2
- package/dist/multicast-manager.d.ts +41 -1
- package/dist/multicast-manager.d.ts.map +1 -1
- package/dist/multicast-manager.js +2953 -30
- 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/repair-fetch.d.ts +8 -1
- package/dist/repair-fetch.d.ts.map +1 -1
- package/dist/repair-fetch.js +3 -2
- package/dist/repair-fetch.js.map +2 -2
- package/package.json +5 -5
- package/src/mmtp-router.ts +99 -2
- package/src/multicast-manager.ts +169 -13
- package/src/multicast-transport.ts +6 -2
- package/src/repair-fetch.ts +20 -4
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.
|
|
@@ -13844,12 +13860,12 @@ var FECManager = class _FECManager {
|
|
|
13844
13860
|
* still be held behind it. Retirement is swept lazily here, on the stream's
|
|
13845
13861
|
* own terminal events, so it needs no timer.
|
|
13846
13862
|
*/
|
|
13847
|
-
#markTerminal(key, sbn, packetId,
|
|
13863
|
+
#markTerminal(key, sbn, packetId, clean2) {
|
|
13848
13864
|
const nowMs = performance.now();
|
|
13849
13865
|
this.#retireTerminalBlocks(packetId, nowMs);
|
|
13850
13866
|
this.#forgetOrderedPending(key);
|
|
13851
13867
|
this.#completedBlocks.add(key);
|
|
13852
|
-
if (
|
|
13868
|
+
if (clean2) this.#cleanlyCompletedBlocks.add(key);
|
|
13853
13869
|
this.#indexBlockKey(key, sbn, packetId);
|
|
13854
13870
|
const config = this.#trackConfig(packetId);
|
|
13855
13871
|
const expiresAtMs = nowMs + config.interleaveDepthMs + 2 * this.#blockLifetimeMs(config);
|
|
@@ -19399,6 +19415,8 @@ var MmtpRouter = class {
|
|
|
19399
19415
|
#fecMappersByPacketId = /* @__PURE__ */ new Map();
|
|
19400
19416
|
/** Protected flows: one 7.2.7 receiver each, and the ingress its datagrams arrive with. */
|
|
19401
19417
|
#provenance = /* @__PURE__ */ new Map();
|
|
19418
|
+
#onFlowProtected = null;
|
|
19419
|
+
#unadmittedListedDrops = 0;
|
|
19402
19420
|
/** The ingress a held datagram arrived with; the receiver returns the same bytes on release. */
|
|
19403
19421
|
#heldIngress = /* @__PURE__ */ new WeakMap();
|
|
19404
19422
|
/** The flow whose receiver admitted a stream's source datagrams, by packetId then catalog track name: recovered packets answer to it. */
|
|
@@ -20083,6 +20101,74 @@ var MmtpRouter = class {
|
|
|
20083
20101
|
this.#provenance.get(ingress.flow)?.receiver.release();
|
|
20084
20102
|
this.#provenance.set(ingress.flow, { receiver, ingress });
|
|
20085
20103
|
}
|
|
20104
|
+
/**
|
|
20105
|
+
* Replace a registered flow's ingress (a rendition switch staged, committed
|
|
20106
|
+
* or cancelled) while keeping its receiver; a no-op for an unregistered flow.
|
|
20107
|
+
*/
|
|
20108
|
+
updateProvenanceFlowIngress(ingress) {
|
|
20109
|
+
const entry = this.#provenance.get(ingress.flow);
|
|
20110
|
+
if (!entry) return;
|
|
20111
|
+
this.#provenance.set(ingress.flow, { ...entry, ingress });
|
|
20112
|
+
if (entry.protected) {
|
|
20113
|
+
for (const { name, packetId } of ingress.tracks) {
|
|
20114
|
+
if (name && !entry.ingress.tracks.some((t) => t.name === name && t.packetId === packetId)) {
|
|
20115
|
+
this.#fecManager?.resetTrack?.(packetId, name);
|
|
20116
|
+
}
|
|
20117
|
+
}
|
|
20118
|
+
}
|
|
20119
|
+
}
|
|
20120
|
+
/**
|
|
20121
|
+
* Called once per protection of a flow: at the admission that makes this
|
|
20122
|
+
* router's consumers protected under it (7.2.7), after their unadmitted
|
|
20123
|
+
* holdings of its listed tracks are discarded. Again after an unprotect.
|
|
20124
|
+
*/
|
|
20125
|
+
onProvenanceFlowProtected(cb) {
|
|
20126
|
+
this.#onFlowProtected = cb;
|
|
20127
|
+
}
|
|
20128
|
+
/**
|
|
20129
|
+
* The receiver ceased to hold the flow's endpoint on failover (7.2.7): drop
|
|
20130
|
+
* its state and hold queues (no expiry counted) and un-protect its tracks,
|
|
20131
|
+
* which then take MoQ again. The registration stays, so multicast resuming
|
|
20132
|
+
* protects the flow anew at its next admission.
|
|
20133
|
+
*/
|
|
20134
|
+
unprotectProvenanceFlow(flow) {
|
|
20135
|
+
const entry = this.#provenance.get(flow);
|
|
20136
|
+
if (!entry) return;
|
|
20137
|
+
entry.receiver.release();
|
|
20138
|
+
entry.protected = false;
|
|
20139
|
+
}
|
|
20140
|
+
/** MoQ packets of a track a protected flow lists, dropped unadmitted (overlap copies included). */
|
|
20141
|
+
get unadmittedListedDropCount() {
|
|
20142
|
+
return this.#unadmittedListedDrops;
|
|
20143
|
+
}
|
|
20144
|
+
/**
|
|
20145
|
+
* The admission that makes this router protected under `flow` (7.2.7):
|
|
20146
|
+
* its FEC instances discard what they hold of the flow's listed tracks so
|
|
20147
|
+
* no unadmitted symbol stands for an admitted one, then notify. Before it
|
|
20148
|
+
* nothing of the flow was admitted, so every such symbol is unadmitted.
|
|
20149
|
+
* The playback parse ledger is kept: it records MoQ copies already played,
|
|
20150
|
+
* not holdings, and clearing it re-emits that media (audio twice).
|
|
20151
|
+
* The FEC reset is the whole 7.2.7 discard here: no keyframe-overlay
|
|
20152
|
+
* instance exists to hold anything (repair-layers.ts is the catalog
|
|
20153
|
+
* contract only, no decoder), and packet_id 0 is never a listed track
|
|
20154
|
+
* (`ingressHasPacketId`), so nothing of it is held for the flow.
|
|
20155
|
+
*/
|
|
20156
|
+
#noteFlowAdmitted(flow) {
|
|
20157
|
+
const entry = this.#provenance.get(flow);
|
|
20158
|
+
if (!entry || entry.protected) return;
|
|
20159
|
+
entry.protected = true;
|
|
20160
|
+
for (const { name, packetId } of entry.ingress.tracks) {
|
|
20161
|
+
if (name) this.#fecManager?.resetTrack?.(packetId, name);
|
|
20162
|
+
}
|
|
20163
|
+
this.#onFlowProtected?.(flow);
|
|
20164
|
+
}
|
|
20165
|
+
/** Whether a protected flow lists `packetId`: its MoQ copies are then unadmitted datagrams of that flow. */
|
|
20166
|
+
#listedByProtectedFlow(packetId) {
|
|
20167
|
+
for (const { ingress, protected: isProtected } of this.#provenance.values()) {
|
|
20168
|
+
if (isProtected && ingressHasPacketId(ingress, packetId)) return true;
|
|
20169
|
+
}
|
|
20170
|
+
return false;
|
|
20171
|
+
}
|
|
20086
20172
|
/**
|
|
20087
20173
|
* Feed one bc-provenance record of `flow`: a datagram from `manifestPort`
|
|
20088
20174
|
* (`multicast`) or an object of the manifest track (`mirror`). Held
|
|
@@ -20093,6 +20179,7 @@ var MmtpRouter = class {
|
|
|
20093
20179
|
if (!entry || this.#disposed) return "unregistered";
|
|
20094
20180
|
const result = entry.receiver.onRecord(record, path);
|
|
20095
20181
|
if (result.admitted.length > 0) {
|
|
20182
|
+
this.#noteFlowAdmitted(flow);
|
|
20096
20183
|
this.#withDeferredFecPokes(() => {
|
|
20097
20184
|
for (const datagram of result.admitted) {
|
|
20098
20185
|
this.#processMMTP(datagram, "multicast", true, this.#heldIngress.get(datagram) ?? entry.ingress, false, true);
|
|
@@ -20199,6 +20286,10 @@ var MmtpRouter = class {
|
|
|
20199
20286
|
`[MmtpRouter] Dropping MoQ repair packet pid=${repairPacketId}: owner hint is required`
|
|
20200
20287
|
);
|
|
20201
20288
|
}
|
|
20289
|
+
if (ownerPacketId != null && this.#listedByProtectedFlow(ownerPacketId)) {
|
|
20290
|
+
this.#unadmittedListedDrops++;
|
|
20291
|
+
return;
|
|
20292
|
+
}
|
|
20202
20293
|
if (ownerPacketId != null && this.#fecManager) {
|
|
20203
20294
|
this.#moqRepairPidCounts[repairPacketId] = (this.#moqRepairPidCounts[repairPacketId] || 0) + 1;
|
|
20204
20295
|
this.#moqRepairOwnerCounts[ownerPacketId] = (this.#moqRepairOwnerCounts[ownerPacketId] || 0) + 1;
|
|
@@ -21088,8 +21179,14 @@ var MmtpRouter = class {
|
|
|
21088
21179
|
return;
|
|
21089
21180
|
}
|
|
21090
21181
|
const { verdict } = protectedFlow.receiver.onDatagram(packet, "multicast");
|
|
21091
|
-
if (verdict === "admitted")
|
|
21092
|
-
|
|
21182
|
+
if (verdict === "admitted") {
|
|
21183
|
+
this.#noteFlowAdmitted(ingress.flow);
|
|
21184
|
+
this.#processMMTP(packet, source, true, ingress, overlapCopy);
|
|
21185
|
+
} else if (verdict === "held") this.#heldIngress.set(packet, ingress);
|
|
21186
|
+
return;
|
|
21187
|
+
}
|
|
21188
|
+
if (source === "moq" && this.#listedByProtectedFlow(info.packetId)) {
|
|
21189
|
+
this.#unadmittedListedDrops++;
|
|
21093
21190
|
return;
|
|
21094
21191
|
}
|
|
21095
21192
|
if (info.isRepair) {
|
|
@@ -22630,10 +22727,11 @@ var MulticastTransport = class {
|
|
|
22630
22727
|
try {
|
|
22631
22728
|
const packetIds = config.packetIds?.length ? new Set(config.packetIds) : null;
|
|
22632
22729
|
console.log(
|
|
22633
|
-
`[MulticastTransport] window.multicast.subscribe(${config.sourceAddress}, ${config.groupAddress}, ${config.port})`
|
|
22730
|
+
`[MulticastTransport] window.multicast.subscribe(${config.sourceAddress ?? config.senderAddress ?? "*"}, ${config.groupAddress}, ${config.port})`
|
|
22634
22731
|
);
|
|
22635
22732
|
const subscribeConfig = toWindowMulticastSubscribeConfig({
|
|
22636
22733
|
source: config.sourceAddress,
|
|
22734
|
+
senderAddress: config.senderAddress,
|
|
22637
22735
|
group: config.groupAddress,
|
|
22638
22736
|
port: config.port,
|
|
22639
22737
|
mode: config.mode,
|
|
@@ -23229,6 +23327,2711 @@ var MulticastTransport = class {
|
|
|
23229
23327
|
}
|
|
23230
23328
|
};
|
|
23231
23329
|
|
|
23330
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/crypto.js
|
|
23331
|
+
var crypto2 = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
23332
|
+
|
|
23333
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/utils.js
|
|
23334
|
+
function isBytes(a) {
|
|
23335
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
23336
|
+
}
|
|
23337
|
+
function anumber(n) {
|
|
23338
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
23339
|
+
throw new Error("positive integer expected, got " + n);
|
|
23340
|
+
}
|
|
23341
|
+
function abytes(b, ...lengths) {
|
|
23342
|
+
if (!isBytes(b))
|
|
23343
|
+
throw new Error("Uint8Array expected");
|
|
23344
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
23345
|
+
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
23346
|
+
}
|
|
23347
|
+
function aexists(instance, checkFinished = true) {
|
|
23348
|
+
if (instance.destroyed)
|
|
23349
|
+
throw new Error("Hash instance has been destroyed");
|
|
23350
|
+
if (checkFinished && instance.finished)
|
|
23351
|
+
throw new Error("Hash#digest() has already been called");
|
|
23352
|
+
}
|
|
23353
|
+
function aoutput(out, instance) {
|
|
23354
|
+
abytes(out);
|
|
23355
|
+
const min = instance.outputLen;
|
|
23356
|
+
if (out.length < min) {
|
|
23357
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
23358
|
+
}
|
|
23359
|
+
}
|
|
23360
|
+
function clean(...arrays) {
|
|
23361
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
23362
|
+
arrays[i].fill(0);
|
|
23363
|
+
}
|
|
23364
|
+
}
|
|
23365
|
+
function createView(arr) {
|
|
23366
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
23367
|
+
}
|
|
23368
|
+
function rotr(word, shift) {
|
|
23369
|
+
return word << 32 - shift | word >>> shift;
|
|
23370
|
+
}
|
|
23371
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
23372
|
+
// @ts-ignore
|
|
23373
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
23374
|
+
))();
|
|
23375
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
23376
|
+
function bytesToHex(bytes) {
|
|
23377
|
+
abytes(bytes);
|
|
23378
|
+
if (hasHexBuiltin)
|
|
23379
|
+
return bytes.toHex();
|
|
23380
|
+
let hex3 = "";
|
|
23381
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
23382
|
+
hex3 += hexes[bytes[i]];
|
|
23383
|
+
}
|
|
23384
|
+
return hex3;
|
|
23385
|
+
}
|
|
23386
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
23387
|
+
function asciiToBase16(ch) {
|
|
23388
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
23389
|
+
return ch - asciis._0;
|
|
23390
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
23391
|
+
return ch - (asciis.A - 10);
|
|
23392
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
23393
|
+
return ch - (asciis.a - 10);
|
|
23394
|
+
return;
|
|
23395
|
+
}
|
|
23396
|
+
function hexToBytes(hex3) {
|
|
23397
|
+
if (typeof hex3 !== "string")
|
|
23398
|
+
throw new Error("hex string expected, got " + typeof hex3);
|
|
23399
|
+
if (hasHexBuiltin)
|
|
23400
|
+
return Uint8Array.fromHex(hex3);
|
|
23401
|
+
const hl = hex3.length;
|
|
23402
|
+
const al = hl / 2;
|
|
23403
|
+
if (hl % 2)
|
|
23404
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
23405
|
+
const array = new Uint8Array(al);
|
|
23406
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
23407
|
+
const n1 = asciiToBase16(hex3.charCodeAt(hi));
|
|
23408
|
+
const n2 = asciiToBase16(hex3.charCodeAt(hi + 1));
|
|
23409
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
23410
|
+
const char = hex3[hi] + hex3[hi + 1];
|
|
23411
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
23412
|
+
}
|
|
23413
|
+
array[ai] = n1 * 16 + n2;
|
|
23414
|
+
}
|
|
23415
|
+
return array;
|
|
23416
|
+
}
|
|
23417
|
+
function utf8ToBytes(str) {
|
|
23418
|
+
if (typeof str !== "string")
|
|
23419
|
+
throw new Error("string expected");
|
|
23420
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
23421
|
+
}
|
|
23422
|
+
function toBytes(data) {
|
|
23423
|
+
if (typeof data === "string")
|
|
23424
|
+
data = utf8ToBytes(data);
|
|
23425
|
+
abytes(data);
|
|
23426
|
+
return data;
|
|
23427
|
+
}
|
|
23428
|
+
function concatBytes(...arrays) {
|
|
23429
|
+
let sum = 0;
|
|
23430
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
23431
|
+
const a = arrays[i];
|
|
23432
|
+
abytes(a);
|
|
23433
|
+
sum += a.length;
|
|
23434
|
+
}
|
|
23435
|
+
const res = new Uint8Array(sum);
|
|
23436
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
23437
|
+
const a = arrays[i];
|
|
23438
|
+
res.set(a, pad);
|
|
23439
|
+
pad += a.length;
|
|
23440
|
+
}
|
|
23441
|
+
return res;
|
|
23442
|
+
}
|
|
23443
|
+
var Hash = class {
|
|
23444
|
+
};
|
|
23445
|
+
function createHasher(hashCons) {
|
|
23446
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
23447
|
+
const tmp = hashCons();
|
|
23448
|
+
hashC.outputLen = tmp.outputLen;
|
|
23449
|
+
hashC.blockLen = tmp.blockLen;
|
|
23450
|
+
hashC.create = () => hashCons();
|
|
23451
|
+
return hashC;
|
|
23452
|
+
}
|
|
23453
|
+
function randomBytes(bytesLength = 32) {
|
|
23454
|
+
if (crypto2 && typeof crypto2.getRandomValues === "function") {
|
|
23455
|
+
return crypto2.getRandomValues(new Uint8Array(bytesLength));
|
|
23456
|
+
}
|
|
23457
|
+
if (crypto2 && typeof crypto2.randomBytes === "function") {
|
|
23458
|
+
return Uint8Array.from(crypto2.randomBytes(bytesLength));
|
|
23459
|
+
}
|
|
23460
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
23461
|
+
}
|
|
23462
|
+
|
|
23463
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_md.js
|
|
23464
|
+
function setBigUint64(view2, byteOffset, value, isLE) {
|
|
23465
|
+
if (typeof view2.setBigUint64 === "function")
|
|
23466
|
+
return view2.setBigUint64(byteOffset, value, isLE);
|
|
23467
|
+
const _32n2 = BigInt(32);
|
|
23468
|
+
const _u32_max = BigInt(4294967295);
|
|
23469
|
+
const wh = Number(value >> _32n2 & _u32_max);
|
|
23470
|
+
const wl = Number(value & _u32_max);
|
|
23471
|
+
const h = isLE ? 4 : 0;
|
|
23472
|
+
const l = isLE ? 0 : 4;
|
|
23473
|
+
view2.setUint32(byteOffset + h, wh, isLE);
|
|
23474
|
+
view2.setUint32(byteOffset + l, wl, isLE);
|
|
23475
|
+
}
|
|
23476
|
+
function Chi(a, b, c) {
|
|
23477
|
+
return a & b ^ ~a & c;
|
|
23478
|
+
}
|
|
23479
|
+
function Maj(a, b, c) {
|
|
23480
|
+
return a & b ^ a & c ^ b & c;
|
|
23481
|
+
}
|
|
23482
|
+
var HashMD = class extends Hash {
|
|
23483
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
23484
|
+
super();
|
|
23485
|
+
this.finished = false;
|
|
23486
|
+
this.length = 0;
|
|
23487
|
+
this.pos = 0;
|
|
23488
|
+
this.destroyed = false;
|
|
23489
|
+
this.blockLen = blockLen;
|
|
23490
|
+
this.outputLen = outputLen;
|
|
23491
|
+
this.padOffset = padOffset;
|
|
23492
|
+
this.isLE = isLE;
|
|
23493
|
+
this.buffer = new Uint8Array(blockLen);
|
|
23494
|
+
this.view = createView(this.buffer);
|
|
23495
|
+
}
|
|
23496
|
+
update(data) {
|
|
23497
|
+
aexists(this);
|
|
23498
|
+
data = toBytes(data);
|
|
23499
|
+
abytes(data);
|
|
23500
|
+
const { view: view2, buffer, blockLen } = this;
|
|
23501
|
+
const len = data.length;
|
|
23502
|
+
for (let pos = 0; pos < len; ) {
|
|
23503
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
23504
|
+
if (take === blockLen) {
|
|
23505
|
+
const dataView = createView(data);
|
|
23506
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
23507
|
+
this.process(dataView, pos);
|
|
23508
|
+
continue;
|
|
23509
|
+
}
|
|
23510
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
23511
|
+
this.pos += take;
|
|
23512
|
+
pos += take;
|
|
23513
|
+
if (this.pos === blockLen) {
|
|
23514
|
+
this.process(view2, 0);
|
|
23515
|
+
this.pos = 0;
|
|
23516
|
+
}
|
|
23517
|
+
}
|
|
23518
|
+
this.length += data.length;
|
|
23519
|
+
this.roundClean();
|
|
23520
|
+
return this;
|
|
23521
|
+
}
|
|
23522
|
+
digestInto(out) {
|
|
23523
|
+
aexists(this);
|
|
23524
|
+
aoutput(out, this);
|
|
23525
|
+
this.finished = true;
|
|
23526
|
+
const { buffer, view: view2, blockLen, isLE } = this;
|
|
23527
|
+
let { pos } = this;
|
|
23528
|
+
buffer[pos++] = 128;
|
|
23529
|
+
clean(this.buffer.subarray(pos));
|
|
23530
|
+
if (this.padOffset > blockLen - pos) {
|
|
23531
|
+
this.process(view2, 0);
|
|
23532
|
+
pos = 0;
|
|
23533
|
+
}
|
|
23534
|
+
for (let i = pos; i < blockLen; i++)
|
|
23535
|
+
buffer[i] = 0;
|
|
23536
|
+
setBigUint64(view2, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
23537
|
+
this.process(view2, 0);
|
|
23538
|
+
const oview = createView(out);
|
|
23539
|
+
const len = this.outputLen;
|
|
23540
|
+
if (len % 4)
|
|
23541
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
23542
|
+
const outLen = len / 4;
|
|
23543
|
+
const state = this.get();
|
|
23544
|
+
if (outLen > state.length)
|
|
23545
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
23546
|
+
for (let i = 0; i < outLen; i++)
|
|
23547
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
23548
|
+
}
|
|
23549
|
+
digest() {
|
|
23550
|
+
const { buffer, outputLen } = this;
|
|
23551
|
+
this.digestInto(buffer);
|
|
23552
|
+
const res = buffer.slice(0, outputLen);
|
|
23553
|
+
this.destroy();
|
|
23554
|
+
return res;
|
|
23555
|
+
}
|
|
23556
|
+
_cloneInto(to) {
|
|
23557
|
+
to || (to = new this.constructor());
|
|
23558
|
+
to.set(...this.get());
|
|
23559
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
23560
|
+
to.destroyed = destroyed;
|
|
23561
|
+
to.finished = finished;
|
|
23562
|
+
to.length = length;
|
|
23563
|
+
to.pos = pos;
|
|
23564
|
+
if (length % blockLen)
|
|
23565
|
+
to.buffer.set(buffer);
|
|
23566
|
+
return to;
|
|
23567
|
+
}
|
|
23568
|
+
clone() {
|
|
23569
|
+
return this._cloneInto();
|
|
23570
|
+
}
|
|
23571
|
+
};
|
|
23572
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
23573
|
+
1779033703,
|
|
23574
|
+
3144134277,
|
|
23575
|
+
1013904242,
|
|
23576
|
+
2773480762,
|
|
23577
|
+
1359893119,
|
|
23578
|
+
2600822924,
|
|
23579
|
+
528734635,
|
|
23580
|
+
1541459225
|
|
23581
|
+
]);
|
|
23582
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
23583
|
+
1779033703,
|
|
23584
|
+
4089235720,
|
|
23585
|
+
3144134277,
|
|
23586
|
+
2227873595,
|
|
23587
|
+
1013904242,
|
|
23588
|
+
4271175723,
|
|
23589
|
+
2773480762,
|
|
23590
|
+
1595750129,
|
|
23591
|
+
1359893119,
|
|
23592
|
+
2917565137,
|
|
23593
|
+
2600822924,
|
|
23594
|
+
725511199,
|
|
23595
|
+
528734635,
|
|
23596
|
+
4215389547,
|
|
23597
|
+
1541459225,
|
|
23598
|
+
327033209
|
|
23599
|
+
]);
|
|
23600
|
+
|
|
23601
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/_u64.js
|
|
23602
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
23603
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
23604
|
+
function fromBig(n, le = false) {
|
|
23605
|
+
if (le)
|
|
23606
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
23607
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
23608
|
+
}
|
|
23609
|
+
function split(lst, le = false) {
|
|
23610
|
+
const len = lst.length;
|
|
23611
|
+
let Ah = new Uint32Array(len);
|
|
23612
|
+
let Al = new Uint32Array(len);
|
|
23613
|
+
for (let i = 0; i < len; i++) {
|
|
23614
|
+
const { h, l } = fromBig(lst[i], le);
|
|
23615
|
+
[Ah[i], Al[i]] = [h, l];
|
|
23616
|
+
}
|
|
23617
|
+
return [Ah, Al];
|
|
23618
|
+
}
|
|
23619
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
23620
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
23621
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
23622
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
23623
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
23624
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
23625
|
+
function add(Ah, Al, Bh, Bl) {
|
|
23626
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
23627
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
23628
|
+
}
|
|
23629
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
23630
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
23631
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
23632
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
23633
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
23634
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
23635
|
+
|
|
23636
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha2.js
|
|
23637
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
23638
|
+
1116352408,
|
|
23639
|
+
1899447441,
|
|
23640
|
+
3049323471,
|
|
23641
|
+
3921009573,
|
|
23642
|
+
961987163,
|
|
23643
|
+
1508970993,
|
|
23644
|
+
2453635748,
|
|
23645
|
+
2870763221,
|
|
23646
|
+
3624381080,
|
|
23647
|
+
310598401,
|
|
23648
|
+
607225278,
|
|
23649
|
+
1426881987,
|
|
23650
|
+
1925078388,
|
|
23651
|
+
2162078206,
|
|
23652
|
+
2614888103,
|
|
23653
|
+
3248222580,
|
|
23654
|
+
3835390401,
|
|
23655
|
+
4022224774,
|
|
23656
|
+
264347078,
|
|
23657
|
+
604807628,
|
|
23658
|
+
770255983,
|
|
23659
|
+
1249150122,
|
|
23660
|
+
1555081692,
|
|
23661
|
+
1996064986,
|
|
23662
|
+
2554220882,
|
|
23663
|
+
2821834349,
|
|
23664
|
+
2952996808,
|
|
23665
|
+
3210313671,
|
|
23666
|
+
3336571891,
|
|
23667
|
+
3584528711,
|
|
23668
|
+
113926993,
|
|
23669
|
+
338241895,
|
|
23670
|
+
666307205,
|
|
23671
|
+
773529912,
|
|
23672
|
+
1294757372,
|
|
23673
|
+
1396182291,
|
|
23674
|
+
1695183700,
|
|
23675
|
+
1986661051,
|
|
23676
|
+
2177026350,
|
|
23677
|
+
2456956037,
|
|
23678
|
+
2730485921,
|
|
23679
|
+
2820302411,
|
|
23680
|
+
3259730800,
|
|
23681
|
+
3345764771,
|
|
23682
|
+
3516065817,
|
|
23683
|
+
3600352804,
|
|
23684
|
+
4094571909,
|
|
23685
|
+
275423344,
|
|
23686
|
+
430227734,
|
|
23687
|
+
506948616,
|
|
23688
|
+
659060556,
|
|
23689
|
+
883997877,
|
|
23690
|
+
958139571,
|
|
23691
|
+
1322822218,
|
|
23692
|
+
1537002063,
|
|
23693
|
+
1747873779,
|
|
23694
|
+
1955562222,
|
|
23695
|
+
2024104815,
|
|
23696
|
+
2227730452,
|
|
23697
|
+
2361852424,
|
|
23698
|
+
2428436474,
|
|
23699
|
+
2756734187,
|
|
23700
|
+
3204031479,
|
|
23701
|
+
3329325298
|
|
23702
|
+
]);
|
|
23703
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
23704
|
+
var SHA256 = class extends HashMD {
|
|
23705
|
+
constructor(outputLen = 32) {
|
|
23706
|
+
super(64, outputLen, 8, false);
|
|
23707
|
+
this.A = SHA256_IV[0] | 0;
|
|
23708
|
+
this.B = SHA256_IV[1] | 0;
|
|
23709
|
+
this.C = SHA256_IV[2] | 0;
|
|
23710
|
+
this.D = SHA256_IV[3] | 0;
|
|
23711
|
+
this.E = SHA256_IV[4] | 0;
|
|
23712
|
+
this.F = SHA256_IV[5] | 0;
|
|
23713
|
+
this.G = SHA256_IV[6] | 0;
|
|
23714
|
+
this.H = SHA256_IV[7] | 0;
|
|
23715
|
+
}
|
|
23716
|
+
get() {
|
|
23717
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
23718
|
+
return [A, B, C, D, E, F, G, H];
|
|
23719
|
+
}
|
|
23720
|
+
// prettier-ignore
|
|
23721
|
+
set(A, B, C, D, E, F, G, H) {
|
|
23722
|
+
this.A = A | 0;
|
|
23723
|
+
this.B = B | 0;
|
|
23724
|
+
this.C = C | 0;
|
|
23725
|
+
this.D = D | 0;
|
|
23726
|
+
this.E = E | 0;
|
|
23727
|
+
this.F = F | 0;
|
|
23728
|
+
this.G = G | 0;
|
|
23729
|
+
this.H = H | 0;
|
|
23730
|
+
}
|
|
23731
|
+
process(view2, offset) {
|
|
23732
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
23733
|
+
SHA256_W[i] = view2.getUint32(offset, false);
|
|
23734
|
+
for (let i = 16; i < 64; i++) {
|
|
23735
|
+
const W15 = SHA256_W[i - 15];
|
|
23736
|
+
const W2 = SHA256_W[i - 2];
|
|
23737
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
23738
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
23739
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
23740
|
+
}
|
|
23741
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
23742
|
+
for (let i = 0; i < 64; i++) {
|
|
23743
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
23744
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
23745
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
23746
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
23747
|
+
H = G;
|
|
23748
|
+
G = F;
|
|
23749
|
+
F = E;
|
|
23750
|
+
E = D + T1 | 0;
|
|
23751
|
+
D = C;
|
|
23752
|
+
C = B;
|
|
23753
|
+
B = A;
|
|
23754
|
+
A = T1 + T2 | 0;
|
|
23755
|
+
}
|
|
23756
|
+
A = A + this.A | 0;
|
|
23757
|
+
B = B + this.B | 0;
|
|
23758
|
+
C = C + this.C | 0;
|
|
23759
|
+
D = D + this.D | 0;
|
|
23760
|
+
E = E + this.E | 0;
|
|
23761
|
+
F = F + this.F | 0;
|
|
23762
|
+
G = G + this.G | 0;
|
|
23763
|
+
H = H + this.H | 0;
|
|
23764
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
23765
|
+
}
|
|
23766
|
+
roundClean() {
|
|
23767
|
+
clean(SHA256_W);
|
|
23768
|
+
}
|
|
23769
|
+
destroy() {
|
|
23770
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
23771
|
+
clean(this.buffer);
|
|
23772
|
+
}
|
|
23773
|
+
};
|
|
23774
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
23775
|
+
"0x428a2f98d728ae22",
|
|
23776
|
+
"0x7137449123ef65cd",
|
|
23777
|
+
"0xb5c0fbcfec4d3b2f",
|
|
23778
|
+
"0xe9b5dba58189dbbc",
|
|
23779
|
+
"0x3956c25bf348b538",
|
|
23780
|
+
"0x59f111f1b605d019",
|
|
23781
|
+
"0x923f82a4af194f9b",
|
|
23782
|
+
"0xab1c5ed5da6d8118",
|
|
23783
|
+
"0xd807aa98a3030242",
|
|
23784
|
+
"0x12835b0145706fbe",
|
|
23785
|
+
"0x243185be4ee4b28c",
|
|
23786
|
+
"0x550c7dc3d5ffb4e2",
|
|
23787
|
+
"0x72be5d74f27b896f",
|
|
23788
|
+
"0x80deb1fe3b1696b1",
|
|
23789
|
+
"0x9bdc06a725c71235",
|
|
23790
|
+
"0xc19bf174cf692694",
|
|
23791
|
+
"0xe49b69c19ef14ad2",
|
|
23792
|
+
"0xefbe4786384f25e3",
|
|
23793
|
+
"0x0fc19dc68b8cd5b5",
|
|
23794
|
+
"0x240ca1cc77ac9c65",
|
|
23795
|
+
"0x2de92c6f592b0275",
|
|
23796
|
+
"0x4a7484aa6ea6e483",
|
|
23797
|
+
"0x5cb0a9dcbd41fbd4",
|
|
23798
|
+
"0x76f988da831153b5",
|
|
23799
|
+
"0x983e5152ee66dfab",
|
|
23800
|
+
"0xa831c66d2db43210",
|
|
23801
|
+
"0xb00327c898fb213f",
|
|
23802
|
+
"0xbf597fc7beef0ee4",
|
|
23803
|
+
"0xc6e00bf33da88fc2",
|
|
23804
|
+
"0xd5a79147930aa725",
|
|
23805
|
+
"0x06ca6351e003826f",
|
|
23806
|
+
"0x142929670a0e6e70",
|
|
23807
|
+
"0x27b70a8546d22ffc",
|
|
23808
|
+
"0x2e1b21385c26c926",
|
|
23809
|
+
"0x4d2c6dfc5ac42aed",
|
|
23810
|
+
"0x53380d139d95b3df",
|
|
23811
|
+
"0x650a73548baf63de",
|
|
23812
|
+
"0x766a0abb3c77b2a8",
|
|
23813
|
+
"0x81c2c92e47edaee6",
|
|
23814
|
+
"0x92722c851482353b",
|
|
23815
|
+
"0xa2bfe8a14cf10364",
|
|
23816
|
+
"0xa81a664bbc423001",
|
|
23817
|
+
"0xc24b8b70d0f89791",
|
|
23818
|
+
"0xc76c51a30654be30",
|
|
23819
|
+
"0xd192e819d6ef5218",
|
|
23820
|
+
"0xd69906245565a910",
|
|
23821
|
+
"0xf40e35855771202a",
|
|
23822
|
+
"0x106aa07032bbd1b8",
|
|
23823
|
+
"0x19a4c116b8d2d0c8",
|
|
23824
|
+
"0x1e376c085141ab53",
|
|
23825
|
+
"0x2748774cdf8eeb99",
|
|
23826
|
+
"0x34b0bcb5e19b48a8",
|
|
23827
|
+
"0x391c0cb3c5c95a63",
|
|
23828
|
+
"0x4ed8aa4ae3418acb",
|
|
23829
|
+
"0x5b9cca4f7763e373",
|
|
23830
|
+
"0x682e6ff3d6b2b8a3",
|
|
23831
|
+
"0x748f82ee5defb2fc",
|
|
23832
|
+
"0x78a5636f43172f60",
|
|
23833
|
+
"0x84c87814a1f0ab72",
|
|
23834
|
+
"0x8cc702081a6439ec",
|
|
23835
|
+
"0x90befffa23631e28",
|
|
23836
|
+
"0xa4506cebde82bde9",
|
|
23837
|
+
"0xbef9a3f7b2c67915",
|
|
23838
|
+
"0xc67178f2e372532b",
|
|
23839
|
+
"0xca273eceea26619c",
|
|
23840
|
+
"0xd186b8c721c0c207",
|
|
23841
|
+
"0xeada7dd6cde0eb1e",
|
|
23842
|
+
"0xf57d4f7fee6ed178",
|
|
23843
|
+
"0x06f067aa72176fba",
|
|
23844
|
+
"0x0a637dc5a2c898a6",
|
|
23845
|
+
"0x113f9804bef90dae",
|
|
23846
|
+
"0x1b710b35131c471b",
|
|
23847
|
+
"0x28db77f523047d84",
|
|
23848
|
+
"0x32caab7b40c72493",
|
|
23849
|
+
"0x3c9ebe0a15c9bebc",
|
|
23850
|
+
"0x431d67c49c100d4c",
|
|
23851
|
+
"0x4cc5d4becb3e42b6",
|
|
23852
|
+
"0x597f299cfc657e2a",
|
|
23853
|
+
"0x5fcb6fab3ad6faec",
|
|
23854
|
+
"0x6c44198c4a475817"
|
|
23855
|
+
].map((n) => BigInt(n))))();
|
|
23856
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
23857
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
23858
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
23859
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
23860
|
+
var SHA512 = class extends HashMD {
|
|
23861
|
+
constructor(outputLen = 64) {
|
|
23862
|
+
super(128, outputLen, 16, false);
|
|
23863
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
23864
|
+
this.Al = SHA512_IV[1] | 0;
|
|
23865
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
23866
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
23867
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
23868
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
23869
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
23870
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
23871
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
23872
|
+
this.El = SHA512_IV[9] | 0;
|
|
23873
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
23874
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
23875
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
23876
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
23877
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
23878
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
23879
|
+
}
|
|
23880
|
+
// prettier-ignore
|
|
23881
|
+
get() {
|
|
23882
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
23883
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
23884
|
+
}
|
|
23885
|
+
// prettier-ignore
|
|
23886
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
23887
|
+
this.Ah = Ah | 0;
|
|
23888
|
+
this.Al = Al | 0;
|
|
23889
|
+
this.Bh = Bh | 0;
|
|
23890
|
+
this.Bl = Bl | 0;
|
|
23891
|
+
this.Ch = Ch | 0;
|
|
23892
|
+
this.Cl = Cl | 0;
|
|
23893
|
+
this.Dh = Dh | 0;
|
|
23894
|
+
this.Dl = Dl | 0;
|
|
23895
|
+
this.Eh = Eh | 0;
|
|
23896
|
+
this.El = El | 0;
|
|
23897
|
+
this.Fh = Fh | 0;
|
|
23898
|
+
this.Fl = Fl | 0;
|
|
23899
|
+
this.Gh = Gh | 0;
|
|
23900
|
+
this.Gl = Gl | 0;
|
|
23901
|
+
this.Hh = Hh | 0;
|
|
23902
|
+
this.Hl = Hl | 0;
|
|
23903
|
+
}
|
|
23904
|
+
process(view2, offset) {
|
|
23905
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
23906
|
+
SHA512_W_H[i] = view2.getUint32(offset);
|
|
23907
|
+
SHA512_W_L[i] = view2.getUint32(offset += 4);
|
|
23908
|
+
}
|
|
23909
|
+
for (let i = 16; i < 80; i++) {
|
|
23910
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
23911
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
23912
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
23913
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
23914
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
23915
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
23916
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
23917
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
23918
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
23919
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
23920
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
23921
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
23922
|
+
}
|
|
23923
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
23924
|
+
for (let i = 0; i < 80; i++) {
|
|
23925
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
23926
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
23927
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
23928
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
23929
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
23930
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
23931
|
+
const T1l = T1ll | 0;
|
|
23932
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
23933
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
23934
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
23935
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
23936
|
+
Hh = Gh | 0;
|
|
23937
|
+
Hl = Gl | 0;
|
|
23938
|
+
Gh = Fh | 0;
|
|
23939
|
+
Gl = Fl | 0;
|
|
23940
|
+
Fh = Eh | 0;
|
|
23941
|
+
Fl = El | 0;
|
|
23942
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
23943
|
+
Dh = Ch | 0;
|
|
23944
|
+
Dl = Cl | 0;
|
|
23945
|
+
Ch = Bh | 0;
|
|
23946
|
+
Cl = Bl | 0;
|
|
23947
|
+
Bh = Ah | 0;
|
|
23948
|
+
Bl = Al | 0;
|
|
23949
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
23950
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
23951
|
+
Al = All | 0;
|
|
23952
|
+
}
|
|
23953
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
23954
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
23955
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
23956
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
23957
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
23958
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
23959
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
23960
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
23961
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
23962
|
+
}
|
|
23963
|
+
roundClean() {
|
|
23964
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
23965
|
+
}
|
|
23966
|
+
destroy() {
|
|
23967
|
+
clean(this.buffer);
|
|
23968
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
23969
|
+
}
|
|
23970
|
+
};
|
|
23971
|
+
var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
23972
|
+
var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
|
|
23973
|
+
|
|
23974
|
+
// ../mmt-manifest-verify/node_modules/@noble/hashes/esm/sha256.js
|
|
23975
|
+
var sha2562 = sha256;
|
|
23976
|
+
|
|
23977
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2.js
|
|
23978
|
+
var BCPV2_HEADER_LENGTH = 68;
|
|
23979
|
+
var BCPV2_SIGNATURE_LENGTH = 64;
|
|
23980
|
+
var BCPV2_MIN_RECORD_LENGTH = BCPV2_HEADER_LENGTH + 32 + BCPV2_SIGNATURE_LENGTH;
|
|
23981
|
+
var BCPV2_MAX_RECORD_LENGTH = 1316;
|
|
23982
|
+
var BCPV2_MAX_DIGESTS = 37;
|
|
23983
|
+
var BCPV2_HASH_ALG_SHA256 = 1;
|
|
23984
|
+
var text = new TextEncoder();
|
|
23985
|
+
var MAGIC = Uint8Array.from([77, 72, 77, 50]);
|
|
23986
|
+
var SIGNING_DOMAIN = text.encode("mcast-manifest-v2\0");
|
|
23987
|
+
function equalBytes(left, right) {
|
|
23988
|
+
if (left.byteLength !== right.byteLength)
|
|
23989
|
+
return false;
|
|
23990
|
+
let different = 0;
|
|
23991
|
+
for (let index = 0; index < left.byteLength; index++)
|
|
23992
|
+
different |= left[index] ^ right[index];
|
|
23993
|
+
return different === 0;
|
|
23994
|
+
}
|
|
23995
|
+
function u16(value) {
|
|
23996
|
+
const result = new Uint8Array(2);
|
|
23997
|
+
new DataView(result.buffer).setUint16(0, value);
|
|
23998
|
+
return result;
|
|
23999
|
+
}
|
|
24000
|
+
function u322(value) {
|
|
24001
|
+
const result = new Uint8Array(4);
|
|
24002
|
+
new DataView(result.buffer).setUint32(0, value);
|
|
24003
|
+
return result;
|
|
24004
|
+
}
|
|
24005
|
+
function validU32(value) {
|
|
24006
|
+
return Number.isInteger(value) && value >= 0 && value <= 4294967295;
|
|
24007
|
+
}
|
|
24008
|
+
function validPort(value) {
|
|
24009
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535;
|
|
24010
|
+
}
|
|
24011
|
+
function parseIpv4(address) {
|
|
24012
|
+
const fields = address.split(".");
|
|
24013
|
+
if (fields.length !== 4 || fields.some((field) => !/^\d+$/.test(field))) {
|
|
24014
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
24015
|
+
}
|
|
24016
|
+
const values = fields.map(Number);
|
|
24017
|
+
if (values.some((value, index) => value > 255 || String(value) !== fields[index])) {
|
|
24018
|
+
throw new Error(`invalid IPv4 address ${address}`);
|
|
24019
|
+
}
|
|
24020
|
+
return Uint8Array.from(values);
|
|
24021
|
+
}
|
|
24022
|
+
function parseIpv6(address) {
|
|
24023
|
+
const compressed = address.includes("::");
|
|
24024
|
+
if ((address.match(/::/g) ?? []).length > 1)
|
|
24025
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24026
|
+
const [leftText, rightText = ""] = address.split("::");
|
|
24027
|
+
const left = leftText ? leftText.split(":") : [];
|
|
24028
|
+
const right = rightText ? rightText.split(":") : [];
|
|
24029
|
+
if (left.some((group) => group.includes(".")) || right.some((group) => group.includes("."))) {
|
|
24030
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24031
|
+
}
|
|
24032
|
+
const missing = 8 - left.length - right.length;
|
|
24033
|
+
if (missing < 0 || !compressed && missing !== 0 || compressed && missing < 1) {
|
|
24034
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24035
|
+
}
|
|
24036
|
+
const groups = [...left, ...Array.from({ length: missing }, () => "0"), ...right];
|
|
24037
|
+
if (groups.length !== 8 || groups.some((group) => !/^[0-9a-f]{1,4}$/i.test(group))) {
|
|
24038
|
+
throw new Error(`invalid IPv6 address ${address}`);
|
|
24039
|
+
}
|
|
24040
|
+
const result = new Uint8Array(16);
|
|
24041
|
+
const view2 = new DataView(result.buffer);
|
|
24042
|
+
groups.forEach((group, index) => view2.setUint16(index * 2, Number.parseInt(group, 16)));
|
|
24043
|
+
return result;
|
|
24044
|
+
}
|
|
24045
|
+
function parseBcProvenanceV2Address(address) {
|
|
24046
|
+
if (address.includes(":"))
|
|
24047
|
+
return parseIpv6(address);
|
|
24048
|
+
return parseIpv4(address);
|
|
24049
|
+
}
|
|
24050
|
+
function endpointBytes(endpoint) {
|
|
24051
|
+
if (!validPort(endpoint.port) || !validPort(endpoint.manifestPort) || !validU32(endpoint.streamId)) {
|
|
24052
|
+
throw new Error("invalid BCPV2 endpoint");
|
|
24053
|
+
}
|
|
24054
|
+
const source = parseBcProvenanceV2Address(endpoint.sourceAddress);
|
|
24055
|
+
const group = parseBcProvenanceV2Address(endpoint.groupAddress);
|
|
24056
|
+
if (source.byteLength !== group.byteLength)
|
|
24057
|
+
throw new Error("endpoint address families differ");
|
|
24058
|
+
return { source, group };
|
|
24059
|
+
}
|
|
24060
|
+
function readU32(view2, offset) {
|
|
24061
|
+
return view2.getUint32(offset);
|
|
24062
|
+
}
|
|
24063
|
+
function readU642(view2, offset) {
|
|
24064
|
+
return view2.getBigUint64(offset);
|
|
24065
|
+
}
|
|
24066
|
+
function decodeBcProvenanceV2Record(input) {
|
|
24067
|
+
const bytes = Uint8Array.from(input);
|
|
24068
|
+
if (bytes.byteLength < BCPV2_MIN_RECORD_LENGTH || bytes.byteLength > BCPV2_MAX_RECORD_LENGTH || (bytes.byteLength - 132) % 32 !== 0) {
|
|
24069
|
+
throw new Error("invalid BCPV2 record length");
|
|
24070
|
+
}
|
|
24071
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
24072
|
+
if (!equalBytes(bytes.subarray(0, 4), MAGIC))
|
|
24073
|
+
throw new Error("invalid BCPV2 record magic");
|
|
24074
|
+
const count = view2.getUint16(28);
|
|
24075
|
+
if (count < 1 || count > BCPV2_MAX_DIGESTS || bytes.byteLength !== 132 + count * 32) {
|
|
24076
|
+
throw new Error("invalid BCPV2 record count");
|
|
24077
|
+
}
|
|
24078
|
+
const body = bytes.slice(0, bytes.byteLength - BCPV2_SIGNATURE_LENGTH);
|
|
24079
|
+
const digests = [];
|
|
24080
|
+
for (let index = 0; index < count; index++) {
|
|
24081
|
+
digests.push(bytes.slice(BCPV2_HEADER_LENGTH + index * 32, BCPV2_HEADER_LENGTH + (index + 1) * 32));
|
|
24082
|
+
}
|
|
24083
|
+
return {
|
|
24084
|
+
kid: readU32(view2, 4),
|
|
24085
|
+
epoch: readU32(view2, 8),
|
|
24086
|
+
mseq: readU642(view2, 12),
|
|
24087
|
+
firstSeq: readU642(view2, 20),
|
|
24088
|
+
count,
|
|
24089
|
+
hashAlg: bytes[30],
|
|
24090
|
+
flags: bytes[31],
|
|
24091
|
+
sent: readU32(view2, 32),
|
|
24092
|
+
ctx: bytes.slice(36, 68),
|
|
24093
|
+
digests,
|
|
24094
|
+
signature: bytes.slice(bytes.byteLength - BCPV2_SIGNATURE_LENGTH),
|
|
24095
|
+
body,
|
|
24096
|
+
raw: bytes
|
|
24097
|
+
};
|
|
24098
|
+
}
|
|
24099
|
+
function bcProvenanceV2SignedMessage(record, endpoint) {
|
|
24100
|
+
const { source, group } = endpointBytes(endpoint);
|
|
24101
|
+
const prefix = new Uint8Array(SIGNING_DOMAIN.byteLength + 4 + source.byteLength + group.byteLength + 2);
|
|
24102
|
+
let offset = 0;
|
|
24103
|
+
prefix.set(SIGNING_DOMAIN, offset);
|
|
24104
|
+
offset += SIGNING_DOMAIN.byteLength;
|
|
24105
|
+
prefix.set(u322(endpoint.streamId), offset);
|
|
24106
|
+
offset += 4;
|
|
24107
|
+
prefix.set(source, offset);
|
|
24108
|
+
offset += source.byteLength;
|
|
24109
|
+
prefix.set(group, offset);
|
|
24110
|
+
offset += group.byteLength;
|
|
24111
|
+
prefix.set(u16(endpoint.manifestPort), offset);
|
|
24112
|
+
const message = new Uint8Array(prefix.byteLength + record.body.byteLength);
|
|
24113
|
+
message.set(prefix);
|
|
24114
|
+
message.set(record.body, prefix.byteLength);
|
|
24115
|
+
return message;
|
|
24116
|
+
}
|
|
24117
|
+
function bcProvenanceV2DatagramDigest(endpoint, payload) {
|
|
24118
|
+
if (payload.byteLength > 65535)
|
|
24119
|
+
throw new Error("datagram payload exceeds u16 length");
|
|
24120
|
+
const { source, group } = endpointBytes(endpoint);
|
|
24121
|
+
const pseudoheader = new Uint8Array(source.byteLength + group.byteLength + 2 + 2 + 2 + 2 + 4);
|
|
24122
|
+
let offset = 0;
|
|
24123
|
+
pseudoheader.set(source, offset);
|
|
24124
|
+
offset += source.byteLength;
|
|
24125
|
+
pseudoheader.set(group, offset);
|
|
24126
|
+
offset += group.byteLength;
|
|
24127
|
+
pseudoheader.set([0, 17], offset);
|
|
24128
|
+
offset += 2;
|
|
24129
|
+
pseudoheader.set(u16(payload.byteLength), offset);
|
|
24130
|
+
offset += 2;
|
|
24131
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
24132
|
+
offset += 2;
|
|
24133
|
+
pseudoheader.set(u16(endpoint.port), offset);
|
|
24134
|
+
offset += 2;
|
|
24135
|
+
pseudoheader.set(u322(endpoint.streamId), offset);
|
|
24136
|
+
const input = new Uint8Array(pseudoheader.byteLength + payload.byteLength);
|
|
24137
|
+
input.set(pseudoheader);
|
|
24138
|
+
input.set(payload, pseudoheader.byteLength);
|
|
24139
|
+
return sha2562(input);
|
|
24140
|
+
}
|
|
24141
|
+
|
|
24142
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/utils.js
|
|
24143
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
24144
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
24145
|
+
function _abool2(value, title = "") {
|
|
24146
|
+
if (typeof value !== "boolean") {
|
|
24147
|
+
const prefix = title && `"${title}"`;
|
|
24148
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
24149
|
+
}
|
|
24150
|
+
return value;
|
|
24151
|
+
}
|
|
24152
|
+
function _abytes2(value, length, title = "") {
|
|
24153
|
+
const bytes = isBytes(value);
|
|
24154
|
+
const len = value?.length;
|
|
24155
|
+
const needsLen = length !== void 0;
|
|
24156
|
+
if (!bytes || needsLen && len !== length) {
|
|
24157
|
+
const prefix = title && `"${title}" `;
|
|
24158
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
24159
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
24160
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
24161
|
+
}
|
|
24162
|
+
return value;
|
|
24163
|
+
}
|
|
24164
|
+
function hexToNumber(hex3) {
|
|
24165
|
+
if (typeof hex3 !== "string")
|
|
24166
|
+
throw new Error("hex string expected, got " + typeof hex3);
|
|
24167
|
+
return hex3 === "" ? _0n : BigInt("0x" + hex3);
|
|
24168
|
+
}
|
|
24169
|
+
function bytesToNumberBE(bytes) {
|
|
24170
|
+
return hexToNumber(bytesToHex(bytes));
|
|
24171
|
+
}
|
|
24172
|
+
function bytesToNumberLE(bytes) {
|
|
24173
|
+
abytes(bytes);
|
|
24174
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
24175
|
+
}
|
|
24176
|
+
function numberToBytesBE(n, len) {
|
|
24177
|
+
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
24178
|
+
}
|
|
24179
|
+
function numberToBytesLE(n, len) {
|
|
24180
|
+
return numberToBytesBE(n, len).reverse();
|
|
24181
|
+
}
|
|
24182
|
+
function ensureBytes(title, hex3, expectedLength) {
|
|
24183
|
+
let res;
|
|
24184
|
+
if (typeof hex3 === "string") {
|
|
24185
|
+
try {
|
|
24186
|
+
res = hexToBytes(hex3);
|
|
24187
|
+
} catch (e) {
|
|
24188
|
+
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
24189
|
+
}
|
|
24190
|
+
} else if (isBytes(hex3)) {
|
|
24191
|
+
res = Uint8Array.from(hex3);
|
|
24192
|
+
} else {
|
|
24193
|
+
throw new Error(title + " must be hex string or Uint8Array");
|
|
24194
|
+
}
|
|
24195
|
+
const len = res.length;
|
|
24196
|
+
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
24197
|
+
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
24198
|
+
return res;
|
|
24199
|
+
}
|
|
24200
|
+
function equalBytes2(a, b) {
|
|
24201
|
+
if (a.length !== b.length)
|
|
24202
|
+
return false;
|
|
24203
|
+
let diff = 0;
|
|
24204
|
+
for (let i = 0; i < a.length; i++)
|
|
24205
|
+
diff |= a[i] ^ b[i];
|
|
24206
|
+
return diff === 0;
|
|
24207
|
+
}
|
|
24208
|
+
function copyBytes(bytes) {
|
|
24209
|
+
return Uint8Array.from(bytes);
|
|
24210
|
+
}
|
|
24211
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
24212
|
+
function inRange(n, min, max) {
|
|
24213
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
24214
|
+
}
|
|
24215
|
+
function aInRange(title, n, min, max) {
|
|
24216
|
+
if (!inRange(n, min, max))
|
|
24217
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
24218
|
+
}
|
|
24219
|
+
function bitLen(n) {
|
|
24220
|
+
let len;
|
|
24221
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
24222
|
+
;
|
|
24223
|
+
return len;
|
|
24224
|
+
}
|
|
24225
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
24226
|
+
function _validateObject(object2, fields, optFields = {}) {
|
|
24227
|
+
if (!object2 || typeof object2 !== "object")
|
|
24228
|
+
throw new Error("expected valid options object");
|
|
24229
|
+
function checkField2(fieldName, expectedType, isOpt) {
|
|
24230
|
+
const val = object2[fieldName];
|
|
24231
|
+
if (isOpt && val === void 0)
|
|
24232
|
+
return;
|
|
24233
|
+
const current = typeof val;
|
|
24234
|
+
if (current !== expectedType || val === null)
|
|
24235
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
24236
|
+
}
|
|
24237
|
+
Object.entries(fields).forEach(([k, v]) => checkField2(k, v, false));
|
|
24238
|
+
Object.entries(optFields).forEach(([k, v]) => checkField2(k, v, true));
|
|
24239
|
+
}
|
|
24240
|
+
var notImplemented = () => {
|
|
24241
|
+
throw new Error("not implemented");
|
|
24242
|
+
};
|
|
24243
|
+
function memoized(fn) {
|
|
24244
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
24245
|
+
return (arg, ...args) => {
|
|
24246
|
+
const val = map.get(arg);
|
|
24247
|
+
if (val !== void 0)
|
|
24248
|
+
return val;
|
|
24249
|
+
const computed = fn(arg, ...args);
|
|
24250
|
+
map.set(arg, computed);
|
|
24251
|
+
return computed;
|
|
24252
|
+
};
|
|
24253
|
+
}
|
|
24254
|
+
|
|
24255
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/modular.js
|
|
24256
|
+
var _0n2 = BigInt(0);
|
|
24257
|
+
var _1n2 = BigInt(1);
|
|
24258
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
24259
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
24260
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
24261
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
24262
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
24263
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
24264
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
24265
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
24266
|
+
function mod(a, b) {
|
|
24267
|
+
const result = a % b;
|
|
24268
|
+
return result >= _0n2 ? result : b + result;
|
|
24269
|
+
}
|
|
24270
|
+
function pow2(x, power, modulo) {
|
|
24271
|
+
let res = x;
|
|
24272
|
+
while (power-- > _0n2) {
|
|
24273
|
+
res *= res;
|
|
24274
|
+
res %= modulo;
|
|
24275
|
+
}
|
|
24276
|
+
return res;
|
|
24277
|
+
}
|
|
24278
|
+
function invert(number, modulo) {
|
|
24279
|
+
if (number === _0n2)
|
|
24280
|
+
throw new Error("invert: expected non-zero number");
|
|
24281
|
+
if (modulo <= _0n2)
|
|
24282
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
24283
|
+
let a = mod(number, modulo);
|
|
24284
|
+
let b = modulo;
|
|
24285
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
24286
|
+
while (a !== _0n2) {
|
|
24287
|
+
const q = b / a;
|
|
24288
|
+
const r = b % a;
|
|
24289
|
+
const m = x - u * q;
|
|
24290
|
+
const n = y - v * q;
|
|
24291
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
24292
|
+
}
|
|
24293
|
+
const gcd = b;
|
|
24294
|
+
if (gcd !== _1n2)
|
|
24295
|
+
throw new Error("invert: does not exist");
|
|
24296
|
+
return mod(x, modulo);
|
|
24297
|
+
}
|
|
24298
|
+
function assertIsSquare(Fp2, root, n) {
|
|
24299
|
+
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
24300
|
+
throw new Error("Cannot find square root");
|
|
24301
|
+
}
|
|
24302
|
+
function sqrt3mod4(Fp2, n) {
|
|
24303
|
+
const p1div4 = (Fp2.ORDER + _1n2) / _4n;
|
|
24304
|
+
const root = Fp2.pow(n, p1div4);
|
|
24305
|
+
assertIsSquare(Fp2, root, n);
|
|
24306
|
+
return root;
|
|
24307
|
+
}
|
|
24308
|
+
function sqrt5mod8(Fp2, n) {
|
|
24309
|
+
const p5div8 = (Fp2.ORDER - _5n) / _8n;
|
|
24310
|
+
const n2 = Fp2.mul(n, _2n);
|
|
24311
|
+
const v = Fp2.pow(n2, p5div8);
|
|
24312
|
+
const nv = Fp2.mul(n, v);
|
|
24313
|
+
const i = Fp2.mul(Fp2.mul(nv, _2n), v);
|
|
24314
|
+
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
24315
|
+
assertIsSquare(Fp2, root, n);
|
|
24316
|
+
return root;
|
|
24317
|
+
}
|
|
24318
|
+
function sqrt9mod16(P) {
|
|
24319
|
+
const Fp_ = Field(P);
|
|
24320
|
+
const tn = tonelliShanks(P);
|
|
24321
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
24322
|
+
const c2 = tn(Fp_, c1);
|
|
24323
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
24324
|
+
const c4 = (P + _7n) / _16n;
|
|
24325
|
+
return (Fp2, n) => {
|
|
24326
|
+
let tv1 = Fp2.pow(n, c4);
|
|
24327
|
+
let tv2 = Fp2.mul(tv1, c1);
|
|
24328
|
+
const tv3 = Fp2.mul(tv1, c2);
|
|
24329
|
+
const tv4 = Fp2.mul(tv1, c3);
|
|
24330
|
+
const e1 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
24331
|
+
const e2 = Fp2.eql(Fp2.sqr(tv3), n);
|
|
24332
|
+
tv1 = Fp2.cmov(tv1, tv2, e1);
|
|
24333
|
+
tv2 = Fp2.cmov(tv4, tv3, e2);
|
|
24334
|
+
const e3 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
24335
|
+
const root = Fp2.cmov(tv1, tv2, e3);
|
|
24336
|
+
assertIsSquare(Fp2, root, n);
|
|
24337
|
+
return root;
|
|
24338
|
+
};
|
|
24339
|
+
}
|
|
24340
|
+
function tonelliShanks(P) {
|
|
24341
|
+
if (P < _3n)
|
|
24342
|
+
throw new Error("sqrt is not defined for small field");
|
|
24343
|
+
let Q = P - _1n2;
|
|
24344
|
+
let S = 0;
|
|
24345
|
+
while (Q % _2n === _0n2) {
|
|
24346
|
+
Q /= _2n;
|
|
24347
|
+
S++;
|
|
24348
|
+
}
|
|
24349
|
+
let Z = _2n;
|
|
24350
|
+
const _Fp = Field(P);
|
|
24351
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
24352
|
+
if (Z++ > 1e3)
|
|
24353
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
24354
|
+
}
|
|
24355
|
+
if (S === 1)
|
|
24356
|
+
return sqrt3mod4;
|
|
24357
|
+
let cc = _Fp.pow(Z, Q);
|
|
24358
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
24359
|
+
return function tonelliSlow(Fp2, n) {
|
|
24360
|
+
if (Fp2.is0(n))
|
|
24361
|
+
return n;
|
|
24362
|
+
if (FpLegendre(Fp2, n) !== 1)
|
|
24363
|
+
throw new Error("Cannot find square root");
|
|
24364
|
+
let M = S;
|
|
24365
|
+
let c = Fp2.mul(Fp2.ONE, cc);
|
|
24366
|
+
let t = Fp2.pow(n, Q);
|
|
24367
|
+
let R = Fp2.pow(n, Q1div2);
|
|
24368
|
+
while (!Fp2.eql(t, Fp2.ONE)) {
|
|
24369
|
+
if (Fp2.is0(t))
|
|
24370
|
+
return Fp2.ZERO;
|
|
24371
|
+
let i = 1;
|
|
24372
|
+
let t_tmp = Fp2.sqr(t);
|
|
24373
|
+
while (!Fp2.eql(t_tmp, Fp2.ONE)) {
|
|
24374
|
+
i++;
|
|
24375
|
+
t_tmp = Fp2.sqr(t_tmp);
|
|
24376
|
+
if (i === M)
|
|
24377
|
+
throw new Error("Cannot find square root");
|
|
24378
|
+
}
|
|
24379
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
24380
|
+
const b = Fp2.pow(c, exponent);
|
|
24381
|
+
M = i;
|
|
24382
|
+
c = Fp2.sqr(b);
|
|
24383
|
+
t = Fp2.mul(t, c);
|
|
24384
|
+
R = Fp2.mul(R, b);
|
|
24385
|
+
}
|
|
24386
|
+
return R;
|
|
24387
|
+
};
|
|
24388
|
+
}
|
|
24389
|
+
function FpSqrt(P) {
|
|
24390
|
+
if (P % _4n === _3n)
|
|
24391
|
+
return sqrt3mod4;
|
|
24392
|
+
if (P % _8n === _5n)
|
|
24393
|
+
return sqrt5mod8;
|
|
24394
|
+
if (P % _16n === _9n)
|
|
24395
|
+
return sqrt9mod16(P);
|
|
24396
|
+
return tonelliShanks(P);
|
|
24397
|
+
}
|
|
24398
|
+
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
24399
|
+
var FIELD_FIELDS = [
|
|
24400
|
+
"create",
|
|
24401
|
+
"isValid",
|
|
24402
|
+
"is0",
|
|
24403
|
+
"neg",
|
|
24404
|
+
"inv",
|
|
24405
|
+
"sqrt",
|
|
24406
|
+
"sqr",
|
|
24407
|
+
"eql",
|
|
24408
|
+
"add",
|
|
24409
|
+
"sub",
|
|
24410
|
+
"mul",
|
|
24411
|
+
"pow",
|
|
24412
|
+
"div",
|
|
24413
|
+
"addN",
|
|
24414
|
+
"subN",
|
|
24415
|
+
"mulN",
|
|
24416
|
+
"sqrN"
|
|
24417
|
+
];
|
|
24418
|
+
function validateField(field) {
|
|
24419
|
+
const initial = {
|
|
24420
|
+
ORDER: "bigint",
|
|
24421
|
+
MASK: "bigint",
|
|
24422
|
+
BYTES: "number",
|
|
24423
|
+
BITS: "number"
|
|
24424
|
+
};
|
|
24425
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
24426
|
+
map[val] = "function";
|
|
24427
|
+
return map;
|
|
24428
|
+
}, initial);
|
|
24429
|
+
_validateObject(field, opts);
|
|
24430
|
+
return field;
|
|
24431
|
+
}
|
|
24432
|
+
function FpPow(Fp2, num, power) {
|
|
24433
|
+
if (power < _0n2)
|
|
24434
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
24435
|
+
if (power === _0n2)
|
|
24436
|
+
return Fp2.ONE;
|
|
24437
|
+
if (power === _1n2)
|
|
24438
|
+
return num;
|
|
24439
|
+
let p = Fp2.ONE;
|
|
24440
|
+
let d = num;
|
|
24441
|
+
while (power > _0n2) {
|
|
24442
|
+
if (power & _1n2)
|
|
24443
|
+
p = Fp2.mul(p, d);
|
|
24444
|
+
d = Fp2.sqr(d);
|
|
24445
|
+
power >>= _1n2;
|
|
24446
|
+
}
|
|
24447
|
+
return p;
|
|
24448
|
+
}
|
|
24449
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
24450
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
|
|
24451
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
24452
|
+
if (Fp2.is0(num))
|
|
24453
|
+
return acc;
|
|
24454
|
+
inverted[i] = acc;
|
|
24455
|
+
return Fp2.mul(acc, num);
|
|
24456
|
+
}, Fp2.ONE);
|
|
24457
|
+
const invertedAcc = Fp2.inv(multipliedAcc);
|
|
24458
|
+
nums.reduceRight((acc, num, i) => {
|
|
24459
|
+
if (Fp2.is0(num))
|
|
24460
|
+
return acc;
|
|
24461
|
+
inverted[i] = Fp2.mul(acc, inverted[i]);
|
|
24462
|
+
return Fp2.mul(acc, num);
|
|
24463
|
+
}, invertedAcc);
|
|
24464
|
+
return inverted;
|
|
24465
|
+
}
|
|
24466
|
+
function FpLegendre(Fp2, n) {
|
|
24467
|
+
const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
|
|
24468
|
+
const powered = Fp2.pow(n, p1mod2);
|
|
24469
|
+
const yes = Fp2.eql(powered, Fp2.ONE);
|
|
24470
|
+
const zero = Fp2.eql(powered, Fp2.ZERO);
|
|
24471
|
+
const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
|
|
24472
|
+
if (!yes && !zero && !no)
|
|
24473
|
+
throw new Error("invalid Legendre symbol result");
|
|
24474
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
24475
|
+
}
|
|
24476
|
+
function nLength(n, nBitLength) {
|
|
24477
|
+
if (nBitLength !== void 0)
|
|
24478
|
+
anumber(nBitLength);
|
|
24479
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
24480
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
24481
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
24482
|
+
}
|
|
24483
|
+
function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
|
|
24484
|
+
if (ORDER <= _0n2)
|
|
24485
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
24486
|
+
let _nbitLength = void 0;
|
|
24487
|
+
let _sqrt = void 0;
|
|
24488
|
+
let modFromBytes = false;
|
|
24489
|
+
let allowedLengths = void 0;
|
|
24490
|
+
if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
|
|
24491
|
+
if (opts.sqrt || isLE)
|
|
24492
|
+
throw new Error("cannot specify opts in two arguments");
|
|
24493
|
+
const _opts = bitLenOrOpts;
|
|
24494
|
+
if (_opts.BITS)
|
|
24495
|
+
_nbitLength = _opts.BITS;
|
|
24496
|
+
if (_opts.sqrt)
|
|
24497
|
+
_sqrt = _opts.sqrt;
|
|
24498
|
+
if (typeof _opts.isLE === "boolean")
|
|
24499
|
+
isLE = _opts.isLE;
|
|
24500
|
+
if (typeof _opts.modFromBytes === "boolean")
|
|
24501
|
+
modFromBytes = _opts.modFromBytes;
|
|
24502
|
+
allowedLengths = _opts.allowedLengths;
|
|
24503
|
+
} else {
|
|
24504
|
+
if (typeof bitLenOrOpts === "number")
|
|
24505
|
+
_nbitLength = bitLenOrOpts;
|
|
24506
|
+
if (opts.sqrt)
|
|
24507
|
+
_sqrt = opts.sqrt;
|
|
24508
|
+
}
|
|
24509
|
+
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);
|
|
24510
|
+
if (BYTES > 2048)
|
|
24511
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
24512
|
+
let sqrtP;
|
|
24513
|
+
const f = Object.freeze({
|
|
24514
|
+
ORDER,
|
|
24515
|
+
isLE,
|
|
24516
|
+
BITS,
|
|
24517
|
+
BYTES,
|
|
24518
|
+
MASK: bitMask(BITS),
|
|
24519
|
+
ZERO: _0n2,
|
|
24520
|
+
ONE: _1n2,
|
|
24521
|
+
allowedLengths,
|
|
24522
|
+
create: (num) => mod(num, ORDER),
|
|
24523
|
+
isValid: (num) => {
|
|
24524
|
+
if (typeof num !== "bigint")
|
|
24525
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
24526
|
+
return _0n2 <= num && num < ORDER;
|
|
24527
|
+
},
|
|
24528
|
+
is0: (num) => num === _0n2,
|
|
24529
|
+
// is valid and invertible
|
|
24530
|
+
isValidNot0: (num) => !f.is0(num) && f.isValid(num),
|
|
24531
|
+
isOdd: (num) => (num & _1n2) === _1n2,
|
|
24532
|
+
neg: (num) => mod(-num, ORDER),
|
|
24533
|
+
eql: (lhs, rhs) => lhs === rhs,
|
|
24534
|
+
sqr: (num) => mod(num * num, ORDER),
|
|
24535
|
+
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
24536
|
+
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
24537
|
+
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
24538
|
+
pow: (num, power) => FpPow(f, num, power),
|
|
24539
|
+
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
24540
|
+
// Same as above, but doesn't normalize
|
|
24541
|
+
sqrN: (num) => num * num,
|
|
24542
|
+
addN: (lhs, rhs) => lhs + rhs,
|
|
24543
|
+
subN: (lhs, rhs) => lhs - rhs,
|
|
24544
|
+
mulN: (lhs, rhs) => lhs * rhs,
|
|
24545
|
+
inv: (num) => invert(num, ORDER),
|
|
24546
|
+
sqrt: _sqrt || ((n) => {
|
|
24547
|
+
if (!sqrtP)
|
|
24548
|
+
sqrtP = FpSqrt(ORDER);
|
|
24549
|
+
return sqrtP(f, n);
|
|
24550
|
+
}),
|
|
24551
|
+
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
24552
|
+
fromBytes: (bytes, skipValidation = true) => {
|
|
24553
|
+
if (allowedLengths) {
|
|
24554
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
24555
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
24556
|
+
}
|
|
24557
|
+
const padded = new Uint8Array(BYTES);
|
|
24558
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
24559
|
+
bytes = padded;
|
|
24560
|
+
}
|
|
24561
|
+
if (bytes.length !== BYTES)
|
|
24562
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
24563
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
24564
|
+
if (modFromBytes)
|
|
24565
|
+
scalar = mod(scalar, ORDER);
|
|
24566
|
+
if (!skipValidation) {
|
|
24567
|
+
if (!f.isValid(scalar))
|
|
24568
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
24569
|
+
}
|
|
24570
|
+
return scalar;
|
|
24571
|
+
},
|
|
24572
|
+
// TODO: we don't need it here, move out to separate fn
|
|
24573
|
+
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
24574
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
24575
|
+
// and it's unclear what to return in there.
|
|
24576
|
+
cmov: (a, b, c) => c ? b : a
|
|
24577
|
+
});
|
|
24578
|
+
return Object.freeze(f);
|
|
24579
|
+
}
|
|
24580
|
+
|
|
24581
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/curve.js
|
|
24582
|
+
var _0n3 = BigInt(0);
|
|
24583
|
+
var _1n3 = BigInt(1);
|
|
24584
|
+
function negateCt(condition, item) {
|
|
24585
|
+
const neg = item.negate();
|
|
24586
|
+
return condition ? neg : item;
|
|
24587
|
+
}
|
|
24588
|
+
function normalizeZ(c, points) {
|
|
24589
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
24590
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
24591
|
+
}
|
|
24592
|
+
function validateW(W, bits) {
|
|
24593
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
24594
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
24595
|
+
}
|
|
24596
|
+
function calcWOpts(W, scalarBits) {
|
|
24597
|
+
validateW(W, scalarBits);
|
|
24598
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
24599
|
+
const windowSize = 2 ** (W - 1);
|
|
24600
|
+
const maxNumber = 2 ** W;
|
|
24601
|
+
const mask = bitMask(W);
|
|
24602
|
+
const shiftBy = BigInt(W);
|
|
24603
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
24604
|
+
}
|
|
24605
|
+
function calcOffsets(n, window2, wOpts) {
|
|
24606
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
24607
|
+
let wbits = Number(n & mask);
|
|
24608
|
+
let nextN = n >> shiftBy;
|
|
24609
|
+
if (wbits > windowSize) {
|
|
24610
|
+
wbits -= maxNumber;
|
|
24611
|
+
nextN += _1n3;
|
|
24612
|
+
}
|
|
24613
|
+
const offsetStart = window2 * windowSize;
|
|
24614
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
24615
|
+
const isZero = wbits === 0;
|
|
24616
|
+
const isNeg = wbits < 0;
|
|
24617
|
+
const isNegF = window2 % 2 !== 0;
|
|
24618
|
+
const offsetF = offsetStart;
|
|
24619
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
24620
|
+
}
|
|
24621
|
+
function validateMSMPoints(points, c) {
|
|
24622
|
+
if (!Array.isArray(points))
|
|
24623
|
+
throw new Error("array expected");
|
|
24624
|
+
points.forEach((p, i) => {
|
|
24625
|
+
if (!(p instanceof c))
|
|
24626
|
+
throw new Error("invalid point at index " + i);
|
|
24627
|
+
});
|
|
24628
|
+
}
|
|
24629
|
+
function validateMSMScalars(scalars, field) {
|
|
24630
|
+
if (!Array.isArray(scalars))
|
|
24631
|
+
throw new Error("array of scalars expected");
|
|
24632
|
+
scalars.forEach((s, i) => {
|
|
24633
|
+
if (!field.isValid(s))
|
|
24634
|
+
throw new Error("invalid scalar at index " + i);
|
|
24635
|
+
});
|
|
24636
|
+
}
|
|
24637
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
24638
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
24639
|
+
function getW(P) {
|
|
24640
|
+
return pointWindowSizes.get(P) || 1;
|
|
24641
|
+
}
|
|
24642
|
+
function assert0(n) {
|
|
24643
|
+
if (n !== _0n3)
|
|
24644
|
+
throw new Error("invalid wNAF");
|
|
24645
|
+
}
|
|
24646
|
+
var wNAF = class {
|
|
24647
|
+
// Parametrized with a given Point class (not individual point)
|
|
24648
|
+
constructor(Point, bits) {
|
|
24649
|
+
this.BASE = Point.BASE;
|
|
24650
|
+
this.ZERO = Point.ZERO;
|
|
24651
|
+
this.Fn = Point.Fn;
|
|
24652
|
+
this.bits = bits;
|
|
24653
|
+
}
|
|
24654
|
+
// non-const time multiplication ladder
|
|
24655
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
24656
|
+
let d = elm;
|
|
24657
|
+
while (n > _0n3) {
|
|
24658
|
+
if (n & _1n3)
|
|
24659
|
+
p = p.add(d);
|
|
24660
|
+
d = d.double();
|
|
24661
|
+
n >>= _1n3;
|
|
24662
|
+
}
|
|
24663
|
+
return p;
|
|
24664
|
+
}
|
|
24665
|
+
/**
|
|
24666
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
24667
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
24668
|
+
* Number of precomputed points depends on the curve size:
|
|
24669
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
24670
|
+
* - 𝑊 is the window size
|
|
24671
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
24672
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
24673
|
+
* @param point Point instance
|
|
24674
|
+
* @param W window size
|
|
24675
|
+
* @returns precomputed point tables flattened to a single array
|
|
24676
|
+
*/
|
|
24677
|
+
precomputeWindow(point, W) {
|
|
24678
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
24679
|
+
const points = [];
|
|
24680
|
+
let p = point;
|
|
24681
|
+
let base = p;
|
|
24682
|
+
for (let window2 = 0; window2 < windows; window2++) {
|
|
24683
|
+
base = p;
|
|
24684
|
+
points.push(base);
|
|
24685
|
+
for (let i = 1; i < windowSize; i++) {
|
|
24686
|
+
base = base.add(p);
|
|
24687
|
+
points.push(base);
|
|
24688
|
+
}
|
|
24689
|
+
p = base.double();
|
|
24690
|
+
}
|
|
24691
|
+
return points;
|
|
24692
|
+
}
|
|
24693
|
+
/**
|
|
24694
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
24695
|
+
* More compact implementation:
|
|
24696
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
24697
|
+
* @returns real and fake (for const-time) points
|
|
24698
|
+
*/
|
|
24699
|
+
wNAF(W, precomputes, n) {
|
|
24700
|
+
if (!this.Fn.isValid(n))
|
|
24701
|
+
throw new Error("invalid scalar");
|
|
24702
|
+
let p = this.ZERO;
|
|
24703
|
+
let f = this.BASE;
|
|
24704
|
+
const wo = calcWOpts(W, this.bits);
|
|
24705
|
+
for (let window2 = 0; window2 < wo.windows; window2++) {
|
|
24706
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window2, wo);
|
|
24707
|
+
n = nextN;
|
|
24708
|
+
if (isZero) {
|
|
24709
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
24710
|
+
} else {
|
|
24711
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
24712
|
+
}
|
|
24713
|
+
}
|
|
24714
|
+
assert0(n);
|
|
24715
|
+
return { p, f };
|
|
24716
|
+
}
|
|
24717
|
+
/**
|
|
24718
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
24719
|
+
* @param acc accumulator point to add result of multiplication
|
|
24720
|
+
* @returns point
|
|
24721
|
+
*/
|
|
24722
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
24723
|
+
const wo = calcWOpts(W, this.bits);
|
|
24724
|
+
for (let window2 = 0; window2 < wo.windows; window2++) {
|
|
24725
|
+
if (n === _0n3)
|
|
24726
|
+
break;
|
|
24727
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window2, wo);
|
|
24728
|
+
n = nextN;
|
|
24729
|
+
if (isZero) {
|
|
24730
|
+
continue;
|
|
24731
|
+
} else {
|
|
24732
|
+
const item = precomputes[offset];
|
|
24733
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
24734
|
+
}
|
|
24735
|
+
}
|
|
24736
|
+
assert0(n);
|
|
24737
|
+
return acc;
|
|
24738
|
+
}
|
|
24739
|
+
getPrecomputes(W, point, transform) {
|
|
24740
|
+
let comp = pointPrecomputes.get(point);
|
|
24741
|
+
if (!comp) {
|
|
24742
|
+
comp = this.precomputeWindow(point, W);
|
|
24743
|
+
if (W !== 1) {
|
|
24744
|
+
if (typeof transform === "function")
|
|
24745
|
+
comp = transform(comp);
|
|
24746
|
+
pointPrecomputes.set(point, comp);
|
|
24747
|
+
}
|
|
24748
|
+
}
|
|
24749
|
+
return comp;
|
|
24750
|
+
}
|
|
24751
|
+
cached(point, scalar, transform) {
|
|
24752
|
+
const W = getW(point);
|
|
24753
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
24754
|
+
}
|
|
24755
|
+
unsafe(point, scalar, transform, prev) {
|
|
24756
|
+
const W = getW(point);
|
|
24757
|
+
if (W === 1)
|
|
24758
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
24759
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
24760
|
+
}
|
|
24761
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
24762
|
+
// using windowed method. This specifies window size and
|
|
24763
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
24764
|
+
createCache(P, W) {
|
|
24765
|
+
validateW(W, this.bits);
|
|
24766
|
+
pointWindowSizes.set(P, W);
|
|
24767
|
+
pointPrecomputes.delete(P);
|
|
24768
|
+
}
|
|
24769
|
+
hasCache(elm) {
|
|
24770
|
+
return getW(elm) !== 1;
|
|
24771
|
+
}
|
|
24772
|
+
};
|
|
24773
|
+
function pippenger(c, fieldN, points, scalars) {
|
|
24774
|
+
validateMSMPoints(points, c);
|
|
24775
|
+
validateMSMScalars(scalars, fieldN);
|
|
24776
|
+
const plength = points.length;
|
|
24777
|
+
const slength = scalars.length;
|
|
24778
|
+
if (plength !== slength)
|
|
24779
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
24780
|
+
const zero = c.ZERO;
|
|
24781
|
+
const wbits = bitLen(BigInt(plength));
|
|
24782
|
+
let windowSize = 1;
|
|
24783
|
+
if (wbits > 12)
|
|
24784
|
+
windowSize = wbits - 3;
|
|
24785
|
+
else if (wbits > 4)
|
|
24786
|
+
windowSize = wbits - 2;
|
|
24787
|
+
else if (wbits > 0)
|
|
24788
|
+
windowSize = 2;
|
|
24789
|
+
const MASK = bitMask(windowSize);
|
|
24790
|
+
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
24791
|
+
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
24792
|
+
let sum = zero;
|
|
24793
|
+
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
24794
|
+
buckets.fill(zero);
|
|
24795
|
+
for (let j = 0; j < slength; j++) {
|
|
24796
|
+
const scalar = scalars[j];
|
|
24797
|
+
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
|
24798
|
+
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
24799
|
+
}
|
|
24800
|
+
let resI = zero;
|
|
24801
|
+
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
24802
|
+
sumI = sumI.add(buckets[j]);
|
|
24803
|
+
resI = resI.add(sumI);
|
|
24804
|
+
}
|
|
24805
|
+
sum = sum.add(resI);
|
|
24806
|
+
if (i !== 0)
|
|
24807
|
+
for (let j = 0; j < windowSize; j++)
|
|
24808
|
+
sum = sum.double();
|
|
24809
|
+
}
|
|
24810
|
+
return sum;
|
|
24811
|
+
}
|
|
24812
|
+
function createField(order, field, isLE) {
|
|
24813
|
+
if (field) {
|
|
24814
|
+
if (field.ORDER !== order)
|
|
24815
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
24816
|
+
validateField(field);
|
|
24817
|
+
return field;
|
|
24818
|
+
} else {
|
|
24819
|
+
return Field(order, { isLE });
|
|
24820
|
+
}
|
|
24821
|
+
}
|
|
24822
|
+
function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
24823
|
+
if (FpFnLE === void 0)
|
|
24824
|
+
FpFnLE = type === "edwards";
|
|
24825
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
24826
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
24827
|
+
for (const p of ["p", "n", "h"]) {
|
|
24828
|
+
const val = CURVE[p];
|
|
24829
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
24830
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
24831
|
+
}
|
|
24832
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
24833
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
24834
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
24835
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
24836
|
+
for (const p of params) {
|
|
24837
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
24838
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
24839
|
+
}
|
|
24840
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
24841
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
24842
|
+
}
|
|
24843
|
+
|
|
24844
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/abstract/edwards.js
|
|
24845
|
+
var _0n4 = BigInt(0);
|
|
24846
|
+
var _1n4 = BigInt(1);
|
|
24847
|
+
var _2n2 = BigInt(2);
|
|
24848
|
+
var _8n2 = BigInt(8);
|
|
24849
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
24850
|
+
const x2 = Fp2.sqr(x);
|
|
24851
|
+
const y2 = Fp2.sqr(y);
|
|
24852
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
24853
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
24854
|
+
return Fp2.eql(left, right);
|
|
24855
|
+
}
|
|
24856
|
+
function edwards(params, extraOpts = {}) {
|
|
24857
|
+
const validated = _createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
24858
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
24859
|
+
let CURVE = validated.CURVE;
|
|
24860
|
+
const { h: cofactor } = CURVE;
|
|
24861
|
+
_validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
24862
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
24863
|
+
const modP = (n) => Fp2.create(n);
|
|
24864
|
+
const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
|
|
24865
|
+
try {
|
|
24866
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
24867
|
+
} catch (e) {
|
|
24868
|
+
return { isValid: false, value: _0n4 };
|
|
24869
|
+
}
|
|
24870
|
+
});
|
|
24871
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
24872
|
+
throw new Error("bad curve params: generator point");
|
|
24873
|
+
function acoord(title, n, banZero = false) {
|
|
24874
|
+
const min = banZero ? _1n4 : _0n4;
|
|
24875
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
24876
|
+
return n;
|
|
24877
|
+
}
|
|
24878
|
+
function aextpoint(other) {
|
|
24879
|
+
if (!(other instanceof Point))
|
|
24880
|
+
throw new Error("ExtendedPoint expected");
|
|
24881
|
+
}
|
|
24882
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
24883
|
+
const { X, Y, Z } = p;
|
|
24884
|
+
const is0 = p.is0();
|
|
24885
|
+
if (iz == null)
|
|
24886
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
24887
|
+
const x = modP(X * iz);
|
|
24888
|
+
const y = modP(Y * iz);
|
|
24889
|
+
const zz = Fp2.mul(Z, iz);
|
|
24890
|
+
if (is0)
|
|
24891
|
+
return { x: _0n4, y: _1n4 };
|
|
24892
|
+
if (zz !== _1n4)
|
|
24893
|
+
throw new Error("invZ was invalid");
|
|
24894
|
+
return { x, y };
|
|
24895
|
+
});
|
|
24896
|
+
const assertValidMemo = memoized((p) => {
|
|
24897
|
+
const { a, d } = CURVE;
|
|
24898
|
+
if (p.is0())
|
|
24899
|
+
throw new Error("bad point: ZERO");
|
|
24900
|
+
const { X, Y, Z, T } = p;
|
|
24901
|
+
const X2 = modP(X * X);
|
|
24902
|
+
const Y2 = modP(Y * Y);
|
|
24903
|
+
const Z2 = modP(Z * Z);
|
|
24904
|
+
const Z4 = modP(Z2 * Z2);
|
|
24905
|
+
const aX2 = modP(X2 * a);
|
|
24906
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
24907
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
24908
|
+
if (left !== right)
|
|
24909
|
+
throw new Error("bad point: equation left != right (1)");
|
|
24910
|
+
const XY = modP(X * Y);
|
|
24911
|
+
const ZT = modP(Z * T);
|
|
24912
|
+
if (XY !== ZT)
|
|
24913
|
+
throw new Error("bad point: equation left != right (2)");
|
|
24914
|
+
return true;
|
|
24915
|
+
});
|
|
24916
|
+
class Point {
|
|
24917
|
+
constructor(X, Y, Z, T) {
|
|
24918
|
+
this.X = acoord("x", X);
|
|
24919
|
+
this.Y = acoord("y", Y);
|
|
24920
|
+
this.Z = acoord("z", Z, true);
|
|
24921
|
+
this.T = acoord("t", T);
|
|
24922
|
+
Object.freeze(this);
|
|
24923
|
+
}
|
|
24924
|
+
static CURVE() {
|
|
24925
|
+
return CURVE;
|
|
24926
|
+
}
|
|
24927
|
+
static fromAffine(p) {
|
|
24928
|
+
if (p instanceof Point)
|
|
24929
|
+
throw new Error("extended point not allowed");
|
|
24930
|
+
const { x, y } = p || {};
|
|
24931
|
+
acoord("x", x);
|
|
24932
|
+
acoord("y", y);
|
|
24933
|
+
return new Point(x, y, _1n4, modP(x * y));
|
|
24934
|
+
}
|
|
24935
|
+
// Uses algo from RFC8032 5.1.3.
|
|
24936
|
+
static fromBytes(bytes, zip215 = false) {
|
|
24937
|
+
const len = Fp2.BYTES;
|
|
24938
|
+
const { a, d } = CURVE;
|
|
24939
|
+
bytes = copyBytes(_abytes2(bytes, len, "point"));
|
|
24940
|
+
_abool2(zip215, "zip215");
|
|
24941
|
+
const normed = copyBytes(bytes);
|
|
24942
|
+
const lastByte = bytes[len - 1];
|
|
24943
|
+
normed[len - 1] = lastByte & ~128;
|
|
24944
|
+
const y = bytesToNumberLE(normed);
|
|
24945
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
24946
|
+
aInRange("point.y", y, _0n4, max);
|
|
24947
|
+
const y2 = modP(y * y);
|
|
24948
|
+
const u = modP(y2 - _1n4);
|
|
24949
|
+
const v = modP(d * y2 - a);
|
|
24950
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
24951
|
+
if (!isValid)
|
|
24952
|
+
throw new Error("bad point: invalid y coordinate");
|
|
24953
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
24954
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
24955
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
24956
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
24957
|
+
if (isLastByteOdd !== isXOdd)
|
|
24958
|
+
x = modP(-x);
|
|
24959
|
+
return Point.fromAffine({ x, y });
|
|
24960
|
+
}
|
|
24961
|
+
static fromHex(bytes, zip215 = false) {
|
|
24962
|
+
return Point.fromBytes(ensureBytes("point", bytes), zip215);
|
|
24963
|
+
}
|
|
24964
|
+
get x() {
|
|
24965
|
+
return this.toAffine().x;
|
|
24966
|
+
}
|
|
24967
|
+
get y() {
|
|
24968
|
+
return this.toAffine().y;
|
|
24969
|
+
}
|
|
24970
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
24971
|
+
wnaf.createCache(this, windowSize);
|
|
24972
|
+
if (!isLazy)
|
|
24973
|
+
this.multiply(_2n2);
|
|
24974
|
+
return this;
|
|
24975
|
+
}
|
|
24976
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
24977
|
+
assertValidity() {
|
|
24978
|
+
assertValidMemo(this);
|
|
24979
|
+
}
|
|
24980
|
+
// Compare one point to another.
|
|
24981
|
+
equals(other) {
|
|
24982
|
+
aextpoint(other);
|
|
24983
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
24984
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
24985
|
+
const X1Z2 = modP(X1 * Z2);
|
|
24986
|
+
const X2Z1 = modP(X2 * Z1);
|
|
24987
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
24988
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
24989
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
24990
|
+
}
|
|
24991
|
+
is0() {
|
|
24992
|
+
return this.equals(Point.ZERO);
|
|
24993
|
+
}
|
|
24994
|
+
negate() {
|
|
24995
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
24996
|
+
}
|
|
24997
|
+
// Fast algo for doubling Extended Point.
|
|
24998
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
24999
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
25000
|
+
double() {
|
|
25001
|
+
const { a } = CURVE;
|
|
25002
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
25003
|
+
const A = modP(X1 * X1);
|
|
25004
|
+
const B = modP(Y1 * Y1);
|
|
25005
|
+
const C = modP(_2n2 * modP(Z1 * Z1));
|
|
25006
|
+
const D = modP(a * A);
|
|
25007
|
+
const x1y1 = X1 + Y1;
|
|
25008
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
25009
|
+
const G = D + B;
|
|
25010
|
+
const F = G - C;
|
|
25011
|
+
const H = D - B;
|
|
25012
|
+
const X3 = modP(E * F);
|
|
25013
|
+
const Y3 = modP(G * H);
|
|
25014
|
+
const T3 = modP(E * H);
|
|
25015
|
+
const Z3 = modP(F * G);
|
|
25016
|
+
return new Point(X3, Y3, Z3, T3);
|
|
25017
|
+
}
|
|
25018
|
+
// Fast algo for adding 2 Extended Points.
|
|
25019
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
25020
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
25021
|
+
add(other) {
|
|
25022
|
+
aextpoint(other);
|
|
25023
|
+
const { a, d } = CURVE;
|
|
25024
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
25025
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
25026
|
+
const A = modP(X1 * X2);
|
|
25027
|
+
const B = modP(Y1 * Y2);
|
|
25028
|
+
const C = modP(T1 * d * T2);
|
|
25029
|
+
const D = modP(Z1 * Z2);
|
|
25030
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
25031
|
+
const F = D - C;
|
|
25032
|
+
const G = D + C;
|
|
25033
|
+
const H = modP(B - a * A);
|
|
25034
|
+
const X3 = modP(E * F);
|
|
25035
|
+
const Y3 = modP(G * H);
|
|
25036
|
+
const T3 = modP(E * H);
|
|
25037
|
+
const Z3 = modP(F * G);
|
|
25038
|
+
return new Point(X3, Y3, Z3, T3);
|
|
25039
|
+
}
|
|
25040
|
+
subtract(other) {
|
|
25041
|
+
return this.add(other.negate());
|
|
25042
|
+
}
|
|
25043
|
+
// Constant-time multiplication.
|
|
25044
|
+
multiply(scalar) {
|
|
25045
|
+
if (!Fn2.isValidNot0(scalar))
|
|
25046
|
+
throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
25047
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
|
|
25048
|
+
return normalizeZ(Point, [p, f])[0];
|
|
25049
|
+
}
|
|
25050
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
25051
|
+
// It's faster, but should only be used when you don't care about
|
|
25052
|
+
// an exposed private key e.g. sig verification.
|
|
25053
|
+
// Does NOT allow scalars higher than CURVE.n.
|
|
25054
|
+
// Accepts optional accumulator to merge with multiply (important for sparse scalars)
|
|
25055
|
+
multiplyUnsafe(scalar, acc = Point.ZERO) {
|
|
25056
|
+
if (!Fn2.isValid(scalar))
|
|
25057
|
+
throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
25058
|
+
if (scalar === _0n4)
|
|
25059
|
+
return Point.ZERO;
|
|
25060
|
+
if (this.is0() || scalar === _1n4)
|
|
25061
|
+
return this;
|
|
25062
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
|
|
25063
|
+
}
|
|
25064
|
+
// Checks if point is of small order.
|
|
25065
|
+
// If you add something to small order point, you will have "dirty"
|
|
25066
|
+
// point with torsion component.
|
|
25067
|
+
// Multiplies point by cofactor and checks if the result is 0.
|
|
25068
|
+
isSmallOrder() {
|
|
25069
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
25070
|
+
}
|
|
25071
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
25072
|
+
// Returns `false` is the point is dirty.
|
|
25073
|
+
isTorsionFree() {
|
|
25074
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
25075
|
+
}
|
|
25076
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
25077
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
25078
|
+
toAffine(invertedZ) {
|
|
25079
|
+
return toAffineMemo(this, invertedZ);
|
|
25080
|
+
}
|
|
25081
|
+
clearCofactor() {
|
|
25082
|
+
if (cofactor === _1n4)
|
|
25083
|
+
return this;
|
|
25084
|
+
return this.multiplyUnsafe(cofactor);
|
|
25085
|
+
}
|
|
25086
|
+
toBytes() {
|
|
25087
|
+
const { x, y } = this.toAffine();
|
|
25088
|
+
const bytes = Fp2.toBytes(y);
|
|
25089
|
+
bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
|
|
25090
|
+
return bytes;
|
|
25091
|
+
}
|
|
25092
|
+
toHex() {
|
|
25093
|
+
return bytesToHex(this.toBytes());
|
|
25094
|
+
}
|
|
25095
|
+
toString() {
|
|
25096
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
25097
|
+
}
|
|
25098
|
+
// TODO: remove
|
|
25099
|
+
get ex() {
|
|
25100
|
+
return this.X;
|
|
25101
|
+
}
|
|
25102
|
+
get ey() {
|
|
25103
|
+
return this.Y;
|
|
25104
|
+
}
|
|
25105
|
+
get ez() {
|
|
25106
|
+
return this.Z;
|
|
25107
|
+
}
|
|
25108
|
+
get et() {
|
|
25109
|
+
return this.T;
|
|
25110
|
+
}
|
|
25111
|
+
static normalizeZ(points) {
|
|
25112
|
+
return normalizeZ(Point, points);
|
|
25113
|
+
}
|
|
25114
|
+
static msm(points, scalars) {
|
|
25115
|
+
return pippenger(Point, Fn2, points, scalars);
|
|
25116
|
+
}
|
|
25117
|
+
_setWindowSize(windowSize) {
|
|
25118
|
+
this.precompute(windowSize);
|
|
25119
|
+
}
|
|
25120
|
+
toRawBytes() {
|
|
25121
|
+
return this.toBytes();
|
|
25122
|
+
}
|
|
25123
|
+
}
|
|
25124
|
+
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
25125
|
+
Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
25126
|
+
Point.Fp = Fp2;
|
|
25127
|
+
Point.Fn = Fn2;
|
|
25128
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
25129
|
+
Point.BASE.precompute(8);
|
|
25130
|
+
return Point;
|
|
25131
|
+
}
|
|
25132
|
+
var PrimeEdwardsPoint = class {
|
|
25133
|
+
constructor(ep) {
|
|
25134
|
+
this.ep = ep;
|
|
25135
|
+
}
|
|
25136
|
+
// Static methods that must be implemented by subclasses
|
|
25137
|
+
static fromBytes(_bytes) {
|
|
25138
|
+
notImplemented();
|
|
25139
|
+
}
|
|
25140
|
+
static fromHex(_hex) {
|
|
25141
|
+
notImplemented();
|
|
25142
|
+
}
|
|
25143
|
+
get x() {
|
|
25144
|
+
return this.toAffine().x;
|
|
25145
|
+
}
|
|
25146
|
+
get y() {
|
|
25147
|
+
return this.toAffine().y;
|
|
25148
|
+
}
|
|
25149
|
+
// Common implementations
|
|
25150
|
+
clearCofactor() {
|
|
25151
|
+
return this;
|
|
25152
|
+
}
|
|
25153
|
+
assertValidity() {
|
|
25154
|
+
this.ep.assertValidity();
|
|
25155
|
+
}
|
|
25156
|
+
toAffine(invertedZ) {
|
|
25157
|
+
return this.ep.toAffine(invertedZ);
|
|
25158
|
+
}
|
|
25159
|
+
toHex() {
|
|
25160
|
+
return bytesToHex(this.toBytes());
|
|
25161
|
+
}
|
|
25162
|
+
toString() {
|
|
25163
|
+
return this.toHex();
|
|
25164
|
+
}
|
|
25165
|
+
isTorsionFree() {
|
|
25166
|
+
return true;
|
|
25167
|
+
}
|
|
25168
|
+
isSmallOrder() {
|
|
25169
|
+
return false;
|
|
25170
|
+
}
|
|
25171
|
+
add(other) {
|
|
25172
|
+
this.assertSame(other);
|
|
25173
|
+
return this.init(this.ep.add(other.ep));
|
|
25174
|
+
}
|
|
25175
|
+
subtract(other) {
|
|
25176
|
+
this.assertSame(other);
|
|
25177
|
+
return this.init(this.ep.subtract(other.ep));
|
|
25178
|
+
}
|
|
25179
|
+
multiply(scalar) {
|
|
25180
|
+
return this.init(this.ep.multiply(scalar));
|
|
25181
|
+
}
|
|
25182
|
+
multiplyUnsafe(scalar) {
|
|
25183
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
25184
|
+
}
|
|
25185
|
+
double() {
|
|
25186
|
+
return this.init(this.ep.double());
|
|
25187
|
+
}
|
|
25188
|
+
negate() {
|
|
25189
|
+
return this.init(this.ep.negate());
|
|
25190
|
+
}
|
|
25191
|
+
precompute(windowSize, isLazy) {
|
|
25192
|
+
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
25193
|
+
}
|
|
25194
|
+
/** @deprecated use `toBytes` */
|
|
25195
|
+
toRawBytes() {
|
|
25196
|
+
return this.toBytes();
|
|
25197
|
+
}
|
|
25198
|
+
};
|
|
25199
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
25200
|
+
if (typeof cHash !== "function")
|
|
25201
|
+
throw new Error('"hash" function param is required');
|
|
25202
|
+
_validateObject(eddsaOpts, {}, {
|
|
25203
|
+
adjustScalarBytes: "function",
|
|
25204
|
+
randomBytes: "function",
|
|
25205
|
+
domain: "function",
|
|
25206
|
+
prehash: "function",
|
|
25207
|
+
mapToCurve: "function"
|
|
25208
|
+
});
|
|
25209
|
+
const { prehash } = eddsaOpts;
|
|
25210
|
+
const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
|
|
25211
|
+
const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
|
|
25212
|
+
const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
|
|
25213
|
+
const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
|
|
25214
|
+
_abool2(phflag, "phflag");
|
|
25215
|
+
if (ctx.length || phflag)
|
|
25216
|
+
throw new Error("Contexts/pre-hash are not supported");
|
|
25217
|
+
return data;
|
|
25218
|
+
});
|
|
25219
|
+
function modN_LE(hash) {
|
|
25220
|
+
return Fn2.create(bytesToNumberLE(hash));
|
|
25221
|
+
}
|
|
25222
|
+
function getPrivateScalar(key) {
|
|
25223
|
+
const len = lengths.secretKey;
|
|
25224
|
+
key = ensureBytes("private key", key, len);
|
|
25225
|
+
const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
|
|
25226
|
+
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
25227
|
+
const prefix = hashed.slice(len, 2 * len);
|
|
25228
|
+
const scalar = modN_LE(head);
|
|
25229
|
+
return { head, prefix, scalar };
|
|
25230
|
+
}
|
|
25231
|
+
function getExtendedPublicKey(secretKey) {
|
|
25232
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
25233
|
+
const point = BASE.multiply(scalar);
|
|
25234
|
+
const pointBytes = point.toBytes();
|
|
25235
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
25236
|
+
}
|
|
25237
|
+
function getPublicKey(secretKey) {
|
|
25238
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
25239
|
+
}
|
|
25240
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
25241
|
+
const msg = concatBytes(...msgs);
|
|
25242
|
+
return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
|
|
25243
|
+
}
|
|
25244
|
+
function sign(msg, secretKey, options = {}) {
|
|
25245
|
+
msg = ensureBytes("message", msg);
|
|
25246
|
+
if (prehash)
|
|
25247
|
+
msg = prehash(msg);
|
|
25248
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
25249
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
25250
|
+
const R = BASE.multiply(r).toBytes();
|
|
25251
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
25252
|
+
const s = Fn2.create(r + k * scalar);
|
|
25253
|
+
if (!Fn2.isValid(s))
|
|
25254
|
+
throw new Error("sign failed: invalid s");
|
|
25255
|
+
const rs = concatBytes(R, Fn2.toBytes(s));
|
|
25256
|
+
return _abytes2(rs, lengths.signature, "result");
|
|
25257
|
+
}
|
|
25258
|
+
const verifyOpts = { zip215: true };
|
|
25259
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
25260
|
+
const { context, zip215 } = options;
|
|
25261
|
+
const len = lengths.signature;
|
|
25262
|
+
sig = ensureBytes("signature", sig, len);
|
|
25263
|
+
msg = ensureBytes("message", msg);
|
|
25264
|
+
publicKey = ensureBytes("publicKey", publicKey, lengths.publicKey);
|
|
25265
|
+
if (zip215 !== void 0)
|
|
25266
|
+
_abool2(zip215, "zip215");
|
|
25267
|
+
if (prehash)
|
|
25268
|
+
msg = prehash(msg);
|
|
25269
|
+
const mid = len / 2;
|
|
25270
|
+
const r = sig.subarray(0, mid);
|
|
25271
|
+
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
25272
|
+
let A, R, SB;
|
|
25273
|
+
try {
|
|
25274
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
25275
|
+
R = Point.fromBytes(r, zip215);
|
|
25276
|
+
SB = BASE.multiplyUnsafe(s);
|
|
25277
|
+
} catch (error) {
|
|
25278
|
+
return false;
|
|
25279
|
+
}
|
|
25280
|
+
if (!zip215 && A.isSmallOrder())
|
|
25281
|
+
return false;
|
|
25282
|
+
const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
|
|
25283
|
+
const RkA = R.add(A.multiplyUnsafe(k));
|
|
25284
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
25285
|
+
}
|
|
25286
|
+
const _size = Fp2.BYTES;
|
|
25287
|
+
const lengths = {
|
|
25288
|
+
secretKey: _size,
|
|
25289
|
+
publicKey: _size,
|
|
25290
|
+
signature: 2 * _size,
|
|
25291
|
+
seed: _size
|
|
25292
|
+
};
|
|
25293
|
+
function randomSecretKey(seed = randomBytes2(lengths.seed)) {
|
|
25294
|
+
return _abytes2(seed, lengths.seed, "seed");
|
|
25295
|
+
}
|
|
25296
|
+
function keygen(seed) {
|
|
25297
|
+
const secretKey = utils.randomSecretKey(seed);
|
|
25298
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
25299
|
+
}
|
|
25300
|
+
function isValidSecretKey(key) {
|
|
25301
|
+
return isBytes(key) && key.length === Fn2.BYTES;
|
|
25302
|
+
}
|
|
25303
|
+
function isValidPublicKey(key, zip215) {
|
|
25304
|
+
try {
|
|
25305
|
+
return !!Point.fromBytes(key, zip215);
|
|
25306
|
+
} catch (error) {
|
|
25307
|
+
return false;
|
|
25308
|
+
}
|
|
25309
|
+
}
|
|
25310
|
+
const utils = {
|
|
25311
|
+
getExtendedPublicKey,
|
|
25312
|
+
randomSecretKey,
|
|
25313
|
+
isValidSecretKey,
|
|
25314
|
+
isValidPublicKey,
|
|
25315
|
+
/**
|
|
25316
|
+
* Converts ed public key to x public key. Uses formula:
|
|
25317
|
+
* - ed25519:
|
|
25318
|
+
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
25319
|
+
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
25320
|
+
* - ed448:
|
|
25321
|
+
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
25322
|
+
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
25323
|
+
*/
|
|
25324
|
+
toMontgomery(publicKey) {
|
|
25325
|
+
const { y } = Point.fromBytes(publicKey);
|
|
25326
|
+
const size = lengths.publicKey;
|
|
25327
|
+
const is25519 = size === 32;
|
|
25328
|
+
if (!is25519 && size !== 57)
|
|
25329
|
+
throw new Error("only defined for 25519 and 448");
|
|
25330
|
+
const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
|
|
25331
|
+
return Fp2.toBytes(u);
|
|
25332
|
+
},
|
|
25333
|
+
toMontgomerySecret(secretKey) {
|
|
25334
|
+
const size = lengths.secretKey;
|
|
25335
|
+
_abytes2(secretKey, size);
|
|
25336
|
+
const hashed = cHash(secretKey.subarray(0, size));
|
|
25337
|
+
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
25338
|
+
},
|
|
25339
|
+
/** @deprecated */
|
|
25340
|
+
randomPrivateKey: randomSecretKey,
|
|
25341
|
+
/** @deprecated */
|
|
25342
|
+
precompute(windowSize = 8, point = Point.BASE) {
|
|
25343
|
+
return point.precompute(windowSize, false);
|
|
25344
|
+
}
|
|
25345
|
+
};
|
|
25346
|
+
return Object.freeze({
|
|
25347
|
+
keygen,
|
|
25348
|
+
getPublicKey,
|
|
25349
|
+
sign,
|
|
25350
|
+
verify,
|
|
25351
|
+
utils,
|
|
25352
|
+
Point,
|
|
25353
|
+
lengths
|
|
25354
|
+
});
|
|
25355
|
+
}
|
|
25356
|
+
function _eddsa_legacy_opts_to_new(c) {
|
|
25357
|
+
const CURVE = {
|
|
25358
|
+
a: c.a,
|
|
25359
|
+
d: c.d,
|
|
25360
|
+
p: c.Fp.ORDER,
|
|
25361
|
+
n: c.n,
|
|
25362
|
+
h: c.h,
|
|
25363
|
+
Gx: c.Gx,
|
|
25364
|
+
Gy: c.Gy
|
|
25365
|
+
};
|
|
25366
|
+
const Fp2 = c.Fp;
|
|
25367
|
+
const Fn2 = Field(CURVE.n, c.nBitLength, true);
|
|
25368
|
+
const curveOpts = { Fp: Fp2, Fn: Fn2, uvRatio: c.uvRatio };
|
|
25369
|
+
const eddsaOpts = {
|
|
25370
|
+
randomBytes: c.randomBytes,
|
|
25371
|
+
adjustScalarBytes: c.adjustScalarBytes,
|
|
25372
|
+
domain: c.domain,
|
|
25373
|
+
prehash: c.prehash,
|
|
25374
|
+
mapToCurve: c.mapToCurve
|
|
25375
|
+
};
|
|
25376
|
+
return { CURVE, curveOpts, hash: c.hash, eddsaOpts };
|
|
25377
|
+
}
|
|
25378
|
+
function _eddsa_new_output_to_legacy(c, eddsa2) {
|
|
25379
|
+
const Point = eddsa2.Point;
|
|
25380
|
+
const legacy = Object.assign({}, eddsa2, {
|
|
25381
|
+
ExtendedPoint: Point,
|
|
25382
|
+
CURVE: c,
|
|
25383
|
+
nBitLength: Point.Fn.BITS,
|
|
25384
|
+
nByteLength: Point.Fn.BYTES
|
|
25385
|
+
});
|
|
25386
|
+
return legacy;
|
|
25387
|
+
}
|
|
25388
|
+
function twistedEdwards(c) {
|
|
25389
|
+
const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
|
|
25390
|
+
const Point = edwards(CURVE, curveOpts);
|
|
25391
|
+
const EDDSA = eddsa(Point, hash, eddsaOpts);
|
|
25392
|
+
return _eddsa_new_output_to_legacy(c, EDDSA);
|
|
25393
|
+
}
|
|
25394
|
+
|
|
25395
|
+
// ../mmt-manifest-verify/node_modules/@noble/curves/esm/ed25519.js
|
|
25396
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
25397
|
+
var _1n5 = BigInt(1);
|
|
25398
|
+
var _2n3 = BigInt(2);
|
|
25399
|
+
var _3n2 = BigInt(3);
|
|
25400
|
+
var _5n2 = BigInt(5);
|
|
25401
|
+
var _8n3 = BigInt(8);
|
|
25402
|
+
var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
25403
|
+
var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
25404
|
+
p: ed25519_CURVE_p,
|
|
25405
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
25406
|
+
h: _8n3,
|
|
25407
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
25408
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
25409
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
25410
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
25411
|
+
}))();
|
|
25412
|
+
function ed25519_pow_2_252_3(x) {
|
|
25413
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
25414
|
+
const P = ed25519_CURVE_p;
|
|
25415
|
+
const x2 = x * x % P;
|
|
25416
|
+
const b2 = x2 * x % P;
|
|
25417
|
+
const b4 = pow2(b2, _2n3, P) * b2 % P;
|
|
25418
|
+
const b5 = pow2(b4, _1n5, P) * x % P;
|
|
25419
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
25420
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
25421
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
25422
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
25423
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
25424
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
25425
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
25426
|
+
const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
|
|
25427
|
+
return { pow_p_5_8, b2 };
|
|
25428
|
+
}
|
|
25429
|
+
function adjustScalarBytes(bytes) {
|
|
25430
|
+
bytes[0] &= 248;
|
|
25431
|
+
bytes[31] &= 127;
|
|
25432
|
+
bytes[31] |= 64;
|
|
25433
|
+
return bytes;
|
|
25434
|
+
}
|
|
25435
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
25436
|
+
function uvRatio(u, v) {
|
|
25437
|
+
const P = ed25519_CURVE_p;
|
|
25438
|
+
const v3 = mod(v * v * v, P);
|
|
25439
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
25440
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
25441
|
+
let x = mod(u * v3 * pow, P);
|
|
25442
|
+
const vx2 = mod(v * x * x, P);
|
|
25443
|
+
const root1 = x;
|
|
25444
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
25445
|
+
const useRoot1 = vx2 === u;
|
|
25446
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
25447
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
25448
|
+
if (useRoot1)
|
|
25449
|
+
x = root1;
|
|
25450
|
+
if (useRoot2 || noRoot)
|
|
25451
|
+
x = root2;
|
|
25452
|
+
if (isNegativeLE(x, P))
|
|
25453
|
+
x = mod(-x, P);
|
|
25454
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
25455
|
+
}
|
|
25456
|
+
var Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
|
|
25457
|
+
var Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
|
|
25458
|
+
var ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
25459
|
+
...ed25519_CURVE,
|
|
25460
|
+
Fp,
|
|
25461
|
+
hash: sha512,
|
|
25462
|
+
adjustScalarBytes,
|
|
25463
|
+
// dom2
|
|
25464
|
+
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
25465
|
+
// Constant-time, u/√v
|
|
25466
|
+
uvRatio
|
|
25467
|
+
}))();
|
|
25468
|
+
var ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
|
25469
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
25470
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
25471
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
25472
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
25473
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
25474
|
+
var invertSqrt = (number) => uvRatio(_1n5, number);
|
|
25475
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
25476
|
+
var bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
25477
|
+
function calcElligatorRistrettoMap(r0) {
|
|
25478
|
+
const { d } = ed25519_CURVE;
|
|
25479
|
+
const P = ed25519_CURVE_p;
|
|
25480
|
+
const mod2 = (n) => Fp.create(n);
|
|
25481
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
25482
|
+
const Ns = mod2((r + _1n5) * ONE_MINUS_D_SQ);
|
|
25483
|
+
let c = BigInt(-1);
|
|
25484
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
25485
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
25486
|
+
let s_ = mod2(s * r0);
|
|
25487
|
+
if (!isNegativeLE(s_, P))
|
|
25488
|
+
s_ = mod2(-s_);
|
|
25489
|
+
if (!Ns_D_is_sq)
|
|
25490
|
+
s = s_;
|
|
25491
|
+
if (!Ns_D_is_sq)
|
|
25492
|
+
c = r;
|
|
25493
|
+
const Nt = mod2(c * (r - _1n5) * D_MINUS_ONE_SQ - D);
|
|
25494
|
+
const s2 = s * s;
|
|
25495
|
+
const W0 = mod2((s + s) * D);
|
|
25496
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
25497
|
+
const W2 = mod2(_1n5 - s2);
|
|
25498
|
+
const W3 = mod2(_1n5 + s2);
|
|
25499
|
+
return new ed25519.Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
25500
|
+
}
|
|
25501
|
+
function ristretto255_map(bytes) {
|
|
25502
|
+
abytes(bytes, 64);
|
|
25503
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
25504
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
25505
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
25506
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
25507
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
25508
|
+
}
|
|
25509
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
25510
|
+
constructor(ep) {
|
|
25511
|
+
super(ep);
|
|
25512
|
+
}
|
|
25513
|
+
static fromAffine(ap) {
|
|
25514
|
+
return new __RistrettoPoint(ed25519.Point.fromAffine(ap));
|
|
25515
|
+
}
|
|
25516
|
+
assertSame(other) {
|
|
25517
|
+
if (!(other instanceof __RistrettoPoint))
|
|
25518
|
+
throw new Error("RistrettoPoint expected");
|
|
25519
|
+
}
|
|
25520
|
+
init(ep) {
|
|
25521
|
+
return new __RistrettoPoint(ep);
|
|
25522
|
+
}
|
|
25523
|
+
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
25524
|
+
static hashToCurve(hex3) {
|
|
25525
|
+
return ristretto255_map(ensureBytes("ristrettoHash", hex3, 64));
|
|
25526
|
+
}
|
|
25527
|
+
static fromBytes(bytes) {
|
|
25528
|
+
abytes(bytes, 32);
|
|
25529
|
+
const { a, d } = ed25519_CURVE;
|
|
25530
|
+
const P = ed25519_CURVE_p;
|
|
25531
|
+
const mod2 = (n) => Fp.create(n);
|
|
25532
|
+
const s = bytes255ToNumberLE(bytes);
|
|
25533
|
+
if (!equalBytes2(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
25534
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
25535
|
+
const s2 = mod2(s * s);
|
|
25536
|
+
const u1 = mod2(_1n5 + a * s2);
|
|
25537
|
+
const u2 = mod2(_1n5 - a * s2);
|
|
25538
|
+
const u1_2 = mod2(u1 * u1);
|
|
25539
|
+
const u2_2 = mod2(u2 * u2);
|
|
25540
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
25541
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
25542
|
+
const Dx = mod2(I * u2);
|
|
25543
|
+
const Dy = mod2(I * Dx * v);
|
|
25544
|
+
let x = mod2((s + s) * Dx);
|
|
25545
|
+
if (isNegativeLE(x, P))
|
|
25546
|
+
x = mod2(-x);
|
|
25547
|
+
const y = mod2(u1 * Dy);
|
|
25548
|
+
const t = mod2(x * y);
|
|
25549
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n5)
|
|
25550
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
25551
|
+
return new __RistrettoPoint(new ed25519.Point(x, y, _1n5, t));
|
|
25552
|
+
}
|
|
25553
|
+
/**
|
|
25554
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
25555
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
25556
|
+
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
25557
|
+
*/
|
|
25558
|
+
static fromHex(hex3) {
|
|
25559
|
+
return __RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex3, 32));
|
|
25560
|
+
}
|
|
25561
|
+
static msm(points, scalars) {
|
|
25562
|
+
return pippenger(__RistrettoPoint, ed25519.Point.Fn, points, scalars);
|
|
25563
|
+
}
|
|
25564
|
+
/**
|
|
25565
|
+
* Encodes ristretto point to Uint8Array.
|
|
25566
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
25567
|
+
*/
|
|
25568
|
+
toBytes() {
|
|
25569
|
+
let { X, Y, Z, T } = this.ep;
|
|
25570
|
+
const P = ed25519_CURVE_p;
|
|
25571
|
+
const mod2 = (n) => Fp.create(n);
|
|
25572
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
25573
|
+
const u2 = mod2(X * Y);
|
|
25574
|
+
const u2sq = mod2(u2 * u2);
|
|
25575
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
25576
|
+
const D1 = mod2(invsqrt * u1);
|
|
25577
|
+
const D2 = mod2(invsqrt * u2);
|
|
25578
|
+
const zInv = mod2(D1 * D2 * T);
|
|
25579
|
+
let D;
|
|
25580
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
25581
|
+
let _x = mod2(Y * SQRT_M1);
|
|
25582
|
+
let _y = mod2(X * SQRT_M1);
|
|
25583
|
+
X = _x;
|
|
25584
|
+
Y = _y;
|
|
25585
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
25586
|
+
} else {
|
|
25587
|
+
D = D2;
|
|
25588
|
+
}
|
|
25589
|
+
if (isNegativeLE(X * zInv, P))
|
|
25590
|
+
Y = mod2(-Y);
|
|
25591
|
+
let s = mod2((Z - Y) * D);
|
|
25592
|
+
if (isNegativeLE(s, P))
|
|
25593
|
+
s = mod2(-s);
|
|
25594
|
+
return Fp.toBytes(s);
|
|
25595
|
+
}
|
|
25596
|
+
/**
|
|
25597
|
+
* Compares two Ristretto points.
|
|
25598
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
25599
|
+
*/
|
|
25600
|
+
equals(other) {
|
|
25601
|
+
this.assertSame(other);
|
|
25602
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
25603
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
25604
|
+
const mod2 = (n) => Fp.create(n);
|
|
25605
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
25606
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
25607
|
+
return one || two;
|
|
25608
|
+
}
|
|
25609
|
+
is0() {
|
|
25610
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
25611
|
+
}
|
|
25612
|
+
};
|
|
25613
|
+
_RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
|
|
25614
|
+
_RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
|
|
25615
|
+
_RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
|
|
25616
|
+
_RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
|
|
25617
|
+
|
|
25618
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2-receiver.js
|
|
25619
|
+
var BCPV2_LEAD_BOUND_MS = 5e3;
|
|
25620
|
+
var REPLAY_WINDOW = 64n;
|
|
25621
|
+
var BUCKET_DEPTH = 64;
|
|
25622
|
+
var BUCKET_RATE_PER_MS = 1;
|
|
25623
|
+
var hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
25624
|
+
function sameBytes(a, b) {
|
|
25625
|
+
if (a.byteLength !== b.byteLength)
|
|
25626
|
+
return false;
|
|
25627
|
+
let diff = 0;
|
|
25628
|
+
for (let i = 0; i < a.byteLength; i++)
|
|
25629
|
+
diff |= a[i] ^ b[i];
|
|
25630
|
+
return diff === 0;
|
|
25631
|
+
}
|
|
25632
|
+
var BcProvenanceV2Receiver = class {
|
|
25633
|
+
maxAuthDelayMs;
|
|
25634
|
+
manifestTimerMs;
|
|
25635
|
+
widenMs;
|
|
25636
|
+
/** Datagrams that left a hold queue at expiry, per path: the loss signal for failover. */
|
|
25637
|
+
expired = { multicast: 0, unicast: 0 };
|
|
25638
|
+
cfg;
|
|
25639
|
+
now;
|
|
25640
|
+
verifySig;
|
|
25641
|
+
accepted;
|
|
25642
|
+
queueLimit;
|
|
25643
|
+
current = null;
|
|
25644
|
+
previous = null;
|
|
25645
|
+
graceStart = {};
|
|
25646
|
+
hashAlg = null;
|
|
25647
|
+
verified = /* @__PURE__ */ new Map();
|
|
25648
|
+
highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
25649
|
+
closedAt = /* @__PURE__ */ new Map();
|
|
25650
|
+
lead = null;
|
|
25651
|
+
pending = /* @__PURE__ */ new Map();
|
|
25652
|
+
admittedUntil = /* @__PURE__ */ new Map();
|
|
25653
|
+
hold = { multicast: [], unicast: [] };
|
|
25654
|
+
holdBytes = { multicast: 0, unicast: 0 };
|
|
25655
|
+
order = 0;
|
|
25656
|
+
buckets;
|
|
25657
|
+
constructor(cfg) {
|
|
25658
|
+
this.maxAuthDelayMs = cfg.maxAuthDelayMs ?? 200;
|
|
25659
|
+
this.manifestTimerMs = cfg.manifestTimerMs ?? 20;
|
|
25660
|
+
if (!(this.maxAuthDelayMs >= 50 && this.maxAuthDelayMs <= 1e3) || !(this.manifestTimerMs >= 1)) {
|
|
25661
|
+
throw new Error("BCPV2 endpoint ignored: maxAuthDelayMs or manifestTimerMs out of range");
|
|
25662
|
+
}
|
|
25663
|
+
if (!(cfg.bandwidth > 0))
|
|
25664
|
+
throw new Error("BCPV2 endpoint ignored: protected endpoint without bandwidth");
|
|
25665
|
+
const kids = new Set(cfg.keys.map((k) => k.kid));
|
|
25666
|
+
const firsts = new Set(cfg.keys.map((k) => k.firstEpoch));
|
|
25667
|
+
if (cfg.keys.length < 1 || kids.size !== cfg.keys.length || firsts.size !== cfg.keys.length) {
|
|
25668
|
+
throw new Error("BCPV2 key set invalid: no keys, or a repeated kid or firstEpoch");
|
|
25669
|
+
}
|
|
25670
|
+
this.cfg = cfg;
|
|
25671
|
+
this.widenMs = Math.min(Math.max(cfg.widenMs ?? 0, 0), BCPV2_LEAD_BOUND_MS);
|
|
25672
|
+
this.now = cfg.now ?? Date.now;
|
|
25673
|
+
this.verifySig = cfg.verifySignature ?? ((pk, msg, sig) => {
|
|
25674
|
+
try {
|
|
25675
|
+
return ed25519.verify(sig, msg, pk, { zip215: false });
|
|
25676
|
+
} catch {
|
|
25677
|
+
return false;
|
|
25678
|
+
}
|
|
25679
|
+
});
|
|
25680
|
+
this.accepted = cfg.acceptedHashAlgs ?? [BCPV2_HASH_ALG_SHA256];
|
|
25681
|
+
const bandwidth = Math.min(cfg.bandwidth, 1e10);
|
|
25682
|
+
this.queueLimit = {
|
|
25683
|
+
multicast: this.maxAuthDelayMs * bandwidth / 8e3,
|
|
25684
|
+
unicast: (this.maxAuthDelayMs + this.widenMs) * bandwidth / 8e3
|
|
25685
|
+
};
|
|
25686
|
+
const t = this.now();
|
|
25687
|
+
this.buckets = {
|
|
25688
|
+
multicast: { tokens: BUCKET_DEPTH, last: t, dropped: 0 },
|
|
25689
|
+
mirror: { tokens: BUCKET_DEPTH, last: t, dropped: 0 }
|
|
25690
|
+
};
|
|
25691
|
+
}
|
|
25692
|
+
get grace() {
|
|
25693
|
+
return this.maxAuthDelayMs + 2 * this.manifestTimerMs;
|
|
25694
|
+
}
|
|
25695
|
+
/** The mirror's lead in force: 0 without a manifest track, the bound until one is measured. */
|
|
25696
|
+
get leadMs() {
|
|
25697
|
+
if (!this.cfg.applyLead)
|
|
25698
|
+
return 0;
|
|
25699
|
+
return this.lead ?? BCPV2_LEAD_BOUND_MS;
|
|
25700
|
+
}
|
|
25701
|
+
/** Step 1: a record datagram on `manifestPort`, or a record object of the manifest track. */
|
|
25702
|
+
onRecord(bytes, path) {
|
|
25703
|
+
const now = this.now();
|
|
25704
|
+
this.expire(now);
|
|
25705
|
+
const drop = (verdict) => ({ verdict, admitted: [] });
|
|
25706
|
+
const len = bytes.byteLength;
|
|
25707
|
+
if (len < BCPV2_MIN_RECORD_LENGTH || len > BCPV2_MAX_RECORD_LENGTH || (len - 132) % 32 !== 0)
|
|
25708
|
+
return drop("length");
|
|
25709
|
+
const view2 = new DataView(bytes.buffer, bytes.byteOffset, len);
|
|
25710
|
+
if (view2.getUint32(0) !== 1296583986)
|
|
25711
|
+
return drop("magic");
|
|
25712
|
+
const kid = view2.getUint32(4);
|
|
25713
|
+
const epoch = view2.getUint32(8);
|
|
25714
|
+
const mseq = view2.getBigUint64(12);
|
|
25715
|
+
const count = view2.getUint16(28);
|
|
25716
|
+
const hashAlg = bytes[30];
|
|
25717
|
+
const sent = view2.getUint32(32);
|
|
25718
|
+
const key = this.cfg.keys.find((k) => k.kid === kid);
|
|
25719
|
+
if (!key)
|
|
25720
|
+
return drop("kid");
|
|
25721
|
+
const next = Math.min(...this.cfg.keys.map((k) => k.firstEpoch).filter((f) => f > key.firstEpoch), Infinity);
|
|
25722
|
+
if (epoch < key.firstEpoch || epoch >= next)
|
|
25723
|
+
return drop("epoch");
|
|
25724
|
+
const id = `${kid}:${epoch}:${mseq}`;
|
|
25725
|
+
const seen = this.verified.get(id);
|
|
25726
|
+
if (seen) {
|
|
25727
|
+
const admitted2 = hex(sha2562(bytes)) === seen.digest ? this.arrival(seen, path, now) : [];
|
|
25728
|
+
return { verdict: "duplicate", admitted: admitted2 };
|
|
25729
|
+
}
|
|
25730
|
+
const ke = `${kid}:${epoch}`;
|
|
25731
|
+
if (this.current === null) {
|
|
25732
|
+
if (epoch < this.cfg.minEpoch || epoch < (this.cfg.pinnedEpochFloor?.get(kid) ?? 0))
|
|
25733
|
+
return drop("epoch");
|
|
25734
|
+
} else if (epoch < this.current.epoch && !this.previousAdmitted(epoch, path, now)) {
|
|
25735
|
+
return drop("epoch");
|
|
25736
|
+
}
|
|
25737
|
+
const floor = this.floor(ke, path);
|
|
25738
|
+
if (floor !== void 0 && mseq <= floor - REPLAY_WINDOW)
|
|
25739
|
+
return drop("mseq");
|
|
25740
|
+
const closed = this.closedAt.get(ke);
|
|
25741
|
+
if (closed !== void 0 && mseq > closed)
|
|
25742
|
+
return drop("closed");
|
|
25743
|
+
if (this.cfg.checkFreshness !== false) {
|
|
25744
|
+
const behind = 7e3 + this.maxAuthDelayMs + (path === "mirror" ? this.widenMs : 0);
|
|
25745
|
+
if (sent * 1e3 < now - behind || sent * 1e3 > now + 6e3)
|
|
25746
|
+
return drop("sent");
|
|
25747
|
+
}
|
|
25748
|
+
if (count !== (len - 132) / 32)
|
|
25749
|
+
return drop("count");
|
|
25750
|
+
if (!this.accepted.includes(hashAlg) || this.hashAlg !== null && hashAlg !== this.hashAlg)
|
|
25751
|
+
return drop("hash_alg");
|
|
25752
|
+
if (this.cfg.catalogCtx && !sameBytes(bytes.subarray(36, 68), this.cfg.catalogCtx))
|
|
25753
|
+
return drop("ctx");
|
|
25754
|
+
if (!this.take(path, now))
|
|
25755
|
+
return drop("budget");
|
|
25756
|
+
const record = decodeBcProvenanceV2Record(bytes);
|
|
25757
|
+
if (!this.verifySig(key.publicKey, bcProvenanceV2SignedMessage(record, this.cfg.endpoint), record.signature)) {
|
|
25758
|
+
return drop("signature");
|
|
25759
|
+
}
|
|
25760
|
+
if (this.current === null || epoch > this.current.epoch) {
|
|
25761
|
+
if (this.current !== null) {
|
|
25762
|
+
this.previous = { epoch: this.current.epoch, deadline: now + BCPV2_LEAD_BOUND_MS + this.grace };
|
|
25763
|
+
}
|
|
25764
|
+
this.current = { epoch, since: now };
|
|
25765
|
+
this.graceStart = { [path]: now };
|
|
25766
|
+
if (this.hashAlg === null) {
|
|
25767
|
+
this.hashAlg = hashAlg;
|
|
25768
|
+
for (const queue of Object.values(this.hold)) {
|
|
25769
|
+
for (const held of queue)
|
|
25770
|
+
held.digest ??= this.digest(held.datagram);
|
|
25771
|
+
}
|
|
25772
|
+
}
|
|
25773
|
+
} else if (epoch === this.current.epoch) {
|
|
25774
|
+
this.graceStart[path] ??= now;
|
|
25775
|
+
}
|
|
25776
|
+
const entry = {
|
|
25777
|
+
kid,
|
|
25778
|
+
epoch,
|
|
25779
|
+
mseq,
|
|
25780
|
+
path,
|
|
25781
|
+
arrival: now,
|
|
25782
|
+
sent,
|
|
25783
|
+
digest: hex(sha2562(bytes)),
|
|
25784
|
+
pairs: record.digests.map((d, i) => ({ digest: hex(d), seq: record.firstSeq + BigInt(i), admitted: false })),
|
|
25785
|
+
awaitingMulticast: path === "mirror"
|
|
25786
|
+
};
|
|
25787
|
+
this.verified.set(id, entry);
|
|
25788
|
+
this.raise(ke, path, mseq);
|
|
25789
|
+
if (record.flags & 1)
|
|
25790
|
+
this.closedAt.set(ke, mseq);
|
|
25791
|
+
const widen = path === "mirror" ? this.cfg.applyLead ? this.leadMs : this.widenMs : 0;
|
|
25792
|
+
const admitted = [];
|
|
25793
|
+
for (const pair of entry.pairs)
|
|
25794
|
+
this.handlePair(pair, now, now + this.grace + widen, now, admitted);
|
|
25795
|
+
this.dropAdmittedHeld();
|
|
25796
|
+
this.prune(now);
|
|
25797
|
+
return { verdict: "verified", admitted };
|
|
25798
|
+
}
|
|
25799
|
+
/** Step 2: a data datagram on `port` (or a packet of a listed track over MoQ unicast). */
|
|
25800
|
+
onDatagram(datagram, path) {
|
|
25801
|
+
const now = this.now();
|
|
25802
|
+
this.expire(now);
|
|
25803
|
+
if (this.hashAlg !== null) {
|
|
25804
|
+
const d = this.digest(datagram);
|
|
25805
|
+
if (this.consumePending(d, now))
|
|
25806
|
+
return { verdict: "admitted", admitted: [datagram] };
|
|
25807
|
+
if (this.admittedUntil.has(d))
|
|
25808
|
+
return { verdict: "duplicate", admitted: [] };
|
|
25809
|
+
return this.enqueue({ digest: d, datagram, expiry: 0, order: 0 }, path, now);
|
|
25810
|
+
}
|
|
25811
|
+
return this.enqueue({ datagram, expiry: 0, order: 0 }, path, now);
|
|
25812
|
+
}
|
|
25813
|
+
/**
|
|
25814
|
+
* A source packet recovered by FEC decode, its base SS_ID (SS_Start + ESI)
|
|
25815
|
+
* appended: admitted only on a pending entry it consumes, never held. One no
|
|
25816
|
+
* verified record lists, or a copy of an admitted packet, is dropped.
|
|
25817
|
+
*/
|
|
25818
|
+
admitRecovered(datagram) {
|
|
25819
|
+
const now = this.now();
|
|
25820
|
+
this.expire(now);
|
|
25821
|
+
return this.hashAlg !== null && this.consumePending(this.digest(datagram), now);
|
|
25822
|
+
}
|
|
25823
|
+
/**
|
|
25824
|
+
* Discard the per-flow state on a `streamId` change or a key-set change that fails the
|
|
25825
|
+
* flow's current (kid, epoch): budgets and hold queues survive, held digests are cleared.
|
|
25826
|
+
*/
|
|
25827
|
+
resetGeneration() {
|
|
25828
|
+
this.current = null;
|
|
25829
|
+
this.previous = null;
|
|
25830
|
+
this.graceStart = {};
|
|
25831
|
+
this.hashAlg = null;
|
|
25832
|
+
this.verified.clear();
|
|
25833
|
+
this.highest = { multicast: /* @__PURE__ */ new Map(), mirror: /* @__PURE__ */ new Map() };
|
|
25834
|
+
this.closedAt.clear();
|
|
25835
|
+
this.lead = null;
|
|
25836
|
+
this.pending.clear();
|
|
25837
|
+
this.admittedUntil.clear();
|
|
25838
|
+
for (const queue of Object.values(this.hold))
|
|
25839
|
+
for (const held of queue)
|
|
25840
|
+
held.digest = void 0;
|
|
25841
|
+
}
|
|
25842
|
+
/** The receiver ceased to hold the endpoint: drop everything; no drop here is an expiry. */
|
|
25843
|
+
release() {
|
|
25844
|
+
this.resetGeneration();
|
|
25845
|
+
this.hold = { multicast: [], unicast: [] };
|
|
25846
|
+
this.holdBytes = { multicast: 0, unicast: 0 };
|
|
25847
|
+
}
|
|
25848
|
+
digest(datagram) {
|
|
25849
|
+
return hex(bcProvenanceV2DatagramDigest(this.cfg.endpoint, datagram));
|
|
25850
|
+
}
|
|
25851
|
+
/** Consume the lowest-seq pending entry for `digest`, admitting it. */
|
|
25852
|
+
consumePending(digest, now) {
|
|
25853
|
+
const list = this.pending.get(digest);
|
|
25854
|
+
if (!list?.length)
|
|
25855
|
+
return false;
|
|
25856
|
+
list.sort((a, b) => a.pair.seq < b.pair.seq ? -1 : a.pair.seq > b.pair.seq ? 1 : 0);
|
|
25857
|
+
const hit = list.shift();
|
|
25858
|
+
if (list.length === 0)
|
|
25859
|
+
this.pending.delete(digest);
|
|
25860
|
+
hit.pair.pending = void 0;
|
|
25861
|
+
hit.pair.admitted = true;
|
|
25862
|
+
this.admittedUntil.set(digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
25863
|
+
return true;
|
|
25864
|
+
}
|
|
25865
|
+
enqueue(held, path, now) {
|
|
25866
|
+
if (this.holdBytes[path] + held.datagram.byteLength > this.queueLimit[path]) {
|
|
25867
|
+
return { verdict: "overflow", admitted: [] };
|
|
25868
|
+
}
|
|
25869
|
+
held.expiry = now + this.maxAuthDelayMs + (path === "unicast" ? this.widenMs : 0);
|
|
25870
|
+
held.order = this.order++;
|
|
25871
|
+
this.hold[path].push(held);
|
|
25872
|
+
this.holdBytes[path] += held.datagram.byteLength;
|
|
25873
|
+
return { verdict: "held", admitted: [] };
|
|
25874
|
+
}
|
|
25875
|
+
/** Remove and return the oldest held datagram with `digest`, across both queues. */
|
|
25876
|
+
takeHeld(digest) {
|
|
25877
|
+
let best;
|
|
25878
|
+
for (const path of ["multicast", "unicast"]) {
|
|
25879
|
+
const index = this.hold[path].findIndex((h) => h.digest === digest);
|
|
25880
|
+
if (index >= 0 && (!best || this.hold[path][index].order < best.order)) {
|
|
25881
|
+
best = { path, index, order: this.hold[path][index].order };
|
|
25882
|
+
}
|
|
25883
|
+
}
|
|
25884
|
+
if (!best)
|
|
25885
|
+
return void 0;
|
|
25886
|
+
const [held] = this.hold[best.path].splice(best.index, 1);
|
|
25887
|
+
this.holdBytes[best.path] -= held.datagram.byteLength;
|
|
25888
|
+
return held.datagram;
|
|
25889
|
+
}
|
|
25890
|
+
dropAdmittedHeld() {
|
|
25891
|
+
for (const path of ["multicast", "unicast"]) {
|
|
25892
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
25893
|
+
const keep = h.digest === void 0 || !this.admittedUntil.has(h.digest);
|
|
25894
|
+
if (!keep)
|
|
25895
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
25896
|
+
return keep;
|
|
25897
|
+
});
|
|
25898
|
+
}
|
|
25899
|
+
}
|
|
25900
|
+
/** Step 1 pair handling: admit a held copy, or insert a pending entry. */
|
|
25901
|
+
handlePair(pair, now, expiry, inserted, out) {
|
|
25902
|
+
const datagram = this.takeHeld(pair.digest);
|
|
25903
|
+
if (datagram) {
|
|
25904
|
+
pair.admitted = true;
|
|
25905
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + this.leadMs);
|
|
25906
|
+
out.push(datagram);
|
|
25907
|
+
return;
|
|
25908
|
+
}
|
|
25909
|
+
const entry = { pair, inserted, expiry };
|
|
25910
|
+
pair.pending = entry;
|
|
25911
|
+
const list = this.pending.get(pair.digest);
|
|
25912
|
+
if (list)
|
|
25913
|
+
list.push(entry);
|
|
25914
|
+
else
|
|
25915
|
+
this.pending.set(pair.digest, [entry]);
|
|
25916
|
+
}
|
|
25917
|
+
/** A byte-identical copy dropped by the already-verified lookup. */
|
|
25918
|
+
arrival(seen, path, now) {
|
|
25919
|
+
const ke = `${seen.kid}:${seen.epoch}`;
|
|
25920
|
+
this.raise(ke, path, seen.mseq);
|
|
25921
|
+
if (this.current && seen.epoch === this.current.epoch)
|
|
25922
|
+
this.graceStart[path] ??= now;
|
|
25923
|
+
if (path !== "multicast" || !seen.awaitingMulticast)
|
|
25924
|
+
return [];
|
|
25925
|
+
seen.awaitingMulticast = false;
|
|
25926
|
+
if (!this.cfg.applyLead)
|
|
25927
|
+
return [];
|
|
25928
|
+
const t = this.manifestTimerMs;
|
|
25929
|
+
const interval = Math.min(Math.ceil((now - seen.arrival) / t) * t, BCPV2_LEAD_BOUND_MS);
|
|
25930
|
+
if (this.lead === null) {
|
|
25931
|
+
this.lead = interval;
|
|
25932
|
+
return [];
|
|
25933
|
+
}
|
|
25934
|
+
if (interval <= this.lead)
|
|
25935
|
+
return [];
|
|
25936
|
+
this.lead = interval;
|
|
25937
|
+
const out = [];
|
|
25938
|
+
const records = [seen, ...[...this.verified.values()].filter((v) => v.awaitingMulticast && v.arrival >= now - interval)];
|
|
25939
|
+
for (const v of records) {
|
|
25940
|
+
for (const pair of v.pairs) {
|
|
25941
|
+
if (pair.admitted) {
|
|
25942
|
+
this.admittedUntil.set(pair.digest, now + this.maxAuthDelayMs + interval);
|
|
25943
|
+
} else if (pair.pending) {
|
|
25944
|
+
pair.pending.expiry = pair.pending.inserted + this.grace + interval;
|
|
25945
|
+
} else {
|
|
25946
|
+
this.handlePair(pair, now, now + this.grace + interval, now, out);
|
|
25947
|
+
}
|
|
25948
|
+
}
|
|
25949
|
+
}
|
|
25950
|
+
this.dropAdmittedHeld();
|
|
25951
|
+
return out;
|
|
25952
|
+
}
|
|
25953
|
+
previousAdmitted(epoch, path, now) {
|
|
25954
|
+
if (!this.previous || epoch !== this.previous.epoch)
|
|
25955
|
+
return false;
|
|
25956
|
+
const m = this.graceStart.multicast;
|
|
25957
|
+
if (path === "multicast")
|
|
25958
|
+
return m === void 0 || now <= m + this.grace;
|
|
25959
|
+
const starts = [m, this.graceStart.mirror].filter((s) => s !== void 0);
|
|
25960
|
+
return starts.length > 0 && now <= Math.min(...starts) + this.grace;
|
|
25961
|
+
}
|
|
25962
|
+
/** The replay floor this record meets: its path's, or the higher of both from the mirror. */
|
|
25963
|
+
floor(ke, path) {
|
|
25964
|
+
const m = this.highest.multicast.get(ke);
|
|
25965
|
+
if (path === "multicast")
|
|
25966
|
+
return m;
|
|
25967
|
+
const r = this.highest.mirror.get(ke);
|
|
25968
|
+
if (m === void 0)
|
|
25969
|
+
return r;
|
|
25970
|
+
return r === void 0 || m > r ? m : r;
|
|
25971
|
+
}
|
|
25972
|
+
raise(ke, path, mseq) {
|
|
25973
|
+
const h = this.highest[path].get(ke);
|
|
25974
|
+
if (h === void 0 || mseq > h)
|
|
25975
|
+
this.highest[path].set(ke, mseq);
|
|
25976
|
+
}
|
|
25977
|
+
take(path, now) {
|
|
25978
|
+
const b = this.buckets[path];
|
|
25979
|
+
b.tokens = Math.min(BUCKET_DEPTH, b.tokens + (now - b.last) * BUCKET_RATE_PER_MS);
|
|
25980
|
+
b.last = now;
|
|
25981
|
+
if (b.tokens < 1) {
|
|
25982
|
+
b.dropped++;
|
|
25983
|
+
return false;
|
|
25984
|
+
}
|
|
25985
|
+
b.tokens -= 1;
|
|
25986
|
+
return true;
|
|
25987
|
+
}
|
|
25988
|
+
expire(now) {
|
|
25989
|
+
for (const path of ["multicast", "unicast"]) {
|
|
25990
|
+
this.hold[path] = this.hold[path].filter((h) => {
|
|
25991
|
+
if (h.expiry > now)
|
|
25992
|
+
return true;
|
|
25993
|
+
this.holdBytes[path] -= h.datagram.byteLength;
|
|
25994
|
+
this.expired[path]++;
|
|
25995
|
+
return false;
|
|
25996
|
+
});
|
|
25997
|
+
}
|
|
25998
|
+
for (const [d, list] of this.pending) {
|
|
25999
|
+
const live = list.filter((p) => {
|
|
26000
|
+
if (p.expiry > now)
|
|
26001
|
+
return true;
|
|
26002
|
+
p.pair.pending = void 0;
|
|
26003
|
+
return false;
|
|
26004
|
+
});
|
|
26005
|
+
if (live.length)
|
|
26006
|
+
this.pending.set(d, live);
|
|
26007
|
+
else
|
|
26008
|
+
this.pending.delete(d);
|
|
26009
|
+
}
|
|
26010
|
+
for (const [d, until] of this.admittedUntil)
|
|
26011
|
+
if (until <= now)
|
|
26012
|
+
this.admittedUntil.delete(d);
|
|
26013
|
+
if (this.previous && now > this.previous.deadline)
|
|
26014
|
+
this.previous = null;
|
|
26015
|
+
}
|
|
26016
|
+
/**
|
|
26017
|
+
* Retire verified entries below their arrival path's replay floor, keeping a mirrored entry
|
|
26018
|
+
* while the freshness check would still pass its copy on `manifestPort`.
|
|
26019
|
+
* ponytail: linear scan per record; index by (kid, epoch) if retained sets grow past a few thousand.
|
|
26020
|
+
*/
|
|
26021
|
+
prune(now) {
|
|
26022
|
+
const retention = 7e3 + this.maxAuthDelayMs;
|
|
26023
|
+
for (const [id, v] of this.verified) {
|
|
26024
|
+
const h = this.highest[v.path].get(`${v.kid}:${v.epoch}`);
|
|
26025
|
+
const aboveFloor = h === void 0 || v.mseq > h - REPLAY_WINDOW;
|
|
26026
|
+
const fresh = this.cfg.checkFreshness === false ? v.arrival + retention >= now : v.sent * 1e3 + retention >= now;
|
|
26027
|
+
const current = this.current && (v.epoch === this.current.epoch || v.epoch === this.previous?.epoch);
|
|
26028
|
+
if (current && (aboveFloor || v.path === "mirror" && fresh))
|
|
26029
|
+
continue;
|
|
26030
|
+
this.verified.delete(id);
|
|
26031
|
+
}
|
|
26032
|
+
}
|
|
26033
|
+
};
|
|
26034
|
+
|
|
23232
26035
|
// src/catalog-network-source.ts
|
|
23233
26036
|
function normalizePort(value) {
|
|
23234
26037
|
const n = typeof value === "number" ? value : typeof value === "string" ? Number(value) : Number.NaN;
|
|
@@ -23580,11 +26383,12 @@ function resolveCatalogTrackFec(catalog, tracks) {
|
|
|
23580
26383
|
}
|
|
23581
26384
|
|
|
23582
26385
|
// src/multicast-manager.ts
|
|
23583
|
-
function freezeIngress(tracks) {
|
|
26386
|
+
function freezeIngress(tracks, flow) {
|
|
23584
26387
|
return Object.freeze({
|
|
23585
26388
|
tracks: Object.freeze(
|
|
23586
26389
|
[...tracks].map(({ name, packetId }) => Object.freeze({ name, packetId }))
|
|
23587
|
-
)
|
|
26390
|
+
),
|
|
26391
|
+
...flow === void 0 ? {} : { flow }
|
|
23588
26392
|
});
|
|
23589
26393
|
}
|
|
23590
26394
|
function sameIdSet(a, b) {
|
|
@@ -23614,6 +26418,7 @@ function newBlockPolicyStats(packetId, catalogTrackName, epoch) {
|
|
|
23614
26418
|
repairsSkippedRttExceedsBudget: 0,
|
|
23615
26419
|
repairsSkippedPresentationDelayUnavailable: 0,
|
|
23616
26420
|
repairsSkippedRangeNoAnchor: 0,
|
|
26421
|
+
repairsSkippedGroupCannotComplete: 0,
|
|
23617
26422
|
lastRepairSkip: null,
|
|
23618
26423
|
keyframeRepairFetchesSent: 0,
|
|
23619
26424
|
rangeRepairFetchesSent: 0,
|
|
@@ -23698,7 +26503,9 @@ var REPAIR_PRIORITY_REASONS = /* @__PURE__ */ new Set([
|
|
|
23698
26503
|
var REPAIR_SKIP_REASONS = /* @__PURE__ */ new Set([
|
|
23699
26504
|
"rtt-unmeasured",
|
|
23700
26505
|
"rtt-exceeds-budget",
|
|
23701
|
-
"presentation-delay-unavailable"
|
|
26506
|
+
"presentation-delay-unavailable",
|
|
26507
|
+
"range-no-anchor",
|
|
26508
|
+
"group-cannot-complete"
|
|
23702
26509
|
]);
|
|
23703
26510
|
var REPAIR_DEADLINE_BOUNDS = /* @__PURE__ */ new Set(["wait", "presentation", "keyframe", "retention"]);
|
|
23704
26511
|
var GAP_REASONS = {
|
|
@@ -23916,7 +26723,7 @@ function altGroupKey(track) {
|
|
|
23916
26723
|
}
|
|
23917
26724
|
function repairTrackNamesBySource(tracks) {
|
|
23918
26725
|
const bySource = /* @__PURE__ */ new Map();
|
|
23919
|
-
const
|
|
26726
|
+
const add2 = (source, repair) => {
|
|
23920
26727
|
if (!source || !repair) return;
|
|
23921
26728
|
const names = bySource.get(source) ?? /* @__PURE__ */ new Set();
|
|
23922
26729
|
names.add(repair);
|
|
@@ -23924,9 +26731,9 @@ function repairTrackNamesBySource(tracks) {
|
|
|
23924
26731
|
};
|
|
23925
26732
|
for (const track of tracks) {
|
|
23926
26733
|
const repairTrack = typeof track.fec === "object" && track.fec !== null ? track.fec.repairTrack : void 0;
|
|
23927
|
-
if (typeof repairTrack === "string")
|
|
26734
|
+
if (typeof repairTrack === "string") add2(track.name, repairTrack);
|
|
23928
26735
|
if (track.packaging === "fec-repair" && Array.isArray(track.depends) && track.depends.length === 1) {
|
|
23929
|
-
|
|
26736
|
+
add2(track.depends[0], track.name);
|
|
23930
26737
|
}
|
|
23931
26738
|
}
|
|
23932
26739
|
return bySource;
|
|
@@ -23936,6 +26743,10 @@ var MulticastManager = class {
|
|
|
23936
26743
|
#router;
|
|
23937
26744
|
#hostMfuTimeoutMs;
|
|
23938
26745
|
#transport;
|
|
26746
|
+
/** The protected flow of the current join and its manifestPort subscription. */
|
|
26747
|
+
#provenanceJoin = null;
|
|
26748
|
+
#onFailback = null;
|
|
26749
|
+
#failbackHooked = false;
|
|
23939
26750
|
/** Live joinRepairEndpoints() releases → the repair owners they registered,
|
|
23940
26751
|
* re-applied after configureMultiTrack() clears catalog-derived owners. */
|
|
23941
26752
|
#repairReleases = /* @__PURE__ */ new Map();
|
|
@@ -24537,8 +27348,35 @@ var MulticastManager = class {
|
|
|
24537
27348
|
onTakeover(cb) {
|
|
24538
27349
|
this.#transport.onTakeover(cb);
|
|
24539
27350
|
}
|
|
27351
|
+
/** Failback un-protects the join's flow (7.2.7 failover), so the fallback's MoQ packets are read. */
|
|
24540
27352
|
onFailback(cb) {
|
|
24541
|
-
this.#
|
|
27353
|
+
this.#onFailback = cb;
|
|
27354
|
+
this.#hookFailback();
|
|
27355
|
+
}
|
|
27356
|
+
/**
|
|
27357
|
+
* Install the manager's own failback listener on the media transport, which
|
|
27358
|
+
* arms its silence watchdog: a protected join un-protects on failover even
|
|
27359
|
+
* when no player listens, else its tracks' MoQ copies stay dropped.
|
|
27360
|
+
*/
|
|
27361
|
+
#hookFailback() {
|
|
27362
|
+
if (this.#failbackHooked) return;
|
|
27363
|
+
this.#failbackHooked = true;
|
|
27364
|
+
this.#transport.onFailback((reason) => {
|
|
27365
|
+
const joined = this.#provenanceJoin;
|
|
27366
|
+
if (joined !== null) this.#router.unprotectProvenanceFlow(joined.flow);
|
|
27367
|
+
this.#onFailback?.(reason);
|
|
27368
|
+
});
|
|
27369
|
+
}
|
|
27370
|
+
/**
|
|
27371
|
+
* A protected join's handoff (draft -01 7.2.7), before `onTakeover`: its
|
|
27372
|
+
* first admitted datagram. MoQ copies of the flow's tracks the router still
|
|
27373
|
+
* held unadmitted are discarded, and later ones are dropped. Fires again
|
|
27374
|
+
* after a failback once multicast resumes.
|
|
27375
|
+
*/
|
|
27376
|
+
onProtected(cb) {
|
|
27377
|
+
this.#router.onProvenanceFlowProtected(cb && ((flow) => {
|
|
27378
|
+
if (this.#provenanceJoin?.flow === flow) cb();
|
|
27379
|
+
}));
|
|
24542
27380
|
}
|
|
24543
27381
|
/** See MulticastTransport.onDataPlaneLive: the join's first accepted packet. */
|
|
24544
27382
|
onDataPlaneLive(cb) {
|
|
@@ -24554,6 +27392,12 @@ var MulticastManager = class {
|
|
|
24554
27392
|
return MulticastTransport.isAvailable();
|
|
24555
27393
|
}
|
|
24556
27394
|
async join(config) {
|
|
27395
|
+
const provenanceFlow = config.provenance?.flow;
|
|
27396
|
+
if (provenanceFlow !== void 0 && ((config.sourceAddress ?? config.senderAddress) !== provenanceFlow.source || config.groupAddress !== provenanceFlow.groupAddress || config.port !== provenanceFlow.port)) {
|
|
27397
|
+
throw new RangeError(
|
|
27398
|
+
"[MulticastManager] join: provenance flow (S, G, port) must match sourceAddress ?? senderAddress, groupAddress and port"
|
|
27399
|
+
);
|
|
27400
|
+
}
|
|
24557
27401
|
const staged = this.#renditionSwitch;
|
|
24558
27402
|
if (staged !== null) this.#endRenditionSwitch(staged, { reason: "superseded" }, false);
|
|
24559
27403
|
this.configureFec(config);
|
|
@@ -24561,16 +27405,21 @@ var MulticastManager = class {
|
|
|
24561
27405
|
const selectedPacketIds = config.packetIds?.length ? new Set(config.packetIds) : null;
|
|
24562
27406
|
const catalogTracks = config.tracks?.filter(({ packetId }) => selectedPacketIds === null || selectedPacketIds.has(packetId));
|
|
24563
27407
|
const ingressTracks = catalogTracks ? catalogTracks : selectedPacketIds === null ? void 0 : [...selectedPacketIds].map((packetId) => ({ name: "", packetId }));
|
|
24564
|
-
|
|
27408
|
+
this.#releaseProvenanceJoin();
|
|
27409
|
+
const provenance = config.provenance;
|
|
27410
|
+
const protectedTracks = ingressTracks ?? provenance?.flow.tracks;
|
|
27411
|
+
const ingress = protectedTracks === void 0 ? void 0 : {
|
|
24565
27412
|
tracks: Object.freeze(
|
|
24566
|
-
|
|
27413
|
+
protectedTracks.map(
|
|
24567
27414
|
({ name, packetId }) => Object.freeze({ name, packetId })
|
|
24568
27415
|
)
|
|
24569
|
-
)
|
|
27416
|
+
),
|
|
27417
|
+
...provenance === void 0 ? {} : { flow: multicastProvenanceFlowKey(provenance.flow) }
|
|
24570
27418
|
};
|
|
24571
27419
|
const amtRelay = config.amtRelay;
|
|
24572
27420
|
const transportConfig = {
|
|
24573
27421
|
sourceAddress: config.sourceAddress,
|
|
27422
|
+
senderAddress: config.senderAddress,
|
|
24574
27423
|
groupAddress: config.groupAddress,
|
|
24575
27424
|
port: config.port,
|
|
24576
27425
|
mode: config.mode,
|
|
@@ -24597,7 +27446,58 @@ var MulticastManager = class {
|
|
|
24597
27446
|
packetIds: config.packetIds,
|
|
24598
27447
|
tracks: catalogTracks?.map(({ name, packetId }) => ({ name, packetId }))
|
|
24599
27448
|
};
|
|
24600
|
-
|
|
27449
|
+
if (provenance !== void 0) this.#joinProvenance(provenance, ingress, transportConfig);
|
|
27450
|
+
const protectedJoin = this.#provenanceJoin;
|
|
27451
|
+
const joined = await (ingress === void 0 ? this.#transport.join(transportConfig) : this.#transport.join(transportConfig, ingress));
|
|
27452
|
+
if (!joined && protectedJoin !== null && this.#provenanceJoin === protectedJoin) this.#releaseProvenanceJoin();
|
|
27453
|
+
return joined;
|
|
27454
|
+
}
|
|
27455
|
+
/**
|
|
27456
|
+
* Register the protected flow and subscribe its `manifestPort` on the same
|
|
27457
|
+
* `(S, G)` with its own listener (draft -01 7.2.7), so records reach the
|
|
27458
|
+
* flow's receiver and media datagrams are held until one admits them. A
|
|
27459
|
+
* manifest subscription that fails to join admits nothing: the media is
|
|
27460
|
+
* held and expires, never read unauthenticated.
|
|
27461
|
+
*/
|
|
27462
|
+
#joinProvenance(provenance, ingress, media) {
|
|
27463
|
+
const { flow, auth } = provenance;
|
|
27464
|
+
const receiver = new BcProvenanceV2Receiver({
|
|
27465
|
+
...provenance.receiver,
|
|
27466
|
+
endpoint: {
|
|
27467
|
+
sourceAddress: flow.source,
|
|
27468
|
+
groupAddress: flow.groupAddress,
|
|
27469
|
+
port: flow.port,
|
|
27470
|
+
manifestPort: flow.manifestPort,
|
|
27471
|
+
streamId: flow.streamId
|
|
27472
|
+
},
|
|
27473
|
+
keys: auth.keys,
|
|
27474
|
+
minEpoch: auth.minEpoch,
|
|
27475
|
+
bandwidth: flow.bandwidth,
|
|
27476
|
+
maxAuthDelayMs: flow.maxAuthDelayMs,
|
|
27477
|
+
manifestTimerMs: flow.manifestTimerMs
|
|
27478
|
+
});
|
|
27479
|
+
this.#router.registerProvenanceFlow(ingress, receiver);
|
|
27480
|
+
this.#hookFailback();
|
|
27481
|
+
const manifest = new MulticastTransport((record) => {
|
|
27482
|
+
if (this.#provenanceJoin?.manifest === manifest) {
|
|
27483
|
+
this.#router.feedProvenanceRecord(ingress.flow, record, "multicast");
|
|
27484
|
+
}
|
|
27485
|
+
});
|
|
27486
|
+
this.#provenanceJoin = { flow: ingress.flow, manifest };
|
|
27487
|
+
const { packetIds: _packetIds, tracks: _tracks, ...path } = media;
|
|
27488
|
+
void manifest.join({ ...path, port: flow.manifestPort }).then((joined) => {
|
|
27489
|
+
if (!joined && this.#provenanceJoin?.manifest === manifest) {
|
|
27490
|
+
console.warn(`[MulticastManager] manifestPort ${flow.groupAddress}:${flow.manifestPort} not joined`, manifest.lastJoinOutcome);
|
|
27491
|
+
}
|
|
27492
|
+
});
|
|
27493
|
+
}
|
|
27494
|
+
/** Un-protect: drop the flow's hold queues (no expiry counted) and leave its manifestPort. */
|
|
27495
|
+
#releaseProvenanceJoin() {
|
|
27496
|
+
const joined = this.#provenanceJoin;
|
|
27497
|
+
if (joined === null) return;
|
|
27498
|
+
this.#provenanceJoin = null;
|
|
27499
|
+
joined.manifest.dispose();
|
|
27500
|
+
this.#router.releaseProvenanceFlow(joined.flow);
|
|
24601
27501
|
}
|
|
24602
27502
|
/**
|
|
24603
27503
|
* Join the selected rung's dedicated repair groups beside the media join
|
|
@@ -24769,7 +27669,7 @@ var MulticastManager = class {
|
|
|
24769
27669
|
for (const track of [...live.tracks, ...request.tracks]) {
|
|
24770
27670
|
stagedTracks.set(`${track.packetId}\0${track.name}`, track);
|
|
24771
27671
|
}
|
|
24772
|
-
const stagedIngress = freezeIngress(stagedTracks.values());
|
|
27672
|
+
const stagedIngress = freezeIngress(stagedTracks.values(), live.flow);
|
|
24773
27673
|
const stagedPacketIds = committedPacketIds === void 0 || livePacketIds === null ? null : /* @__PURE__ */ new Set([...livePacketIds, ...committedPacketIds]);
|
|
24774
27674
|
const widened = stagedPacketIds !== null && !sameIdSet(stagedPacketIds, livePacketIds);
|
|
24775
27675
|
const active = {
|
|
@@ -24780,7 +27680,7 @@ var MulticastManager = class {
|
|
|
24780
27680
|
liveIngress: live,
|
|
24781
27681
|
livePacketIds,
|
|
24782
27682
|
widened,
|
|
24783
|
-
committedIngress: freezeIngress(request.tracks),
|
|
27683
|
+
committedIngress: freezeIngress(request.tracks, live.flow),
|
|
24784
27684
|
committedPacketIds,
|
|
24785
27685
|
router: null,
|
|
24786
27686
|
state: "staged"
|
|
@@ -24793,7 +27693,7 @@ var MulticastManager = class {
|
|
|
24793
27693
|
let widening;
|
|
24794
27694
|
try {
|
|
24795
27695
|
if (widened) widening = this.#transport.setPacketIds([...stagedPacketIds]);
|
|
24796
|
-
this.#
|
|
27696
|
+
this.#setSwitchIngress(stagedIngress);
|
|
24797
27697
|
} catch (error) {
|
|
24798
27698
|
active.router.cancel();
|
|
24799
27699
|
throw error;
|
|
@@ -24814,6 +27714,11 @@ var MulticastManager = class {
|
|
|
24814
27714
|
}
|
|
24815
27715
|
};
|
|
24816
27716
|
}
|
|
27717
|
+
/** Route a switch's ingress and keep a protected flow's registration in step (7.2.7). */
|
|
27718
|
+
#setSwitchIngress(ingress) {
|
|
27719
|
+
this.#transport.setContext(ingress);
|
|
27720
|
+
if (ingress.flow !== void 0) this.#router.updateProvenanceFlowIngress({ ...ingress, flow: ingress.flow });
|
|
27721
|
+
}
|
|
24817
27722
|
/** The router committed the staged switch (it already retired `from`). */
|
|
24818
27723
|
#commitRenditionSwitch(active, commit) {
|
|
24819
27724
|
if (this.#renditionSwitch !== active) return;
|
|
@@ -24821,7 +27726,7 @@ var MulticastManager = class {
|
|
|
24821
27726
|
active.state = "committed";
|
|
24822
27727
|
active.router = null;
|
|
24823
27728
|
if (this.#transport.joined) {
|
|
24824
|
-
this.#
|
|
27729
|
+
this.#setSwitchIngress(active.committedIngress);
|
|
24825
27730
|
if (active.committedPacketIds !== void 0) {
|
|
24826
27731
|
this.#transport.setPacketIds(active.committedPacketIds).catch((error) => {
|
|
24827
27732
|
console.warn(
|
|
@@ -24857,7 +27762,7 @@ var MulticastManager = class {
|
|
|
24857
27762
|
active.router = null;
|
|
24858
27763
|
if (restore) {
|
|
24859
27764
|
if (this.#transport.joined) {
|
|
24860
|
-
this.#
|
|
27765
|
+
this.#setSwitchIngress(active.liveIngress);
|
|
24861
27766
|
if (active.widened && active.livePacketIds !== null) {
|
|
24862
27767
|
this.#transport.setPacketIds([...active.livePacketIds]).catch((error) => {
|
|
24863
27768
|
console.warn(
|
|
@@ -25243,6 +28148,7 @@ var MulticastManager = class {
|
|
|
25243
28148
|
if (reason === "rtt-unmeasured") stats.repairsSkippedRttUnmeasured++;
|
|
25244
28149
|
else if (reason === "rtt-exceeds-budget") stats.repairsSkippedRttExceedsBudget++;
|
|
25245
28150
|
else if (reason === "range-no-anchor") stats.repairsSkippedRangeNoAnchor++;
|
|
28151
|
+
else if (reason === "group-cannot-complete") stats.repairsSkippedGroupCannotComplete++;
|
|
25246
28152
|
else stats.repairsSkippedPresentationDelayUnavailable++;
|
|
25247
28153
|
stats.lastRepairSkip = {
|
|
25248
28154
|
reason,
|
|
@@ -25438,11 +28344,15 @@ var MulticastManager = class {
|
|
|
25438
28344
|
this.#trackResetObservers.clear();
|
|
25439
28345
|
this.#repairLifecycleObservers.clear();
|
|
25440
28346
|
this.#transport.dispose();
|
|
28347
|
+
this.#releaseProvenanceJoin();
|
|
25441
28348
|
for (const release of [...this.#repairReleases.keys()]) release();
|
|
25442
28349
|
this.#router.dispose();
|
|
25443
28350
|
this.#fec.dispose();
|
|
25444
28351
|
}
|
|
25445
28352
|
};
|
|
28353
|
+
function multicastProvenanceFlowKey(flow) {
|
|
28354
|
+
return JSON.stringify([flow.source, flow.groupAddress, flow.port]);
|
|
28355
|
+
}
|
|
25446
28356
|
|
|
25447
28357
|
// src/fec-worker-setup.ts
|
|
25448
28358
|
async function setupFecWorkerForTrack(opts) {
|
|
@@ -27698,12 +30608,13 @@ function wireRepairFetch(mc, opts) {
|
|
|
27698
30608
|
const admit = (entry, now) => {
|
|
27699
30609
|
const budgetMs = entry.deadlineAt - now;
|
|
27700
30610
|
const rttMs = readRtt();
|
|
27701
|
-
|
|
30611
|
+
const reason = !entry.range && !groupCanComplete(entry.sbn, entry.packetId, entry.catalogTrackName) ? "group-cannot-complete" : rttMs === null ? "rtt-unmeasured" : rttMs < budgetMs ? null : "rtt-exceeds-budget";
|
|
30612
|
+
if (reason === null) return rttMs;
|
|
27702
30613
|
retire(entry);
|
|
27703
30614
|
releaseHold(entry);
|
|
27704
30615
|
clearEntry(entry);
|
|
27705
30616
|
mc.noteRepairSkipped(
|
|
27706
|
-
|
|
30617
|
+
reason,
|
|
27707
30618
|
{ rttMs, budgetMs, priority: entry.priority },
|
|
27708
30619
|
entry.sbn,
|
|
27709
30620
|
entry.packetId,
|
|
@@ -28341,4 +31252,16 @@ export {
|
|
|
28341
31252
|
wireRepairFetch,
|
|
28342
31253
|
wrapAdtsFrame
|
|
28343
31254
|
};
|
|
31255
|
+
/*! Bundled license information:
|
|
31256
|
+
|
|
31257
|
+
@noble/hashes/esm/utils.js:
|
|
31258
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
31259
|
+
|
|
31260
|
+
@noble/curves/esm/utils.js:
|
|
31261
|
+
@noble/curves/esm/abstract/modular.js:
|
|
31262
|
+
@noble/curves/esm/abstract/curve.js:
|
|
31263
|
+
@noble/curves/esm/abstract/edwards.js:
|
|
31264
|
+
@noble/curves/esm/ed25519.js:
|
|
31265
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
31266
|
+
*/
|
|
28344
31267
|
//# sourceMappingURL=browser-runtime.js.map
|