import Mathlib.NumberTheory.Bertrand import Mathlib.Data.ZMod.Basic import Mathlib.FieldTheory.Finite.Basic import Mathlib.Algebra.Polynomial.Roots import Mathlib.Algebra.Polynomial.Degree.Operations import Mathlib.Data.Nat.Log /-! # Nisan–Wigderson Polynomial Design (Lemma 1 of `main.tex`) Formalizes the polynomial design over `ZMod q` (for prime `q ≥ ℓ > Δ`): - Graph sets `S_g = {(a, g(a)) : a ∈ A}` for `g ∈ Polynomial (ZMod q)` of `natDegree ≤ Δ` - Cardinality `|S_g| = ℓ` - Pairwise intersection bound `|S_g ∩ S_h| ≤ Δ` for `g ≠ h` - Distinctness of `S_g` when `Δ < ℓ` - Existence of `n` distinct sets in a universe `U = A ×ˢ univ` of size `ℓ * q < 2 * ℓ^2` whenever `n ≤ q ^ (Δ + 1)`, together with the `main.tex` specialization `ℓ = m^2`, `Δ = ⌈log₂ n⌉`, `|U| < 2 * m^4`. -/ namespace PermanentPIT.Design open Polynomial Finset /-- The universe `U = A × F_q` for a subset `A ⊆ ZMod q`. -/ def designUniverse {q : ℕ} [NeZero q] (A : Finset (ZMod q)) : Finset (ZMod q × ZMod q) := A ×ˢ (Finset.univ : Finset (ZMod q)) /-- The graph set `S_g = {(a, g(a)) : a ∈ A}` of a polynomial `g` over `ZMod q`. -/ def polyGraphSet {q : ℕ} (A : Finset (ZMod q)) (g : Polynomial (ZMod q)) : Finset (ZMod q × ZMod q) := A.image (fun a => (a, g.eval a)) /-- The universe has cardinality `|A| * q`. -/ theorem designUniverse_card {q : ℕ} [NeZero q] (A : Finset (ZMod q)) : (designUniverse A).card = A.card * q := by simp [designUniverse, Finset.card_product, ZMod.card] /-- If `q < 2 * ℓ` and `|A| = ℓ` with `0 < ℓ`, then `|U| < 2 * ℓ^2`. -/ theorem designUniverse_card_lt {q ℓ : ℕ} [NeZero q] (A : Finset (ZMod q)) (hA : A.card = ℓ) (hℓ : 0 < ℓ) (hq : q < 2 * ℓ) : (designUniverse A).card < 2 * ℓ ^ 2 := by rw [designUniverse_card, hA] have h1 : ℓ * q < ℓ * (2 * ℓ) := Nat.mul_lt_mul_of_pos_left hq hℓ have h2 : ℓ * (2 * ℓ) = 2 * ℓ ^ 2 := by ring omega /-- Every graph set `S_g` is contained in the universe `U = A × F_q`. -/ theorem polyGraphSet_subset_universe {q : ℕ} [NeZero q] (A : Finset (ZMod q)) (g : Polynomial (ZMod q)) : polyGraphSet A g ⊆ designUniverse A := by intro p hp rcases Finset.mem_image.mp hp with ⟨a, ha, rfl⟩ exact Finset.mem_product.mpr ⟨ha, Finset.mem_univ _⟩ /-- Every graph set `S_g` has cardinality `|A|`. -/ theorem polyGraphSet_card {q : ℕ} (A : Finset (ZMod q)) (g : Polynomial (ZMod q)) : (polyGraphSet A g).card = A.card := by unfold polyGraphSet apply Finset.card_image_of_injective intro a b h exact (Prod.mk.inj h).1 /-- Two distinct polynomials of degree at most `Δ` over the prime field `ZMod q` have graph sets intersecting in at most `Δ` points. -/ theorem polyGraphSet_inter_card_le {q Δ : ℕ} [Fact (Nat.Prime q)] (A : Finset (ZMod q)) {g h : Polynomial (ZMod q)} (hgh : g ≠ h) (hg : g.natDegree ≤ Δ) (hh : h.natDegree ≤ Δ) : (polyGraphSet A g ∩ polyGraphSet A h).card ≤ Δ := by classical have hsub_ne : g - h ≠ 0 := sub_ne_zero.mpr hgh let I := polyGraphSet A g ∩ polyGraphSet A h have hfst_inj : Set.InjOn Prod.fst (I : Set (ZMod q × ZMod q)) := by intro p₁ hp₁ p₂ hp₂ heq rw [Finset.mem_coe] at hp₁ hp₂ have hp₁' : p₁ ∈ polyGraphSet A g := (Finset.mem_inter.mp hp₁).1 have hp₂' : p₂ ∈ polyGraphSet A g := (Finset.mem_inter.mp hp₂).1 rcases Finset.mem_image.mp hp₁' with ⟨a₁, _, rfl⟩ rcases Finset.mem_image.mp hp₂' with ⟨a₂, _, rfl⟩ dsimp at heq subst heq rfl have himg_sub : (polyGraphSet A g ∩ polyGraphSet A h).image Prod.fst ⊆ (g - h).roots.toFinset := by intro a ha rcases Finset.mem_image.mp ha with ⟨p, hp, rfl⟩ have hpg : p ∈ polyGraphSet A g := (Finset.mem_inter.mp hp).1 have hph : p ∈ polyGraphSet A h := (Finset.mem_inter.mp hp).2 rcases Finset.mem_image.mp hpg with ⟨a₁, _, hp₁⟩ rcases Finset.mem_image.mp hph with ⟨a₂, _, hp₂⟩ have h12 : a₁ = a₂ := by have := hp₁.trans hp₂.symm exact (Prod.mk.inj this).1 have heval : g.eval a₁ = h.eval a₁ := by have := hp₁.trans hp₂.symm rw [← h12] at this exact (Prod.mk.inj this).2 have hp_fst : p.1 = a₁ := by rw [← hp₁] rw [hp_fst, Multiset.mem_toFinset, Polynomial.mem_roots hsub_ne, Polynomial.IsRoot, Polynomial.eval_sub, heval, sub_self] calc (polyGraphSet A g ∩ polyGraphSet A h).card = ((polyGraphSet A g ∩ polyGraphSet A h).image Prod.fst).card := (Finset.card_image_of_injOn hfst_inj).symm _ ≤ (g - h).roots.toFinset.card := Finset.card_le_card himg_sub _ ≤ (g - h).roots.card := Multiset.toFinset_card_le _ _ ≤ (g - h).natDegree := Polynomial.card_roots' (g - h) _ ≤ max g.natDegree h.natDegree := Polynomial.natDegree_sub_le g h _ ≤ Δ := max_le hg hh /-- When `Δ < |A|`, distinct polynomials of degree `≤ Δ` produce distinct graph sets. -/ theorem polyGraphSet_ne_of_ne {q Δ : ℕ} [Fact (Nat.Prime q)] (A : Finset (ZMod q)) {g h : Polynomial (ZMod q)} (hΔ : Δ < A.card) (hgh : g ≠ h) (hg : g.natDegree ≤ Δ) (hh : h.natDegree ≤ Δ) : polyGraphSet A g ≠ polyGraphSet A h := by intro heq have hinter : (polyGraphSet A g ∩ polyGraphSet A h).card = A.card := by rw [heq, Finset.inter_self, polyGraphSet_card] have hle := polyGraphSet_inter_card_le A hgh hg hh omega /-- Map a coefficient vector of length `Δ + 1` to the corresponding polynomial of degree `≤ Δ`. -/ noncomputable def polyOfCoeffs {q Δ : ℕ} (c : Fin (Δ + 1) → ZMod q) : Polynomial (ZMod q) := ∑ k : Fin (Δ + 1), Polynomial.C (c k) * Polynomial.X ^ (k : ℕ) /-- Every polynomial constructed from `Δ + 1` coefficients has `natDegree ≤ Δ`. -/ theorem polyOfCoeffs_natDegree_le {q Δ : ℕ} (c : Fin (Δ + 1) → ZMod q) : (polyOfCoeffs c).natDegree ≤ Δ := by unfold polyOfCoeffs refine Polynomial.natDegree_sum_le_of_forall_le _ _ fun k _ => ?_ exact (Polynomial.natDegree_C_mul_X_pow_le (c k) (k : ℕ)).trans (Nat.le_of_lt_succ k.isLt) /-- The `k`-th coefficient of `polyOfCoeffs c` is `c k`. -/ theorem polyOfCoeffs_coeff {q Δ : ℕ} (c : Fin (Δ + 1) → ZMod q) (k : Fin (Δ + 1)) : (polyOfCoeffs c).coeff (k : ℕ) = c k := by unfold polyOfCoeffs rw [Polynomial.finsetSum_coeff] simp only [Polynomial.coeff_C_mul, Polynomial.coeff_X_pow, mul_ite, mul_one, mul_zero] rw [Finset.sum_eq_single k] · simp · intro j _ hjk have : (k : ℕ) ≠ (j : ℕ) := fun h => hjk (Fin.ext h.symm) simp [this] · intro hk exact absurd (Finset.mem_univ k) hk /-- Distinct coefficient vectors give distinct polynomials. -/ theorem polyOfCoeffs_injective {q Δ : ℕ} : Function.Injective (@polyOfCoeffs q Δ) := by intro c₁ c₂ h funext k have := congrArg (fun p => p.coeff (k : ℕ)) h simpa [polyOfCoeffs_coeff] using this /-- Bertrand's postulate supplies a prime `q` with `ℓ ≤ q < 2 * ℓ` for any `ℓ ≥ 2`. -/ theorem exists_prime_ge_and_lt_two_mul {ℓ : ℕ} (hℓ : 2 ≤ ℓ) : ∃ q : ℕ, Nat.Prime q ∧ ℓ ≤ q ∧ q < 2 * ℓ := by have hne : ℓ - 1 ≠ 0 := by omega obtain ⟨q, hq_prime, hgt, hle⟩ := Nat.exists_prime_lt_and_le_two_mul (ℓ - 1) hne exact ⟨q, hq_prime, by omega, by omega⟩ /-- **Nisan–Wigderson polynomial design (fixed prime `q`).** For any prime `q ≥ ℓ > Δ` and `n ≤ q ^ (Δ + 1)`, there exist `A ⊆ ZMod q` of size `ℓ` and `n` distinct subsets `S : Fin n → Finset (ZMod q × ZMod q)` of `U = A × F_q` such that `|U| = ℓ * q`, `|S i| = ℓ`, and `|S i ∩ S j| ≤ Δ` for all `i ≠ j`. -/ theorem nw_design_over_zmod {q ℓ Δ n : ℕ} [Fact (Nat.Prime q)] (hℓq : ℓ ≤ q) (hΔℓ : Δ < ℓ) (hn : n ≤ q ^ (Δ + 1)) : ∃ (A : Finset (ZMod q)) (S : Fin n → Finset (ZMod q × ZMod q)), A.card = ℓ ∧ (designUniverse A).card = ℓ * q ∧ Function.Injective S ∧ (∀ i, S i ⊆ designUniverse A) ∧ (∀ i, (S i).card = ℓ) ∧ (∀ i j, i ≠ j → (S i ∩ S j).card ≤ Δ) := by have hcard_univ : ℓ ≤ (Finset.univ : Finset (ZMod q)).card := by simpa [ZMod.card] using hℓq obtain ⟨A, -, hAcard⟩ := Finset.exists_subset_card_eq hcard_univ have hcard_coeffs : Fintype.card (Fin n) ≤ Fintype.card (Fin (Δ + 1) → ZMod q) := by simp [ZMod.card, hn] have hnonempty : Nonempty (Fin n ↪ (Fin (Δ + 1) → ZMod q)) := Function.Embedding.nonempty_of_card_le hcard_coeffs obtain ⟨emb⟩ := hnonempty let g : Fin n → Polynomial (ZMod q) := fun i => polyOfCoeffs (emb i) let S : Fin n → Finset (ZMod q × ZMod q) := fun i => polyGraphSet A (g i) have hg_deg : ∀ i, (g i).natDegree ≤ Δ := fun i => polyOfCoeffs_natDegree_le (emb i) have hg_inj : Function.Injective g := polyOfCoeffs_injective.comp emb.injective have hS_inj : Function.Injective S := by intro i j hij by_contra hne exact polyGraphSet_ne_of_ne A (by omega) (fun h => hne (hg_inj h)) (hg_deg i) (hg_deg j) hij refine ⟨A, S, hAcard, ?_, hS_inj, ?_, ?_, ?_⟩ · rw [designUniverse_card, hAcard] · intro i exact polyGraphSet_subset_universe A (g i) · intro i rw [polyGraphSet_card, hAcard] · intro i j hij exact polyGraphSet_inter_card_le A (fun h => hij (hg_inj h)) (hg_deg i) (hg_deg j) /-- **Nisan–Wigderson polynomial design (with Bertrand prime choice).** For any `ℓ ≥ 2`, `Δ < ℓ`, and `n ≤ 2 ^ (Δ + 1)`, there exist a prime `q` with `ℓ ≤ q < 2 * ℓ`, a universe `U = A × F_q` of size `ℓ * q < 2 * ℓ^2`, and `n` distinct subsets `S i ⊆ U` of size `ℓ` with pairwise intersections of size at most `Δ`. -/ theorem nw_design_exists {ℓ Δ n : ℕ} (hℓ : 2 ≤ ℓ) (hΔℓ : Δ < ℓ) (hn : n ≤ 2 ^ (Δ + 1)) : ∃ (q : ℕ) (_ : Fact (Nat.Prime q)) (A : Finset (ZMod q)) (S : Fin n → Finset (ZMod q × ZMod q)), ℓ ≤ q ∧ q < 2 * ℓ ∧ (designUniverse A).card = ℓ * q ∧ (designUniverse A).card < 2 * ℓ ^ 2 ∧ Function.Injective S ∧ (∀ i, S i ⊆ designUniverse A) ∧ (∀ i, (S i).card = ℓ) ∧ (∀ i j, i ≠ j → (S i ∩ S j).card ≤ Δ) := by obtain ⟨q, hq_prime, hℓq, hq_lt⟩ := exists_prime_ge_and_lt_two_mul hℓ have : Fact (Nat.Prime q) := ⟨hq_prime⟩ have h2q : 2 ≤ q := le_trans hℓ hℓq have hn_q : n ≤ q ^ (Δ + 1) := le_trans hn (Nat.pow_le_pow_left h2q (Δ + 1)) obtain ⟨A, S, hAcard, hUcard, hS_inj, hS_sub, hS_card, hS_inter⟩ := nw_design_over_zmod (q := q) hℓq hΔℓ hn_q refine ⟨q, ⟨hq_prime⟩, A, S, hℓq, hq_lt, hUcard, ?_, hS_inj, hS_sub, hS_card, hS_inter⟩ exact designUniverse_card_lt A hAcard (by omega) hq_lt /-- **Lemma 1 of `main.tex` (specialized to `ℓ = m^2`, `Δ = ⌈log₂ n⌉`).** For `m ≥ 2`, `n ≥ 2`, and `Δ = Nat.clog 2 n < m^2`, there exist a prime `q` with `m^2 ≤ q < 2 * m^2`, a universe `U = A × F_q` with `|U| = m^2 * q < 2 * m^4`, and `n` distinct subsets `S_0, ..., S_{n-1} ⊆ U` with `|S_i| = m^2` and `|S_i ∩ S_j| ≤ Δ` for `i ≠ j`. -/ theorem lemma1_design {m n : ℕ} (hm : 2 ≤ m) (_hn : 2 ≤ n) (hΔ : Nat.clog 2 n < m ^ 2) : ∃ (q : ℕ) (_ : Fact (Nat.Prime q)) (A : Finset (ZMod q)) (S : Fin n → Finset (ZMod q × ZMod q)), m ^ 2 ≤ q ∧ q < 2 * m ^ 2 ∧ (designUniverse A).card = m ^ 2 * q ∧ (designUniverse A).card < 2 * m ^ 4 ∧ Function.Injective S ∧ (∀ i, S i ⊆ designUniverse A) ∧ (∀ i, (S i).card = m ^ 2) ∧ (∀ i j, i ≠ j → (S i ∩ S j).card ≤ Nat.clog 2 n) := by have hℓ : 2 ≤ m ^ 2 := by nlinarith have hn_le : n ≤ 2 ^ (Nat.clog 2 n + 1) := by have h1 : n ≤ 2 ^ Nat.clog 2 n := Nat.le_pow_clog (by omega) n have h2 : 2 ^ Nat.clog 2 n ≤ 2 ^ (Nat.clog 2 n + 1) := Nat.pow_le_pow_right (by omega) (Nat.le_succ _) exact le_trans h1 h2 obtain ⟨q, hq, A, S, h1, h2, h3, h4, h5, h6, h7, h8⟩ := nw_design_exists hℓ hΔ hn_le refine ⟨q, hq, A, S, h1, h2, h3, ?_, h5, h6, h7, h8⟩ have hpow : 2 * (m ^ 2) ^ 2 = 2 * m ^ 4 := by ring omega end PermanentPIT.Design #print axioms PermanentPIT.Design.polyGraphSet_card #print axioms PermanentPIT.Design.polyGraphSet_inter_card_le #print axioms PermanentPIT.Design.nw_design_over_zmod #print axioms PermanentPIT.Design.nw_design_exists #print axioms PermanentPIT.Design.lemma1_design