Spaces:
Running
Running
File size: 7,570 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | /**
* composition_overhead.test.ts
*
* Vitest tests for the Lutar.Composition.Overhead.composition_overhead_bound gate.
*
* Tests:
* 1. 1000-input random test — verifies totalOverhead ≤ N*C inequality holds
* 2. Edge cases: empty pipeline, singleton, large N
* 3. Pipeline append tests
* 4. Receipt emission with mock signer
*
* Lean commit: c4d13795689601324fce0236351bfe0ade990a43
*/
import { describe, it, expect } from "vitest";
import {
totalOverhead,
checkCompositionOverheadBound,
appendPipelines,
emitCompositionOverheadReceipt,
compositionOverheadGate,
type CostSystem,
type BoundedPipeline,
type Signer,
} from "../../src/gates/composition_overhead";
// ---------------------------------------------------------------------------
// Seeded LCG
// ---------------------------------------------------------------------------
function seedRandom(seed: number) {
let s = seed >>> 0;
return () => {
s = (Math.imul(1664525, s) + 1013904223) >>> 0;
return s / 0x100000000;
};
}
// ---------------------------------------------------------------------------
// Mock signer
// ---------------------------------------------------------------------------
const mockSigner: Signer = (payload: string) =>
`mock-sig::${Buffer.from(payload).slice(0, 16).toString("hex")}`;
// ---------------------------------------------------------------------------
// 1. 1000-input random overhead bound test
// ---------------------------------------------------------------------------
describe("composition_overhead: 1000-input random bound test", () => {
it("totalOverhead ≤ N * C always holds when all costs ≤ C", () => {
const rand = seedRandom(0xabcdef01);
let violations = 0;
for (let i = 0; i < 1000; i++) {
const cap = Math.ceil(rand() * 100) + 1; // cap in [1,101]
const N = Math.ceil(rand() * 20) + 1; // N in [1,21]
const systems: CostSystem[] = Array.from({ length: N }, () => ({
cost: Math.ceil(rand() * cap), // cost in [1, cap]
}));
const total = totalOverhead(systems);
const bound = N * cap;
if (total > bound) violations++;
}
expect(violations).toBe(0);
});
it("checkCompositionOverheadBound returns true for 1000 random valid pipelines", () => {
const rand = seedRandom(0xbeef1234);
let failures = 0;
for (let i = 0; i < 1000; i++) {
const cap = Math.ceil(rand() * 50) + 1;
const N = Math.ceil(rand() * 15) + 1;
const systems: CostSystem[] = Array.from({ length: N }, () => ({
cost: Math.ceil(rand() * cap),
}));
const pipeline: BoundedPipeline = { systems, cap };
if (!checkCompositionOverheadBound(pipeline)) failures++;
}
expect(failures).toBe(0);
});
});
// ---------------------------------------------------------------------------
// 2. Edge cases
// ---------------------------------------------------------------------------
describe("composition_overhead: edge cases", () => {
it("empty pipeline has totalOverhead = 0", () => {
expect(totalOverhead([])).toBe(0);
});
it("singleton pipeline: overhead = cost", () => {
const s: CostSystem = { cost: 7 };
expect(totalOverhead([s])).toBe(7);
});
it("singleton pipeline bound: 7 ≤ 1 * 10", () => {
const pipeline: BoundedPipeline = { systems: [{ cost: 7 }], cap: 10 };
expect(checkCompositionOverheadBound(pipeline)).toBe(true);
});
it("pipeline where all costs = cap → bound is tight (N*C = N*C)", () => {
const cap = 5;
const N = 4;
const systems: CostSystem[] = Array.from({ length: N }, () => ({ cost: cap }));
expect(totalOverhead(systems)).toBe(N * cap);
expect(checkCompositionOverheadBound({ systems, cap })).toBe(true);
});
it("returns false when a system cost exceeds cap", () => {
const pipeline: BoundedPipeline = {
systems: [{ cost: 5 }, { cost: 15 }],
cap: 10,
};
expect(checkCompositionOverheadBound(pipeline)).toBe(false);
});
it("returns false when cap = 0", () => {
const pipeline: BoundedPipeline = { systems: [{ cost: 1 }], cap: 0 };
expect(checkCompositionOverheadBound(pipeline)).toBe(false);
});
it("large N (1000) all costs = 1, cap = 1 → bound tight", () => {
const N = 1000;
const systems: CostSystem[] = Array.from({ length: N }, () => ({ cost: 1 }));
expect(totalOverhead(systems)).toBe(N);
expect(checkCompositionOverheadBound({ systems, cap: 1 })).toBe(true);
});
});
// ---------------------------------------------------------------------------
// 3. Pipeline append (BoundedPipeline.append)
// ---------------------------------------------------------------------------
describe("composition_overhead: pipeline append", () => {
it("appending two pipelines with same cap is valid", () => {
const p1: BoundedPipeline = { systems: [{ cost: 3 }, { cost: 2 }], cap: 5 };
const p2: BoundedPipeline = { systems: [{ cost: 1 }, { cost: 4 }], cap: 5 };
const merged = appendPipelines(p1, p2);
expect(merged.systems.length).toBe(4);
expect(merged.cap).toBe(5);
expect(checkCompositionOverheadBound(merged)).toBe(true);
});
it("appending throws on cap mismatch", () => {
const p1: BoundedPipeline = { systems: [{ cost: 3 }], cap: 5 };
const p2: BoundedPipeline = { systems: [{ cost: 2 }], cap: 7 };
expect(() => appendPipelines(p1, p2)).toThrow("cap mismatch");
});
it("overhead additivity: append preserves total overhead sum", () => {
const p1: BoundedPipeline = { systems: [{ cost: 3 }, { cost: 2 }], cap: 10 };
const p2: BoundedPipeline = { systems: [{ cost: 1 }, { cost: 4 }], cap: 10 };
const merged = appendPipelines(p1, p2);
expect(totalOverhead(merged.systems)).toBe(
totalOverhead(p1.systems) + totalOverhead(p2.systems)
);
});
});
// ---------------------------------------------------------------------------
// 4. Receipt emission
// ---------------------------------------------------------------------------
describe("composition_overhead: DSSE receipt emission", () => {
it("emits receipt with correct theorem and commit SHA", () => {
const pipeline: BoundedPipeline = {
systems: [{ cost: 3 }, { cost: 2 }],
cap: 5,
};
const receipt = emitCompositionOverheadReceipt(pipeline, mockSigner);
expect(receipt.theorem).toBe(
"Lutar.Composition.Overhead.composition_overhead_bound"
);
expect(receipt.lean_commit_sha).toBe(
"c4d13795689601324fce0236351bfe0ade990a43"
);
expect(receipt.output).toBe(true);
expect(receipt.inputs_hash).toMatch(/^[0-9a-f]{64}$/);
expect(receipt.sig).toContain("mock-sig::");
});
it("receipt output = false when bound violated", () => {
const pipeline: BoundedPipeline = {
systems: [{ cost: 20 }],
cap: 5,
};
const receipt = emitCompositionOverheadReceipt(pipeline, mockSigner);
expect(receipt.output).toBe(false);
});
it("gate returns boundHolds and receipt", () => {
const pipeline: BoundedPipeline = { systems: [{ cost: 4 }], cap: 4 };
const { boundHolds, receipt } = compositionOverheadGate(pipeline, mockSigner);
expect(boundHolds).toBe(true);
expect(receipt.output).toBe(true);
});
it("identical pipelines yield identical inputs_hash", () => {
const p: BoundedPipeline = { systems: [{ cost: 2 }], cap: 5 };
const r1 = emitCompositionOverheadReceipt(p, mockSigner);
const r2 = emitCompositionOverheadReceipt(p, mockSigner);
expect(r1.inputs_hash).toBe(r2.inputs_hash);
});
});
|