File size: 21,484 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import OAI.Computability.PerfectCompleteness.Machines.CellsMachine
import OAI.Computability.PerfectCompleteness.Machines.CircuitLemmas
import OAI.Computability.PerfectCompleteness.Machines.InitializationControl
import OAI.Computability.PerfectCompleteness.Machines.InputCellsMachineLemmas
import OAI.Computability.PerfectCompleteness.Machines.PaddingCellsMachine
import OAI.Computability.PerfectCompleteness.Machines.PayloadCellsAlignment
import OAI.Computability.PerfectCompleteness.Machines.PayloadRowsLoop
import OAI.Computability.PerfectCompleteness.Machines.ValidityComposition

namespace OAI


noncomputable section
namespace UniqueGamesTheorem.Foundations.Complexity.CookLevin.CellsTokensAlignment


open WitnessEncoding InitializationTemplate PaddingCellsMachine

local instance (V : NPVerifier) : DecidableEq V.computation.tm.Λ := Classical.decEq _
local instance (V : NPVerifier) : DecidableEq V.computation.tm.σ := Classical.decEq _
local instance (V : NPVerifier) : ∀ k, DecidableEq (V.computation.tm.Γ k) :=
  fun _ => Classical.decEq _

theorem cellsInterval_append (V : NPVerifier) (input : List Bool) (S start m n : Nat)
    (within : start + (m + n) ≤ S) :
    cellsInterval V input S start (m + n) within =
      cellsInterval V input S start m (by omega) ++
        cellsInterval V input S (start + m) n (by omega) := by
  simp only [cellsInterval, List.flatMap_def, ← List.ofFn_eq_map]
  rw [List.ofFn_add, List.flatten_append]
  simp only [Fin.val_castLE, Fin.val_natAdd, Nat.add_assoc]

theorem cellsInterval_full (V : NPVerifier) (input : List Bool) (S : Nat) :
    cellsInterval V input S 0 S (by omega) = InitializationControl.cells V input S := by
  simp only [cellsInterval, InitializationControl.cells, Nat.zero_add]

theorem prefix_eq_interval (V : NPVerifier) (input : List Bool) (S : Nat)
    (within : (inputPrefix input).length ≤ S) :
    InputCellsMachine.streamTokens (InputCellsMachine.prefixTruth V) (inputPrefix input) =
      cellsInterval V input S 0 (inputPrefix input).length (by omega) := by
  simpa only [cellsInterval, List.flatMap_def, ← List.ofFn_eq_map, Nat.zero_add] using
    InputCellsMachine.prefixStream_eq_initialCells V input S within

theorem payload_eq_interval (V : NPVerifier) (input : List Bool) (S : Nat)
    (within : (inputPrefix input).length + V.witnessBound.eval input.length ≤ S) :
    PayloadRowsLoop.tokens (PayloadCellsMachine.symbolTable V)
      (V.witnessBound.eval input.length) 0 (V.witnessBound.eval input.length) =
      cellsInterval V input S (inputPrefix input).length (V.witnessBound.eval input.length) within := by
  rw [PayloadRowsLoop.tokens_eq_forTokens, forTokens_eq_ofFn]
  simp only [cellsInterval, List.flatMap_def, ← List.ofFn_eq_map, Nat.zero_add]
  apply congrArg List.flatten
  apply congrArg List.ofFn
  funext i
  exact PayloadCellsMachine.payloadRowTokens_eq V input S i.val i.isLt (by omega)

