{"unit":"L-pcx-409","title":"Three unit-cost matchings encode NAND failures and count satisfying extensions under zero pins","level":null,"weight":100,"state":"open","file":"lean/PEqualNP/Outside/L_pcx_409.lean","module":"PEqualNP.Outside.L_pcx_409","imports":["PEqualNP.Defs"],"statement_sha256":"69151cdb05844f30ea45435c96160427810a1fb31e54300717e5b23bf8ddca7a","proved":false,"lean":"import Mathlib\nimport PEqualNP.Defs\n\nnamespace PEqualNP.Outside.L_pcx_409\n\nopen Finset\nopen scoped BigOperators\n\n/- This development proves the local NAND identities and the robust matching layout. -/\n\ndef diff (a b : Bool) : ℕ := if a ≠ b then 1 else 0\n\ntheorem diff_tri (a b c : Bool) : diff a c ≤ diff a b + diff b c := by\n  cases a <;> cases b <;> cases c <;> decide\n\ntheorem diff_symm (a b : Bool) : diff a b = diff b a := by\n  cases a <;> cases b <;> rfl\n\ntheorem diff_zero (a b : Bool) : diff a b = 0 ↔ a = b := by\n  cases a <;> cases b <;> decide\n\ntheorem diff_le (a b : Bool) : diff a b ≤ 1 := by\n  cases a <;> cases b <;> decide\n\ntheorem diff_self (a : Bool) : diff a a = 0 := (diff_zero _ _).2 rfl\n\ntheorem diff_xor (a b r : Bool) : diff (xor a r) (xor b r) = diff a b := by\n  cases a <;> cases b <;> cases r <;> rfl\n\ntheorem xor_cancel (a r : Bool) : xor (xor a r) r = a := by\n  cases a <;> cases r <;> rfl\n\ndef triangle (a b c : Bool) : ℕ :=\n  diff a (!b) + diff b (!c) + diff c (!a)\n\ndef gateCost (a b z h : Bool) : ℕ :=\n  triangle a b h + triangle a h false + triangle b h false + diff z h\n\ntheorem gate_exact (a b z : Bool) :\n    gateCost a b z (!(a && b)) = 3 + diff z (!(a && b)) := by\n  cases a <;> cases b <;> cases z <;> decide\n\ntheorem gate_repair (a b z h : Bool) :\n    3 + diff z (!(a && b)) + diff h (!(a && b)) ≤ gateCost a b z h := by\n  cases a <;> cases b <;> cases z <;> cases h <;> decide\n\ntheorem choose_ratio (n t k : ℕ) (ht : 4 * t ≤ 3 * n) (hk : k ≤ t) :\n    4^k * t.choose k ≤ 3^k * n.choose k := by\n  have desc : 4^k * t.descFactorial k ≤ 3^k * n.descFactorial k := by\n    induction k with\n    | zero => simp\n    | succ k ih =>\n      rw [Nat.descFactorial_succ, Nat.descFactorial_succ, pow_succ, pow_succ]\n      calc\n        _ = (4^k * t.descFactorial k) * (4 * (t-k)) := by ring\n        _ ≤ (3^k * n.descFactorial k) * (3 * (n-k)) :=\n          Nat.mul_le_mul (ih (by omega)) (by omega)\n        _ = _ := by ring\n  rw [Nat.descFactorial_eq_factorial_mul_choose, Nat.descFactorial_eq_factorial_mul_choose] at desc\n  exact Nat.le_of_mul_le_mul_left (by simpa only [mul_left_comm] using desc) (Nat.factorial_pos _)\n\ntheorem card_large_subsets {A : Type} [Fintype A] [DecidableEq A]\n    (s t : ℕ) (hst : s ≤ t) (hts : t ≤ 2*s) :\n    (Fintype.card A).choose t ≤ ((Fintype.card A).choose s)^2 := by\n  let U := (univ : Finset A).powersetCard s\n  let V := (univ : Finset A).powersetCard t\n  have h : V.card ≤ (U ×ˢ U).card := by\n    apply card_le_card_of_surjOn (fun p : Finset A × Finset A => p.1 ∪ p.2)\n    intro T hT\n    have hcard := (mem_powersetCard.mp hT).2\n    obtain ⟨S,hST,hS⟩ := exists_subset_card_eq (s := T) (n := s) (by omega)\n    obtain ⟨R,hSR,hRT,hR⟩ := exists_subsuperset_card_eq\n      (s := T \\ S) (t := T) (n := s)\n      (sdiff_subset) (by rw [card_sdiff_of_subset hST]; omega) (by omega)\n    refine ⟨(S,R), mem_product.mpr ⟨mem_powersetCard.mpr ⟨subset_univ _,hS⟩,\n      mem_powersetCard.mpr ⟨subset_univ _,hR⟩⟩, ?_⟩\n    apply Subset.antisymm (union_subset hST hRT)\n    intro x hx\n    by_cases hxs : x ∈ S\n    · exact mem_union_left _ hxs\n    · exact mem_union_right _ (hSR (mem_sdiff.mpr ⟨hx,hxs⟩))\n  simpa [U,V,card_powersetCard,pow_two] using h\n\ndef images {A : Type} [DecidableEq A] (S T : Finset A) [Fintype A] :=\n  (univ : Finset (Equiv.Perm A)).filter (fun p => S.map p.toEmbedding = T)\n\ntheorem images_card {A : Type} [Fintype A] [DecidableEq A]\n    (S T : Finset A) (h : S.card = T.card) :\n    (images S T).card = (images S S).card := by\n  obtain ⟨p,hp⟩ := Equiv.Perm.exists_map_finset_eq T S h.symm\n  refine card_equiv (Equiv.mulLeft p) (fun q => ?_)\n  simp only [images,mem_filter,mem_univ,true_and]\n  have hm : S.map (p * q).toEmbedding = (S.map q.toEmbedding).map p.toEmbedding := by\n    rw [Finset.map_map]\n    rfl\n  change S.map q.toEmbedding = T ↔ S.map (p * q).toEmbedding = S\n  rw [hm]\n  exact ⟨fun e => e ▸ hp, fun e => Finset.map_injective p.toEmbedding (e.trans hp.symm)⟩\n\ndef contained {A : Type} [Fintype A] [DecidableEq A] (S T : Finset A) :=\n  (univ : Finset (Equiv.Perm A)).filter (fun p => S.map p.toEmbedding ⊆ T)\n\ntheorem contained_card {A : Type} [Fintype A] [DecidableEq A]\n    (S T : Finset A) :\n    (contained S T).card = T.card.choose S.card * (images S S).card := by\n  have hh := sum_card_fiberwise_eq_card_filter\n    (univ : Finset (Equiv.Perm A)) (T.powersetCard S.card) (fun p => S.map p.toEmbedding)\n  rw [← show (univ.filter fun p : Equiv.Perm A => S.map p.toEmbedding ∈\n      T.powersetCard S.card) = contained S T by\n    ext p\n    simp [contained,mem_powersetCard]] at *\n  rw [← hh]\n  change (∑ U ∈ T.powersetCard S.card, (images S U).card) = _\n  rw [sum_congr rfl (fun U hU => images_card S U ((mem_powersetCard.mp hU).2.symm))]\n  simp [card_powersetCard]\n\ntheorem perm_card_factor {A : Type} [Fintype A] [DecidableEq A] (S : Finset A) :\n    Fintype.card (Equiv.Perm A) =\n      (Fintype.card A).choose S.card * (images S S).card := by\n  simpa [contained] using contained_card S (univ : Finset A)\n\ntheorem geom_bound (n : ℕ) :\n    (∑ i ∈ range n, (1/2 : ℚ)^(i+1)) = 1 - (1/2 : ℚ)^n := by\n  induction n with\n  | zero => simp\n  | succ n ih =>\n    rw [sum_range_succ, ih, pow_succ (1/2 : ℚ) n]\n    ring\n\ntheorem ratio_bound (n s t : ℕ) (hs : s ≤ t) (ht : t ≤ 2*s)\n    (h : 4*t ≤ 3*n) (hsn : s ≤ n) :\n    (n.choose t : ℚ) * (t.choose s : ℚ) *\n      ((t.choose s : ℚ) / n.choose s)^31 ≤ (1/2 : ℚ)^s := by\n  have B : (0 : ℚ) < n.choose s := by exact_mod_cast Nat.choose_pos hsn\n  have A : (0 : ℚ) ≤ t.choose s := by positivity\n  have R : (t.choose s : ℚ) / n.choose s ≤ (3/4 : ℚ)^s := by\n    rw [div_le_iff₀ B, div_pow, div_mul_eq_mul_div, le_div_iff₀ (by positivity)]\n    exact_mod_cast (by simpa [mul_comm] using choose_ratio n t s h hs)\n  have T : (t.choose s : ℚ) ≤ (4 : ℚ)^s := by\n    exact_mod_cast calc\n      t.choose s ≤ 2^t := Nat.choose_le_two_pow _ _\n      _ ≤ 2^(2*s) := Nat.pow_le_pow_right (by decide) ht\n      _ = 4^s := by rw [pow_mul]; rfl\n  have N : (n.choose t : ℚ) ≤ (n.choose s : ℚ)^2 := by\n    have hN := card_large_subsets (A := Fin n) s t hs ht\n    simp only [Fintype.card_fin] at hN\n    exact_mod_cast hN\n  calc\n    _ ≤ (n.choose s : ℚ)^2 * (t.choose s : ℚ) *\n        ((t.choose s : ℚ) / n.choose s)^31 := by gcongr\n    _ = (t.choose s : ℚ)^3 * ((t.choose s : ℚ) / n.choose s)^29 := by\n      field_simp <;> ring\n    _ ≤ ((4 : ℚ)^s)^3 * (((3/4 : ℚ)^s)^29) := by gcongr\n    _ = ((4 : ℚ)^3 * (3/4 : ℚ)^29)^s := by\n      rw [mul_pow, ← pow_mul, ← pow_mul, ← pow_mul, ← pow_mul, Nat.mul_comm 3, Nat.mul_comm 29]\n    _ ≤ (1/2 : ℚ)^s := by gcongr <;> norm_num\n\ntheorem bad_size {A : Type} [Fintype A] [DecidableEq A] (s : ℕ)\n    (hs : 0 < s) (hN : 2*s ≤ Fintype.card A) :\n    let t := s + s/2\n    let bad := fun S T : Finset A =>\n      Fintype.piFinset (fun _ : Fin 32 => contained S T)\n    let B := ((univ : Finset A).powersetCard s).biUnion\n      (fun S => ((univ : Finset A).powersetCard t).biUnion (bad S))\n    (B.card : ℚ) / Fintype.card (Fin 32 → Equiv.Perm A) ≤ (1/2 : ℚ)^s := by\n  intro t bad B\n  let n := Fintype.card A\n  let M := Fintype.card (Fin 32 → Equiv.Perm A)\n  have Mpos : (0 : ℚ) < M := by\n    haveI : Nonempty (Fin 32 → Equiv.Perm A) := ⟨fun _ => Equiv.refl _⟩\n    exact_mod_cast Fintype.card_pos (α := Fin 32 → Equiv.Perm A)\n  have na : (0 : ℚ) < n.choose s := by exact_mod_cast Nat.choose_pos (by omega : s ≤ n)\n  have hcard (S T : Finset A) (hS : S.card = s) (hT : T.card = t) :\n      ((bad S T).card : ℚ) / M = ((t.choose s : ℚ) / n.choose s)^32 := by\n    have hc := contained_card S T\n    rw [hS,hT] at hc\n    have hp := perm_card_factor S\n    rw [hS] at hp\n    have hP : 0 < (Fintype.card (Equiv.Perm A) : ℚ) := by\n      haveI : Nonempty (Equiv.Perm A) := ⟨Equiv.refl _⟩\n      exact_mod_cast Fintype.card_pos (α := Equiv.Perm A)\n    have hF : 0 < ((images S S).card : ℚ) := by\n      have hh : 0 < Fintype.card (Equiv.Perm A) := by exact_mod_cast hP\n      rw [hp] at hh\n      exact_mod_cast Nat.pos_of_mul_pos_left hh\n    simp only [bad,Fintype.card_piFinset,prod_const,card_univ,Fintype.card_fin,hc,\n      M,Fintype.card_fun, Nat.cast_pow, Nat.cast_mul, ← div_pow]\n    congr 1\n    have hpq : (Fintype.card (Equiv.Perm A) : ℚ) =\n        (n.choose s : ℚ) * (images S S).card := by exact_mod_cast hp\n    rw [hpq]\n    field_simp\n  have unionBound {ι : Type} (U : Finset ι) (V : ι → Finset (Fin 32 → Equiv.Perm A)) :\n      ((U.biUnion V).card : ℚ) / M ≤ ∑ i ∈ U, ((V i).card : ℚ) / M := by\n    rw [← sum_div]\n    apply div_le_div_of_nonneg_right _ Mpos.le\n    exact_mod_cast card_biUnion_le\n  calc\n    _ ≤ ∑ S ∈ (univ : Finset A).powersetCard s,\n        ∑ T ∈ (univ : Finset A).powersetCard t, ((bad S T).card : ℚ) / M :=\n      (unionBound _ _).trans (sum_le_sum (fun _ _ => unionBound _ _))\n    _ = (n.choose s : ℚ) * (n.choose t : ℚ) * ((t.choose s : ℚ) / n.choose s)^32 := by\n      rw [sum_congr rfl (fun S hS => sum_congr rfl (fun T hT =>\n        hcard S T (mem_powersetCard.mp hS).2 (mem_powersetCard.mp hT).2))]\n      simp [card_powersetCard,n,mul_assoc]\n    _ ≤ _ := by\n      rw [mul_comm (n.choose s : ℚ) (n.choose t : ℚ)]\n      have ht : 4 * t ≤ 3 * n := by dsimp [t]; omega\n      let w : ℚ := (t.choose s : ℚ) / n.choose s\n      have he : (n.choose s : ℚ) * w = t.choose s := by\n        dsimp [w]\n        field_simp\n      calc\n        _ = (n.choose t : ℚ) * ((n.choose s : ℚ) * w) * w^31 := by\n          change (n.choose t : ℚ) * n.choose s * w^32 = _\n          rw [show 32 = 31+1 by decide,pow_succ]\n          ring\n        _ ≤ _ := by\n          rw [he]\n          exact ratio_bound n s t (by dsimp [t]; omega) (by dsimp [t]; omega) ht (by omega)\n\ntheorem expand_sets {A : Type} [Fintype A] [DecidableEq A] :\n    ∃ p : Fin 32 → Equiv.Perm A, ∀ S : Finset A, 2*S.card ≤ Fintype.card A →\n      S.card ≤ 2 * ∑ j : Fin 32, (S.filter (fun i => p j i ∉ S)).card := by\n  classical\n  let n := Fintype.card A\n  let M := Fintype.card (Fin 32 → Equiv.Perm A)\n  let B := fun i : ℕ =>\n    ((univ : Finset A).powersetCard (i+1)).biUnion (fun S =>\n      ((univ : Finset A).powersetCard (i+1+(i+1)/2)).biUnion (fun T =>\n        Fintype.piFinset (fun _ : Fin 32 => contained S T)))\n  let F := (range (n/2)).biUnion B\n  have Mpos : (0 : ℚ) < M := by\n    haveI : Nonempty (Fin 32 → Equiv.Perm A) := ⟨fun _ => Equiv.refl _⟩\n    exact_mod_cast Fintype.card_pos (α := Fin 32 → Equiv.Perm A)\n  have hF : (F.card : ℚ) / M < 1 := by\n    calc\n      _ ≤ ∑ i ∈ range (n/2), ((B i).card : ℚ) / M := by\n        rw [← sum_div]\n        exact div_le_div_of_nonneg_right (by exact_mod_cast card_biUnion_le) Mpos.le\n      _ ≤ ∑ i ∈ range (n/2), (1/2 : ℚ)^(i+1) := by\n        refine sum_le_sum fun i hi => ?_\n        have hi' := mem_range.mp hi\n        exact bad_size (A := A) (i+1) (by omega) (by omega)\n      _ < _ := by rw [geom_bound]; exact sub_lt_self _ (by positivity)\n  have hn : F ≠ univ := by\n    intro h\n    rw [h,card_univ,show Fintype.card (Fin 32 → Equiv.Perm A) = M from rfl,\n      div_self Mpos.ne'] at hF\n    exact (lt_irrefl 1 hF)\n  obtain ⟨p,hp⟩ : ∃ p, p ∉ F := by\n    by_contra! h\n    exact hn (eq_univ_iff_forall.mpr h)\n  refine ⟨p,fun S hS => ?_⟩\n  by_contra! hL\n  let s := S.card\n  let t := s+s/2\n  have hs : 0 < s := by dsimp [s]; omega\n  let D := fun j : Fin 32 => (S.filter (fun i => p j i ∉ S)).map (p j).toEmbedding\n  let U := S ∪ (univ : Finset (Fin 32)).biUnion D\n  have hu : U.card ≤ t := by\n    have hh := card_biUnion_le (s := (univ : Finset (Fin 32))) (t := D)\n    have hh' : ((univ : Finset (Fin 32)).biUnion D).card ≤ _ := hh\n    simp only [D,card_map] at hh'\n    dsimp [U,D,t,s]\n    exact (card_union_le _ _).trans (by omega)\n  obtain ⟨T,hUT,hT⟩ := exists_superset_card_eq hu (by dsimp [t,s]; omega)\n  apply hp\n  apply mem_biUnion.mpr\n  refine ⟨s-1,mem_range.mpr (by omega),?_⟩\n  have eq : s-1+1 = s := by omega\n  simp only [B,eq]\n  refine mem_biUnion.mpr ⟨S,mem_powersetCard.mpr ⟨subset_univ _,rfl⟩,?_⟩\n  refine mem_biUnion.mpr ⟨T,mem_powersetCard.mpr ⟨subset_univ _,hT⟩,?_⟩\n  refine Fintype.mem_piFinset.mpr fun j => mem_filter.mpr ⟨mem_univ _,?_⟩\n  intro x hx\n  obtain ⟨i,hi,rfl⟩ := mem_map.mp hx\n  apply hUT\n  by_cases h : p j i ∈ S\n  · exact mem_union_left _ h\n  · exact mem_union_right _ (mem_biUnion.mpr ⟨j,mem_univ _,mem_map.mpr\n      ⟨i,mem_filter.mpr ⟨hi,h⟩,rfl⟩⟩)\n\ntheorem expand_bool {A : Type} [Fintype A] [DecidableEq A] :\n    ∃ p : Fin 32 → Equiv.Perm A, ∀ b : A → Bool, ∃ c : Bool,\n      (∑ i, diff (b i) c) ≤ 2 * ∑ j : Fin 32, ∑ i, diff (b i) (b (p j i)) := by\n  obtain ⟨p,hp⟩ := expand_sets (A := A)\n  refine ⟨p,fun b => ?_⟩\n  let S := fun c : Bool => (univ : Finset A).filter (fun i => b i ≠ c)\n  have ha : (S false).card + (S true).card = Fintype.card A := by\n    have h : (univ : Finset A).filter (fun i => ¬ b i ≠ false) = S true := by\n      ext i\n      cases hb : b i <;> simp [S,hb]\n    have he := card_filter_add_card_filter_not (s := (univ : Finset A))\n      (fun i : A => b i ≠ false)\n    rw [h,card_univ] at he\n    exact he\n  have hb (c : Bool) (hc : 2*(S c).card ≤ Fintype.card A) :\n      (∑ i, diff (b i) c) ≤ 2 * ∑ j : Fin 32, ∑ i, diff (b i) (b (p j i)) := by\n    have hh := hp (S c) hc\n    rw [show (∑ i, diff (b i) c) = (S c).card by simp [S,card_filter,diff]]\n    refine hh.trans (Nat.mul_le_mul_left _ (sum_le_sum fun j _ => ?_))\n    rw [filter_filter,card_filter]\n    apply sum_le_sum\n    intro i _\n    by_cases he : b i ≠ c ∧ p j i ∉ S c\n    · have he' : b (p j i) = c := by simpa [S] using he.2\n      simp [he,diff,he',he.1]\n    · simp [he]\n  by_cases h : 2*(S false).card ≤ Fintype.card A\n  · exact ⟨false,hb false h⟩\n  · exact ⟨true,hb true (by omega)⟩\n\ntheorem expand_fibers {A W : Type} [Fintype A] [Fintype W] (f : A → W) :\n    ∃ p : Fin 32 → Equiv.Perm A, (∀ j i, f (p j i) = f i) ∧\n      ∀ a : A → Bool, ∃ c : W → Bool,\n        (∑ i, diff (a i) (c (f i))) ≤\n          2 * ∑ j : Fin 32, ∑ i, diff (a i) (a (p j i)) := by\n  classical\n  choose p hp using fun w : W => expand_bool (A := {i // f i = w})\n  let E := Equiv.sigmaFiberEquiv f\n  let P : Fin 32 → Equiv.Perm A := fun j =>\n    E.symm.trans ((Equiv.sigmaCongrRight (fun w => p w j)).trans E)\n  have hP (j : Fin 32) (w : W) (i : {i // f i = w}) :\n      P j i = (p w j i : A) := by\n    change P j (E ⟨w,i⟩) = _\n    simp only [P,Equiv.trans_apply,Equiv.symm_apply_apply,Equiv.sigmaCongrRight_apply]\n    rfl\n  refine ⟨P,fun j i => ?_,fun a => ?_⟩\n  · rw [hP j (f i) ⟨i,rfl⟩]\n    exact (p _ _ _).2\n  · choose c hc using fun w : W => hp w (fun i => a i)\n    refine ⟨c,?_⟩\n    have sums (g : A → ℕ) : (∑ i, g i) = ∑ w : W, ∑ i : {i // f i = w}, g i := by\n      calc\n        _ = ∑ i, g (E i) := (Equiv.sum_comp E g).symm\n        _ = _ := Fintype.sum_sigma _\n    rw [sums (fun i => diff (a i) (c (f i)))]\n    simp_rw [sums (fun i => diff (a i) (a (P _ i)))]\n    simp_rw [hP]\n    rw [sum_comm, mul_sum]\n    apply sum_le_sum\n    intro w _\n    simpa only [show ∀ i : {i // f i = w}, f i = w from fun i => i.2] using hc w\n\nnoncomputable def cycle {A : Type} [Fintype A] : Equiv.Perm A :=\n  (Fintype.equivFin A).trans ((finRotate _).trans (Fintype.equivFin A).symm)\n\ntheorem cycle_const {A : Type} [Fintype A] (f : A → Bool)\n    (h : ∀ i, f (cycle i) = f i) (i j : A) : f i = f j := by\n  let e := Fintype.equivFin A\n  let g := f ∘ e.symm\n  have hg (k) : g (finRotate _ k) = g k := by simpa [g,e,cycle] using h (e.symm k)\n  have hi : f i = g (e i) := by simp [g]\n  have hj : f j = g (e j) := by simp [g]\n  rw [hi,hj]\n  have finConst (n : ℕ) (g : Fin n → Bool) (hg : ∀ k, g (finRotate n k) = g k)\n      (i j : Fin n) : g i = g j := by\n    cases n with\n    | zero => exact i.elim0\n    | succ n =>\n      have eq (k : Fin (n+1)) : g k = g 0 := by\n        induction k using Fin.induction with\n        | zero => rfl\n        | succ k ih => rw [← ih, ← hg k.castSucc, finRotate_apply, Fin.coeSucc_eq_succ]\n      rw [eq i,eq j]\n  exact finConst _ g hg _ _\n\nnoncomputable def blockCost {A : Type} [Fintype A] (u : Bool → A → Bool) : ℕ :=\n  (∑ i, diff (u false i) (u true i)) +\n    ∑ i, diff (u false i) (u true (cycle i))\n\nnoncomputable def dirty {A : Type} (u : Bool → A → Bool) (i : A) : ℕ := by\n  classical\n  exact if ∀ b j, u b j = u false i then 0 else 1\n\ntheorem dirty_le {A : Type} (u : Bool → A → Bool) (i : A)\n    (b : Bool) (j : A) : diff (u b j) (u false i) ≤ dirty u i := by\n  classical\n  unfold dirty\n  split_ifs with h\n  · simp [h b j,diff_self]\n  · exact diff_le _ _\n\ntheorem dirty_zero {A : Type} (u : Bool → A → Bool) (i : A)\n    (h : dirty u i = 0) (b : Bool) (j : A) : u b j = u false i :=\n  (diff_zero _ _).mp (Nat.eq_zero_of_le_zero ((dirty_le u i b j).trans (by omega)))\n\ntheorem dirty_cases {A : Type} (u : Bool → A → Bool) (i : A) :\n    dirty u i = 0 ∨ dirty u i = 1 := by\n  classical\n  unfold dirty\n  split_ifs <;> simp\n\ntheorem block_bound {A : Type} [Fintype A] (u : Bool → A → Bool) (i : A) :\n    2 * dirty u i ≤ blockCost u := by\n  classical\n  have parity (r : Equiv.Perm A) :\n      (∑ j, diff (u false j) (u true (r j))) +\n        2 * (∑ j, (u false j && u true (r j)).toNat) =\n      (∑ j, (u false j).toNat) + ∑ j, (u true j).toNat := by\n    rw [mul_sum,← sum_add_distrib,← Equiv.sum_comp r (fun j => (u true j).toNat),← sum_add_distrib]\n    refine sum_congr rfl fun j _ => ?_\n    generalize u false j = a\n    generalize u true (r j) = b\n    cases a <;> cases b <;> decide\n  have hzero (h : blockCost u = 0) : dirty u i = 0 := by\n    have h1 (j : A) : u false j = u true j := by\n      apply (diff_zero _ _).mp\n      have hh := single_le_sum (f := fun j => diff (u false j) (u true j))\n        (fun _ _ => Nat.zero_le _) (mem_univ j)\n      unfold blockCost at h\n      omega\n    have h2 (j : A) : u false j = u true (cycle j) := by\n      apply (diff_zero _ _).mp\n      have hh := single_le_sum (f := fun j => diff (u false j) (u true (cycle j)))\n        (fun _ _ => Nat.zero_le _) (mem_univ j)\n      unfold blockCost at h\n      omega\n    have h3 := cycle_const (u true) (fun j => (h2 j).symm.trans (h1 j))\n    have h4 (b : Bool) (j : A) : u b j = u false i := by\n      cases b\n      · exact (h1 j).trans ((h3 j i).trans (h1 i).symm)\n      · exact (h3 j i).trans (h1 i).symm\n    simp [dirty,h4]\n  have p1 := parity (Equiv.refl A)\n  have p2 := parity (cycle (A := A))\n  simp only [Equiv.refl_apply] at p1\n  rcases dirty_cases u i with h | h\n  · simp [h]\n  · have hp : blockCost u ≠ 0 := fun e => by have := hzero e; omega\n    unfold blockCost at *\n    omega\n\nabbrev TBlock (Q : Type) := (Q × Bool) × Bool\nabbrev KBlock (Q : Type) := (Q × Bool) × Fin 1024\nabbrev CBlock (Q : Type) := KBlock Q × Bool\nabbrev TPos := Option (Fin 1024)\nabbrev CPos := Option (Option (Fin 32))\nabbrev Sites (Q : Type) := (TBlock Q × TPos) ⊕ (CBlock Q × CPos)\n\nabbrev Assignment (Q : Type) := Bool × Sites Q → Bool\nabbrev Network (Q : Type) := Fin 32 → Equiv.Perm (KBlock Q)\n\nsection Layout\nvariable {Q W : Type} [Fintype Q]\n\ndef term (u : Assignment Q) (t : TBlock Q) (b : Bool) (j : TPos) : Bool :=\n  u (b,Sum.inl (t,j))\ndef core (u : Assignment Q) (c : CBlock Q) (b : Bool) (j : CPos) : Bool :=\n  u (b,Sum.inr (c,j))\ndef keyT (t : TBlock Q) : Q := t.1.1\ndef keyK (k : KBlock Q) : Q := k.1.1\ndef atCore (t : TBlock Q) (j : Fin 1024) : CBlock Q := ((t.1,j),!t.2)\n\ndef linkCoords (Q : Type) : TBlock Q × Fin 1024 ≃ CBlock Q where\n  toFun p := atCore p.1 p.2\n  invFun p := ((p.1.1,!p.2),p.1.2)\n  left_inv := by rintro ⟨⟨q,s⟩,j⟩; cases s <;> rfl\n  right_inv := by rintro ⟨⟨q,j⟩,s⟩; cases s <;> rfl\n\ndef roleCoords (Q : Type) : (Q × Bool) × Bool ≃ TBlock Q where\n  toFun p := ((p.1.1,p.2),p.1.2)\n  invFun p := ((p.1.1,p.2),p.1.2)\n  left_inv := by rintro ⟨⟨q,r⟩,s⟩; rfl\n  right_inv := by rintro ⟨⟨q,r⟩,s⟩; rfl\n\ndef sign : Sites Q → Bool\n  | .inl (t,_) => t.2\n  | .inr (c,_) => c.2\n\ndef owner : Sites Q → Q\n  | .inl (t,_) => keyT t\n  | .inr (c,_) => keyK c.1\n\ndef canonical (f : Q → W) (w : W → Bool) (p : Bool × Sites Q) : Bool :=\n  xor (w (f (owner p.2))) (sign p.2)\n\ndef oriented (y : Assignment Q) (p : Bool × Sites Q) : Bool :=\n  xor (y p) (sign p.2)\n\nnoncomputable def blue : Equiv.Perm (Sites Q) :=\n  (Equiv.prodCongr (Equiv.refl _) cycle).sumCongr (Equiv.prodCongr (Equiv.refl _) cycle)\n\ndef roleT (x : Q × Bool) (side : Bool) : TBlock Q := ((x.1,side),x.2)\ndef otherSign (x : Q × Bool) : Q × Bool := (x.1,!x.2)\n\ndef greenFun (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) : Sites Q → Sites Q\n  | .inl (t, none) =>\n    if t.1.2 then .inl (((t.1.1,false),!t.2),none)\n    else .inl (roleT (otherSign (E (t.1.1,t.2))) true,none)\n  | .inl (t,some j) => .inr (atCore t j,none)\n  | .inr (c,none) => .inl (((c.1.1,!c.2),some c.1.2))\n  | .inr (c,some none) => .inr ((c.1,!c.2),some none)\n  | .inr (c,some (some j)) => .inr ((p j c.1,!c.2),some (some j))\n\ndef greenInv (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) : Sites Q → Sites Q\n  | .inl (t, none) =>\n    if t.1.2 then .inl (roleT (E.symm (t.1.1,!t.2)) false,none)\n    else .inl (((t.1.1,true),!t.2),none)\n  | .inl (t,some j) => .inr (atCore t j,none)\n  | .inr (c,none) => .inl (((c.1.1,!c.2),some c.1.2))\n  | .inr (c,some none) => .inr ((c.1,!c.2),some none)\n  | .inr (c,some (some j)) => .inr (((p j).symm c.1,!c.2),some (some j))\n\ndef green (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) : Equiv.Perm (Sites Q) where\n  toFun := greenFun E p\n  invFun := greenInv E p\n  left_inv := by\n    rintro (⟨⟨⟨q,r⟩,s⟩,(_|j)⟩ | ⟨⟨⟨⟨q,r⟩,k⟩,s⟩,(_|_|j)⟩)\n    · cases r <;> simp [greenFun,greenInv,roleT,otherSign]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv]\n    · simp [greenFun,greenInv]\n  right_inv := by\n    rintro (⟨⟨⟨q,r⟩,s⟩,(_|j)⟩ | ⟨⟨⟨⟨q,r⟩,k⟩,s⟩,(_|_|j)⟩)\n    · cases r <;> simp [greenFun,greenInv,roleT,otherSign]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv]\n    · simp [greenFun,greenInv]\n\ndef greenErr (u : Assignment Q) (a b : Sites Q) : ℕ :=\n  diff (u (false,a)) (xor (u (true,b)) (!(xor (sign a) (sign b))))\n\ntheorem green_oriented (y : Assignment Q) (a b : Sites Q) :\n    diff (y (false,a)) (!(y (true,b))) = greenErr (oriented y) a b := by\n  unfold greenErr oriented\n  generalize y (false,a) = x\n  generalize y (true,b) = z\n  generalize sign a = r\n  generalize sign b = s\n  cases x <;> cases z <;> cases r <;> cases s <;> decide\n\ndef extErr (E : Equiv.Perm (Q × Bool))\n    (l r : Q × Bool → Bool) : ℕ :=\n  ∑ x, diff (l x) (xor (r (E x)) (xor x.2 (E x).2))\n\nnoncomputable def siteCost (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) (y : Assignment Q) : ℕ :=\n  (∑ i, diff (y (false,i)) (y (true,i))) +\n    (∑ i, diff (y (false,i)) (y (true,blue i))) +\n    ∑ i, diff (y (false,i)) (!(y (true,green E p i)))\n\ndef pn (p : Network Q)\n    (u : Assignment Q) : ℕ :=\n  ∑ j : Fin 32, ∑ k : KBlock Q,\n    diff (core u (k,false) false (some (some j))) (core u (p j k,true) true (some (some j)))\n\ndef sn (u : Assignment Q) : ℕ :=\n  ∑ k : KBlock Q, diff (core u (k,false) false (some none)) (core u (k,true) true (some none))\n\ndef ln (u : Assignment Q) : ℕ :=\n  ∑ t : TBlock Q, ∑ j : Fin 1024, diff (term u t false (some j)) (core u (atCore t j) true none)\n\ndef ext (E : Equiv.Perm (Q × Bool))\n    (u : Assignment Q) : ℕ :=\n  extErr E (fun x => term u (roleT x false) false none)\n    (fun x => term u (roleT (otherSign x) true) true none)\n\nnoncomputable def sumDirtyT (u : Assignment Q) : ℕ :=\n  ∑ t, dirty (term u t) none\n\nnoncomputable def sumDirtyC (u : Assignment Q) : ℕ :=\n  ∑ c, dirty (core u c) none\n\ntheorem diff_chain (a b c d : Bool) : diff a d ≤ diff a b + diff b c + diff c d := by\n  cases a <;> cases b <;> cases c <;> cases d <;> decide\n\ntheorem green_diff (u : Assignment Q) (a b : Sites Q)\n    (h : sign b = !sign a) : greenErr u a b = diff (u (false,a)) (u (true,b)) := by\n  unfold greenErr\n  rw [h]\n  cases sign a <;> cases u (true,b) <;> rfl\n\ntheorem green_ext (u : Assignment Q)\n    (E : Equiv.Perm (Q × Bool)) (p : Network Q) (x : Q × Bool) :\n    greenErr u (.inl (roleT x false,none)) (green E p (.inl (roleT x false,none))) =\n      diff (term u (roleT x false) false none)\n        (xor (term u (roleT (otherSign (E x)) true) true none) (xor x.2 (E x).2)) := by\n  change diff _ (xor _ (!(xor x.2 (!(E x).2)))) = diff _ (xor _ (xor x.2 (E x).2))\n  have h : ∀ a b : Bool, (!(xor a (!b))) = xor a b := by decide\n  rw [h]\n  rfl\n\ntheorem split_cost (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) (y : Assignment Q) :\n    let u := oriented y\n    siteCost E p y =\n      (∑ t : TBlock Q, blockCost (term u t)) + (∑ c : CBlock Q, blockCost (core u c)) +\n      ext E u + ln u +\n      (∑ x : Q × Bool, diff (term u (roleT x true) false none)\n        (term u (roleT (otherSign x) false) true none)) +\n      (∑ c : CBlock Q, diff (core u c false none)\n        (term u (c.1.1,!c.2) true (some c.1.2))) +\n      (∑ c : CBlock Q, diff (core u c false (some none)) (core u (c.1,!c.2) true (some none))) +\n      ∑ j : Fin 32, ∑ c : CBlock Q,\n        diff (core u c false (some (some j))) (core u (p j c.1,!c.2) true (some (some j))) := by\n  intro u\n  have r (i : Sites Q) : diff (y (false,i)) (y (true,i)) = diff (u (false,i)) (u (true,i)) :=\n    (diff_xor _ _ _).symm\n  have b (i : Sites Q) : diff (y (false,i)) (y (true,blue i)) =\n      diff (u (false,i)) (u (true,blue i)) := by\n    have h : sign (blue i) = sign i := by cases i <;> rfl\n    exact (by simpa only [u,oriented,h] using (diff_xor (y (false,i)) (y (true,blue i)) (sign i)).symm)\n  have g (i : Sites Q) : diff (y (false,i)) (!(y (true,green E p i))) =\n      greenErr u i (green E p i) := green_oriented _ _ _\n  have tr (x : Q × Bool) :\n      greenErr u (.inl (roleT x true,none)) (green E p (.inl (roleT x true,none))) =\n      diff (term u (roleT x true) false none) (term u (roleT (otherSign x) false) true none) :=\n    green_diff u _ _ rfl\n  have tl (a : TBlock Q) (j : Fin 1024) :\n      greenErr u (.inl (a,some j)) (green E p (.inl (a,some j))) =\n      diff (term u a false (some j)) (core u (atCore a j) true none) :=\n    green_diff u _ _ rfl\n  have cl (c : CBlock Q) :\n      greenErr u (.inr (c,none)) (green E p (.inr (c,none))) =\n      diff (core u c false none) (term u (c.1.1,!c.2) true (some c.1.2)) :=\n    green_diff u _ _ rfl\n  have cs (c : CBlock Q) :\n      greenErr u (.inr (c,some none)) (green E p (.inr (c,some none))) =\n      diff (core u c false (some none)) (core u (c.1,!c.2) true (some none)) :=\n    green_diff u _ _ rfl\n  have cp (c : CBlock Q) (j : Fin 32) :\n      greenErr u (.inr (c,some (some j))) (green E p (.inr (c,some (some j)))) =\n      diff (core u c false (some (some j))) (core u (p j c.1,!c.2) true (some (some j))) :=\n    green_diff u _ _ rfl\n  have bT (t : TBlock Q) (j : TPos) : blue (.inl (t,j)) = .inl (t,cycle j) := rfl\n  have bC (c : CBlock Q) (j : CPos) : blue (.inr (c,j)) = .inr (c,cycle j) := rfl\n  have tEq (t : TBlock Q) b j : u (b,Sum.inl (t,j)) = term u t b j := rfl\n  have cEq (c : CBlock Q) b j : u (b,Sum.inr (c,j)) = core u c b j := rfl\n  have h0 (q : Q) (s : Bool) :\n      greenErr u (.inl (((q,false),s),none)) (green E p (.inl (((q,false),s),none))) = _ :=\n    green_ext u E p (q,s)\n  have h1 (q : Q) (s : Bool) :\n      greenErr u (.inl (((q,true),s),none)) (green E p (.inl (((q,true),s),none))) = _ :=\n    tr (q,s)\n  unfold siteCost\n  simp_rw [r,b,g]\n  simp only [Fintype.sum_sum_type]\n  rw [sum_comm (f := fun j c => diff (core u c false (some (some j)))\n    (core u (p j c.1,!c.2) true (some (some j))))]\n  simp only [Fintype.sum_prod_type,Fintype.sum_option,Fintype.sum_bool,bT,bC,tEq,cEq,\n    blockCost,ext,extErr,ln,tl,cl,cs,cp,h0,h1,roleT,otherSign,Prod.fst,Prod.snd]\n  simp only [sum_add_distrib]\n  ac_rfl\n\ntheorem site_lower (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) (y : Assignment Q) :\n    let u := oriented y\n    2*sumDirtyT u + 2*sumDirtyC u + ext E u + ln u + sn u + pn p u ≤ siteCost E p y := by\n  intro u\n  rw [split_cost]\n  have t : 2*sumDirtyT u ≤ ∑ t : TBlock Q, blockCost (term u t) := by\n    simp only [sumDirtyT,mul_sum]\n    exact sum_le_sum fun t _ => block_bound (term u t) none\n  have c : 2*sumDirtyC u ≤ ∑ c : CBlock Q, blockCost (core u c) := by\n    simp only [sumDirtyC,mul_sum]\n    exact sum_le_sum fun c _ => block_bound (core u c) none\n  have s : sn u ≤ ∑ c : CBlock Q,\n      diff (core u c false (some none)) (core u (c.1,!c.2) true (some none)) := by\n    unfold sn\n    rw [Fintype.sum_prod_type (f := fun c : CBlock Q => _)]\n    exact sum_le_sum fun k _ => by rw [Fintype.sum_bool]; simp only [Prod.fst,Prod.snd,Bool.not_true,Bool.not_false]; omega\n  have h : pn p u ≤ ∑ j : Fin 32, ∑ c : CBlock Q,\n      diff (core u c false (some (some j))) (core u (p j c.1,!c.2) true (some (some j))) := by\n    unfold pn\n    apply sum_le_sum fun j _ => ?_\n    rw [Fintype.sum_prod_type (f := fun c : CBlock Q => _)]\n    exact sum_le_sum fun k _ => by rw [Fintype.sum_bool]; simp only [Prod.fst,Prod.snd,Bool.not_true,Bool.not_false]; omega\n  dsimp [u] at *\n  omega\n\ndef coreRep (u : Assignment Q) (c : CBlock Q) :=\n  core u c false none\n\ndef termRep (u : Assignment Q) (t : TBlock Q) :=\n  term u t false none\n\ntheorem core_dist (f : Q → W)\n    (p : Network Q)\n    (hp : ∀ b : KBlock Q → Bool, ∃ w : W → Bool,\n      (∑ k, diff (b k) (w (f (keyK k)))) ≤\n        2 * ∑ j : Fin 32, ∑ k, diff (b k) (b (p j k)))\n    (u : Assignment Q) :\n    ∃ w : W → Bool, (∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1)))) ≤\n      257*sumDirtyC u + 129*sn u + 4*pn p u := by\n  let a := coreRep u\n  let d := fun c : CBlock Q => dirty (core u c) none\n  have hpair (r : Equiv.Perm (KBlock Q)) :\n      (∑ k : KBlock Q, (d (k,false) + d (r k,true))) = sumDirtyC u := by\n    rw [sum_add_distrib, Equiv.sum_comp r (fun k => d (k,true))]\n    unfold sumDirtyC\n    rw [Fintype.sum_prod_type (f := fun c : CBlock Q => d c)]\n    simp only [Fintype.sum_bool,sum_add_distrib]\n    exact add_comm _ _\n  have hs : (∑ k : KBlock Q, diff (a (k,false)) (a (k,true))) ≤ sumDirtyC u + sn u := by\n    rw [← hpair (Equiv.refl _),sn,← sum_add_distrib]\n    refine sum_le_sum fun k _ => ?_\n    have h := diff_chain (a (k,false)) (core u (k,false) false (some none))\n      (core u (k,true) true (some none)) (a (k,true))\n    have hl := dirty_le (core u (k,false)) none false (some none)\n    have hr := dirty_le (core u (k,true)) none true (some none)\n    rw [diff_symm (a (k,false)) (core u (k,false) false (some none))] at h\n    change diff (core u (k,false) false (some none)) (a (k,false)) ≤ d (k,false) at hl\n    change diff (core u (k,true) true (some none)) (a (k,true)) ≤ d (k,true) at hr\n    change diff (a (k,false)) (a (k,true)) ≤ d (k,false) + d (k,true) + _\n    omega\n  have hj (j : Fin 32) :\n      (∑ k : KBlock Q, diff (a (k,false)) (a (p j k,false))) ≤\n      (sumDirtyC u + (∑ k : KBlock Q, diff (a (k,false)) (a (k,true)))) +\n      ∑ k : KBlock Q, diff (core u (k,false) false (some (some j))) (core u (p j k,true) true (some (some j))) := by\n    rw [← hpair (p j), ← Equiv.sum_comp (p j) (fun k => diff (a (k,false)) (a (k,true)))]\n    simp_rw [← sum_add_distrib]\n    refine sum_le_sum fun k _ => ?_\n    let l := core u (k,false) false (some (some j))\n    let r := core u (p j k,true) true (some (some j))\n    have h := diff_chain (a (k,false)) l r (a (p j k,true))\n    have hh := diff_tri (a (k,false)) (a (p j k,true)) (a (p j k,false))\n    have hl : diff (a (k,false)) l ≤ d (k,false) := by\n      rw [diff_symm (a (k,false)) l]\n      exact dirty_le (core u (k,false)) none false (some (some j))\n    have hr : diff r (a (p j k,true)) ≤ d (p j k,true) :=\n      dirty_le (core u (p j k,true)) none true (some (some j))\n    rw [diff_symm (a (p j k,true)) (a (p j k,false))] at hh\n    change diff (a (k,false)) (a (p j k,false)) ≤ _ + diff l r\n    omega\n  have hcut : (∑ j : Fin 32, ∑ k : KBlock Q, diff (a (k,false)) (a (p j k,false))) ≤\n      64*sumDirtyC u + 32*sn u + pn p u := by\n    have h := sum_le_sum (s := (univ : Finset (Fin 32))) (fun j _ => hj j)\n    rw [sum_add_distrib,sum_const,card_univ,Fintype.card_fin,smul_eq_mul,← pn] at h\n    omega\n  obtain ⟨w,hw⟩ := hp (fun k => a (k,false))\n  refine ⟨w,?_⟩\n  have hsum : (∑ k : KBlock Q, diff (a (k,true)) (w (f (keyK k)))) ≤\n      (∑ k : KBlock Q, diff (a (k,false)) (a (k,true))) +\n      ∑ k : KBlock Q, diff (a (k,false)) (w (f (keyK k))) := by\n    rw [← sum_add_distrib]\n    exact sum_le_sum fun k _ => by\n      rw [diff_symm (a (k,false)) (a (k,true))]\n      exact diff_tri _ _ _\n  rw [Fintype.sum_prod_type]\n  simp_rw [Fintype.sum_bool,sum_add_distrib]\n  change (∑ k, diff (a (k,true)) _) + (∑ k, diff (a (k,false)) _) ≤ _\n  omega\n\nnoncomputable def cleanErr (f : Q → W) (w : W → Bool)\n    (u : Assignment Q) (t : TBlock Q) :=\n  if dirty (term u t) none = 0 then diff (termRep u t) (w (f (keyT t))) else 0\n\ntheorem term_bound (f : Q → W) (w : W → Bool)\n    (u : Assignment Q) (t : TBlock Q) (b : Bool) (j : TPos) :\n    diff (term u t b j) (w (f (keyT t))) ≤ dirty (term u t) none + cleanErr f w u t := by\n  rcases dirty_cases (term u t) none with h | h\n  · rw [dirty_zero _ _ h]\n    simp [h,cleanErr,termRep]\n  · simpa [h,cleanErr] using diff_le (term u t b j) (w (f (keyT t)))\n\ntheorem clean_bound (f : Q → W) (w : W → Bool)\n    (u : Assignment Q) :\n    1024*(∑ t, cleanErr f w u t) ≤ ln u + sumDirtyC u +\n      ∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1))) := by\n  let g := fun c : CBlock Q => dirty (core u c) none + diff (coreRep u c) (w (f (keyK c.1)))\n  let le := fun t j => diff (term u t false (some j)) (core u (atCore t j) true none)\n  have h (t : TBlock Q) (j : Fin 1024) :\n      cleanErr f w u t ≤ le t j + g (atCore t j) := by\n    unfold cleanErr\n    split_ifs with h\n    · dsimp [le,g]\n      rw [dirty_zero _ _ h false (some j)]\n      have h0 := diff_chain (termRep u t) (core u (atCore t j) true none)\n        (coreRep u (atCore t j)) (w (f (keyT t)))\n      have h1 := dirty_le (core u (atCore t j)) none true none\n      change diff _ (coreRep u (atCore t j)) ≤ _ at h1\n      change diff (termRep u t) _ ≤ diff (termRep u t) _ + (_ + diff _ (w (f (keyT t))))\n      omega\n    · exact Nat.zero_le _\n  have hh := sum_le_sum (s := (univ : Finset (TBlock Q))) (fun t _ =>\n    sum_le_sum (s := (univ : Finset (Fin 1024))) (fun j _ => h t j))\n  simp only [sum_const,card_univ,Fintype.card_fin,smul_eq_mul,← mul_sum] at hh\n  have eq : (∑ t : TBlock Q, ∑ j : Fin 1024, g (atCore t j)) = ∑ c, g c := by\n    calc\n      _ = ∑ x : TBlock Q × Fin 1024, g (linkCoords Q x) := (Fintype.sum_prod_type _).symm\n      _ = _ := Equiv.sum_comp (linkCoords Q) g\n  simp only [sum_add_distrib] at hh\n  rw [eq] at hh\n  simpa [le,g,ln,sum_add_distrib,sumDirtyC,add_assoc] using hh\n\ndef target (E : Equiv.Perm (Q × Bool))\n    (f : Q → W) (w : W → Bool) : ℕ :=\n  extErr E (fun x => w (f x.1)) (fun x => w (f x.1))\n\ntheorem ext_bound (E : Equiv.Perm (Q × Bool))\n    (f : Q → W) (w : W → Bool) (u : Assignment Q) :\n    target E f w ≤ ext E u + sumDirtyT u + ∑ t, cleanErr f w u t := by\n  let m := fun t : TBlock Q => dirty (term u t) none + cleanErr f w u t\n  let R := (Equiv.prodCongr (Equiv.refl Q) (Function.Involutive.toPerm Bool.not Bool.not_not)).trans E.symm\n  have h (x : Q × Bool) :\n      diff (w (f x.1)) (xor (w (f (E x).1)) (xor x.2 (E x).2)) ≤\n      diff (term u (roleT x false) false none)\n        (xor (term u (roleT (otherSign (E x)) true) true none) (xor x.2 (E x).2)) +\n        (m (roleT x false) + m (roleT (otherSign (E x)) true)) := by\n    have hh := diff_chain (w (f x.1)) (term u (roleT x false) false none)\n      (xor (term u (roleT (otherSign (E x)) true) true none) (xor x.2 (E x).2))\n      (xor (w (f (E x).1)) (xor x.2 (E x).2))\n    rw [diff_xor,diff_symm (w (f x.1)) (term u (roleT x false) false none)] at hh\n    have hl := term_bound f w u (roleT x false) false none\n    have hr := term_bound f w u (roleT (otherSign (E x)) true) true none\n    change diff _ (w (f x.1)) ≤ m (roleT x false) at hl\n    change diff _ (w (f (E x).1)) ≤ m (roleT (otherSign (E x)) true) at hr\n    omega\n  have hh := sum_le_sum (s := (univ : Finset (Q × Bool))) (fun x _ => h x)\n  simp only [sum_add_distrib] at hh\n  have he := Equiv.sum_comp (R.symm) (fun x => m (roleT x true))\n  have er (x : Q × Bool) : R.symm x = otherSign (E x) := rfl\n  simp only [er] at he\n  rw [he] at hh\n  have hm := Equiv.sum_comp (roleCoords Q) m\n  rw [Fintype.sum_prod_type (f := fun p : (Q × Bool) × Bool => m (roleCoords Q p))] at hm\n  simp only [Fintype.sum_bool,sum_add_distrib] at hm\n  change (∑ x : Q × Bool, m (roleT x true)) + (∑ x : Q × Bool, m (roleT x false)) = _ at hm\n  change target E f w ≤ ext E u + (_ + _) at hh\n  rw [add_comm (∑ x : Q × Bool, m (roleT x false)),hm] at hh\n  simpa [m,sum_add_distrib,sumDirtyT,add_assoc] using hh\n\ndef distance {A : Type} [Fintype A] (x y : A → Bool) : ℕ :=\n  ∑ i, diff (x i) (y i)\n\ntheorem layout_dist (f : Q → W) (w : W → Bool)\n    (y : Assignment Q) :\n    let u := oriented y\n    distance y (canonical f w) ≤\n      2050*(sumDirtyT u + ∑ t, cleanErr f w u t) +\n      68*(sumDirtyC u + ∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1)))) := by\n  intro u\n  have e (b : Bool) (i : Sites Q) :\n      diff (y (b,i)) (canonical f w (b,i)) = diff (u (b,i)) (w (f (owner i))) := by\n    unfold canonical\n    rw [← diff_xor _ _ (sign i),xor_cancel]\n    rfl\n  have hT (b : Bool) (t : TBlock Q) :\n      (∑ j : TPos, diff (term u t b j) (w (f (keyT t)))) ≤\n      1025*(dirty (term u t) none + cleanErr f w u t) := by\n    simpa only [sum_const,card_univ,Fintype.card_option,Fintype.card_fin,smul_eq_mul] using\n      sum_le_sum (s := (univ : Finset TPos)) (fun j _ => term_bound f w u t b j)\n  have hC (b : Bool) (c : CBlock Q) :\n      (∑ j : CPos, diff (core u c b j) (w (f (keyK c.1)))) ≤\n      34*(dirty (core u c) none + diff (coreRep u c) (w (f (keyK c.1)))) := by\n    have h (j : CPos) := (diff_tri (core u c b j) (coreRep u c) (w (f (keyK c.1)))).trans\n      (Nat.add_le_add_right (dirty_le (core u c) none b j) _)\n    simpa only [sum_const,card_univ,Fintype.card_option,Fintype.card_fin,smul_eq_mul] using\n      sum_le_sum (s := (univ : Finset CPos)) (fun j _ => h j)\n  have h (b : Bool) :\n      (∑ i : Sites Q, diff (u (b,i)) (w (f (owner i)))) ≤\n      1025*(sumDirtyT u + ∑ t, cleanErr f w u t) +\n      34*(sumDirtyC u + ∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1)))) := by\n    simp only [Fintype.sum_sum_type]\n    rw [Fintype.sum_prod_type (f := fun p : TBlock Q × TPos => _),\n      Fintype.sum_prod_type (f := fun p : CBlock Q × CPos => _)]\n    have ht := sum_le_sum (s := (univ : Finset (TBlock Q))) (fun t _ => hT b t)\n    have hc := sum_le_sum (s := (univ : Finset (CBlock Q))) (fun c _ => hC b c)\n    simp only [sumDirtyT,sumDirtyC,mul_sum,← sum_add_distrib]\n    exact Nat.add_le_add ht hc\n  rw [distance,Fintype.sum_prod_type]\n  simp only [e,Fintype.sum_bool]\n  have := h true\n  have := h false\n  omega\n\ntheorem layout_round [Fintype W]\n    (E : Equiv.Perm (Q × Bool)) (f : Q → W) :\n    ∃ p : Network Q, (∀ j k, f (keyK (p j k)) = f (keyK k)) ∧\n      ∀ y : Assignment Q, ∃ w : W → Bool,\n        let u := oriented y\n        let I := sumDirtyT u + sumDirtyC u + ln u + sn u + pn p u\n        distance y (canonical f w) ≤ 40000*I ∧ 2*target E f w + I ≤ 2*siteCost E p y := by\n  obtain ⟨p,hp,h⟩ := expand_fibers (fun k : KBlock Q => f (keyK k))\n  refine ⟨p,hp,fun y => ?_⟩\n  let u := oriented y\n  obtain ⟨w,hw⟩ := core_dist f p h u\n  refine ⟨w,?_⟩\n  dsimp only\n  have hcl := clean_bound f w u\n  have hex := ext_bound E f w u\n  have hd := layout_dist f w y\n  have hl := site_lower E p y\n  dsimp only at hd hl\n  dsimp [u] at hw hcl hex\n  constructor <;> omega\n\ntheorem oriented_canonical (f : Q → W) (w : W → Bool)\n    (b : Bool) (i : Sites Q) : oriented (canonical f w) (b,i) = w (f (owner i)) :=\n  xor_cancel _ _\n\ntheorem layout_exact (E : Equiv.Perm (Q × Bool))\n    (f : Q → W) (p : Network Q)\n    (hp : ∀ j k, f (keyK (p j k)) = f (keyK k)) (w : W → Bool) :\n    siteCost E p (canonical f w) = target E f w := by\n  rw [split_cost]\n  simp only [term,core,blockCost,ln,ext,target,extErr,oriented_canonical,owner,keyT,keyK,atCore,\n    roleT,otherSign,Prod.fst,Prod.snd,diff_self,sum_const_zero,zero_add,add_zero]\n  have h (j : Fin 32) (k : KBlock Q) : f ((p j k).1.1) = f k.1.1 := hp j k\n  simp only [h,diff_self,sum_const_zero,add_zero]\n\nend Layout\n\nend PEqualNP.Outside.L_pcx_409\n","lean_sha256":"b5784ef3204a3ee1fefff6cc0923263f587c989acf5cd22eab1f46730128df7d","build":{"command":"lake build PEqualNP.Outside.L_pcx_409","exit":0,"seconds":28.57},"axioms":["propext","Classical.choice","Quot.sound"],"upstream_commit":"1d812430ac44fdcafedd4cc5a2405c3c1c0eb5cc","approach":"Each NAND gate uses three disagreement triangles and an equality comparison between its private output h and z. Exhausting the four local bits and both reference orientations gave cost 3 plus the output mismatch at the correct h. A wrong h adds at least one unit to that bound. I also enumerated every pin mask for empty circuits, a repeated-input gate, a self-referential gate, and one- and two-gate examples with three variables. The self-referential gate had zero satisfying extensions. The anchored gadget counts agreed with the logical counts in all cases.\n\nFor each logical wire, I constructed a finite expander from 32 permutations using a union bound over small sets and supersets. The physical layout uses red and blue cycle edges, complementary green links to the expander, and one external comparison endpoint per terminal cycle. A nonconstant cycle pays at least two red-blue violations. Replicated links bound the number of constant terminals that disagree with the core rounding. Together, these control the Hamming distance with enough cost slack to repair the private NAND outputs. The zero pin on the reference fixes orientation. At cost 6g, the repair inequality forces Hamming distance zero and rules out any NAND violation, so the encoding gives a bijection on the pinned sets.\n\nThe complete proof of the exact row statement built in 215.99 seconds and passed Lean kernel replay and the separate axiom audit. Its 58,558 characters exceed the 40,000-character cap. The included 39,990-character partial file builds and contains the NAND identities, the permutation expansion proof, and the exact-cost and robust rounding lemmas for the matching layout. The build record is for that partial file. The axiom list is from the complete local theorem. The row response omitted level.","reason":"other","skipped":[]}
