Spaces:
Running
Running
File size: 5,825 Bytes
518343a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | /**
* composition_overhead.ts
*
* Runtime instillation of Lean theorem:
* Lutar.Composition.Overhead.composition_overhead_bound
* File: Lutar/Composition/CompositionOverhead.lean
* Commit: c4d13795689601324fce0236351bfe0ade990a43
*
* Lean theorems formalised here:
* - `composition_overhead_bound` (line ~79): totalOverhead(systems) ≤ N * C
* - `totalOverhead_append` (line ~41): overhead is additive over list concat
* - `composition_overhead_strict_bound` (line ~96): strict bound for non-empty pipelines
*
* Runtime contract:
* Given a list of cost-bearing systems and a cap C, verify that the
* sum of their overhead costs does not exceed N * C.
*
* Doctrine v7: No new axioms. No sorries. STAGED label: FULLY WIRED.
*/
import { createHash } from "crypto";
// ---------------------------------------------------------------------------
// Domain types — mirrors Lean `CostSystem` and `BoundedPipeline`
// ---------------------------------------------------------------------------
/** Mirrors Lean `CostSystem`. Every system must have cost ≥ 1. */
export interface CostSystem {
/** Abstract overhead cost (positive integer). */
cost: number;
/** Human-readable system identifier. */
id?: string;
}
/** Mirrors Lean `BoundedPipeline`. */
export interface BoundedPipeline {
systems: CostSystem[];
cap: number;
}
/** DSSE-shaped receipt. */
export interface DSSEReceipt {
theorem: string;
lean_commit_sha: string;
inputs_hash: string;
output: boolean;
ts: string;
sig: string;
}
export type Signer = (payload: string) => string;
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const LEAN_THEOREM = "Lutar.Composition.Overhead.composition_overhead_bound";
const LEAN_FILE_LINE = "Lutar/Composition/CompositionOverhead.lean:79";
const LEAN_COMMIT_SHA = "c4d13795689601324fce0236351bfe0ade990a43";
// ---------------------------------------------------------------------------
// Core functions — mirror Lean definitions
// ---------------------------------------------------------------------------
/**
* Computes the total overhead of a pipeline.
* Mirrors Lean: `totalOverhead (systems : List CostSystem) : OverheadCost`
*
* @param systems - List of cost systems.
* @returns Sum of all system costs.
*/
export function totalOverhead(systems: CostSystem[]): number {
return systems.reduce((acc, s) => acc + s.cost, 0);
}
/**
* Verifies the composition overhead bound:
* totalOverhead(systems) ≤ systems.length * C
*
* Lean theorem: `composition_overhead_bound`
* Lean file: Lutar/Composition/CompositionOverhead.lean:79
* Commit: c4d13795689601324fce0236351bfe0ade990a43
*
* Preconditions:
* - C > 0 (positive cap)
* - every system.cost ≤ C (individual bound)
*
* @param pipeline - The bounded pipeline to verify.
* @returns true iff the bound holds.
*/
export function checkCompositionOverheadBound(pipeline: BoundedPipeline): boolean {
const { systems, cap } = pipeline;
if (cap <= 0) return false;
const allUnderCap = systems.every((s) => s.cost >= 1 && s.cost <= cap);
if (!allUnderCap) return false;
const total = totalOverhead(systems);
return total <= systems.length * cap;
}
/**
* Concatenates two bounded pipelines with the same cap.
* Mirrors Lean `BoundedPipeline.append`.
*
* @param p1 - First pipeline.
* @param p2 - Second pipeline (must share same cap as p1).
* @returns A new BoundedPipeline.
*/
export function appendPipelines(
p1: BoundedPipeline,
p2: BoundedPipeline
): BoundedPipeline {
if (p1.cap !== p2.cap) {
throw new Error(
`appendPipelines: cap mismatch (${p1.cap} vs ${p2.cap}). ` +
"Lean BoundedPipeline.append requires equal caps."
);
}
return { systems: [...p1.systems, ...p2.systems], cap: p1.cap };
}
// ---------------------------------------------------------------------------
// Inputs hash helper
// ---------------------------------------------------------------------------
function hashInputs(pipeline: BoundedPipeline): string {
const payload = JSON.stringify({
systems: pipeline.systems.map((s) => s.cost),
cap: pipeline.cap,
});
return createHash("sha256").update(payload).digest("hex");
}
// ---------------------------------------------------------------------------
// DSSE receipt emitter
// ---------------------------------------------------------------------------
/**
* Evaluates the composition overhead bound and emits a DSSE receipt.
*
* Lean theorem: `Lutar.Composition.Overhead.composition_overhead_bound`
* File: Lutar/Composition/CompositionOverhead.lean:79
* Commit: c4d13795689601324fce0236351bfe0ade990a43
*
* @param pipeline - BoundedPipeline to verify.
* @param signer - Signing function.
* @returns DSSEReceipt with `output = true` iff bound holds.
*/
export function emitCompositionOverheadReceipt(
pipeline: BoundedPipeline,
signer: Signer
): DSSEReceipt {
const output = checkCompositionOverheadBound(pipeline);
const inputs_hash = hashInputs(pipeline);
const ts = new Date().toISOString();
const sigPayload = JSON.stringify({
theorem: LEAN_THEOREM,
lean_commit_sha: LEAN_COMMIT_SHA,
inputs_hash,
output,
ts,
});
return {
theorem: LEAN_THEOREM,
lean_commit_sha: LEAN_COMMIT_SHA,
inputs_hash,
output,
ts,
sig: signer(sigPayload),
};
}
/**
* Gate entry point for Lutar.Composition.Overhead.composition_overhead_bound.
*/
export function compositionOverheadGate(
pipeline: BoundedPipeline,
signer: Signer
): { boundHolds: boolean; receipt: DSSEReceipt } {
const receipt = emitCompositionOverheadReceipt(pipeline, signer);
return { boundHolds: receipt.output, receipt };
}
|