theorem cells_eq_streams (V : NPVerifier) (input : List Bool) (S : Nat)
    (within : (inputPrefix input).length + V.witnessBound.eval input.length ≤ S) :
    InputCellsMachine.streamTokens (InputCellsMachine.prefixTruth V) (inputPrefix input) ++
      PayloadRowsLoop.tokens (PayloadCellsMachine.symbolTable V)
        (V.witnessBound.eval input.length) 0 (V.witnessBound.eval input.length) ++
      repeatedRows (blankRow V)
        (S - (inputPrefix input).length - V.witnessBound.eval input.length) =
      InitializationControl.cells V input S := by
  let P := (inputPrefix input).length
  let q := V.witnessBound.eval input.length
  let r := S - P - q
  have hpq : P + q ≤ S := within
  have total : (P + q) + r = S := by dsimp only [r]; omega
  have hpre := prefix_eq_interval V input S (by omega)
  have hpay := payload_eq_interval V input S within
  have hpad := cellsInterval_padding V input S (P + q) r (by omega) (by exact le_rfl)
  rw [hpre, hpay, ← hpad]
  have first := cellsInterval_append V input S 0 P q (by omega)
  have second := cellsInterval_append V input S 0 (P + q) r (by omega)
  simp only [Nat.zero_add] at first second
  rw [← first, ← second]
  simpa only [total] using cellsInterval_full V input S

end UniqueGamesTheorem.Foundations.Complexity.CookLevin.CellsTokensAlignment


namespace UniqueGamesTheorem.Foundations.Complexity.CookLevin.InitializationAssemblyBounds

open Polynomial


local instance (V : NPVerifier) : ∀ k, DecidableEq (V.computation.tm.Γ k) :=
  fun _ => Classical.decEq _

def cellsPolynomial (V : NPVerifier) : Polynomial Nat :=
  let A := (VerifierCircuit.indexing V).symbolCount
  let q := V.witnessBound
  let s := Bounds.capacityPolynomial V.computation.time q
    (Runtime.programPushBound V.computation.tm)
  (2 * (s + 2) + (C (A + 4) * (2 * X + 1) + 3)) + (2 * (q + 2) + 1) +
    (q * (C A * (120 * (q + 2)^2 + 1) + 3) + 1) + (s + 1) + (q + 3)

def timePolynomial (V : NPVerifier) : Polynomial Nat :=
  1 + cellsPolynomial V + ValidityMachine.Full.timePolynomial.comp V.witnessBound

theorem capacityPolynomial_eval (V : NPVerifier) (n : Nat) :
    (Bounds.capacityPolynomial V.computation.time V.witnessBound
      (Runtime.programPushBound V.computation.tm)).eval n = VerifierCircuit.capacity V n := by
  simp [VerifierCircuit.capacity, NPVerifier.horizon, Nat.mul_comm]

theorem cellsPolynomial_eval (V : NPVerifier) (input : List Bool) :
    (cellsPolynomial V).eval input.length =
      (2 * (VerifierCircuit.capacity V input.length + 2) +
        (((VerifierCircuit.indexing V).symbolCount + 4) * (WitnessEncoding.inputPrefix input).length + 3)) +
      (2 * (V.witnessBound.eval input.length + 2) + 1) +
      (V.witnessBound.eval input.length * ((VerifierCircuit.indexing V).symbolCount *
        (120 * (V.witnessBound.eval input.length + 2)^2 + 1) + 3) + 1) +
      (VerifierCircuit.capacity V input.length + 1) + (V.witnessBound.eval input.length + 3) := by
  simp only [cellsPolynomial, Polynomial.eval_add, Polynomial.eval_mul,    Polynomial.eval_pow, Polynomial.eval_C, Polynomial.eval_X, Polynomial.eval_ofNat,    Polynomial.eval_one, capacityPolynomial_eval, WitnessEncoding.inputPrefix_length]

theorem cells_steps_le (V : NPVerifier) (input : List Bool) :
    CellsMachine.steps (PayloadCellsMachine.symbolTable V) (WitnessEncoding.inputPrefix input).length
      (V.witnessBound.eval input.length) (VerifierCircuit.capacity V input.length) ≤
      (cellsPolynomial V).eval input.length := by
  rw [cellsPolynomial_eval]
  exact CellsMachine.steps_le (PayloadCellsMachine.symbolTable V)
    (WitnessEncoding.inputPrefix input).length (V.witnessBound.eval input.length)
    (VerifierCircuit.capacity V input.length)

