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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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import OAI.Computability.WLIdentification.RejectingMates

namespace OAI

namespace WLIdentification

namespace BufferAlgebra

abbrev Vector := Fin 2 → Bit

def Determined {V : Type} (Q : Set V) (f : V → Bit) : Prop :=
  ∀ v ∈ Q, ∀ w ∈ Q, f v = f w

theorem determined_comp {V W : Type} {Q : Set V} {R : Set W} {f : V → Bit}
    {g : W → Bit} (d : Bit)
    (he : f '' Q = (fun w => d + g w) '' R) :
    Determined Q f ↔ Determined R g := by
  constructor
  · intro h v hv w hw
    obtain ⟨v', hv', ev⟩ := he.symm ▸ (show d + g v ∈ (fun w => d + g w) '' R from ⟨v, hv, rfl⟩)
    obtain ⟨w', hw', ew⟩ := he.symm ▸ (show d + g w ∈ (fun w => d + g w) '' R from ⟨w, hw, rfl⟩)
    exact add_left_cancel (ev.symm.trans ((h v' hv' w' hw').trans ew))
  · intro h v hv w hw
    obtain ⟨v', hv', ev⟩ := he ▸ (show f v ∈ f '' Q from ⟨v, hv, rfl⟩)
    obtain ⟨w', hw', ew⟩ := he ▸ (show f w ∈ f '' Q from ⟨w, hw, rfl⟩)
    exact ev.symm.trans ((congrArg (d + ·) (h v' hv' w' hw')).trans ew)

theorem determined_zero {V : Type} (Q : Set V) : Determined Q (fun _ => 0) :=
  fun _ _ _ _ => rfl

theorem determined_sum {J V : Type} [Fintype J] {Q : Set V} {f : J → V → Bit}
    (h : ∀ j, Determined Q (f j)) : Determined Q (fun v => ∑ j, f j v) := by
  intro v hv w hw
  exact Finset.sum_congr rfl (fun j _ => h j v hv w hw)

theorem subsingleton_of_coordinates {J : Type} {Q : Set (J → Bit)}
    (h : ∀ j, Determined Q (fun v => v j)) : Q.Subsingleton := by
  intro v hv w hw
  funext j
  exact h j v hv w hw

theorem determined_of_subsingleton {V : Type} {Q : Set V}
    (h : Q.Subsingleton) (f : V → Bit) : Determined Q f := by
  intro v hv w hw
  rw [h hv hw]

/-- Arbitrarily shifted private buffers propagate singletonness only forwards. -/
theorem forward {V W : Type} {Q : Set V} {R : Set W} {B : Set Vector}
    {f : V → Bit} {g : W → Bit} (d₁ d₂ d₃ : Bit)
    (h₁ : f '' Q = (fun v => d₁ + v 0) '' B)
    (h₂ : f '' Q = (fun v => d₂ + v 1) '' B)
    (h₃ : (fun v => v 0 + v 1) '' B = (fun w => d₃ + g w) '' R)
    (h : Determined Q f) : Determined R g := by
  apply (determined_comp d₃ h₃).mp
  have ha := (determined_comp d₁ h₁).mp h
  have hb := (determined_comp d₂ h₂).mp h
  intro v hv w hw
  exact congrArg₂ (· + ·) (ha v hv w hw) (hb v hv w hw)

/-- Two copies of one source value cancel, even for nonconstant wire shifts. -/
theorem self_value (x z₁ z₂ d₁ d₂ d₃ : Bit)
    (h₁ : x = d₁ + z₁) (h₂ : x = d₂ + z₂)
    (h₃ : z₁ + z₂ = d₃ + x) : x = d₁ + d₂ + d₃ := by
  linear_combination h₁ + h₂ + h₃

/-- Truth fixes a coordinate to zero; false coordinates remain free. -/
def truthVectors {J : Type} (t : J → Prop) : Set (J → Bit) :=
  {v | ∀ j, t j → v j = 0}

theorem truthVectors_nonempty {J : Type} (t : J → Prop) : (truthVectors t).Nonempty :=
  ⟨fun _ => 0, fun _ _ => rfl⟩

