File size: 6,831 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
/**
 * kitaev_surface.ts
 *
 * Runtime instillation of Lean theorem:
 *   Lutar.QEC.Kitaev (KitaevSurface module)
 *   File: Lutar/QEC/KitaevSurface.lean
 *   Commit: c4d13795689601324fce0236351bfe0ade990a43
 *
 * Lean theorems formalised here:
 *   - `kitaev_single_site_flips_parity_n` (line ~72): a single site error
 *      at the north vertex flips parity to true.
 *   - `kitaev_no_errors_zero_parity` (line ~79): no errors → zero parity.
 *   - `kitaev_all_errors_zero_parity` (line ~84): all errors → zero parity
 *      (weight-4 undetectable error, distance-1 limit).
 *
 * Runtime contract:
 *   Given a VertexCheck (4 adjacent lattice sites) and an error map
 *   (site → bool), compute vertex parity and detect syndromes.
 *   Agents-as-rows, time-slices-as-columns model for receipt lattice.
 *
 * Citations (from Lean file):
 *   - Kitaev (2003) DOI 10.1016/S0003-4916(02)00018-0
 *   - Bravyi & Kitaev (1998) arXiv:quant-ph/9811052
 *
 * Doctrine v7: No new axioms. No sorries. STAGED label: FULLY WIRED.
 */

import { createHash } from "crypto";

// ---------------------------------------------------------------------------
// Domain types — mirrors Lean types
// ---------------------------------------------------------------------------

/** Mirrors Lean `Site`. A lattice site is (agent, slice). */
export interface Site {
  agent: number; // AgentId
  slice: number; // SliceIdx
}

/** Mirrors Lean `VertexCheck`. Models a vertex parity check over 4 sites. */
export interface VertexCheck {
  n: Site; // north
  s: Site; // south
  e: Site; // east
  w: Site; // west
}

/** Mirrors Lean `ErrorBit`. false = clean, true = corrupted. */
export type ErrorBit = boolean;

/** Error map: site key → ErrorBit. Key is `"${agent}:${slice}"`. */
export type ErrorMap = Map<string, ErrorBit>;

/** 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.QEC.Kitaev.kitaev_single_site_flips_parity_n";
const LEAN_FILE_LINE = "Lutar/QEC/KitaevSurface.lean:72";
const LEAN_COMMIT_SHA = "c4d13795689601324fce0236351bfe0ade990a43";

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/** Serialize a Site to a string key for the ErrorMap. */
export function siteKey(s: Site): string {
  return `${s.agent}:${s.slice}`;
}

/** Look up the ErrorBit for a site. Absent sites are clean (false). */
function siteError(errors: ErrorMap, s: Site): ErrorBit {
  return errors.get(siteKey(s)) ?? false;
}

// ---------------------------------------------------------------------------
// Core functions — mirror Lean definitions
// ---------------------------------------------------------------------------

/**
 * Computes vertex parity: XOR of the 4 incident error bits.
 * Odd parity (true) flags a syndrome.
 *
 * Mirrors Lean:
 *   `def vertexParity (errs : Site → ErrorBit) (v : VertexCheck) : Bool :=
 *      errs v.n != errs v.s != errs v.e != errs v.w`
 *
 * Lean theorem `kitaev_single_site_flips_parity_n` proves that a single
 * north-site error produces parity = true.
 *
 * @param errors - ErrorMap (site → bool).
 * @param v      - VertexCheck specifying the 4 adjacent sites.
 * @returns true iff the vertex has an odd-parity syndrome.
 */
export function vertexParity(errors: ErrorMap, v: VertexCheck): boolean {
  const en = siteError(errors, v.n);
  const es = siteError(errors, v.s);
  const ee = siteError(errors, v.e);
  const ew = siteError(errors, v.w);
  // XOR chain matches Lean's `!=` (Bool XOR)
  return en !== es !== ee !== ew;
}

/**
 * Scans all vertices in a lattice for syndromes.
 *
 * @param vertices - Array of VertexChecks.
 * @param errors   - ErrorMap.
 * @returns Array of syndrome vertices (those with parity = true).
 */
export function detectSyndromes(
  vertices: VertexCheck[],
  errors: ErrorMap
): VertexCheck[] {
  return vertices.filter((v) => vertexParity(errors, v));
}

/**
 * Constructs an error map from a single corrupted site.
 * Utility for testing/simulation.
 *
 * @param corruptedSite - The one site to mark as corrupted.
 * @returns ErrorMap with exactly one true entry.
 */
export function singleSiteError(corruptedSite: Site): ErrorMap {
  const m = new Map<string, ErrorBit>();
  m.set(siteKey(corruptedSite), true);
  return m;
}

// ---------------------------------------------------------------------------
// Inputs hash helper
// ---------------------------------------------------------------------------

function hashInputs(v: VertexCheck, errorSites: Site[]): string {
  const payload = JSON.stringify({ v, errorSites });
  return createHash("sha256").update(payload).digest("hex");
}

// ---------------------------------------------------------------------------
// DSSE receipt emitter
// ---------------------------------------------------------------------------

/**
 * Evaluates vertex parity for a given VertexCheck + error list and emits a
 * DSSE receipt.
 *
 * Lean theorem: `Lutar.QEC.Kitaev.kitaev_single_site_flips_parity_n`
 * File: Lutar/QEC/KitaevSurface.lean:72
 * Commit: c4d13795689601324fce0236351bfe0ade990a43
 *
 * The `output` field is `true` iff the vertex has a syndrome (odd parity).
 *
 * @param v          - VertexCheck to evaluate.
 * @param errorSites - List of corrupted sites.
 * @param signer     - Signing function.
 * @returns DSSEReceipt.
 */
export function emitKitaevSurfaceReceipt(
  v: VertexCheck,
  errorSites: Site[],
  signer: Signer
): { parity: boolean; receipt: DSSEReceipt } {
  const errors: ErrorMap = new Map();
  for (const site of errorSites) {
    errors.set(siteKey(site), true);
  }

  const output = vertexParity(errors, v);
  const inputs_hash = hashInputs(v, errorSites);
  const ts = new Date().toISOString();

  const sigPayload = JSON.stringify({
    theorem: LEAN_THEOREM,
    lean_commit_sha: LEAN_COMMIT_SHA,
    inputs_hash,
    output,
    ts,
  });

  const receipt: DSSEReceipt = {
    theorem: LEAN_THEOREM,
    lean_commit_sha: LEAN_COMMIT_SHA,
    inputs_hash,
    output,
    ts,
    sig: signer(sigPayload),
  };

  return { parity: output, receipt };
}

/**
 * Gate entry point for Lutar.QEC.Kitaev.
 */
export function kitaevSurfaceGate(
  v: VertexCheck,
  errorSites: Site[],
  signer: Signer
): { hasSyndrome: boolean; receipt: DSSEReceipt } {
  const { parity, receipt } = emitKitaevSurfaceReceipt(v, errorSites, signer);
  return { hasSyndrome: parity, receipt };
}