Spaces:
Running
Running
File size: 6,913 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | /**
* doctrine_entropy.ts
*
* Runtime instillation of Lean theorem:
* Lutar.Shannon (DoctrineEntropy module)
* File: Lutar/Shannon/DoctrineEntropy.lean
* Commit: c4d13795689601324fce0236351bfe0ade990a43
*
* Lean theorems formalised here:
* - `doctrine_alphabet_size_4` (line ~80): |DoctrineLabel| = 4.
* - `shannon_roundtrip` (line ~97): encoder–decoder round-trip is lossless.
* - `shannon_code_in_2_bits` (line ~103): every codeword fits in 2 bits.
* - `doctrine_average_codeword_length` (line ~116): all labels → length 2.
* - `kraft_inequality_doctrine` (line ~131): Kraft sum = 1 (equality).
* - `doctrine_uniform_code_length_2_bits` (line ~146): ∀ l, length(l) = 2.
* - `channel_rate_bound` (line ~157): rate * 2 ≤ B → rate ≤ B/2.
*
* Runtime contract:
* Encodes/decodes DoctrineLabel values using the Shannon-optimal 2-bit code.
* Verifies code properties and emits DSSE receipts asserting correctness.
*
* Citations (from Lean file):
* - Shannon (1948) DOI 10.1002/j.1538-7305.1948.tb01338.x
* - Cover & Thomas (2006) ISBN 978-0-471-24195-9 (Kraft inequality)
*
* Doctrine v7: No new axioms. No sorries. STAGED label: FULLY WIRED.
*/
import { createHash } from "crypto";
// ---------------------------------------------------------------------------
// Domain types
// ---------------------------------------------------------------------------
/** Doctrine label — 4-level lattice. Mirrors Lean `DoctrineLabel`. */
export type DoctrineLabel = "Bot" | "L1" | "L2" | "Top";
/** All labels in canonical order. */
export const ALL_LABELS: ReadonlyArray<DoctrineLabel> = ["Bot", "L1", "L2", "Top"];
/** 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.Shannon.doctrine_uniform_code_length_2_bits";
const LEAN_FILE_LINE = "Lutar/Shannon/DoctrineEntropy.lean:146";
const LEAN_COMMIT_SHA = "c4d13795689601324fce0236351bfe0ade990a43";
/** Shannon-optimal codeword length for the 4-symbol doctrine alphabet. */
export const DOCTRINE_CODEWORD_LENGTH = 2;
/** Size of the doctrine alphabet. Lean: `doctrine_alphabet_size_4`. */
export const DOCTRINE_ALPHABET_SIZE = 4;
// ---------------------------------------------------------------------------
// Core functions — mirror Lean definitions
// ---------------------------------------------------------------------------
/**
* Shannon encoder for doctrine labels.
* Mirrors Lean `shannonCode`:
* Bot→0, L1→1, L2→2, Top→3
*
* @param label - Doctrine label.
* @returns 2-bit codeword (0–3).
*/
export function shannonCode(label: DoctrineLabel): number {
switch (label) {
case "Bot": return 0;
case "L1": return 1;
case "L2": return 2;
case "Top": return 3;
}
}
/**
* Shannon decoder for doctrine labels.
* Mirrors Lean `shannonDecode`:
* 0→Bot, 1→L1, 2→L2, 3→Top, else→null
*
* @param codeword - 2-bit codeword (0–3).
* @returns DoctrineLabel or null for invalid codewords.
*/
export function shannonDecode(codeword: number): DoctrineLabel | null {
switch (codeword) {
case 0: return "Bot";
case 1: return "L1";
case 2: return "L2";
case 3: return "Top";
default: return null;
}
}
/**
* Verifies the encoder-decoder round-trip for all labels.
* Lean theorem `shannon_roundtrip`: shannonDecode(shannonCode(l)) = some l.
*
* @returns true iff all 4 labels round-trip correctly.
*/
export function verifyRoundtrip(): boolean {
return ALL_LABELS.every((l) => shannonDecode(shannonCode(l)) === l);
}
/**
* Verifies all codewords fit in 2 bits (< 4).
* Lean theorem `shannon_code_in_2_bits`.
*
* @returns true iff all codewords < 4.
*/
export function verifyCodeIn2Bits(): boolean {
return ALL_LABELS.every((l) => shannonCode(l) < DOCTRINE_ALPHABET_SIZE);
}
/**
* Verifies the Kraft inequality at equality.
* Lean theorem `kraft_inequality_doctrine`:
* 4 * 2^(2-2) = 2^2 → 4 * 1 = 4.
*
* @returns true iff Kraft sum = 1 (expressed as 4 * 1 = 4).
*/
export function verifyKraftEquality(): boolean {
const codewordLengths = ALL_LABELS.map(() => DOCTRINE_CODEWORD_LENGTH);
const L = Math.max(...codewordLengths);
const kraftSum = codewordLengths.reduce(
(acc, li) => acc + Math.pow(2, L - li),
0
);
return kraftSum === Math.pow(2, L);
}
/**
* Channel rate bound: rate * 2 ≤ B → rate ≤ floor(B / 2).
* Lean theorem `channel_rate_bound`.
*
* @param B - Bit-rate budget (bits/second).
* @param rate - Claimed receipt rate (receipts/second).
* @returns true iff `rate * 2 ≤ B`.
*/
export function channelRateBound(B: number, rate: number): boolean {
return rate * DOCTRINE_CODEWORD_LENGTH <= B;
}
// ---------------------------------------------------------------------------
// Inputs hash helper
// ---------------------------------------------------------------------------
function hashInputs(): string {
return createHash("sha256")
.update("doctrine_entropy_code_verification")
.digest("hex");
}
// ---------------------------------------------------------------------------
// DSSE receipt emitter
// ---------------------------------------------------------------------------
/**
* Verifies all Shannon code properties and emits a DSSE receipt.
*
* Lean theorem: `Lutar.Shannon.doctrine_uniform_code_length_2_bits`
* File: Lutar/Shannon/DoctrineEntropy.lean:146
* Commit: c4d13795689601324fce0236351bfe0ade990a43
*
* The `output` field is `true` iff all code properties are verified.
*
* @param signer - Signing function.
* @returns DSSEReceipt.
*/
export function emitDoctrineEntropyReceipt(signer: Signer): DSSEReceipt {
const output =
verifyRoundtrip() &&
verifyCodeIn2Bits() &&
verifyKraftEquality();
const inputs_hash = hashInputs();
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.Shannon.DoctrineEntropy.
*/
export function doctrineEntropyGate(signer: Signer): {
alphabetSize: number;
codewordLength: number;
kraftEqualityHolds: boolean;
receipt: DSSEReceipt;
} {
const receipt = emitDoctrineEntropyReceipt(signer);
return {
alphabetSize: DOCTRINE_ALPHABET_SIZE,
codewordLength: DOCTRINE_CODEWORD_LENGTH,
kraftEqualityHolds: verifyKraftEquality(),
receipt,
};
}
|