theorem steps_le (V : NPVerifier) (input : List Bool) :
    1 + CellsMachine.steps (PayloadCellsMachine.symbolTable V) (WitnessEncoding.inputPrefix input).length
      (V.witnessBound.eval input.length) (VerifierCircuit.capacity V input.length) +
      ValidityMachine.Full.steps (V.witnessBound.eval input.length) ≤
      (timePolynomial V).eval input.length := by
  simp only [timePolynomial, Polynomial.eval_add, Polynomial.eval_one, Polynomial.eval_comp]
  exact Nat.add_le_add (Nat.add_le_add_left (cells_steps_le V input) 1)
    (ValidityMachine.Full.steps_le_time (V.witnessBound.eval input.length))

end UniqueGamesTheorem.Foundations.Complexity.CookLevin.InitializationAssemblyBounds

end


namespace UniqueGamesTheorem.Foundations.Complexity.CookLevin.InitializationAssembly

open Turing PostfixModel


inductive Tape (cellWork : Nat) where
  | rawInput | framedInput | witnessBound | capacity | reversed | count
  | validityWork (index : Fin 7)
  | cellWork (index : Fin cellWork)
  deriving DecidableEq, Fintype

def validitySlots (n : Nat) : Fin 10 ↪ Tape n :=
  ⟨![.witnessBound, .validityWork 0, .validityWork 1, .validityWork 2,      .validityWork 3, .validityWork 4, .validityWork 5, .reversed, .count,      .validityWork 6], by
    intro a b h
    fin_cases a <;> fin_cases b <;> simp_all⟩

def controlPorts (n : Nat) : InitializationControl.Ports (Tape n) where
  reversed := .reversed
  count := .count
  distinct := by intro h; cases h

theorem validityReady {n : Nat} (base : Tape n → List Bool) (q : Nat)
    (hbound : base .witnessBound = encodeWord q)
    (hwork : ∀ j, base (.validityWork j) = []) :
    ClashMachine.Full.Ready (validitySlots n) base q where
  source := hbound
  empty := by
    intro j hj ho hc
    fin_cases j <;> try contradiction
    · exact hwork 0
    · exact hwork 1
    · exact hwork 2
    · exact hwork 3
    · exact hwork 4
    · exact hwork 5
    · exact hwork 6

theorem control_emitted_eq {n : Nat} (base : Tape n → List Bool) (ts : List Token) :
    InitializationControl.emitted (controlPorts n) base ts =
      ClashMachine.emitted ((validitySlots n) 7) ((validitySlots n) 8) base ts := rfl

theorem validityReady_control {n : Nat} {base : Tape n → List Bool} {q : Nat}
    (ready : ClashMachine.Full.Ready (validitySlots n) base q) (V : NPVerifier) :
    ClashMachine.Full.Ready (validitySlots n)
      (InitializationControl.emitted (controlPorts n) base (InitializationControl.tokens V)) q := by
  rw [control_emitted_eq]
  exact ready.emitted (InitializationControl.tokens V)

theorem validitySlots_not_cellWork (n : Nat) (i : Fin 10) (j : Fin n) :
    validitySlots n i ≠ Tape.cellWork j := by
  rw [validitySlots.eq_1]
  change (![Tape.witnessBound, Tape.validityWork 0, Tape.validityWork 1,    Tape.validityWork 2, Tape.validityWork 3, Tape.validityWork 4, Tape.validityWork 5,    Tape.reversed, Tape.count, Tape.validityWork 6] i : Tape n) ≠ Tape.cellWork j
  fin_cases i <;> simp

theorem controlTrace {n : Nat} {Λ σ : Type}
    (V : NPVerifier) (entry : Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (fun _ : Tape n => Bool) Λ σ)
    (code : program entry = InitializationControl.statement V (controlPorts n) exit)
    (base : Tape n → List Bool) (state : σ) (q : Nat)
    (ready : ClashMachine.Full.Ready (validitySlots n) base q) :
    ((MachineComposition.advance (TM2.step program))^[1]
      (some ⟨some entry, state, base⟩) =
        some ⟨exit, state, InitializationControl.emitted (controlPorts n) base
          (InitializationControl.tokens V)⟩) ∧
      ClashMachine.Full.Ready (validitySlots n)
        (InitializationControl.emitted (controlPorts n) base (InitializationControl.tokens V)) q :=
  ⟨InitializationControl.trace V (controlPorts n) entry exit program code base state,
    validityReady_control ready V⟩

