import Mathlib.Data.ZMod.Basic import Mathlib.Data.Nat.ModEq import Mathlib.Data.Nat.Factors import Mathlib.Data.Fintype.BigOperators import Mathlib.Tactic /-! # CRT Unit-Lift Normalization for Opposite-Sign Swap Homometry Formalizes the Normalization section of `swap-displacements.md`: 1. For any `n ≥ 1` and `d : ℕ` with `q = Nat.gcd n d`, there exists a unit `u : (ZMod n)ˣ` such that `(u : ZMod n) * (q : ZMod n) = (d : ZMod n)` (via Chinese Remainder Theorem). 2. Pulling back a sign word along multiplication by a unit `u : (ZMod n)ˣ` permutes autocorrelation lags, preserves autocorrelation equality, and conjugates dihedral + complement symmetries to dihedral + complement symmetries. 3. Consequently, a nontrivial opposite-sign swap exists at displacement `d` iff one exists at displacement `q = Nat.gcd n d`. -/ namespace SwapHomometry open scoped BigOperators section Core variable {n : ℕ} [NeZero n] /-- A binary sign word takes values in `{1, -1}`. -/ def SignWord (x : ZMod n → ℤ) : Prop := ∀ i : ZMod n, x i = 1 ∨ x i = -1 /-- Periodic autocorrelation of `x` at lag `k`. -/ def corr (x : ZMod n → ℤ) (k : ZMod n) : ℤ := ∑ i : ZMod n, x i * x (i + k) /-- Swap the entries of `x` at positions `0` and `d`. -/ def swapAt (d : ZMod n) (x : ZMod n → ℤ) : ZMod n → ℤ := fun i => if i = 0 then x d else if i = d then x 0 else x i /-- Dihedral + global-negation (complement) equivalence of cyclic sign words. -/ def DihedralEquiv (x y : ZMod n → ℤ) : Prop := ∃ (rev neg : Bool) (t : ZMod n), ∀ i : ZMod n, y i = (if neg then (-1 : ℤ) else 1) * x (if rev then t - i else t + i) /-- Existence of a nontrivial homometric opposite-sign swap at displacement `d` in `ZMod n`. -/ def HasNontrivialSwap (n d : ℕ) [NeZero n] : Prop := ∃ x : ZMod n → ℤ, SignWord x ∧ x 0 = 1 ∧ x (d : ZMod n) = -1 ∧ (∀ k : ZMod n, corr x k = corr (swapAt (d : ZMod n) x) k) ∧ ¬ DihedralEquiv x (swapAt (d : ZMod n) x) end Core section UnitLift /-- CRT unit-lift lemma: for `n ≥ 1` and `d : ℕ` with `q = Nat.gcd n d`, there exists a unit `u : (ZMod n)ˣ` such that `(u : ZMod n) * q = d`. -/ theorem exists_unit_mul_gcd_eq (n d : ℕ) [NeZero n] : ∃ u : (ZMod n)ˣ, (u : ZMod n) * (Nat.gcd n d : ZMod n) = (d : ZMod n) := by set q := Nat.gcd n d have hqn : q ∣ n := Nat.gcd_dvd_left n d have hqd : q ∣ d := Nat.gcd_dvd_right n d have hqpos : 0 < q := Nat.gcd_pos_of_pos_left d (NeZero.pos n) set L := n / q set a := d / q have hqL : q * L = n := Nat.mul_div_cancel' hqn have hqa : q * a = d := Nat.mul_div_cancel' hqd have hcop_La : Nat.Coprime L a := by simpa [L, a, q] using Nat.coprime_div_gcd_div_gcd hqpos -- Product of all primes dividing `n` that do not divide `L` set M := ∏ p ∈ n.primeFactors.filter (fun p => ¬ p ∣ L), p have hcop_LM : Nat.Coprime L M := by refine Nat.coprime_prod_right_iff.mpr ?_ intro p hp simp only [Finset.mem_filter, Nat.mem_primeFactors] at hp exact (hp.1.1.coprime_iff_not_dvd.mpr hp.2).symm -- Lift `a mod L` and `1 mod M` via CRT let cr := Nat.chineseRemainder hcop_LM a 1 set uNat := cr.val have huL : uNat ≡ a [MOD L] := cr.property.1 have huM : uNat ≡ 1 [MOD M] := cr.property.2 -- Show `Nat.Coprime uNat n` by checking no prime divides both have hcop_un : Nat.Coprime uNat n := by rw [Nat.coprime_iff_gcd_eq_one] by_contra hne set p := (Nat.gcd uNat n).minFac have hp : p.Prime := Nat.minFac_prime hne have hpu : p ∣ uNat := dvd_trans (Nat.minFac_dvd _) (Nat.gcd_dvd_left uNat n) have hpn : p ∣ n := dvd_trans (Nat.minFac_dvd _) (Nat.gcd_dvd_right uNat n) by_cases hpL : p ∣ L · have hmod : uNat ≡ a [MOD p] := huL.of_dvd hpL have hpa : p ∣ a := (Nat.ModEq.dvd_iff hmod dvd_rfl).mp hpu have hdvd1 : p ∣ Nat.gcd L a := Nat.dvd_gcd hpL hpa rw [hcop_La.gcd_eq_one] at hdvd1 exact hp.not_dvd_one hdvd1 · have hp_mem : p ∈ n.primeFactors.filter (fun p => ¬ p ∣ L) := by simp only [Finset.mem_filter, Nat.mem_primeFactors] exact ⟨⟨hp, hpn, NeZero.ne n⟩, hpL⟩ have hpM : p ∣ M := Finset.dvd_prod_of_mem _ hp_mem have hmod : uNat ≡ 1 [MOD p] := huM.of_dvd hpM have hp1 : p ∣ 1 := (Nat.ModEq.dvd_iff hmod dvd_rfl).mp hpu exact hp.not_dvd_one hp1 -- Form the unit `u : (ZMod n)ˣ` refine ⟨ZMod.unitOfCoprime uNat hcop_un, ?_⟩ rw [ZMod.coe_unitOfCoprime] -- Check `(uNat : ZMod n) * q = d` via `uNat * q ≡ d [MOD n]` have hmod_n : uNat * q ≡ a * q [MOD L * q] := by rw [Nat.ModEq] at huL ⊢ rw [Nat.mul_mod_mul_right, Nat.mul_mod_mul_right, huL] rw [mul_comm L q, hqL, mul_comm a q, hqa] at hmod_n rw [← Nat.cast_mul] exact (ZMod.natCast_eq_natCast_iff (uNat * q) d n).mpr hmod_n end UnitLift section Action /-- Pullback of a word along multiplication by a unit `u : (ZMod n)ˣ`. -/ def pullback {n : ℕ} (u : (ZMod n)ˣ) (x : ZMod n → ℤ) : ZMod n → ℤ := fun i => x ((u : ZMod n) * i) lemma pullback_mul {n : ℕ} (u v : (ZMod n)ˣ) (x : ZMod n → ℤ) : pullback v (pullback u x) = pullback (u * v) x := by funext i simp [pullback, mul_assoc] lemma pullback_one {n : ℕ} (x : ZMod n → ℤ) : pullback (1 : (ZMod n)ˣ) x = x := by funext i simp [pullback] lemma pullback_inv_pullback {n : ℕ} (u : (ZMod n)ˣ) (x : ZMod n → ℤ) : pullback u⁻¹ (pullback u x) = x := by rw [pullback_mul, mul_inv_cancel, pullback_one] /-- Pullback along a unit preserves `SignWord`. -/ theorem signWord_pullback_iff {n : ℕ} (u : (ZMod n)ˣ) (x : ZMod n → ℤ) : SignWord (pullback u x) ↔ SignWord x := by constructor · intro h i have hi := h ((u⁻¹ : (ZMod n)ˣ) * i) simpa [pullback, ← mul_assoc] using hi · intro h i exact h ((u : ZMod n) * i) /-- Multiplication by a unit `u` permutes periodic autocorrelation lags by `k ↦ u * k`. -/ theorem corr_pullback {n : ℕ} [NeZero n] (u : (ZMod n)ˣ) (x : ZMod n → ℤ) (k : ZMod n) : corr (pullback u x) k = corr x ((u : ZMod n) * k) := by unfold corr pullback have hrew : ∀ i : ZMod n, x ((u : ZMod n) * i) * x ((u : ZMod n) * (i + k)) = x ((u : ZMod n) * i) * x ((u : ZMod n) * i + (u : ZMod n) * k) := by intro i; rw [mul_add] simp_rw [hrew] exact Equiv.sum_comp (Units.mulLeft u) (fun j => x j * x (j + (u : ZMod n) * k)) /-- Pulling back by a unit preserves full periodic autocorrelation equality. -/ theorem corr_eq_pullback_iff {n : ℕ} [NeZero n] (u : (ZMod n)ˣ) (x y : ZMod n → ℤ) : (∀ k : ZMod n, corr (pullback u x) k = corr (pullback u y) k) ↔ (∀ k : ZMod n, corr x k = corr y k) := by simp_rw [corr_pullback] constructor · intro h k have hk := h ((u⁻¹ : (ZMod n)ˣ) * k) simpa [← mul_assoc] using hk · intro h k exact h ((u : ZMod n) * k) /-- Pulling back by a unit conjugates dihedral + complement symmetries to themselves. -/ theorem dihedralEquiv_pullback_iff {n : ℕ} (u : (ZMod n)ˣ) (x y : ZMod n → ℤ) : DihedralEquiv (pullback u x) (pullback u y) ↔ DihedralEquiv x y := by constructor · rintro ⟨rev, neg, t, h⟩ refine ⟨rev, neg, (u : ZMod n) * t, ?_⟩ intro i have hi := h ((u⁻¹ : (ZMod n)ˣ) * i) simp only [pullback] at hi have hmul : (u : ZMod n) * ((u⁻¹ : (ZMod n)ˣ) * i) = i := by rw [← mul_assoc]; simp rw [hmul] at hi cases rev with | false => simp only [Bool.false_eq_true, ↓reduceIte] at hi ⊢ rw [mul_add, hmul] at hi exact hi | true => simp only [↓reduceIte] at hi ⊢ rw [mul_sub, hmul] at hi exact hi · rintro ⟨rev, neg, t, h⟩ refine ⟨rev, neg, (u⁻¹ : (ZMod n)ˣ) * t, ?_⟩ intro i have hi := h ((u : ZMod n) * i) simp only [pullback] cases rev with | false => simp only [Bool.false_eq_true, ↓reduceIte] at hi ⊢ rw [mul_add, ← mul_assoc] simpa using hi | true => simp only [↓reduceIte] at hi ⊢ rw [mul_sub, ← mul_assoc] simpa using hi /-- Pullback intertwines `swapAt d` and `swapAt q` whenever `u * q = d`. -/ lemma pullback_swapAt {n : ℕ} (u : (ZMod n)ˣ) {q d : ZMod n} (hu : (u : ZMod n) * q = d) (x : ZMod n → ℤ) : pullback u (swapAt d x) = swapAt q (pullback u x) := by funext i simp only [pullback, swapAt] have h0 : (u : ZMod n) * i = 0 ↔ i = 0 := by constructor · intro h have := congrArg (fun z => ((u⁻¹ : (ZMod n)ˣ) : ZMod n) * z) h simpa [← mul_assoc] using this · intro h; rw [h, mul_zero] have hq : (u : ZMod n) * i = d ↔ i = q := by rw [← hu] constructor · intro h have := congrArg (fun z => ((u⁻¹ : (ZMod n)ˣ) : ZMod n) * z) h simpa [← mul_assoc] using this · intro h; rw [h] simp only [h0, hq, mul_zero, hu] /-- Normalization theorem: a nontrivial opposite-sign swap exists at displacement `d` if and only if one exists at displacement `q = Nat.gcd n d`. -/ theorem hasNontrivialSwap_iff_gcd (n d : ℕ) [NeZero n] : HasNontrivialSwap n d ↔ HasNontrivialSwap n (Nat.gcd n d) := by obtain ⟨u, hu⟩ := exists_unit_mul_gcd_eq n d set q := Nat.gcd n d constructor · rintro ⟨x, hsw, hx0, hxd, hcorr, hneq⟩ refine ⟨pullback u x, (signWord_pullback_iff u x).mpr hsw, ?_, ?_, ?_, ?_⟩ · simp [pullback, hx0] · simp [pullback, hu, hxd] · rw [← pullback_swapAt u hu x] exact (corr_eq_pullback_iff u x (swapAt (d : ZMod n) x)).mpr hcorr · rw [← pullback_swapAt u hu x, dihedralEquiv_pullback_iff] exact hneq · rintro ⟨y, hsw, hy0, hyq, hcorr, hneq⟩ set x := pullback u⁻¹ y have hux : pullback u x = y := by simp [x, pullback_mul, pullback_one] refine ⟨x, (signWord_pullback_iff u⁻¹ y).mpr hsw, ?_, ?_, ?_, ?_⟩ · simp [x, pullback, hy0] · have hu' : ((u⁻¹ : (ZMod n)ˣ) : ZMod n) * (d : ZMod n) = (q : ZMod n) := by rw [← hu, ← mul_assoc]; simp simp [x, pullback, hu', hyq] · rw [← corr_eq_pullback_iff u, pullback_swapAt u hu x, hux] exact hcorr · rw [← dihedralEquiv_pullback_iff u, pullback_swapAt u hu x, hux] exact hneq #print axioms exists_unit_mul_gcd_eq #print axioms corr_eq_pullback_iff #print axioms dihedralEquiv_pullback_iff #print axioms hasNontrivialSwap_iff_gcd end Action end SwapHomometry