theorem projection_truth {J : Type} [DecidableEq J] (t : J → Prop) [DecidablePred t] (j : J) :
    (fun v => v j) '' truthVectors t = if t j then {0} else Set.univ := by
  classical
  by_cases hj : t j
  · rw [ite_eq_left hj]
    ext x
    constructor
    · rintro ⟨v, hv, rfl⟩
      exact hv j hj
    · intro hx
      obtain rfl := Set.mem_singleton_iff.mp hx
      exact ⟨fun _ => 0, fun _ _ => rfl, rfl⟩
  · rw [ite_eq_right hj]
    apply Set.eq_univ_of_forall
    intro x
    refine ⟨Function.update (fun _ => 0) j x, ?_, by simp⟩
    intro i hi
    have hij : i ≠ j := by rintro rfl; exact hj hi
    simp [hij]

theorem sum_truth {J : Type} [Fintype J] (t : J → Prop) [DecidablePred t] :
    (fun v => ∑ j, v j) '' truthVectors t = if ∀ j, t j then {0} else Set.univ := by
  classical
  by_cases ht : ∀ j, t j
  · rw [ite_eq_left ht]
    ext x
    constructor
    · rintro ⟨v, hv, rfl⟩
      simp only [Set.mem_singleton_iff]
      exact Finset.sum_eq_zero (fun j _ => hv j (ht j))
    · intro hx
      obtain rfl := Set.mem_singleton_iff.mp hx
      exact ⟨fun _ => 0, fun _ _ => rfl, by simp⟩
  · rw [ite_eq_right ht]
    obtain ⟨j, hj⟩ := not_forall.mp ht
    apply Set.eq_univ_of_forall
    intro x
    refine ⟨Function.update (fun _ => 0) j x, ?_, ?_⟩
    · intro i hi
      have hij : i ≠ j := by rintro rfl; exact hj hi
      simp [hij]
    · dsimp only
      rw [Finset.sum_eq_single j]
      · simp
      · intro i _ hij; simp [Function.update_of_ne hij]
      · simp

/-- Correlated coordinates keep both input images full while prescribing the sum. -/
def correlated (D : Set Bit) : Set Vector := {v | v 0 + v 1 ∈ D}

theorem correlated_nonempty {D : Set Bit} (hD : D.Nonempty) : (correlated D).Nonempty := by
  obtain ⟨d, hd⟩ := hD
  exact ⟨![0, d], by simpa [correlated] using hd⟩

theorem correlated_projection {D : Set Bit} (hD : D.Nonempty) (j : Fin 2) :
    (fun v => v j) '' correlated D = Set.univ := by
  obtain ⟨d, hd⟩ := hD
  apply Set.eq_univ_of_forall
  intro x
  fin_cases j
  · refine ⟨![x, d + x], ?_, by simp⟩
    simpa [correlated, add_assoc, add_comm, add_left_comm, CharTwo.add_self_eq_zero] using hd
  · refine ⟨![d + x, x], ?_, by simp⟩
    simpa [correlated, add_assoc, CharTwo.add_self_eq_zero] using hd

theorem correlated_sum (D : Set Bit) :
    (fun v => v 0 + v 1) '' correlated D = D := by
  ext x
  constructor
  · rintro ⟨v, hv, rfl⟩; exact hv
  · intro hx
    exact ⟨![0, x], by simpa [correlated] using hx, by simp⟩

end BufferAlgebra

/-- A componentwise injection on a nonempty Boolean product domain.
Values of `mapping` off the domain are immaterial. -/
structure ProductMap (r : ℕ) where
  domain : Fin r → Finset Bool
  nonempty : ∀ i, (domain i).Nonempty
  mapping : Fin r → Bool → Bool
  injective : ∀ i, Set.InjOn (mapping i) {x | x ∈ domain i}