noncomputable section

open MachineComposition
open ClashMachine (State clean emitted)

def cellsSlots : Fin 14 ↪ Tape 9 :=
  ⟨![.framedInput, .witnessBound, .capacity, .reversed, .count,      .cellWork 0, .cellWork 1, .cellWork 2, .cellWork 3, .cellWork 4,      .cellWork 5, .cellWork 6, .cellWork 7, .cellWork 8], by decide⟩

inductive CodeLabel (A : Nat) where
  | control
  | cells (label : CellsMachine.Label A)
  | validity (label : ValidityMachine.Full.Label)
  deriving DecidableEq, Fintype

abbrev Label (V : NPVerifier) := CodeLabel (VerifierCircuit.indexing V).symbolCount

variable {K Λ σ : Type} [DecidableEq K]

def placedControl (slots : Tape 9 ↪ K) : InitializationControl.Ports K where
  reversed := slots .reversed
  count := slots .count
  distinct := slots.injective.ne (by decide)

def cellsLabels {V : NPVerifier} (labels : Label V ↪ Λ) :
    CellsMachine.Label (VerifierCircuit.indexing V).symbolCount ↪ Λ :=
  ⟨fun l => labels (.cells l), fun _ _ h => CodeLabel.cells.inj (labels.injective h)⟩

structure Ready (V : NPVerifier) (slots : Tape 9 ↪ K) (base : K → List Bool)
    (input : List Bool) : Prop where
  rawWord : base (slots .rawInput) = input
  framedWord : base (slots .framedInput) = WitnessEncoding.inputPrefix input
  boundWord : base (slots .witnessBound) = encodeWord (V.witnessBound.eval input.length)
  capacityWord : base (slots .capacity) = encodeWord (VerifierCircuit.capacity V input.length)
  cellEmpty : ∀ j, base (slots (.cellWork j)) = []
  validityEmpty : ∀ j, base (slots (.validityWork j)) = []

theorem Ready.afterEmitted {V : NPVerifier} {slots : Tape 9 ↪ K}
    {base : K → List Bool} {input : List Bool} (ready : Ready V slots base input)
    (ts : List Token) :
    Ready V slots (emitted (slots .reversed) (slots .count) base ts) input := by
  constructor
  · simpa [emitted, slots.injective.eq_iff] using ready.rawWord
  · simpa [emitted, slots.injective.eq_iff] using ready.framedWord
  · simpa [emitted, slots.injective.eq_iff] using ready.boundWord
  · simpa [emitted, slots.injective.eq_iff] using ready.capacityWord
  · intro j
    simpa [emitted, slots.injective.eq_iff] using ready.cellEmpty j
  · intro j
    simpa [emitted, slots.injective.eq_iff] using ready.validityEmpty j

omit [DecidableEq K] in
theorem Ready.cellsReady {V : NPVerifier} {slots : Tape 9 ↪ K}
    {base : K → List Bool} {input : List Bool} (ready : Ready V slots base input) :
    CellsMachine.Ready (cellsSlots.trans slots) base (WitnessEncoding.inputPrefix input)
      (V.witnessBound.eval input.length) (VerifierCircuit.capacity V input.length) := by
  constructor
  · exact ready.framedWord
  · exact ready.boundWord
  · exact ready.capacityWord
  · intro j hj
    fin_cases j <;> simp_all [cellsSlots, ready.cellEmpty]

omit [DecidableEq K] in
theorem Ready.validityReady {V : NPVerifier} {slots : Tape 9 ↪ K}
    {base : K → List Bool} {input : List Bool} (ready : Ready V slots base input) :
    ClashMachine.Full.Ready ((validitySlots 9).trans slots) base
      (V.witnessBound.eval input.length) := by
  have localReady := InitializationAssembly.validityReady (fun t => base (slots t))
    (V.witnessBound.eval input.length) ready.boundWord ready.validityEmpty
  constructor
  · exact localReady.source
  · intro j hj ho hc
    exact localReady.empty j hj ho hc

