The proof
Source
Main.lean · 3727 lines · 174.7 kB
Showing the first 500 of 3727 lines. The whole file is 174.7 kB; download it to read the rest.
/-!
# Erdős problem 156: a small maximal Sidon set
This file proves the Conjectures.io target `Bounty.target` using finite
sign-pattern selection, quadratic Sidon sets, two separated covering alphabets,
periodic tagged anchors, Bertrand's postulate, and maximal completion.
Upload this file as the proof payload. The site supplies the trusted imports
and the enclosing Bounty namespace. All auxiliary proofs are included below.
-/
namespace SignPatterns
open Finset
variable {ι V : Type*} [Fintype ι] [DecidableEq ι]
variable [AddCommGroup V] [Module ℝ V]
omit [DecidableEq ι] in
theorem independent_of_all_patterns (f : ι → Module.Dual ℝ V)
(hpatterns : ∀ S : Finset ι, ∃ x : V, ∀ i, 0 < f i x ↔ i ∈ S) :
LinearIndependent ℝ f := by
classical
have hnonpos : ∀ g : ι → ℝ, ∑ i, g i • f i = 0 → ∀ i, g i ≤ 0 := by
intro g hg i
by_contra! hi
let S := univ.filter fun j => 0 < g j
obtain ⟨x, hx⟩ := hpatterns S
have hterms : ∀ j ∈ (univ : Finset ι), 0 ≤ g j * f j x := by
intro j _
by_cases hj : 0 < g j
· exact (mul_pos hj ((hx j).mpr (mem_filter.mpr ⟨mem_univ _, hj⟩))).le
· have hfx : f j x ≤ 0 := by
by_contra! hfx
exact hj (mem_filter.mp ((hx j).mp hfx)).2
exact mul_nonneg_of_nonpos_of_nonpos (le_of_not_gt hj) hfx
have hip : 0 < g i * f i x :=
mul_pos hi ((hx i).mpr (mem_filter.mpr ⟨mem_univ _, hi⟩))
have hsum : 0 < ∑ j, g j * f j x :=
sum_pos' hterms ⟨i, mem_univ _, hip⟩
have heval := congrArg (fun L : Module.Dual ℝ V => L x) hg
simp only [LinearMap.sum_apply, LinearMap.smul_apply, smul_eq_mul,
LinearMap.zero_apply] at heval
linarith
apply Fintype.linearIndependent_iff.mpr
intro g hg i
have hneg : ∑ j, (-g j) • f j = 0 := by
have hterm : ∀ j, (-g j) • f j = -(g j • f j) := fun j => neg_smul (g j) (f j)
simp_rw [hterm]
rw [sum_neg_distrib, hg, neg_zero]
have h₁ := hnonpos g hg i
have h₂ := hnonpos (fun j => -g j) hneg i
linarith
noncomputable def positiveFamily (f : ι → Module.Dual ℝ V) : Finset (Finset ι) := by
classical
exact univ.filter fun S => ∃ x : V, ∀ i, 0 < f i x ↔ i ∈ S
omit [DecidableEq ι] in
theorem mem_positiveFamily {f : ι → Module.Dual ℝ V} {S : Finset ι} :
S ∈ positiveFamily f ↔ ∃ x : V, ∀ i, 0 < f i x ↔ i ∈ S := by
classical
simp [positiveFamily]
theorem independent_of_shattered {f : ι → Module.Dual ℝ V} {S : Finset ι}
(hS : (positiveFamily f).Shatters S) :
LinearIndependent ℝ (fun i : S => f i) := by
classical
apply independent_of_all_patterns
intro T
let T' := T.image (fun i : S => (i : ι))
have hsub : T' ⊆ S := by
intro i hi
obtain ⟨j, _, rfl⟩ := mem_image.mp hi
exact j.property
obtain ⟨U, hU, heq⟩ := hS hsub
obtain ⟨x, hx⟩ := mem_positiveFamily.mp hU
refine ⟨x, fun i => ?_⟩
have himem : (i : ι) ∈ T' ↔ i ∈ T := by
simp [T', mem_image]
have hiU : (i : ι) ∈ U ↔ (i : ι) ∈ T' := by
have h := congrArg (fun A : Finset ι => (i : ι) ∈ A) heq
simpa only [mem_inter, i.property, true_and] using Iff.of_eq h
exact (hx i).trans (hiU.trans himem)
theorem positiveFamily_vcDim_le [Module.Finite ℝ V] (f : ι → Module.Dual ℝ V) :
(positiveFamily f).vcDim ≤ Module.finrank ℝ V := by
classical
apply Finset.sup_le
intro S hS
have hi := independent_of_shattered (mem_shatterer.mp hS)
have hc := hi.fintype_card_le_finrank
simpa only [Fintype.card_coe, Subspace.dual_finrank_eq] using hc
theorem family_card_le_of_vcDim_le (F : Finset (Finset ι)) {d : ℕ} (hd : F.vcDim ≤ d) :
F.card ≤ (d + 1) * (Fintype.card ι + 1) ^ d := by
calc
F.card ≤ F.shatterer.card := card_le_card_shatterer F
_ ≤ ∑ k ∈ Iic F.vcDim, (Fintype.card ι).choose k := card_shatterer_le_sum_vcDim
_ ≤ ∑ _k ∈ Iic F.vcDim, (Fintype.card ι + 1) ^ d := by
apply sum_le_sum
intro k hk
have hkd : k ≤ d := (mem_Iic.mp hk).trans hd
calc
(Fintype.card ι).choose k ≤ (Fintype.card ι) ^ k := Nat.choose_le_pow _ _
_ ≤ (Fintype.card ι + 1) ^ k := Nat.pow_le_pow_left (by omega) k
_ ≤ (Fintype.card ι + 1) ^ d := Nat.pow_le_pow_right (by omega) hkd
_ = (F.vcDim + 1) * (Fintype.card ι + 1) ^ d := by simp
_ ≤ (d + 1) * (Fintype.card ι + 1) ^ d := by gcongr
theorem positiveFamily_card_le [Module.Finite ℝ V] (f : ι → Module.Dual ℝ V) :
(positiveFamily f).card ≤
(Module.finrank ℝ V + 1) * (Fintype.card ι + 1) ^ (Module.finrank ℝ V) :=
family_card_le_of_vcDim_le _ (positiveFamily_vcDim_le f)
noncomputable def positiveSet (f : ι → Module.Dual ℝ V) (x : V) : Finset ι := by
classical
exact univ.filter fun i => 0 < f i x
omit [DecidableEq ι] in
theorem positiveSet_mem (f : ι → Module.Dual ℝ V) (x : V) :
positiveSet f x ∈ positiveFamily f := by
classical
apply mem_positiveFamily.mpr
exact ⟨x, fun i => by simp [positiveSet]⟩
/-- Finite labels determined by signs have a polynomial cardinality bound, including zeros. -/
theorem sign_determined_card_le [Module.Finite ℝ V] {X α : Type*} [DecidableEq α]
(f : ι → Module.Dual ℝ V) (embed : X → V) (label : X → α) (F : Finset α)
(hrealize : ∀ a ∈ F, ∃ x : X, label x = a)
(hdet : ∀ x y, (∀ i,
(0 < f i (embed x) ↔ 0 < f i (embed y)) ∧
(f i (embed x) < 0 ↔ f i (embed y) < 0)) → label x = label y) :
F.card ≤ ((Module.finrank ℝ V + 1) *
(Fintype.card ι + 1) ^ (Module.finrank ℝ V)) ^ 2 := by
classical
let rep (a : F) : X := Classical.choose (hrealize a a.property)
have hrep (a : F) : label (rep a) = a := Classical.choose_spec (hrealize a a.property)
let g : F → (positiveFamily f) × (positiveFamily (fun i => -f i)) := fun a =>
(⟨positiveSet f (embed (rep a)), positiveSet_mem _ _⟩,
⟨positiveSet (fun i => -f i) (embed (rep a)), positiveSet_mem _ _⟩)
have hginj : Function.Injective g := by
intro a b hab
have hpos : positiveSet f (embed (rep a)) = positiveSet f (embed (rep b)) :=
congrArg (fun z : (positiveFamily f) × (positiveFamily (fun i => -f i)) => z.1.val) hab
have hneg : positiveSet (fun i => -f i) (embed (rep a)) =
positiveSet (fun i => -f i) (embed (rep b)) :=
congrArg (fun z : (positiveFamily f) × (positiveFamily (fun i => -f i)) => z.2.val) hab
apply Subtype.ext
rw [← hrep a, ← hrep b]
apply hdet
intro i
constructor
· have h := congrArg (fun S : Finset ι => i ∈ S) hpos
simpa [positiveSet] using Iff.of_eq h
· have h := congrArg (fun S : Finset ι => i ∈ S) hneg
simpa [positiveSet] using Iff.of_eq h
have hcard : F.card ≤ (positiveFamily f).card * (positiveFamily (fun i => -f i)).card := by
simpa only [Fintype.card_prod, Fintype.card_coe] using Fintype.card_le_of_injective g hginj
exact hcard.trans (by
simpa only [pow_two] using Nat.mul_le_mul (positiveFamily_card_le f)
(positiveFamily_card_le (fun i => -f i)))
def augmentForm (a : Module.Dual ℝ V) (c : ℝ) : Module.Dual ℝ (ℝ × V) :=
c • LinearMap.fst ℝ ℝ V + a.comp (LinearMap.snd ℝ ℝ V)
theorem augmentForm_apply (a : Module.Dual ℝ V) (c : ℝ) (x : V) :
augmentForm a c (1, x) = c + a x := by
simp [augmentForm]
/-- An explicit uniform bound for labels determined by finitely many affine signs. -/
theorem affine_sign_determined_card_le [Module.Finite ℝ V] {X α : Type*} [DecidableEq α]
(a : ι → Module.Dual ℝ V) (c : ι → ℝ) (embed : X → V) (label : X → α) (F : Finset α)
(hrealize : ∀ y ∈ F, ∃ x : X, label x = y)
(hdet : ∀ x y, (∀ i,
(0 < c i + a i (embed x) ↔ 0 < c i + a i (embed y)) ∧
(c i + a i (embed x) < 0 ↔ c i + a i (embed y) < 0)) → label x = label y) :
F.card ≤ ((Module.finrank ℝ V + 2) *
(Fintype.card ι + 1) ^ (Module.finrank ℝ V + 1)) ^ 2 := by
have h := sign_determined_card_le (fun i => augmentForm (a i) (c i))
(fun x => (1, embed x)) label F hrealize (by simpa only [augmentForm_apply] using hdet)
simpa [Module.finrank_prod, add_assoc, add_comm, add_left_comm] using h
theorem floor_mem_cut_interval {B : ℕ} {x : ℝ}
(hx : -(B : ℝ) ≤ x ∧ x ≤ B) : ⌊x⌋ ∈ Icc (-(B : ℤ)) B := by
apply mem_Icc.mpr
constructor
· apply Int.le_floor.mpr
simpa using hx.1
· apply Int.floor_le_iff.mpr
push_cast
linarith [hx.2]
theorem floor_eq_of_integer_cuts {B : ℕ} {x y : ℝ}
(hx : -(B : ℝ) ≤ x ∧ x ≤ B) (hy : -(B : ℝ) ≤ y ∧ y ≤ B)
(hcuts : ∀ k ∈ Icc (-(B : ℤ)) B, (x - k < 0 ↔ y - k < 0)) :
⌊x⌋ = ⌊y⌋ := by
apply le_antisymm
· apply Int.le_floor.mpr
have hcut := hcuts ⌊x⌋ (floor_mem_cut_interval hx)
have hfloor := Int.floor_le x
by_contra! h
have hycut : y - (⌊x⌋ : ℝ) < 0 := by linarith
have := hcut.mpr hycut
linarith
· apply Int.le_floor.mpr
have hcut := hcuts ⌊y⌋ (floor_mem_cut_interval hy)
have hfloor := Int.floor_le y
by_contra! h
have hxcut : x - (⌊y⌋ : ℝ) < 0 := by linarith
have := hcut.mp hxcut
linarith
theorem fract_lt_iff_of_integer_cuts {B : ℕ} {x y u v : ℝ}
(hx : -(B : ℝ) ≤ x ∧ x ≤ B) (hy : -(B : ℝ) ≤ y ∧ y ≤ B)
(hcuts : ∀ k ∈ Icc (-(B : ℤ)) B, (x - k < 0 ↔ y - k < 0))
(hcomp : ∀ k ∈ Icc (-(B : ℤ)) B, (x - u - k < 0 ↔ y - v - k < 0)) :
Int.fract x < u ↔ Int.fract y < v := by
have hfloor := floor_eq_of_integer_cuts hx hy hcuts
have h := hcomp ⌊x⌋ (floor_mem_cut_interval hx)
change x - (⌊x⌋ : ℝ) < u ↔ y - (⌊y⌋ : ℝ) < v
rw [← hfloor]
constructor
· intro hx'
have hx'' : x - u - (⌊x⌋ : ℝ) < 0 := by linarith
have hy' := h.mp hx''
linarith
· intro hy'
have hy'' : y - v - (⌊x⌋ : ℝ) < 0 := by linarith
have hx' := h.mpr hy''
linarith
/-- Fractional affine phases with a bounded range still have polynomially many labels. -/
theorem fractional_phase_card_le [Module.Finite ℝ V] {X α : Type*} [DecidableEq α]
(f g : ι → Module.Dual ℝ V) (cf cg : ι → ℝ)
(embed : X → V) (label : X → α) (F : Finset α) (B : ℕ)
(hbound : ∀ x i, -(B : ℝ) ≤ cf i + f i (embed x) ∧ cf i + f i (embed x) ≤ B)
(hrealize : ∀ y ∈ F, ∃ x : X, label x = y)
(hdet : ∀ x y, (∀ i,
(Int.fract (cf i + f i (embed x)) < cg i + g i (embed x) ↔
Int.fract (cf i + f i (embed y)) < cg i + g i (embed y))) → label x = label y) :
F.card ≤ ((Module.finrank ℝ V + 2) *
(2 * Fintype.card ι * (2 * B + 1) + 1) ^ (Module.finrank ℝ V + 1)) ^ 2 := by
classical
let J := Bool × ι × ↥(Icc (-(B : ℤ)) B)
let af : J → Module.Dual ℝ V := fun j => if j.1 then f j.2.1 - g j.2.1 else f j.2.1
let ac : J → ℝ := fun j =>
if j.1 then cf j.2.1 - cg j.2.1 - (j.2.2 : ℤ) else cf j.2.1 - (j.2.2 : ℤ)
have hdet' : ∀ x y, (∀ j : J,
(0 < ac j + af j (embed x) ↔ 0 < ac j + af j (embed y)) ∧
(ac j + af j (embed x) < 0 ↔ ac j + af j (embed y) < 0)) → label x = label y := by
intro x y hsign
apply hdet
intro i
apply fract_lt_iff_of_integer_cuts (hbound x i) (hbound y i)
· intro k hk
have h := (hsign (false, i, ⟨k, hk⟩)).2
simp only [af, ac, Bool.false_eq_true, ite_false] at h
constructor
· intro h'
have hh := h.mp (by linarith)
linarith
· intro h'
have hh := h.mpr (by linarith)
linarith
· intro k hk
have h := (hsign (true, i, ⟨k, hk⟩)).2
simp only [af, ac, ite_true, LinearMap.sub_apply] at h
constructor
· intro h'
have hh := h.mp (by linarith)
linarith
· intro h'
have hh := h.mpr (by linarith)
linarith
have hc := affine_sign_determined_card_le af ac embed label F hrealize hdet'
have hcutcard : Fintype.card ↥(Icc (-(B : ℤ)) B) = 2 * B + 1 := by
rw [Fintype.card_coe, Int.card_Icc]
omega
have hJ : Fintype.card J = 2 * Fintype.card ι * (2 * B + 1) := by
simp [J, hcutcard, mul_assoc]
simpa only [hJ] using hc
end SignPatterns
section
open Finset Filter
/-- Finite union-bound form of the manuscript's random-word argument. -/
theorem exists_hitting_word {m b : ℕ} (patterns : Finset (Fin m → Finset (Fin b)))
(hne : ∀ G ∈ patterns, ∀ i, (G i).Nonempty)
(hsize : patterns.card * (b - 1) ^ m < b ^ m) :
∃ w : Fin m → Fin b, ∀ G ∈ patterns, ∃ i, w i ∈ G i := by
classical
let bad (G : Fin m → Finset (Fin b)) : Finset (Fin m → Fin b) :=
Fintype.piFinset fun i => univ \ G i
have hbad (G) (hG : G ∈ patterns) : (bad G).card ≤ (b - 1) ^ m := by
rw [Fintype.card_piFinset]
calc
_ ≤ ∏ _ : Fin m, (b - 1) := by
apply Finset.prod_le_prod
intro i _
have hi : 1 ≤ (G i).card := card_pos.mpr (hne G hG i)
simp only [card_sdiff_of_subset (subset_univ _), card_univ, Fintype.card_fin]
omega
_ = (b - 1) ^ m := by simp
let excluded := patterns.biUnion bad
have hexcluded : excluded.card < Fintype.card (Fin m → Fin b) := by
calc
excluded.card ≤ ∑ G ∈ patterns, (bad G).card := card_biUnion_le
_ ≤ ∑ _G ∈ patterns, (b - 1) ^ m := sum_le_sum hbad
_ = patterns.card * (b - 1) ^ m := by simp
_ < b ^ m := hsize
_ = Fintype.card (Fin m → Fin b) := by simp
have houtside : ∃ w : Fin m → Fin b, w ∉ excluded := by
by_contra! h
have hu : (univ : Finset (Fin m → Fin b)) ⊆ excluded := fun w _ => h w
have := card_le_card hu
simp only [card_univ] at this
omega
obtain ⟨w, hw⟩ := houtside
refine ⟨w, ?_⟩
intro G hG
by_contra! h
apply hw
apply mem_biUnion.mpr
refine ⟨G, hG, Fintype.mem_piFinset.mpr ?_⟩
intro i
exact mem_sdiff.mpr ⟨mem_univ _, h i⟩
/-- One word can hit every pattern under each member of a finite set of permutations. -/
theorem exists_hitting_word_permutations {m b : ℕ}
(patterns : Finset (Fin m → Finset (Fin b))) (perms : Finset (Equiv.Perm (Fin m)))
(hne : ∀ G ∈ patterns, ∀ i, (G i).Nonempty)
(hsize : perms.card * patterns.card * (b - 1) ^ m < b ^ m) :
∃ w : Fin m → Fin b, ∀ e ∈ perms, ∀ G ∈ patterns, ∃ i, w (e i) ∈ G i := by
classical
let transformed := (perms ×ˢ patterns).image fun (e, G) i => G (e.symm i)
have hnonempty : ∀ G ∈ transformed, ∀ i, (G i).Nonempty := by
intro G hG i
obtain ⟨⟨e, H⟩, hEH, rfl⟩ := mem_image.mp hG
exact hne H (mem_product.mp hEH).2 (e.symm i)
have hcard : transformed.card ≤ perms.card * patterns.card := by
exact (card_image_le).trans_eq (card_product _ _)
obtain ⟨w, hw⟩ := exists_hitting_word transformed hnonempty
(lt_of_le_of_lt (Nat.mul_le_mul_right ((b - 1) ^ m) hcard) hsize)
refine ⟨w, ?_⟩
intro e he G hG
have hmem : (fun i => G (e.symm i)) ∈ transformed :=
mem_image.mpr ⟨(e, G), mem_product.mpr ⟨he, hG⟩, rfl⟩
obtain ⟨i, hi⟩ := hw _ hmem
exact ⟨e.symm i, by simpa using hi⟩
/-- Every fixed polynomial times `(b - 1)^m` is eventually smaller than `b^m`. -/
theorem polynomial_geometric_eventually_lt (A B b : ℕ) (hb : 1 < b) :
∀ᶠ m : ℕ in atTop, A * m ^ B * (b - 1) ^ m < b ^ m := by
have hnorm : ‖((b - 1 : ℕ) : ℝ)‖ < (b : ℝ) := by
rw [Real.norm_natCast]
exact_mod_cast (by omega : b - 1 < b)
have hsmall :=
(isLittleO_pow_const_mul_const_pow_const_pow_of_norm_lt (R := ℝ) B hnorm).const_mul_left
(A : ℝ)
have hbound := hsmall.bound (by norm_num : (0 : ℝ) < 1 / 2)
filter_upwards [hbound] with m hm
have hle : ((A * m ^ B * (b - 1) ^ m : ℕ) : ℝ) ≤ 1 / 2 * (b : ℝ) ^ m := by
simpa [norm_mul, norm_pow, Real.norm_natCast, Nat.cast_mul, Nat.cast_pow, mul_assoc] using hm
have hpos : (0 : ℝ) < (b : ℝ) ^ m := by
apply pow_pos
exact_mod_cast (by omega : 0 < b)
have hlt : ((A * m ^ B * (b - 1) ^ m : ℕ) : ℝ) < (b : ℝ) ^ m := by linarith
exact_mod_cast hlt
/-- Polynomially many nonempty choice matrices admit a common hitting word. -/
theorem exists_hitting_word_of_polynomial_bound {A B b : ℕ} (hb : 1 < b)
(patterns : (m : ℕ) → Finset (Fin m → Finset (Fin b)))
(hne : ∀ m, ∀ G ∈ patterns m, ∀ i, (G i).Nonempty)
(hcard : ∀ m, (patterns m).card ≤ A * m ^ B) (lower : ℕ) :
∃ m ≥ lower, ∃ w : Fin m → Fin b, ∀ G ∈ patterns m, ∃ i, w i ∈ G i := by
obtain ⟨m, hm, hlower⟩ :=
((polynomial_geometric_eventually_lt A B b hb).and (eventually_ge_atTop lower)).exists
refine ⟨m, hlower, exists_hitting_word (patterns m) (hne m) ?_⟩
exact lt_of_le_of_lt (Nat.mul_le_mul_right ((b - 1) ^ m) (hcard m)) hm
private theorem pair_of_sum_sq {F : Type*} [Field F] {x y z w : F}
(h2 : (2 : F) ≠ 0) (hs : x + y = z + w)
(hq : x ^ 2 + y ^ 2 = z ^ 2 + w ^ 2) :
(x = z ∧ y = w) ∨ (x = w ∧ y = z) := by
have hp : x * y = z * w := by
apply mul_left_cancel₀ h2
linear_combination (x + y + z + w) * hs - hq
have hr : (x - z) * (x - w) = 0 := by
linear_combination x * hs - hp
rcases mul_eq_zero.mp hr with h | h
· left
have hx := sub_eq_zero.mp h
exact ⟨hx, add_left_cancel (hx ▸ hs)⟩
· right
have hx := sub_eq_zero.mp h
refine ⟨hx, ?_⟩
rw [hx, add_comm z w] at hs
exact add_left_cancel hs
/-- The integer representative used for the manuscript's quadratic seed. -/
def quadraticMark (p t : ℕ) : ℕ := t + 2 * p * (t ^ 2 % p)
/-- The seed has one mark for every residue modulo `p`. -/
def quadraticSeed (p : ℕ) : Finset ℕ := (range p).image (quadraticMark p)
private theorem quadraticMark_mod (p t : ℕ) :
quadraticMark p t % (2 * p) = t % (2 * p) := by
simp [quadraticMark, Nat.add_mod]
/-- Equality of seed pair sums determines the unordered pair of parameters. -/
theorem quadraticMark_pair {p i j k l : ℕ} (hp : p.Prime) (hodd : p ≠ 2)
(hi : i < p) (hj : j < p) (hk : k < p) (hl : l < p)
(h : quadraticMark p i + quadraticMark p j =
quadraticMark p k + quadraticMark p l) :
(i = k ∧ j = l) ∨ (i = l ∧ j = k) := by
have hsum : i + j = k + l := by
have hm := congrArg (fun n : ℕ => n % (2 * p)) h
have hpair (a b : ℕ) : (quadraticMark p a + quadraticMark p b) % (2 * p) =
(a + b) % (2 * p) := by
calc
_ = (quadraticMark p a % (2 * p) + quadraticMark p b % (2 * p)) %
(2 * p) := Nat.add_mod _ _ _
_ = (a % (2 * p) + b % (2 * p)) % (2 * p) := by
rw [quadraticMark_mod, quadraticMark_mod]
_ = (a + b) % (2 * p) := (Nat.add_mod _ _ _).symm
have hm' : (i + j) % (2 * p) = (k + l) % (2 * p) := by
rwa [hpair, hpair] at hm
rwa [Nat.mod_eq_of_lt (by omega : i + j < 2 * p),
Nat.mod_eq_of_lt (by omega : k + l < 2 * p)] at hm'
have hres : i ^ 2 % p + j ^ 2 % p = k ^ 2 % p + l ^ 2 % p := by
have hpos := hp.pos
dsimp [quadraticMark] at h
nlinarith
let : Fact p.Prime := ⟨hp⟩
have htwo : (2 : ZMod p) ≠ 0 := by
intro heq
have hd : p ∣ 2 := (ZMod.natCast_eq_zero_iff 2 p).mp heq
have := (Nat.dvd_prime Nat.prime_two).mp hd
rcases this with h | h
· exact hp.ne_one h
· exact hodd h
have hs : (i : ZMod p) + j = (k : ZMod p) + l := by
simpa using congrArg (fun n : ℕ => (n : ZMod p)) hsum
have hq : (i : ZMod p) ^ 2 + (j : ZMod p) ^ 2 =
(k : ZMod p) ^ 2 + (l : ZMod p) ^ 2 := by
have heq := congrArg (fun n : ℕ => (n : ZMod p)) hres
simpa using heq
have hinj : ∀ {a b : ℕ}, a < p → b < p →
(a : ZMod p) = b → a = b := by
intro a b ha hb hab
have heq := (ZMod.natCast_eq_natCast_iff' a b p).mp hab
simpa [Nat.mod_eq_of_lt ha, Nat.mod_eq_of_lt hb] using heq
rcases pair_of_sum_sq htwo hs hq with h | h
· exact Or.inl ⟨hinj hi hk h.1, hinj hj hl h.2⟩
· exact Or.inr ⟨hinj hi hl h.1, hinj hj hk h.2⟩
/-- A field difference lifts to an actual integer difference with two binary carries. -/
theorem quadratic_difference_lift {p d H : ℕ} (hp : p.Prime) (hodd : p ≠ 2)
(hd0 : 0 < d) (hdp : d < p) (hH0 : 0 < H) (hHp : H < p) :
∃ t < p, ∃ u < p, ∃ σ : ℕ, σ ≤ 1 ∧ ∃ ε : ℕ, ε ≤ 1 ∧
(quadraticMark p t : ℤ) - quadraticMark p u =
d - (p : ℤ) * σ + 2 * p * H - 2 * (p : ℤ) ^ 2 * ε := by
let : Fact p.Prime := ⟨hp⟩
have htwo : (2 : ZMod p) ≠ 0 := by
intro heq
have hd : p ∣ 2 := (ZMod.natCast_eq_zero_iff 2 p).mp heq
rcases (Nat.dvd_prime Nat.prime_two).mp hd with h | h
· exact hp.ne_one h
· exact hodd h
have hd : (d : ZMod p) ≠ 0 := by
intro heq
have hm := (ZMod.natCast_eq_natCast_iff' d 0 p).mp (by simpa using heq)
simp only [Nat.mod_eq_of_lt hdp, Nat.zero_mod] at hm
omega
have hden : (2 * (d : ZMod p)) ≠ 0 := mul_ne_zero htwo hd
let t := (((H : ZMod p) + (d : ZMod p) ^ 2) / (2 * d)).val
have ht : t < p := ZMod.val_lt _
have htf : (t : ZMod p) = ((H : ZMod p) + (d : ZMod p) ^ 2) / (2 * d) :=
ZMod.natCast_zmod_val _
have hlin : 2 * (d : ZMod p) * t = H + (d : ZMod p) ^ 2 := by
rw [htf]
field_simp
let σ : ℕ := if d ≤ t then 0 else 1
let u : ℕ := if d ≤ t then t - d else t + p - d
have hσ : σ ≤ 1 := by dsimp [σ]; split_ifs <;> omega
have hu : u < p := by dsimp [u]; split_ifs <;> omega
have husum : u + d = t + p * σ := by
dsimp [u, σ]
split_ifs <;> simp <;> omega
have hsub : (t : ℤ) - u = d - (p : ℤ) * σ := by
have hz : (u : ℤ) + d = t + (p : ℤ) * σ := by exact_mod_cast husum
omega
have huf : (u : ZMod p) = (t : ZMod p) - d := by
apply eq_sub_of_add_eq
have hz := congrArg (fun n : ℕ => (n : ZMod p)) husumProvenance