namespace ProductMap
variable {r : ℕ}
abbrev Point (D : ProductMap r) := (i : Fin r) → {b // b ∈ D.domain i}
def source (D : ProductMap r) (a : D.Point) : Fin r → Bool := fun i => (a i).val
def target (D : ProductMap r) (a : D.Point) : Fin r → Bool := fun i => D.mapping i (a i).val

def identity (r : ℕ) : ProductMap r where
  domain _ := Finset.univ
  nonempty _ := ⟨false, Finset.mem_univ _⟩
  mapping _ := id
  injective _ _ _ _ _ h := h

def flip (i : Fin r) : ProductMap r where
  domain _ := Finset.univ
  nonempty _ := ⟨false, Finset.mem_univ _⟩
  mapping j b := if j = i then !b else b
  injective j x _ y _ h := by
    by_cases hj : j = i
    · simpa only [hj, ite_true, Bool.not_not] using congrArg (fun b => !b) h
    · simpa only [hj, ite_false] using h

def identityOn (D : ProductMap r) : ProductMap r where
  domain := D.domain
  nonempty := D.nonempty
  mapping _ := id
  injective _ _ _ _ _ h := h

def ofAddress (a : Fin r → Bool) : (identity r).Point :=
  fun i => ⟨a i, Finset.mem_univ _⟩

theorem source_ofAddress (a : Fin r → Bool) : (identity r).source (ofAddress a) = a := rfl

end ProductMap

structure ProductCircuit (r : ℕ) (P E Z T : Type) where
  pointIndex : P ↪ ℕ
  edgeIndex : E ↪ ℕ
  seedIndex : Z ↪ ℕ
  testIndex : T ↪ ℕ
  dimension : P → ℕ
  dimension_bound : ∀ p, dimension p = 1 ∨ dimension p = 2
  source : E → P
  target : E → P
  port : (e : E) → Fin (dimension (target e))
  edgeMap : E → ProductMap r
  seedType : Z → P
  seed_scalar : ∀ z, dimension (seedType z) = 1
  seedMap : Z → ProductMap r
  testType : T → P
  test_scalar : ∀ t, dimension (testType t) = 1
  testMap : T → ProductMap r

namespace ProductCircuit
variable {r : ℕ} {P E Z T : Type} (C : ProductCircuit r P E Z T)

inductive PortTrue : (p : P) → (Fin r → Bool) → Fin (C.dimension p) → Prop
  | seed (z : Z) (a : (C.seedMap z).Point) (j : Fin (C.dimension (C.seedType z))) :
      PortTrue (C.seedType z) ((C.seedMap z).source a) j
  | edge (e : E) (a : (C.edgeMap e).Point)
      (h : ∀ j, PortTrue (C.source e) ((C.edgeMap e).source a) j) :
      PortTrue (C.target e) ((C.edgeMap e).target a) (C.port e)

def True (p : P) (a : Fin r → Bool) : Prop := ∀ j, C.PortTrue p a j

def Accepted : Prop := ∃ (t : T) (a : (C.testMap t).Point),
  C.True (C.testType t) ((C.testMap t).source a)

theorem accepted_reseed (zindex : Z ↪ ℕ) :
    ({C with seedIndex := zindex} : ProductCircuit r P E Z T).Accepted ↔ C.Accepted := by
  let D : ProductCircuit r P E Z T := {C with seedIndex := zindex}
  have hp (p : P) (a : Fin r → Bool) (j : Fin (C.dimension p)) :
      D.PortTrue p a j ↔ C.PortTrue p a j := by
    constructor
    · intro h
      induction h using @PortTrue.rec r P E Z T D with
      | seed z a j => exact PortTrue.seed (C := C) z a j
      | edge e a h ih => exact PortTrue.edge (C := C) e a ih
    · intro h
      induction h using @PortTrue.rec r P E Z T C with
      | seed z a j => exact PortTrue.seed (C := D) z a j
      | edge e a h ih => exact PortTrue.edge (C := D) e a ih
  constructor
  · rintro ⟨t,a,h⟩
    exact ⟨t,a,fun j => (hp _ _ j).mp (h j)⟩
  · rintro ⟨t,a,h⟩
    exact ⟨t,a,fun j => (hp _ _ j).mpr (h j)⟩

abbrev Primary (_C : ProductCircuit r P E Z T) := Option P

abbrev primaryDimension : C.Primary → ℕ
  | none => 1
  | some p => C.dimension p

def sumForm (d : ℕ) : (Fin d → Bit) →ₗ[Bit] Bit := ∑ j, LinearMap.proj j

theorem sumForm_apply (d : ℕ) (v : Fin d → Bit) : sumForm d v = ∑ j, v j := by
  simp [sumForm]

inductive Connection where
  | original (e : E)
  | test (t : T)
  | flip (i : Fin r)
  | broadcast (p : P) (j : Fin (C.dimension p))
  | self (p : C.Primary) (j : Fin (C.primaryDimension p))
  | conflict
  deriving Fintype

abbrev connectionSource : C.Connection → C.Primary
  | .original e => some (C.source e)
  | .test t => some (C.testType t)
  | .flip _ => none
  | .broadcast _ _ => none
  | .self p _ => p
  | .conflict => none

abbrev connectionTarget : C.Connection → C.Primary
  | .original e => some (C.target e)
  | .test _ => none
  | .flip _ => none
  | .broadcast p _ => some p
  | .self p _ => p
  | .conflict => none

abbrev connectionMap : C.Connection → ProductMap r
  | .original e => C.edgeMap e
  | .test t => (C.testMap t).identityOn
  | .flip i => .flip i
  | .broadcast _ _ => .identity r
  | .self _ _ => .identity r
  | .conflict => .identity r

abbrev sourceForm (l : C.Connection) : (Fin (C.primaryDimension (C.connectionSource l)) → Bit) →ₗ[Bit] Bit :=
  match l with
  | .original e => sumForm (C.dimension (C.source e))
  | .test t => sumForm (C.dimension (C.testType t))
  | .flip _ => LinearMap.proj (0 : Fin 1)
  | .broadcast _ _ => LinearMap.proj (0 : Fin 1)
  | .self _ j => LinearMap.proj j
  | .conflict => LinearMap.proj (0 : Fin 1)

abbrev targetForm (l : C.Connection) : (Fin (C.primaryDimension (C.connectionTarget l)) → Bit) →ₗ[Bit] Bit :=
  match l with
  | .original e => LinearMap.proj (C.port e)
  | .test _ => LinearMap.proj (0 : Fin 1)
  | .flip _ => LinearMap.proj (0 : Fin 1)
  | .broadcast _ j => LinearMap.proj j
  | .self _ j => LinearMap.proj j
  | .conflict => LinearMap.proj (0 : Fin 1)

inductive RealType where
  | primary (p : C.Primary)
  | buffer (l : C.Connection)
  | dummy
  deriving Fintype

inductive Comparison where
  | seed (z : Z)
  | first (l : C.Connection)
  | second (l : C.Connection)
  | third (l : C.Connection)
  deriving Fintype

abbrev realDimension : C.RealType → ℕ
  | .primary p => C.primaryDimension p
  | .buffer _ => 2
  | .dummy => 1

abbrev comparisonSource : C.Comparison → C.RealType
  | .seed z => .primary (some (C.seedType z))
  | .first l => .primary (C.connectionSource l)
  | .second l => .primary (C.connectionSource l)
  | .third l => .buffer l

abbrev comparisonTarget : C.Comparison → C.RealType
  | .seed _ => .dummy
  | .first l => .buffer l
  | .second l => .buffer l
  | .third l => .primary (C.connectionTarget l)

abbrev comparisonMap : C.Comparison → ProductMap r
  | .seed z => (C.seedMap z).identityOn
  | .first l => (C.connectionMap l).identityOn
  | .second l => (C.connectionMap l).identityOn
  | .third l => C.connectionMap l

abbrev comparisonLeft (e : C.Comparison) :
    (Fin (C.realDimension (C.comparisonSource e)) → Bit) →ₗ[Bit] Bit :=
  match e with
  | .seed z => sumForm (C.dimension (C.seedType z))
  | .first l => C.sourceForm l
  | .second l => C.sourceForm l
  | .third _ => (LinearMap.proj (0 : Fin 2) : (Fin 2 → Bit) →ₗ[Bit] Bit) + LinearMap.proj (1 : Fin 2)

abbrev comparisonRight (e : C.Comparison) :
    (Fin (C.realDimension (C.comparisonTarget e)) → Bit) →ₗ[Bit] Bit :=
  match e with
  | .seed _ => 0
  | .first _ => LinearMap.proj (0 : Fin 2)
  | .second _ => LinearMap.proj (1 : Fin 2)
  | .third l => C.targetForm l

/-- Exactly seeds and three comparisons per private buffer, with two distinct
copies of A's full-product identity self-connection. -/
def connectionIndexValue : C.Connection → ℕ
  | .original e => Nat.pair 0 (C.edgeIndex e)
  | .test t => Nat.pair 1 (C.testIndex t)
  | .flip i => Nat.pair 2 i.val
  | .broadcast p j => Nat.pair 3 (Nat.pair (C.pointIndex p) j.val)
  | .self p j => Nat.pair 4 (Nat.pair (NaturalCode.option C.pointIndex p) j.val)
  | .conflict => Nat.pair 5 0

def connectionIndex : C.Connection ↪ ℕ where
  toFun := C.connectionIndexValue
  inj' := by
    intro a b h
    cases a <;> cases b <;>
      simp only [connectionIndexValue, Nat.pair_eq_pair, Nat.reduceEqDiff, false_and,
        true_and] at h
    · exact congrArg Connection.original (C.edgeIndex.injective h)
    · exact congrArg Connection.test (C.testIndex.injective h)
    · exact congrArg Connection.flip (Fin.ext h)
    · obtain ⟨hp,hj⟩ := h
      have he := C.pointIndex.injective hp
      subst_vars
      have he := Fin.ext hj
      subst_vars
      rfl
    · obtain ⟨hp,hj⟩ := h
      have he := (NaturalCode.option C.pointIndex).injective hp
      subst_vars
      have he := Fin.ext hj
      subst_vars
      rfl
    · rfl

def realIndexValue : C.RealType → ℕ
  | .primary p => Nat.pair 0 (NaturalCode.option C.pointIndex p)
  | .buffer l => Nat.pair 1 (C.connectionIndex l)
  | .dummy => Nat.pair 2 0

def realIndex : C.RealType ↪ ℕ where
  toFun := C.realIndexValue
  inj' := by
    intro a b h
    cases a <;> cases b <;>
      simp only [realIndexValue, Nat.pair_eq_pair, Nat.reduceEqDiff, false_and,
        true_and] at h
    · exact congrArg RealType.primary ((NaturalCode.option C.pointIndex).injective h)
    · exact congrArg RealType.buffer (C.connectionIndex.injective h)
    · rfl

def comparisonIndexValue : C.Comparison → ℕ
  | .seed z => Nat.pair 0 (C.seedIndex z)
  | .first l => Nat.pair 1 (C.connectionIndex l)
  | .second l => Nat.pair 2 (C.connectionIndex l)
  | .third l => Nat.pair 3 (C.connectionIndex l)

def comparisonIndex : C.Comparison ↪ ℕ where
  toFun := C.comparisonIndexValue
  inj' := by
    intro a b h
    cases a <;> cases b <;>
      simp only [comparisonIndexValue, Nat.pair_eq_pair, Nat.reduceEqDiff, false_and,
        true_and] at h
    · exact congrArg Comparison.seed (C.seedIndex.injective h)
    · exact congrArg Comparison.first (C.connectionIndex.injective h)
    · exact congrArg Comparison.second (C.connectionIndex.injective h)
    · exact congrArg Comparison.third (C.connectionIndex.injective h)

def schemas : ComparisonSchemas r C.RealType C.Comparison where
  pointIndex := C.realIndex
  edgeIndex := C.comparisonIndex
  dimension := C.realDimension
  dimension_bound p := by
    cases p with
    | primary p => cases p with
      | none => exact Or.inl rfl
      | some p => exact C.dimension_bound p
    | buffer _ => exact Or.inr rfl
    | dummy => exact Or.inl rfl
  source := C.comparisonSource
  target := C.comparisonTarget
  domain e := (C.comparisonMap e).domain
  domain_nonempty e := (C.comparisonMap e).nonempty
  mapping e i a := (C.comparisonMap e).mapping i a.val
  mapping_injective e i := by
    intro a b h
    exact Subtype.ext ((C.comparisonMap e).injective i a.property b.property h)
  left := C.comparisonLeft
  right := C.comparisonRight

end ProductCircuit

end WLIdentification

end OAI