theorem capacity_within (V : NPVerifier) (input : List Bool) :
    (WitnessEncoding.inputPrefix input).length + V.witnessBound.eval input.length ≤
      VerifierCircuit.capacity V input.length := by
  simp only [WitnessEncoding.inputPrefix_length, VerifierCircuit.capacity]
  omega

def statement (V : NPVerifier) (slots : Tape 9 ↪ K) (labels : Label V ↪ Λ)
    (exit : Option Λ) : Label V → TM2.Stmt (fun _ : K => Bool) Λ (State σ)
  | .control => InitializationControl.statement V (placedControl slots)
      (some (labels (.cells (.prefix .capacityFirst))))
  | .cells l => CellsMachine.statement (cellsSlots.trans slots) (cellsLabels labels)
      (some (labels (.validity (.nonempty .initialize))))
      (InputCellsMachine.prefixTruth V) (PayloadCellsMachine.symbolTable V)
      (PaddingCellsMachine.blankRow V) l
  | .validity l => ValidityMachine.Full.statement ((validitySlots 9).trans slots)
      (fun l => labels (.validity l)) exit l

def Agrees (V : NPVerifier) (slots : Tape 9 ↪ K) (labels : Label V ↪ Λ)
    (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State σ)) : Prop :=
  ∀ l, program (labels l) = statement V slots labels exit l

def steps (V : NPVerifier) (input : List Bool) : Nat :=
  1 + CellsMachine.steps (PayloadCellsMachine.symbolTable V)
    (WitnessEncoding.inputPrefix input).length (V.witnessBound.eval input.length)
    (VerifierCircuit.capacity V input.length) +
    ValidityMachine.Full.steps (V.witnessBound.eval input.length)

theorem cellsTokens_eq (V : NPVerifier) (input : List Bool) :
    CellsMachine.tokens (InputCellsMachine.prefixTruth V) (PayloadCellsMachine.symbolTable V)
      (PaddingCellsMachine.blankRow V) (WitnessEncoding.inputPrefix input)
      (V.witnessBound.eval input.length) (VerifierCircuit.capacity V input.length) =
      InitializationControl.cells V input (VerifierCircuit.capacity V input.length) :=
  CellsTokensAlignment.cells_eq_streams V input (VerifierCircuit.capacity V input.length)
    (capacity_within V input)

theorem trace (V : NPVerifier) (slots : Tape 9 ↪ K) (labels : Label V ↪ Λ)
    (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State σ))
    (ha : Agrees V slots labels exit program) (base : K → List Bool) (input : List Bool)
    (ready : Ready V slots base input) (ambient : σ) :
    (advance (TM2.step program))^[steps V input]
      (some ⟨some (labels .control), clean ambient, base⟩) =
      some ⟨exit, clean ambient, emitted (slots .reversed) (slots .count) base
        (InitializationTemplate.initializationTokens V input)⟩ := by
  let first := InitializationControl.tokens V
  let middle := InitializationControl.cells V input (VerifierCircuit.capacity V input.length)
  let b₁ := emitted (slots .reversed) (slots .count) base first
  let b₂ := emitted (slots .reversed) (slots .count) b₁ middle
  have h₁ := InitializationControl.trace V (placedControl slots) (labels .control)
    (some (labels (.cells (.prefix .capacityFirst)))) program (ha .control) base (clean ambient)
  change (advance (TM2.step program))^[1]
    (some ⟨some (labels .control), clean ambient, base⟩) =
    some ⟨some (labels (.cells (.prefix .capacityFirst))), clean ambient, b₁⟩ at h₁
  have h₂ := CellsMachine.trace (cellsSlots.trans slots) (cellsLabels labels)
    (some (labels (.validity (.nonempty .initialize))))
    (InputCellsMachine.prefixTruth V) (PayloadCellsMachine.symbolTable V)
    (PaddingCellsMachine.blankRow V) program (fun l => ha (.cells l)) b₁
    (WitnessEncoding.inputPrefix input) (V.witnessBound.eval input.length)
    (VerifierCircuit.capacity V input.length) (capacity_within V input)
    (ready.afterEmitted first).cellsReady ambient
  rw [cellsTokens_eq] at h₂
  change (advance (TM2.step program))^[CellsMachine.steps (PayloadCellsMachine.symbolTable V)    (WitnessEncoding.inputPrefix input).length (V.witnessBound.eval input.length)    (VerifierCircuit.capacity V input.length)]
    (some ⟨some (labels (.cells (.prefix .capacityFirst))), clean ambient, b₁⟩) =
    some ⟨some (labels (.validity (.nonempty .initialize))), clean ambient, b₂⟩ at h₂
  have h₃ := ValidityMachine.Full.trace ((validitySlots 9).trans slots)
    (fun l => labels (.validity l)) exit program (fun l => ha (.validity l)) b₂
    (V.witnessBound.eval input.length)
    ((ready.afterEmitted first).afterEmitted middle).validityReady ambient
  have h := ClashMachine.chain (ClashMachine.chain h₁ h₂) h₃
  simpa only [steps, b₂, b₁, first, middle,    show ((validitySlots 9).trans slots) 7 = slots .reversed from rfl,    show ((validitySlots 9).trans slots) 8 = slots .count from rfl,    ClashMachine.emitted_append (slots .reversed) (slots .count)      (slots.injective.ne (by decide : (Tape.reversed : Tape 9) ≠ .count)),    InitializationControl.initializationTokens_eq_blocks] using h

