This journal is in preparation. Submissions are open as a pilot and the editorial board is being formed. Articles marked Demonstration show the format only.
Journal of Machine-Verified MathematicsPeer-reviewed mathematics with machine-checked proofs, for the age of AI

E424/RoughElements/L6.lean

All files · 238 lines · source at the verified commit

1module
2
3public import E424.Setup
4
5@[expose] public section
6
7/-!
8# Section 3 of the outline: L6
9
10Words of affine maps `x ↦ a x - 1` with `a ∈ {2, 3}`. A block `[3, 2]` is `U1 : x ↦ 6x - 3`, a block
11`[2, 3]` is `V1 : x ↦ 6x - 4`. A word of `k` blocks has slope `6^k`; its offset is read off the exponents.
12-/
13
14set_option linter.style.header false
15open Finset
16
17namespace E424
18
19open Inp (evalInt evalInt_cons eval_cast_eq_evalInt Letter)
20
21/-- Offset digit of a block: `3` for `U1`, `4` for `V1`. -/
22def L6c (b : Bool) : ℕ := if b then 4 else 3
23
24/-- `L6word bb n`: `n` blocks. Exponent `e` carries the block `bb e`; exponent `0` is the outermost
25block, applied last. -/
26def L6word (bb : ℕ → Bool) : ℕ → List ℕ
27 | 0 => []
28 | n + 1 => L6word (fun e => bb (e + 1)) n ++ (if bb 0 then [2, 3] else [3, 2])
29
30theorem evalInt_append (w v : List ℕ) (x : ℤ) :
31 evalInt (w ++ v) x = evalInt v (evalInt w x) := by
32 induction w generalizing x with
33 | nil => rfl
34 | cons a w ih => simp only [List.cons_append, evalInt_cons, ih]
35
36theorem L6word_letters (bb : ℕ → Bool) (n : ℕ) : ∀ a ∈ L6word bb n, a = 2 ∨ a = 3 := by
37 induction n generalizing bb with
38 | zero => simp [L6word]
39 | succ n ih =>
40 intro a ha
41 simp only [L6word, List.mem_append] at ha
42 rcases ha with ha | ha
43 · exact ih _ a ha
44 · cases h : bb 0 <;> simp [h] at ha <;> omega
45
46/-- Closed form of the affine map of a word. -/
47theorem L6word_evalInt (n : ℕ) : ∀ (bb : ℕ → Bool) (x : ℤ),
48 evalInt (L6word bb n) x = 6 ^ n * x - ∑ e ∈ range n, (L6c (bb e) : ℤ) * 6 ^ e := by
49 induction n with
50 | zero => intro bb x; simp [L6word, evalInt]
51 | succ n ih =>
52 intro bb x
53 have hS : ∑ k ∈ range n, (L6c (bb (k + 1)) : ℤ) * 6 ^ (k + 1)
54 = 6 * ∑ k ∈ range n, (L6c (bb (k + 1)) : ℤ) * 6 ^ k := by
55 rw [Finset.mul_sum]; exact Finset.sum_congr rfl fun k _ => by ring
56 rw [L6word, evalInt_append, ih, Finset.sum_range_succ', hS]
57 cases h : bb 0 <;> simp [evalInt, L6c, h] <;> ring
58
59/-- The number of blocks: the exponents `e < P φ(P) + 2` carry the blocks. -/
60def L6k (P : ℕ) : ℕ := P * Nat.totient P + 2
61
62/-- Positions `e - 2` of the tail that carry `V1`: `t` multiples of `φ(P)`. Since `6^{2 + mφ} ≡ 36 mod P`,
63choosing `t` of them shifts the offset by `36 t`. -/
64def L6T (P t : ℕ) : Finset ℕ := (range t).image (fun m => m * Nat.totient P)
65
66/-- Block choice at exponent `e`: exponent `0` is `top`, exponent `1` is `V1`, and the tail is `V1` exactly on
67`L6T P t` (shifted by 2). -/
68def L6bb (P : ℕ) (top : Bool) (t e : ℕ) : Bool :=
69 if e = 0 then top else if e = 1 then true else decide (e - 2 ∈ L6T P t)
70
71/-- The offset of the word `L6word (L6bb P top t) (L6k P)`: it evaluates to `6^k x - L6off`. -/
72def L6off (P : ℕ) (top : Bool) (t : ℕ) : ℕ :=
73 ∑ e ∈ range (L6k P), L6c (L6bb P top t e) * 6 ^ e
74
75theorem L6c_le (b : Bool) : L6c b ≤ 4 := by cases b <;> simp [L6c]
76
77/-- Digit sums with digits at most `4` stay below `6^n`. -/
78theorem sum_digits_lt (c : ℕ → ℕ) (hc : ∀ e, c e ≤ 4) (n : ℕ) :
79 5 * ∑ e ∈ range n, c e * 6 ^ e + 4 ≤ 4 * 6 ^ n := by
80 induction n with
81 | zero => simp
82 | succ n ih =>
83 rw [Finset.sum_range_succ, pow_succ]
84 have h : c n * 6 ^ n ≤ 4 * 6 ^ n := Nat.mul_le_mul_right _ (hc n)
85 nlinarith
86
87theorem L6off_lt (P : ℕ) (top : Bool) (t : ℕ) : L6off P top t < 6 ^ L6k P := by
88 have h := sum_digits_lt (fun e => L6c (L6bb P top t e)) (fun e => L6c_le _) (L6k P)
89 have hpos : 0 < 6 ^ L6k P := by positivity
90 unfold L6off
91 omega
92
93theorem L6word_letters' (bb : ℕ → Bool) (n : ℕ) : ∀ a ∈ L6word bb n, Letter a := by
94 intro a ha
95 rcases L6word_letters bb n a ha with rfl | rfl <;> simp [Letter]
96
97/-- Solve `36 t + a = 0` in `ZMod P` for some `t < P`, as `36` is a unit mod `P`. -/
98theorem L6_solve (P : ℕ) [NeZero P] (h36 : Nat.Coprime 36 P) (a : ZMod P) :
99 ∃ t < P, 36 * (t : ZMod P) + a = 0 := by
100 set u : (ZMod P)ˣ := ZMod.unitOfCoprime 36 h36
101 have hu : (u : ZMod P) = 36 := by
102 rw [ZMod.coe_unitOfCoprime]; norm_num
103 refine ⟨((-a) * ((u⁻¹ : (ZMod P)ˣ) : ZMod P)).val, ZMod.val_lt _, ?_⟩
104 rw [ZMod.natCast_zmod_val, ← hu]
105 linear_combination (-a) * Units.mul_inv u
106
107/-- Offset split: the head is the two outer blocks, the tail is `V1` exactly on `L6T P t`. -/
108theorem L6off_split (P t : ℕ) (top : Bool) (hφ : 0 < Nat.totient P) (ht : t ≤ P) :
109 L6off P top t = L6c top + 6 * 4 + 3 * ∑ e ∈ range (P * Nat.totient P), 6 ^ (e + 2)
110 + ∑ m ∈ range t, 6 ^ (m * Nat.totient P + 2) := by
111 have hsub : L6T P t ⊆ range (P * Nat.totient P) := by
112 intro e he
113 obtain ⟨m, hm, rfl⟩ := Finset.mem_image.1 he
114 have hm' : m < P := by rw [Finset.mem_range] at hm; omega
115 rw [Finset.mem_range]
116 nlinarith
117 have htail : ∑ e ∈ range (P * Nat.totient P), L6c (L6bb P top t (e + 2)) * 6 ^ (e + 2)
118 = 3 * ∑ e ∈ range (P * Nat.totient P), 6 ^ (e + 2)
119 + ∑ m ∈ range t, 6 ^ (m * Nat.totient P + 2) := by
120 have hterm : ∀ e, L6c (L6bb P top t (e + 2)) * 6 ^ (e + 2)
121 = 3 * 6 ^ (e + 2) + if e ∈ L6T P t then 6 ^ (e + 2) else 0 := by
122 intro e
123 by_cases he : e ∈ L6T P t <;> simp [L6bb, L6c, he] <;> ring
124 rw [Finset.sum_congr rfl (fun e _ => hterm e), Finset.sum_add_distrib, ← Finset.mul_sum,
125 Finset.sum_ite_mem, Finset.inter_eq_right.2 hsub, L6T,
126 Finset.sum_image (fun x _ y _ hxy => Nat.eq_of_mul_eq_mul_right hφ hxy)]
127 unfold L6off
128 rw [L6k, show P * Nat.totient P + 2 = (P * Nat.totient P + 1) + 1 from rfl, Finset.sum_range_succ',
129 Finset.sum_range_succ']
130 simp only [show ∀ e, e + 1 + 1 = e + 2 from fun e => rfl]
131 rw [htail]
132 have h1 : L6c (L6bb P top t 1) = 4 := by simp [L6bb, L6c]
133 have h0 : L6c (L6bb P top t 0) = L6c top := by simp [L6bb]
134 rw [h1, h0]
135 ring
136
137/-- **Lemmas 5 and 6.** For `P` coprime to 6 there is one length `k` such that for both residues
138`r ∈ {0, 8}` mod 9 the words `w_i` in `U1 : x ↦ 6x - 3`, `V1 : x ↦ 6x - 4` (with offsets `o i`)
139send `x ≡ i mod P` to multiples of `P`, and everything to the class `r` mod 9. -/
140theorem L6 (P : ℕ) (hP : P.Coprime 6) (hP0 : 0 < P) : ∃ k ≥ 2, ∀ r : ℕ, r = 0 ∨ r = 8 →
141 ∃ o : ℕ → ℕ, (∀ i, o i < 6 ^ k) ∧
142 (∀ x ∈ A, 14 < x → 6 ^ k * x - o (x % P) ∈ A) ∧
143 ∀ x, 1 ≤ x → P ∣ 6 ^ k * x - o (x % P) ∧ (6 ^ k * x - o (x % P)) % 9 = r := by
144 have hφ : 0 < Nat.totient P := Nat.totient_pos.2 hP0
145 refine ⟨L6k P, by unfold L6k; omega, ?_⟩
146 intro r hr
147 have : NeZero P := ⟨hP0.ne'⟩
148 have hcop6 : Nat.Coprime 6 P := hP.symm
149 have h36 : Nat.Coprime 36 P := by
150 have := Nat.Coprime.pow_left 2 hcop6
151 norm_num at this
152 exact this
153 -- Fermat: `6^φ(P) ≡ 1 mod P`, so `6^{mφ + 2} ≡ 36`
154 have hpow : (6 : ZMod P) ^ Nat.totient P = 1 := by
155 have h := (ZMod.natCast_eq_natCast_iff _ _ _).2 (Nat.ModEq.pow_totient hcop6)
156 push_cast at h
157 exact h
158 have hpow2 : ∀ m : ℕ, (6 : ZMod P) ^ (m * Nat.totient P + 2) = 36 := by
159 intro m
160 rw [pow_add, mul_comm m, pow_mul, hpow, one_pow]
161 norm_num
162 have h9sq : (9 : ℕ) ∣ 6 ^ 2 := by norm_num
163 set top : Bool := decide (r = 8) with htop
164 -- cast of the offset: every tail `V1` adds `36` mod P
165 have hcast : ∀ t, t ≤ P → ((L6off P top t : ℕ) : ZMod P) = (L6c top : ZMod P) + 6 * 4
166 + 3 * ∑ e ∈ range (P * Nat.totient P), (6 : ZMod P) ^ (e + 2) + 36 * (t : ZMod P) := by
167 intro t ht
168 rw [L6off_split P t top hφ ht]
169 push_cast
170 simp only [hpow2, Finset.sum_const, Finset.card_range, nsmul_eq_mul]
171 ring
172 -- for each residue `i`, a tail length `t` with `offset ≡ 6^k i mod P`
173 have hex : ∀ i : ℕ, ∃ t < P, ((L6off P top t : ℕ) : ZMod P) = (6 : ZMod P) ^ L6k P * (i : ZMod P) := by
174 intro i
175 obtain ⟨t, ht, h⟩ := L6_solve P h36 ((L6c top : ZMod P) + 6 * 4
176 + 3 * ∑ e ∈ range (P * Nat.totient P), (6 : ZMod P) ^ (e + 2) - 6 ^ L6k P * (i : ZMod P))
177 refine ⟨t, ht, ?_⟩
178 rw [hcast t ht.le]
179 linear_combination h
180 choose τ hτP hτ using hex
181 have hle : ∀ i x : ℕ, 1 ≤ x → L6off P top (τ i) ≤ 6 ^ L6k P * x := by
182 intro i x hx
183 have h1 := L6off_lt P top (τ i)
184 have h2 : 6 ^ L6k P ≤ 6 ^ L6k P * x := Nat.le_mul_of_pos_right _ hx
185 omega
186 refine ⟨fun i => L6off P top (τ i), fun i => L6off_lt P top (τ i), ?_, ?_⟩
187 · -- membership: the word `L6word` is a word in the letters, so `L2` applies
188 intro x hxA hx14
189 dsimp only
190 have hW := L2 hxA hx14 (L6word (L6bb P top (τ (x % P))) (L6k P)) (L6word_letters' _ _)
191 have hev : Inp.eval (L6word (L6bb P top (τ (x % P))) (L6k P)) x
192 = 6 ^ L6k P * x - L6off P top (τ (x % P)) := by
193 have h1 := eval_cast_eq_evalInt (L6word (L6bb P top (τ (x % P))) (L6k P)) (x := x)
194 (by omega) (fun a ha => by
195 rcases L6word_letters _ _ a ha with rfl | rfl <;> norm_num)
196 rw [L6word_evalInt] at h1
197 have h2 : ((6 ^ L6k P * x - L6off P top (τ (x % P)) : ℕ) : ℤ)
198 = 6 ^ L6k P * (x : ℤ) - ∑ e ∈ range (L6k P), (L6c (L6bb P top (τ (x % P)) e) : ℤ) * 6 ^ e := by
199 rw [Nat.cast_sub (hle _ _ (by omega))]
200 unfold L6off
201 push_cast
202 ring
203 exact_mod_cast h1.trans h2.symm
204 rw [← hev]
205 exact hW
206 · -- congruences: `x ≡ i mod P` gives `6^k x ≡ 6^k i ≡ offset`, and mod 9 only the outer digits survive
207 intro x hx1
208 dsimp only
209 have hxi : (x : ZMod P) = ((x % P : ℕ) : ZMod P) := (ZMod.natCast_mod x P).symm
210 have h9k : 9 ∣ 6 ^ L6k P * x := by
211 refine Dvd.dvd.mul_right ?_ x
212 unfold L6k
213 rw [pow_add]
214 exact Dvd.dvd.mul_left h9sq _
215 have hdiv : 9 ∣ 3 * ∑ e ∈ range (P * Nat.totient P), 6 ^ (e + 2)
216 + ∑ m ∈ range (τ (x % P)), 6 ^ (m * Nat.totient P + 2) := by
217 refine dvd_add (Dvd.dvd.mul_left (Finset.dvd_sum fun e _ => ?_) 3)
218 (Finset.dvd_sum fun m _ => ?_)
219 · rw [pow_add]; exact Dvd.dvd.mul_left h9sq _
220 · rw [pow_add]; exact Dvd.dvd.mul_left h9sq _
221 have hoff9 : L6off P top (τ (x % P)) % 9 = (L6c top + 24) % 9 := by
222 rw [L6off_split P _ top hφ (hτP _).le]
223 omega
224 constructor
225 · rw [← ZMod.natCast_eq_zero_iff, Nat.cast_sub (hle _ _ hx1), Nat.cast_mul, Nat.cast_pow]
226 push_cast
227 rw [hxi, hτ]
228 ring
229 · rcases hr with rfl | rfl
230 · have hc : L6c top = 3 := by simp [L6c, htop]
231 have := hle (x % P) x hx1
232 omega
233 · have hc : L6c top = 4 := by simp [L6c, htop]
234 have := hle (x % P) x hx1
235 omega
236
237end E424
238