def program (V : NPVerifier) :
    Label V → TM2.Stmt (fun _ : Tape 9 => Bool) (Label V) (State Unit) :=
  statement V (Function.Embedding.refl _) (Function.Embedding.refl _) none

def machine (V : NPVerifier) : FinTM2 where
  K := Tape 9
  k₀ := .rawInput
  k₁ := .reversed
  Γ := fun _ => Bool
  Λ := Label V
  main := .control
  σ := State Unit
  initialState := clean ()
  Γk₀Fin := inferInstance
  m := program V

theorem machineTrace (V : NPVerifier) (base : Tape 9 → List Bool) (input : List Bool)
    (ready : Ready V (Function.Embedding.refl _) base input) :
    (advance (machine V).step)^[steps V input]
      (some ⟨some .control, clean (), base⟩) =
      some ⟨none, clean (), emitted .reversed .count base
        (InitializationTemplate.initializationTokens V input)⟩ :=
  trace V (Function.Embedding.refl _) (Function.Embedding.refl _) none
    (program V) (fun _ => rfl) base input ready ()

def timePolynomial (V : NPVerifier) : Polynomial Nat :=
  InitializationAssemblyBounds.timePolynomial V

theorem steps_le_timePolynomial (V : NPVerifier) (input : List Bool) :
    steps V input ≤ (timePolynomial V).eval input.length :=
  InitializationAssemblyBounds.steps_le V input

def inPolynomialTime (V : NPVerifier) (slots : Tape 9 ↪ K) (labels : Label V ↪ Λ)
    (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State σ))
    (ha : Agrees V slots labels exit program) (base : K → List Bool) (input : List Bool)
    (ready : Ready V slots base input) (ambient : σ) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .control), clean ambient, base⟩
      (some ⟨exit, clean ambient, emitted (slots .reversed) (slots .count) base
        (InitializationTemplate.initializationTokens V input)⟩)
      ((timePolynomial V).eval input.length) where
  steps := steps V input
  evals_in_steps := by
    change (advance (TM2.step program))^[steps V input] _ = _
    exact trace V slots labels exit program ha base input ready ambient
  steps_le_m := steps_le_timePolynomial V input

def machineInPolynomialTime (V : NPVerifier) (base : Tape 9 → List Bool) (input : List Bool)
    (ready : Ready V (Function.Embedding.refl _) base input) :
    StateTransition.EvalsToInTime (machine V).step
      ⟨some .control, clean (), base⟩
      (some ⟨none, clean (), emitted .reversed .count base
        (InitializationTemplate.initializationTokens V input)⟩)
      ((timePolynomial V).eval input.length) :=
  inPolynomialTime V (Function.Embedding.refl _) (Function.Embedding.refl _) none
    (program V) (fun _ => rfl) base input ready ()

end
end UniqueGamesTheorem.Foundations.Complexity.CookLevin.InitializationAssembly

end OAI